You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1449 lines
42 KiB
PHTML

5 years ago
<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
// OneThink常量定义
const ONETHINK_VERSION = '3.0版本';
const ONETHINK_ADDON_PATH = './Addons/';
/**
* 系统公共库文件
* 主要定义系统公共函数库
*/
/**
* 检测用户是否登录
* @return integer 0-未登录大于0-当前登录用户ID
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function is_login()
{
5 years ago
// 判断是否过期 过期时间一个小时
$expire = 3600;
5 years ago
$user = session('user_auth');
if (empty($user)) {
return 0;
} else {
5 years ago
if (time() - intval(session('user_auth_expire')) > $expire) { // 过期正则直接去除掉session 并且返回false
session('user_auth', null);
session('user_auth_sign', null);
return 0;
} else {
session('user_auth_expire', time() + $expire);
}
5 years ago
return session('user_auth_sign') == data_auth_sign($user) ? $user['uid'] : 0;
}
}
/**
* 检测当前用户是否为管理员
* @return boolean true-管理员false-非管理员
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function is_administrator($uid = null)
{
$uid = is_null($uid) ? is_login() : $uid;
return $uid && (intval($uid) === C('USER_ADMINISTRATOR'));
}
/**
* 字符串转换为数组,主要用于把分隔符调整到第二个参数
* @param string $str 要分割的字符串
* @param string $glue 分割符
* @return array
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function str2arr($str, $glue = ',')
{
return explode($glue, $str);
}
/**
* 数组转换为字符串,主要用于把分隔符调整到第二个参数
* @param array $arr 要连接的数组
* @param string $glue 分割符
* @return string
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function arr2str($arr = [], $glue = ',')
5 years ago
{
return implode($glue, $arr);
}
/**
* 字符串截取,支持中文和其他编码
* @static
* @access public
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
* @return string
*/
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
if (function_exists("mb_substr"))
$slice = mb_substr($str, $start, $length, $charset);
elseif (function_exists('iconv_substr')) {
$slice = iconv_substr($str, $start, $length, $charset);
if (false === $slice) {
$slice = '';
}
} else {
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
}
return $suffix ? $slice . '...' : $slice;
}
//如果字符长度没有超过限制 则不加上。。。
function msubstr2($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
$slice = mb_substr($str, $start, $length, $charset);
if (mb_strlen($str, $charset) > $length) {
return $suffix ? $slice . '...' : $slice;
} else {
return $slice;
}
}
//如果字符长度没有超过限制 则不加上。。。
function msubstr3($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
$slice = mb_substr($str, $start, $length, $charset);
if (mb_strlen($str) > $length) {
return $suffix ? $slice . '...' : $slice;
} else {
return $slice;
}
}
/**
* 系统加密方法
* @param string $data 要加密的字符串
* @param string $key 加密密钥
* @param int $expire 过期时间 单位 秒
* @return string
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function think_encrypt($data, $key = '', $expire = 0)
{
$key = md5(empty($key) ? C('DATA_AUTH_KEY') : $key);
$data = base64_encode($data);
$x = 0;
$len = strlen($data);
$l = strlen($key);
$char = '';
for ($i = 0; $i < $len; $i++) {
if ($x == $l) $x = 0;
$char .= substr($key, $x, 1);
$x++;
}
$str = sprintf('%010d', $expire ? $expire + time() : 0);
for ($i = 0; $i < $len; $i++) {
$str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256);
}
return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode($str));
}
/**
* 系统解密方法
* @param string $data 要解密的字符串 必须是think_encrypt方法加密的字符串
* @param string $key 加密密钥
* @return string
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function think_decrypt($data, $key = '')
{
$key = md5(empty($key) ? C('DATA_AUTH_KEY') : $key);
$data = str_replace(array('-', '_'), array('+', '/'), $data);
$mod4 = strlen($data) % 4;
if ($mod4) {
$data .= substr('====', $mod4);
}
$data = base64_decode($data);
$expire = substr($data, 0, 10);
$data = substr($data, 10);
if ($expire > 0 && $expire < time()) {
return '';
}
$x = 0;
$len = strlen($data);
$l = strlen($key);
$char = $str = '';
for ($i = 0; $i < $len; $i++) {
if ($x == $l) $x = 0;
$char .= substr($key, $x, 1);
$x++;
}
for ($i = 0; $i < $len; $i++) {
if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
} else {
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
}
}
return base64_decode($str);
}
/**
* 数据签名认证
* @param array $data 被认证的数据
* @return string 签名
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function data_auth_sign($data)
{
//数据类型检测
if (!is_array($data)) {
$data = (array)$data;
}
ksort($data); //排序
$code = http_build_query($data); //url编码并生成query字符串
$sign = sha1($code); //生成签名
return $sign;
}
/**
* 对查询结果集进行排序
* @access public
* @param array $list 查询结果
* @param string $field 排序的字段名
* @param array $sortby 排序类型
* asc正向排序 desc逆向排序 nat自然排序
* @return array
*/
function list_sort_by($list, $field, $sortby = 'asc')
{
if (is_array($list)) {
$refer = $resultSet = array();
foreach ($list as $i => $data)
$refer[$i] = &$data[$field];
switch ($sortby) {
case 'asc': // 正向排序
asort($refer);
break;
case 'desc':// 逆向排序
arsort($refer);
break;
case 'nat': // 自然排序
natcasesort($refer);
break;
}
foreach ($refer as $key => $val)
$resultSet[] = &$list[$key];
return $resultSet;
}
return false;
}
/**
* 把返回的数据集转换成Tree
* @param array $list 要转换的数据集
* @param string $pid parent标记字段
* @param string $level level标记字段
* @return array
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
{
// 创建Tree
$tree = array();
if (is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
} else {
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
/**
* 将list_to_tree的树还原成列表
* @param array $tree 原来的树
* @param string $child 孩子节点的键
* @param string $order 排序显示的键,一般是主键 升序排列
* @param array $list 过渡用的中间数组,
* @return array 返回排过序的列表数组
* @author yangweijie <yangweijiester@gmail.com>
*/
function tree_to_list($tree, $child = '_child', $order = 'id', &$list = array())
{
if (is_array($tree)) {
foreach ($tree as $key => $value) {
$reffer = $value;
if (isset($reffer[$child])) {
unset($reffer[$child]);
tree_to_list($value[$child], $child, $order, $list);
}
$list[] = $reffer;
}
$list = list_sort_by($list, $order, $sortby = 'asc');
}
return $list;
}
/**
* 格式化字节大小
* @param number $size 字节数
* @param string $delimiter 数字和单位分隔符
* @return string 格式化后的带单位的大小
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function format_bytes($size, $delimiter = '')
{
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
return round($size, 2) . $delimiter . $units[$i];
}
/**
* 设置跳转页面URL
* 使用函数再次封装方便以后选择不同的存储方式目前使用cookie存储
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function set_redirect_url($url)
{
cookie('redirect_url', $url);
}
/**
* 获取跳转页面URL
* @return string 跳转页URL
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function get_redirect_url()
{
$url = cookie('redirect_url');
return empty($url) ? __APP__ : $url;
}
/**
* 处理插件钩子
* @param string $hook 钩子名称
* @param mixed $params 传入参数
* @return void
*/
function hook($hook, $params = array())
{
\Think\Hook::listen($hook, $params);
}
/**
* 获取插件类的类名
* @param strng $name 插件名
*/
function get_addon_class($name)
{
$class = "Addons\\{$name}\\{$name}Addon";
return $class;
}
/**
* 获取插件类的配置文件数组
* @param string $name 插件名
*/
function get_addon_config($name)
{
$class = get_addon_class($name);
if (class_exists($class)) {
$addon = new $class();
return $addon->getConfig();
} else {
return array();
}
}
/**
* 插件显示内容里生成访问插件的url
* @param string $url url
* @param array $param 参数
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function addons_url($url, $param = array())
{
$url = parse_url($url);
$case = C('URL_CASE_INSENSITIVE');
$addons = $case ? parse_name($url['scheme']) : $url['scheme'];
$controller = $case ? parse_name($url['host']) : $url['host'];
$action = trim($case ? strtolower($url['path']) : $url['path'], '/');
/* 解析URL带的参数 */
if (isset($url['query'])) {
parse_str($url['query'], $query);
$param = array_merge($query, $param);
}
/* 基础参数 */
$params = array(
'_addons' => $addons,
'_controller' => $controller,
'_action' => $action,
);
$params = array_merge($params, $param); //添加额外参数
return U('Addons/execute', $params);
}
/**
* 时间戳格式化
* @param int $time
* @return string 完整的时间显示
* @author huajie <banhuajie@163.com>
*/
function time_format($time = NULL, $format = 'Y-m-d H:i')
{
$time = $time === NULL ? NOW_TIME : intval($time);
return date($format, $time);
}
/**
* 时间戳格式化
* @param int $time
* @return string 完整的时间显示
* @author huajie <banhuajie@163.com>
*/
function time_format0($time = NULL, $format = 'Y-m-d H:i')
{
$time = $time === NULL ? NOW_TIME : intval($time);
if ($time) {
return date($format, $time);
} else {
return "暂无登录";
}
}
/**
* 根据用户ID获取用户名
* @param integer $uid 用户ID
* @return string 用户名
*/
function get_username($uid = 0)
{
static $list;
if (!($uid && is_numeric($uid))) { //获取当前登录用户名
return session('user_auth.username');
}
/* 获取缓存数据 */
if (empty($list)) {
$list = S('sys_active_user_list');
}
/* 查找用户信息 */
$key = "u{$uid}";
if (isset($list[$key])) { //已缓存,直接使用
$name = $list[$key];
} else { //调用接口获取用户信息
$User = new User\Api\UserApi();
$info = $User->info($uid);
if ($info && isset($info[1])) {
$name = $list[$key] = $info[1];
/* 缓存用户 */
$count = count($list);
$max = C('USER_MAX_CACHE');
while ($count-- > $max) {
array_shift($list);
}
S('sys_active_user_list', $list);
} else {
$name = '';
}
}
return $name;
}
/**
* 根据用户ID获取用户昵称
* @param integer $uid 用户ID
* @return string 用户昵称
*/
function get_nickname($uid = 0)
{
static $list;
if (!($uid && is_numeric($uid))) { //获取当前登录用户名
return session('user_auth.username');
}
/* 获取缓存数据 */
if (empty($list)) {
$list = S('sys_user_nickname_list');
}
/* 查找用户信息 */
$key = "u{$uid}";
if (isset($list[$key])) { //已缓存,直接使用
$name = $list[$key];
} else { //调用接口获取用户信息
$info = M('Member')->field('nickname')->find($uid);
if ($info !== false && $info['nickname']) {
$nickname = $info['nickname'];
$name = $list[$key] = $nickname;
/* 缓存用户 */
$count = count($list);
$max = C('USER_MAX_CACHE');
while ($count-- > $max) {
array_shift($list);
}
S('sys_user_nickname_list', $list);
} else {
$name = '';
}
}
return $name;
}
/**
* 获取分类信息并缓存分类
* @param integer $id 分类ID
* @param string $field 要获取的字段名
* @return string 分类信息
*/
function get_category($id, $field = null)
{
static $list;
/* 非法分类ID */
if (empty($id) || !is_numeric($id)) {
return '';
}
/* 读取缓存数据 */
if (empty($list)) {
$list = S('sys_category_list');
}
/* 获取分类名称 */
if (!isset($list[$id])) {
$cate = M('Category')->find($id);
if (!$cate || 1 != $cate['status']) { //不存在分类,或分类被禁用
return '';
}
$list[$id] = $cate;
S('sys_category_list', $list); //更新缓存
}
return is_null($field) ? $list[$id] : $list[$id][$field];
}
/* 根据ID获取分类标识 */
function get_category_name($id)
{
return get_category($id, 'name');
}
/* 根据ID获取分类名称 */
function get_category_title($id)
{
return get_category($id, 'title');
}
/**
* 获取顶级模型信息
*/
function get_top_model($model_id = null)
{
$map = array('status' => 1, 'extend' => 0);
if (!is_null($model_id)) {
$map['id'] = array('neq', $model_id);
}
$model = M('Model')->where($map)->field(true)->select();
foreach ($model as $value) {
$list[$value['id']] = $value;
}
return $list;
}
/**
* 获取文档模型信息
* @param integer $id 模型ID
* @param string $field 模型字段
* @return array
*/
function get_document_model($id = null, $field = null)
{
static $list;
/* 非法分类ID */
if (!(is_numeric($id) || is_null($id))) {
return '';
}
/* 读取缓存数据 */
if (empty($list)) {
$list = S('DOCUMENT_MODEL_LIST');
}
/* 获取模型名称 */
if (empty($list)) {
$map = array('status' => 1, 'extend' => 1);
$model = M('Model')->where($map)->field(true)->select();
foreach ($model as $value) {
$list[$value['id']] = $value;
}
S('DOCUMENT_MODEL_LIST', $list); //更新缓存
}
/* 根据条件返回数据 */
if (is_null($id)) {
return $list;
} elseif (is_null($field)) {
return $list[$id];
} else {
return $list[$id][$field];
}
}
/**
* 解析UBB数据
* @param string $data UBB字符串
* @return string 解析为HTML的数据
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
function ubb($data)
{
//TODO: 待完善,目前返回原始数据
return $data;
}
/**
* 记录行为日志,并执行该行为的规则
* @param string $action 行为标识
* @param string $model 触发行为的模型名
* @param int $record_id 触发行为的记录id
* @param int $user_id 执行行为的用户id
* @return boolean
* @author huajie <banhuajie@163.com>
*/
function action_log($action = null, $model = null, $record_id = null, $user_id = null)
{
//参数检查
if (empty($action) || empty($model) || empty($record_id)) {
return '参数不能为空';
}
if (empty($user_id)) {
$user_id = is_login();
}
//查询行为,判断是否执行
$action_info = M('Action')->getByName($action);
if ($action_info['status'] != 1) {
return '该行为被禁用或删除';
}
//插入行为日志
$data['action_id'] = $action_info['id'];
$data['user_id'] = $user_id;
$data['action_ip'] = ip2long(get_client_ip());
$data['model'] = $model;
$data['record_id'] = $record_id;
$data['create_time'] = NOW_TIME;
//解析日志规则,生成日志备注
if (!empty($action_info['log'])) {
if (preg_match_all('/\[(\S+?)\]/', $action_info['log'], $match)) {
$log['user'] = $user_id;
$log['record'] = $record_id;
$log['model'] = $model;
$log['time'] = NOW_TIME;
$log['data'] = array('user' => $user_id, 'model' => $model, 'record' => $record_id, 'time' => NOW_TIME);
foreach ($match[1] as $value) {
$param = explode('|', $value);
if (isset($param[1])) {
$replace[] = call_user_func($param[1], $log[$param[0]]);
} else {
$replace[] = $log[$param[0]];
}
}
$data['remark'] = str_replace($match[0], $replace, $action_info['log']);
} else {
$data['remark'] = $action_info['log'];
}
} else {
//未定义日志规则记录操作url
$data['remark'] = '操作url' . $_SERVER['REQUEST_URI'];
}
M('ActionLog')->add($data);
if (!empty($action_info['rule'])) {
//解析行为
$rules = parse_action($action, $user_id);
//执行行为
$res = execute_action($rules, $action_info['id'], $user_id);
}
}
/**
* 解析行为规则
* 规则定义 table:$table|field:$field|condition:$condition|rule:$rule[|cycle:$cycle|max:$max][;......]
* 规则字段解释table->要操作的数据表,不需要加表前缀;
* field->要操作的字段;
* condition->操作的条件,目前支持字符串,默认变量{$self}为执行行为的用户
* rule->对字段进行的具体操作目前支持四则混合运算1+score*2/2-3
* cycle->执行周期,单位(小时),表示$cycle小时内最多执行$max次
* max->单个周期内的最大执行次数($cycle和$max必须同时定义否则无效
* 单个行为后可加 连接其他规则
* @param string $action 行为id或者name
* @param int $self 替换规则里的变量为执行用户的id
* @return boolean|array: false解析出错 成功返回规则数组
* @author huajie <banhuajie@163.com>
*/
function parse_action($action = null, $self)
{
if (empty($action)) {
return false;
}
//参数支持id或者name
if (is_numeric($action)) {
$map = array('id' => $action);
} else {
$map = array('name' => $action);
}
//查询行为信息
$info = M('Action')->where($map)->find();
if (!$info || $info['status'] != 1) {
return false;
}
//解析规则:table:$table|field:$field|condition:$condition|rule:$rule[|cycle:$cycle|max:$max][;......]
$rules = $info['rule'];
$rules = str_replace('{$self}', $self, $rules);
$rules = explode(';', $rules);
$return = array();
foreach ($rules as $key => &$rule) {
$rule = explode('|', $rule);
foreach ($rule as $k => $fields) {
$field = empty($fields) ? array() : explode(':', $fields);
if (!empty($field)) {
$return[$key][$field[0]] = $field[1];
}
}
//cycle(检查周期)和max(周期内最大执行次数)必须同时存在,否则去掉这两个条件
if (!array_key_exists('cycle', $return[$key]) || !array_key_exists('max', $return[$key])) {
unset($return[$key]['cycle'], $return[$key]['max']);
}
}
return $return;
}
/**
* 执行行为
* @param array $rules 解析后的规则数组
* @param int $action_id 行为id
* @param array $user_id 执行的用户id
* @return boolean false 失败 true 成功
* @author huajie <banhuajie@163.com>
*/
function execute_action($rules = false, $action_id = null, $user_id = null)
{
if (!$rules || empty($action_id) || empty($user_id)) {
return false;
}
$return = true;
foreach ($rules as $rule) {
//检查执行周期
$map = array('action_id' => $action_id, 'user_id' => $user_id);
$map['create_time'] = array('gt', NOW_TIME - intval($rule['cycle']) * 3600);
$exec_count = M('ActionLog')->where($map)->count();
if ($exec_count > $rule['max']) {
continue;
}
//执行数据库操作
$Model = M(ucfirst($rule['table']));
$field = $rule['field'];
$res = $Model->where($rule['condition'])->setField($field, array('exp', $rule['rule']));
if (!$res) {
$return = false;
}
}
return $return;
}
//基于数组创建目录和文件
function create_dir_or_files($files)
{
foreach ($files as $key => $value) {
if (substr($value, -1) == '/') {
mkdir($value);
} else {
@file_put_contents($value, '');
}
}
}
if (!function_exists('array_column')) {
function array_column(array $input, $columnKey, $indexKey = null)
{
$result = array();
if (null === $indexKey) {
if (null === $columnKey) {
$result = array_values($input);
} else {
foreach ($input as $row) {
$result[] = $row[$columnKey];
}
}
} else {
if (null === $columnKey) {
foreach ($input as $row) {
$result[$row[$indexKey]] = $row;
}
} else {
foreach ($input as $row) {
$result[$row[$indexKey]] = $row[$columnKey];
}
}
}
return $result;
}
}
/**
* 获取表名(不含表前缀)
* @param string $model_id
* @return string 表名
* @author huajie <banhuajie@163.com>
*/
function get_table_name($model_id = null)
{
if (empty($model_id)) {
return false;
}
$Model = M('Model');
$name = '';
$info = $Model->getById($model_id);
if ($info['extend'] != 0) {
$name = $Model->getFieldById($info['extend'], 'name') . '_';
}
$name .= $info['name'];
return $name;
}
/**
* 获取属性信息并缓存
* @param integer $id 属性ID
* @param string $field 要获取的字段名
* @return string 属性信息
*/
function get_model_attribute($model_id, $group = true, $fields = true)
{
static $list;
/* 非法ID */
if (empty($model_id) || !is_numeric($model_id)) {
return '';
}
/* 获取属性 */
if (!isset($list[$model_id])) {
$map = array('model_id' => $model_id);
$extend = M('Model')->getFieldById($model_id, 'extend');
if ($extend) {
$map = array('model_id' => array("in", array($model_id, $extend)));
}
$info = M('Attribute')->where($map)->field($fields)->select();
$list[$model_id] = $info;
}
$attr = array();
if ($group) {
foreach ($list[$model_id] as $value) {
$attr[$value['id']] = $value;
}
$model = M("Model")->field("field_sort,attribute_list,attribute_alias")->find($model_id);
$attribute = explode(",", $model['attribute_list']);
if (empty($model['field_sort'])) { //未排序
$group = array(1 => array_merge($attr));
} else {
$group = json_decode($model['field_sort'], true);
$keys = array_keys($group);
foreach ($group as &$value) {
foreach ($value as $key => $val) {
$value[$key] = $attr[$val];
unset($attr[$val]);
}
}
if (!empty($attr)) {
foreach ($attr as $key => $val) {
if (!in_array($val['id'], $attribute)) {
unset($attr[$key]);
}
}
$group[$keys[0]] = array_merge($group[$keys[0]], $attr);
}
}
if (!empty($model['attribute_alias'])) {
$alias = preg_split('/[;\r\n]+/s', $model['attribute_alias']);
$fields = array();
foreach ($alias as &$value) {
$val = explode(':', $value);
$fields[$val[0]] = $val[1];
}
foreach ($group as &$value) {
foreach ($value as $key => $val) {
if (!empty($fields[$val['name']])) {
$value[$key]['title'] = $fields[$val['name']];
}
}
}
}
$attr = $group;
} else {
foreach ($list[$model_id] as $value) {
$attr[$value['name']] = $value;
}
}
return $attr;
}
/**
* 调用系统的API接口方法静态方法
* api('User/getName','id=5'); 调用公共模块的User接口的getName方法
* api('Admin/User/getName','id=5'); 调用Admin模块的User接口
* @param string $name 格式 [模块名]/接口名/方法名
* @param array|string $vars 参数
*/
function api($name, $vars = array())
{
$array = explode('/', $name);
$method = array_pop($array);
$classname = array_pop($array);
$module = $array ? array_pop($array) : 'Common';
$callback = $module . '\\Api\\' . $classname . 'Api::' . $method;
if (is_string($vars)) {
parse_str($vars, $vars);
}
return call_user_func_array($callback, $vars);
}
/**
* 根据条件字段获取指定表的数据
* @param mixed $value 条件,可用常量或者数组
* @param string $condition 条件字段
* @param string $field 需要返回的字段,不传则返回整个数据
* @param string $table 需要查询的表
* @author huajie <banhuajie@163.com>
*/
function get_table_field($value = null, $condition = 'id', $field = null, $table = null)
{
if (empty($value) || empty($table)) {
return false;
}
//拼接参数
$map[$condition] = $value;
$info = M(ucfirst($table))->where($map);
if (empty($field)) {
$info = $info->field(true)->find();
} else {
$info = $info->getField($field);
}
return $info;
}
/**
* 获取链接信息
* @param int $link_id
* @param string $field
* @return 完整的链接信息或者某一字段
* @author huajie <banhuajie@163.com>
*/
function get_link($link_id = null, $field = 'url')
{
$link = '';
if (empty($link_id)) {
return $link;
}
$link = M('Url')->getById($link_id);
if (empty($field)) {
return $link;
} else {
return $link[$field];
}
}
/**
* 获取文档封面图片
* @param int $cover_id
* @param string $field
* @return 完整的数据 或者 指定的$field字段值
* @author huajie <banhuajie@163.com>
*/
function get_cover($cover_id, $field = null, $root = 1, $flag = true)
{
if (empty($cover_id)) {
return "";
}
$picture = M('Picture')->where(array('status' => 1))->getById($cover_id);
if (get_tool_status("oss_storage") == 1) {
if (!empty($picture['oss_url'])) {
5 years ago
return str_replace('http:', 'https:', $picture['oss_url']);
5 years ago
} else {
5 years ago
return 'https://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
5 years ago
}
} elseif (get_tool_status("qiniu_storage") == 1) {
if (!empty($picture['url'])) {
return $picture['url'];
} else {
5 years ago
return '//' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
5 years ago
}
} elseif (get_tool_status("cos_storage") == 1) {
if (!empty($picture['url'])) {
return $picture['url'];
} else {
5 years ago
return '//' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
5 years ago
}
} elseif (get_tool_status("bos_storage") == 1) {
if (!empty($picture['bos_url'])) {
return $picture['bos_url'];
} else {
5 years ago
return '//' . $_SERVER['HTTP_HOST'] . __ROOT__ . $picture['path'];
5 years ago
}
} else {
if ($field == 'path') {
if (!empty($picture['url']) && get_tool_status('qiniu_storage') == 1) {
$picture['path'] = $picture['url'];
} else {
if ($picture['water'] && MODULE_NAME != 'Admin' && $flag && (C('watermark.status') == 1)) {
$rp = realpath(dirname(__FILE__) . '/../../../');
if (is_file($rp . $picture['water'])) {
$field = 'water';
}
} else {
if ($root == 1) {
$picture['path'] = __ROOT__ . $picture['path'];
}
}
}
}
return empty($field) ? $picture : $picture[$field];
}
}
function get_file($cover_id, $field = null)
{
if (empty($cover_id)) {
return "";
}
$file = M('File')->where(array('status' => 1))->getById($cover_id);
if ($file) {
$file['path'] = __ROOT__ . substr(C('DOWNLOAD_UPLOAD.rootPath'), 1) . '' . $file['savepath'] . '' . $file['savename'];
return empty($field) ? $file : $file[$field];
} else {
return '';
}
}
/**
* 检查$pos(推荐位的值)是否包含指定推荐位$contain
* @param number $pos 推荐位的值
* @param number $contain 指定推荐位
* @return boolean true 包含 false 不包含
* @author huajie <banhuajie@163.com>
*/
function check_document_position($pos = 0, $contain = 0)
{
if (empty($pos) || empty($contain)) {
return false;
}
//将两个参数进行按位与运算不为0则表示$contain属于$pos
$res = $pos & $contain;
if ($res !== 0) {
return true;
} else {
return false;
}
}
/**
* 获取数据的所有子孙数据的id值
* @author 朱亚杰 <xcoolcc@gmail.com>
*/
function get_stemma($pids, Model &$model, $field = 'id')
{
$collection = array();
//非空判断
if (empty($pids)) {
return $collection;
}
if (is_array($pids)) {
$pids = trim(implode(',', $pids), ',');
}
$result = $model->field($field)->where(array('pid' => array('IN', (string)$pids)))->select();
$child_ids = array_column((array)$result, 'id');
while (!empty($child_ids)) {
$collection = array_merge($collection, $result);
$result = $model->field($field)->where(array('pid' => array('IN', $child_ids)))->select();
$child_ids = array_column((array)$result, 'id');
}
return $collection;
}
/**
* 验证分类是否允许发布内容
* @param integer $id 分类ID
* @return boolean true-允许发布内容false-不允许发布内容
*/
function check_category($id)
{
if (is_array($id)) {
$id['type'] = !empty($id['type']) ? $id['type'] : 2;
$type = get_category($id['category_id'], 'type');
$type = explode(",", $type);
return in_array($id['type'], $type);
} else {
$publish = get_category($id, 'allow_publish');
return $publish ? true : false;
}
}
/**
* 检测分类是否绑定了指定模型
* @param array $info 模型ID和分类ID数组
* @return boolean true-绑定了模型false-未绑定模型
*/
function check_category_model($info)
{
$cate = get_category($info['category_id']);
$array = explode(',', $info['pid'] ? $cate['model_sub'] : $cate['model']);
return in_array($info['model_id'], $array);
}
/**
* 系统非常规MD5加密方法
* @param string $str 要加密的字符串
* @return string
*/
function think_psw_md5($str, $key = 'ThinkUCenter')
{
return '' === $str ? '' : md5(sha1($str) . $key);
}
//对年龄的审核
function age_verify($cardno, $name)
{
$date = age($cardno, $name);
if ($date['resp']['code'] == 0 && $date > 0) {
$age = floor((time() - strtotime($date['data']['birthday'])) / (60 * 60 * 24 * 365));
if ($age > 17) {
return 1;
} else {
return 2;
}
} elseif ($date['resp']['code'] != 0 && $date > 0) {
return 0;
} else {
return $date;
}
}
//根据配置向接口发送身份证号和姓名进行验证
function age($cardno, $name)
{
$host = "https://idcardcert.market.alicloudapi.com";
$path = "/idCardCert";
5 years ago
$method = "GET";
$appcode = C('tool_age.appcode');
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$querys = "idCard=" . $cardno . "&name=" . $name;
5 years ago
$bodys = "";
$url = $host . $path . "?" . $querys;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
if (1 == strpos("$" . $host, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$output = curl_exec($curl);
if (empty($output)) {
return -1;//用完
}
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
$headersize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($output, 0, $headersize);
$body = substr($output, $headersize);
curl_close($curl);
return json_decode($body, true);
} else {
return -2;//失败
}
}
/*短信发送验证限制条件 */
function checksendcode($phone, $limit)
{
$number = M('Short_message')->where(array('pid' => 0, 'create_ip' => get_client_ip(), 'send_status' => '000000', 'send_time' => array(array('egt', strtotime('today')), array('elt', strtotime('tomorrow')))))->count();
if (!empty($limit) && $number >= $limit) {
$return['status'] = '0';
$return['msg'] = '每天发送数量不能超过' . $limit . '条';
echo json_encode($return);
exit;
}
$request_time = time();
$map = array('phone' => $phone);
$map['create_time'] = array(array('egt', ($request_time - 60)), array('elt', $request_time));
$number = $time = M('Short_message')->where($map)->count();
if ($number > 0) {
echo json_encode(array('status' => 0, 'msg' => '请一分钟后再次尝试'));
exit;
}
}
//验证短信
function sdkchecksendcode($phone, $limit, $type = 1)
{
$number = M('Short_message')->where(array('pid' => 0, 'create_ip' => get_client_ip(), 'send_status' => '000000', 'send_time' => array(array('egt', strtotime('today')), array('elt', strtotime('tomorrow')))))->count();
if (!empty($limit) && $number >= $limit) {
$return['status'] = '0';
$return['msg'] = '每天发送数量不能超过' . $limit . '条';
echo base64_encode(json_encode($return));
exit;
}
$request_time = time();
$map = array('phone' => $phone);
$map['create_time'] = array(array('egt', ($request_time - 60)), array('elt', $request_time));
$number = $time = M('Short_message')->where($map)->count();
if ($number > 0) {
echo base64_encode(json_encode(array('status' => $type == 1 ? 0 : 1024, $type == 1 ? "msg" : "return_msg" => '请一分钟后再次尝试')));
exit;
}
}
//验证短信
function sdk2checksendcode($phone, $limit)
{
$number = M('Short_message')->where(array('pid' => 0, 'create_ip' => get_client_ip(), 'send_status' => '000000', 'send_time' => array(array('egt', strtotime('today')), array('elt', strtotime('tomorrow')))))->count();
if (!empty($limit) && $number >= $limit) {
$return['status'] = '0';
$return['return_msg'] = '每天发送数量不能超过' . $limit . '条';
echo base64_encode(json_encode($return));
exit;
}
$request_time = time();
$map = array('phone' => $phone);
$map['create_time'] = array(array('egt', ($request_time - 60)), array('elt', $request_time));
$number = $time = M('Short_message')->where($map)->count();
if ($number > 0) {
echo base64_encode(json_encode(array('status' => 0, 'return_msg' => '请一分钟后再次尝试')));
exit;
}
}
function appchecksendcode($phone, $limit, $code = 1)
{
$number = M('Short_message')->where(array('pid' => 0, 'create_ip' => get_client_ip(), 'send_status' => '000000', 'send_time' => array(array('egt', strtotime('today')), array('elt', strtotime('tomorrow')))))->count();
if (!empty($limit) && $number >= $limit) {
$msg = array(
"status" => -1,
"return_code" => "每天发送数量不能超过" . $limit . "条",
"data" => []
);
echo json_encode($msg);
exit;
}
$request_time = time();
$map = array('phone' => $phone);
$map['create_time'] = array(array('egt', ($request_time - 60)), array('elt', $request_time));
$number = $time = M('Short_message')->where($map)->count();
if ($number > 0) {
$msg = array(
"status" => -1,
"return_code" => "请一分钟后再次尝试",
"data" => []
);
if ($code == 2) {
$msg['code'] = 1024;
$msg['msg'] = '验证码发送过于频繁,请稍后再试';
unset($msg['status']);
unset($msg['return_code']);
}
echo json_encode($msg);
exit;
}
}
function footer_news($type)
{
if (empty($type)) {
return 0;
}
$name = 'media_' . $type;
$news = M("Document")->field("d.id")->table("__DOCUMENT__ as d")
->join("__CATEGORY__ as c on(c.id=d.category_id and c.name='$name')", 'right')
->where("d.status>0 and d.display=1 and c.title=d.title")->find();
if ($news) {
return 1;
} else {
return 0;
}
}
/*
获取sdk微信参数
@author cb
*/
function get_game_wxlogin_param($game_id)
{
$map['game_id'] = ['in', [$game_id, 0]];
$map['type'] = 2;
$map['status'] = 1;
$res = M('param', 'tab_')->field('wx_appid,appsecret')->where($map)->find();
if (empty($res)) {
return '';
} else {
return $res;
}
}
function pp($val = '', $isdie = true)
{
var_dump($val);
if ($isdie) die();
5 years ago
}
function curl_post($url = '', $post_data = array())
{
if (empty($url) || empty($post_data)) {
return false;
}
$requestString = http_build_query($post_data);
$postUrl = $url;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL, $postUrl); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestString); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$data = curl_exec($curl);//运行curl
curl_close($curl);
return $data;
5 years ago
}
if (!function_exists('dd')) {
function dd($var)
{
if (func_num_args() > 1) {
$var = func_get_args();
}
$debug = debug_backtrace();
echo '<pre class="yun-debug" style="background:#EAEAEA;padding:10px;">';
echo '<div class="file" style="color:#f00;">', $debug[0]['file'], ' ', $debug[0]['line'], PHP_EOL, '</div>';
if (is_object($var) || is_array($var)) {
$mode = 0;
} else {
$mode = 1;
}
switch ($mode) {
case 0:
print_r($var);
break;
case 1:
var_dump($var);
break;
default:
print_r($var);
}
echo '</pre>';
exit;
}
}
//获取推广员提现状态
function promoteWithdrawStatus($status = null)
{
$statusData = [
-1 => '审核未通过',
0 => '待审核',
1 => '汇款中',
2 => '已汇款',
];
if (isset($statusData[$status])) {
return $statusData[$status];
}
return $statusData;
}