PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/Common/common.php

https://github.com/alin40404/FanweShare
PHP | 449 lines | 322 code | 40 blank | 87 comment | 43 complexity | 9b90991d4f7f3efb8cb9064e84fcf4fc MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * 由数据库取出系统的配置
  4. *
  5. * @access public
  6. * @param mix $name
  7. *
  8. * @return mix
  9. */
  10. function fanweC($name)
  11. {
  12. static $sys_conf = NULL;
  13. if($name == 'SITE_URL')
  14. return "http://".$_SERVER['HTTP_HOST'];
  15. else
  16. {
  17. if ($sys_conf === NULL)
  18. {
  19. $sys_conf = D("SysConf")->where("status=1")->getField("name,val");
  20. }
  21. return $sys_conf[$name];
  22. }
  23. }
  24. /**
  25. * 递归方式的对变量中的特殊字符进行转义
  26. *
  27. * @access public
  28. * @param mix $value
  29. *
  30. * @return mix
  31. */
  32. function addslashesDeep($value)
  33. {
  34. if (empty($value))
  35. {
  36. return $value;
  37. }
  38. else
  39. {
  40. return is_array($value) ? array_map('addslashesDeep', $value) : addslashes($value);
  41. }
  42. }
  43. /**
  44. * 将对象成员变量或者数组的特殊字符进行转义
  45. *
  46. * @access public
  47. * @param mix $obj 对象或者数组
  48. * @author Xuan Yan
  49. *
  50. * @return mix 对象或者数组
  51. */
  52. function addslashesDeepObj($obj)
  53. {
  54. if (is_object($obj) == true)
  55. {
  56. foreach ($obj AS $key => $val)
  57. {
  58. $obj->$key = addslashesDeep($val);
  59. }
  60. }
  61. else
  62. {
  63. $obj = addslashesDeep($obj);
  64. }
  65. return $obj;
  66. }
  67. /**
  68. * 递归方式的对变量中的特殊字符去除转义
  69. *
  70. * @access public
  71. * @param mix $value
  72. *
  73. * @return mix
  74. */
  75. function stripslashesDeep($value)
  76. {
  77. if (empty($value))
  78. {
  79. return $value;
  80. }
  81. else
  82. {
  83. return is_array($value) ? array_map('stripslashesDeep', $value) : stripslashes($value);
  84. }
  85. }
  86. /**
  87. * 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
  88. *
  89. * @access public
  90. * @param string $str 待转换字串
  91. *
  92. * @return string $str 处理后字串
  93. */
  94. function makeSemiangle($str)
  95. {
  96. $arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
  97. '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
  98. 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
  99. 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
  100. 'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
  101. 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
  102. 'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
  103. 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
  104. 'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
  105. 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
  106. 'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
  107. 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
  108. 'y' => 'y', 'z' => 'z',
  109. '(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
  110. '】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
  111. '‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<',
  112. '》' => '>',
  113. '%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
  114. ':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
  115. ';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
  116. '”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"',
  117. ' ' => ' ');
  118. return strtr($str, $arr);
  119. }
  120. // 获取客户端IP地址
  121. function getClientIp()
  122. {
  123. if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  124. $ip = getenv("HTTP_CLIENT_IP");
  125. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
  126. $ip = getenv("HTTP_X_FORWARDED_FOR");
  127. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  128. $ip = getenv("REMOTE_ADDR");
  129. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  130. $ip = $_SERVER['REMOTE_ADDR'];
  131. else
  132. $ip = "unknown";
  133. return($ip);
  134. }
  135. /**
  136. +----------------------------------------------------------
  137. * 字符串截取,支持中文和其他编码
  138. +----------------------------------------------------------
  139. * @static
  140. * @access public
  141. +----------------------------------------------------------
  142. * @param string $str 需要转换的字符串
  143. * @param string $start 开始位置
  144. * @param string $length 截取长度
  145. * @param string $charset 编码格式
  146. * @param string $suffix 截断显示字符
  147. +----------------------------------------------------------
  148. * @return string
  149. +----------------------------------------------------------
  150. */
  151. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
  152. {
  153. if(function_exists("mb_substr"))
  154. {
  155. if ($suffix && strlen($str)>$length)
  156. return mb_substr($str, $start, $length, $charset)."...";
  157. else
  158. return mb_substr($str, $start, $length, $charset);
  159. }
  160. elseif(function_exists('iconv_substr'))
  161. {
  162. if ($suffix && strlen($str)>$length)
  163. return iconv_substr($str,$start,$length,$charset)."...";
  164. else
  165. return iconv_substr($str,$start,$length,$charset);
  166. }
  167. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  168. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  169. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  170. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  171. preg_match_all($re[$charset], $str, $match);
  172. $slice = join("",array_slice($match[0], $start, $length));
  173. if($suffix)
  174. return $slice."…";
  175. return $slice;
  176. }
  177. /**
  178. * 获得当前格林威治时间的时间戳
  179. *
  180. * @return integer
  181. */
  182. function gmtTime()
  183. {
  184. return (time() - date('Z'));
  185. }
  186. function toDate($time,$format='Y-m-d H:i:s')
  187. {
  188. if( empty($time))
  189. return '';
  190. $format = str_replace('#',':',$format);
  191. $time_zone = intval(fanweC('TIME_ZONE'));
  192. $time = $time + $time_zone * 3600;
  193. return date($format,$time);
  194. }
  195. function strZTime($str)
  196. {
  197. $str = trim($str);
  198. if(empty($str))
  199. return 0;
  200. $time_zone = intval(fanweC('TIME_ZONE'));
  201. $time = strtotime($str) - $time_zone * 3600;
  202. return $time;
  203. }
  204. function getStatusImg($status)
  205. {
  206. $status = intval($status);
  207. return '<img status="'.$status.'" src="'.APP_TMPL_PATH.'Static/Images/status-'.$status.'.gif" />';
  208. }
  209. function clearCache()
  210. {
  211. Dir::delDir(FANWE_ROOT.'./public/data/admin/runtime');
  212. @mkdir(FANWE_ROOT.'./public/data/admin/runtime', 0777);
  213. @chmod(FANWE_ROOT.'./public/data/admin/runtime', 0777);
  214. }
  215. function request($url, $post = '', $timeout = 15)
  216. {
  217. $context = array();
  218. if(is_array($post))
  219. $post = requestData($post);
  220. $context['http'] = array
  221. (
  222. 'timeout' => $timeout,
  223. 'method' => 'POST',
  224. 'header'=>"Content-Type: application/x-www-form-urlencoded\r\n".
  225. "Content-Length: ".strlen($post)."\r\n".
  226. "Connection: Close\r\n".
  227. "Cache-Control: no-cache\r\n",
  228. 'content' => $post,
  229. );
  230. return file_get_contents($url, false, stream_context_create($context));
  231. }
  232. function requestData($arg='')
  233. {
  234. $s = $sep = '';
  235. foreach($arg as $k => $v)
  236. {
  237. $k = urlencode($k);
  238. if(is_array($v))
  239. {
  240. $s2 = $sep2 = '';
  241. foreach($v as $k2 => $v2)
  242. {
  243. $k2 = urlencode($k2);
  244. $s2 .= "$sep2{$k}[$k2]=".urlencode(stripslashesDeep($v2));
  245. $sep2 = '&';
  246. }
  247. $s .= $sep.$s2;
  248. }
  249. else
  250. {
  251. $s .= "$sep$k=".urlencode(stripslashesDeep($v));
  252. }
  253. $sep = '&';
  254. }
  255. return $s;
  256. }
  257. if(!function_exists("mysqlLikeQuote"))
  258. {
  259. /**
  260. * 对 MYSQL LIKE 的内容进行转义
  261. *
  262. * @access public
  263. * @param string string 内容
  264. * @return string
  265. */
  266. function mysqlLikeQuote($str)
  267. {
  268. return strtr($str, array("\\\\" => "\\\\\\\\", '_' => '\_', '%' => '\%', "\'" => "\\\\\'"));
  269. }
  270. }
  271. function createIN($item_list, $field_name = '')
  272. {
  273. if (empty($item_list))
  274. {
  275. return $field_name . " IN ('') ";
  276. }
  277. else
  278. {
  279. if (! is_array($item_list))
  280. {
  281. $item_list = explode(',', $item_list);
  282. }
  283. $item_list = array_unique($item_list);
  284. $item_list_tmp = '';
  285. foreach ($item_list as $item)
  286. {
  287. if ($item !== '')
  288. {
  289. $item_list_tmp .= $item_list_tmp ? ",'$item'" : "'$item'";
  290. }
  291. }
  292. if (empty($item_list_tmp))
  293. {
  294. return $field_name . " IN ('') ";
  295. }
  296. else
  297. {
  298. return $field_name . ' IN (' . $item_list_tmp . ') ';
  299. }
  300. }
  301. }
  302. function getRelateShare($share_id)
  303. {
  304. $share_data = M("Share")->getByShareId($share_id);
  305. if($share_data)
  306. return "<a href='".u("Share/edit",array("share_id"=>$share_id))."' target='_blank'>".l("RELATE_SHARE")."</a>";
  307. else
  308. return "<span style='text-decoration:line-through;'>".l("SHARE_DELETE")."</span>";
  309. }
  310. function getLang($key,$file)
  311. {
  312. if(!empty($file))
  313. L(include LANG_PATH . FANWE_LANG_SET . '/'.$file.'.php');
  314. return L($key);
  315. }
  316. function echoFlush($str)
  317. {
  318. echo str_repeat(' ',4096);
  319. echo $str;
  320. }
  321. /**
  322. * utf8字符转Unicode字符
  323. * @param string $char 要转换的单字符
  324. * @return void
  325. */
  326. function utf8ToUnicodeA($char)
  327. {
  328. switch(strlen($char))
  329. {
  330. case 1:
  331. return ord($char);
  332. case 2:
  333. $n = (ord($char[0]) & 0x3f) << 6;
  334. $n += ord($char[1]) & 0x3f;
  335. return $n;
  336. case 3:
  337. $n = (ord($char[0]) & 0x1f) << 12;
  338. $n += (ord($char[1]) & 0x3f) << 6;
  339. $n += ord($char[2]) & 0x3f;
  340. return $n;
  341. case 4:
  342. $n = (ord($char[0]) & 0x0f) << 18;
  343. $n += (ord($char[1]) & 0x3f) << 12;
  344. $n += (ord($char[2]) & 0x3f) << 6;
  345. $n += ord($char[3]) & 0x3f;
  346. return $n;
  347. }
  348. }
  349. /**
  350. * utf8字符串分隔为unicode字符串
  351. * @param string $str 要转换的字符串
  352. * @param string $pre
  353. * @return string
  354. */
  355. function segmentToUnicodeA($str,$pre = '')
  356. {
  357. $arr = array();
  358. $str_len = mb_strlen($str,'UTF-8');
  359. for($i = 0;$i < $str_len;$i++)
  360. {
  361. $s = mb_substr($str,$i,1,'UTF-8');
  362. if($s != ' ' && $s != ' ')
  363. {
  364. $arr[] = $pre.'ux'.utf8ToUnicodeA($s);
  365. }
  366. }
  367. $arr = array_unique($arr);
  368. return implode(' ',$arr);
  369. }
  370. /**
  371. * 清除符号
  372. * @param string $str 要清除符号的字符串
  373. * @return string
  374. */
  375. function clearSymbolA($str)
  376. {
  377. static $symbols = NULL;
  378. if($symbols === NULL)
  379. {
  380. $symbols = file_get_contents(FANWE_ROOT.'public/table/symbol.table');
  381. $symbols = explode("\r\n",$symbols);
  382. }
  383. return str_replace($symbols,"",$str);
  384. }
  385. function getRefUrl()
  386. {
  387. $ref_url = $_SERVER['HTTP_REFERER'];
  388. $url = $_SERVER['REQUEST_URI'];
  389. echo $ref_url.'<br/>';
  390. echo $url.'<br/>';
  391. }
  392. function getAllFiles ($path)
  393. {
  394. $list = array();
  395. foreach (glob($path . '/*') as $item)
  396. {
  397. if (is_dir($item))
  398. {
  399. $list = array_merge($list, getAllFiles($item));
  400. }
  401. else
  402. {
  403. //if(eregi(".php",$item)){}//这里可以增加判断文件名或其他。changed by:edlongren
  404. $list[] = $item;
  405. }
  406. }
  407. return $list;
  408. }
  409. ?>