* Date: 2019/12/5 * Time: 17:44 */ // +---------------------------------------------------------------------- // 系统方法 // +---------------------------------------------------------------------- /** * 清理temp目录 */ function temp_clear() { array_map('unlink', glob(TEMP_PATH . '*.php')); } /** * 获取后台名称 * @return string */ function systemName() { if (is_installed()) { $name = \app\common\model\Webconfig::getValue('name', 3600); if ($name) { return $name; } } return PRODUCT_NAME; } /** * 检查php版本 */ function check_php_version() { if (PHP_VERSION < 7 || PHP_VERSION >= 7.3) { header("Content-type: text/html; charset=utf-8"); exit("运行环境要求PHP 7.0 ~ 7.2,请修改PHP版本"); } } /** * 检查安装文件 */ function check_installed() { if (!is_installed()) { exit("未找到安裝文件,请重新安装!"); } } /** * 判断网站维护中 */ function check_is_close_site() { if (is_installed()) { $is_close_site = \app\common\model\Webconfig::getValue('is_close_site', 3600); if ($is_close_site) { $request = \think\Request::instance(); if ($request->has('key')) { cookie('key', $request->param('key')); } if (cookie('key') != (new \app\admin\model\Urlconfig())->getCloseSiteKey()) { exit("网站维护中 ..."); } } } } /** * 注册自定义路由 */ function register_route() { if (is_installed()) { //url美化 例:Route::rule('blog/:id','index/blog/read'); if (think\Cache::get('vae_route')) { $runtimeRoute = think\Cache::get('vae_route'); } else { $runtimeRoute = \think\Db::name("urlconfig")->where(['status' => 1])->order('id desc')->column('aliases,url'); think\Cache::set('vae_route', $runtimeRoute); } if ($runtimeRoute) { \think\Route::rule($runtimeRoute); } } } /** * 判断是否完成安装 * @return bool */ function is_installed() { static $isInstalled; if (empty($isInstalled)) { $isInstalled = file_exists(ROOT_PATH . 'install.lock'); } return $isInstalled; } /** * 用于整個系统的加密解密的salt * @return string */ function system_salt() { check_installed(); //使用安装文件计算得到salt,注意保密,否则会导致密码被破解 $origin = file_get_contents(ROOT_PATH . "install.lock"); return password($origin, 'Y8xz1WH7KiQyE4GXgpJIVcbvLCeUdoOr'); } /** * 获取附件表的url * @param $id int 附件表的id * @param $default string 设置空时的默认值 * @param $host bool/string 可指定域名,默认当前域名 * @return bool|mixed|string */ function geturl($id, $default = '', $host = true) { if ($id) { $attachment = \think\Db::name("attachment")->where(['id' => $id])->find(); if (empty($attachment)) { return $default; } if ($attachment['status'] == 1) { //审核通过 $thumb_url = replaceUrlDS($attachment['filepath']); } elseif ($attachment['status'] == 0) { //待审核 $thumb_url = '/static/public/images/shenhe.jpg'; } else { //不通过 $thumb_url = '/static/public/images/shenhe.jpg'; } if (startWith($thumb_url, 'http')) { return $thumb_url; } else { return url($thumb_url, '', false, $host); } } return $default; } /** * 邮件发送 * @param $to_email string 收件邮箱地址 * @param $title string 邮件标题 * @param $content string 邮件内容 * @param $from_email string 发件邮箱地址 * @param $from_name string 发件人名字 * @param $smtp string [smtp服务器] * @param $username string 发件箱的账号 * @param $password string 发件箱的密码(或授权码) * @return bool */ function SendMail($to_email, $title, $content, $from_email, $from_name, $smtp, $username, $password) { vendor('phpmailer.PHPMailerAutoload'); //vendor('PHPMailer.class#PHPMailer'); $mail = new \PHPMailer(); // 设置PHPMailer使用SMTP服务器发送Email $mail->IsSMTP(); // 设置邮件的字符编码,若不指定,则为'UTF-8' $mail->CharSet = 'UTF-8'; // 添加收件人地址,可以多次使用来添加多个收件人 $mail->AddAddress($to_email); // 设置邮件正文 $mail->Body = $content; // 设置邮件头的From字段。 $mail->From = $from_email; // 设置发件人名字 $mail->FromName = $from_name; // 设置邮件标题 $mail->Subject = $title; // 设置SMTP服务器。 $mail->Host = $smtp; // 端口 $mail->SMTPSecure = 'ssl'; $mail->Port = 465; // 设置为"需要验证" ThinkPHP 的config方法读取配置文件 $mail->SMTPAuth = true; //设置html发送格式 $mail->isHTML(true); // 设置用户名和密码。 $mail->Username = $username; $mail->Password = $password; // 发送邮件。 return ($mail->Send()); } /** * 阿里大鱼短信发送 * @param $param * @param $phone */ function SendSms($param, $phone) { // 配置信息 import('dayu.top.TopClient'); import('dayu.top.TopLogger'); import('dayu.top.request.AlibabaAliqinFcSmsNumSendRequest'); import('dayu.top.ResultSet'); import('dayu.top.RequestCheckUtil'); //获取短信配置 $data = \think\Db::name('smsconfig')->where('sms', 'sms')->find(); $appkey = $data['appkey']; $secretkey = $data['secretkey']; $type = $data['type']; $name = $data['name']; $code = $data['code']; $c = new \TopClient(); $c->appkey = $appkey; $c->secretKey = $secretkey; $req = new \AlibabaAliqinFcSmsNumSendRequest(); //公共回传参数,在“消息返回”中会透传回该参数。非必须 $req->setExtend(""); //短信类型,传入值请填写normal $req->setSmsType($type); //短信签名,传入的短信签名必须是在阿里大于“管理中心-验证码/短信通知/推广短信-配置短信签名”中的可用签名。 $req->setSmsFreeSignName($name); //短信模板变量,传参规则{"key":"value"},key的名字须和申请模板中的变量名一致,多个变量之间以逗号隔开。 $req->setSmsParam($param); //短信接收号码。支持单个或多个手机号码,传入号码为11位手机号码,不能加0或+86。群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码。 $req->setRecNum($phone); //短信模板ID,传入的模板必须是在阿里大于“管理中心-短信模板管理”中的可用模板。 $req->setSmsTemplateCode($code); //发送 $resp = $c->execute($req); } /** * 判断蜘蛛访问 * @return bool */ function isSpiderRobot() { $ua = strtolower($_SERVER['HTTP_USER_AGENT']); $botchar = "/(baidu|google|spider|soso|yahoo|sohu|yodao|robozilla|bing|sogou|twiceler|msn|haosou|360|ia_archiver|iaarchiver|slurp|bot)/i"; return preg_match($botchar, $ua) ? true : false; } /** * 判断是否微信中打开 * @return bool */ function isWeixin() { if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { return true; } else { return false; } } /** * 防止快速刷新,如3秒内请求5次,则提示异常 * @param string $seconds 几秒内 * @param string $refresh 刷新次数 */ function stopCC($seconds = '3', $refresh = '5') { //代理IP直接退出 empty($_SERVER['HTTP_VIA']) or exit('Ip Agent Denied'); // 设置监控变量 $cur_time = time(); if (session("?last_time")) { session("refresh_times", session("refresh_times") + 1); } else { session("last_time", $cur_time); session("refresh_times", 1); } // 处理监控结果 if ($cur_time - session("last_time") <= $seconds && session("refresh_times") >= $refresh) { // 跳转至攻击者服务器地址 header(sprintf('Location:%s', 'http://127.0.0.1')); exit ('Access Denied'); } else { session("refresh_times", 0); session("last_time", $cur_time); } } /** * 获取$dir目录下的所有 文件名or目录名 * @param $dir string 根目录 * @param string $pattern 指定匹配的文件名,通配符`*` * @param string $suffix 可选,文件扩展名。如果文件名有指定的扩展名,将不会返回这个扩展名 * @return array */ function getFilenameList($dir, $pattern = '*.*', $suffix = null) { if (!$suffix) { $suffix = deleteStartDS($pattern, '*');//默认值 } $pathList = glob(appendEndDS($dir) . $pattern);//匹配文件/目录的数组。如果失败则返回 FALSE $res = []; foreach ($pathList as $key => $path) { $res[] = basename($path, $suffix);//返回路径的文件名 } return $res; } /** * 获取控制器的所有方法名 * @param $className [类全名] * @param string $base [父类全名,过滤父级的方法名] * @return array */ function getActions($className, $base = '\app\api\controller\base\Base') { $methods = get_class_methods(new $className()); $baseMethods = get_class_methods(new $base()); return array_diff($methods, $baseMethods); }