/** * start to build folders * * @return void */ protected function startBuild() { if (is_dir($this->_appPath)) { return; } foreach ($this->_folders as $folder) { Helper::mkdirs($this->_appPath . '/' . $folder); } if (!is_file($this->_appPath . '/common.php')) { file_put_contents($this->_appPath . '/common.php', '<?php // common functions'); } }
/** * 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); } }
/** * 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); }
function __autoload($className) { Helper::autoload($className); }
/** * 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/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjAgNjEuMTM0Nzc3LCAyMDEwLzAyLzEyLTE3OjMyOjAwICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M1IFdpbmRvd3MiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MUQxMjc1MUJCQUJDMTFFMTk0OUVGRjc3QzU4RURFNkEiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6MUQxMjc1MUNCQUJDMTFFMTk0OUVGRjc3QzU4RURFNkEiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDoxRDEyNzUxOUJBQkMxMUUxOTQ5RUZGNzdDNThFREU2QSIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDoxRDEyNzUxQUJBQkMxMUUxOTQ5RUZGNzdDNThFREU2QSIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PgH//v38+/r5+Pf29fTz8vHw7+7t7Ovq6ejn5uXk4+Lh4N/e3dzb2tnY19bV1NPS0dDPzs3My8rJyMfGxcTDwsHAv769vLu6ubi3trW0s7KxsK+urayrqqmop6alpKOioaCfnp2cm5qZmJeWlZSTkpGQj46NjIuKiYiHhoWEg4KBgH9+fXx7enl4d3Z1dHNycXBvbm1sa2ppaGdmZWRjYmFgX15dXFtaWVhXVlVUU1JRUE9OTUxLSklIR0ZFRENCQUA/Pj08Ozo5ODc2NTQzMjEwLy4tLCsqKSgnJiUkIyIhIB8eHRwbGhkYFxYVFBMSERAPDg0MCwoJCAcGBQQDAgEAACH5BAAAAAAALAAAAAAPAA8AAAIdjI6JZqotoJPR1fnsgRR3C2jZl3Ai9aWZZooV+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; }
/** * Map URL to controller and action * * @return void */ public function dispatch() { if (Config::getSoul()->URL_MODE == 'QUERY_STRING') { $this->_uri = explode('?', $this->_uri, 2); $_SERVER['QUERY_STRING'] = isset($this->_uri[1]) ? $this->_uri[1] : ''; $this->_uri = $this->_uri[0]; parse_str($_SERVER['QUERY_STRING'], $_GET); } define('URI', $this->_uri); switch ($this->_uri) { case 'favicon.ico': Response::getSoul()->setHeader('Content-Type', 'image/x-icon'); Response::getSoul()->setCacheHeader(); echo base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', Helper::logo())); exit; case 'kotori-php-system-route/highlight-github.css': Response::getSoul()->setHeader('Content-Type', 'text/css; charset=utf-8'); Response::getSoul()->setCacheHeader(); echo file_get_contents(Helper::getComposerVendorPath() . '/components/highlightjs/styles/github.css'); exit; case 'kotori-php-system-route/highlight.js': Response::getSoul()->setHeader('Content-Type', 'text/javascript; charset=utf-8'); Response::getSoul()->setCacheHeader(); echo file_get_contents(Helper::getComposerVendorPath() . '/components/highlightjs/highlight.pack.min.js'); exit; } $parsedRoute = $this->parseRoutes($this->_uri); if ($parsedRoute) { $this->_uri = $parsedRoute; } else { throw new \Exception('Request URI ' . $this->_uri . ' is not Matched by any route.'); } $this->_uris = $this->_uri != '' ? explode('/', trim($this->_uri, '/')) : array(); //Clean uris foreach ($this->_uris as $key => $value) { if ($value == '') { unset($this->_uris[$key]); } } $this->_uris = array_merge($this->_uris); $this->_controller = $this->getController(); $this->_action = $this->getAction(); //Define some variables define('CONTROLLER_NAME', $this->_controller); define('ACTION_NAME', $this->_action); define('PUBLIC_DIR', Request::getSoul()->getBaseUrl() . 'public'); //If is already initialized $prefix = Config::getSoul()->NAMESPACE_PREFIX; $controllerClassName = $prefix . 'controllers\\' . $this->_controller; if (isset($this->_controllers[$this->_controller])) { $class = $this->_controllers[$this->_controller]; } else { $class = new $controllerClassName(); $this->_controllers[$this->_controller] = $class; } if (!class_exists($controllerClassName)) { throw new \Exception('Request Controller ' . $this->_controller . ' is not Found.'); } if (!method_exists($class, $this->_action)) { throw new \Exception('Request Action ' . $this->_action . ' is not Found.'); } //Parse params from uri $this->_params = $this->getParams(); //Do some final cleaning of the params $_GET = array_merge($this->_params, $_GET); $_REQUEST = array_merge($_POST, $_GET, $_COOKIE); Response::getSoul()->setHeader('X-Powered-By', 'Kotori'); Response::getSoul()->setHeader('Cache-control', 'private'); //Call the requested method call_user_func_array(array($class, $this->_action), $this->_params); }
/** * Display Output * * Processes and sends finalized output data to the browser along * * @param string $tpl Template Path * @return void */ public function display($tpl = '') { if ('' === $tpl) { $tpl = CONTROLLER_NAME . '/' . ACTION_NAME; } $this->_viewPath = $this->_tplDir . $tpl . '.html'; if (!Helper::isFile($this->_viewPath)) { throw new \Exception('Template is not existed.'); } unset($tpl); ob_start(); extract($this->_data, EXTR_OVERWRITE); include $this->_viewPath; $buffer = ob_get_contents(); ob_get_clean(); $output = Helper::comment() . preg_replace('|</body>.*?</html>|is', '', $buffer, -1, $count) . Trace::getSoul()->showTrace(); if ($count > 0) { $output .= '</body></html>'; } echo $output; }