Ejemplo n.º 1
0
 private function __construct()
 {
     if (!function_exists('mcrypt_module_open')) {
         throw new Hayate_Exception(sprintf(_('%s: mcrypt extension is missing.'), __CLASS__));
     }
     switch (self::ALGO) {
         case 'aes':
             $algo = MCRYPT_RIJNDAEL_256;
             break;
         case 'tripledes':
             $algo = MCRYPT_TRIPLEDES;
             break;
         case 'blowfish':
             $algo = MCRYPT_BLOWFISH;
             break;
         default:
             throw new Hayate_Exception(sprintf(_('%s is not supported, please use "aes", "tripledes" or "blowfish"'), self::ALGO));
     }
     // initialize mcrypt
     $this->mcrypt = mcrypt_module_open($algo, '', MCRYPT_MODE_CBC, '');
     // calculate IV size
     $this->ivsize = mcrypt_enc_get_iv_size($this->mcrypt);
     // calculate key max key length
     $this->maxKeysize = mcrypt_enc_get_key_size($this->mcrypt);
     $config = Hayate_Config::getInstance();
     if ($config->get('core.secret_key', false)) {
         $this->setKey($config->core->secret_key);
     }
 }
Ejemplo n.º 2
0
 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__));
 }
Ejemplo n.º 3
0
 protected function __construct()
 {
     $config = Hayate_Config::getInstance();
     if ((!$config->get('view', false) || !isset($config->core->view['smarty_dir'])) && !class_exists('Smarty', false)) {
         throw new Hayate_View_Exception(_('smarty_dir configuration parameter missing.'));
     }
     if (!class_exists('Smarty', false)) {
         require_once rtrim($config->core->view['smarty_dir'], '\\/') . '/Smarty.class.php';
     }
     // finally we can instantiate
     $this->smarty = new Smarty();
     // and set the properties values
     $ro = new ReflectionObject($this->smarty);
     foreach ($config->core->view as $prop => $val) {
         if ($ro->hasProperty($prop)) {
             $this->smarty->{$prop} = $val;
         }
     }
     /*
             if (version_compare($this->smarty->_version, '3.0') < 0)
             {
                 $this->is_smarty_2 = true;
             }
     */
 }
Ejemplo n.º 4
0
 protected function __construct()
 {
     $config = Hayate_Config::load('routes', false);
     if ($config && isset($config->routes)) {
         $this->routes = $config->routes->getArrayCopy();
     }
     if (count($this->routes)) {
         $keys = array_keys($this->routes);
         $values = array_values($this->routes);
         array_walk($keys, array($this, 'trimSlash'));
         array_walk($values, array($this, 'trimSlash'));
         $this->routes = array_combine($keys, $values);
     }
     $base_path = array();
     if (isset($config->core->base_path)) {
         $base_path = preg_split('|/|', $config->core->base_path, -1, PREG_SPLIT_NO_EMPTY);
     }
     $segments = Hayate_URI::getInstance()->segments();
     for ($i = 0; $i < count($base_path); $i++) {
         if (isset($segments[$i]) && $segments[$i] == $base_path[$i]) {
             unset($segments[$i]);
         }
     }
     $this->path = $this->routed_path = implode('/', $segments);
 }
Ejemplo n.º 5
0
 /**
  * @param string $name Name of the configuration file to load, the
  * default 'core' loads the main application config.php
  * file
  * @return Hayate_Config The loaded configuration file
  */
 public static function load($name = 'core', $required = true)
 {
     if (null === self::$instance) {
         self::$instance = new self();
     }
     self::$instance->_load($name, $required);
     return self::$instance;
 }
Ejemplo n.º 6
0
 protected function __construct()
 {
     $this->config = Hayate_Config::getInstance();
     $this->current = $this->scheme() . '://' . $this->hostname() . '/' . $this->path() . $this->query(true);
     $this->segments = preg_split('|/|', $this->path(), -1, PREG_SPLIT_NO_EMPTY);
     // make sure all segments are lower case
     $this->segments = array_map('mb_strtolower', $this->segments);
 }
Ejemplo n.º 7
0
 protected function __construct()
 {
     $this->config = Hayate_Config::load('session');
     $connName = $this->config->get('session.connection', 'default');
     $this->db = Hayate_Database::getInstance($connName);
     $this->crypto = null;
     // if we want the session encrypted, and the cookie does not
     // encrypt then we encrypt here otherwise we let the cookie class encrypt
     if ($this->config->get('session.encrypt', false)) {
         $this->crypto = Hayate_Crypto::getInstance();
     }
 }
Ejemplo n.º 8
0
 public static function getInstance($name = 'default')
 {
     if (isset(self::$db[$name])) {
         return self::$db[$name];
     }
     $config = Hayate_Config::getInstance()->get('database.' . $name, null);
     if (null === $config) {
         throw new Hayate_Database_Exception(sprintf(_('Database config "%s" not found.'), $name));
     }
     self::$db[$name] = new Hayate_Database_Pdo($config);
     return self::$db[$name];
 }
Ejemplo n.º 9
0
 protected function __construct()
 {
     $this->params = array('get' => array(), 'post' => array(), 'cookie' => array(), 'put' => array(), 'rawput' => array());
     if (Hayate_Config::getInstance()->get('xss_clean', true)) {
         foreach ($_GET as $key => $val) {
             $this->params['get'][$key] = htmlentities($val, ENT_QUOTES, 'utf-8');
         }
         foreach ($_POST as $key => $val) {
             $this->params['post'][$key] = htmlentities($val, ENT_QUOTES, 'utf-8');
         }
         foreach ($_COOKIE as $key => $val) {
             $this->params['cookie'][$key] = htmlentities($val, ENT_QUOTES, 'utf-8');
         }
     }
     if (Hayate_Request::getInstance()->isPut()) {
         $this->loadPut();
     }
 }
