public function testCache() { $this->assertTrue(!Cache::exist($this->cacheVarName), "Not set variable exists"); Cache::set($this->cacheVarName, 'a.b.c'); $this->assertTrue(Cache::exist($this->cacheVarName), "Set variable doesnt exist"); $this->assertTrue(Cache::get($this->cacheVarName, 'qwe') == 'a.b.c', "Cache value doesnt match"); Cache::remove($this->cacheVarName); $this->assertTrue(!Cache::exist($this->cacheVarName), "Removed value exists"); }
private function updateCronLastActionTime($cron_id) { if (empty($cron_id)) { return; } $cache_var_name = 'update_cron_' . $cron_id; /* if less than 20 seconds have passed then skip updating db */ if (\CB\Cache::exist($cache_var_name) && time() - \CB\Cache::get($cache_var_name) < 20) { return; } \CB\Cache::set($cache_var_name, time()); DB\dbQuery('UPDATE crons SET last_action = CURRENT_TIMESTAMP WHERE cron_id = $1', $cron_id) or die('error updating crons last action'); }
/** * get template type by its id * @param int $templateId * @return varchar */ public function getType($templateId) { if (!is_numeric($templateId)) { return null; } // check if template has been loaded if (!empty($this->templates[$templateId])) { return $this->templates[$templateId]->getData()['type']; } $var_name = 'template_type' . $templateId; if (!\CB\Cache::exist($var_name)) { $res = DB\dbQuery('SELECT `type` FROM templates WHERE id = $1', $templateId) or die(DB\dbQueryError()); if ($r = $res->fetch_assoc()) { \CB\Cache::set($var_name, $r['type']); } $res->close(); } return \CB\Cache::get($var_name); }
/** * get objects from cache or loads them and store in cache * @param array $ids * @return array */ public static function getCachedObjects($ids) { $ids = Util\toNumericArray($ids); $rez = array(); $toLoad = array(); foreach ($ids as $id) { //verify if already have cached result $var_name = 'Objects[' . $id . ']'; if (\CB\Cache::exist($var_name)) { $rez[$id] = \CB\Cache::get($var_name); } else { $toLoad[] = $id; } } if (!empty($toLoad)) { $tc = Templates\SingletonCollection::getInstance(); $data = DataModel\Objects::read($toLoad); foreach ($data as $objData) { $var_name = 'Objects[' . $objData['id'] . ']'; $o = static::getCustomClassByType($tc->getType($objData['template_id'])); if (!empty($o)) { $o->setData($objData); \CB\Cache::set($var_name, $o); $rez[$objData['id']] = $o; } } } return $rez; }
/** * Check if userId (or current loged user) is an administrator * * @param int $userId * @return boolean */ public static function isAdmin($userId = false) { if ($userId == false) { $userId = User::getId(); } $var_name = 'is_admin' . $userId; if (!Cache::exist($var_name)) { Cache::set($var_name, DM\Users::getIdByName('root') == $userId); } return Cache::get($var_name); }
/** * function to display errors in interactive mode or to raise them * @param varchar $error * @return void */ function displayError($error) { if (\CB\Cache::exist('RUN_SETUP_INTERACTIVE_MODE')) { if (\CB\Cache::get('RUN_SETUP_INTERACTIVE_MODE')) { showError($error); return; } } trigger_error($error, E_USER_ERROR); }
/** * get template type of an object * @param int $objectId * @return varchar|null */ public static function getType($objectId) { if (!is_numeric($objectId)) { return null; } $var_name = 'obj_template_type' . $objectId; if (!Cache::exist($var_name)) { $tc = Templates\SingletonCollection::getInstance(); Cache::set($var_name, $tc->getType(self::getTemplateId($objectId))); } return Cache::get($var_name); }
/** * get display data for given ids * @param varchar|array $ids * @return associative array of users display data */ public static function getDisplayData($ids) { $rez = array(); $ids = Util\toNumericArray($ids); if (!empty($ids)) { $cdd = array(); if (Cache::exist('UsersGroupsDisplayData')) { $cdd = Cache::get('UsersGroupsDisplayData'); } else { $cdd = DataModel\UsersGroups::getDisplayData(); Cache::set('UsersGroupsDisplayData', $cdd); } $rez = array_intersect_key($cdd, array_flip($ids)); } return $rez; }
/** * get photo param to be added for photo urls * @param $idOrData * @return varchar */ public static function getPhotoParam($idOrData = false) { $data = array(); if ($idOrData === false) { //use current logged users $id = static::getId(); } elseif (is_numeric($idOrData)) { //id specified $id = $idOrData; } elseif (is_array($idOrData) && !empty($idOrData['id']) && is_numeric($idOrData['id'])) { $id = $idOrData['id']; $data = $idOrData; } else { return ''; } $var_name = 'users[' . $id . "]['photoParam']"; if (!Cache::exist($var_name)) { if (empty($data)) { $data = DM\Users::read($id); } $rez = ''; $photosPath = Config::get('photos_path'); $photoFile = $photosPath . $data['photo']; if (file_exists($photoFile)) { $rez = date('ynjGis', filemtime($photoFile)); } Cache::set($var_name, $rez); } return Cache::get($var_name); }
private function updateCronLastActionTime($cronId) { if (empty($cronId)) { return; } $cache_var_name = 'update_cron_' . $cronId; /* if less than 20 seconds have passed then skip updating db */ if (\CB\Cache::exist($cache_var_name) && time() - \CB\Cache::get($cache_var_name) < 20) { return; } \CB\Cache::set($cache_var_name, time()); $id = DM\Crons::toId($cronId, 'cron_id'); if (empty($id)) { DM\Crons::create(array('cron_id' => $cronId, 'last_action' => 'CURRENT_TIMESTAMP')); } else { DM\Crons::update(array('id' => $id, 'last_action' => 'CURRENT_TIMESTAMP')); } }
/** * formats a value for display according to it's field definition * @param array | int $field array of field properties or field id * @param variant $value field value to be formated * @param boolean $html default true - format for html, otherwise format for text display * @return varchar formated value */ public static function formatValueForDisplay($field, $value, $html = true) { $cacheVarName = ''; if (is_numeric($field)) { $field = $this->data->fields[$field]; } //condition is specified for values from search templates $condition = null; if (is_array($value)) { if (isset($value['cond'])) { $condition = Template::formatConditionForDisplay($field, $value['cond'], $html) . ' '; } if (isset($value['value'])) { $value = $value['value']; } else { $value = null; } } //we'll cache scalar by default, but will exclude textual fields $cacheValue = is_scalar($value); if ($cacheValue) { $fid = empty($field['id']) ? $field['name'] : $field['id']; $cacheVarName = 'dv' . $html . '_' . $fid . '_' . $value; //check if value is in cache and return if (Cache::exist($cacheVarName)) { return Cache::get($cacheVarName); } } /*check if field is not rezerved field for usernames (cid, oid, uid, did)*/ if (!empty($field['name']) && in_array($field['name'], array('cid', 'oid', 'uid', 'did'))) { $value = Util\toNumericArray($value); for ($i = 0; $i < sizeof($value); $i++) { $value[$i] = User::getDisplayName($value[$i]); } $value = implode(', ', $value); } else { switch ($field['type']) { case 'boolean': case 'checkbox': $value = empty($value) ? '' : ($value < 0 ? L\get('no') : L\get('yes')); break; case '_sex': switch ($value) { case 'm': $value = L\get('male'); break; case 'f': $value = L\get('female'); break; default: $value = ''; } break; case '_language': @($value = @\CB\Config::get('language_settings')[\CB\Config::get('languages')[$value - 1]][0]); break; case 'combo': case '_objects': if (empty($value)) { $value = ''; break; } $ids = Util\toNumericArray($value); if (empty($ids)) { if (empty($field['cfg']['source']) || !is_array($field['cfg']['source'])) { $value = ''; } break; } $value = array(); if (in_array(@$field['cfg']['source'], array('users', 'groups', 'usersgroups'))) { $udp = UsersGroups::getDisplayData($ids); foreach ($ids as $id) { if (empty($udp[$id])) { continue; } $r =& $udp[$id]; $label = @htmlspecialchars(Util\coalesce($r['title'], $r['name']), ENT_COMPAT); if ($html) { switch (@$field['cfg']['renderer']) { case 'listGreenIcons': $label = '<li class="icon-padding icon-element">' . $label . '</li>'; break; // case 'listObjIcons': // case 'listObjIcons': default: $icon = empty($r['iconCls']) ? 'icon-none' : $r['iconCls']; $label = '<li class="icon-padding ' . $icon . '">' . $label . '</li>'; break; } } $value[] = $label; } } else { $objects = \CB\Objects::getCachedObjects($ids); foreach ($ids as $id) { if (empty($objects[$id])) { continue; } $obj =& $objects[$id]; $d = $obj->getData(); $label = $obj->getHtmlSafeName(); $pids = $d['pids']; if ($html && !empty($pids)) { $pids = str_replace(',', '/', $pids); $linkType = empty($field['cfg']['linkType']) ? '' : 'link-type-' . $field['cfg']['linkType']; $label = '<a class="click ' . $linkType . '" template_id="' . $d['template_id'] . '" path="' . $pids . '" nid="' . $id . '">' . $label . '</a>'; } switch (@$field['cfg']['renderer']) { case 'listGreenIcons': $value[] = $html ? '<li class="icon-padding icon-element">' . $label . '</li>' : $label; break; // case 'listObjIcons': // case 'listObjIcons': default: $icon = \CB\Browser::getIcon($d); if (empty($icon)) { $icon = 'icon-none'; } $value[] = $html ? '<li class="icon-padding ' . $icon . '">' . $label . '</li>' : $label; break; } } } $value = $html ? '<ul class="clean">' . implode('', $value) . '</ul>' : implode(', ', $value); break; case '_fieldTypesCombo': $value = L\get(@static::$fieldTypeNames[$value]); break; case 'date': $value = Util\formatMysqlDate(Util\dateISOToMysql($value)); break; case 'datetime': $value = Util\UTCTimeToUserTimezone($value); break; case 'time': if (empty($value)) { continue; } $format = empty($field['format']) ? 'H:i' : $field['format']; if (is_numeric($value)) { $s = $value % 60; $value = floor($value / 60); $m = $value % 60; $value = floor($value / 60); if (strlen($value) < 2) { $value = '0' . $value; } if (strlen($m) < 2) { $m = '0' . $m; } $value .= ':' . $m; if (!empty($s)) { if (strlen($s) < 2) { $s = '0' . $s; } $value .= ':' . $s; } } else { $date = \DateTime::createFromFormat($format, $value); if (is_object($date)) { $value = $date->format($format); } } break; case 'html': $cacheValue = false; // $value = trim(strip_tags($value)); // $value = nl2br($value); break; case 'varchar': case 'memo': case 'text': $cacheValue = false; $renderers = ''; if (!empty($field['cfg']['linkRenderers'])) { $renderers = $field['cfg']['linkRenderers']; } elseif (!empty($field['cfg']['text_renderer'])) { $renderers = $field['cfg']['text_renderer']; } $value = empty($renderers) ? nl2br(htmlspecialchars($value, ENT_COMPAT)) : nl2br(Comment::processAndFormatMessage($value), $renderers); break; default: if (is_array($value)) { $cacheValue = false; $value = Util\jsonEncode($value); } else { $value = htmlspecialchars($value, ENT_COMPAT); } } } if ($cacheValue) { Cache::set($cacheVarName, $condition . $value); } return $condition . $value; }
/** * stores loaded nodes in 'DAVNodes' ary, * later it's used in PropertyStoragePlugin to get 'creationdate' * @param [type] $ary [description] * @return [type] [description] */ public static function cacheNodes($ary) { // initialize DAVNodes cache if (!\CB\Cache::exist('DAVNodes')) { \CB\Cache::set('DAVNodes', []); } $cachedNodes = \CB\Cache::get('DAVNodes'); // store nodes in cache foreach ($ary as $id => $node) { // remove '/' $path = str_replace('\\', '/', $node['path']); $path = trim($path, '/'); // use only '/' as separator $cachedNodes[$path] = $node; } \CB\Cache::set('DAVNodes', $cachedNodes); }
$coreName = empty($options['c']) ? @$options['core'] : $options['c']; if (empty($coreName)) { die('no core specified or invalid options set.'); } $sqlFile = empty($options['s']) ? @$options['sql'] : $options['s']; if (empty($sqlFile)) { $ds = DIRECTORY_SEPARATOR; $sqlFile = $cbHome . "install{$ds}mysql{$ds}bare_bone_core.sql"; } /*if (!defined('CB\INTERACTIVE_MODE')) { //define working mode define('CB\INTERACTIVE_MODE', empty($options['config'])); } */ if (!\CB\Cache::get('RUN_SETUP_INTERACTIVE_MODE')) { //define working mode \CB\Cache::set('RUN_SETUP_INTERACTIVE_MODE', !\CB\Cache::exist('RUN_SETUP_CFG')); } \CB\Install\defineBackupDir($cfg); $dbName = (isset($cfg['prefix']) ? $cfg['prefix'] . '_' : \CB\PREFIX) . $coreName; $dbUser = isset($cfg['su_db_user']) ? $cfg['su_db_user'] : $cfg['db_user']; $dbPass = isset($cfg['su_db_pass']) ? $cfg['su_db_pass'] : $cfg['db_pass']; $applyDump = true; if (\CB\DB\dbQuery('use `' . $dbName . '`')) { if (confirm('overwrite_existing_core_db')) { if (\CB\Cache::get('RUN_SETUP_CREATE_BACKUPS') !== false) { echo 'Backuping .. '; backupDB($dbName, $dbUser, $dbPass); showMessage(); } } else { $applyDump = false;
/** * get param for this node * * @param varchar $param for now using to get 'facets' or 'DC' * @return array */ public function getNodeParam($param = 'facets') { $rez = false; $from = $this->getId(); //check if cached $cacheParam = 'nodeParam_' . $param . '_' . $from; if (Cache::exist($cacheParam)) { return Cache::get($cacheParam); } //select configs from tree and template of the current node $sql = 'SELECT t.cfg, t.template_id, tt.cfg `templateCfg` FROM tree t LEFT JOIN templates tt ON (t.template_id = tt.id) OR ((tt.id = $2) AND (t.template_id IS NULL)) WHERE t.id = $1'; //if template_id specified in config then select directly from templates table if (empty($from) && !empty($this->config['template_id'])) { $from = 'template_' . $this->config['template_id']; $sql = 'SELECT null `cfg`, t.id template_id, t.cfg `templateCfg` FROM templates t WHERE t.id = $2'; } //check node configuration and/or its template for facets definitions $res = DB\dbQuery($sql, array($this->id, @$this->config['template_id'])) or die(DB\dbQueryError()); if ($r = $res->fetch_assoc()) { $cfg = Util\toJSONArray($r['cfg']); if (isset($cfg[$param])) { $rez = $cfg[$param]; } else { $cfg = Util\toJSONArray($r['templateCfg']); if (isset($cfg[$param])) { $rez = $cfg[$param]; $from = 'template_' . $r['template_id']; } } //add grouping param for DC if ($param == 'DC' && $rez !== false) { if (!empty($cfg['view']['group'])) { $rez['group'] = $cfg['view']['group']; } elseif (!empty($cfg['group'])) { $rez['group'] = $cfg['group']; } } } $res->close(); if ($rez === false) { $rez = parent::getNodeParam($param); } else { $rez = array('from' => $from, 'data' => $rez); } Cache::set($cacheParam, $rez); return $rez; }
/** * get param for this node * * @param varchar $param for now using to get 'facets' or 'DC' * @return array */ public function getNodeParam($param = 'facets') { $rez = false; $from = $this->getId(); //check if cached $cacheParam = 'nodeParam_' . $param . '_' . $from; if (Cache::exist($cacheParam)) { return Cache::get($cacheParam); } $cfg = array(); $templateId = null; $tplCfg = array(); if (!empty($this->id) && is_numeric($this->id)) { $r = DM\Tree::read($this->id); } if (!empty($r)) { $cfg = $r['cfg']; $templateId = $r['template_id']; } if (!empty($this->config['template_id'])) { $templateId = $this->config['template_id']; } if (!empty($templateId)) { $r = DM\Templates::read($templateId); if (!empty($r)) { $tplCfg = $r['cfg']; } } if (isset($cfg[$param])) { $rez = $cfg[$param]; } elseif (isset($tplCfg[$param])) { $cfg = $tplCfg; $rez = $cfg[$param]; $from = 'template_' . $templateId; } //add grouping param for DC if ($param == 'DC' && $rez !== false) { if (!empty($cfg['view']['group'])) { $rez['group'] = $cfg['view']['group']; } elseif (!empty($cfg['group'])) { $rez['group'] = $cfg['group']; } } if ($rez === false) { $rez = parent::getNodeParam($param); } else { $rez = array('from' => $from, 'data' => $rez); } Cache::set($cacheParam, $rez); return $rez; }
/** * get template type by its id * @param int $id * @return varchar */ public function getType($id) { if (!is_numeric($id)) { return null; } // check if template has been loaded if (!empty($this->templates[$id])) { return $this->templates[$id]->getData()['type']; } $var_name = 'template_type' . $id; if (!\CB\Cache::exist($var_name)) { $r = DM\Templates::read($id); if (!empty($r)) { \CB\Cache::set($var_name, $r['type']); } } return \CB\Cache::get($var_name); }