Example #1
0
 /**
  * 获取一个指定类型的数据库实例
  *
  * @return Minifw\DB 数据库唯一的实例
  */
 public static function get($args = [])
 {
     $type = '';
     if (isset($args['type'])) {
         $type = strval($args['type']);
     }
     if ($type == '') {
         $type = Minifw\Config::get('main', 'db', '');
     }
     if ($type == '') {
         throw new Minifw\Exception("未指定数据库类型");
     }
     $class_name = __NAMESPACE__ . "\\DB\\" . $type;
     if (!class_exists($class_name)) {
         throw new Minifw\Exception("类型不存在");
     }
     return $class_name::get_instance($args);
 }
Example #2
0
 public function __construct($config = ['/config.php'])
 {
     if (!defined('WEB_ROOT')) {
         if (isset($_SERVER['DOCUMENT_ROOT']) && $_SERVER['DOCUMENT_ROOT'] != '') {
             define('WEB_ROOT', $_SERVER['DOCUMENT_ROOT']);
         } else {
             die('"WEB_ROOT" is not define.');
         }
     }
     Config::load_config($config);
     if (!defined('DEBUG')) {
         define('DEBUG', Minifw\Config::get('debug', 'debug', 0));
     }
     if (!defined('DBPREFIX')) {
         define('DBPREFIX', Minifw\Config::get('main', 'dbprefix', ''));
     }
     date_default_timezone_set(Minifw\Config::get('main', 'timezone', 'UTC'));
     //设置错误处理函数
     set_error_handler([__NAMESPACE__ . '\\Error', 'captureNormal']);
     //设置异常处理函数
     set_exception_handler([__NAMESPACE__ . '\\Error', 'captureException']);
     //设置停机处理函数
     register_shutdown_function([__NAMESPACE__ . '\\Error', 'captureShutdown']);
 }
Example #3
0
 /**
  * 保存通过表单提交的文件
  *
  * @param array $file 要上传的文件
  * @param string $group 文件分组
  * @param string $fsencoding 文件系统的编码,如不为空则会自动进行一些编码转换
  * @return string 保存的相对路径
  */
 public static function upload_file($file, $group, $fsencoding = '')
 {
     if (empty($file)) {
         return '';
     }
     if ($file['error'] != 0) {
         $error = '';
         switch ($file['error']) {
             case '1':
                 $error = '超过服务器允许的大小。';
                 break;
             case '2':
                 $error = '超过表单允许的大小。';
                 break;
             case '3':
                 $error = '文件只有部分被上传。';
                 break;
             case '4':
                 $error = '请选择文件。';
                 break;
             case '6':
                 $error = '找不到临时目录。';
                 break;
             case '7':
                 $error = '写文件到硬盘出错。';
                 break;
             case '8':
                 $error = '文件传输被扩展终止。';
                 break;
             case '999':
             default:
                 $error = '未知错误。';
         }
         return self::error($error);
     }
     $dirmap = Config::get('upload');
     if (!isset($dirmap[$group])) {
         return self::error('文件分组错误');
     }
     $allow = $dirmap[$group]['allow'];
     $base_dir = $dirmap[$group]['path'];
     $pinfo = pathinfo($file['name']);
     $ext = strtolower($pinfo['extension']);
     if (!in_array($ext, $allow)) {
         return self::error('不允许的文件类型');
     }
     $name = self::mkname(WEB_ROOT . $base_dir, '.' . $ext, $fsencoding);
     if ($name == '') {
         return self::error('同一时间上传的文件过多');
     }
     $dest = WEB_ROOT . $base_dir . '/' . $name;
     $dest = self::conv_to($dest, $fsencoding);
     self::mkdir(dirname($dest));
     if (move_uploaded_file($file['tmp_name'], $dest)) {
         return $base_dir . '/' . $name;
     } else {
         return self::error('文件移动出错');
     }
 }
