GA»Æ½ð¼×ADLab£ºThinkPHP5Ô¶³Ì´úÂëÖ´Ðзì϶·ÖÎö

°ä²¼¹¦·ò 2018-12-11
·ì϶½éÉÜ

2018Äê12ÔÂ9ÈÕ£¬ThinkPHPÍŶӰ䲼ÁËÒ»¸ö²¹¶¡¸üУ¬½¨¸´ÁËÒ»´¦ÓÉÓÚ·ÓɽâÎöȱµãµ¼ÖµĴúÂëÖ´Ðзì϶¡£¸Ã·ì϶·çÏÕˮƽ¼«¶È¸ß£¬Ä¬ÈÏ»·¾³ÅäÖü´¿Éµ¼ÖÂÔ¶³Ì´úÂëÖ´ÐС£¾­¹ýGA»Æ½ð¼×ADLab°²È«×êÑÐÔ±¶ÔThinkPHPµÄ56¸öÓ×°æ±¾µÄÔ´Âë·ÖÎöºÍÑéÖ¤£¬È·¶¨¾ßÌåÊÜÓ°ÏìµÄ°æ±¾Îª:


ThinkPHP 5.0.5-5.0.22

ThinkPHP 5.1.0-5.1.30


·ì϶¸´ÏÖ


±¾µØ»·¾³Ñ¡È¡ThinkPHP 5.1.29+PHP7+Apache½øÐи´ÏÖ¡£×°Öû·¾³ºóÖ±½Ó½Ó¼ûPOC¸ø¶¨µÄURL¼´¿ÉÖ´ÐÐphpinfo()£¬ÈçͼËùʾ¡£

GA»Æ½ð¼×¡¤(ÖйúÇø)¹Ù·½ÍøÕ¾


·ì϶·ÖÎö


ÒÔ5.1.29°æ±¾½øÐзÖÎö£¬Ê×ÏÈ¿´È¡Â·Óɵĺ¯Êýpathinfo£º

library/think/Request.php:678

public function pathinfo()
    {
        if (is_null($this->pathinfo)) {
            if (isset($_GET[$this->config['var_pathinfo']])) {
                // ÅжÏURLÀïÃæÊÇ·ñÓмæÈÝģʽ²ÎÊý
                $pathinfo = $_GET[$this->config['var_pathinfo']];
                unset($_GET[$this->config['var_pathinfo']]);
            } elseif ($this->isCli()) {
                // CLIģʽÏ index.php module/controller/action/params/...
                $pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
            } elseif ('cli-server' == PHP_SAPI) {
                $pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI');
            } elseif ($this->server('PATH_INFO')) {
                $pathinfo = $this->server('PATH_INFO');
            }
 
            // ·ÖÎöPATHINFOÐÅÏ¢
            if (!isset($pathinfo)) {
                foreach ($this->config['pathinfo_fetch'] as $type) {
                    if ($this->server($type)) {
                        $pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ?
                        substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type);
                        break;
                    }
                }
            }
 
            $this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/');
        }
 
        return $this->pathinfo;
    }


¸Ã·Óɺ¯ÊýÖÐ$this->config['var_pathinfo']ÊÇÅäÖÃÎļþµÄĬÈÏÖµ£¬Æä³õʼ»¯´úÂëÈçÏ£¬ÖµÎª¡¯s¡¯:


GA»Æ½ð¼×¡¤(ÖйúÇø)¹Ù·½ÍøÕ¾



µ±ÒªÇó±¨ÎÄÔ̺¬$_GET['s']£¬¾ÍÈ¡ÆäÖµ×÷Ϊpathinfo£¬²¢·µ»Øpathinfo¸øÅ²Óú¯Êý¡£

·ÖÎö·¢ÏÖpathinfoº¯Êý±»library/think/Request.php:716ÖеÄpathº¯ÊýŲÓãº

public function path()
{
    if (is_null($this->path)) {
        $suffix   = $this->config['url_html_suffix'];
        $pathinfo = $this->pathinfo();


        if (false === $suffix) {
            // ²»ÈÝα¾²Ì¬½Ó¼û
            $this->path = $pathinfo;
        } elseif ($suffix) {
            // È¥³ýÕý³£µÄURLºó׺
            $this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
        } else {
            // ÔÊÐíÈκκó׺½Ó¼û
            $this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
        }
    }
    return $this->path;
}

 

ÏÔÈ»£¬ÕâÀï$this->pathÔ´×Ôpathinfo£¬Òò¶øÄܹ»±»¹¥»÷Õß½ÚÔì¡£³ÖÐø·ÖÎö¸Ã±äÁ¿µÄ´«µÝ£¬ÔÚlibrary/think/App.php:597Öб»ÒýÓãº

