/** * Class constructor * * Setup Memcache(d) * * @return void */ public function __construct() { // Try to load memcached server info from the config file. $defaults = $this->_memcacheConf['default']; $config = Config::getSoul()->CACHE; $memcacheConf = isset($config['MEMCACHED']) ? $config['MEMCACHED'] : null; if (is_array($memcacheConf)) { $this->_memcacheConf = array(); foreach ($memcacheConf as $name => $conf) { $this->_memcacheConf[$name] = $conf; } } if (class_exists('Memcached', false)) { $this->_memcached = new \Memcached(); } elseif (class_exists('Memcache', false)) { $this->_memcached = new \Memcache(); } else { Log::normal('[Error] Failed to create Memcache(d) object; extension not loaded?'); } foreach ($this->_memcacheConf as $cacheServer) { isset($cacheServer['HOST']) or $cacheServer['HOST'] = $defaults['HOST']; isset($cacheServer['PORT']) or $cacheServer['PORT'] = $defaults['PORT']; isset($cacheServer['WEIGHT']) or $cacheServer['WEIGHT'] = $defaults['WEIGHT']; if (get_class($this->_memcached) === 'Memcache') { // Third parameter is persistance and defaults to TRUE. $this->_memcached->addServer($cacheServer['HOST'], $cacheServer['PORT'], true, $cacheServer['WEIGHT']); } else { $this->_memcached->addServer($cacheServer['HOST'], $cacheServer['PORT'], $cacheServer['WEIGHT']); } } Hook::listen(__CLASS__); }
/** * Start the App. * * @return void */ public function run() { //Define a custom error handler so we can log PHP errors set_error_handler(array('\\Kotori\\Core\\Handle', 'error')); set_exception_handler(array('\\Kotori\\Core\\Handle', 'exception')); register_shutdown_function(array('\\Kotori\\Core\\Handle', 'end')); Config::getSoul()->initialize($this->_config); ini_set('date.timezone', Config::getSoul()->TIME_ZONE); if (Config::getSoul()->USE_SESSION) { !session_id() && session_start(); } //Build new Build(Config::getSoul()->APP_FULL_PATH); //Load application's common functions Helper::import(Config::getSoul()->APP_FULL_PATH . '/common.php'); if (function_exists('spl_autoload_register')) { spl_autoload_register(array('\\Kotori\\Core\\Helper', 'autoload')); } else { function __autoload($className) { Helper::autoload($className); } } //Load route class Route::getSoul()->dispatch(); //Global security filter array_walk_recursive($_GET, array('\\Kotori\\Http\\Request', 'filter')); array_walk_recursive($_POST, array('\\Kotori\\Http\\Request', 'filter')); array_walk_recursive($_REQUEST, array('\\Kotori\\Http\\Request', 'filter')); }
/** * Class constructor * * Initialize view and database classes. * * @return void */ public function __construct() { $this->view = new View(); $this->response = Response::getSoul(); $this->request = Request::getSoul(); $this->route = Route::getSoul(); $this->db = $this->db(); $this->model = ModelProvider::getSoul(); $this->config = Config::getSoul(); $this->cache = Cache::getSoul(); Hook::listen(__CLASS__); }
/** * Write Log File * * Support Sina App Engine * * @param string $msg Message * @param string $level Log level * @return void */ protected static function write($msg, $level = '') { if (Config::getSoul()->APP_DEBUG == false) { return; } if (function_exists('saeAutoLoader')) { $msg = "[{$level}]" . $msg; sae_set_display_errors(false); sae_debug(trim($msg)); sae_set_display_errors(true); } else { $msg = date('[ Y-m-d H:i:s ]') . "[{$level}]" . $msg . "\r\n"; $logPath = Config::getSoul()->APP_FULL_PATH . '/logs'; if (!file_exists($logPath)) { Helper::mkdirs($logPath); } file_put_contents($logPath . '/' . date('Ymd') . '.log', $msg, FILE_APPEND); } }
/** * get singleton * * @return object */ public static function getSoul($key = null) { if (count(Config::getSoul()->DB) == 0) { return null; } elseif ($key == null) { $dbKeys = array_keys(Config::getSoul()->DB); if (isset($dbKeys[0])) { $key = $dbKeys[0]; } else { return null; } } Config::getSoul()->SELECTED_DB_KEY = $key; $config = array('database_type' => Config::getSoul()->DB[$key]['TYPE'], 'database_name' => Config::getSoul()->DB[$key]['NAME'], 'server' => Config::getSoul()->DB[$key]['HOST'], 'username' => Config::getSoul()->DB[$key]['USER'], 'password' => Config::getSoul()->DB[$key]['PWD'], 'charset' => Config::getSoul()->DB[$key]['CHARSET'], 'port' => Config::getSoul()->DB[$key]['PORT']); if (!isset(self::$_soul[$key])) { self::$_soul[$key] = new self($config); } return self::$_soul[$key]; }
/** * General Error Page * * Takes an error message as input * and displays it using the specified template. * * @param string $message Error Message * @param int $code HTTP Header code * * @return void */ public static function halt($message, $code = 404) { Response::getSoul()->setStatus($code); if (Config::getSoul()->APP_DEBUG == false) { $message = '404 Not Found.'; } $tplPath = Config::getSoul()->ERROR_TPL; if ($tplPath == null || !Helper::isFile(Config::getSoul()->APP_FULL_PATH . '/views/' . $tplPath . '.html')) { $tpl = '<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>Kotori.php 500 Internal Error</title> <meta name="robots" content="NONE,NOARCHIVE"> <style type="text/css"> html * { padding:0; margin:0; } body * { padding:10px 20px; } body * * { padding:0; } body { font:small sans-serif; background:#eee; } body>div { border-bottom:1px solid #ddd; } h1 { font-weight:normal; margin-bottom:.4em; } h1 span { font-size:60%; color:#666; font-weight:normal; } table { border:none; border-collapse: collapse; width:100%; } td, th { vertical-align:top; padding:2px 3px; } th { width:12em; text-align:right; color:#666; padding-right:.5em; } #info { background:#f6f6f6; } #info p {font-size: 16px} #summary { background: #ffc; } #explanation { background:#eee; border-bottom: 0px none; } </style> </head> <body> <div id="summary"> <h1>Kotori.php Internal Error <span>(500)</span></h1> <table class="meta"> <tr> <th>Request Method:</th> <td>' . strtoupper($_SERVER['REQUEST_METHOD']) . '</td> </tr> <tr> <th>Request URL:</th> <td>' . Request::getSoul()->getBaseUrl() . ltrim($_SERVER['REQUEST_URI'], '/') . '</td> </tr> </table> </div> <div id="info"> ' . $message . ' </div> <div id="explanation"> <p> You\'re seeing this error because you have <code>APP_DEBUG = True</code> in your index.php file. Change that to <code>False</code>, and Kotori.php will display a standard 404 page. </p> </div> </body> </html>'; } else { $tpl = file_get_contents(Config::getSoul()->APP_FULL_PATH . '/views/' . $tplPath . '.html'); } $tpl = str_replace('{$message}', $message, $tpl); $htmlParser = htmlParserFactory::construct(); $tpl = $htmlParser->compress($tpl); exit($tpl); }
/** * Show Page Trace in Output * * @return string */ public function showTrace() { if (Config::getSoul()->APP_DEBUG == false) { return; } $trace = $this->getTrace(); $tpl = ' <!-- Kotori Page Trace (If you want to hide this feature, please set APP_DEBUG to false.)--> <div id="page_trace" style="position:fixed;bottom:0;right:0;font-size:14px;width:100%;z-index: 999999;color: #000;text-align:left;font-family:\'Hiragino Sans GB\',\'Microsoft YaHei\',\'WenQuanYi Micro Hei\';"> <div id="page_trace_tab" style="display:none;background:white;margin:0;height:250px;"> <div id="page_trace_tab_tit" style="height:30px;padding: 6px 12px 0;border-bottom:1px solid #ececec;border-top:1px solid #ececec;font-size:16px">'; foreach ($trace as $key => $value) { $tpl .= '<span id="page_trace_tab_tit_' . strtolower($key) . '" style="color:#000;padding-right:12px;height:30px;line-height: 30px;display:inline-block;margin-right:3px;cursor: pointer;font-weight:700">' . $key . '</span>'; } $tpl .= '</div> <div id="page_trace_tab_cont" style="overflow:auto;height:212px;padding:0;line-height:24px">'; foreach ($trace as $key => $info) { $tpl .= '<div id="page_trace_tab_cont_' . strtolower($key) . '" style="display:none;"> <ol style="padding: 0; margin:0">'; if (is_array($info)) { foreach ($info as $k => $val) { $val = is_array($val) ? print_r($val, true) : (is_bool($val) ? json_encode($val) : $val); $val = in_array($key, array('Support')) ? $val : htmlentities($val, ENT_COMPAT, 'utf-8'); $tpl .= '<li style="border-bottom:1px solid #EEE;font-size:14px;padding:0 12px">' . (is_numeric($k) ? '' : $k . ' : ') . $val . '</li>'; } } $tpl .= '</ol> </div>'; } $tpl .= '</div> </div> <div id="page_trace_close" style="display:none;text-align:right;height:15px;position:absolute;top:10px;right:12px;cursor: pointer;"><img style="vertical-align:top;" src="data:image/gif;base64,R0lGODlhDwAPAJEAAAAAAAMDA////wAAACH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjAgNjEuMTM0Nzc3LCAyMDEwLzAyLzEyLTE3OjMyOjAwICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9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2NTQzMjEwLy4tLCsqKSgnJiUkIyIhIB8eHRwbGhkYFxYVFBMSERAPDg0MCwoJCAcGBQQDAgEAACH5BAAAAAAALAAAAAAPAA8AAAIdjI6JZqotoJPR1fnsgRR3C2jZl3Ai9aWZZooV+RQAOw==" /></div> </div> <div id="page_trace_open" style="height:30px;float:right;text-align: right;overflow:hidden;position:fixed;bottom:0;right:0;color:#000;line-height:30px;cursor:pointer;"><div style="background:#232323;color:#FFF;padding:0 6px;float:right;line-height:30px;font-size:14px">'; $errorCount = count(Handle::$errors); if ($errorCount == 0) { $tpl .= Hook::listen('\\Kotori\\App') . 'μs'; } else { $tpl .= $errorCount . ' errors'; } $tpl .= '</div><img width="30" style="border-left:2px solid black;border-top:2px solid black;border-bottom:2px solid black;" title="ShowPageTrace" src="' . Helper::logo() . '"></div> <script type="text/javascript"> (function() { \'use strict\'; var tab_tit = document.getElementById(\'page_trace_tab_tit\').getElementsByTagName(\'span\'), tab_cont = document.getElementById(\'page_trace_tab_cont\').getElementsByTagName(\'div\'), open = document.getElementById(\'page_trace_open\'), close = document.getElementById(\'page_trace_close\').children[0], trace = document.getElementById(\'page_trace_tab\'), storage = localStorage.getItem(\'kotori_show_page_trace\'), history = (storage !== null && storage.split(\'|\')) || [0,0], bindClick = function(dom, listener) { if (dom.addEventListener) { dom.addEventListener(\'click\', listener, false); } else { dom.attachEvent(\'onclick\', listener); } }; bindClick(open, function() { trace.style.display = \'block\'; this.style.display = \'none\'; close.parentNode.style.display = \'block\'; history[0] = 1; localStorage.setItem(\'kotori_show_page_trace\', history.join(\'|\')); }); bindClick(close, function() { trace.style.display = \'none\'; this.parentNode.style.display = \'none\'; open.style.display = \'block\'; history[0] = 0; localStorage.setItem(\'kotori_show_page_trace\', history.join(\'|\')); }); for (var i = 0; i < tab_tit.length; i++) { bindClick(tab_tit[i], (function(i) { return function() { for (var j = 0; j < tab_cont.length; j++) { tab_cont[j].style.display = \'none\'; tab_tit[j].style.color = \'#999\'; } tab_cont[i].style.display = \'block\'; tab_tit[i].style.color = \'#000\'; history[1] = i; localStorage.setItem(\'kotori_show_page_trace\', history.join(\'|\')); }; })(i)); } parseInt(history[0]) && open.click(); tab_tit[history[1]].click(); })(); </script>'; return $tpl; }
/** * Global autoload function * * @param string $class Class name * @return void */ public static function autoload($class) { $baseRoot = Config::getSoul()->APP_FULL_PATH; // project-specific namespace prefix $prefix = Config::getSoul()->NAMESPACE_PREFIX; // does the class use the namespace prefix? $len = strlen($prefix); if (strncmp($prefix, $class, $len) !== 0) { // no, move to the next registered autoloader return; } // get the relative class name $relativeClass = substr($class, $len); // replace the namespace prefix with the base directory, replace namespace // separators with directory separators in the relative class name, append // with .php $file = $baseRoot . '/' . str_replace('\\', '/', $relativeClass) . '.php'; self::import($file); }
/** * __get magic * * Allows controllers to access model * * @param string $key */ public function __get($key) { if (isset($this->_models[$key])) { return $this->_models[$key]; } $modelClassName = Config::getSoul()->NAMESPACE_PREFIX . 'models\\' . $key; if (!class_exists($modelClassName)) { throw new \Exception('Request Model ' . $key . ' is not Found'); } else { $model = new $modelClassName(); $this->_models[$key] = $model; return $model; } }
/** * Build Full URL * * @param string $uri URI * @param string $module module name * @return string */ public function url($uri = '', $module = null) { if ($module != null) { $appPaths = Config::getSoul()->APP_PATH; if (is_array($appPaths)) { foreach ($appPaths as &$appPath) { $appPath = str_replace('./', '', $appPath); } $appPaths = array_flip($appPaths); $baseUrl = $appPaths[$module]; $baseUrl = '//' . $baseUrl . '/'; } } else { $baseUrl = Request::getSoul()->getBaseUrl(); } $uri = is_array($uri) ? implode('/', $uri) : trim($uri, '/'); $prefix = $baseUrl . 'index.php?_i='; switch (Config::getSoul()->URL_MODE) { case 'PATH_INFO': return $uri == '' ? rtrim($baseUrl, '/') : $baseUrl . $uri; break; case 'QUERY_STRING': return $uri == '' ? rtrim($baseUrl, '/') : $prefix . $uri; break; default: throw new \Exception('URL_MODE Config ERROR'); } }
/** * Include Template * * @param string $path Template Path * @param array $data Data Array * @return void */ public function need($path, $data = array()) { $this->_needData = array('path' => Config::getSoul()->APP_FULL_PATH . '/views/' . $path . '.html', 'data' => $data); unset($path); unset($data); extract($this->_needData['data']); include $this->_needData['path']; }
/** * Constructor * * Initialize class properties based on the configuration array. * * @return void */ public function __construct() { $config = Config::getSoul()->CACHE; isset($config['ADAPTER']) && ($this->_adapter = $config['ADAPTER']); isset($config['PREFIX']) && ($this->keyPrefix = $config['PREFIX']); $className = '\\Kotori\\Core\\Cache\\' . ucfirst($this->_adapter); $this->{$this->_adapter} = new $className(); if (!$this->isSupported($this->_adapter)) { Log::normal('[Error] Cache adapter "' . $this->_adapter . '" is unavailable. Cache is now using "Dummy" adapter.'); $this->_adapter = 'dummy'; } Hook::listen(__CLASS__); }