标签 thinkphp 下的文章

最近接到反馈,说网站无法在电脑微信上打开,提示 页面错误!请稍后再试~,开启调试模式后发现报 Undefined offset: 1 in Lang.php line 204

问题定位

经过排查,应该是微信浏览器近期进行了更新,给服务器传递了 Accept-Language: *
然后框架的多语言自动侦测逻辑不严谨,导致了此次错误。

出错文件:library/think/Lang.php
出错代码:

        if (isset($_GET[self::$langDetectVar])) {
            // url 中设置了语言变量
            $langSet = strtolower($_GET[self::$langDetectVar]);
        } elseif (isset($_COOKIE[self::$langCookieVar])) {
            // Cookie 中设置了语言变量
            $langSet = strtolower($_COOKIE[self::$langCookieVar]);
        } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            // 自动侦测浏览器语言
            preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
            $langSet     = strtolower($matches[1]);
            $acceptLangs = Config::get('header_accept_lang');

            if (isset($acceptLangs[$langSet])) {
                $langSet = $acceptLangs[$langSet];
            } elseif (isset(self::$acceptLanguage[$langSet])) {
                $langSet = self::$acceptLanguage[$langSet];
            }
        }

关键定位:

// 自动侦测浏览器语言
preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
// 这里没有判断 preg_match 是否匹配到内容,直接就使用 $matches[1] ,然后就报错了
$langSet     = strtolower($matches[1]);

解决办法

解决办法很简单,先判断 preg_match,再执行后续代码。

调整后代码:

        if (isset($_GET[self::$langDetectVar])) {
            // url 中设置了语言变量
            $langSet = strtolower($_GET[self::$langDetectVar]);
        } elseif (isset($_COOKIE[self::$langCookieVar])) {
            // Cookie 中设置了语言变量
            $langSet = strtolower($_COOKIE[self::$langCookieVar]);
        } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && preg_match('/^([a-z\d\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches)) {
            // 自动侦测浏览器语言
            $langSet     = strtolower($matches[1]);
            $acceptLangs = Config::get('header_accept_lang');

            if (isset($acceptLangs[$langSet])) {
                $langSet = $acceptLangs[$langSet];
            } elseif (isset(self::$acceptLanguage[$langSet])) {
                $langSet = self::$acceptLanguage[$langSet];
            }
        }

影响版本

目前主要发现影响 ThinkPHP 5.0 和 5.1 版本,已向官方提交修复,但这两个版本已停止更新,不确定是否会合并。

在接触一些thinkphp新手时,发现总是有一部分人不会使用composer来安装扩展包。于是他们就按照tp3的方式来下载扩展包的压缩包,然后将扩展包解压到项目里面去,结果最后发现用不了,提示类不存在Class 'EasyWeChat\Factory not found`。这里主要下,如何在thinkphp的项目里使用composer来安装扩展包,助力下这部分"迷途的人"。

安装composer

安装composer的方法网上已经很多了,所以这里就不重复去说了。但是要注意电脑里的php版本不要太低,建议使用php7.2
参考方法:https://www.runoob.com/w3cnote/composer-install-and-usage.html

使用composer安装扩展包

现今的9102年,大多数的php扩展包都支持使用composer来进行安装,所以会composer的使用已经算是一项非常必要的技能了,就跟学会复制黏贴一样重要。
下面就以安装PHPMailer为例。

1.获取composer安装命令

打开PHPMailerGitHub,在它的文档里能看到一条composer的命令,一般在支持composer安装的扩展包文档里都会包含这个命令,命令以composer require开头,后面跟着扩展包的“名称”。
命令:

composer require phpmailer/phpmailer

2.打开命令行,并切换到项目目录

首先,这里假设我们的项目放在了E:/wwwroot/www.ll00.cn,打开这个目录能看到config extend public route runtime vendor等目录。
然后打开命令行,输入E:切换到E盘,再输入cd E:/wwwroot/www.ll00.cn切换到项目目录

不要将运行目录切换到public或者vender,我看很多人都犯这样的错误
E:
cd E:/wwwroot/www.ll00.cn

3.执行composer安装命令

安装命令我们已经在第一步获取到了,并且命令行也将运行目录切换到了项目目录里,这时候就可以执行composer命令来安装扩展包了

composer require phpmailer/phpmailer

到这里,如无意外,扩展包就安装好了

使用扩展包

以下是在项目里使用PHPMailer的示例代码

<?php
// 导入 PHPMailer 类到当前命名空间
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// 实例化PHPMailer
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'smtp1.example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     // SMTP username
    $mail->Password   = 'secret';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
    $mail->Port       = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

最近ThinkPHP框架出现了一个比较严重的漏洞,在没有开启强制路由的情况下可能的getshell漏洞,受影响的版本包括5.0.23和5.1.31之前的所有版本。
官方也很快提供了解决方案,大大的点个赞。但是只是讲了个重点,没讲太详细,对于一些新手和初学者可能不大方便操作。下面提供一些修复的方法,应该算是比较详细了。

ThinkPHP5.0

使用行为

手册:https://www.kancloud.cn/manual/thinkphp5/118130
/application/tags.php文件绑定模块初始化行为

<?php
\think\Hook::add('module_init',function(){
    if (!preg_match('/^[A-Za-z](\w|\.)*$/', \think\Request::instance()->controller())) {
        throw new \think\exception\HttpException(404, 'controller not exists:' . \think\Request::instance()->controller());
    }
});

直接修改框架

打开/thinkphp/library/think/App.php,搜索获取控制器名,然后在获取控制器的代码后面加上三行代码。
下面是示例(在一些比较低的版本,控制器名的变量是$controllerName):

// 获取控制器名
$controller = strip_tags($result[1] ?: $config['default_controller']);
$controller = $convert ? strtolower($controller) : $controller;

// 获取控制器的代码后面加上下面三行代码
if (!preg_match('/^[A-Za-z](\w|\.)*$/', $controller)) {
    throw new HttpException(404, 'controller not exists:' . $controller);
}

ThinkPHP5.1

使用行为

手册:https://www.kancloud.cn/manual/thinkphp5_1/354129
/application/tags.php文件绑定模块初始化行为

<?php
\think\facade\Hook::add('module_init', function () {
    if (!preg_match('/^[A-Za-z](\w|\.)*$/', \think\facade\Request::controller())) {
        throw new \think\exception\HttpException(404, 'controller not exists:' . \think\facade\Request::controller());
    }
});

使用中间件

手册:https://www.kancloud.cn/manual/thinkphp5_1/564279
/config/middleware.php文件注册中间件

<?php
\think\facade\Route::middleware(function (\think\Request $request, \Closure $next) {
    if (!preg_match('/^[A-Za-z](\w|\.)*$/', $request->controller())) {
        throw new \think\exception\HttpException(404, 'controller not exists:' . $request->controller());
    }
    return $next($request);
});

直接修改框架

打开/thinkphp/library/think/route/dispatch/Url.php,搜索解析控制器,然后在解析控制器的代码后面加上三行代码。
下面是示例:

if ($this->param['auto_search']) {
    $controller = $this->autoFindController($module, $path);
} else {
    // 解析控制器
    $controller = !empty($path) ? array_shift($path) : null;
}

// 解析控制器的代码后面加上下面三行代码
if ($controller && !preg_match('/^[A-Za-z][\w|\.]*$/', $controller)) {
    throw new HttpException(404, 'controller not exists:' . $controller);
}