| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- /**
- * 此文件放置与业务无关的函数
- * 与业务无关是指不依赖于项目本身,不同项目或多种环境下可以通用
- *
- * 与业务有关的函数放置于function.inc.php
- * 系统环境有关的函数见lib/common/Env类
- * 参数检查有关的函数见lib/common/ParamCheck类
- *
- * @createtime 2018/03/01
- * @author 空竹
- * @copyright 芝麻开发(http://www.zhimawork.com)
- */
- /**
- * 操作响应通知(默认json格式)
- *
- * @param $msg 消息内容
- * @param $code 消息代码
- * @return
- */
- function action_msg($msg, $code, $json = true){
- $r = array(
- 'code' => $code,
- 'msg' => $msg
- );
- if($json)
- return json_encode_cn($r);
- else
- return $r;
- }
- /**
- * 退出并输出json格式的响应
- */
- function die_json($msg, $code = -1) {
- die(action_msg($msg, $code));
- }
- /**
- * safeCheck() 参数检查,并防XSS 和 SQL注入
- *
- * @param mixed $str
- * @param bool $number 是否做数字检查 1-(默认)数字 0-不是数字
- * @param bool $script 是否过滤script 1-(默认)过滤;0-不过滤
- * @return
- */
- function safeCheck($str, $number = 1, $script = 1){
- return _safeCheck1($str, $number, $script, 0);
- }
- /**
- * safeCheck1() 功能同safeCheck(),唯一区别是在检查不通过时返回json格式的响应
- *
- * @param mixed $str
- * @param bool $number 是否做数字检查 1-(默认)数字 0-不是数字
- * @param bool $script 是否过滤script 1-(默认)过滤;0-不过滤
- * @return
- */
- function safeCheck1($str, $number = 1, $script = 1){
- return _safeCheck1($str, $number, $script, 1);
- }
- /**
- * 勿直接调用,谢谢!
- */
- function _safeCheck1($str, $number = 1, $script = 1, $json = 1){
- $str = trim($str);
- //防止SQL注入
- if(!get_magic_quotes_gpc()){
- $str = addslashes($str);
- }
- //数字检查
- if($number == 1){
- $isint = preg_match('/^-?\d+$/',$str);
- $isfloat = preg_match('/^(-?\d+)(\.\d+)?$/',$str);
- if(!$isint && !$isfloat){
- if ($json == 1) {
- die_json('参数值'.$str.'必须为数字');
- } else {
- die('参数值'.$str.'必须为数字');
- }
- }
- }else{
- //过滤script、防XSS
- if($script == 1){
- $str = htmlspecialchars($str);
- }
- }
- return $str;
- }
- /**
- * ckReplace() ckEditor编辑器内容处理
- *
- * @param mixed $content
- * @return
- */
- function ckReplace($content){
- if (!empty($content)){
- $content = str_replace("'", "'", $content);
- $content = str_replace("<br />", "</p><p>", $content);
- }
- return $content;
- }
- /**
- * HTMLEncode()将特殊字符转成HTML格式,主要用于textarea获取值
- *
- * @param mixed $str
- * @return
- */
- function HTMLEncode($str){
- if (!empty($str)){
- $str = str_replace("&","&",$str);
- $str = str_replace(">",">",$str);
- $str = str_replace("<","<",$str);
- $str = str_replace(CHR(32)," ",$str);
- $str = str_replace(CHR(9)," ",$str);
- $str = str_replace(CHR(9),"    ",$str);
- $str = str_replace(CHR(34),""",$str);
- $str = str_replace("'","'",$str);
- $str = str_replace(CHR(39),"'",$str);
- $str = str_replace(CHR(13),"",$str);
- $str = str_replace(CHR(10),"<br/>",$str);
- }
- return $str;
- }
- /**
- * HTMLDecode()将HTMLEncode的数据还原
- *
- * @param mixed $str
- * @return
- */
- Function HTMLDecode($str){
- if (!empty($str)){
- $str = str_replace("&","&",$str);
- $str = str_replace(">",">",$str);
- $str = str_replace("<","<",$str);
- $str = str_replace(" ",CHR(32),$str);
- $str = str_replace(" ",CHR(9),$str);
- $str = str_replace("    ",CHR(9),$str);
- $str = str_replace(""",CHR(34),$str);
- $str = str_replace("'",CHR(39),$str);
- $str = str_replace("",CHR(13),$str);
- $str = str_replace("<br/>",CHR(10),$str);
- $str = str_replace("<br />",CHR(10),$str);
- $str = str_replace("<br>",CHR(10),$str);
- }
- return $str;
- }
- /**
- * 生成随机数randcode()
- *
- * @param mixed $len
- * @param integer $mode
- * @return
- */
- function randcode($len, $mode = 2){
- $rcode = '';
- switch($mode){
- case 1: //去除0、o、O、l等易混淆字符
- $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghijkmnpqrstuvwxyz';
- break;
- case 2: //纯数字
- $chars = '0123456789';
- break;
- case 3: //全数字+大小写字母
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
- break;
- case 4: //全数字+大小写字母+一些特殊字符
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()';
- break;
- }
-
- $count = strlen($chars) - 1;
- mt_srand((double)microtime() * 1000000);
- for($i = 0; $i < $len; $i++) {
- $rcode .= $chars[mt_rand(0, $count)];
- }
-
- return $rcode;
- }
- /**
- * Json_encode的Unicode中文(\u4e2d\u56fd)问题
- *
- * @param mixed $array
- * @return
- */
- function json_encode_cn($array){
- $str = json_encode($array);
- $os = Env::getOSType();
- if($os == 'windows')
- $ucs = 'UCS-2';
- else
- $ucs = 'UCS-2BE';
-
- if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
- $str = preg_replace_callback("/\\\\u([0-9a-f]{4})/i", function($matches) use($ucs){return iconv($ucs, "UTF-8", pack("H*", $matches[1]));}, $str);
- /** //2018/12/23 修正PHP7.2以上版本不支持create_function
- }else if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
- $str = preg_replace_callback("/\\\\u([0-9a-f]{4})/i", create_function('$matches', 'return iconv("'.$ucs.'", "UTF-8", pack("H*", $matches[1]));'), $str);
- **/
- }else{
- $str = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('".$ucs."', 'UTF-8', pack('H4', '\\1'))", $str);
- }
- return $str;
- }
- /**
- * 检测图片宽高是否符合要求,常用于图片上传
- * @param $filepath 文件绝对路径
- * @param $targetWidth 目标宽度
- * @param $targetHeight 目标高度
- * @return bool
- */
- function checkImgSize($filepath, $targetWidth, $targetHeight) {
- $imageinfo = getimagesize($filepath);
- if ($imageinfo[0] != $targetWidth || $imageinfo[1] != $targetHeight) {
- return false;
- }
- return true;
- }
- function subStrContent($content,$len,$dian=0)
- {
- if(mb_strlen($content,"UTF-8")>$len)
- {
- $content = mb_substr($content,0,$len,"utf-8");
- if($dian)
- {
- $content .="...";
- }
- }
- return $content;
- }
- /**
- * getDateStrS() 时间格式转换
- */
- function getDateStrS($time){
- if(empty($time)) return "";
- $todayBegin = strtotime(date("Y-m-d"));
- $diff = $time - $todayBegin;
- if($diff>=0)
- return date('H:i', $time);
- else if($diff<0&&$diff>-86400)
- return '昨天';
- else
- return date('Y/m/d H:i:s', $time);
- }
- /**
- * getDateStr() 时间格式转换
- */
- function getDateStr($time){
- if($time)
- return date('Y-m-d H:i:s', $time);
- else
- return '';
- }
- /**
- * getDateStrI() 时间格式转换
- */
- function getDateStrI($time){
- if($time)
- return date('Y-m-d H:i', $time);
- else
- return '';
- }
- /**
- * getDateStrI() 时间格式转换
- */
- function getDateStrC($time){
- if($time)
- return date('Y-m-d', $time);
- else
- return '';
- }
- function wordCode($parameter){
- $arr = explode('<br/>',$parameter);
- if(!is_array($arr)){
- $arr = explode('<br />',$parameter);
- }
- $str='';
- foreach($arr as $val){
- //这里是先去掉空格等一般样式
- $val = HTMLDecode($val);
- $val = str_replace("<", "<", $val);
- $val = str_replace(">", ">", $val);
- $str.= '<w:p><w:r><w:rPr><w:sz w:val="21"/><w:sz-cs w:val="10.5"/></w:rPr><w:t>'.htmlspecialchars($val, ENT_COMPAT).'</w:t></w:r></w:p>';
- }
- //这里是转换上面的<w:p>等
- $str = HTMLDecode($str);
- return $str;
- }
- /**
- * wordPrintReplace() 写入word特殊字符替换
- *
- * @param mixed $content
- * @return
- */
- function wordPrintReplace($content){
- if (!empty($content)){
- $content = str_replace("&", "&", $content);
- }
- return $content;
- }
- /**
- * 获取首字母
- * @param $str
- * @return null|string
- */
- function getFirstCharter($str)
- {
- if (empty($str)) {
- return '';
- }
- $fchar = ord($str{0});
- if ($fchar >= ord('A') && $fchar <= ord('z'))
- return strtoupper($str{0});
- $s1 = iconv('UTF-8','gbk//ignore', $str);
- $s2 = iconv('gbk', 'UTF-8//ignore', $s1);
- $s = $s2 == $str ? $s1 : $str;
- if($s{1}){
- $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
- }else{
- return null;
- }
- if ($asc >= -20319 && $asc <= -20284)
- return 'A';
- if ($asc >= -20283 && $asc <= -19776)
- return 'B';
- if ($asc >= -19775 && $asc <= -19219)
- return 'C';
- if ($asc >= -19218 && $asc <= -18711)
- return 'D';
- if ($asc >= -18710 && $asc <= -18527)
- return 'E';
- if ($asc >= -18526 && $asc <= -18240)
- return 'F';
- if ($asc >= -18239 && $asc <= -17923)
- return 'G';
- if ($asc >= -17922 && $asc <= -17418)
- return 'H';
- if ($asc >= -17417 && $asc <= -16475)
- return 'J';
- if ($asc >= -16474 && $asc <= -16213)
- return 'K';
- if ($asc >= -16212 && $asc <= -15641)
- return 'L';
- if ($asc >= -15640 && $asc <= -15166)
- return 'M';
- if ($asc >= -15165 && $asc <= -14923)
- return 'N';
- if ($asc >= -14922 && $asc <= -14915)
- return 'O';
- if ($asc >= -14914 && $asc <= -14631)
- return 'P';
- if ($asc >= -14630 && $asc <= -14150)
- return 'Q';
- if ($asc >= -14149 && $asc <= -14091)
- return 'R';
- if ($asc >= -14090 && $asc <= -13319)
- return 'S';
- if ($asc >= -13318 && $asc <= -12839)
- return 'T';
- if ($asc >= -12838 && $asc <= -12557)
- return 'W';
- if ($asc >= -12556 && $asc <= -11848)
- return 'X';
- if ($asc >= -11847 && $asc <= -11056)
- return 'Y';
- if ($asc >= -11055 && $asc <= -10247)
- return 'Z';
- return null;
- }
- ?>
|