AJAX
[ class tree: AJAX ] [ index: AJAX ] [ all elements ]

Source for file Server.php

Documentation is available at Server.php

  1. <?php
  2. require_once 'HTML/AJAX.php';
  3.  
  4. /**
  5.  * Class for creating an external AJAX server
  6.  *
  7.  * Can be used in 2 different modes, registerClass mode where you create an instance of the server and add the classes that will be registered
  8.  * and then run handle request
  9.  *
  10.  * Or you can extend it and add init{className} methods for each class you want to export
  11.  *
  12.  * Client js generation is exposed through 2 _GET params client and stub
  13.  *  Setting the _GET param client to `all` will give you all the js classes needed
  14.  *  Setting the _GET param stub to `all` will give you stubs of all registered classes, you can also set it too just 1 class
  15.  *
  16.  * @category   HTML
  17.  * @package    AJAX
  18.  * @author     Joshua Eichorn <josh@bluga.net>
  19.  * @copyright  2005 Joshua Eichorn
  20.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  21.  * @version    Release: @package_version@
  22.  * @link       http://pear.php.net/package/PackageName
  23.  * @todo       Decide if a syntax is needed to generate 2 stubs at once
  24.  */
  25.  
  26.     /**
  27.      * Client options array if set to true the code looks at _GET
  28.      * @var    bool|array
  29.      */
  30.     var $options = true;
  31.  
  32.     /**
  33.      * HTML_AJAX instance
  34.      * @var    HTML_AJAX 
  35.      */
  36.     var $ajax;
  37.  
  38.     /**
  39.      * Set to true if your extending the server to add init{className methods}
  40.      */
  41.     var $initMethods = false;
  42.  
  43.     /**
  44.      * Constructor creates the HTML_AJAX instance
  45.      *
  46.      * @todo: verify that PHP_SELF always does what we want
  47.      */
  48.     function HTML_AJAX_Server({
  49.         $this->ajax =new HTML_AJAX();
  50.         $this->ajax->serverUrl = $_SERVER['PHP_SELF'];
  51.     }
  52.  
  53.     /**
  54.      * Handle a client request, either generating a client or having HTML_AJAX handle the request
  55.      */
  56.     function handleRequest({
  57.         if ($this->options == true{
  58.             $this->_loadOptions();
  59.         }
  60.  
  61.         if (!isset($_GET['c'])) {
  62.             return $this->generateClient();
  63.         }
  64.         else {
  65.             $this->_init($_GET['c']);
  66.             return $this->ajax->handleRequest();
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * Register method passthrough to HTML_AJAX
  72.      *
  73.      * @see HTML_AJAX::registerClass for docs
  74.      */
  75.     function registerClass(&$instance$exportedName = false$exportedMethods = false{
  76.         $this->ajax->registerClass($instance,$exportedName,$exportedMethods);
  77.     }
  78.  
  79.     /**
  80.      * Generate client js
  81.      *
  82.      * @todo       Add Http_Cache type functionality so the client will cache the js
  83.      */
  84.     function generateClient({
  85.         header('Content-type: text/javascript');
  86.  
  87.         if ($this->options['stub'=== 'all'{
  88.             if ($this->initMethods{
  89.                 $this->_initAll();
  90.             }
  91.             echo $this->ajax->generateJavaScriptClient($this->options['stub']);
  92.         }
  93.         else {
  94.             if ($this->options['stub'!== false{
  95.                 $this->_init($this->options['stub']);
  96.                 echo $this->ajax->generateClassStub($this->options['stub']);
  97.             }
  98.         }
  99.  
  100.         $library strtolower($this->options['client']);
  101.         switch($library{
  102.             case 'all':
  103.             case 'html_ajax':
  104.                 $this->_readFile($this->clientJsLocation()."HTML_AJAX.js");
  105.                 break;
  106.             case 'json':
  107.                 $this->_readFile($this->clientJsLocation()."JSON.js");
  108.                 break;
  109.             case 'request':
  110.                 $this->_readFile($this->clientJsLocation()."Request.js");
  111.                 break;
  112.             case 'main':
  113.                 $this->_readFile($this->clientJsLocation()."Main.js");
  114.                 break;
  115.             case 'httpclient':
  116.                 $this->_readFile($this->clientJsLocation()."HttpClient.js");
  117.                 break;
  118.             case 'dispatcher':
  119.                 $this->_readFile($this->clientJsLocation()."Dispatcher.js");
  120.                 break;
  121.             case false:
  122.                 break;
  123.             default:
  124.                 echo "alert('Unknown javascript library:  $library');";
  125.                 break;
  126.         }
  127.  
  128.     }
  129.  
  130.     /**
  131.      * Run readfile on input with basic error checking
  132.      *
  133.      * @param   string  $file   file to read
  134.      * @access  private
  135.      */
  136.     function _readFile($file{
  137.         if (file_exists($file)) {
  138.             readfile($file);
  139.         }
  140.         else {
  141.                 echo "alert('Unable to find javascript file: $file');";
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Get the location of the client js
  147.      *
  148.      * @return    string 
  149.      * @todo    figure out where this will be on an install
  150.      */
  151.     function clientJsLocation({
  152.         return '@data-dir@/HTML_AJAX/js/';
  153.     }
  154.  
  155.     /**
  156.      * Load options from _GET
  157.      *
  158.      * @access private
  159.      * @todo Is this preg_replace a good enough security check?
  160.      */
  161.     function _loadOptions({
  162.         $this->options = array('client'=>false,'stub'=>false);
  163.         if (isset($_GET['client'])) {
  164.             $this->options['client'$_GET['client'];
  165.         }
  166.         if (isset($_GET['stub'])) {
  167.             $stub trim(preg_replace('/[^A-Za-z_0-9]/','',$_GET['stub']));
  168.             if (!empty($stub)) {
  169.                 $this->options['stub'$stub;
  170.             }
  171.             else {
  172.                 $this->options['stub'= false;
  173.             }
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * Run every init method on the class
  179.      * @access private
  180.      */
  181.     function _initAll({
  182.         $methods get_class_methods(get_class($this));
  183.  
  184.         foreach($methods as $method{
  185.             if (substr($method,0,4== 'init'{
  186.                 $this->$method();
  187.             }
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Init one class
  193.      * @param    string    $className 
  194.      * @access private
  195.      * @todo    error handling if the method doesn't exist
  196.      */
  197.     function _init($className{
  198.         $m = "init$className";
  199.         if (is_callable(array(&$this,$m))) {
  200.             $this->$m();
  201.         }
  202.     }
  203. }
  204. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  205. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.