protected function __construct() { $this->config = Hayate_Config::load('session'); $driver = isset($this->config->session->driver) ? $this->config->session->driver : 'native'; switch ($driver) { case 'database': // can we get a db connection ? if (null === Hayate_Database::getInstance()) { throw new Hayate_Exception(sprintf(_('%s cannot use "database" driver as it is unable' . ' to retrieve a valid database connection.'), __CLASS__)); } $ses = Hayate_Session_Database::getInstance(); session_set_save_handler(array($ses, 'open'), array($ses, 'close'), array($ses, 'read'), array($ses, 'write'), array($ses, 'destroy'), array($ses, 'gc')); break; case 'native': break; default: throw new Hayate_Exception(sprintf(_('Session driver: "%s" not supported.'), $driver)); } // @see http://php.net/manual/en/function.session-write-close.php Hayate_Event::add('hayate.shutdown', 'session_write_close'); ini_set('session.use_only_cookies', true); ini_set('session.use_trans_sid', 0); session_name($this->config->get('session.name', 'HayateSession')); // session will not work with a domain without top level $domain = $this->config->get('session.domain', $_SERVER['SERVER_NAME']); if (preg_match('/^\\.?.+\\.[a-z]{2,4}$/i', $domain) != 1) { $domain = ''; } session_set_cookie_params((int) $this->config->get('session.lifetime', 0), $this->config->get('session.path', '/'), $domain, $this->config->get('session.secure', false), $this->config->get('session.httponly', false)); session_start(); Hayate_Log::info(sprintf(_('%s initialized.'), __CLASS__)); }
public function __destruct() { foreach (self::$db as &$pdo) { if ($pdo instanceof Hayate_Database_Pdo) { Hayate_Log::info('Closing db connection.'); $pdo = null; } } }
/** * This will trigger an exception if not overwritten */ public function __call($method, array $args) { Hayate_Log::info(__METHOD__ . ' ' . sprintf(_('method "%s" not found.'), $method)); throw new Hayate_Exception(sprintf(_('"%s" not found.'), Hayate_URI::getInstance()->current()), 400); }
/** * @param integer $maxlifetime Max session lifetime * @return boolean */ public function gc($maxlifetime) { Hayate_Log::info(__METHOD__); try { $lifetime = gmdate('U', time()) - $maxlifetime; $this->db->delete('sessions', array('access <' => $lifetime)); return true; } catch (Exception $ex) { Hayate_Log::error($ex, true); } return false; }
/** * Execute an sql query, if values contains elements the query is * first prepared * * @param string $query An sql query, optionally with place holders i.e. ... VALUES (?,?,?) * @param array $values If not empty values are going to be interpolate into the query * @param ORM|string $model If not null must be a model classname or model object * * @return int|array If the query was a DELETE, INSERT, or UPDATE * the number of affected rows is returned, for SELECT query an * array of rows is returned with SELECTs statements the * developers should be aware that the whole result set is going * to be hold in memory */ public function execute($query, array $values = array(), $model = null) { try { $stm = $this->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL)); if (count($values) > 0) { $i = 1; foreach ($values as $value) { switch (true) { case is_bool($value): $stm->bindParam($i++, $value, PDO::PARAM_BOOL); break; case is_null($value): $stm->bindParam($i++, $value, PDO::PARAM_NULL); break; case is_int($value): $stm->bindParam($i++, $value, PDO::PARAM_INT); break; case is_string($value): $stm->bindParam($i++, $value, PDO::PARAM_STR); break; default: $stm->bindParam($i++, $value); } } } $stm->execute(); // store the query string $this->lastQuery = $stm->queryString; // log the query Hayate_Log::info($stm->queryString); // reset query builder's properties after each query $this->reset(); // if (preg_match('/^DELETE|INSERT|UPDATE/i', $query) != 1) { return new Hayate_Database_Iterator($stm, $model, $this->fetchMode); } return $stm->rowCount(); } catch (PDOException $ex) { throw new Hayate_Database_Exception($ex); } catch (Exception $ex) { throw new Hayate_Database_Exception($ex); } }