/** * 构造函数 * @param $cachetype 缓存类型(可以传入,也可以不传) */ function __construct($cache_type = '', $cache_id = 'default') { if (!$cache_type) { $cache_type = App_Info::config('DEFAULT_CACHE_TYPE'); } $this->type = $cache_type; $this->cache_id = $cache_id; $this->_cache = $this->_get_cache_ins(); }
function __construct() { $config = App_Info::config('LOG_CONFIG'); $this->_config = $config; $this->_record = $config['log_record']; $this->_level = $config['log_level']; $this->_length = $config['log_length']; $this->_levels = App_Key::$LOG_LEVELS; }
/** * 初始化 * @throws App_Exception * @return class */ public static function init() { $f = App_Info::config('LIMIT_ENABLE'); if (!$f) { return; } $c = App_Info::config('LIMIT_CONFIG'); $ins = new Limit_Visit(); self::$over_limit = !$ins->visit($c['interval'], $c['count'], $c['key']); }
/** * 解析显示模板 * @param string $url_str controller/action * @param string $cache_id 缓存id * @param string $compile_id 编译id */ public function render($url_str = null, $cache_id = null, $compile_id = null) { $charset = $this->_header['charset'] ? $this->_header['charset'] : App_Info::config('APP_CHARSET'); $content_type = $this->_header['content_type']; if (!headers_sent()) { //如果之前没有输出 header('Content-Type:' . $content_type . '; charset=' . $charset); header('X-Powered-By:' . ESUN_PHP_NAME); } $temp = $this->template_view(); $temp->assign($this->_params); echo $temp->template_content($url_str, $cache_id, $compile_id); }
/** * * 初始化日志存储方式 * @param $log_type 日志存储方式 * @throws App_Exception * @return class */ private function _init($log_type = '') { if (empty($log_type)) { $config = App_Info::config('LOG_CONFIG'); $log_type = $config['log_type']; } $log_type = ucfirst(strtolower($log_type)); $classname = 'Log_' . $log_type; if (!class_exists($classname)) { throw new App_Exception('类' . $classname . '不存在!', App_Key::$ERROR_KEY); } $class = Common_Func::get_instance($classname); return $class; }
/** * 取得Memcache实例 * * @param string $cache_id 缓存配置id * @author huangls */ public static function get_instance($cache_id = 'default') { if (!isset(self::$_instance[$cache_id])) { $conf = App_Info::config('CACHE_MEM_CONFIG'); self::$_instance[$cache_id] = null; if (isset($conf[$cache_id])) { $conf = $conf[$cache_id]; $ins = new self($conf['host'], intval($conf['port'])); self::$_instance[$cache_id] = $ins; } else { throw new App_Exception('memcached key:"' . $cache_id . '" not config in inc/config.php!', App_Key::$ERROR_KEY); } } return self::$_instance[$cache_id]; }
/** * header方法 * @param string $content * @param string $replace * @param string $http_response_code */ protected function header($content, $replace = true, $http_response_code = false) { $conx = explode(':', $content, 2); if (strtoupper($conx[0]) == 'LOCATION' && isset($conx[1])) { $f = Common_Func::url_in_array_host(trim($conx[1]), App_Info::config('HOST_LOCATION_ALLOW')); if (!is_null($f) && !$f) { $this->error('url跳转非法,可在URL_LOCATION_ALLOW配置'); } } $header_func = function_exists('rename_function') ? Core_Utils::random_name('header') : 'header'; if (is_int($http_response_code)) { $header_func($content, $replace, $http_response_code); } else { $header_func($content, $replace); } }
/** * 构造函数 */ public function __construct($id = 'default') { if (!function_exists('mysql_connect')) { $this->error_msg('not function:mysql_connect!'); } $conf = App_Info::config('DB_CONFIG'); $id = 'mysql_' . $id; if (!isset($conf[$id])) { $this->error_msg('not mysql config:' . $id); } $conf = $conf[$id]; if (!($this->conn = mysql_connect($conf['host'] . ':' . $conf['port'], $conf['user'], $conf['pswd']))) { $this->error_msg("db connect error:" . mysql_error()); } if ($this->get_mysql_version() > '4.1') { mysql_query("SET NAMES '" . $conf['charset'] . "'"); } @mysql_select_db($conf['db'], $this->conn) || $this->error_msg("not found database:" . $conf['db']); }
public function __construct() { $config = App_Info::config('TEMPLATE_CONFIG'); if (isset($config['source_class'])) { foreach ($config['source_class'] as $path) { include $path; } } if (!isset($config['smarty_version']) || $config['smarty_version'] > 2) { $this->tpl = new Smarty(); } else { $this->tpl = new SmartyBC(); } if (isset($config['config'])) { foreach ($config['config'] as $k => $v) { $this->tpl->{$k} = $v; } } $this->_load_sys_func(); }
/** * 加载控制器 * @param string $con_name */ public static function import_controller($con_name, $con_ext) { $con_path = App_Info::config('CONTROLLER_PATH'); $con_ins = null; if (is_array($con_path)) { $con_conf = self::get_values_array($con_path); if (!isset($con_conf[$con_name])) { return $con_ins; } $con_path = $con_conf[$con_name]; } $con_name = ucwords($con_name) . $con_ext; $con_file = $con_path . $con_name . '.php'; if (is_file($con_file)) { require $con_file; if (class_exists($con_name)) { $con_ins = new ReflectionClass($con_name); } } return $con_ins; }
/** * 异常格式化脚本 * @param string|array $ex */ public static function exception_handler($ex) { if (is_string($ex) || is_object($ex) && is_string($ex->__toString())) { $trace = debug_backtrace(); $e['message'] = $ex; $e['file'] = $trace[0]['file']; $e['class'] = isset($trace[0]['class']) ? $trace[0]['class'] : ''; $e['function'] = isset($trace[0]['function']) ? $trace[0]['function'] : ''; $e['line'] = $trace[0]['line']; $trace_info = ''; $time = date('y-m-d H:i:m'); foreach ($trace as $t) { $trace_info .= '[' . $time . '] ' . $t['file'] . ' (' . $t['line'] . ') '; $trace_info .= $t['class'] . $t['type'] . $t['function'] . '('; $trace_info .= implode(', ', $t['args']); $trace_info .= ')<br/>'; } $e['trace'] = $trace_info; } else { $e = $ex->__toString(); } include App_Info::config('TEMPLATE_EXCEPTION'); }
/** * 获取模版文件 * @param string $url_str controller/action */ public function template_file($url_str = '') { if (isset($this->_template_file[$url_str])) { return $this->_template_file[$url_str]; } $temp_ext = App_Info::config('TEMPLATE_EXT'); if ($url_str) { $temp_file = str_replace('.', '/', $url_str); $temp_file = explode('/', $temp_file); if (trim($temp_file[0]) == '') { $temp_file[0] = App_Info::$CONTROLLER_NAME; } if (trim($temp_file[1]) == '') { $temp_file[1] = App_Info::$ACTION_NAME; } $temp_file = implode($temp_file, '/'); } else { $temp_file = App_Info::$CONTROLLER_NAME . '/' . App_Info::$ACTION_NAME; } $temp_file = App_Info::config('TEMPLATE_PATH') . $temp_file . '.' . $temp_ext; $this->_template_file[$url_str] = $temp_file; return $temp_file; }
} include implode('/', explode('_', $class_name)) . '.php'; } spl_autoload_register('esun_autoload'); //框架名称 defined('ESUN_PHP_NAME') or define('ESUN_PHP_NAME', 'EsunPHP'); //框架版本 define('ESUN_PHP_VERSION', '1.0'); //应用路径 defined('ROOT_PATH') or define('ROOT_PATH', dirname(dirname($_SERVER['SCRIPT_FILENAME'])) . '/'); defined('APP_PATH') or define('APP_PATH', '../'); //框架路径 defined('ESUN_PHP_PATH') or define('ESUN_PHP_PATH', dirname(__FILE__) . '/'); set_include_path(get_include_path() . PATH_SEPARATOR . ESUN_PHP_PATH . 'lib/' . PATH_SEPARATOR . ROOT_PATH . 'lib/'); //加载一些框架一定会用到的类库 include ESUN_PHP_PATH . 'lib/App/Key.php'; include ESUN_PHP_PATH . 'clib/Core/Exception.php'; include ESUN_PHP_PATH . 'clib/Common/Func.php'; include ESUN_PHP_PATH . 'clib/Common/Utils.php'; include ESUN_PHP_PATH . 'clib/Core/Utils.php'; include ESUN_PHP_PATH . 'clib/Core/Dispatcher.php'; include ESUN_PHP_PATH . 'clib/Core/Action.php'; include ESUN_PHP_PATH . 'clib/Core/Template.php'; include ESUN_PHP_PATH . 'lib/App/Info.php'; include ESUN_PHP_PATH . 'lib/App/Url.php'; include ESUN_PHP_PATH . 'lib/App/Controller.php'; include ESUN_PHP_PATH . 'lib/App/Exception.php'; include ESUN_PHP_PATH . 'lib/App/Service.php'; //加载配置文件 App_Info::config(include ESUN_PHP_PATH . 'inc/config.php');
/** * 获取带hashkey的url * @param string $url controller/action[?k1=v1] * @param string $vars k2=v2&k3=v3 * @param string $url_suffix .html */ public static function hash_url($url = '', $vars = '', $url_suffix = false, $hash_key = '__hash') { $hstr = $hash_key . '=' . self::get_hash_value(); $vars = $vars ? $vars . '&' . $hstr : $hstr; return self::_get($url, $vars, $url_suffix, App_Info::config('URL_TYPE')); }
/** * 执行APP Service调用 */ private static function _exec($get_args, $post_args) { $c = App_Info::$CONTROLLER_NAME; $a = App_Info::$ACTION_NAME; $con_ins = false; $con_ext = App_Info::config('CONTROLLER_NAME_EXT'); if (preg_match('/^[A-Za-z](\\w)*$/D', $c)) { //检测控制器名安全 $con_ins = Core_Action::import_controller($c, $con_ext); } if (!$con_ins) { //如果控制器不存在,试图访问空控制器 $con_ins = Core_Action::import_controller('empty', $con_ext); } $method_exist = false; if ($con_ins) { if (!$con_ins->isSubclassOf('App_Controller')) { throw new Core_Exception('Controller[' . $c . '] Don\'t extends App_Controller', App_Key::$CONTROLLER_NOT_EXTENTD); } if (!preg_match('/^[A-Za-z](\\w)*$/D', $a)) { //检测操作名安全 throw new Core_Exception('Action Name[' . htmlspecialchars($a) . '] Error, Controller:' . $c, App_Key::$ACTION_NAME_ERROR); } $act_ext = App_Info::config('ACTION_NAME_EXT'); $method_name = $a . $act_ext; if ($con_ins->hasMethod($method_name)) { //操作存在,执行 $method_exist = true; Core_Action::exec_action($con_ins, $method_name, $get_args, $post_args); } else { $method_name = 'undefined' . $act_ext; if ($con_ins->hasMethod($method_name)) { //empty操作存在,执行 $method_exist = true; Core_Action::exec_action($con_ins, $method_name, $get_args, $post_args); } } } if (!$con_ins || !$method_exist) { $f = self::_load_default_template(); $g = $c === App_Info::config('DEFAULT_CONTROLLER_NAME') && $a === App_Info::config('DEFAULT_ACTION_NAME'); if (!$f) { if ($g) { //新系统 include ESUN_PHP_PATH . 'view/index/main.php'; } else { if ($con_ins) { //控制器存在 throw new Core_Exception('Action Name[' . htmlspecialchars($a) . '] Error, Controller:' . $c, App_Key::$ACTION_NOT_EXIST); } else { throw new Core_Exception('Controller Name[' . htmlspecialchars($c) . '] Error', App_Key::$CONTROLLER_NOT_EXIST); //Common_Func::send_http_status(404); } } } } }
private static function decode_url_str($paths) { $paths = explode(App_Info::config('URL_PATHINFO_SEPARATOR'), $paths); App_Info::$CONTROLLER_NAME = isset($paths[0]) && $paths[0] ? strip_tags(array_shift($paths)) : App_Info::config('DEFAULT_CONTROLLER_NAME'); App_Info::$ACTION_NAME = isset($paths[0]) && $paths[0] ? strip_tags(array_shift($paths)) : App_Info::config('DEFAULT_ACTION_NAME'); $var = array(); // 解析剩余的URL参数 preg_replace('@(\\w+)\\/([^\\/]+)@e', '$var[\'\\1\']=strip_tags(\'\\2\');', implode('/', $paths)); $_GET = array_merge($var, $_GET); $_REQUEST = array_merge($var, $_REQUEST); }
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title><?php echo App_Info::config('APP_TITLE'); ?> </title> <meta name="keywords" content="<?php echo App_Info::config('APP_KEYWORDS'); ?> "> <meta name="description" content="<?php echo App_Info::config('APP_DESCRIPTION'); ?> ">