[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/net/http/ -> httpvars.class.php (source)

   1  <?php
   2  
   3      
   4      
   5      /**
   6       * \defgroup Net_HTTP
   7       *
   8       * Package that contains several classes and code related to HTTP, such as code for making
   9       * HTTP requests or code for dealing with certain HTTP headers
  10       */
  11  
  12      /**
  13       * \ingroup Net_HTTP
  14       * 
  15       * HttpVars compatibility package, which allows to fetch some of php's basic
  16       * global variables without having to worry about which version of php we're using.
  17       * The problem here is that since PHP 4.1.0 things like $_REQUEST, $_POST, $_GET, etc
  18       * are available, and before that their equivalents were $HTTP_GET_VARS,
  19       * $HTTP_POST_VARS and so on. By using this package and calling the functions
  20       * getPostVars, getGetVars, getSessionVars/setSessionVars we will get rid of any
  21       * incompatibility with the version of php we are running while having access to the
  22       * variables we most need.
  23       */
  24      class HttpVars  
  25      {
  26  
  27          /**
  28           * Returns an array with all the variables in the GET header, fetching them
  29           * either from $_GET (PHP >= 4.1.0) or $HTTP_GET_VARS (PHP < 4.1.0)
  30           *
  31           * @return An associative array with the values of the GET header.
  32           * @static
  33           */
  34          function getGet()
  35          {
  36              if( phpversion() >= "4.1.0" )
  37                  $getVars = $_GET;
  38              else {
  39                  global $HTTP_GET_VARS;
  40                  $getVars = $HTTP_GET_VARS;
  41              }
  42  
  43              return $getVars;
  44          }
  45  
  46          /**
  47           * Returns an array with all the variables in the GET header, fetching them
  48           * either from $_POST (PHP >= 4.1.0) or $HTTP_POST_VARS (PHP < 4.1.0)
  49           *
  50           * @return An associative array with the values of the POST header.
  51           * @static
  52           */
  53          function getPost()
  54          {
  55              if( phpversion() >= "4.1.0" )
  56                  $postVars = $_POST;
  57              else {
  58                  global $HTTP_POST_VARS;
  59                  $postVars = $HTTP_POST_VARS;
  60              }
  61  
  62              return $postVars;
  63          }
  64  
  65          /**
  66           * Returns an array with all the variables in the session, fetching them
  67           * either from $_SESSION (PHP >= 4.1.0) or $HTTP_SESSION_VARS (PHP < 4.1.0)
  68           *
  69           * @return An associative array with the values of the session.
  70           * @static
  71           */
  72          function getSession()
  73          {
  74              if( phpversion() >= "4.1.0" )
  75                  $sessionVars = $_SESSION;
  76              else {
  77                  global $HTTP_SESSION_VARS;
  78                  $sessionVars = $HTTP_SESSION_VARS;
  79              }
  80  
  81              return $sessionVars;
  82          }
  83  
  84          /**
  85           * Saves the array in the session.
  86           *
  87           * @param sessionVars An array that will be used as the values for the http session.
  88           * @return Always returns true.
  89           * @static
  90           */
  91          function setSession( $sessionVars )
  92          {
  93              //print("saving: ");print_r($sessionVars);
  94  
  95              if( phpversion() >= "4.1.0" ) {
  96                  foreach( $sessionVars as $key => $value ) {
  97                      $_SESSION["$key"] = $value;
  98                  }
  99              }
 100              else {
 101                  global $HTTP_SESSION_VARS;
 102                  foreach( $sessionVars as $key => $value ) {
 103                      $HTTP_SESSION_VARS["$key"] = $value;
 104                  }
 105              }
 106  
 107              return true;
 108          }
 109  
 110          /**
 111           * Returns an array with the contents of the $_COOKIE global variable, if PHP version >= 4.1.0
 112           * or the values of the array HTTP_COOKIE_VARS if we're using a lower version.
 113           *
 114           * @return An associative array with all the cookies created by our application.
 115           * @static
 116           */
 117          function getCookie()
 118          {
 119              if( phpversion() >= "4.1.0" )
 120                  $cookieVars = $_COOKIE;
 121              else {
 122                  global $HTTP_COOKIE_VARS;
 123                  $cookieVars = $HTTP_COOKIE_VARS;
 124              }
 125  
 126              return $cookieVars;
 127          }
 128  
 129          /**
 130           * Returns the value of the $_REQUEST array. In PHP >= 4.1.0 it is defined as a mix
 131           * of the $_POST, $_GET and $_COOKIE arrays, but it didn't exist in earlier versions.
 132           * If we are running PHP < 4.1.0, then we will manually create it by merging the needed
 133           * arrays.
 134           *
 135           * @return An associative array containing the variables in the GET, POST and COOKIES header.
 136           * @static
 137           */
 138          function getRequest()
 139          {
 140              if( phpversion() >= "4.1.0" )
 141                  $requestVars = $_REQUEST;
 142              else {
 143                  $postVars = HttpVars::getPost();
 144                  $getVars  = HttpVars::getGet();
 145                  $cookieVars = HttpVars::getCookie();
 146  
 147                  $requestVars = array_merge( $getVars, $postVars, $cookieVars );
 148              }
 149  
 150              return $requestVars;
 151          }
 152          
 153          /**
 154           * returns the value of a certain key from the request
 155           *
 156           * @param key
 157           * @return The value, or empty if not found
 158           */
 159          function getRequestValue( $key )
 160          {
 161              $request = HttpVars::getRequest();
 162              if ( isset($request[$key]) )
 163                  return( $request[$key] );   
 164              return '';
 165          }
 166  
 167          /**
 168           * Sets the value of the $_REQUEST array in PHP 4.1.0 or higher. If using a lower version,
 169           * then the content of this array will be copied into $HTTP_GET_VARS
 170           *
 171           * @param requestArray An associative array with the contents of our future $_REQUEST
 172           * array
 173           * @return Returns always true.
 174           */
 175          function setRequest( $requestArray )
 176          {
 177              if( phpversion() >= "4.1.0" ) {
 178                  foreach( $requestArray as $key => $value ) {
 179                      $_REQUEST["$key"] = $value;
 180                  }
 181              }
 182              else {
 183                  HttpVars::setGet( $requestArray );
 184              }
 185  
 186              return true;
 187          }
 188          
 189          /**
 190           * sets a value in the request
 191           *
 192           * @param key
 193           * @param value
 194           * @return true
 195           */
 196          function setRequestValue( $key, $value )
 197          {
 198              $request = HttpVars::getRequest();
 199              $request["$key"] = $value;
 200              HttpVars::setRequest( $request );
 201              
 202              return true;
 203          }
 204  
 205          /**
 206           * Sets the value of the $_GET array in PHP 4.1.0 or higher and of the
 207           * $HTTP_GET_VARS if lower.
 208           *
 209           * @param getArray An associative array with the contents of our future $_GET
 210           * array
 211           * @return Returns always true.
 212           */
 213          function setGet( $getArray )
 214          {
 215              if( phpversion() >= "4.1.0" ) {
 216                  foreach( $getArray as $key => $value ) {
 217                      $_GET["$key"] = $value;
 218                  }
 219              }
 220              else {
 221                  global $HTTP_GET_VARS;
 222                  foreach( $getArray as $key => $value ) {
 223                      $HTTP_GET_VARS["$key"] = $value;
 224                  }
 225              }
 226  
 227              return true;
 228          }
 229  
 230          /**
 231           * Returns the $_SERVER array, otherwise known as $HTTP_SERVER_VARS in versions older
 232           * than PHP 4.1.0
 233           *
 234           * @return An associative array with the contents of the $_SERVER array, or equivalent.
 235           * @static
 236           */
 237          function getServer()
 238          {
 239              if( phpversion() >= "4.1.0" )
 240                  $serverVars = $_SERVER;
 241              else {
 242                  global $HTTP_SERVER_VARS;
 243                  $serverVars = $HTTP_SERVER_VARS;
 244              }
 245  
 246              return $serverVars;
 247          }
 248  
 249          function getFiles()
 250          {
 251              if( phpversion() >= "4.1.0" )
 252                  $files = $_FILES;
 253              else {
 254                  global $HTTP_POST_FILES;
 255                  $files = $HTTP_POST_FILES;
 256              }
 257  
 258              return $files;
 259          }
 260  
 261          /**
 262           * Returns the base URL of the script
 263           *
 264           * @return A string containing the base URL of the script
 265           * @static
 266           */
 267          function getBaseUrl()
 268          {
 269              $serverVars = HttpVars::getServer();
 270              if ( !isset($serverVars['HTTPS']) || strtolower($serverVars['HTTPS']) != 'on' ) {
 271                  $protocol = 'http://';
 272              } else {
 273                  $protocol = 'https://';
 274              }
 275              $host      = $serverVars["HTTP_HOST"];
 276              $scriptUrl = $serverVars["PHP_SELF"];
 277  
 278              return $protocol . $host . $scriptUrl;
 279          }
 280      }
 281  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics