function_common.inc.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * 此文件放置与业务无关的函数
  4. * 与业务无关是指不依赖于项目本身,不同项目或多种环境下可以通用
  5. *
  6. * 与业务有关的函数放置于function.inc.php
  7. * 系统环境有关的函数见lib/common/Env类
  8. * 参数检查有关的函数见lib/common/ParamCheck类
  9. *
  10. * @createtime 2018/03/01
  11. * @author 空竹
  12. * @copyright 芝麻开发(http://www.zhimawork.com)
  13. */
  14. /**
  15. * 操作响应通知(默认json格式)
  16. *
  17. * @param $msg 消息内容
  18. * @param $code 消息代码
  19. * @return
  20. */
  21. function action_msg($msg, $code, $json = true){
  22. $r = array(
  23. 'code' => $code,
  24. 'msg' => $msg
  25. );
  26. if($json)
  27. return json_encode_cn($r);
  28. else
  29. return $r;
  30. }
  31. /**
  32. * 退出并输出json格式的响应
  33. */
  34. function die_json($msg, $code = -1) {
  35. die(action_msg($msg, $code));
  36. }
  37. /**
  38. * safeCheck() 参数检查,并防XSS 和 SQL注入
  39. *
  40. * @param mixed $str
  41. * @param bool $number 是否做数字检查 1-(默认)数字 0-不是数字
  42. * @param bool $script 是否过滤script 1-(默认)过滤;0-不过滤
  43. * @return
  44. */
  45. function safeCheck($str, $number = 1, $script = 1){
  46. return _safeCheck1($str, $number, $script, 0);
  47. }
  48. /**
  49. * safeCheck1() 功能同safeCheck(),唯一区别是在检查不通过时返回json格式的响应
  50. *
  51. * @param mixed $str
  52. * @param bool $number 是否做数字检查 1-(默认)数字 0-不是数字
  53. * @param bool $script 是否过滤script 1-(默认)过滤;0-不过滤
  54. * @return
  55. */
  56. function safeCheck1($str, $number = 1, $script = 1){
  57. return _safeCheck1($str, $number, $script, 1);
  58. }
  59. /**
  60. * 勿直接调用,谢谢!
  61. */
  62. function _safeCheck1($str, $number = 1, $script = 1, $json = 1){
  63. $str = trim($str);
  64. //防止SQL注入
  65. if(!get_magic_quotes_gpc()){
  66. $str = addslashes($str);
  67. }
  68. //数字检查
  69. if($number == 1){
  70. $isint = preg_match('/^-?\d+$/',$str);
  71. $isfloat = preg_match('/^(-?\d+)(\.\d+)?$/',$str);
  72. if(!$isint && !$isfloat){
  73. if ($json == 1) {
  74. die_json('参数值'.$str.'必须为数字');
  75. } else {
  76. die('参数值'.$str.'必须为数字');
  77. }
  78. }
  79. }else{
  80. //过滤script、防XSS
  81. if($script == 1){
  82. $str = htmlspecialchars($str);
  83. }
  84. }
  85. return $str;
  86. }
  87. /**
  88. * ckReplace() ckEditor编辑器内容处理
  89. *
  90. * @param mixed $content
  91. * @return
  92. */
  93. function ckReplace($content){
  94. if (!empty($content)){
  95. $content = str_replace("'", "&#39;", $content);
  96. $content = str_replace("<br />", "</p><p>", $content);
  97. }
  98. return $content;
  99. }
  100. /**
  101. * HTMLEncode()将特殊字符转成HTML格式,主要用于textarea获取值
  102. *
  103. * @param mixed $str
  104. * @return
  105. */
  106. function HTMLEncode($str){
  107. if (!empty($str)){
  108. $str = str_replace("&","&amp;",$str);
  109. $str = str_replace(">","&gt;",$str);
  110. $str = str_replace("<","&lt;",$str);
  111. $str = str_replace(CHR(32),"&nbsp;",$str);
  112. $str = str_replace(CHR(9),"&nbsp;&nbsp;&nbsp;&nbsp;",$str);
  113. $str = str_replace(CHR(9),"&#160;&#160;&#160;&#160;",$str);
  114. $str = str_replace(CHR(34),"&quot;",$str);
  115. $str = str_replace("'","&#39;",$str);
  116. $str = str_replace(CHR(39),"&#39;",$str);
  117. $str = str_replace(CHR(13),"",$str);
  118. $str = str_replace(CHR(10),"<br/>",$str);
  119. }
  120. return $str;
  121. }
  122. /**
  123. * HTMLDecode()将HTMLEncode的数据还原
  124. *
  125. * @param mixed $str
  126. * @return
  127. */
  128. Function HTMLDecode($str){
  129. if (!empty($str)){
  130. $str = str_replace("&amp;","&",$str);
  131. $str = str_replace("&gt;",">",$str);
  132. $str = str_replace("&lt;","<",$str);
  133. $str = str_replace("&nbsp;",CHR(32),$str);
  134. $str = str_replace("&nbsp;&nbsp;&nbsp;&nbsp;",CHR(9),$str);
  135. $str = str_replace("&#160;&#160;&#160;&#160;",CHR(9),$str);
  136. $str = str_replace("&quot;",CHR(34),$str);
  137. $str = str_replace("&#39;",CHR(39),$str);
  138. $str = str_replace("",CHR(13),$str);
  139. $str = str_replace("<br/>",CHR(10),$str);
  140. $str = str_replace("<br />",CHR(10),$str);
  141. $str = str_replace("<br>",CHR(10),$str);
  142. }
  143. return $str;
  144. }
  145. /**
  146. * 生成随机数randcode()
  147. *
  148. * @param mixed $len
  149. * @param integer $mode
  150. * @return
  151. */
  152. function randcode($len, $mode = 2){
  153. $rcode = '';
  154. switch($mode){
  155. case 1: //去除0、o、O、l等易混淆字符
  156. $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghijkmnpqrstuvwxyz';
  157. break;
  158. case 2: //纯数字
  159. $chars = '0123456789';
  160. break;
  161. case 3: //全数字+大小写字母
  162. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  163. break;
  164. case 4: //全数字+大小写字母+一些特殊字符
  165. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()';
  166. break;
  167. }
  168. $count = strlen($chars) - 1;
  169. mt_srand((double)microtime() * 1000000);
  170. for($i = 0; $i < $len; $i++) {
  171. $rcode .= $chars[mt_rand(0, $count)];
  172. }
  173. return $rcode;
  174. }
  175. /**
  176. * Json_encode的Unicode中文(\u4e2d\u56fd)问题
  177. *
  178. * @param mixed $array
  179. * @return
  180. */
  181. function json_encode_cn($array){
  182. $str = json_encode($array);
  183. $os = Env::getOSType();
  184. if($os == 'windows')
  185. $ucs = 'UCS-2';
  186. else
  187. $ucs = 'UCS-2BE';
  188. if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
  189. $str = preg_replace_callback("/\\\\u([0-9a-f]{4})/i", function($matches) use($ucs){return iconv($ucs, "UTF-8", pack("H*", $matches[1]));}, $str);
  190. /** //2018/12/23 修正PHP7.2以上版本不支持create_function
  191. }else if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
  192. $str = preg_replace_callback("/\\\\u([0-9a-f]{4})/i", create_function('$matches', 'return iconv("'.$ucs.'", "UTF-8", pack("H*", $matches[1]));'), $str);
  193. **/
  194. }else{
  195. $str = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('".$ucs."', 'UTF-8', pack('H4', '\\1'))", $str);
  196. }
  197. return $str;
  198. }
  199. /**
  200. * 检测图片宽高是否符合要求,常用于图片上传
  201. * @param $filepath 文件绝对路径
  202. * @param $targetWidth 目标宽度
  203. * @param $targetHeight 目标高度
  204. * @return bool
  205. */
  206. function checkImgSize($filepath, $targetWidth, $targetHeight) {
  207. $imageinfo = getimagesize($filepath);
  208. if ($imageinfo[0] != $targetWidth || $imageinfo[1] != $targetHeight) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. function subStrContent($content,$len,$dian=0)
  214. {
  215. if(mb_strlen($content,"UTF-8")>$len)
  216. {
  217. $content = mb_substr($content,0,$len,"utf-8");
  218. if($dian)
  219. {
  220. $content .="...";
  221. }
  222. }
  223. return $content;
  224. }
  225. /**
  226. * getDateStrS() 时间格式转换
  227. */
  228. function getDateStrS($time){
  229. if(empty($time)) return "";
  230. $todayBegin = strtotime(date("Y-m-d"));
  231. $diff = $time - $todayBegin;
  232. if($diff>=0)
  233. return date('H:i', $time);
  234. else if($diff<0&&$diff>-86400)
  235. return '昨天';
  236. else
  237. return date('Y/m/d H:i:s', $time);
  238. }
  239. /**
  240. * getDateStr() 时间格式转换
  241. */
  242. function getDateStr($time){
  243. if($time)
  244. return date('Y-m-d H:i:s', $time);
  245. else
  246. return '';
  247. }
  248. /**
  249. * getDateStrI() 时间格式转换
  250. */
  251. function getDateStrI($time){
  252. if($time)
  253. return date('Y-m-d H:i', $time);
  254. else
  255. return '';
  256. }
  257. /**
  258. * getDateStrI() 时间格式转换
  259. */
  260. function getDateStrC($time){
  261. if($time)
  262. return date('Y-m-d', $time);
  263. else
  264. return '';
  265. }
  266. function wordCode($parameter){
  267. $arr = explode('<br/>',$parameter);
  268. if(!is_array($arr)){
  269. $arr = explode('<br />',$parameter);
  270. }
  271. $str='';
  272. foreach($arr as $val){
  273. //这里是先去掉空格等一般样式
  274. $val = HTMLDecode($val);
  275. $val = str_replace("<", "<", $val);
  276. $val = str_replace(">", ">", $val);
  277. $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>';
  278. }
  279. //这里是转换上面的<w:p>等
  280. $str = HTMLDecode($str);
  281. return $str;
  282. }
  283. /**
  284. * wordPrintReplace() 写入word特殊字符替换
  285. *
  286. * @param mixed $content
  287. * @return
  288. */
  289. function wordPrintReplace($content){
  290. if (!empty($content)){
  291. $content = str_replace("&", "&", $content);
  292. }
  293. return $content;
  294. }
  295. /**
  296. * 获取首字母
  297. * @param $str
  298. * @return null|string
  299. */
  300. function getFirstCharter($str)
  301. {
  302. if (empty($str)) {
  303. return '';
  304. }
  305. $fchar = ord($str{0});
  306. if ($fchar >= ord('A') && $fchar <= ord('z'))
  307. return strtoupper($str{0});
  308. $s1 = iconv('UTF-8','gbk//ignore', $str);
  309. $s2 = iconv('gbk', 'UTF-8//ignore', $s1);
  310. $s = $s2 == $str ? $s1 : $str;
  311. if($s{1}){
  312. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  313. }else{
  314. return null;
  315. }
  316. if ($asc >= -20319 && $asc <= -20284)
  317. return 'A';
  318. if ($asc >= -20283 && $asc <= -19776)
  319. return 'B';
  320. if ($asc >= -19775 && $asc <= -19219)
  321. return 'C';
  322. if ($asc >= -19218 && $asc <= -18711)
  323. return 'D';
  324. if ($asc >= -18710 && $asc <= -18527)
  325. return 'E';
  326. if ($asc >= -18526 && $asc <= -18240)
  327. return 'F';
  328. if ($asc >= -18239 && $asc <= -17923)
  329. return 'G';
  330. if ($asc >= -17922 && $asc <= -17418)
  331. return 'H';
  332. if ($asc >= -17417 && $asc <= -16475)
  333. return 'J';
  334. if ($asc >= -16474 && $asc <= -16213)
  335. return 'K';
  336. if ($asc >= -16212 && $asc <= -15641)
  337. return 'L';
  338. if ($asc >= -15640 && $asc <= -15166)
  339. return 'M';
  340. if ($asc >= -15165 && $asc <= -14923)
  341. return 'N';
  342. if ($asc >= -14922 && $asc <= -14915)
  343. return 'O';
  344. if ($asc >= -14914 && $asc <= -14631)
  345. return 'P';
  346. if ($asc >= -14630 && $asc <= -14150)
  347. return 'Q';
  348. if ($asc >= -14149 && $asc <= -14091)
  349. return 'R';
  350. if ($asc >= -14090 && $asc <= -13319)
  351. return 'S';
  352. if ($asc >= -13318 && $asc <= -12839)
  353. return 'T';
  354. if ($asc >= -12838 && $asc <= -12557)
  355. return 'W';
  356. if ($asc >= -12556 && $asc <= -11848)
  357. return 'X';
  358. if ($asc >= -11847 && $asc <= -11056)
  359. return 'Y';
  360. if ($asc >= -11055 && $asc <= -10247)
  361. return 'Z';
  362. return null;
  363. }
  364. ?>