2023-01-29 10:26:52 +08:00

350 lines
9.6 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | 小说系统 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
namespace app\common\controller;
use think\Request;
class Base extends \think\Controller {
protected $url;
protected $request;
protected $module;
protected $controller;
protected $action;
public function _initialize() {
if (!is_file(APP_PATH . 'database.php') || !is_file(APP_PATH . 'install.lock')) {
return $this->redirect('install/index/index');
}
/* 读取数据库中的配置 */
$config = cache('db_config_data');
if (!$config) {
$config = model('Config')->lists();
cache('db_config_data', $config);
}
config($config);
//获取request信息
$this->requestInfo();
$request = Request::instance();
$url = $request->controller().'/'.$request->action();
switch ($url){
case "Index/news":
$nv = "news";break;
case "Index/index":
$nv = "home";break;
case "Books/type":
$nv = "home";break;
case "Books/history":
$nv = "shujia";break;
case "Books/index":
$nv = "shujia";break;
default:
$nv=$url;
}
$this->assign('nav',$nv);
}
public function execute($mc = null, $op = '', $ac = null) {
$op = $op ? $op : $this->request->module();
if (\think\Config::get('url_case_insensitive')) {
$mc = ucfirst(parse_name($mc, 1));
$op = parse_name($op, 1);
}
if (!empty($mc) && !empty($op) && !empty($ac)) {
$ops = ucwords($op);
$class = "\\addons\\{$mc}\\controller\\{$ops}";
$addons = new $class;
$addons->$ac();
} else {
$this->error('没有指定插件名称,控制器或操作!');
}
}
/**
* 解析数据库语句函数
* @param string $sql sql语句 带默认前缀的
* @param string $tablepre 自己的前缀
* @return multitype:string 返回最终需要的sql语句
*/
public function sqlSplit($sql, $tablepre) {
if ($tablepre != "sent_") {
$sql = str_replace("sent_", $tablepre, $sql);
}
$sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=utf8", $sql);
if ($r_tablepre != $s_tablepre) {
$sql = str_replace($s_tablepre, $r_tablepre, $sql);
$sql = str_replace("\r", "\n", $sql);
$ret = array();
$num = 0;
$queriesarray = explode(";\n", trim($sql));
unset($sql);
foreach ($queriesarray as $query) {
$ret[$num] = '';
$queries = explode("\n", trim($query));
$queries = array_filter($queries);
foreach ($queries as $query) {
$str1 = substr($query, 0, 1);
if ($str1 != '#' && $str1 != '-') {
$ret[$num] .= $query;
}
}
$num++;
}
}
return $ret;
}
protected function setSeo($title = '', $keywords = '', $description = '') {
$host = explode('.',$_SERVER['HTTP_HOST']);
if($host[0]!="writes" && strpos($host[0],'t') !==false){
$wxmpid= split_host($host[0],2);
$nickname = '';//db('wxmp')->where('id',$wxmpid)->value('webname');
if($nickname){
$title = str_replace(config('web_site_title'),$nickname,$title);
}
}
$data = array(
'web_title' => $title,
'web_keywords' => $keywords,
'web_description' => $description,
);
$this->assign($data);
}
/**
* 验证码
* @param integer $id 验证码ID
* @author 郭平平 <molong@tensent.cn>
*/
public function verify($id = 1) {
$verify = new \org\Verify(array('length' => 4));
$verify->entry($id);
}
/**
* 检测验证码
* @param integer $id 验证码ID
* @return boolean 检测结果
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
public function checkVerify($code, $id = 1) {
if ($code) {
$verify = new \org\Verify();
$result = $verify->check($code, $id);
if (!$result) {
return $this->error("验证码错误!", "");
}
} else {
return $this->error("验证码为空!", "");
}
}
/**
* @title 后台设置title
* @description 设置后台页面的title
* @Author molong
* @DateTime 2017-06-21
* @param string $title 标题名称
*/
protected function setMeta($title = '') {
if(is_array($title)){
$meta = '';
foreach($title as $v){
if(array_key_exists('url',$v)){
$meta .="<a href='".$v['url']."'>".$v['title']."</a>>></li><li>";
}else{
$meta .=$v['title']."</li><li>";
}
}
$this->assign('meta_title', $meta);
}else{
$this->assign('meta_title', $title);
}
}
//request信息
protected function requestInfo() {
$this->param = $this->request->param();
defined('MODULE_NAME') or define('MODULE_NAME', $this->request->module());
defined('CONTROLLER_NAME') or define('CONTROLLER_NAME', $this->request->controller());
defined('ACTION_NAME') or define('ACTION_NAME', $this->request->action());
defined('IS_POST') or define('IS_POST', $this->request->isPost());
defined('IS_GET') or define('IS_GET', $this->request->isGet());
$this->url = strtolower($this->request->module() . '/' . $this->request->controller() . '/' . $this->request->action());
$this->assign('request', $this->request);
$this->assign('param', $this->param);
}
/**
* 获取单个参数的数组形式
*/
protected function getArrayParam($param) {
if (isset($this->param['id'])) {
return array_unique((array) $this->param[$param]);
} else {
return array();
}
}
/**
* 返回Json数据
* @param type $ResponseCode 响应码
* @param type $ResponseMsg 响应消息
* @param type $ResponseData 响应数据
*/
protected function ReturnJson($ResponseCode = 1,$ResponseMsg = '调用成功',$ResponseData = array(),$style=1){
if(!is_numeric($ResponseCode)) {
return false;
}
if($style){
$result = array(
'code'=>$ResponseCode,
'msg'=>$ResponseMsg,
'data'=>$ResponseData
);
}else{
$result = $ResponseData;
}
header("Content-type: application/json;charset=utf-8");
echo json_encode($result);
exit();
}
/**
* checkcode [检测验证码是否正确]
*
* @param type $username [desc]
*
* @return type
**/
protected function checkcode($username,$code){
$usercode = db('MemberCode')->where(['username'=>$username,'status' => 2])->order('id desc')->find();
if(!$usercode['code']){
$this->ReturnJson(0,"验证码不正确");
exit();
}
if($usercode['code'] != $code) {
$this->ReturnJson(0,"验证码不正确");
exit();
}
db('MemberCode')->where('id','=',$usercode['id'])->update(['status' => 1]);
return true;
}
/**
* 是否为手机访问
* @return boolean [description]
*/
public function isMobile() {//return true;
// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
return true;
}
// 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
if (isset($_SERVER['HTTP_VIA'])) {
// 找不到为flase,否则为true
return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
}
// 脑残法,判断手机发送的客户端标志,兼容性有待提高
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile');
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}
}
// 协议法,因为有可能不准确,放到最后判断
if (isset($_SERVER['HTTP_ACCEPT'])) {
// 如果只支持wml并且不支持html那一定是移动设备
// 如果支持wml和html但是wml在html之前则是移动设备
if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
return true;
}
}
return false;
}
public function is_wechat() {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
public function curl_get($url){
if(function_exists('curl_init') && function_exists('curl_exec')){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
}else{
for($i=0;$i<3;$i++){
$data = @file_get_contents($url);
if($data) break;
}
}
return json_decode($data,true);
}
/******产生随机数*****/
public function randomStr(){
$arr = array_merge(range(0,9),range('A','Z'));
$str = '';
$arr_len = count($arr);
for($i = 0;$i < 8;$i++){
$rand = mt_rand(0,$arr_len-1);
$str.=$arr[$rand];
}
return $str;
}
/**
* 分割中文字符串
* $str 字符串
* $count 个数
*/
public function mb_str_split($str, $count=3){
$leng = strlen($str);
$arr = array();
for ($i=0; $i < $leng; $i+=$count) {
$arr[] = mb_substr($str, $i, $count);
}
return $arr;
}
/*****章节列表防采集处理****/
public function reverse($reverse,$num=2) {
$data = array();
$sub_arr=array_chunk($reverse,$num);
foreach($sub_arr as $a){ //循环输出所有的数组
$b=array_reverse($a);//输出的数组进行倒序排列
foreach($b as $c){ //以3章为一个单元倒序循环输出所有章节
$data[] = $c;
}
}
return $data;
}
}