<?php
/**
 *  Guojiz网址导航系统
 *	www.guojiz.com
 *  作者：梦雨
 *  @ QQ50361804
 */
use app\common\Hook;
use Captcha\Captcha;
use org\Http;
use think\Cache;
use think\Db;
use think\Loader;
use think\Request;
use think\Url;
use QL\QueryList;
use PHPMailer\PHPMailer;
// 应用公共文件
error_reporting(E_ERROR | E_PARSE);
/*
邮件发送
*/
function send_mail($tomail, $subject = '', $body = '')
{
    if (warning() == 2) {
        $mail = new PHPMailer(true);
        //实例化PHPMailer对象
        $mail->CharSet = 'UTF-8';
        //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->IsSMTP();
        // 设定使用SMTP服务
        $mail->SMTPDebug = 0;
        // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
        $mail->SMTPAuth = true;
        // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';
        // 使用安全协议
        $mail->Host = guojiz('mailHost');
        // SMTP 服务器
        $mail->Port = guojiz('mailPort');
        // SMTP服务器的端口号
        $mail->Username = guojiz('mailUsername');
        // SMTP服务器用户名
        $mail->Password = guojiz('mailPassword');
        // SMTP服务器密码
        $mail->SetFrom('' . guojiz('mailUsername') . '', '' . guojiz('title') . '');
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($tomail);
        return $mail->Send() ? true : $mail->ErrorInfo;
    }
}
//ip黑名单公用
function htmltext($str)
{
    $str = preg_replace("/<style .*?<\\/style>/is", "", $str);
    $str = preg_replace("/<script .*?<\\/script>/is", "", $str);
    $str = preg_replace("/<p .*?<\\/p>/is", "", $str);
    $str = preg_replace("/<br \\s*\\/>/i", "", $str);
    $str = preg_replace("/<\\/?p>/i", "", $str);
    $str = preg_replace("/<\\/?td>/i", "", $str);
    $str = preg_replace("/<\\/?div>/i", "", $str);
    $str = preg_replace("/<\\/?ul>/i", "", $str);
    $str = preg_replace("/<\\/?span>/i", "", $str);
    $str = preg_replace("/<\\/?li>/i", "", $str);
    $str = preg_replace("/ /i", " ", $str);
    $str = preg_replace("/ /i", " ", $str);
    $str = preg_replace("/&/i", "&", $str);
    $str = preg_replace("/&/i", "&", $str);
    $str = preg_replace("/</i", "<", $str);
    $str = preg_replace("/</i", "<", $str);
    $str = preg_replace("/“/i", '"', $str);
    $str = preg_replace("/&ldquo/i", '"', $str);
    $str = preg_replace("/‘/i", "'", $str);
    $str = preg_replace("/&lsquo/i", "'", $str);
    $str = preg_replace("/'/i", "'", $str);
    $str = preg_replace("/&rsquo/i", "'", $str);
    $str = preg_replace("/>/i", ">", $str);
    $str = preg_replace("/>/i", ">", $str);
    $str = preg_replace("/”/i", '"', $str);
    $str = preg_replace("/&rdquo/i", '"', $str);
    $str = strip_tags($str);
    $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
    $str = preg_replace("/&#.*?;/i", "", $str);
    return $str;
}
$hoss = (include 'application/extra/ip.php');
if (strpos($hoss['ip'], $_SERVER["REMOTE_ADDR"]) !== false) {
    //header(sprintf('Location:%s', $hoss['ipwww']));
    exit($hoss['ipts']);
}
empty($_SERVER['HTTP_VIA']) or exit('Access Denied');
//防止快速刷新
session_start();
$seconds = $hoss['ipmiao'];
//时间段[秒]
$refresh = $hoss['ipshu'];
//刷新次数
//设置监控变量
$cur_time = time();
if (isset($_SESSION['last_time'])) {
    $_SESSION['refresh_times'] += 1;
} else {
    $_SESSION['refresh_times'] = 1;
    $_SESSION['last_time'] = $cur_time;
}
//处理监控结果
if ($cur_time - $_SESSION['last_time'] < $seconds) {
    if ($_SESSION['refresh_times'] >= $refresh) {
        //跳转至攻击者服务器地址
        //header(sprintf('Location:%s', 'http://127.0.0.1'));
        exit('' . $hoss['ipccts'] . '，稍候' . $seconds . '秒后再访问！');
    }
} else {
    $_SESSION['refresh_times'] = 0;
    $_SESSION['last_time'] = $cur_time;
}
/**
 * 定义值
 * @param  [type] $s 初始值
 * @param  string $v 定义
 * @return [type]    [description]
 */
function dingyi($s, $v = '1')
{
    if (empty($s)) {
        return $v;
    } else {
        return $s;
    }
}
/**
 * 图片检查是否是木马
 * @param  [type] $img [description]
 * @return [type]      [description]
 */
function checkHex($img)
{
    $status = 0;
    //文件没问题
    if (file_exists($img)) {
        $resource = fopen($img, 'rb');
        $fileSize = filesize($img);
        fseek($resource, 0);
        if ($fileSize > 512) {
            // 取头和尾
            $hexCode = bin2hex(fread($resource, 512));
            fseek($resource, $fileSize - 512);
            $hexCode .= bin2hex(fread($resource, 512));
        } else {
            // 取全部
            $hexCode = bin2hex(fread($resource, $fileSize));
        }
        fclose($resource);
        /* 匹配16进制中的 <% ( ) %> */
        /* 匹配16进制中的 <? ( ) ?> */
        /* 匹配16进制中的 <script | /script> 大小写亦可 */
        if (preg_match("/(3c25.*?28.*?29.*?253e)|(3c3f.*?28.*?29.*?3f3e)|(3C534352495054)|(2F5343524950543E)|(3C736372697074)|(2F7363726970743E)/is", $hexCode)) {
            $status = 5;
            //文件有毒
        }
    } else {
        $status = -1;
        //文件没有上传
    }
    return $status;
}
//万能输出数据不带翻页随机推荐
function fun_sj($set, $open, $x)
{
    $wanneng = Db::name($set)->where($open)->orderRaw('rand()')->limit($x)->select();
    return $wanneng;
}
/*
搜索
fun_so(表, 条件, 关键词,显示数量)
*/
function fun_so($set, $open, $ks, $x, $y, $cate='category')
{
    $wanneng = Db::name($set)->alias('f')->join(''.$cate.' c', 'c.id=f.tid')->field('f.*,c.id as cid')->order('f.id desc')->where($open)->where('f.title|f.lianjie|f.keywords|f.content', 'like', '%' . $ks . '%')->paginate($x, $y, $config = ['query' => array('ks' => $ks)]);
    return $wanneng;
}
//转换数组输出
function exps($ks)
{
    $a = explode(",", $ks);
    return $a;
}
/**
 * 任意获取页面数据
 * @param [type] $url  获取的链接
 * @param [type] $id   选择器 class 和id的容器
 * @param string $html 获取页面的类型 html是包含标签 text是纯文本 href是链接 src是图片地址
 */
function Query($url, $id, $html = 'html')
{
    $rules = array('html' => array($id, $html));
    $data = QueryList::Query($url, $rules)->data;
    
    return $data[0]['html'];
}
function cut_c($content, $cut = '200')
{
    $content = preg_replace('/\\"/i', '', str_replace(PHP_EOL, '', cut(strip_tags($content), $cut)));
    $content = preg_replace("/\\'/i", "", str_replace(PHP_EOL, "", cut(strip_tags($content), $cut)));
    return $content;
}
 