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; } } }
/** * Hayate_Excetion * * @param string|Exception $message Error message or Wrapped exception * @param int $code Error code or number * @param Exception $prev From (php >= 5.3.0) * * This constructor also optionally accept and Exception object as * the 3rd argument (php >= 5.3.0) */ public function __construct($message = '', $code = 0, Exception $prev = null) { if ($message instanceof Exception) { parent::__construct($message->getMessage(), (int) $message->getCode()); $this->setFile($message->getFile()); $this->setLine($message->getLine()); require_once 'Hayate/Log.php'; Hayate_Log::error($message->getMessage()); } else { if (version_compare(PHP_VERSION, '5.3.0') >= 0) { parent::__construct($message, $code, $prev); } else { parent::__construct($message, $code); if ($prev instanceof Exception) { $this->setFile($prev->getFile()); $this->setLine($prev->getLine()); } } require_once 'Hayate/Log.php'; Hayate_Log::error($message); } }
/** * 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); }
public static function shutdown_function() { $error = error_get_last(); if (is_array($error)) { require_once 'Hayate/Exception.php'; $ex = new Hayate_Exception($error['message'], $error['type']); $ex->setFile($error['file']); $ex->setLine($error['line']); Hayate_Log::ex($ex); throw $ex; } }
protected function getSession($id, $mode = PDO::FETCH_OBJ) { try { $stm = $this->db->prepare('SELECT * FROM sessions WHERE session_id=:id LIMIT 1'); $stm->bindValue(':id', $id, PDO::PARAM_STR); $stm->execute(); $ses = $stm->fetch($mode); $stm->closeCursor(); return $ses; } 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); } }
public static function shutdown_function() { $error = error_get_last(); if (is_array($error)) { $msg = 'Fatal error: ' . $error['message'] . ' in: ' . $error['file'] . ' on line: ' . $error['line']; Hayate_Log::error($msg); } }
/** * @return bool TRUE if credentials are valid, FALSE otherwise */ public function authenticate($identifier, $secret, $salt = '', $algo = NULL, $remember = FALSE) { $this->setStatus(FALSE); if (is_string($algo)) { $secret = hash($algo, $salt . $secret); } try { $this->identity = $this->db->from($this->table)->where($this->identifier, $identifier)->get(); if (!$this->identity) { $this->setStatus(self::ERROR_IDENTIFIER); return FALSE; } if (0 !== strcmp($secret, $this->identity->{$this->secret})) { $this->setStatus(self::ERROR_SECRET); return FALSE; } // unset the secret unset($this->identity->{$this->secret}); // store in session $this->session->set(self::AUTHID, $this->identity); if (is_numeric($remember)) { $this->cookie->set(self::AUTHID, $this->identity, $remember); } $this->setStatus(self::SUCCESS); return TRUE; } catch (Exception $ex) { Hayate_Log::error($ex); } return FALSE; }