/
www
/
wwwroot
/
guandn.cn
/
inc
/
Upload File
HOME
<?php if (!defined('BY')) { exit('Access Denied'); } function chkemp($str = "", $msg = "", $flag = true) { if (empty($str)) { if ($flag === true) { jsonecho('error', $msg); } else if ($flag === false) { hrc(404); } else { exit('Access Denied'); } } } function chk_rewrite() { global $document_root; $uri = @$_SERVER['REQUEST_URI']; if (!empty($uri)) { $uri = explode('?', $uri)[0]; $ext_arr = ['css', 'js', 'jpg', 'jpeg', 'png', 'bmp']; foreach ($ext_arr as $val) { if (strpos($uri, ".{$val}") !== false) { if (!file_exists("{$document_root}{$uri}")) { hrc(404); } } } } } function en_emoji($content) { $return = preg_replace_callback('/[\xf0-\xf7].{3}/', function ($r) { return '@E' . base64_encode($r[0]); }, $content); return $return; } function de_emoji($content) { $return = preg_replace_callback('/@E(.{6}==)/', function ($r) { return base64_decode($r[1]); }, $content); return $return; } function kfilter($str = "") { if (empty($str)) { return ""; } if (mb_strlen($str) >= 10) { $str = mb_substr($str, 0, 10, 'utf-8'); } $bad = array('select', 'or', 'and', 'delete', 'union', '%', ',', '(', ')', 'from', 'by', '=', "'", 'in', 'group'); for ($i = 1; $i < count($bad); $i++) { $rep[] = ''; } $str = str_replace($bad, $rep, $str); return $str; } function idv($tbl, $str, $id, $idstr = "id") { return nostr($GLOBALS['db']->v("#@__{$tbl}|{$str}|{$idstr}='{$id}'")); } function get_sort_str($cataid, $tpl = "[classname]") { global $catarr; if (empty($cataid)) { return ""; } else { $sorts = idv('cata', 'sorts', $cataid); if (empty($sorts)) { return ""; } else { $sorts = explode('|', $sorts); foreach ($sorts as $val) { if (empty($val)) { continue; } $tpl_str = $tpl; $tpl_str = str_replace('[classname]', $catarr[$val]['classname'], $tpl_str); $tpl_str = str_replace('[classid]', $val, $tpl_str); $arr[] = $tpl_str; } return implode(' > ', $arr); } } } function gettoken($str) { global $conf; return substr(md5($conf['cookieencode'] . $str), 0, 16); } function mkds($f) { $str = "/"; $s = explode('/', $f); for ($i = 1; $i <= count($s) - 2; $i++) { $str = "{$str}/{$s[$i]}/"; if (!file_exists("{$GLOBALS['document_root']}{$str}")) { @mkdir("{$GLOBALS['document_root']}{$str}"); } } } function fgs($url) { $stream_opts = [ "ssl" => [ "verify_peer" => false, "verify_peer_name" => false, ] ]; return file_get_contents($url, false, stream_context_create($stream_opts)); } function getHTTPS($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); curl_close($ch); return $result; } function getesByTxt($txt) { $em = ""; $span = ""; $val = txt2arr($txt); if (isset($val[0])) { $em = $val[0]; } if (isset($val[1])) { $span = $val[1]; } return [$em, $span]; } function format_num($num, $point = 1, $k = 'k', $w = 'w') { $str = ""; if ($num >= 10000) { $return_num = $num / 10000; $str = $w; } elseif ($num >= 1000) { $return_num = $num / 1000; $str = $k; } else { return $num; } return sprintf("%.{$point}f", $return_num) . $str; } function str_valid($str) { //字母、数字、下划线 if (preg_match('/^[_0-9a-z]+$/i', $str)) { return true; } else { return false; } } function loadhtml($filename) { $opts = array( 'file' => array( 'encoding' => "utf-8" ) ); $ctxt = stream_context_create($opts); $str = file_get_contents($filename, FILE_TEXT, $ctxt); return $str; } function is_img($filename) { $types = '.gif|.jpeg|.png|.bmp'; if (file_exists($filename)) { $info = getimagesize($filename); if ($info) { return true; } else { return false; } } else { return false; } } function hrc($num) { http_response_code($num); exit(); } function cssjs($arr) { foreach ($arr as $val) { if (strpos($val, '.css') > 0) { echo "<link rel='stylesheet' type='text/css' href='{$val}' />\n"; } if (strpos($val, '.js') > 0) { echo "<script src='{$val}'></script>\n"; } } } function c2tpl($str, $a, $b) { if (empty($str)) { return ""; } $res = ""; $s = explode($a, $str); foreach ($s as $val) { $res .= str_replace('[]', $val, $b); } return $res; } function arr2tpl($arr, $b) { if (empty($arr)) { return ""; } $res = ""; foreach ($arr as $val) { $res .= str_replace('[]', $val, $b); } return $res; } function http_post_json($url, $data) { $json = json_encode($data, JSON_UNESCAPED_UNICODE); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($json) )); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return json_decode($response, true); } function post_json($url, $jsonstr) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonstr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($jsonstr) ) ); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return array($httpcode, $response); } function get_json() { $str = file_get_contents("php://input"); if (empty($str)) { return false; } if (is_json($str)) { return json_decode($str, true); } return false; } function is_json($str) { return !is_null(json_decode($str, true)); } function listdir($dir) { $temp = scandir($dir); foreach ($temp as $val) { $tar = $dir . '/' . $val; if ($val == '.' || $val == '..') { continue; } if (is_dir($tar)) { echo "<font color='red'>{$tar}</font><br/>"; listdir($tar); } else { echo "{$tar}<br/>"; } } } function buqi($data, $colnum, $str) { $left = count($data) % $colnum; if ($left > 0) { $left = $colnum - $left; for ($i = 1; $i <= $left; $i++) { echo $str; } } } function flagstr($istrue, $str_true = '', $str_false = '') { if ($istrue) { return $str_true; } else { return $str_false; } } function empstr($str, $default) { if (empty($str)) { return $default; } else { return $str; } } function empstrs($str, $default, $strval) { if (empty($str)) { return $default; } else { return $strval; } } function str2arr($str = "") { if (empty($str)) { return []; } $length = mb_strlen($str, 'utf-8'); for ($i = 0; $i < $length; $i++) { $arr[] = mb_substr($str, $i, 1, 'utf-8'); } return $arr; } function txt2arr($txt, $separator = "\r\n") { if (empty($txt)) { return false; } else { $txt = explode($separator, $txt); foreach ($txt as $val) { $arr[] = trim($val); } return $arr; } } function arr2txt($arr, $separator = '|') { if (empty($arr)) { return ""; } else { return implode($separator, $arr); } } function sys() { $value = [ 'model' => '', 'action' => '', 'id' => '', 'dir' => '', 'p' => 0, 'k' => '' ]; //IIS $url = $_SERVER["REQUEST_URI"]; $url = iconv("gb2312//ignore", "UTF-8", $url); //Apache //$url = @$_SERVER["PATH_INFO"]; //Ngix,k=>urldecode //$url = $_SERVER["REQUEST_URI"]; if ($url == '/' || empty($url)) { return false; } $url = explode('?', $url); $url[0] = rtrim($url[0], '/'); //路由解析 $return = router_search($url[0], $value); if ($return) { return $return; } //搜索 $return = urlsearch($url[0], $value, "search"); if ($return) { return $return; } //TAG $return = urlsearch($url[0], $value, "tag"); if ($return) { return $return; } //全匹配 $return = catasearch($url[0]); if ($return) { return setvalue($value, $return); } //分页匹配 $s = explode('/', $url[0]); $last = $s[count($s) - 1]; if (is_numeric($last)) { array_pop($s); $return = catasearch(implode('/', $s)); if ($return) { $value = setvalue($value, $return); $value['p'] = $last; return $value; } } //内页匹配 if (strpos("#{$last}#", '.html') > 0) { array_pop($s); $return = catasearch(implode('/', $s)); if ($return) { $value = setvalue($value, $return, 'content'); $value['id'] = intval($last); return $value; } } } //路由解析 function router_search($url, $value) { global $router; foreach ($router as $val) { if (strpos($url, rtrim($val['url'], "/")) !== false) { $value['model'] = $val['model']; $value['action'] = $val['action']; $value['dir'] = $val['url']; if ($url == rtrim($val['url'], "/")) { return $value; } $s = explode('/', $url); $last = $s[count($s) - 1]; if (is_numeric($last)) { $value['p'] = intval($last); } if (strpos($last, ".html") !== false) { $value['id'] = intval(rtrim($last, '.html')); $value['action'] = $val['caction']; } return $value; } } return false; } //搜索 function urlsearch($url, $value, $tag) { if (strpos("#{$url}#", "/{$tag}/") == 1) { $return = catasearch("/{$tag}/"); if ($return) { $value = setvalue($value, $return); $s = explode('/', trim($url, '/')); if (isset($s[1])) { $value['k'] = $s[1]; $value['dir'] = clearpgdir("/{$s[0]}/{$s[1]}/"); } if (isset($s[2])) { $value['p'] = intval($s[2]); } return $value; } } else { return false; } } function setvalue($v1, $v2, $mode = 'cata') { $v1['model'] = $v2['model']; $v1['action'] = $v2['action']; $v1['id'] = $v2['id']; $v1['dir'] = clearpgdir($v2['url']); if ($mode == 'content') { $v1['action'] = $v2['caction']; } return $v1; } function catasearch($url) { $return = false; foreach ($GLOBALS['catarr'] as $val) { if (trim($val['url'], '/') == trim($url, '/') || trim($val['urlreal'], '/') == trim($url, '/')) { $return = $val; } } return $return; } function clearpgdir($url) { if (empty($url)) { return ''; } else { $url = trim($url, '.html'); } $url = trim($url, '/'); $url = "/{$url}/"; return $url; } function chtml($classid, $id) { if (empty($classid)) { return ''; } if (isset($GLOBALS['catarr'][$classid]['url'])) { $url = $GLOBALS['catarr'][$classid]['url']; $url = clearpgdir($url); return "{$GLOBALS['sys_weburl']}{$url}{$id}.html"; } return ''; } //关键词标红 function redstr($str, $k) { return str_replace($k, "<font color='red'>{$k}</font>", $str); } //N列mr0 function setcss($a, $b, $i, $css = " class='mr0'") { if ($i % $a == $b) { return $css; } else { return ""; } } function chkfrm($rule) { global $document_root; foreach ($rule as $k => $val) { $frmdata[$k] = strpost($k); $s = explode('|', $val); $ss = explode(';', $s[1]); foreach ($ss as $vals) { switch ($vals) { case 'required': if (empty(strpost($k))) { if (strpos($s[0], '请') === false) { jsonecho("error", "{$s[0]}不能为空", $k); } else { jsonecho("error", $s[0], $k); } } break; case 'email': if (!chkemail($_POST[$k])) { jsonecho("error", "请输入正确的邮箱地址", $k); } break; case 'mobile': if (!chkmobile($_POST[$k])) { jsonecho("error", "请输入正确的手机号码", $k); } break; case 'img': if (!is_img("{$document_root}{$_POST[$k]}")) { jsonecho("error", "请上传有效格式图片", $k); } break; case 'sfz': require_once "{$document_root}/inc/IDValidator/IDValidator.php"; $v = com\jdk5\blog\IDValidator\IDValidator::getInstance(); if (!$v->isValid($_POST[$k])) { jsonecho("error", "身份证号格式错误", $k); } break; default: break; } if (strpos($vals, 'eq:') !== false) { $arr = explode(':', $vals); if ($_POST[$k] != $_POST[$arr[1]]) { jsonecho("error", $arr[2], $k); } } } } return $frmdata; } function showckbx($fname, $fid, $default, $tbl = 'ext') { $a = array(); $b = array(); $sql = "select * from #@__{$tbl} where fid='{$fid}'"; $data = $GLOBALS['db']->getall($sql); foreach ($data as $i => $val) { $a[] = $val['id']; $b[] = $val['classname']; } return showcheckboxtxt($fname, implode('|', $a), implode('|', $b), $default); } function showrdbx($fname, $fid, $default, $tbl = 'ext') { $a = array(); $b = array(); $sql = "select * from #@__{$tbl} where fid='{$fid}'"; $data = $GLOBALS['db']->getall($sql); foreach ($data as $i => $val) { $a[] = $val['id']; $b[] = $val['classname']; } return showradiotxt($fname, implode('|', $a), implode('|', $b), $default); } function pr($val, $flag = false) { echo "<pre>"; print_r($val); echo "</pre>"; if ($flag) { exit(); } } function vd($val, $flag = false) { echo "<pre>"; var_dump($val); echo "</pre>"; if ($flag) { exit(); } } function idencode($id) { global $conf; $hashids = new Hashids\Hashids($conf['cookieencode'], 16); return $hashids->encode($id); } function iddecode($str) { global $conf; $hashids = new Hashids\Hashids($conf['cookieencode'], 16); $arr = $hashids->decode($str); if (empty($arr)) { return ''; } else { return $arr[0]; } } function json($strjson) { exit(json_encode($strjson)); } function jsonecho($status, $msg = '', $code = '', $url = '', $callback = '', $data = '') { $msg = array( "status" => $status, "msg" => $msg, "code" => $code, "callback" => $callback, "url" => $url, "data" => $data, ); json($msg); } function jsonecho_if($flag, $status, $msg = '', $code = '', $url = '', $callback = '', $data = '') { if ($flag) { jsonecho($status, $msg, $code, $url, $callback, $data); } } function jsonchkid($tbl = "", $idstr = "", $id = 0, $addtion = '', $prefix = "#@_") { global $msg; if (intval($GLOBALS['db']->v("{$prefix}_{$tbl}|{$idstr}|{$idstr}='{$id}' {$addtion} ")) === 0) { $msg['msg'] = 'Access Denied.'; echo json_encode($msg); exit(); } } //返回时间字符 function gettime($ints, $format = "Y-m-d H:i:s") { $date = new DateTime("@{$ints}"); $date->setTimezone(new DateTimeZone('PRC')); return $date->format($format); } //返回时间戳 function settime($datestr, $format = "Y-m-d H:i:s", $timezone = 'PRC') { $datetime = new DateTime($datestr, new DateTimeZone($timezone)); $datetime->format($format); return $datetime->format('U'); } function gk($obj) { $arr = json_encode($obj); return json_decode($arr, true); } function bm($num) { $num = "{$num}"; $len = strlen($num); $str = str_repeat("0", 5 - $len); return "{$str}{$num}"; } function repchr($str) { $regex = "/\/|\~|\,|\。|\!|\?|\“|\”|\【|\】|\『|\』|\:|\;|\《|\》|\’|\‘|\ |\·|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|/"; return preg_replace($regex, "", $str); } function filter($chars, $encoding = 'utf8') { $pattern = ($encoding == 'utf8') ? '/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u' : '/[\x80-\xFF]/'; preg_match_all($pattern, $chars, $result); $temp = join('', $result[0]); return $temp; } function https_request($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } function emp($str, $default) { if (!empty($str)) { return $str; } else { return $default; } } function getapk($keyname) { $key = strget($keyname); if (empty($key)) { $str = $_SERVER['REQUEST_URI']; $str = substr($str, strpos($str, "?") + 1); parse_str($str, $arr); if (array_key_exists($keyname, $arr)) { $key = $arr[$keyname]; } } return $key; } function filterbadstring($str) { $a = array( "\n", "\\", "\r", "\t", "'", "\"", "%", ",", "(", ")", "[", "]", "$" ); $b = array( "", "", "", "", "", "", "", "", "", "", "", "", "" ); return str_replace($a, $b, $str); } function lefttime($a, $b) { $lefttime = $a - $b; if ($lefttime > 0) { $hour = floor($lefttime / 3600); $minute = floor(($lefttime - $hour * 3600) / 60); $second = $lefttime % 60; $hour = str_pad($hour, 2, "0", STR_PAD_LEFT); $minute = str_pad($minute, 2, "0", STR_PAD_LEFT); $second = str_pad($second, 2, "0", STR_PAD_LEFT); } else { $hour = "00"; $minute = "00"; $second = "00"; } return "{$hour}:{$minute}:{$second}"; } function hidemobile($str, $a = 3, $b = 7) { return substr($str, 0, $a) . "****" . substr($str, $b); } function strsafe($str) { return htmlentities($str, ENT_QUOTES, "UTF-8"); } function strtrim($str) { global $conf; $search = array( " ", "\n", "\r", "\t" ); $replace = array( "", "", "", "" ); if ($conf['language'] !== 'en') { $search[] = ' '; $replace[] = ''; } return str_replace($search, $replace, $str); } function getdes($str, $num) { $search = array( ' ', '“', '”' ); $replace = array( "☀", "☁", "☂" ); $str = str_replace($search, $replace, $str); $str = strtrim(filterbadstring(strip_tags($str))); $str = cutstr($str, $num); $str = str_replace($replace, $search, $str); return $str; } function sms($towho = "", $mtitle = "", $mcont) { $smtpserver = "smtp.qiye.163.com"; $smtpserverport = 25; $smtpusermail = "marketing@cogito.com.cn"; $smtpuser = "marketing@cogito.com.cn"; $smtppass = "vip.163.com"; $mailtype = "HTML"; $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); $smtp->debug = false; $mailok = $smtp->sendmail($towho, $smtpusermail, $mtitle, $mcont, "HTML", "utf-8"); return $mailok; } function subtext($text, $length, $flag = true) { $str = ""; if (mb_strlen($text, 'utf8') > $length) { $str = mb_substr($text, 0, $length, 'utf8'); if ($flag) { $str .= "..."; } } else { $str = $text; } return $str; } function mkpic($w, $h) { global $document_root; $disep = DIRECTORY_SEPARATOR; if (!is_dir("{$document_root}{$disep}cache{$disep}{$w}_{$h}")) { mkdir("{$document_root}{$disep}cache{$disep}{$w}_{$h}"); } if (file_exists("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$w}_{$h}.png")) { return "/cache/{$w}_{$h}/{$w}_{$h}.png"; } $image = imagecreate($w, $h); $yellow = imagecolorallocatealpha($image, 255, 255, 255, 127); imagefill($image, 0, 0, $yellow); imagepng($image, "{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$w}_{$h}.png"); return "/cache/{$w}_{$h}/{$w}_{$h}.png"; } function img_resize($filename, $w, $h) { list($width, $height) = getimagesize($filename); if ($width == $w && $height == $h) { return; } $ext = explode(".", $filename); $ext = strtolower($ext[count($ext) - 1]); if ($ext == "jpg" || $ext == "jpeg") { $im = imagecreatefromjpeg($filename); } else if ($ext == "png") { $im = imagecreatefrompng($filename); } else if ($ext == "gif") { $im = imagecreatefromgif($filename); } $x = imagesx($im); $y = imagesy($im); $im2 = imagecreatetruecolor($w, $h); imagecopyresized($im2, $im, 0, 0, 0, 0, $w, $h, $x, $y); imagepng($im2, $filename); } function imgcaches($pic, $w, $h, $salt = "") { global $document_root; $disep = DIRECTORY_SEPARATOR; $salt = "{$w}*{$h}"; $cache = md5("{$pic}{$salt}") . ".jpg"; $pic = str_replace("/", $disep, $pic); if (!is_dir("{$document_root}{$disep}cache{$disep}{$w}_{$h}")) { mkdir("{$document_root}{$disep}cache{$disep}{$w}_{$h}"); } if (file_exists("{$document_root}{$pic}") && !empty($pic)) { if (!file_exists("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$cache}")) { $thumb = new PHPThumb\GD("{$document_root}{$pic}"); $thumb->resize($w, $h); $thumb->save("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$cache}", "jpg"); } return "/cache/{$w}_{$h}/{$cache}"; } else { return mkpic($w, $h); } } function imgcache($pic, $w, $h, $salt = "") { global $document_root; $disep = DIRECTORY_SEPARATOR; $salt = "{$w}*{$h}"; $cache = md5("{$pic}{$salt}") . ".jpg"; $pic = str_replace("/", $disep, $pic); if (!is_dir("{$document_root}{$disep}cache{$disep}{$w}_{$h}")) { mkdir("{$document_root}{$disep}cache{$disep}{$w}_{$h}"); } if (file_exists("{$document_root}{$pic}") && !empty($pic)) { list($width, $height) = getimagesize("{$document_root}{$disep}{$pic}"); $wd = $width; $ht = $height; if (!file_exists("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$cache}")) { $bi = ceil($width / $height); if ($width < $w) { $width = $w; $height = ceil($width / $bi); } if ($height < $h) { $height = $h; $width = ceil($bi * $h); } $bf = ceil($width / $wd) * 100; $thumb = new PHPThumb\GD("{$document_root}{$pic}"); $thumb->resizePercent($bf); $thumb->save("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$cache}", "jpg"); $thumb = new PHPThumb\GD("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$cache}"); $thumb->adaptiveResize($w, $h); $thumb->save("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$cache}", "jpg"); img_resize("{$document_root}{$disep}cache{$disep}{$w}_{$h}{$disep}{$cache}", $w, $h); } return "/cache/{$w}_{$h}/{$cache}"; } else { return mkpic($w, $h); } } function timetran($time) { $t = time() - $time; $f = array( '31536000' => '年', '2592000' => '个月', '604800' => '星期', '86400' => '天', '3600' => '小时', '60' => '分钟', '1' => '秒' ); foreach ($f as $k => $v) { if (0 != $c = floor($t / (int)$k)) { return $c . $v . '前'; } } } function gettblfields($tbl = '', $prefix = "#@_") { global $db; $columns_data = $db->getall("SHOW COLUMNS FROM {$prefix}_{$tbl}"); $field_data = array_column($columns_data, 'Field'); return $field_data; } function safe_getformdata($tbl = '', $id = 0, $validfields = "", $flag = true, $prefix = "#@_") { global $db; $n = 0; $arr = array(); $arraystr = gettblfields($tbl, $prefix); if (!empty($validfields)) { $validfields = str_replace(",", "|", $validfields); $validfields = "|{$validfields}|"; foreach ($arraystr as $val) { $n = $n + 1; if ($n == 1) { $arr[$val] = $id; continue; } if ($flag) { if (strpos($validfields, "|{$val}|") !== false) { $arr[$val] = strip_tags(strpost($val)); } } else { if (strpos($validfields, "|{$val}|") === false) { $arr[$val] = strip_tags(strpost($val)); } } } } else { foreach ($arraystr as $val) { $arr[$val] = strip_tags(strpost($val)); } $keyid = key($arr); $arr[$keyid] = $id; } return $arr; } function getformdata($tbl = '', $id = 0, $validfields = "", $flag = true, $prefix = "#@_") { global $db; $n = 0; $arr = array(); $arraystr = gettblfields($tbl, $prefix); if (!empty($validfields)) { $validfields = str_replace(",", "|", $validfields); $validfields = "|{$validfields}|"; foreach ($arraystr as $val) { $n = $n + 1; if ($n == 1) { $arr[$val] = $id; continue; } if ($flag) { if (strpos($validfields, "|{$val}|") !== false) { $arr[$val] = strpost($val); } } else { if (strpos($validfields, "|{$val}|") === false) { $arr[$val] = strpost($val); } } } } else { foreach ($arraystr as $val) { $arr[$val] = strpost($val); } $keyid = key($arr); $arr[$keyid] = $id; } return $arr; } function saveformdata($formdata = array(), $tbl = '', $prefix = "#@_") { global $db; $strkey = implode(",", array_keys($formdata)); $strval = implode("','", array_values($formdata)); // 获取KEYID $keyid = key($formdata); if (empty($formdata[$keyid])) { $sql = "insert into {$prefix}_{$tbl}($strkey) values('{$strval}')"; } else { $valstr = ''; foreach ($formdata as $key => $val) { $valstr .= "{$key}='{$val}',"; } $valstr = rtrim($valstr, ','); $sql = "update {$prefix}_{$tbl} set {$valstr} where {$keyid}='{$formdata[$keyid]}'"; } $db->q($sql); if (empty($formdata[$keyid])) { return $db->insertid(); } else { return $formdata[$keyid]; } } function removehtml($str) { $str = trim($str); $str = strip_tags($str, ""); $str = str_replace("\t", "", $str); $str = str_replace("\r\n", "", $str); $str = str_replace("\r", "", $str); $str = str_replace("\n", "", $str); $str = str_replace(" ", " ", $str); return trim($str); } function fileext($file) { return strtolower(pathinfo($file, PATHINFO_EXTENSION)); } function getshowtxt($a, $b, $val = "") { $s = explode("|", $a); $ss = explode("|", $b); foreach ($s as $key => $value) { if ($value == $val) { return $ss[$key]; } } } // 防止直接访问 function hack() { if (!referer()) { exit("Access Denied."); } } // 生成随机字符串 function rndstr($length = 10, $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') { $randomstring = ''; for ($i = 0; $i < $length; $i++) { $randomstring .= $characters[rand(0, strlen($characters) - 1)]; } return $randomstring; } // SORTS查询 function idss($id = 0, $cata = "cata", $prefix = "#@_") { $str = ""; $str = " and classid in (select id from {$prefix}_{$cata} where sorts like '%|{$id}|%')"; return $str; } // 检测表ID function chkid($tbl = "", $idstr = "", $id = 0, $addtion = '', $prefix = "#@_") { if (intval($GLOBALS['db']->v("{$prefix}_{$tbl}|{$idstr}|{$idstr}='{$id}' {$addtion} ")) === 0) { hrc(404); exit(); } } // 检测表ID function chkidreturn($tbl = "", $idstr = "", $id = 0, $addtion = '', $prefix = "#@_") { if (intval($GLOBALS['db']->v("{$prefix}_{$tbl}|{$idstr}|{$idstr}='{$id}' {$addtion} ")) === 0) { return false; } else { return true; } } // 依据条件检测表ID function chkex($tbl = "", $vals = "", $prefix = "#@_") { if (intval($GLOBALS['db']->v("{$prefix}_{$tbl}|id|{$vals}")) === 0) { return false; } else { return true; } } //获取系统初始化信息 function get_cfg($str = '') { return $GLOBALS['db']->v("#@__config|content|fname='{$str}'") . ''; } // 根据ID获取单表数据 function data($tbl = "", $idstr = "id", $id = 0, $prefix = "#@_") { $result = $GLOBALS['db']->getall("select * from {$prefix}_{$tbl} where {$idstr}='{$id}'"); $data = array(); if (empty($id) || empty($result)) { $tblfields = gettblfields($tbl, $prefix); foreach ($tblfields as $key => $value) { $data[$value] = ''; } return $data; } else { return $result[0]; } } // 根据ID获取单表数据简易版 function datax($tbl = "", $id = 0, $idstr = "id", $prefix = "#@_") { return data($tbl, $idstr, $id, $prefix); } // 直接SQL获取单条数据 function datas($sql = "") { $result = $GLOBALS['db']->getall($sql); if ($result) { return $result[0]; } else { return false; } } // 分类表数据 function ca($sorts = "", $index = 1) { $s = explode("|", $sorts); $strlen = count($s) - 2; $pos = sprintf("{$GLOBALS['lang']['position']}<a href='{$GLOBALS['sys_weburl']}'>%s</a>", $GLOBALS['lang']['home']); $nav = ""; $pic = ""; $mpic = ""; for ($i = $index; $i <= $strlen; $i++) { $thisclassname = $GLOBALS['catarr'][$s[$i]]['classname']; $thisurl = $GLOBALS['catarr'][$s[$i]]['url']; $thispic = $GLOBALS['catarr'][$s[$i]]['catapic']; $thismpic = $GLOBALS['catarr'][$s[$i]]['mcatapic']; if (!empty($thispic)) { $pic = $thispic; } if (!empty($thismpic)) { $mpic = $thismpic; } if ($thisurl == "") { $thisurl = ac($s[$i]); } if (is_numeric($thisurl)) { $thisurl = ac($thisurl); } if ($thisurl == "#") { $thisurl = $thisclassname; } else { if ($thisurl[0] == '/') { $thisurl = "{$GLOBALS['sys_weburl']}{$thisurl}"; } $thisurl = "<a href='{$thisurl}'>{$thisclassname}</a>"; } $nav = $thisclassname . "_" . $nav; $pos .= " > " . $thisurl; } $val = array(); $val['pos'] = $pos; $val['nav'] = $nav; $val['pic'] = $pic; $val['mpic'] = $mpic; return $val; } // 栏目链接 function ac($idnum) { if (!isset($GLOBALS['catarr'][$idnum])) { return "javascript:alert('ID:{$idnum} NOT EXISTS!')"; } $m = $GLOBALS['catarr'][$idnum]['model']; $a = $GLOBALS['catarr'][$idnum]['action']; $url = $GLOBALS['catarr'][$idnum]['url']; // 静态转化 if (empty($idnum)) { $data = "{$GLOBALS['sys_weburl']}/{$m}/{$a}.html"; } else { $data = "{$GLOBALS['sys_weburl']}/{$m}/{$a}-{$idnum}.html"; } // 跳转URL if (!empty($url)) { if (is_numeric($url)) { $data = ac($url); } else { if ($url[0] == '/') { $url = "{$GLOBALS['sys_weburl']}{$url}"; } $data = $url; } } return $data; } // 获取广告调用 function getads($str, $lx = 'content', $isnl2br = false, $prefix = "#@_") { $data = ''; $data = $GLOBALS['db']->v("{$prefix}_ad|{$lx}|code='{$str}'"); if ($isnl2br) { return nl2br($data); } else { return $data; } } // URL组合 function geturl($fileurl = "index", $para = "") { $str = ""; $i = 0; if (!empty($_GET)) { foreach ($_GET as $key => $value) { $i += 1; if ($key !== $para) { if ($i == 1) { $str .= "?{$key}={$value}"; } else { $str .= "&{$key}={$value}"; } } } } if ($str == "") { $str = "?act=list"; } return "{$fileurl}.php{$str}"; } // 来源URL function referer() { return @$_SERVER['HTTP_REFERER']; } // 获取扩展名字 function getext($file) { return pathinfo($file, PATHINFO_EXTENSION); } // 验证邮件地址 function chkemail($email = "") { return strlen($email) > 6 && strlen($email) <= 32 && preg_match("/^([A-Za-z0-9\-_.+]+)@([A-Za-z0-9\-]+[.][A-Za-z0-9\-.]+)$/", $email); } // 验证手机号 function chkmobile($mobilephone = "") { if (preg_match('/^1[3|4|5|6|7|8|9][0-9]{9}$/', $mobilephone)) { return true; } else { return false; } } // 关键词变红 function redtxt($a = "", $b = "") { if (empty($b)) { return $a; } else { return str_ireplace($b, "<font color='red'>{$b}</font>", $a); } } // 返回连接字符串 function alink($aname = "", $url = "", $target = "") { if (!empty($target)) { $target = ' target="_blank"'; } return "<a href=\"{$url}\"{$target}>{$aname}</a>"; } // 获取当前四件 function getnow() { $arr = array( '日', '一', '二', '三', '四', '五', '六' ); return date('Y年m月d日') . ' 星期' . $arr[date('w')]; } // 空白文字返回 function nostr($str = "", $defaut = "-") { if ($str == "" || empty($str)) { return $defaut; } else { return $str; } } // 获取复选框 function getcheckbox($dom) { if (isset($_POST[$dom])) { return implode(',', $_POST[$dom]); } else { return ""; } } // 返回左边N个字符 function strleft($text, $length, $flag = true) { $str = ""; if (mb_strlen($text, 'utf8') > $length) { $str = mb_substr($text, 0, $length, 'utf8'); if ($flag) { $str .= "..."; } } else { $str = $text; } return $str; } function cutstr($string, $length, $dot = '...', $charset = 'utf-8') { if (strlen($string) <= $length) { return $string; } $pre = chr(1); $end = chr(1); $string = str_replace(array( '&', '"', '<', '>' ), array( $pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end ), $string); $strcut = ''; if (strtolower($charset) == 'utf-8') { $n = $tn = $noc = 0; while ($n < strlen($string)) { $t = ord($string[$n]); if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { $tn = 1; $n++; $noc++; } elseif (194 <= $t && $t <= 223) { $tn = 2; $n += 2; $noc += 2; } elseif (224 <= $t && $t <= 239) { $tn = 3; $n += 3; $noc += 2; } elseif (240 <= $t && $t <= 247) { $tn = 4; $n += 4; $noc += 2; } elseif (248 <= $t && $t <= 251) { $tn = 5; $n += 5; $noc += 2; } elseif ($t == 252 || $t == 253) { $tn = 6; $n += 6; $noc += 2; } else { $n++; } if ($noc >= $length) { break; } } if ($noc > $length) { $n -= $tn; } $strcut = substr($string, 0, $n); } else { $_length = $length - 1; for ($i = 0; $i < $length; $i++) { if (ord($string[$i]) <= 127) { $strcut .= $string[$i]; } else if ($i < $_length) { $strcut .= $string[$i] . $string[++$i]; } } } $strcut = str_replace(array( $pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end ), array( '&', '"', '<', '>' ), $strcut); $pos = strrpos($strcut, chr(1)); if ($pos !== false) { $strcut = substr($strcut, 0, $pos); } if ($string == $strcut) { return $strcut; } else { return $strcut . $dot; } } function enaddslashes($str = "") { if (get_magic_quotes_gpc() == 1) { return $str; } else { return addslashes($str); } } // GETSTRING function safeget($str) { if ($str == "") { return ""; } else { return trim(htmlspecialchars(@$_GET[$str], ENT_QUOTES, 'utf-8')) . ""; } } // STRGET function strget($str) { $val = ''; if (is_array(@$_GET[$str])) { $val = enaddslashes(implode(',', @$_GET[$str])); } else { $val = enaddslashes(@$_GET[$str]) . ""; } return trim($val); } // GETPOST function safepost($str) { if ($str == "") { return ""; } else { return trim(htmlspecialchars(@$_POST[$str], ENT_QUOTES, 'utf-8')) . ""; } } // STRPOST function strpost($str) { $val = ''; if (is_array(@$_POST[$str])) { $val = enaddslashes(implode(',', @$_POST[$str])); } else { $val = enaddslashes(@$_POST[$str]) . ""; } return trim($val); } //Payload数据提交方式 function strpayload($keyname) { $str = file_get_contents("php://input"); if (empty($str)) { return ""; } else { $arr = json_decode($str, true); if (array_key_exists($keyname, $arr)) { return $arr[$keyname]; } else { return ""; } } } // SETSESSION function setsession($a, $b, $t = 7200) { $_SESSION[$a] = $b; $_SESSION['expiretime'] = time() + $t; } // GETSESSION function getsession($a) { return @$_SESSION[$a] . ""; } // SETCOOKIE function setcookies($a, $b, $t = 86400) { setcookie($a, $b, time() + $t, "/"); } // GETCOOKIE function getcookies($a) { return @$_COOKIE[$a] . ""; } // SAFE COOKIE function stc($key, $value) { global $conf; setcookie($key, $value, time() + 3600 * 240, '/'); setcookie($key . 'ck', substr(md5($conf['cookieencode'] . $value), 0, 16), time() + 3600 * 240, '/'); } //SAFE COOKIE function gtc($key) { global $conf; if (!isset($_COOKIE[$key]) || !isset($_COOKIE[$key . 'ck'])) { return ''; } else { if ($_COOKIE[$key . 'ck'] != substr(md5($conf['cookieencode'] . $_COOKIE[$key]), 0, 16)) { return ''; } else { return $_COOKIE[$key]; } } } // GETMD5 function getmd5($a) { $str = md5($a); $str = md5($str); $str = substr($str, 0, 16); return $str; } // GETSTRING function getstring() { return $_SERVER['QUERY_STRING']; } // GETSCRIPTNAME function getscriptname() { return $_SERVER['SCRIPT_NAME']; } // SHOWINPUT function showinput($inputname, $inputvalue, $inputsize) { return "<input class='ipt w{$inputsize}' type='text' name='{$inputname}' id='{$inputname}' value=\"{$inputvalue}\" />"; } // SHOWINPUTS function showinputx($inputname, $inputvalue, $paras = "") { if ($paras !== "") { $paras = " {$paras}"; } return "<input type='text' name='{$inputname}' id='{$inputname}' value=\"{$inputvalue}\" {$paras} />"; } // SHOWTEXTREA function showtextarea($textareaname, $textareavalue) { return "<div class='txtbox'><textarea class='textbox' name='{$textareaname}' id='{$textareaname}' cols='45' rows='5'>{$textareavalue}</textarea></div>"; } // SHOWTEXTAREAX function showtextareax($textareaname, $textareavalue, $paras = "") { if ($paras !== "") { $paras = " {$paras}"; } return "<div class='txtbox'><textarea name='{$textareaname}' id='{$textareaname}' cols='45' rows='5'{$paras}>{$textareavalue}</textarea></div>"; } // SHOWRADIO function showradio($radioname, $a, $val) { if (is_array($a)) { $s = $a; } else { $s = explode('|', $a); } $str = ""; foreach ($s as $value) { if ($value == $val) { $str .= "<a class='cur' href='javascript:void(0)'><em><input type='radio' class='radio' name='{$radioname}' value=\"{$value}\" checked /></em><span>{$value}</span></a>"; } else { $str .= "<a href='javascript:void(0)'><em><input type='radio' class='radio' name='{$radioname}' value=\"{$value}\" /></em><span>{$value}</span></a>"; } } return "<div class='iradio bke bks bka'>{$str}</div>"; } function showradiotxt($radioname, $a, $b, $val) { if (is_array($a)) { $a = implode('|', array_keys($a)); $b = implode('|', array_values($b)); } $s = explode('|', $a); $ss = explode('|', $b); $str = ""; foreach ($s as $key => $value) { if ($value == $val) { $str .= "<a class='cur' href='javascript:void(0)'><em><input type='radio' class='radio' name='{$radioname}' value=\"{$value}\" checked /></em><span>{$ss[$key]}</span></a>"; } else { $str .= "<a href='javascript:void(0)'><em><input type='radio' class='radio' name='{$radioname}' value=\"{$value}\" /></em><span>{$ss[$key]}</span></a>"; } } return "<div class='iradio bke bks bka'>{$str}</div>"; } // SHOWCHECKBOX function showcheckbox($radioname, $a, $val) { if (is_array($a)) { $s = $a; } else { $s = explode('|', $a); } $str = ""; $pso = ""; foreach ($s as $key => $value) { $pso = strpos(",{$val},", ",{$value},"); if ($pso === false) { $str .= "<a href='javascript:void(0)'><em><input type='checkbox' class='checkbox' name='{$radioname}[]' value=\"{$value}\" /></em><span>{$value}</span></a>"; } else { $str .= "<a class='cur' href='javascript:void(0)'><em><input type='checkbox' class='checkbox' name='{$radioname}[]' value=\"{$value}\" checked /></em><span>{$value}</span></a>"; } } return "<div class='icheck bke bks bka'>{$str}</div>"; } function showcheckboxtxt($radioname, $a, $b, $val) { if (is_array($a)) { $a = implode('|', array_keys($a)); $b = implode('|', array_values($b)); } $s = explode('|', $a); $ss = explode('|', $b); $str = ""; $pso = ""; foreach ($s as $key => $value) { $pso = strpos(",{$val},", ",{$value},"); if ($pso === false) { $str .= "<a href='javascript:void(0)'><em><input type='checkbox' class='checkbox' name='{$radioname}[]' value=\"{$value}\" /></em><span>{$ss[$key]}</span></a>"; } else { $str .= "<a class='cur' href='javascript:void(0)'><em><input type='checkbox' class='checkbox' name='{$radioname}[]' value=\"{$value}\" checked /></em><span>{$ss[$key]}</span></a>"; } } return "<div class='icheck bke bks bka'>{$str}</div>"; } // SHOWSELECT function showselect($selectname, $a, $val, $para = "", $css = "select", $attr = "") { if ($attr !== "") { $attr = " {$attr}"; } $str = ""; if (is_array($a)) { $s = $a; } else { $s = explode('|', $a); } $str .= "<select class='{$css}' name='{$selectname}' id='{$selectname}'{$attr}>{$para}"; foreach ($s as $value) { if ($value == $val) { $str .= "<option value=\"{$value}\" selected='selected'>{$value}</option>"; } else { $str .= "<option value=\"{$value}\">{$value}</option>"; } } $str .= '</select>'; return $str; } // SHOWSELECT function showselecttxt($selectname, $a, $b, $val, $para = "", $css = "select", $attr = "") { if ($attr !== "") { $attr = " {$attr}"; } $str = ""; if (is_array($a)) { $a = implode('|', array_keys($a)); $b = implode('|', array_values($b)); } $s = explode('|', $a); $ss = explode('|', $b); $str .= "<select class='{$css}' name='{$selectname}' id='{$selectname}'{$attr}>{$para}"; foreach ($s as $key => $value) { if ($value == $val) { $str .= "<option value=\"{$value}\" selected='selected'>{$ss[$key]}</option>"; } else { $str .= "<option value=\"{$value}\">{$ss[$key]}</option>"; } } $str .= '</select>'; return $str; } function showselectbytbl($selectname, $defaultval, $tbl, $a, $b, $where = "1=1", $para = "", $css = "select", $attr = "") { if ($attr !== "") { $attr = " {$attr}"; } $str = ""; $data = $GLOBALS['db']->getall("select {$a}, {$b} from #@__{$tbl} where {$where}"); $str .= "<select class='{$css}' name='{$selectname}' id='{$selectname}'{$attr}>{$para}"; foreach ($data as $key => $val) { if ($defaultval == $val[$a]) { $str .= "<option value=\"{$val[$a]}\" selected='selected'>{$val[$b]}</option>"; } else { $str .= "<option value=\"{$val[$a]}\">{$val[$b]}</option>"; } } $str .= '</select>'; return $str; } //开关 function showkg($iptname, $option = "0|1", $default = 0) { $opt = explode('|', $option); $css = ""; if ($default == $opt[1]) { $css = ' cur'; } return "<div class='kaiguan fs0 bkd{$css}' rel1='{$opt[0]}' rel2='{$opt[1]}'>" . "<div class='d1'><input type='hidden' name='{$iptname}' id='{$iptname}' value='{$default}'></div>" . "</div>"; } //勾选 function showgx($iptname, $option = "0|1", $default = 0) { $opt = explode('|', $option); $css = ""; if ($default == $opt[1]) { $css = ' cur'; } return "<div class='gouxuan fs0 bkd{$css}' rel1='{$opt[0]}' rel2='{$opt[1]}'>" . "<div class='d1'><input type='hidden' name='{$iptname}' id='{$iptname}' value='{$default}'></div>" . "</div>"; } // 弹出消息 function alert($str = "", $flag = false) { echo "<script language='javascript' type='text/javascript'>alert('{$str}');</script>\n"; if ($flag) { exit(); } } // 弹出消息并返回 function alertback($str = "", $flag = false) { echo "<script language='javascript' type='text/javascript'>alert('{$str}');history.go(-1)</script>\n"; if ($flag) { exit(); } } // 弹出消息并跳转 function alerturl($str = "", $url = "", $flag = false) { echo "<script language='javascript' type='text/javascript'>alert('{$str}');window.location.href='{$url}'</script>\n"; if ($flag) { exit(); } } // 直接跳转 function go($url = "", $flag = false) { echo "<script language='javascript' type='text/javascript'>window.location.href='{$url}'</script>"; if ($flag) { exit(); } } // 输出js function echojs($str) { echo "<script language='javascript' type='text/javascript'>$(function(){{$str}})</script>\n"; } // 编辑器 function cke($domid, $strs = "", $h = 350, $jsconfig = "", $method = "uploadcke") { if (empty($h)) { $h = "350"; } if (!empty($jsconfig)) { $jsconfig = "customConfig:'/plugins/ckeditor/{$jsconfig}.js?t=" . time() . "',"; } $str = "<div class='rel'>"; $str .= "<div>"; $str .= "<textarea id='{$domid}' name='{$domid}' style='height:350px;visibility:hidden;'>" . $strs . '</textarea>'; $str .= '<script>'; $str .= "CKEDITOR.replace('{$domid}',{{$jsconfig}height:'{$h}',qtRows:10,qtColumns:10,qtBorder:'1',qtWidth:'100%',qtStyle:{'border-collapse':'collapse'},qtClass:'test',qtCellPadding:'5',qtCellSpacing:'1'})"; $str .= '</script>'; $str .= "</div>"; $str .= "<div class='cke_tools_bar'>"; $str .= "<a href='javascript:;' id='file_upload_{$domid}' class='ckebtn'><i class='ri-upload-2-fill mr5'></i>批量上传</a><script>$(function(){{$method}('{$domid}')})</script>"; $str .= "<a href='javascript:;' class='ckebtn btn-fujian' dm='{$domid}'><i class='ri-upload-cloud-fill mr5'></i>已有附件</a>"; $str .= "</div>"; $str .= "</div>"; return $str; } // 批量上传文件 function ufs($filetypeexts = "", $domid = "", $maxnum = 1) { return "<a id='file_upload_{$domid}' class='uploads'><i class='ri-upload-2-fill mr5'></i>浏览上传</a><script>$(function(){uploads('{$domid}')})</script>"; } // 普通上传文件 function uf($filetypeexts = "", $domid = "", $maxnum = 1, $method = "upload") { return "<a id='file_upload_{$domid}' class='uploads'><i class='ri-upload-2-fill mr5'></i>浏览上传</a><script>$(function(){{$method}('{$domid}')})</script>"; } // 清除空白字符 function clearblank($str = "") { $str = preg_replace("/\r\n/", "", $str); $str = preg_replace("/\r/", "", $str); $str = preg_replace("/\t/", "", $str); $str = preg_replace("/\n/", "", $str); return $str; } // 返回安全字符 function getsafestr($str = "") { $safestr = '{|_|0|1|2|3|4|5|6|7|8|9|q|w|e|r|t|y|u|i|o|p|a|s|d|f|g|h|j|k|l|z|x|c|v|b|n|m|Q|W|E|R|T|Y|U|I|O|P|A|S|D|F|G|H|J|K|L|Z|X|C|V|B|N|M|}'; $returstr = ""; for ($i = 0; $i < strlen($str); $i++) { if (strpos($safestr, '|' . substr($str, $i, 1) . '|')) { $returstr .= substr($str, $i, 1); } } return $returstr; } // 获得用户IP function getip() { $realip = ""; $unknown = 'unknown'; if (isset($_SERVER)) { if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); foreach ($arr as $ip) { $ip = trim($ip); if ($ip != 'unknown') { $realip = $ip; break; } } } else if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)) { $realip = $_SERVER['HTTP_CLIENT_IP']; } else if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) { $realip = $_SERVER['REMOTE_ADDR']; } else { $realip = $unknown; } } else { if (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)) { $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)) { $realip = getenv("HTTP_CLIENT_IP"); } else if (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)) { $realip = getenv("REMOTE_ADDR"); } else { $realip = $unknown; } } $realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown; return $realip; } // 获取IP地址信息 function ipinfo($ip = "") { if (empty($ip)) { $ip = getip(); } $res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip); if (empty($res)) { return false; } $jsonMatches = array(); preg_match('#\{.+?\}#', $res, $jsonMatches); if (!isset($jsonMatches[0])) { return false; } $json = json_decode($jsonMatches[0], true); if (isset($json['ret']) && $json['ret'] == 1) { $json['ip'] = $ip; unset($json['ret']); } else { return false; } return $json; }