//public function routecheck()
$path = $this->request->path();
 
        // ÊÇ·ñÇ¿Ôì·ÓÉģʽ
        $must = !is_null($this->routeMust) ? $this->routeMust : $this->route->config('url_route_must');
 
        // ·Óɼì²â ·µ»ØÒ»¸öDispatch¶ÔÏó
        $dispatch = $this->route->check($path, $must);
 
        if (!empty($routeKey)) {
            try {
                if ($option) {
                    $this->cache->connect($option)->tag('route_cache')->set($routeKey, $dispatch);
                } else {
                    $this->cache->tag('route_cache')->set($routeKey, $dispatch);
                }
            } catch (\Exception $e) {
                // ´æÔڹذüµÄʱ³½»º´æÎÞЧ
            }
        }
 

        return $dispatch;


ÕâÀïÊǽøÐзÓɼì²â£¬¹¥»÷Õ߿ɿصÄ$path±»´«µÝ¸øÁËÈçϵÄcheckº¯Êý£º

public function check($url, $must = false)
    {
        // ×Ô¶¯¼ì²âÓòÃû·ÓÉ
        $domain = $this->checkDomain();
        $url    = str_replace($this->config['pathinfo_depr'], '|', $url);
 
        $completeMatch = $this->config['route_complete_match'];
 
        $result = $domain->check($this->request, $url, $completeMatch);
 
        if (false === $result && !empty($this->cross)) {
            // ¼ì²â¿çÓò·ÓÉ
            $result = $this->cross->check($this->request, $url, $completeMatch);
        }
 
        if (false !== $result) {
            // ·ÓÉÆ¥Åä
            return $result;
        } elseif ($must) {
            // Ç¿Ôì·Óɲ»Æ¥ÅäÔòÅ׳öÒì³£
            throw new RouteNotFoundException();
        }
        // ĬÈÏ·ÓɽâÎö
        return new UrlDispatch($this->request, $this->group, $url, [
            'auto_search' => $this->autoSearchController,
        ]);
    }


·ÖÎö´úÂë¿ÉÖª£¬ÈôÊÇ¿ªÆôÁËÇ¿Ôì·ÓÉÔò»áÅ׳öÒì³££¬Ò²¾ÍÊǹٷ½Ëù˵µÄ¸Ã·ì϶ÔÚ¿ªÆôÇ¿Ôì·ÓɵÄÇé¿öϲ»ÊÜÓ°Ï죨ĬÈϲ»¿ªÆô£©¡£


Checkº¯Êý×îºóÊ·ý»¯Ò»¸öUrlDispatch¶ÔÏ󣬽«$url´«µÝ¸øÁË»ú¹Øº¯Êý¡£³ÖÐø·ÖÎöUrlDispatchµÄ¸¸ÀàÒ²¾ÍÊÇDispatchÀàµÄ»ú¹Øº¯Êý:

library/think/route/Dispatch.php:64


 public function __construct(Request $request, Rule $rule, $dispatch, $param = [], $code = null)
    {
        $this->request  = $request;
        $this->rule     = $rule;
        $this->app      = Container::get('app');
        $this->dispatch = $dispatch;
        $this->param    = $param;
        $this->code     = $code;
 
        if (isset($param['convert'])) {
            $this->convert = $param['convert'];
        }
    }


$dispatch±äÁ¿¿É¿Ø²¢¸³Öµ¸øÁË$this->dispatch£¬¾­¹ýÂŴκ¯ÊýŲÓ÷µ»Ø£¬×îºóÈçϵÄUrlÀàµÄinit º¯Êý½«»á±»Å²ÓÃÀ´´¦ÖÃ$this->dispatch¡£

class Url extends Dispatch
{
    public function init()
    {
        // ½âÎöĬÈϵÄURL¹æ¶¨
        $result = $this->parseUrl($this->dispatch);
 
        return (new Module($this->request, $this->rule, $result))->init();
    }
 
