[ Index ]

PHP Cross Reference of phool

title

Body

[close]

/Phool/Web/ -> HTTP.php (source)

   1  <?php
   2  //============================================================================
   3  // This program is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU Lesser General Public License (LGPL) as
   5  // published by the Free Software Foundation, either version 3 of the License,
   6  // or (at your option) any later version.
   7  //
   8  // This program is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU Lesser General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU Lesser General Public License
  14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15  //============================================================================
  16  /**
  17  * @copyright Francois Laupretre <phool@tekwire.net>
  18  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0
  19  * @category phool
  20  * @package phool
  21  */
  22  //============================================================================
  23  
  24  namespace Phool\Web;
  25  
  26  class HTTP
  27  {
  28  
  29  //---------
  30  
  31  public static function getArg($name)
  32  {
  33  if (!array_key_exists($name,$_GET))
  34      throw new \Exception("$name: GET argument missing");
  35  return trim($_GET[$name]);
  36  }
  37  
  38  //---------
  39  // Compute the base URL we were called with
  40  
  41  public static function baseUrl()
  42  {
  43  if (!\Phool\Util::envIsWeb()) return '';
  44  
  45  if (!isset($_SERVER['PATH_INFO'])) return $_SERVER['PHP_SELF'];
  46  
  47  $phpself=$_SERVER['PHP_SELF'];
  48  $slen=strlen($phpself);
  49  
  50  $pathinfo=$_SERVER['PATH_INFO'];
  51  $ilen=strlen($pathinfo);
  52  
  53  // Remove PATH_INFO from PHP_SELF if it is at the end. Don't know
  54  // which config does this, but some servers put it, some don't.
  55  
  56  if (($slen > $ilen) && (substr($phpself,$slen-$ilen)==$pathinfo))
  57      $phpself=substr($phpself,0,$slen-$ilen);
  58  
  59  return $phpself;
  60  }
  61  
  62  //---------------------------------
  63  // Sends an HTTP 301 redirection
  64  
  65  public static function http301Redirect($path)
  66  {
  67  header('Location: http://'.$_SERVER['HTTP_HOST'].self::http_baseUrl().$path);
  68  header('HTTP/1.1 301 Moved Permanently');
  69  exit(0);
  70  }
  71  
  72  //---------------------------------
  73  // Sends an HTTP 400 failure
  74  
  75  public static function http400Fail($msg='')
  76  {
  77  if ($msg!='') $msg=' - '.$msg;
  78  header("HTTP/1.0 400 Bad Request".$msg);
  79  exit(1);
  80  }
  81  
  82  //---------------------------------
  83  // Sends an HTTP 403 failure
  84  
  85  public static function http403Fail($msg='')
  86  {
  87  if ($msg!='') $msg=' - '.$msg;
  88  header("HTTP/1.0 403 Forbidden".$msg);
  89  exit(1);
  90  }
  91  
  92  //---------------------------------
  93  // Sends an HTTP 404 failure
  94  
  95  public static function http404Fail($msg='')
  96  {
  97  if ($msg!='') $msg=' - '.$msg;
  98  header("HTTP/1.0 404 Not Found".$msg);
  99  exit(1);
 100  }
 101  
 102  //---------------------------------
 103  // Sends a mime header
 104  
 105  public static function mimeHeader($type)
 106  {
 107  header("Content-type: $type");
 108  }
 109  
 110  //----------
 111  } // End of class
 112  //=============================================================================
 113  ?>


Generated: Thu Jun 4 19:17:11 2015 Cross-referenced by PHPXref 0.7.1