Example #4
0
 /**
  * 私有构造函数
  */
 protected function __construct($args = [])
 {
     parent::__construct();
     $ini = Minifw\Config::get('sqlite');
     if (!empty($args)) {
         $ini['path'] = isset($args['path']) ? strval($args['path']) : $ini['path'];
     }
     if (empty($ini)) {
         throw new Minifw\Exception('数据库未配置');
     }
     $this->_sqlite = new \SQLite3(WEB_ROOT . $ini['path'], SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
 }
Example #5
0
 /**
  * 配置系统的主要参数
  */
 protected function _set_env()
 {
     ob_start();
     if (!defined('MAGIC_QUOTES_GPC')) {
         define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
     }
     $_GET = self::magic_gpc($_GET);
     $_POST = self::magic_gpc($_POST);
     $_COOKIE = self::magic_gpc($_COOKIE);
     header('Content-type:text/html;charset=' . Minifw\Config::get('main', 'encoding', 'utf-8'));
     $session_name = Minifw\Config::get('main', 'session', 'PHPSESSION');
     session_name($session_name);
     session_set_cookie_params(36000, '/', Minifw\Config::get('main', 'domain', ''));
     //处理Flash丢失cookie的问题
     $session_id = '';
     isset($_POST[$session_name]) && ($session_id = strval($_POST[$session_name]));
     if ($session_id == '') {
         isset($_GET[$session_name]) && ($session_id = strval($_GET[$session_name]));
     }
     if ($session_id != '') {
         session_id($session_id);
     }
     session_start();
 }
Example #6
0
 /**
  * 显示指定的页面模板
  *
  * @param string $tpl 页面模板
  * @param string $args 页面参数
  * @param string $theme 模板的主题
  */
 public static function display($tpl, $args, $theme = '', $die = true)
 {
     self::$render = true;
     $theme = $theme == '' ? Minifw\Config::get('main', 'theme') : $theme;
     $tpl_src = WEB_ROOT . self::$theme_path . '/' . $theme . '/page' . $tpl . '.html';
     $tpl_dest = WEB_ROOT . self::$compiled_path . '/' . $theme . '/page' . $tpl . '.php';
     try {
         if (self::_compile($tpl_src, $tpl_dest, $theme)) {
             extract(self::$_varis);
             include $tpl_dest;
         }
     } catch (\Exception $ex) {
         ob_end_clean();
         if (DEBUG) {
             throw $ex;
         }
         if ($die) {
             die;
         } else {
             return false;
         }
     }
     if (DEBUG && !empty(self::$_error)) {
         $content = ob_get_clean();
         echo '<pre>';
         print_r(self::$_error);
         echo '</pre>' . $content;
     } else {
         ob_end_flush();
     }
     if ($die) {
         die;
     } else {
         return true;
     }
 }
Example #7
0
 /**
  * 显示404页面
  */
 public static function show_404()
 {
     header("HTTP/1.1 404 Not Found");
     header("status: 404 not found");
     die(readfile(WEB_ROOT . Minifw\Config::get('main', 'err_404')));
 }
Example #8
0
    public static function show_304($mtime)
    {
        $expire = gmdate('D, d M Y H:i:s', time() + self::$cache_time) . ' GMT';
        header('Expires: ' . $expire);
        header('Pragma: cache');
        header('Cache-Control: max-age=' . self::$cache_time);
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $mtime) . ' GMT');
        header('Etag: ' . $mtime);
        if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && trim($_SERVER['HTTP_IF_NONE_MATCH']) == $mtime) {
            header('HTTP/1.1 304 Not Modified');
            die(0);
        }
    }
    /**
     * 生成到前一个页面的链接
     *
     * @param string $default 如果referer不存在则返回该值
     * @return string 生成的链接
     */
    public static function referer($default = '')
    {
        if (isset($_SERVER['HTTP_REFERER'])) {
            $url = $_SERVER['HTTP_REFERER'];
        } else {
            $url = $default;
        }
        return $url;
    }
}
Server::$cache_time = Config::get('main', 'cache', 3600);
Example #9
0
 /**
  * 私有构造函数
  */
 protected function __construct($args = [])
 {
     parent::__construct();
     $ini = Minifw\Config::get('mysql');
     if (!empty($args)) {
         $ini['host'] = isset($args['host']) ? strval($args['host']) : $ini['host'];
         $ini['username'] = isset($args['username']) ? strval($args['username']) : $ini['username'];
         $ini['password'] = isset($args['password']) ? strval($args['password']) : $ini['password'];
         $ini['dbname'] = isset($args['dbname']) ? strval($args['dbname']) : $ini['dbname'];
         $ini['encoding'] = isset($args['encoding']) ? strval($args['encoding']) : $ini['encoding'];
     }
     if (empty($ini)) {
         throw new Minifw\Exception('数据库未配置');
     }
     $this->_host = $ini['host'];
     $this->_username = $ini['username'];
     $this->_password = $ini['password'];
     $this->_dbname = $ini['dbname'];
     $this->_encoding = $ini['encoding'];
     $this->_mysqli = new \mysqli($this->_host, $this->_username, $this->_password, $this->_dbname);
     if ($this->_mysqli->connect_error) {
         throw new Minifw\Exception('数据库连接失败');
     }
     if (!$this->_mysqli->set_charset($this->_encoding)) {
         throw new Minifw\Exception('数据库查询失败');
     }
 }