Ejemplo n.º 10
0
 protected static function write($type, $msg, $print_r)
 {
     if (!$print_r && !is_string($msg)) {
         return self::write($type, $msg, true);
     }
     $config = Hayate_Config::load();
     $error_level = $config->get('error_level', self::HAYATE_LOG_OFF);
     if ($type <= $error_level) {
         $logdir = $config->get('log_directory', APPPATH . 'logs/', true);
         if (is_dir($logdir) && is_writable($logdir)) {
             $filename = $logdir . 'log-' . date('d-m-Y') . '.log';
             $logfile = new SplFileObject($filename, 'a');
             self::header($type, $logfile);
             if (true === $print_r) {
                 $msg = print_r($msg, true);
             }
             $logfile->fwrite($msg);
             self::footer($logfile);
         }
     }
 }
Ejemplo n.º 11
0
 /**
  * @param string $field The field name
  * @param array $params Min and Max values respective at position 0 and 1 of the array
  */
 protected function size($field, array $params)
 {
     if (array_key_exists($field, $this)) {
         $charset = Hayate_Config::getInstance()->get('charset', 'UTF-8');
         $min = $params[0];
         $max = $params[1];
         return mb_strlen($this[$field], $charset) >= $min && mb_strlen($this[$field], $charset) <= $max;
     }
     return true;
 }
Ejemplo n.º 12
0
 protected static function factory()
 {
     $config = Hayate_Config::getInstance()->get('view', array('name' => 'native'));
     if (isset($config['name'])) {
         switch ($config['name']) {
             case 'smarty':
                 return Hayate_View_Smarty::getInstance();
             case 'native':
                 return Hayate_View_Native::getInstance();
             default:
                 throw new Hayate_View_Exception(_('Supported views are "smarty" and "native".'));
         }
     }
     throw new Hayate_View_Exception(_('View name is missing.'));
 }
Ejemplo n.º 13
0
 public static function modules()
 {
     $config = Hayate_Config::getInstance();
     $modules = $config->get('modules', array());
     $modules[] = $config->get('default_module', 'default');
     return $modules;
 }
Ejemplo n.º 14
0
 public function exceptionDispatch(Exception $ex)
 {
     try {
         if (Hayate_Event::run('hayate.exception', array($this, $ex))) {
             return;
         }
         // try to dispatch to the current module error.php controller
         $module = $this->module();
         $filepath = $this->modulesPath . $module . '/controllers/error.php';
         // if the error controller does not exists in the current module
         // look in the default module
         if (!is_file($filepath)) {
             $module = Hayate_Config::getInstance()->get('default_module', 'default');
             $filepath = $this->modulesPath . $module . '/controllers/error.php';
         }
         if (is_file($filepath)) {
             require_once $filepath;
             $classname = ucfirst($module) . '_ErrorController';
             $rfc = new ReflectionClass($classname);
             if ($rfc->isSubclassOf('Hayate_Controller') && $rfc->isInstantiable()) {
                 $controller = $rfc->newInstance();
                 $action = $rfc->hasMethod('index') ? $rfc->getMethod('index') : $rfc->getMethod('__call');
                 if ($action->isPublic()) {
                     $action->invokeArgs($controller, array($ex));
                 }
             }
         } else {
             $display_errors = Hayate_Config::getInstance()->get('display_errors', false);
             if ($display_errors && $this->errorReporter) {
                 Hayate_Event::remove('hayate.send_headers');
                 Hayate_Event::remove('hayate.render');
                 $this->errorReporter->setException($ex);
                 echo $this->errorReporter->report();
             }
         }
     } catch (Exception $ex) {
     }
 }
Ejemplo n.º 15
0
 /**
  * @param string $name The name of the cookie
  * @param mixed $default If $name is not set this value is returned
  * @param bool $xss_clean If boolean it will overwrite the configuration settings (prevent xss attacts)
  * @return mixed The value of the cookie
  */
 public function get($name, $default = null, $xss_clean = null)
 {
     if (!$this->exists($name)) {
         return $default;
     }
     $ans = $_COOKIE[$name];
     if ($this->encrypt) {
         $crypto = Hayate_Crypto::getInstance();
         $ans = $crypto->decrypt($ans);
     }
     $xss = Hayate_Config::getInstance()->get('xss_clean', false);
     if (is_bool($xss_clean)) {
         $xss = $xss_clean;
     }
     return $xss ? htmlentities($ans, ENT_QUOTES, 'utf-8') : $ans;
 }
Ejemplo n.º 16
0
 /**
  * @param string $name The name of the cookie
  * @param mixed $default If $name is not set this value is returned
  * @param bool $xss_clean If boolean it will overwrite the configuration settings (prevent xss attacts)
  * @return mixed The value of the cookie
  */
 public function get($name, $default = null, $xss_clean = null)
 {
     if (!array_key_exists($name, $_COOKIE)) {
         return $default;
     }
     $ans = $_COOKIE[$name];
     if ($this->encrypt) {
         $crypto = Hayate_Crypto::getInstance();
         $ans = $crypto->decrypt($ans);
     }
     $xss = Hayate_Config::getInstance()->get('xss_clean', false);
     if (is_bool($xss_clean)) {
         $xss = $xss_clean;
     }
     $ans = unserialize($ans);
     return is_string($ans) && $xss ? htmlspecialchars($ans, ENT_QUOTES, Hayate_Config::getInstance()->get('charset', 'UTF-8')) : $ans;
 }