/** * Save data to cache. * * @param string $key Data to save as. * @param mixed $value Data to save. * @param boolean $db Cache to database. * * @return void */ public static function saveData($key, $value, $db = false) { $cache = Cache::getInstance(); $cache->data[$key] = $value; if ($key != 'ModelCachingFields' && $db && Config::getSetting('cache_to_database', false, false) && Clockwork::isModuleLoaded('Data/Database')) { $lifespan = $db === true ? 0 : $db; if (($obj = Caching::create($key, '*`key`')) === false) { $obj = new Caching(); } $obj->set('key', $key)->set('value', serialize($value))->set('object', is_object($value) ? get_class($value) : '')->set('lifespan', $lifespan)->save(); } }
} // --- CSRF if (Clockwork::isModuleLoaded('CSRF') && $_POST) { try { CSRF::check($_POST['CSRF-key']); } catch (Exception $e) { unset($_POST); $_POST = []; } } // --- locale if (Config::getSetting('locale', false)) { setlocale(LC_ALL, Config::getSetting('locale')); } // --- Login if (Clockwork::isModuleLoaded('Login') && !defined('CW_CRON') && !defined('CW_SKIP_LOGIN')) { new Login(); $_loginpage = Config::getSetting('login_loginpage', false, false) ? Config::getSetting('login_loginpage') : 'login/'; $_allowed = Config::getSetting('login_no_login', false, false) ? Config::getSetting('login_no_login') : []; $_allowed[] = $_loginpage; if (!Login::getUser() && !in_array(substr(strpos($_SERVER['REQUEST_URI'], '?') !== false ? stristr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI'], 1), $_allowed)) { redirect($_loginpage); } } // --- app if (!defined('CW_CRON') && !defined('CW_OVERRIDE_TEMPLATE')) { if (Config::getSetting('load_template_engine', false, true) && !isset($template)) { $template = new Template(); include_once APP_DIR . 'index.php'; $template->loadFromURL(); } else {
/** * Get columns from database. * * @param string|boolean $key Field to return. * * @return array */ public function getFields($key = null) { if (Clockwork::isModuleLoaded('Cache') && count($this->fields) == 0) { $this->fields = Cache::loadData('Model' . ucfirst($this->modelName) . 'Fields'); } if (count($this->fields) == 0 && Clockwork::isModuleLoaded('Data/Database')) { $this->fields = Database::getInstance()->fetchAll("SHOW COLUMNS FROM " . $this->tableName); if (Clockwork::isModuleLoaded('Cache')) { Cache::saveData('Model' . ucfirst($this->modelName) . 'Fields', $this->fields, true); } } if (!$key) { return $this->fields; } else { if ($key === true) { $fields = array(); foreach ($this->fields as $field) { $fields[] = $field['Field']; } return $fields; } else { if ($key == 'primary' && !empty($this->fields)) { $pri = array(); foreach ($this->fields as $field) { if ($field['Key'] == 'PRI') { $pri[] = $field['Field']; } } return $pri; } else { if (!empty($this->fields)) { foreach ($this->fields as $field) { if ($field['Field'] == $key) { return $field; } } } return null; } } } }
/** * Update an object in the database, returns object on failure. * * @param object $object Object to update. * @param int $step Step to use in check(). * @param string $column Column to use in WHERE statement. Defaults to primary key. * * @return boolean|object */ protected function updateObject(&$object, $step = 0, $column = null) { if (!Clockwork::isModuleLoaded('Data/Database')) { return; } $database = Database::getInstance(); if ($object->check($step)) { if ($this->getFields('edited')) { $object->set('edited', date('Y-m-d H:i:s')); } $params = array(); $sql = "UPDATE " . $this->tableName . " SET "; //check fields if value is given $fields = $this->getFields(true); foreach ($fields as $field) { $found = false; foreach ($object->values as $f => $value) { if ($field == $f) { $found = true; } } if (!$found) { unset($fields[array_search($field, $fields)]); } } //query foreach ($fields as $field) { $info = $object->getFields($field); $sql .= "`" . $field . "` = :" . $field . ", "; $params[$field] = $object->get($field, -1); } $sql = substr($sql, 0, -2) . " WHERE "; if ($column === null) { $primary = $object->getFields('primary'); foreach ($primary as $field) { $sql .= "`" . $field . "` = :" . $field . " AND "; $params[$field] = $object->get($field, -1); } $sql = substr($sql, 0, -5); } else { $sql .= $column . " = '" . $object->get($column, -1) . "'"; } // if ($database->query($sql, $params)) { return true; } else { $object->setError('handle', 'Update query failed'); } } return $object; }
/** * Constructor * * @param mixed $settings (optional) * * @return void */ public function __construct($settings = null) { // >= v3.x if ($settings === null || is_array($settings)) { $defaults = ['basedir' => APP_DIR, 'loadFromUrl' => false, 'useLayoutBasedir' => true, 'view' => false]; $settings = $settings ? array_merge($defaults, $settings) : $defaults; $this->basedir = $settings['basedir']; $this->useBasedir = $settings['useLayoutBasedir']; } if (Clockwork::isModuleLoaded('Twig/Autoloader')) { $this->setupTwig(); } $this->url = $this->parseUrl(); // >= v3.x if (is_array($settings)) { if ($settings['loadFromUrl'] === true) { $this->loadFromURL(); } if ($settings['view'] !== false) { $this->load($settings['view']); } } else { if ($view === true) { $this->loadFromURL(); } else { if ($view) { $this->load($settings['view']); } } } }