<?php<364666827@qq.com>,开发者QQ群:50304283use think\exception\HttpException;
use think\facade\Env;
include Env::get('app_path').'function.php';
if (!function_exists('hs_array_filter_callback')) {
function hs_array_filter_callback($val)
{
if ($val === '' || $val === 'NULL' || $val === null || $val === ' ') {
return false;
}
return true;
}
}
if (!function_exists('dblang')) {
function dblang($group = '') {
$lang = cookie($group.'_language');
if (empty($lang)) {
$lang = config('default_lang');
}
return (new app\system\model\SystemLanguage)->lists($lang);
}
}
if (!function_exists('get_domain')) {
function get_domain($http = true) {
$host = input('server.http_host');
$port = input('server.server_port');
if ($port != 80 && $port != 443 && strpos($host, ':') === false) {
$host .= ':'.input('server.server_port');
}
if ($http) {
if (input('server.https') && input('server.https') == 'on') {
return 'https://'.$host;
}
return 'http://'.$host;
}
return $host;
}
}
if (!function_exists('get_num')) {
function get_num($field = 'id') {
$_id = input('param.' . $field. '/d', 0);
if ($_id > 0) {
return $_id;
}
if (request()->isAjax()) {
json(['msg'=> '参数传递错误', 'code'=> 0]);
} else {
throw new HttpException(404, $field.'参数传递错误!');
}
exit;
}
}
if (!function_exists('is_email')) {
function is_email($str) {
return preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/", $str);
}
}
if (!function_exists('is_mobile')) {
function is_mobile($num) {
return preg_match("/^1(3|4|5|6|7|8|9)\d{9}$/", $num);
}
}
if (!function_exists('cur_url')) {
function cur_url() {
$pageURL = 'http';
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] === 'on') {
$pageURL .= "s";
}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
}
return $pageURL;
}
}
if (!function_exists('is_username')) {
function is_username($str) {
return preg_match("/^[\x80-\xffA-Za-z]{1,1}[\x80-\xff_A-Za-z0-9]{2,19}+$/", $str);
}
}
if (!function_exists('random')) {
function random($length = 16, $type = 1) {
$seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $type ? 10 : 35);
$seed = $type ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
if($type) {
$hash = '';
} else {
$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
$length--;
}
$max = strlen($seed) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $seed{mt_rand(0, $max)};
}
return $hash;
}
}
if (!function_exists('order_number')) {
function order_number() {
return date('YmdHis').random(5);
}
}
if (!function_exists('hide_str')) {
function hide_str($string, $bengin=0, $len = 4, $type = 0, $glue = "@") {
if (empty($string))
return false;
$array = array();
if ($type == 0 || $type == 1 || $type == 4) {
$strlen = $length = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string, 0, 1, "utf8");
$string = mb_substr($string, 1, $strlen, "utf8");
$strlen = mb_strlen($string);
}
}
if ($type == 0) {
for ($i = $bengin; $i < ($bengin + $len); $i++) {
if (isset($array[$i])) $array[$i] = "*";
}
$string = implode("", $array);
}else if ($type == 1) {
$array = array_reverse($array);
for ($i = $bengin; $i < ($bengin + $len); $i++) {
if (isset($array[$i])) $array[$i] = "*";
}
$string = implode("", array_reverse($array));
}else if ($type == 2) {
$array = explode($glue, $string);
if (isset($array[0])) {
$array[0] = hide_str($array[0], $bengin, $len, 1);
}
$string = implode($glue, $array);
} else if ($type == 3) {
$array = explode($glue, $string);
if (isset($array[1])) {
$array[1] = hide_str($array[1], $bengin, $len, 0);
}
$string = implode($glue, $array);
} else if ($type == 4) {
$left = $bengin;
$right = $len;
$tem = array();
for ($i = 0; $i < ($length - $right); $i++) {
if (isset($array[$i])) $tem[] = $i >= $left ? "" : $array[$i];
}
$tem[] = '*****';
$array = array_chunk(array_reverse($array), $right);
$array = array_reverse($array[0]);
for ($i = 0; $i < $right; $i++) {
if (isset($array[$i])) $tem[] = $array[$i];
}
$string = implode("", $tem);
}
return $string;
}
}
if (!function_exists('get_client_ip')) {
function get_client_ip($type = 0, $adv = false) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if($adv){
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown',$arr);
if(false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
}elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
$long = sprintf("%u",ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
}
if (!function_exists('parse_attr')) {
function parse_attr($value = '') {
if (is_array($value)) return $value;
$array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
if (strpos($value, ':')) {
$value = array();
foreach ($array as $val) {
list($k, $v) = explode(':', $val);
$value[$k] = $v;
}
} else {
$value = $array;
}
return $value;
}
}
if (!function_exists('xml2array')) {
function xml2array(&$xml, $isnormal = FALSE) {
$xml_parser = new hisi\Xml($isnormal);
$data = $xml_parser->parse($xml);
$xml_parser->destruct();
return $data;
}
}
if (!function_exists('array2xml')) {
function array2xml($arr, $ignore = true, $level = 1) {
$s = $level == 1 ? "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n<root>\r\n" : '';
$space = str_repeat("\t", $level);
foreach($arr as $k => $v) {
if(!is_array($v)) {
$s .= $space."<item id=\"$k\">".($ignore ? '<![CDATA[' : '').$v.($ignore ? ']]>' : '')."</item>\r\n";
} else {
$s .= $space."<item id=\"$k\">\r\n".array2xml($v, $ignore, $level + 1).$space."</item>\r\n";
}
}
$s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
return $level == 1 ? $s."</root>" : $s;
}
}
if (!function_exists('form_type')) {
function form_type($type = '') {
$arr = [];
$arr['input'] = '单行文本';
$arr['textarea'] = '多行文本';
$arr['array'] = '数组';
$arr['switch'] = '开关';
$arr['radio'] = '单选按钮';
$arr['checkbox'] = '多选框';
$arr['tags'] = '标签';
$arr['select'] = '下拉框';
$arr['hidden'] = '隐藏';
$arr['image'] = '图片';
$arr['file'] = '文件';
$arr['date'] = '日期';
$arr['datetime'] = '日期+时间';
$arr['time'] = '时间';
if (isset($arr[$type])) {
return $arr[$type];
}
return $arr;
}
}
if (!function_exists('json_indent')) {
function json_indent($json) {
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = ' ';
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
for ($i=0; $i<=$strLen; $i++) {
$char = substr($json, $i, 1);
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos --;
for ($j=0; $j<$pos; $j++) {
$result .= $indentStr;
}
}
$result .= $char;
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos ++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
return $result;
}
}
if (!function_exists('parse_sql')) {
function parse_sql($sql = '', $limit = 0, $prefix = []) {
$from = '';
$to = '';
if (!empty($prefix)) {
$to = current($prefix);
$from = current(array_flip($prefix));
}
if ($sql != '') {
$pure_sql = [];
$comment = false;
$sql = str_replace(["\r\n", "\r"], "\n", $sql);
$sql = explode("\n", trim($sql));
foreach ($sql as $key => $line) {
if ($line == '') {
continue;
}
if (preg_match("/^(#|--)/", $line)) {
continue;
}
if (preg_match("/^\/\*(.*?)\* continue;
}
if (substr($line, 0, 2) == '') {
$comment = false;
continue;
}
if ($comment) {
continue;
}
if ($from != '') {
$line = str_replace('`'.$from, '`'.$to, $line);
}
if ($line == 'BEGIN;' || $line =='COMMIT;') {
continue;
}
array_push($pure_sql, $line);
}
if ($limit == 1) {
return implode($pure_sql, "");
}
$pure_sql = implode($pure_sql, "\n");
$pure_sql = explode(";\n", $pure_sql);
return $pure_sql;
} else {
return $limit == 1 ? '' : [];
}
}
}
if (!function_exists('editor')) {
function editor($obj = [], $name = '', $url = '') {
$jsPath = '/static/js/editor/';
if (empty($name)) {
$name = config('sys.editor');
}
if (empty($url)) {
$url = url("system/annex/upload?full_path=yes&thumb=no&from=".$name);
}
switch (strtolower($name)) {
case 'ueditor':
$html = '<script src="'.$jsPath.'ueditor/ueditor.config.js"></script>';
$html .= '<script src="'.$jsPath.'ueditor/ueditor.all.min.js"></script>';
$html .= '<script src="'.$jsPath.'ueditor/plugins/135editor.js"></script>';
$html .= '<script>';
foreach ($obj as $k =>$v) {
$html .= 'var ue'.$k.' = UE.ui.Editor({serverUrl:"'.$url.'",initialFrameHeight:500,initialFrameWidth:"100%",autoHeightEnabled:false});ue'.$k.'.render("'.$v.'");';
}
$html .= '</script>';
break;
case 'kindeditor':
if (is_array($obj)) {
$obj = implode(',#', $obj);
}
$html = '<script src="'.$jsPath.'kindeditor/kindeditor-min.js"></script>
<script>
var editor;
KindEditor.ready(function(K) {
editor = K.create(\'#'.$obj.'\', {uploadJson: "'.$url.'",allowFileManager : false,minHeight:500, width:"100%",autoHeightEnabled:false, afterBlur:function(){this.sync();}});
});
</script>';
break;
case 'ckeditor':
$html = '<script src="'.$jsPath.'ckeditor/ckeditor.js"></script>';
$html .= '<script>';
foreach ($obj as $v) {
$html .= 'CKEDITOR.replace("'.$v.'",{filebrowserImageUploadUrl:"'.$url.'"});';
}
$html .= '</script>';
break;
default:
$html = '<link href="'.$jsPath.'umeditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">';
$html .= '<script src="'.$jsPath.'umeditor/third-party/jquery.min.js"></script>';
$html .= '<script src="'.$jsPath.'umeditor/third-party/template.min.js"></script>';
$html .= '<script src="'.$jsPath.'umeditor/umeditor.config.js"></script>';
$html .= '<script src="'.$jsPath.'umeditor/umeditor.min.js"></script>';
$html .= '<script>';
foreach ($obj as $k => $v) {
$html .= 'var um'.$k.' = UM.getEditor("'.$v.'", {
initialFrameWidth:"100%"
,initialFrameHeight:"500"
,autoHeightEnabled:false
,imageUrl:"'.$url.'"
,imageFieldName:"upfile"});';
}
$html .= '</script>';
break;
}
return $html;
}
}
if (!function_exists('str_coding')) {
function str_coding($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : config('hs_auth.key'));
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
}
if (!function_exists('is_empty')) {
function is_empty($value) {
if (!isset($value)) {
return true;
}
if ($value === null) {
return true;
}
if (trim($value) === '') {
return true;
}
return false;
}
}
if (!function_exists('module_info')) {
function module_info($name = '')
{
if (is_empty($name)) {
$name = request()->module();
}
$module = model('system/SystemModule')->where('name', $name)->find();
if (!$module) {
$path = \Env::get('app_path').strtolower($name).'/info.php';
if (!file_exists($path)) {
return false;
}
return include_once $path;
}
return $module->toArray();
}
}
if (!function_exists('module_config')) {
function module_config($name = '', $fileName = 'config')
{
if (is_empty($name)) {
return [];
}
$path = \Env::get('app_path').strtolower($name).'/config/'.$fileName.'.php';
if (!file_exists($path)) {
return false;
}
return include_once $path;
}
}
if (!function_exists('runhook')) {
function runhook($name = '', $params = null, $return = false, $once = false) {
$result = \Hook::listen($name, $params, $once);
if ($return) {
return $result;
}
}
}
if (!function_exists('get_plugins_class')) {
function get_plugins_class($name)
{
return "plugins\\{$name}\\{$name}";
}
}
if (!function_exists('plugins_action_exist')) {
function plugins_action_exist($path = '', $group = 'admin')
{
if (strpos($path, '/')) {
list($name, $controller, $action) = explode('/', $path);
}
$controller = empty($controller) ? 'Index' : ucfirst($controller);
$action = empty($action) ? 'index' : $action;
return method_exists("plugins\\{$name}\\{$group}\\{$controller}", $action);
}
}
if (!function_exists('plugins_run')) {
function plugins_run($path = '', $params = [], $group = 'admin')
{
!defined('IS_PLUGINS') && define('IS_PLUGINS', true);
if (strpos($path, '/')) {
list($name, $controller, $action) = explode('/', $path);
} else {
$name = $path;
}
$controller = empty($controller) ? 'index' : ucfirst($controller);
$action = empty($action) ? 'index' : $action;
if (!is_array($params)) {
$params = (array)$params;
}
$class = "plugins\\{$name}\\{$group}\\{$controller}";
$obj = new $class;
$_GET['_p'] = $name;
$_GET['_c'] = $controller;
$_GET['_a'] = $action;
return call_user_func_array([$obj, $action], [$params]);
}
}
if (!function_exists('plugins_info')) {
function plugins_info($name = '')
{
$path = \Env::get('root_path').'plugins/'.$name.'/info.php';
if (!file_exists($path)) {
return false;
}
return include_once $path;
}
}
if (!function_exists('plugins_url')) {
function plugins_url($url = '', $param = [], $group = '', $urlmode = 2)
{
$params = [];
$params['_p'] = input('param._p');
$params['_c'] = input('param._c', 'Index');
$params['_a'] = input('param._a', 'index');
if ($url) {
$url = explode('/', $url);
$count = count($url);
if ($count == 3) {
$params['_p'] = isset($url[0]) ? $url[0] : '';
$params['_c'] = isset($url[1]) ? ucfirst($url[1]) : 'Index';
$params['_a'] = isset($url[2]) ? $url[2] : 'index';
} else if ($count == 2) {
$params['_c'] = isset($url[0]) ? ucfirst($url[0]) : 'Index';
$params['_a'] = isset($url[1]) ? $url[1] : 'index';
} else {
$params['_a'] = $url[0];
}
}
if (!$params['_p']) {
return '#链接错误';
}
$params = array_merge($params, $param);
if (empty($group)) {
if (defined('ENTRANCE')) {
return url('system/plugins/run', $params);
} else {
if ($urlmode == 2) {
return '/plugins.php?'.http_build_query($params);
}
return '/plugins/'.$params['_p'].'/'.$params['_c'].'/'.$params['_a'].'?'.http_build_query($param);
}
} elseif ($group == 'admin') {
return url('system/plugins/run', $params);
} else {
if ($urlmode == 2) {
return '/plugins.php?'.http_build_query($params);
}
return '/plugins/'.$params['_p'].'/'.$params['_c'].'/'.$params['_a'].'?'.http_build_query($param);
}
}
}