Ejemplo n.º 1
0
 /**
  * 读取文件并输出到浏览器
  *
  * @param type $full 文件路径
  * @param type $fsencoding 文件系统的编码,如不为空则会自动进行一些编码转换
  */
 public static function readfile($full, $fsencoding = '')
 {
     $full = self::conv_to($full, $fsencoding);
     if (file_exists($full)) {
         $mtime = filemtime($full);
         Server::show_304($mtime);
         $fi = new \finfo(FILEINFO_MIME_TYPE);
         $mime_type = $fi->file($full);
         header('Content-Type: ' . $mime_type);
         readfile($full);
     } else {
         Server::show_404();
     }
 }
Ejemplo n.º 2
0
 /**
  * 分发请求到对应的回调函数
  *
  * @param string $path 请求的路径
  */
 public function dispatch($path)
 {
     foreach ($this->_calls as $v) {
         $matches = [];
         if (preg_match($v['reg'], $path, $matches) === 1) {
             array_shift($matches);
             call_user_func_array($v['callback'], $matches);
             return;
         }
     }
     Minifw\Server::show_404();
 }
Ejemplo n.º 3
0
 /**
  * 将一个路径路由到指定方法
  *
  * @param string $path 路径
  * @param string $prefix 指定方法所在类的名空间前缀
  * @param string $def_func 当路径中方法名为空时掉用的默认函数
  */
 public static function route($path, $prefix = '', $die = true)
 {
     list($classname, $funcname, $args, $nouse) = self::path_info($path);
     $classname = str_replace('/', '\\', $classname);
     $classname = $prefix . ucwords($classname, '\\');
     try {
         $class = new \ReflectionClass($classname);
         if ($funcname == '' && $class->hasConstant('DEF_FUNC')) {
             $funcname = $class->getConstant('DEF_FUNC');
         }
         $funcname = str_replace('.', '', $funcname);
         if ($funcname == '') {
             if ($die) {
                 Server::show_404();
             }
             return false;
         }
         $funcname = 'c_' . $funcname;
         $func = $class->getMethod($funcname);
         $doc = $func->getDocComment();
         $doc = str_replace(' ', '', $doc);
         if (!preg_match('/^\\*@route(\\(prev=(true|false)\\))?$/im', $doc, $matches)) {
             if ($die) {
                 Server::show_404();
             }
             return false;
         }
         $obj = $class->newInstance();
         if (!isset($matches[2]) || $matches[2] === 'true') {
             if ($class->hasMethod('prev')) {
                 $prev = $class->getMethod('prev');
                 $prev->setAccessible(true);
                 $prev->invoke($obj);
             }
         }
         $func->setAccessible(true);
         $func->invoke($obj, $args);
         if ($die) {
             die;
         }
         return true;
     } catch (Minifw\Exception $ex) {
         if (DEBUG === 1) {
             echo $ex->getMessage();
         }
         if ($die) {
             Server::show_404();
         }
         return false;
     } catch (\Exception $ex) {
         if ($die) {
             Server::show_404();
         }
         return false;
     }
 }
Ejemplo n.º 4
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);