Source for file OCSP_AJAX.phpclass

Documentation is available at OCSP_AJAX.phpclass

  1. <?php
  2. /**
  3.   * Class file OCSP_AJAX.phpclass
  4.   *
  5.   * thanx to the SAJAX project
  6.   *
  7.   * @project    Open CSP-Management
  8.   * @package    common
  9.   * @subpackage ajax
  10.   * @category   class
  11.   *
  12.   *
  13.   * @author     Peter Krebs (pk) <pitlinz@users.sourceforge.net>
  14.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  15.   *
  16.   * @since pk-07-04-19
  17.   *
  18.   ***/
  19.  
  20. /**
  21.   * constant definitions
  22.   */
  23.  
  24. define('OCSP_AJAX_RQMODE_GET',"get");
  25. define('OCSP_AJAX_RQMODE_POST',"post");
  26.  
  27. /**
  28.   * constant requires
  29.   */
  30.  
  31. require_once dirname(__FILE__)._OCSP_DIRSEP_."OCSP_AJAX_FUNCTION.phpclass";
  32. require_once __OCSP_PHPINCPATH__."common"._OCSP_DIRSEP_."pcf_directory.phpinc";
  33. require_once __OCSP_PHPINCPATH__."common"._OCSP_DIRSEP_."OCSP_OBJ.phpclass";
  34.  
  35. if (!function_exists('jOCSP_registerError'))
  36. {
  37.     require_once dirname(__FILE___OCSP_DIRSEP_ "jOCSP_functions.phpinc";
  38. }
  39.  
  40. /**
  41.   * Class OCSP_AJAX
  42.   *
  43.   * this class is designed to handle ajex requests
  44.   *
  45.   * @project    Open CSP-Management
  46.   * @package    cms
  47.   * @category   chapter
  48.   *
  49.   * @author     Peter Krebs (pk) <p.krebs@amicas.at>
  50.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  51.   *
  52.   * @since pk-07-02-21
  53.   *
  54.   ***/
  55. class OCSP_AJAX extends OCSP_OBJ {
  56.  
  57.     /*** class constants  --------------------------------------------- */
  58.  
  59.     /**
  60.       * @constant string CLASS_SRC_FILE
  61.       */
  62.     const CLASS_SRC_FILE = __FILE__;    
  63.     
  64.     /*** class variables  --------------------------------------------- */
  65.     
  66.  
  67.     /*** class methods  --------------------------------------------- */
  68.  
  69.     public static function isAjaxCall()
  70.     {
  71.         return (isset($_SERVER['HTTP_X_REQUESTED_WITH']&& ($_SERVER['HTTP_X_REQUESTED_WITH'== 'XMLHttpRequest'));        
  72.     }
  73.     
  74.     public static function isjOCSPLoad()
  75.     {
  76.         if (self::isAjaxCall())
  77.         {
  78.             return (isset($_GET['inToDiv']&& !empty($_GET['inToDiv']));
  79.         
  80.         return False;
  81.     }
  82.     
  83.     
  84.     /** Aggregations: */
  85.  
  86.     /** Compositions: */
  87.  
  88.     /**
  89.       * @var array $functionLst the functions available (registered)
  90.       * @access protected
  91.       ***/
  92.     protected $functionLst=array();
  93.  
  94.     /** Attributes: **/
  95.  
  96.     /**
  97.       * @var array $done_funcLst the functions available (registered)
  98.       * @access protected
  99.       ***/
  100.     protected $done_funcLst=array();
  101.  
  102.     /**
  103.       * @var string $objId the id of the ajax object
  104.       * @access protected
  105.       ***/
  106.     protected $objId="";
  107.  
  108.  
  109.     /**
  110.       * @var array $valueLst values the functions uses
  111.       * @access protected
  112.       ***/
  113.     protected $valueLst=array();
  114.  
  115.     /**
  116.       * @var string $jsAction the action used
  117.       * @access protected
  118.       ***/
  119.     protected $jsAction="";
  120.  
  121.     /**
  122.       * @var string $jsCode aditional code to include in on the page
  123.       * @access protected
  124.       * @since pk-07-09-18
  125.       */
  126.     protected $jsCode="";
  127.  
  128.     /**
  129.       * @var string $myClassSrcFile 
  130.       * @access protected
  131.       ***/
  132.     protected $myClassSrcFile=__FILE__;
  133.  
  134.     /**
  135.       * @var boolean $isCachable 
  136.       * @access protected
  137.       ***/
  138.     protected $isCachable=FALSE;
  139.  
  140.     /**
  141.       * where to cache the object
  142.       * possible settings are:
  143.       *     - session (cache the object data to the session)
  144.       *
  145.       * @var string $myCacheType 
  146.       * @access protected
  147.       * @since pk-07-03-18
  148.       */
  149.     protected $myCacheType='session';
  150.  
  151.     /**
  152.       * array of cached objects
  153.       * @var array $myCacheObjects 
  154.       * @access protected
  155.       * @since pk-07-03-18
  156.       */
  157.     protected $myCacheObjects=array();
  158.  
  159.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160.     // init methods
  161.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  162.  
  163.     function OCSP_AJAX($aId="",$funcInc="",$debug=FALSE{
  164.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_AJAX::OCSP_AJAX()");
  165.  
  166.         $this->jsAction = OCSP_CONF::getInstance('OCSP')->getValue('SYSTEMURL'"ajax.php";
  167.  
  168.         if (!empty($aId)) {
  169.             $this->objId=$aId;
  170.         else {
  171.             $this->get_objId($debug);
  172.         }
  173.     }    
  174.  
  175.     /**
  176.       * returns the object ID
  177.       * if $this->objId is empty $_SERVER['UNIQUE
  178.       *
  179.       * if you use a special class to handle the ajax requests overwrite this methode
  180.       *
  181.       * @param boolean $debug 
  182.       *
  183.       * @returns string
  184.       *
  185.       * @access public
  186.       *
  187.       ***/
  188.     function get_objId($debug=FALSE{
  189.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_AJAX::get_objId()");
  190.         if (empty($this->objId)) {
  191.             $this->objId=session_id()."__".md5($_SERVER['REQUEST_URI']);
  192.             if ($debugechoDebug(__FILE__,"<p style=\"padding-left:20px\">Generating objId: <b>".$this->objId."</b></p>\n");
  193.         }
  194.         return $this->objId;
  195.     }
  196.  
  197.     /**
  198.       * @param string $aId 
  199.       * @access public
  200.       */
  201.     function set_objId($aId)
  202.     {
  203.         $this->objId=$aId;
  204.     }
  205.  
  206.     /**
  207.       * @returns string
  208.       * @access public
  209.       ***/
  210.     function getActionUri({
  211.         return $this->jsAction;
  212.     }
  213.  
  214.     /**
  215.       * sets the action uri (where the request is sent)
  216.       *
  217.       * @param string $rqUrl 
  218.       * @access public
  219.       * @since pk-07-04-19
  220.       */
  221.     function setActionUri($rqUrl)
  222.     {
  223.         $this->jsAction=$rqUrl;
  224.     }
  225.  
  226.     /**
  227.       * @param string $aCode 
  228.       * @access public
  229.       * @since pk-07-09-18
  230.       */
  231.     function addJsCode($aCode)
  232.     {
  233.         $this->jsCode.=$aCode."\n";
  234.     }
  235.  
  236.     /**
  237.       * @return string (js)
  238.       * @access public
  239.       * @since pk-07-09-18
  240.       */
  241.     function getJsCode()
  242.     {
  243.         return $this->jsCode;
  244.     }
  245.  
  246.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  247.     // function methods
  248.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  249.  
  250.     /**
  251.       * retuns a registered function or an pointer to an empty function in $this::functionLst
  252.       *
  253.       * @param string $func_name 
  254.       *
  255.       * @returns OCSP_AJAX_FUNCTION
  256.       *
  257.       * @access public
  258.       *
  259.       ***/
  260.     function &getFunction($func_name{
  261.         return $this->functionLst[$func_name];
  262.     }
  263.  
  264.  
  265.     /**
  266.       * register a function
  267.       *
  268.       * @param string $func_name name of the php function to call
  269.       * @param string $func_include include file containing the $func_name
  270.       * @param array $func_args argument array to submit too
  271.       * @param string $rq_methode request methode (GET|POST)
  272.       * @param boolean $debug 
  273.       *
  274.       * @returns OCSP_AJAX_FUNCTION
  275.       *
  276.       * @access public
  277.       *
  278.       ***/
  279.     function &registerFunction($func_name,$func_include="",$func_args=NULL,$rq_methode=OCSP_AJAX_RQMODE_GET,$debug=FALSE{
  280.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_AJAX::registerFunction($func_name)");
  281.  
  282.         $this->functionLst[$func_name]=new OCSP_AJAX_FUNCTION($this,$func_name,$func_include,$func_args,$rq_methode);
  283.         return $this->functionLst[$func_name];
  284.  
  285.     }
  286.  
  287.     /**
  288.       * register a form submit
  289.       *
  290.       * @param string $func_name name of the php function to call
  291.       * @param string $frmName name of the formular (document.forms['$frmName'])
  292.       * @param string $func_include include file containing the $func_name
  293.       * @param array $func_args argument array to submit too
  294.       * @param string $rq_methode request methode (GET|POST)
  295.       * @param boolean $debug 
  296.       *
  297.       * @returns OCSP_AJAX_FUNC_FRMSUBMIT
  298.       *
  299.       * @access public
  300.       *
  301.       ***/
  302.     function &registerFormSubmit($func_name,$frmName,$func_include,$func_args=NULL,$rq_methode=OCSP_AJAX_RQMODE_POST,$debug=FALSE{
  303.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_AJAX::registerFormSubmit()");
  304.  
  305.         require_once dirname(__FILE__)."/OCSP_AJAX_FUNC_FRMSUBMIT.phpclass";
  306.         $this->functionLst[$func_name]=new OCSP_AJAX_FUNC_FRMSUBMIT($this,$func_name,$frmName,$func_include,$func_args,$rq_methode);
  307.         return $this->functionLst[$func_name];
  308.     }
  309.  
  310.  
  311.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  312.     // js methods
  313.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  314.  
  315.     /**
  316.       * javascript escape a value
  317.       *
  318.       * @param string $val 
  319.       *
  320.       * @returns string
  321.       *
  322.       ***/
  323.     function js_esc($val{
  324.         return pcf_js_escape($val);
  325.     }
  326.  
  327.  
  328.     /**
  329.       * returns the js code for the function List
  330.       *
  331.       * @param boolean $debug 
  332.       *
  333.       * @return string (javascript code)
  334.       *
  335.       * @since pk-07-08-20
  336.       */
  337.     function js_getFunctionCode($debug=FALSE)
  338.     {
  339.         if ($debugechoDebugMethod(__FILE__,pcf_object_id($this),"OCSP_AJAX::js_getFunctionCode()");
  340.  
  341.         $s_ret="";
  342.         foreach($this->functionLst as $s_funcName => &$o_function{
  343.             if (!isset($this->done_funcLst[$s_funcName]|| (!$this->done_funcLst[$s_funcName])) {
  344.                 $this->done_funcLst[$s_funcName]=TRUE;
  345.                 if (is_object($o_function))
  346.                 {
  347.                     if (method_exists($o_function,"js_getFunction"))
  348.                     {
  349.                         $s_ret.=$o_function->js_getFunction();
  350.                     }
  351.                     if ($this->isCachable && method_exists($o_function,"getMyCacheValues"))
  352.                     {
  353.                         $this->myCacheObjects[$func_name]=$o_function->getMyCacheValues();
  354.                     }
  355.                 }
  356.  
  357.             }
  358.         }
  359.  
  360.         if ($this->isCachable)
  361.         {
  362.             $this->writeCache();
  363.         }
  364.  
  365.         return $s_ret;
  366.     }
  367.  
  368.  
  369.     /**
  370.       * returns the javascript for the object
  371.       *
  372.       * @param boolean $debug 
  373.       *
  374.       * @access public
  375.       *
  376.       ***/
  377.     function js_getScript($debug=FALSE{       
  378.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_AJAX::js_getScript()");
  379.  
  380.         $s_ret="";
  381.         $s_ret.="<script language=\"JavaScript\" type=\"text/javascript\">\n<!--\n";
  382.         $s_ret.="// ajax js_getScript\n";
  383.         $s_ret.=$this->js_getFunctionCode($debug);
  384.         $s_ret.=$this->getJsCode();
  385.         $s_ret.="// -->\n</script>\n";
  386.         return $s_ret;
  387.     }
  388.  
  389.     /**
  390.       * returns the javascript code for an ajax function argument
  391.       *
  392.       * @param string $varName 
  393.       * @param mixed $value 
  394.       *
  395.       * @return string 
  396.       */
  397.     function js_getNewFuncArg($varName,$value)
  398.     {
  399.         $str_ret="new ocspAjax_funcArg('MEN_ID',";
  400.         if (intval($value)) {
  401.             $str_ret.=$value;
  402.         else {
  403.             $str_ret.="'".$this->js_esc($value)."'";
  404.         }
  405.         return $str_ret.")";
  406.     }
  407.  
  408.     /**
  409.       * returns the javascript code for an ajax function argument list
  410.       *
  411.       * @param array $argList (array(varName => value))
  412.       *
  413.       * @return string 
  414.       */
  415.     function js_getFuncArgList($argList)
  416.     {
  417.         $str_ret="new Array(";
  418.         $char_sep='';
  419.         if (is_array($argList))
  420.         {
  421.             foreach($argList as $str_name => $str_val)
  422.             {
  423.                 $str_ret.=$char_sep.$this->js_getNewFuncArg($str_name,$str_val);
  424.             }
  425.         }
  426.  
  427.         return $str_ret.")";
  428.     }
  429.  
  430.     /**
  431.       * sets the http header for the ajax return
  432.       *
  433.       * @param string $contentType 
  434.       * @param string $charset 
  435.       *
  436.       */
  437.     function setHTTPheader($contentType="text/javascript",$charset="UTF-8")
  438.     {
  439.         header ("Expires: Mon, 26 Feb 2007 00:00:00 GMT");    // Date in the past
  440.         header ("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  441.         header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
  442.         header ("Pragma: no-cache");                          // HTTP/1.0
  443.         if (!empty($contentType&& !empty($charset))
  444.         {
  445.             header ("Content-Type: ".$contentType."; charset=".$charset);
  446.         }
  447.     }
  448.  
  449.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  450.     // cache methods
  451.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  452.  
  453.     /**
  454.       * enables the object caching
  455.       *
  456.       * @param string cacheType
  457.       * @access public
  458.       * @since pk-07-03-18
  459.       */
  460.     function enableCache($cacheType='session')
  461.     {
  462.         switch($cacheType// check the type
  463.         {
  464.             case "session":
  465.                 $this->enableCache=TRUE;
  466.                 $this->myCacheType='session';
  467.                 break;
  468.             default:
  469.                 echo "<p>Cachetype$cacheType NOT IMPLEMENTED<br>".__FILE__." Line: ".__LINE__." (".__CLASS__."::".__METHOD__.")</p>\n";
  470.         }
  471.     }
  472.  
  473.     /**
  474.       * writes the cache
  475.       * @param boolean $debug 
  476.       * @access public
  477.       * @since pk-07-03-18
  478.       * @todo OCSP_AJAX::cacheStore() use ocsp session
  479.       */
  480.     function cacheStore($debug=FALSE)
  481.     {
  482.         if ($debugechoDebugMethod(__FILE__,get_class($this),__CLASS__."::cacheStore()");
  483.  
  484.         switch($this->myCacheType{
  485.             case "session":
  486.                 if (!is_array($_SESSION)) // session not startet -> start it
  487.                 {
  488.                     session_start();
  489.                 }
  490.                 $_SESSION['OCSP_AJAX'][$this->get_objId()]['CVAL']=$this->myCacheObjects;
  491.                 break;
  492.             default:
  493.                 echo "<p>Cachetype$cacheType NOT IMPLEMENTED<br>".__FILE__." Line: ".__LINE__." (".__CLASS__."::".__METHOD__.")</p>\n";
  494.         }
  495.  
  496.     }
  497.  
  498.  
  499.     /**
  500.       * loads the cached data
  501.       *
  502.       * @param string $cacheType 
  503.       * @param boolean $debug 
  504.       *
  505.       * @returns boolean
  506.       * @access public
  507.       * @since pk-07-03-18
  508.       * @todo OCSP_AJAX::cacheLoad() use ocsp session
  509.       */
  510.     function cacheLoad($cacheType='session',$debug=FALSE)
  511.     {
  512.         if ($debugechoDebugMethod(__FILE__,get_class($this),__CLASS__."::cacheLoad()");
  513.  
  514.         switch($this->myCacheType{
  515.             case "session":
  516.                 if (!is_array($_SESSION)) // session not startet -> start it
  517.                 {
  518.                     session_start();
  519.                 }
  520.                 $this->myCacheObjects=$_SESSION['OCSP_AJAX'][$this->get_objId()]['CVAL'];
  521.                 break;
  522.             default:
  523.                 echo "<p>Cachetype$cacheType NOT IMPLEMENTED<br>".__FILE__." Line: ".__LINE__." (".__CLASS__."::".__METHOD__.")</p>\n";
  524.         }
  525.     }
  526. }

Documentation generated on Thu, 08 Jan 2009 17:45:06 +0100 by phpDocumentor 1.4.0a2