Source for file OCSP_MODULE.phpclass

Documentation is available at OCSP_MODULE.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file OCSP_MODULE
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package modules
  7.   *
  8.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-13.06.2008
  12.   * @version $Id: OCSP_MODULE.phpclass,v 1.11 2008/11/17 21:50:38 pitlinz Exp $
  13.   */
  14.  
  15. // ---------------------------------------------------------
  16. // requirements
  17. // ---------------------------------------------------------
  18.  
  19. pcf_require_class('DBMS_TABLEOBJ',__OCSP_PHPINCPATH__ "db" _OCSP_DIRSEP_ "DBMS_TABLEOBJ.phpclass");
  20. pcf_require_class('OCSP_CACHE_APC',__OCSP_PHPINCPATH__ "cache" _OCSP_DIRSEP_ "OCSP_CACHE_APC.phpclass");
  21. pcf_require_class('OCSP_CACHE_FILE',__OCSP_PHPINCPATH__ "cache" _OCSP_DIRSEP_ "OCSP_CACHE_FILE.phpclass");
  22.  
  23. /**
  24.   * openCSP class OCSP_MODULE
  25.   * uses the singleton pattern
  26.   * 
  27.   * @project Open CSP-Management
  28.   * @package modules
  29.   *
  30.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  31.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  32.   *
  33.   * @since pk-13.06.2008
  34.   * @version $Id: OCSP_MODULE.phpclass,v 1.11 2008/11/17 21:50:38 pitlinz Exp $
  35.   */
  36. class OCSP_MODULE extends DBMS_TABLEOBJ
  37. {
  38.     /*** class constants  --------------------------------------------- */
  39.  
  40.     /**
  41.      * @constant string CLASS_SRC_FILE
  42.      *
  43.      * @abstract
  44.      */
  45.     const CLASS_SRC_FILE = __FILE__;
  46.  
  47.     /*** class vars  ------------------------------------------------- */
  48.     
  49.     /**
  50.      * array containing already loaded modules
  51.      *
  52.      * @var array 
  53.      */
  54.     protected static $loadedModules array();
  55.     
  56.     /**
  57.      * arry containing module id's
  58.      * 
  59.      * [ID] => prefix
  60.      * 
  61.      * @staticvar array $knownModules 
  62.      */
  63.     protected static $knownModules array();
  64.         
  65.     /*** class functions ---------------------------------------------- */
  66.  
  67.     /**
  68.      * returns a loaded instance of the module with prefix
  69.      * 
  70.      * @param string $modPrefix 
  71.      * @param boolean $debug 
  72.      * 
  73.      * @return OCSP_MODULE 
  74.      * 
  75.      */
  76.     public static function &getInstance($modPrefix,$debug=False)
  77.     {
  78.         if (!isset(self::$loadedModules[$modPrefix]|| !pcf_is_instance_of(self::$loadedModules[$modPrefix],"OCSP_MODULE"))
  79.         {
  80.             self::factoryFromPrefix($modPrefix,False,$debug);        
  81.         }
  82.         return self::$loadedModules[$modPrefix];
  83.     }
  84.  
  85.     /**
  86.      * returns the module unit of work with module id $modId
  87.      *
  88.      * @param int $modId 
  89.      * @param boolean $debug 
  90.      * 
  91.      * @return OCSP_MODULE 
  92.      * 
  93.      * @since pk-08-10-06
  94.      */
  95.     public static function &getInstanceByModId($modId,$debug=False)
  96.     {
  97.         if (isset(self::$knownModules[$modId]))
  98.         {
  99.             return self::getInstance(self::$knownModules[$modId],$debug);    
  100.         }
  101.         
  102.         return self::factoryFromId(intval($modId,$debug));
  103.     }
  104.     
  105.     /**
  106.      * returns an array with all installed modules
  107.      *
  108.      * @param boolean $refreshCache 
  109.      * @param boolean $debug 
  110.      * 
  111.      * @return array of OCSP_MODULE
  112.      */
  113.     public static function factoryAllInstalledModules($refreshCache=True,$debug=False)
  114.     {
  115.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_MODULE::factoryAllInstalledModules()");
  116.         
  117.         if ($arr_modules OCSP_OBJ::defaultReadDBObj()->getArray('T_MOD_MODULES'))
  118.         {
  119.             foreach($arr_modules as $arr_modRow)
  120.             {
  121.                 self::factoryFromRow($arr_row['MOD_PREFIX'],$arr_modRow,$debug);
  122.                 if (refreshCache)
  123.                 {
  124.                     self::$loadedModules[$arr_row['MOD_PREFIX']]->cacheToApc(0,$debug);
  125.                     self::$loadedModules[$arr_row['MOD_PREFIX']]->cacheToFile(0,$debug);
  126.                 }
  127.                 $knownModules[$arr_row['MOD_ID']] $arr_row['MOD_PREFIX'];
  128.             }
  129.         }
  130.         
  131.         return self::$loadedModules;
  132.         
  133.     }
  134.     
  135.     /*** compostion --------------------------------------------------- */
  136.     
  137.     /**
  138.      * array of already loaded configuration values
  139.      *
  140.      * @var array $myLoadedConfValues 
  141.      */
  142.     protected $myLoadedConfValues = array();
  143.     
  144.     /**
  145.      * timestamp the configurations have been populated
  146.      * 
  147.      * @var timestamp $myLoadedConfValuesTS 
  148.      */
  149.     protected $myLoadedConfValuesTS = 0;
  150.     
  151.     /*** attributes  -------------------------------------------------- */
  152.     
  153.     /**
  154.      * table name
  155.      *
  156.      * @var string 
  157.      */
  158.     protected $myTable="T_MOD_MODULES";    
  159.         
  160.     /*** factory / construct  ----------------------------------------- */
  161.     
  162.     /**
  163.      * factories a module
  164.      *
  165.      * @param string $$modPrefix 
  166.      * @param boolean $debug 
  167.      * 
  168.      * @return OCSP_MODULE 
  169.      */
  170.     public static function &factoryFromId($modId,$debug=False)
  171.     {    
  172.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_MODULE::factoryFromId(" $modId ")");
  173.         
  174.         if (isset(self::$knownModules[intval($modId)]))
  175.         {        
  176.             return self::factoryFromPrefix(self::$knownModules[intval($modId)]);
  177.         else {
  178.             if($obj_ret self::factoryFromDB($modId,$debug))
  179.             {
  180.                 $obj_ret->cacheToApc(0,$debug);
  181.                 $obj_ret->cacheToFile(0,$debug);
  182.                 return self::$loadedModules[$obj_ret->getPrefix()];
  183.             }
  184.         }
  185.     }
  186.     
  187.     /**
  188.      * factories a module
  189.      * 
  190.      * first tries APC and FILE cache
  191.      *
  192.      * @param string $$modPrefix 
  193.      * @param boolean $debug 
  194.      * 
  195.      * @return OCSP_MODULE 
  196.      * 
  197.      */
  198.     public static function &factoryFromPrefix($modPrefix,$forceReload=False,$debug=False)
  199.     {
  200.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::factoryFromPrefix(".$modPrefix.")");
  201.         if (isset(self::$loadedModules[$modPrefix]&& pcf_is_instance_of(self::$loadedModules[$modPrefix],"OCSP_MODULE"))
  202.         {
  203.             return self::$loadedModules[$modPrefix];
  204.         }
  205.         
  206.         $bol_useAPC      OCSP_CONF::getInstance('OCSP')->getValue('USEMODAPC');
  207.         
  208.         if (!$forceReload)
  209.         {
  210.             $bol_storeToApc False;
  211.             if ($bol_useAPC && OCSP_CACHE_APC::isUseable())
  212.             {
  213.                 if ($arr_modRow=OCSP_CACHE_APC::getInstance('MOD' $modPrefix)->getValue('MODULE_ROW'))
  214.                 {
  215.                     $obj_ret self::factoryFromRow($modPrefix,$arr_modRow,$debug);
  216.                     if (intval($obj_ret->getId()))
  217.                     {
  218.                         $knownModules[$obj_ret->getId()$obj_ret->getPrefix();
  219.                         return $obj_ret;
  220.                     }
  221.                 else {
  222.                     $bol_storeToApc True;
  223.                 }
  224.             }
  225.             
  226.             $bol_storeToFile False;
  227.             if (OCSP_CACHE_FILE::isUseable())
  228.             {
  229.                 if ($arr_modRow=OCSP_CACHE_FILE::getInstance('MOD' $modPrefix)->getValue('MODULE_ROW'))
  230.                 {
  231.                     if (self::factoryFromRow($modPrefix,$arr_modRow,$debug))
  232.                     {
  233.                         
  234.                         if ($bol_storeToApc)
  235.                         {
  236.                             self::$loadedModules[$modPrefix]->cacheToApc(0,$debug);    
  237.                         }
  238.                         return self::$loadedModules[$modPrefix];
  239.                     }
  240.                 else {
  241.                     $bol_storeToFile True;
  242.                 }            
  243.             }
  244.         else {
  245.             $bol_storeToApc True;
  246.             $bol_storeToFile True;
  247.         }
  248.  
  249.         
  250.         if (self::factoryFromDB($modPrefix,$debug))
  251.         {
  252.             if ($bol_useAPC && $bol_storeToApc)
  253.             {
  254.                 self::$loadedModules[$modPrefix]->cacheToApc(0,$debug);    
  255.             }    
  256.             if ($bol_storeToFile)
  257.             {
  258.                 self::$loadedModules[$modPrefix]->cacheToFile(0,$debug);    
  259.             }
  260.         else {
  261.             self::$loadedModules[$modPrefix]=Null;
  262.         }
  263.         
  264.         return self::$loadedModules[$modPrefix];
  265.             
  266.     }
  267.     
  268.     
  269.     /**
  270.      * factory the object from the database
  271.      * 
  272.      * @param mixed $aKey value of [MOD_ID|MOD_PREFIX]
  273.      * @param boolean $debug 
  274.      * 
  275.      * @return OCSP_MODULE 
  276.      */
  277.     public static function factoryFromDB($aKey,$debug)
  278.     {
  279.         if ($debugechoDebugMethod(__FILE__,"static","OCSP_MODULE::factoryFromDB(" $aKey .")");
  280.         
  281.         if (intval($aKey))
  282.         {
  283.             $arr_filter=array('MOD_ID' => intval($aKey));
  284.         else if (!empty($aKey)) {
  285.             $arr_filter=array('MOD_PREFIX' => $aKey);
  286.         else {
  287.             throw new Exception(_OCSP_EXCEP_NOID_);
  288.         }
  289.         
  290.         if ($debugechoDebugLine(__FILE__,__LINE__,"Filter: <pre>".print_r($arr_filter,True"</pre>");
  291.         
  292.         if ($arr_row OCSP_OBJ::defaultReadDBObj()->getRow('T_MOD_MODULES',$arr_filter,$debug))
  293.         {
  294.             return self::factoryFromRow($arr_row['MOD_PREFIX'],$arr_row,$debug);
  295.         else {
  296.             throw new Exception(_OCSP_EXCEP_NODATA_.":T_MOD_MODULES Filter: " print_r($arr_filter));        
  297.         }
  298.     }
  299.     
  300.     
  301.     /**
  302.      * factory the object from a data row
  303.      *
  304.      * @param string $modPrefix 
  305.      * @param array $dbRow 
  306.      * @param boolean $debug 
  307.      * 
  308.      * @return OCSP_MODULE 
  309.      */
  310.     public static function &factoryFromRow($modPrefix,$dbRow,$debug=False)
  311.     {
  312.         self::$loadedModules[$modPrefixnew OCSP_MODULE();
  313.         self::$loadedModules[$modPrefix]->setDBRow($dbRow,True,$debug);
  314.         self::$knownModules[$dbRow['MOD_ID']] $dbRow['MOD_PREFIX'];
  315.         return self::$loadedModules[$modPrefix];
  316.     }
  317.     
  318.     
  319.     /*** getter / setter ---------------------------------------------- */
  320.     
  321.     /**
  322.      * returns the module id
  323.      *
  324.      * @return int 
  325.      */
  326.     public function getId()
  327.     {
  328.         return $this->getDBField('MOD_ID');
  329.     }
  330.     
  331.     /**
  332.      * @return string 
  333.      */
  334.     public function getPrefix()
  335.     {
  336.         return $this->getDBField('MOD_PREFIX');
  337.     }
  338.         
  339.     // ----------------------------------------------------------------
  340.     // includes path
  341.     // ----------------------------------------------------------------
  342.     
  343.     /**
  344.      * returns the absolute module path
  345.      *
  346.      * @return string 
  347.      */
  348.     public function getModulePath($debug=False)
  349.     {        
  350.         $str_return $this->getDBField('MOD_PATH');
  351.         if (strstr($str_return,_OCSP_DIRSEP_!== 0)
  352.         {
  353.             $str_return  OCSP_CONF::getInstance()->getValue('MODULEPATH'$str_return;
  354.             if ($debugechoDebugLine(__FILE__,__LINE__,"<h1>relativ path adding" OCSP_CONF::getInstance()->getValue('MODULEPATH'"</h1>");
  355.         else {
  356.             if ($debugechoDebugLine(__FILE__,__LINE__,"<h1>absolut path </h1>");            
  357.         }
  358.         return $str_return;
  359.     }
  360.     
  361.     /**
  362.      * returns the absolute module include path
  363.      * 
  364.      * @return string 
  365.      */
  366.     public function getModuleIncPath()
  367.     {
  368.         return $this->getModulePath("php" _OCSP_DIRSEP_;
  369.     }
  370.  
  371.     /**
  372.      * requires a class file for $className
  373.      *
  374.      * @param string $className 
  375.      * @param string $subPath 
  376.      * @param boolean $debug 
  377.      */
  378.     public function require_class($className,$subPath="",$debug=False)
  379.     {
  380.         if($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::require_class({$className})");
  381.         if (!class_exists($className))
  382.         {
  383.             if ($debugechoDebugLine(__FILE__,__LINE__,"include: "$this->getModuleIncPath().$subPath);
  384.             pcf_require_class($className,$this->getModuleIncPath().$subPath);
  385.         }
  386.     }
  387.     
  388.     // ----------------------------------------------------------------
  389.     // urls
  390.     // ----------------------------------------------------------------
  391.     
  392.     /**
  393.      * returns the admin root url of the module
  394.      *
  395.      * @return string 
  396.      */
  397.     public function getAdminUrl()
  398.     {
  399.         return OCSP_CONF::getInstance()->getValue('ADMINURL'"modules/" $this->getPrefix("/";
  400.     }
  401.     
  402.     /*** cache methods   ---------------------------------------------- */
  403.  
  404.     /**
  405.      * returns the cache group name
  406.      *
  407.      * @return unknown 
  408.      */
  409.     public function getCacheGroup()
  410.     {
  411.         return 'MOD' $this->getDBField('MOD_PREFIX');
  412.     }
  413.     
  414.     /**
  415.      * caches the module to APC
  416.      *
  417.      * @param int $ttl 
  418.      * @param boolean $debug 
  419.      * 
  420.      * @return boolean 
  421.      */
  422.     public function cacheToApc($ttl=0,$debug)
  423.     {
  424.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::cacheToApc();");
  425.         
  426.         if (!$this->isPopulated()) return False;
  427.         
  428.         if (OCSP_CACHE_APC::isUseable())
  429.         {
  430.             OCSP_CACHE_APC::getInstance($this->getCacheGroup())->setValue('MODULE_ROW',$this->getDBRow(True,$debug),$ttl,True);
  431.             return True;
  432.         }
  433.         
  434.         return False;
  435.     }
  436.  
  437.     /**
  438.      * caches the module row to the file cache
  439.      *
  440.      * @param int $ttl 
  441.      * @param boolean $debug 
  442.      * @return boolean 
  443.      */
  444.     public function cacheToFile($ttl=0,$debug)
  445.     {
  446.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::cacheToFile();");
  447.         
  448.         if (!$this->isPopulated()) return False;
  449.         
  450.         if (OCSP_CACHE_FILE::isUseable())
  451.         {
  452.             OCSP_CACHE_FILE::getInstance($this->getCacheGroup())->setValue('MODULE_ROW',$this->getDBRow(True,$debug),$ttl,True);
  453.             return True;
  454.         }
  455.         
  456.         return False;
  457.     }
  458.     
  459.     /**
  460.      * clears the used caches
  461.      *
  462.      * @param boolean $debug 
  463.      */
  464.     public function clearCache($debug=False)
  465.     {
  466.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::clearCache();");
  467.         
  468.         if (OCSP_CACHE_APC::isUseable())
  469.         {
  470.             OCSP_CACHE_APC::getInstance($this->getCacheGroup())->unsetValue('MODULE_ROW');
  471.         }
  472.         if (OCSP_CACHE_FILE::isUseable())
  473.         {
  474.             OCSP_CACHE_FILE::getInstance($this->getCacheGroup())->unsetValue('MODULE_ROW');
  475.         }
  476.         
  477.     }
  478.     
  479.     // ----------------------------------------------------
  480.     // configuration
  481.     // ----------------------------------------------------
  482.  
  483.     /**
  484.      * populates all configuration values from the database
  485.      *
  486.      * @param boolean $debug 
  487.      */
  488.     protected function populateMyConf($debug=False)
  489.     {
  490.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::popuateMyConf()");
  491.         
  492.         if ($arr_confRows OCSP_OBJ::defaultReadDBObj()->getArray('T_MOD_SETTINGS',array('MOD_ID'=>$this->getId())))
  493.         {
  494.             $this->myLoadedConfValues = array();
  495.             foreach($arr_confRows as $arr_row)
  496.             {
  497.                 if (!empty($arr_row['MOS_VALUE']))
  498.                 {
  499.                     $this->myLoadedConfValues[$arr_row['MOS_NAME']] unserialize($arr_row['MOS_VALUE']);
  500.                 else {
  501.                     $this->myLoadedConfValues[$arr_row['MOS_NAME']] Null;
  502.                 }
  503.             }
  504.         else if ($debug{
  505.             echoDebugLine('Didi not found any settings from MOD_ID: ' $this->getId());
  506.         }
  507.                     
  508.         if ($debugechoDebugLine(__FILE__,__LINE__,"Settings: <pre>" print_r($this->myLoadedConfValues,True"</pre>");
  509.         $this->myLoadedConfValuesTS = time();
  510.     }
  511.     
  512.     /**
  513.      * returns a configuration value
  514.      *
  515.      * @param string $key 
  516.      * @param boolean $debug 
  517.      * 
  518.      * @return mixed 
  519.      * 
  520.      * @since pk-08-10-28
  521.      */
  522.     public function getMyConf($key,$debug=False)
  523.     {
  524.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::getMyConf({$key})");
  525.         
  526.         if (empty($key)) 
  527.         {
  528.             return False;
  529.         }
  530.         
  531.         if ($this->myLoadedConfValuesTS < (time(ini_get('max_execution_time')))
  532.         {
  533.             $this->populateMyConf($debug);
  534.         }
  535.         
  536.         if (!isset($this->myLoadedConfValues[$key]))
  537.         {
  538.             return Null;
  539.         
  540.         return $this->myLoadedConfValues[$key];
  541.     }
  542.     
  543.     /**
  544.      * sets a configuration option
  545.      *
  546.      * @param string $key 
  547.      * @param mixed $value 
  548.      * @param boolean $debug 
  549.      * 
  550.      * @since pk-08-10-28
  551.      */
  552.     public function setMyConf($key,$value,$debug=False)
  553.     {
  554.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::setMyConf({$key});",print_r($value));
  555.         
  556.         $this->myLoadedConfValues[$key$value;
  557.         $arr_row array(
  558.             'MOD_ID'     => $this->getId(),
  559.             'MOS_NAME'    => $key,
  560.             'MOS_VALUE' => serialize($value)
  561.         );
  562.         
  563.         OCSP_OBJ::defaultWriteDBObj()->replaceArray('T_MOD_SETTINGS',$arr_row);        
  564.     }
  565.     
  566.     /**
  567.      * sets a config setting only if it's not already set
  568.      *
  569.      * @param string $key 
  570.      * @param mixed $value 
  571.      * @param boolean $debug 
  572.      */
  573.     public function setNewMyConf($key,$value,$debug=False)
  574.     {
  575.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_MODULE::setNewMyConf({$key});",$value);
  576.         
  577.         if ($this->myLoadedConfValuesTS < (time(ini_get('max_execution_time')))
  578.         {
  579.             $this->populateMyConf($debug);
  580.         }
  581.         if (!isset($this->myLoadedConfValues[$key]))
  582.         {
  583.             $this->myLoadedConfValues[$key$value;
  584.             $arr_row array(
  585.                 'MOD_ID'     => $this->getId(),
  586.                 'MOS_NAME'    => $key,
  587.                 'MOS_VALUE' => serialize($value)
  588.             );
  589.             
  590.             OCSP_OBJ::defaultWriteDBObj()->insertArray('T_MOD_SETTINGS',$arr_row);                    
  591.         }
  592.     }
  593.     
  594. }
  595.  
  596. ?>

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