/** * Stores value by key. * * @param string $key Cache key. * @param mixed $value Cache value. * @param integer $expire Expire time, in seconds(optional). * * @access public * @uses Core\Modules\DB\Query * * @return boolean */ public function store($key, $value, $expire = 0) { if ($expire) { $expire = time() + $expire; } $value = json_encode($value); $query = new Query(); $exists = $query->select('all')->from($this->tableName)->where("{$this->fields[0]} = ?", array(md5($key)))->first(); if (is_null($exists)) { $insert = $query->insert($this->fields, array(md5($key), $value, $expire))->into($this->tableName); return Core\DB()->run($insert); } else { $update = $query->update($this->tableName)->set($this->fields, array(md5($key), $value, $expire))->where("{$this->fields[0]} = ?", array(md5($key))); return Core\DB()->run($update); } }
/** * Retrieves the total count of the result set. * * @return integer */ public function getCount() { $obj = clone $this; $obj->query_options['limit'] = $obj->query_options['offset'] = null; return count(Core\DB()->run($obj)); }
/** * Assigns filtering of the results. * * @param DB\Query $query Current query object instance of BaseModel or its children. * @param array $params Query params to format filtering criteria. * * @access private * @static * @uses Core\DB() * @uses Core\DbCache() * @uses Core\Helpers\SQL * * @return Core\Modules\DB\Query */ private static function assignFilter(DB\Query $query, array $params) { if (isset($params['filtering']) && !empty($params['filtering']) && is_array($params['filtering'])) { $model_fields = $query->getObject()->getSchema(); if ($query->getObject()->hasAndBelongsToMany) { $model_fields = array_merge($model_fields, $query->getObject()->hasAndBelongsToMany); } foreach ($params['filtering'] as $field => $value) { if ($value && isset($model_fields[$field])) { if (is_array($value)) { if (isset($value['start'], $value['end']) && !empty($value['start']) && !empty($value['end'])) { if (in_array($model_fields[$field]['type'], array('date', 'datetime'), true)) { $decorator = 'Core\\Modules\\DB\\Decorators\\Interfaces\\TimezoneAwareness'; if (is_subclass_of($query->getObject(), $decorator)) { $value['start'] = Core\Helpers\DateTime::formatGmt($value['start'] . date(' H:i:s'), 'Y-m-d'); $value['end'] = Core\Helpers\DateTime::formatGmt($value['end'] . date(' H:i:s'), 'Y-m-d'); } $query = $query->where("(DATE({$field}) BETWEEN " . Core\DB()->escapeString($value['start']) . ' AND ' . Core\DB()->escapeString($value['end']) . ')'); } else { $query = $query->where("{$field} BETWEEN " . Core\DB()->escapeString($value['start']) . ' AND ' . Core\DB()->escapeString($value['end'])); } } else { if (isset($query->getObject()->hasAndBelongsToMany[$field]) && $query->getObject()->hasAndBelongsToMany[$field]) { $related = $query->getObject()->hasAndBelongsToMany[$field]; $obj = $query->getObject(); $primaryKey = $obj->primaryKeyField(); $prefix = Core\Config()->DB['tables_prefix']; foreach ($value as $v) { $query = $query->join("{$related['table']} as {$related['table']}{$v}", "{$prefix}{$obj::$tableName}.{$primaryKey} = " . "{$related['table']}{$v}.{$related['key']}" . ' AND ' . "{$related['table']}{$v}.{$related['relative_key']} = {$v}"); } } } } elseif (strlen($value)) { if ($model_fields[$field]['type'] === 'string') { $value_to_match = trim(Core\DB()->escapeString($value), "'"); $query = $query->where("{$field} LIKE \"%{$value_to_match}%\""); } else { $query = $query->where($field . ' = ' . Core\DB()->escapeString($value)); } } } } } return $query; }
/** * Retrieve meta information about supplied database table. * * @param string $tableName Name of the table. * * @throws \LogicException Error extracting schema meta information. * @access private * @uses Core\DB() * @uses associateType() * * @return array */ private function extractSchemaMeta($tableName) { $fields_meta = array(); $results = Core\DB()->getTableSchema($tableName, $this->dsn['name']); foreach ($results as $result) { $fields_meta[$result['COLUMN_NAME']]['type'] = $this->associateType($result['DATA_TYPE']); $fields_meta[$result['COLUMN_NAME']]['is_null'] = $result['IS_NULLABLE']; $fields_meta[$result['COLUMN_NAME']]['extra'] = $result['EXTRA']; $fields_meta[$result['COLUMN_NAME']]['default'] = $result['COLUMN_DEFAULT']; $fields_meta[$result['COLUMN_NAME']]['unique'] = $result['COLUMN_KEY'] == 'UNI'; /* If the field is string, varchar, text etc. add the max length of this field into the schema */ if ('string' === $fields_meta[$result['COLUMN_NAME']]['type']) { $fields_meta[$result['COLUMN_NAME']]['length'] = $result['CHARACTER_MAXIMUM_LENGTH']; } if ('enum' === $fields_meta[$result['COLUMN_NAME']]['type'] && isset($result['COLUMN_TYPE'])) { $fields_meta[$result['COLUMN_NAME']]['values'] = explode("','", str_replace(array("enum('", "')", "''"), array('', '', "'"), $result['COLUMN_TYPE'])); } } if (empty($fields_meta)) { throw new \LogicException('Error extracting schema meta information.'); } return $fields_meta; }
/** * Removes the record from the database. * * @access private * * @return boolean */ private function remove() { $query = new DB\Query(); $this->query = $query->remove()->from(static::$tableName)->where(static::$primaryKeyField . ' = ?', array($this->{static::$primaryKeyField})); if (static::$isI18n) { $query_i18n = new DB\Query(); $query_i18n = $query_i18n->remove()->from(static::$i18nTableName)->where(static::$i18nForeignKeyField . ' = ?', array($this->{static::$primaryKeyField})); return Core\DB()->run($this->query) && Core\DB()->run($query_i18n); } return Core\DB()->run($this->query); }
/** * Initialize variables. */ public function __construct() { if (!self::$started) { header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); header('Pragma: no-cache'); $this->db = Core\DB(); $this->cookie_path = Core\Config()->urls('relative'); self::$ip = $_SERVER['REMOTE_ADDR']; self::$userAgent = $_SERVER['HTTP_USER_AGENT']; self::$started = true; $this->storageTable = Core\Config()->DB['tables_prefix'] . 'sessions'; $this->storageVariablesTable = Core\Config()->DB['tables_prefix'] . 'session_vars'; if (Core\Config()->SESSION['transparency']) { $_COOKIE[Core\Config()->SESSION['name']] = isset($_REQUEST[Core\Config()->SESSION['parameter']]) ? $_REQUEST[Core\Config()->SESSION['parameter']] : null; } if (isset($_COOKIE[Core\Config()->SESSION['name']]) && strlen($_COOKIE[Core\Config()->SESSION['name']]) == Core\Config()->SESSION['key_length'] && $this->isSession($_COOKIE[Core\Config()->SESSION['name']])) { $this->sessionKey = $_COOKIE[Core\Config()->SESSION['name']]; if ($this->isValidHost()) { $this->loadVars($this->sessionKey); } $this->updateTimestamp($this->sessionKey); $this->killOld(); } $this->sessionKey = null; $this->start(); } }
/** * Remove Ownership Data for an Owner. * * @param \Core\Base\Model|null $owner Owner instance * * @return void */ public static function resetOwner(Base\Model $owner = null) { if (!$owner) { $owner = Core\Registry()->get('current_cms_user'); } $query = new Core\Modules\DB\Query(); Core\DB()->run($query->remove()->from('cms_ownership')->where('owner_id = ? AND model = ?', array($owner->getPrimaryKeyValue(), get_class($resource)))); }