    public function exec()
    {}


ÕâÀïŲÓÃparseUrl¶Ô$this->dispatch½øÐнâÎö£¬ÕâÊǸ÷ì϶µÄÖ÷ÌâµãÖ®Ò»£º

protected function parseUrl($url)
{
    $depr = $this->rule->getConfig('pathinfo_depr');
    $bind = $this->rule->getRouter()->getBind();
 
    if (!empty($bind) && preg_match('/^[a-z]/is', $bind)) {
        $bind = str_replace('/', $depr, $bind);
        // ÈôÊÇÓÐÄ £¿é/½ÚÔìÆ÷°ó¶¨
        $url = $bind . ('.' != substr($bind, -1) ? $depr : '') . ltrim($url, $depr);
    }
 
    list($path, $var) = $this->rule->parseUrlPath($url);
    if (empty($path)) {


ÕâÀïŲÓÃparseUrlPathº¯Êý¶Ô$url½øÐнâÎö£¬³ÖÐø·ÖÎö¸Ãº¯Êý£º


public function parseUrlPath($url)

    {
       ....
    ....
        } elseif (strpos($url, '/')) {
            // [Ä £¿é/½ÚÔìÆ÷/²Ù×÷]
            $path = explode('/', $url);
        } elseif (false !== strpos($url, '=')) {
            // ²ÎÊý1=Öµ1&²ÎÊý2=Öµ2...
            $path = [];
            parse_str($url, $var);
        } else {
            $path = [$url];
        }
 
        return [$path, $var];
    }


ÏÔÈ»£¬$urlµÄÌåʽΪ¡°Ä £¿é/½ÚÔìÆ÷/²Ù×÷¡±£¬$urlÔ׸îÐγÉÒ»¸öÊý×é´æµ½$path±äÁ¿Öв¢·µ»Øµ½Å²ÓÃÕß¡£


³ÖÐø·ÖÎö·âװ·ÓɵĴúÂë:


library/think/route/dispatch/Url.php:48

 list($path, $var) = $this->rule->parseUrlPath($url);
    ...
    ...
        // ½âÎöÄ £¿é
        $module = $this->rule->getConfig('app_multi_module') ? array_shift($path) : null;
 
        if ($this->param['auto_search']) {
            $controller = $this->autoFindController($module, $path);
        } else {
            // ½âÎö½ÚÔìÆ÷
            $controller = !empty($path) ? array_shift($path) : null;
        }
 
        // ½âÎö²Ù×÷
        $action = !empty($path) ? array_shift($path) : null;
        ...
        ...
        // ÉèÖõ±Ç°ÒªÇóµÄ²ÎÊý
        $this->request->setRouteVars($var);
 
        // ·âװ·ÓÉ
        $route = [$module, $controller, $action];
        return $route;


·ÓÉ·â×°·µ»Øµ½library/think/route/dispatch/Url.php:20


class Url extends Dispatch

{
    public function init()
    {
        // ½âÎöĬÈϵÄURL¹æ¶¨
        $result = $this->parseUrl($this->dispatch);
 
        return (new Module($this->request, $this->rule, $result))->init();
    }


$result¾ÍÊÇ·â×°ºÃµÄ·ÓÉÊý×飬´«µÝ¸øÁËModuleµÄ»ú¹Øº¯Êý¡£
ÓÉÓÚModuleÒ²ÊǼ̳Ð×ÔDispatchÀֱ࣬½Ó¿´DispatchµÄ»ú¹Øº¯Êý:


public function __construct(Request $request, Rule $rule, $dispatch, $param = [], $code = null)
    {
        $this->request  = $request;
        $this->rule     = $rule;
        $this->app      = Container::get('app');
        $this->dispatch = $dispatch;
        $this->param    = $param;
        $this->code     = $code;
 
        if (isset($param['convert'])) {
            $this->convert = $param['convert'];
        }
    }


$result¸³Öµ¸øÁË$this->dispatch¡£¶øºóŲÓÃModuleÀàµÄinitº¯Êý£º


public function init()
    {
        parent::init();
        $result = $this->dispatch;
  
        if ($this->rule->getConfig('app_multi_module')) {
            // ¶àÄ £¿é²¿Êð
            $module    = strip_tags(strtolower($result[0] ?: $this->rule->getConfig('default_module')));
            ...
            ...
            } elseif (!in_array($module, $this->rule->getConfig('deny_module_list')) && is_dir($this->app->getAppPath() . $module)) {
                $available = true;
            } 
           ...
           ...
            // Ä £¿é³õʼ»¯
            if ($module && $available) {
                // ³õʼ»¯Ä £¿é
                $this->request->setModule($module);
                $this->app->init($module);
            } else {
                throw new HttpException(404, 'module not exists:' . $module);
            }
        }
        // »ñÈ¡½ÚÔìÆ÷Ãû
        $controller       = strip_tags($result[1] ?: $this->rule->getConfig('default_controller'));
        $this->controller = $convert ? strtolower($controller) : $controller;
        // »ñÈ¡²Ù×÷Ãû
        $this->actionName = strip_tags($result[2] ?: $this->rule->getConfig('default_action'));
        // ÉèÖõ±Ç°ÒªÇóµÄ½ÚÔìÆ÷¡¢²Ù×÷
        $this->request
            ->setController(Loader::parseName($this->controller, 1))
            ->setAction($this->actionName);
 
        return $this;
    }


ÕâÀï´æÔÚµÚÒ»¸ö¶Ô$moduleµÄÅжÏ£¬±ØÒªÈÃ$availableµÅ×Útrue£¬Õâ¾Í±ØÒªis_dir($this->app->getAppPath() . $module)³ÉÁ¢¡£¹Ù·½demo¸ø³öµÄÄ £¿éÊÇindex£¬¶øÏÖʵ¿ª·¢·¨Ê½²»Ô¸¶¨´æÔÚ¸ÃÄ £¿éÃû£¬ËùÒÔ»ú¹ØpayloadʹØâÀïÊÇÒ»¸ö°ÑÎȵã¡£


Âú×ãÕâ¸öÅжÏǰÌáºó£¬³ÖÐø·ÖÎöºóÐøµÄ½ÚÔìÁ÷»á½øÈëÈçÏÂmoduleµÄexecº¯Êý£ºlibrary/think/route/dispatch/Module.php:80


public function exec()

    {
        // ¼àÌýmodule_init
        $this->app['hook']->listen('module_init');
 
        try {
            // Ê·ý»¯½ÚÔìÆ÷
            $instance = $this->app->controller($this->controller,
                $this->rule->getConfig('url_controller_layer'),
                $this->rule->getConfig('controller_suffix'),
                $this->rule->getConfig('empty_controller'));
 
            if ($instance instanceof Controller) {
                $instance->registerMiddleware();
            }
        } catch (ClassNotFoundException $e) {
            throw new HttpException(404, 'controller not exists:' . $e->getClass());
        }


·ÖÎö·¢ÏÖ£¬$this->controllerÊǹ¥»÷Õ߿ɿصÄ£¬²¢´«µÝ¸øÁËÈçϵÄcontrollerº¯Êý£¬³ÖÐø·ÖÎö¸Ãº¯Êý£º


  public function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '')
    {
        list($module, $class) = $this->parseModuleAndClass($name, $layer, $appendSuffix);
 
        if (class_exists($class)) {
            return $this->__get($class);
        } elseif ($empty && class_exists($emptyClass = $this->parseClass($module, $layer, $empty, $appendSuffix))) {
            return $this->__get($emptyClass);
        }
        throw new ClassNotFoundException('class not exists:' . $class, $class);
    }


ÔÚÕâÀnameÊǹ¥»÷Õ߿ɿصÄ£¬²¢´«µÝ¸øÁËÈçϵÄparseModuleAndClassº¯Êý£º


protected function parseModuleAndClass($name, $layer, $appendSuffix)

    {
        if (false !== strpos($name, '\\')) {
            $class  = $name;
            $module = $this->request->module();
        } else {
            if (strpos($name, '/')) {
                list($module, $name) = explode('/', $name, 2);
            } else {
                $module = $this->request->module();
            }
 
            $class = $this->parseClass($module, $layer, $name, $appendSuffix);
        }
 
        return [$module, $class];
    }


·ÖÎö·¢ÏÖ£¬µ±$name´æÔÚ·´Ð±¸Üʱ¾ÍÖ±½Ó½«$name¸³Öµ¸ø$class²¢·µ»Ø¡£ÏÔÈ»£¬¹¥»÷Õßͨ¹ý½ÚÔìÊäÈë¾ÍÄܹ»²Ù¿ØÀàµÄÊ·ý»¯¹ý³Ì£¬´Ó¶øÔì³É´úÂëÖ´Ðзì϶¡£



²¹¶¡·ÖÎö

ÔÚThinkPHP5.0.23ÒÔ¼°5.1.31°æ±¾ÖУ¬Ôö³¤Á˶Ô$controllerµÄÕýÔò¹ýÂË£º


µ¼ÖÂÎÞ·¨ÔÙ´«Èë\think\appÕâÖÖ´ó¾ÖµÄ½ÚÔìÆ÷¡£


½á ÂÛ

´Ë·ì϶ÊÇÓÉÓÚ¿ò¼Ü¶Ô´«ÈëµÄ·ÓɲÎÊý¹ýÂ˲»Ñϸñ£¬µ¼Ö¹¥»÷ÕßÄܹ»²Ù×÷·ÇÔ¤ÆÚµÄ½ÚÔìÆ÷ÀàÀ´Ô¶³ÌÖ´ÐдúÂë¡£½øÒ»²½·ÖÎö·¢ÏÖ£¬Ä³Ð©ThinkPHP°æ±¾²»ÊÜÒѹ«¿ªµÄPOCµÄÓ°Ï죬ÕâÊÇÓÉÓÚ¸ÃPOC²»×ãÍêÕûÐÔ˼¿¼¡£Òò¶ø£¬Ç¿ÁÒ½¨ÒéÓû§ÊµÊ±½«5.0.x°æ±¾Éý¼¶µ½5.0.23£¬½«5.1.x°æ±¾Éý¼¶µ½5.1.31£¬ÒÔÃâÔâ·ê¹¥»÷¡£