vendor/cic/cssc/lib/COMMON.php line 30

Open in your IDE?
  1. <?php
  2. namespace CIC\CSSC;
  3. use CIC\DB\cicDatabaseInterface;
  4. class COMMON {
  5. protected cicDatabaseInterface $db;
  6. /**
  7.  * Config Loaded from DB using LoadConfig method
  8.  * @author Randy de Resendes
  9.  * @var array $Config
  10.  */
  11. public $Config=array();
  12. //Configured from $DB object
  13. //ZEBRA 
  14. public     $caching_method=null//defauts to DB setup but can be overriden
  15. public     $cache_path=__DIR__.'/../cache'//ditto
  16. protected $memcache_host;
  17. protected $memcache_port;
  18. protected $memcache_compressed;
  19. public    $cache;
  20. public      $APPID='CSSC';
  21. /**
  22.  * 
  23.  * @author Randy de Resendes
  24.  * @param \CIC\DB\cic_db $_db
  25.  */
  26. public function __construct(cicDatabaseInterface $_db$cache=null) {
  27.     $this->db=$_db
  28.     //$this->RefererURL=$_SERVER['PHP_SELF'];
  29.     //$this->REMOTE_IP=$_SERVER['REMOTE_ADDR'];
  30.     //$this->USER_AGENT=$_SERVER['HTTP_USER_AGENT'];
  31.     if(!is_null($cache)) $this->cache=$cache;
  32.         else $this->SetupCache();
  33.     $this->LoadConfig();
  34.     
  35.     }
  36. /**
  37.  * Load Config values from database table Config
  38.  * uses current CLASS NAME to get values
  39.  * Called as part of constructor
  40.  * @author Randy de Resendes
  41.  */
  42. public function LoadConfig() {
  43.     $q="select * from Config where (CLASS=? OR CLASS=?) ORDER BY DATASORTER,DATAVAL";
  44.     $this->db->query($q, array(get_class($this),get_class()));
  45.     $r=$this->db->fetch_assoc_all();
  46.     foreach($r as $rows) {
  47.     if(strpos($rows['DATASET'],':') !== FALSE) {
  48.         $tmp=explode(':',$rows['DATASET']);
  49.         $this->Config[$tmp[0]][$tmp[1]][$rows['DATAKEY']]=$rows['DATAVAL'];
  50.         unset($tmp);
  51.         }
  52.         else $this->Config[$rows['DATASET']][$rows['DATAKEY']]=$rows['DATAVAL'];
  53.     }
  54. }
  55. public function randomPassword($length,$count$characters="upper_case_clear,numbers") {
  56.  
  57. // $length - the length of the generated password
  58. // $count - number of passwords to be generated
  59. // $characters - types of characters to be used in the password
  60.  
  61. // define variables used within the function    
  62.     $symbols = array();
  63.     $passwords = array();
  64.     $used_symbols '';
  65.     $pass '';
  66.  
  67. // an array of different character types    
  68.     $symbols["lower_case"] = 'abcdefghijklmnopqrstuvwxyz';
  69.     $symbols["upper_case"] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  70.     $symbols["lower_case_clear"] = 'abcdefghjkmnpqrstuvwxyz';
  71.     $symbols["upper_case_clear"] = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
  72.     $symbols["numbers"] = '1234567890';
  73.     $symbols["special_symbols"] = '!?~@#-_+<>[]{}';
  74.  
  75.     $characters explode(",",$characters); // get characters types to be used for the passsword
  76.     foreach ($characters as $key=>$value) {
  77.         $used_symbols .= $symbols[$value]; // build a string with all characters
  78.     }
  79.     $symbols_length strlen($used_symbols) - 1//strlen starts from 0 so to get number of characters deduct 1
  80.      
  81.     for ($p 0$p $count$p++) {
  82.         $pass '';
  83.         for ($i 0$i $length$i++) {
  84.             $n rand(0$symbols_length); // get a random character from the string with all characters
  85.             $pass .= $used_symbols[$n]; // add the character to the password string
  86.         }
  87.         $passwords[] = $pass;
  88.     }
  89.      
  90.     return $passwords// return the generated password
  91. }
  92. public function setupCache() {
  93.     if($this->db->caching_method=='memcache') {
  94.         $this->memcache_host=$this->db->memcache_host;
  95.         $this->memcache_port=$this->db->memcache_port;
  96.         $this->memcache_compressed=$this->db->memcache_compressed;
  97.         //$this->cache=new \CIC\CACHE\MEMCACHE($this->memcache_host,$this->memcache_port,$this->memcache_compress);
  98.     
  99. //    $driver = new \Stash\Driver\Memcache();
  100.         $options=array('servers' => array($this->memcache_host$this->memcache_port));
  101.         $options['compressed']=$this->memcache_compressed;
  102.     $driver = new \Stash\Driver\Memcache($options);
  103.     //$driver->setOptions($options);
  104.     $this->cache=new \Stash\Pool($driver);
  105.         }
  106.         else {
  107.             //we caching to DISK
  108.             //ignore zebra case dir, use our own...user can overstep
  109.             $this->cache_path=__DIR__.'/../cache';
  110.         $options = array('path' => $this->cache_path);
  111.             $driver = new \Stash\Driver\FileSystem($options);
  112.         $this->cache=new \Stash\Pool($driver);
  113.             } 
  114.     }
  115. //Shortcut
  116. public function refreshItem($KEY) {
  117.     $this->cache->deleteItem($KEY);
  118. }
  119. public function storeEmptyItem($item,$expires=300) {
  120.     $item->set([]);
  121.     $item->expiresAfter($expires);
  122.     $this->cache->save($item);
  123. }
  124. }