helper_env.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. // +----------------------------------------------------------------------
  9. // 系统方法
  10. // +----------------------------------------------------------------------
  11. /**
  12. * 清理temp目录
  13. */
  14. function temp_clear()
  15. {
  16. array_map('unlink', glob(TEMP_PATH . '*.php'));
  17. }
  18. /**
  19. * 获取后台名称
  20. * @return string
  21. */
  22. function systemName()
  23. {
  24. if (is_installed()) {
  25. $name = \app\common\model\Webconfig::getValue('name', 3600);
  26. if ($name) {
  27. return $name;
  28. }
  29. }
  30. return PRODUCT_NAME;
  31. }
  32. /**
  33. * 检查php版本
  34. */
  35. function check_php_version()
  36. {
  37. if (PHP_VERSION < 7 || PHP_VERSION >= 7.3) {
  38. header("Content-type: text/html; charset=utf-8");
  39. exit("运行环境要求PHP 7.0 ~ 7.2,请修改PHP版本");
  40. }
  41. }
  42. /**
  43. * 检查安装文件
  44. */
  45. function check_installed()
  46. {
  47. if (!is_installed()) {
  48. exit("未找到安裝文件,请重新安装!");
  49. }
  50. }
  51. /**
  52. * 判断网站维护中
  53. */
  54. function check_is_close_site()
  55. {
  56. if (is_installed()) {
  57. $is_close_site = \app\common\model\Webconfig::getValue('is_close_site', 3600);
  58. if ($is_close_site) {
  59. $request = \think\Request::instance();
  60. if ($request->has('key')) {
  61. cookie('key', $request->param('key'));
  62. }
  63. if (cookie('key') != (new \app\admin\model\Urlconfig())->getCloseSiteKey()) {
  64. exit("网站维护中 ...");
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * 注册自定义路由
  71. */
  72. function register_route()
  73. {
  74. if (is_installed()) {
  75. //url美化 例:Route::rule('blog/:id','index/blog/read');
  76. if (think\Cache::get('vae_route')) {
  77. $runtimeRoute = think\Cache::get('vae_route');
  78. } else {
  79. $runtimeRoute = \think\Db::name("urlconfig")->where(['status' => 1])->order('id desc')->column('aliases,url');
  80. think\Cache::set('vae_route', $runtimeRoute);
  81. }
  82. if ($runtimeRoute) {
  83. \think\Route::rule($runtimeRoute);
  84. }
  85. }
  86. }
  87. /**
  88. * 判断是否完成安装
  89. * @return bool
  90. */
  91. function is_installed()
  92. {
  93. static $isInstalled;
  94. if (empty($isInstalled)) {
  95. $isInstalled = file_exists(ROOT_PATH . 'install.lock');
  96. }
  97. return $isInstalled;
  98. }
  99. /**
  100. * 用于整個系统的加密解密的salt
  101. * @return string
  102. */
  103. function system_salt()
  104. {
  105. check_installed();
  106. //使用安装文件计算得到salt,注意保密,否则会导致密码被破解
  107. $origin = file_get_contents(ROOT_PATH . "install.lock");
  108. return password($origin, 'Y8xz1WH7KiQyE4GXgpJIVcbvLCeUdoOr');
  109. }
  110. /**
  111. * 获取附件表的url
  112. * @param $id int 附件表的id
  113. * @param $default string 设置空时的默认值
  114. * @param $host bool/string 可指定域名,默认当前域名
  115. * @return bool|mixed|string
  116. */
  117. function geturl($id, $default = '', $host = true)
  118. {
  119. if ($id) {
  120. $attachment = \think\Db::name("attachment")->where(['id' => $id])->find();
  121. if (empty($attachment)) {
  122. return $default;
  123. }
  124. if ($attachment['status'] == 1) {
  125. //审核通过
  126. $thumb_url = replaceUrlDS($attachment['filepath']);
  127. } elseif ($attachment['status'] == 0) {
  128. //待审核
  129. $thumb_url = '/static/public/images/shenhe.jpg';
  130. } else {
  131. //不通过
  132. $thumb_url = '/static/public/images/shenhe.jpg';
  133. }
  134. if (startWith($thumb_url, 'http')) {
  135. return $thumb_url;
  136. } else {
  137. return url($thumb_url, '', false, $host);
  138. }
  139. }
  140. return $default;
  141. }
  142. /**
  143. * 邮件发送
  144. * @param $to_email string 收件邮箱地址
  145. * @param $title string 邮件标题
  146. * @param $content string 邮件内容
  147. * @param $from_email string 发件邮箱地址
  148. * @param $from_name string 发件人名字
  149. * @param $smtp string [smtp服务器]
  150. * @param $username string 发件箱的账号
  151. * @param $password string 发件箱的密码(或授权码)
  152. * @return bool
  153. */
  154. function SendMail($to_email, $title, $content, $from_email, $from_name, $smtp, $username, $password)
  155. {
  156. vendor('phpmailer.PHPMailerAutoload');
  157. //vendor('PHPMailer.class#PHPMailer');
  158. $mail = new \PHPMailer();
  159. // 设置PHPMailer使用SMTP服务器发送Email
  160. $mail->IsSMTP();
  161. // 设置邮件的字符编码,若不指定,则为'UTF-8'
  162. $mail->CharSet = 'UTF-8';
  163. // 添加收件人地址,可以多次使用来添加多个收件人
  164. $mail->AddAddress($to_email);
  165. // 设置邮件正文
  166. $mail->Body = $content;
  167. // 设置邮件头的From字段。
  168. $mail->From = $from_email;
  169. // 设置发件人名字
  170. $mail->FromName = $from_name;
  171. // 设置邮件标题
  172. $mail->Subject = $title;
  173. // 设置SMTP服务器。
  174. $mail->Host = $smtp;
  175. // 端口
  176. $mail->SMTPSecure = 'ssl';
  177. $mail->Port = 465;
  178. // 设置为"需要验证" ThinkPHP 的config方法读取配置文件
  179. $mail->SMTPAuth = true;
  180. //设置html发送格式
  181. $mail->isHTML(true);
  182. // 设置用户名和密码。
  183. $mail->Username = $username;
  184. $mail->Password = $password;
  185. // 发送邮件。
  186. return ($mail->Send());
  187. }
  188. /**
  189. * 阿里大鱼短信发送
  190. * @param $param
  191. * @param $phone
  192. */
  193. function SendSms($param, $phone)
  194. {
  195. // 配置信息
  196. import('dayu.top.TopClient');
  197. import('dayu.top.TopLogger');
  198. import('dayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
  199. import('dayu.top.ResultSet');
  200. import('dayu.top.RequestCheckUtil');
  201. //获取短信配置
  202. $data = \think\Db::name('smsconfig')->where('sms', 'sms')->find();
  203. $appkey = $data['appkey'];
  204. $secretkey = $data['secretkey'];
  205. $type = $data['type'];
  206. $name = $data['name'];
  207. $code = $data['code'];
  208. $c = new \TopClient();
  209. $c->appkey = $appkey;
  210. $c->secretKey = $secretkey;
  211. $req = new \AlibabaAliqinFcSmsNumSendRequest();
  212. //公共回传参数,在“消息返回”中会透传回该参数。非必须
  213. $req->setExtend("");
  214. //短信类型,传入值请填写normal
  215. $req->setSmsType($type);
  216. //短信签名,传入的短信签名必须是在阿里大于“管理中心-验证码/短信通知/推广短信-配置短信签名”中的可用签名。
  217. $req->setSmsFreeSignName($name);
  218. //短信模板变量,传参规则{"key":"value"},key的名字须和申请模板中的变量名一致,多个变量之间以逗号隔开。
  219. $req->setSmsParam($param);
  220. //短信接收号码。支持单个或多个手机号码,传入号码为11位手机号码,不能加0或+86。群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码。
  221. $req->setRecNum($phone);
  222. //短信模板ID,传入的模板必须是在阿里大于“管理中心-短信模板管理”中的可用模板。
  223. $req->setSmsTemplateCode($code);
  224. //发送
  225. $resp = $c->execute($req);
  226. }
  227. /**
  228. * 判断蜘蛛访问
  229. * @return bool
  230. */
  231. function isSpiderRobot()
  232. {
  233. $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
  234. $botchar = "/(baidu|google|spider|soso|yahoo|sohu|yodao|robozilla|bing|sogou|twiceler|msn|haosou|360|ia_archiver|iaarchiver|slurp|bot)/i";
  235. return preg_match($botchar, $ua) ? true : false;
  236. }
  237. /**
  238. * 判断是否微信中打开
  239. * @return bool
  240. */
  241. function isWeixin()
  242. {
  243. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
  244. return true;
  245. } else {
  246. return false;
  247. }
  248. }
  249. /**
  250. * 防止快速刷新,如3秒内请求5次,则提示异常
  251. * @param string $seconds 几秒内
  252. * @param string $refresh 刷新次数
  253. */
  254. function stopCC($seconds = '3', $refresh = '5')
  255. {
  256. //代理IP直接退出
  257. empty($_SERVER['HTTP_VIA']) or exit('Ip Agent Denied');
  258. // 设置监控变量
  259. $cur_time = time();
  260. if (session("?last_time")) {
  261. session("refresh_times", session("refresh_times") + 1);
  262. } else {
  263. session("last_time", $cur_time);
  264. session("refresh_times", 1);
  265. }
  266. // 处理监控结果
  267. if ($cur_time - session("last_time") <= $seconds && session("refresh_times") >= $refresh) {
  268. // 跳转至攻击者服务器地址
  269. header(sprintf('Location:%s', 'http://127.0.0.1'));
  270. exit ('Access Denied');
  271. } else {
  272. session("refresh_times", 0);
  273. session("last_time", $cur_time);
  274. }
  275. }
  276. /**
  277. * 获取$dir目录下的所有 文件名or目录名
  278. * @param $dir string 根目录
  279. * @param string $pattern 指定匹配的文件名,通配符`*`
  280. * @param string $suffix 可选,文件扩展名。如果文件名有指定的扩展名,将不会返回这个扩展名
  281. * @return array
  282. */
  283. function getFilenameList($dir, $pattern = '*.*', $suffix = null)
  284. {
  285. if (!$suffix) {
  286. $suffix = deleteStartDS($pattern, '*');//默认值
  287. }
  288. $pathList = glob(appendEndDS($dir) . $pattern);//匹配文件/目录的数组。如果失败则返回 FALSE
  289. $res = [];
  290. foreach ($pathList as $key => $path) {
  291. $res[] = basename($path, $suffix);//返回路径的文件名
  292. }
  293. return $res;
  294. }
  295. /**
  296. * 获取控制器的所有方法名
  297. * @param $className [类全名]
  298. * @param string $base [父类全名,过滤父级的方法名]
  299. * @return array
  300. */
  301. function getActions($className, $base = '\app\api\controller\base\Base')
  302. {
  303. $methods = get_class_methods(new $className());
  304. $baseMethods = get_class_methods(new $base());
  305. return array_diff($methods, $baseMethods);
  306. }