Location: PHPKode > scripts > Prioritexter > Http.php
<?php
class Http {

  function baseUrl() {
    return $_SERVER['SCRIPT_NAME'];
  }

  function fullUrl() {
    return "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
  }

  function noCacheStr($delimiter="&") {
    return $delimiter . "noCache=" . rand();
  }

  function noCacheUrl() {
    return Http::baseUrl() . Http::noCacheStr("?");
  }

  function formUrl($params=array()) {
    return Http::urlWithParams($params);
  }

  function urlWithParams($params=array()) {
    // this line is a bit controversial... if no default page asked for,
    // just go back to THIS same page.
    Http::ensureBaseParams($params);
    $url = Http::baseUrl() . "?";
    $i=0;
    foreach($params as $key => $value) {
      if ($i>0) $url .= "&";
      $url .= $key ."=$value";
      $i++;
    }
    $url .= "&noCache=" . rand();
    return $url;
  }

  function linkWithParams($link_text="", $params=array(), $class="", 
			  $img_func="", $alt="") {
    $link = "";
    if ($class) $link .= "<font class=\"$class\">";
    $link .= "<a class=\"$class\" href=\""
       . Http::urlWithParams($params)
       . "\">"
       . $link_text;
    if (($img_func) && (is_callable(array("Image",$img_func)))) 
      $link .= call_user_func(Array("Image",$img_func),$alt,2);
    $link .= "</a>";
    if (strlen($class)>0) $link .= "</font>";

    return $link;
  }

  function imageLink($image_func="", $params=array(), $alt="") {
    return Http::linkWithParams("", $params, "", $image_func, $alt);
  }

  function linkInternal($link_text="", $page=HOME_PAGE_FILENAME,
			$class="") {
    $params[PAGE_CGI_VAR_NAME]=$page;
    return Http::linkWithParams($link_text, $params, $class);
  }

  function ensureBaseParams(&$params) {
    // make sure page is in there somewhere!
    if (!isset($params[PAGE_CGI_VAR_NAME])) {
      $page = Http::getCgiVar(PAGE_CGI_VAR_NAME);
      if ($page === FALSE)
	$params[PAGE_CGI_VAR_NAME] = HOME_PAGE_FILENAME;
      else
	$params[PAGE_CGI_VAR_NAME] = $page;	
    }
    // same with old page!
    if (!isset($params[OLD_PAGE_CGI_VAR_NAME])) {
      $oldpage = Http::getCgiVar(PAGE_CGI_VAR_NAME);
      if ($oldpage === FALSE)
	$params[OLD_PAGE_CGI_VAR_NAME] = HOME_PAGE_FILENAME;
      else
	$params[OLD_PAGE_CGI_VAR_NAME] = $oldpage;	
    }
  }

  function openForm($params=array(), $name="default", 
		    $onSubmit="", $is_file=false) {
    Http::ensureBaseParams($params);
    $s = "<form name=form_$name id=form_$name action=\"" 
       . Http::noCacheUrl()
       . "\" method=POST";
    if ($is_file)
      $s .= " enctype=\"multipart/form-data\"";
    if ($onSubmit)
      $s .= " onSubmit=\"". $onSubmit ."\"";
    $s .= ">";
    foreach($params as $key => $value) {
      $s .= "<input type=hidden name=\"". $key ."\" value=\""
	 . $value ."\">";
    }
    return $s;
  }

  function form($params=array(), $name="default",
		$onSubmit="", $is_file=false) {
    $s = Http::openForm($params, $name, $onSubmit, $is_file)
       . "</form>";
    return $s;
  }
  
  function submitFormHtml($name="") {
    return ("document.form_". $name .".submit();");
  }

  function submitFormLink($link_text, $name, $class="bold_text",
			  $img_func="goArrow") {
    $link = "";
    if (strlen($class)>0) $link .= "<font class=\"$class\">";
    $link .= "<a href=\""
       . "javascript: ". Http::submitFormHtml($name)
       . "\">"
       . $link_text
       . call_user_func(Array("Image",$img_func),2)
       . "</a>";
    if (strlen($class)>0) $link .= "</font>";

    return $link;
  }

  function selfContainedSubmit($text="", $params=array()) {
    $s = Http::openForm($params)
       . "<input type=submit class=item_content value=\"$text\">"
       . "</form>";
    return $s;
  }

  function send() {
    global $name, $from, $subject, $message, $mail_dest;
    
    $mail_content = "[This message was received from an online user of ";
    $mail_content .= $_SERVER['SERVER_NAME'] . baseUrl();
    $mail_content .= " ]\n\nFrom:\n $name\n $from\nSubject:\n $subject\n\n";
    $mail_content .= "Message:\n $message";
    
    if (mail($mail_dest, "Email from " . $_SERVER['SERVER_NAME'], $mail_content)) {
      bannerMessage("Your message has been sent.");
    } else {
      bannerMessage("Sorry, your message could not be sent.<br>Please try again.");
    }
  }

  function baseScriptRedirect($GET_params="") {
    $new_loc = "Location: http://" . $_SERVER['SERVER_NAME'] . 
	   $_SERVER['SCRIPT_NAME'];
    if ($GET_params) $new_loc .= "?$GET_params";
    header($new_loc);
  }

  function iHateMagic() {
    if (get_magic_quotes_gpc())
      $_POST = Text::restore($_POST);
  }

  function getCgiVar($var_name="") {
    $val = Http::getPostVar($var_name);
    if ($val === FALSE)
      $val = Http::getGetVar($var_name);
    return $val;
  }

  function getPostVar($post_var_name="") {
    if(sizeof($_POST)>0) {
      foreach($_POST as $key => $this_POST_VAR) {
	if ($key == $post_var_name) {
	  // this restore line had to be put in because it seems that
	  // when POST communicates a value from a previous page to the new
	  // page, it adds escape characters itself... I appreciate it, but
	  // it's annoying because often the string appears with those
	  // escapes showing!!! chimina simina!
	return Text::restore($this_POST_VAR);
	}
      }
    }
    return FALSE;
  }

  function getGetVar($get_var_name="") {
    Debug::debug("looking for get var called $get_var_name...");
    // NOTE: these functs need to be rewritten with array_find or 
    // whatever that's called!
    if(sizeof($_GET)>0) {
      foreach($_GET as $key => $this_GET_VAR) {
	if ($key == $get_var_name) {
	  // needed for same reason given in getPostVar
	  Debug::debug("found $this_GET_VAR");
	  return Text::restore($this_GET_VAR);
	}
      }
    }
    Debug::debug("didn't find $get_var_name...");
    return FALSE;
  }

  // gets all attributes passed called FIELD_anything=value
  // and returns array of them, minus that FIELD_ part.
  function getPostAttributes() {
    $attributes = Array();
    foreach($_POST as $FIELD_key => $val) {
      Debug::debug("post : $FIELD_key is $val");
      if (substr($FIELD_key,0,10) == "FIELD_") {
	$attribute_title = substr($FIELD_key,10);
	$attributes[$attribute_title] = $val;
	Debug::debug("$FIELD_key is valid");
      }
    }
    return $attributes;
  }

  function submitButton($text) {
    $s = "<input type=submit value=\"$text\">";
    return $s;
  }

}
Return current item: Prioritexter