<?php
namespace CIC\CSSC;
use CIC\DB\cicDatabaseInterface;
class COMMON {
protected cicDatabaseInterface $db;
/**
* Config Loaded from DB using LoadConfig method
* @author Randy de Resendes
* @var array $Config
*/
public $Config=array();
//Configured from $DB object
//ZEBRA
public $caching_method=null; //defauts to DB setup but can be overriden
public $cache_path=__DIR__.'/../cache'; //ditto
protected $memcache_host;
protected $memcache_port;
protected $memcache_compressed;
public $cache;
public $APPID='CSSC';
/**
*
* @author Randy de Resendes
* @param \CIC\DB\cic_db $_db
*/
public function __construct(cicDatabaseInterface $_db, $cache=null) {
$this->db=$_db;
//$this->RefererURL=$_SERVER['PHP_SELF'];
//$this->REMOTE_IP=$_SERVER['REMOTE_ADDR'];
//$this->USER_AGENT=$_SERVER['HTTP_USER_AGENT'];
if(!is_null($cache)) $this->cache=$cache;
else $this->SetupCache();
$this->LoadConfig();
}
/**
* Load Config values from database table Config
* uses current CLASS NAME to get values
* Called as part of constructor
* @author Randy de Resendes
*/
public function LoadConfig() {
$q="select * from Config where (CLASS=? OR CLASS=?) ORDER BY DATASORTER,DATAVAL";
$this->db->query($q, array(get_class($this),get_class()));
$r=$this->db->fetch_assoc_all();
foreach($r as $rows) {
if(strpos($rows['DATASET'],':') !== FALSE) {
$tmp=explode(':',$rows['DATASET']);
$this->Config[$tmp[0]][$tmp[1]][$rows['DATAKEY']]=$rows['DATAVAL'];
unset($tmp);
}
else $this->Config[$rows['DATASET']][$rows['DATAKEY']]=$rows['DATAVAL'];
}
}
public function randomPassword($length,$count, $characters="upper_case_clear,numbers") {
// $length - the length of the generated password
// $count - number of passwords to be generated
// $characters - types of characters to be used in the password
// define variables used within the function
$symbols = array();
$passwords = array();
$used_symbols = '';
$pass = '';
// an array of different character types
$symbols["lower_case"] = 'abcdefghijklmnopqrstuvwxyz';
$symbols["upper_case"] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$symbols["lower_case_clear"] = 'abcdefghjkmnpqrstuvwxyz';
$symbols["upper_case_clear"] = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
$symbols["numbers"] = '1234567890';
$symbols["special_symbols"] = '!?~@#-_+<>[]{}';
$characters = explode(",",$characters); // get characters types to be used for the passsword
foreach ($characters as $key=>$value) {
$used_symbols .= $symbols[$value]; // build a string with all characters
}
$symbols_length = strlen($used_symbols) - 1; //strlen starts from 0 so to get number of characters deduct 1
for ($p = 0; $p < $count; $p++) {
$pass = '';
for ($i = 0; $i < $length; $i++) {
$n = rand(0, $symbols_length); // get a random character from the string with all characters
$pass .= $used_symbols[$n]; // add the character to the password string
}
$passwords[] = $pass;
}
return $passwords; // return the generated password
}
public function setupCache() {
if($this->db->caching_method=='memcache') {
$this->memcache_host=$this->db->memcache_host;
$this->memcache_port=$this->db->memcache_port;
$this->memcache_compressed=$this->db->memcache_compressed;
//$this->cache=new \CIC\CACHE\MEMCACHE($this->memcache_host,$this->memcache_port,$this->memcache_compress);
// $driver = new \Stash\Driver\Memcache();
$options=array('servers' => array($this->memcache_host, $this->memcache_port));
$options['compressed']=$this->memcache_compressed;
$driver = new \Stash\Driver\Memcache($options);
//$driver->setOptions($options);
$this->cache=new \Stash\Pool($driver);
}
else {
//we caching to DISK
//ignore zebra case dir, use our own...user can overstep
$this->cache_path=__DIR__.'/../cache';
$options = array('path' => $this->cache_path);
$driver = new \Stash\Driver\FileSystem($options);
$this->cache=new \Stash\Pool($driver);
}
}
//Shortcut
public function refreshItem($KEY) {
$this->cache->deleteItem($KEY);
}
public function storeEmptyItem($item,$expires=300) {
$item->set([]);
$item->expiresAfter($expires);
$this->cache->save($item);
}
}