[ Index ]

PHP Cross Reference of JPSpan 0.4 (beta)

title

Body

[close]

/JPSpan/ -> Server.php (source)

   1  <?php
   2  /**
   3  * @package JPSpan
   4  * @subpackage Server
   5  * @version $Id: Server.php,v 1.5 2004/11/23 13:49:25 harryf Exp $
   6  */
   7  //--------------------------------------------------------------------------------
   8  /**
   9  * Define
  10  */
  11  if ( !defined('JPSPAN') ) {
  12      define ('JPSPAN',dirname(__FILE__).'/');
  13  }
  14  /**
  15  * Include
  16  */
  17  require_once JPSPAN . 'Handle.php';
  18  //--------------------------------------------------------------------------------
  19  
  20  /**
  21  * Base Server class.
  22  * @package JPSpan
  23  * @subpackage Server
  24  * @public
  25  * @abstract
  26  */
  27  class JPSpan_Server {
  28  
  29      /**
  30      * Hash of user defined handlers (keys are class name)
  31      * @var array
  32      * @access private
  33      */
  34      var $handlers = array();
  35      
  36      /**
  37      * Descriptions of handlers stored here as hash
  38      * @var array
  39      * @access private
  40      */
  41      var $descriptions = array();
  42      
  43      /**
  44      * URL where server is published
  45      * @var string
  46      * @access private
  47      */
  48      var $serverUrl;
  49  
  50      /**
  51      * Sets up the default server url
  52      * @access public
  53      */
  54      function JPSpan_Server() {
  55          if ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) {
  56              $prot = 'https://';
  57          } else {
  58              $prot = 'http://';
  59          }
  60          $this->serverUrl = $prot.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
  61      }
  62      
  63      /**
  64      * Set the URL where the server is published
  65      * @param string server url (where the server is public)
  66      * @return void
  67      * @access public
  68      */
  69      function setServerUrl($serverUrl) {
  70          $this->serverUrl = $serverUrl;
  71      }
  72      
  73      /**
  74      * Return the server url
  75      * @return string server url (where the server is public)
  76      * @access public
  77      */
  78      function getServerUrl() {
  79          return $this->serverUrl;
  80      }
  81      
  82      /**
  83      * Return reference to a handler given it's name.
  84      * Note this will also resolve the handle
  85      * @param string handler name (class name)
  86      * @return mixed object handler or FALSE if not found
  87      * @access public
  88      */
  89      function & getHandler($name) {
  90          $name = strtolower($name);
  91          if ( isset($this->handlers[$name]) ) {
  92              JPSpan_Handle::resolve($this->handlers[$name]);
  93              return $this->handlers[$name];
  94          }
  95          return FALSE;
  96      }
  97      
  98      /**
  99      * Return handler description given it's name
 100      * @param string handler name (class name)
 101      * @return mixed object handler description or FALSE if not found
 102      * @access public
 103      */
 104      function getDescription($name) {
 105          $name = strtolower($name);
 106          if ( isset($this->descriptions[$name]) ) {
 107              return $this->descriptions[$name];
 108          }
 109          return FALSE;
 110      }
 111  
 112      /**
 113      * Registers a user handler class with the server
 114      * @see http://wact.sourceforge.net/index.php/Handle
 115      * @param mixed handle to user class
 116      * @return void
 117      * @access public
 118      */
 119      function addHandler(& $Handle, $Description = NULL) {
 120          if ( is_null($Description) ) {
 121              if ( FALSE !== ($Description = JPSpan_Handle::examine($Handle)) ) {
 122                  $this->handlers[$Description->Class] = & $Handle;
 123                  $this->descriptions[$Description->Class] = $Description;
 124              } else {
 125                  trigger_error('Invalid handle',E_USER_ERROR);
 126              }
 127          } else {
 128              if ( isset($Description->Class) && is_string($Description->Class) && is_array($Description->methods) ) {
 129                  $Description->Class = strtolower($Description->Class);
 130                  $Description->methods = array_map('strtolower',$Description->methods);
 131                  $this->handlers[strtolower($Description->Class)] = & $Handle;
 132                  $this->descriptions[strtolower($Description->Class)] = $Description;
 133              } else {
 134                  trigger_error('Invalid description',E_USER_ERROR);
 135              }
 136          }
 137      }
 138  
 139      /**
 140      * Returns object for generating the client
 141      * @return object
 142      * @access public
 143      * @abstract
 144      */
 145      function getGenerator() {}
 146  
 147      /**
 148      * Start serving (override in subclasses)
 149      * @return boolean FALSE if serve failed
 150      * @access public
 151      * @abstact
 152      */
 153      function serve() {}
 154      
 155      /**
 156      * Returns the portion of the URL to the right of the executed
 157      * PHP script e.g. http://localhost/index.php/foo/bar/ returns
 158      * 'foo/bar'. Returns the string up to the end or to the first ?
 159      * character
 160      * @return string
 161      * @access public
 162      * @static
 163      */
 164      function getUriPath() {
 165      
 166          $basePath = explode('/',$_SERVER['SCRIPT_NAME']);
 167          $script = array_pop($basePath);
 168          $basePath = implode('/',$basePath);
 169          
 170          // Determine URI path - path variables to the right of the PHP script
 171          if ( false !== strpos ( $_SERVER['REQUEST_URI'], $script ) ) {
 172              $uriPath = explode( $script,$_SERVER['REQUEST_URI'] );
 173              $uriPath = $uriPath[1];
 174          } else {
 175              $pattern = '/^'.str_replace('/','\/',$basePath).'/';
 176              $uriPath = preg_replace($pattern,'',$_SERVER['REQUEST_URI']);
 177          }
 178          if ( FALSE !== ( $pos = strpos($uriPath,'?') )  ) {
 179              $uriPath = substr($uriPath,0,$pos);
 180          }
 181          $uriPath = preg_replace(array('/^\//','/\/$/'),'',$uriPath);
 182          return $uriPath;
 183          
 184      }
 185      
 186      /**
 187      * Load the error reader
 188      * @param string (optional) 2 letter localization code e.g. 'en'
 189      * @param array (optional) list of Application_Errors to merge in
 190      * @param array (optional) list of Server_Errors to merge in
 191      * @param array (optional) list of Client_Errors to merge in
 192      * @todo Break this function up
 193      * @return void
 194      * @access public
 195      */
 196      function loadErrorReader($lang='en',$app=array(),$ser=array(),$cli=array()) {
 197          require_once JPSPAN . 'Include.php';
 198          JPSpan_Include_ErrorReader($lang,$app,$ser,$cli);
 199      }
 200      
 201      /**
 202      * Display the Javascript client and exit
 203      * @return void
 204      * @access public
 205      */
 206      function displayClient() {
 207          $G = & $this->getGenerator();
 208          require_once JPSPAN . 'Include.php';
 209          $I = & JPSpan_Include::instance();
 210          
 211          // HACK - this needs to change
 212          $I->loadString(__FILE__,$G->getClient());
 213          $client = $I->getCode();
 214          header('Content-Type: application/x-javascript');
 215          header('Content-Length: '.strlen($client));
 216          echo $client;
 217          exit();
 218      }
 219      
 220  }
 221  
 222  


Generated: Fri Nov 26 11:42:46 2004 Cross-referenced by PHPXref 0.6