global.func.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. <?php
  2. class Load {
  3. static function controller($name) {
  4. return @include_once (ROOT_PATH . 'protected/controller/' . $name . '.php');
  5. }
  6. static function logic($name) {
  7. return @include_once (ROOT_PATH . 'protected/logic/' . $name . 'Logic.php');
  8. }
  9. static function lib($name) {
  10. return @include_once (ROOT_PATH . 'protected/plugin/' . $name . '.han.php');
  11. }
  12. static function dao($name) {
  13. return @include_once (ROOT_PATH . 'protected/model/' . $name . '.Dao.php');
  14. }
  15. }
  16. /**
  17. * 获取时间戳,day获取几天后的时间戳
  18. */
  19. function get_date($day = 0) {
  20. if ($day != 0)
  21. return strtotime ( "+" . $day . " day" );
  22. return strtotime ( "now" );
  23. }
  24. /**
  25. * 获取几个小时后的时间戳
  26. * @param unknown_type $hours
  27. */
  28. function get_hours($hours = 0) {
  29. if ($hours != 0)
  30. return strtotime ( "+" . $hours . " hours" );
  31. return strtotime ( "now" );
  32. }
  33. /**
  34. * 获取几个分钟后的时间戳
  35. * @param unknown_type $hours
  36. */
  37. function get_seconds($seconds = 0) {
  38. if ($seconds != 0)
  39. return strtotime ( "+" . $seconds . " seconds" );
  40. return strtotime ( "now" );
  41. }
  42. /**
  43. * 获取时间戳
  44. */
  45. function get_time($time = 0) {
  46. return strtotime ( $time );
  47. }
  48. /**
  49. * 格式化时间为年月日时分秒
  50. * @param unknown_type $strtotime
  51. */
  52. function format_date($strtotime = 0, $format = "Y-m-d H:i:s") {
  53. return date ( $format, $strtotime );
  54. }
  55. /**
  56. * 获取当天开始时间和结束时间
  57. */
  58. function get_today() {
  59. $t = time ();
  60. $start = mktime ( 0, 0, 0, date ( "m", $t ), date ( "d", $t ), date ( "Y", $t ) );
  61. $end = mktime ( 23, 59, 59, date ( "m", $t ), date ( "d", $t ), date ( "Y", $t ) );
  62. return array ('start' => $start, 'end' => $end );
  63. }
  64. /**
  65. * 格式化问题状态
  66. * @param unknown_type $status
  67. */
  68. function format_question_status($status = 1, $type = "index") {
  69. if ($type == "index") {
  70. if (1 == $status)
  71. return "待解决";
  72. elseif (2 == $status)
  73. return "<span class='queState colGreen'>已解决</span>";
  74. elseif (9 == $status)
  75. return "<span class='queState colRed'>已关闭</span>";
  76. elseif (6 == $status)
  77. return "推荐问题";
  78. } elseif ($type == "page") {
  79. if (1 == $status)
  80. return "<b class='extraIcon iconNotDone'></b>待解决";
  81. elseif (2 == $status)
  82. return "<b class='extraIcon iconDone'></b><span class='queState colGreen'>已解决</span>";
  83. elseif (9 == $status)
  84. return "<b class='extraIcon iconStop'></b><span class='queState colRed'>已关闭</span>";
  85. elseif (6 == $status)
  86. return "推荐问题";
  87. } elseif ($type == "vip") {
  88. if (1 == $status)
  89. return "未提问";
  90. elseif (2 == $status)
  91. return "已提问";
  92. elseif (3 == $status)
  93. return "满意";
  94. elseif (4 == $status)
  95. return "不满意";
  96. } else {
  97. if (1 == $status)
  98. return "<b class='extraIcon iconNotDone'></b>";
  99. elseif (2 == $status)
  100. return "<b class='extraIcon iconDone'></b>";
  101. elseif (9 == $status)
  102. return "<b class='extraIcon iconStop'></b>";
  103. elseif (6 == $status)
  104. return "推荐问题";
  105. }
  106. }
  107. /**
  108. * 获取客户端IP地址
  109. */
  110. function client_ip() {
  111. if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
  112. $onlineip = getenv ( 'HTTP_CLIENT_IP' );
  113. } elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
  114. $onlineip = getenv ( 'HTTP_X_FORWARDED_FOR' );
  115. } elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
  116. $onlineip = getenv ( 'REMOTE_ADDR' );
  117. } elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
  118. $onlineip = $_SERVER ['REMOTE_ADDR'];
  119. }
  120. preg_match ( '/[\d\.]{7,15}/', $onlineip, $onlineipmatches );
  121. $onlineip = ($onlineipmatches [0] ? $onlineipmatches [0] : 'unknown');
  122. return $onlineip;
  123. }
  124. /**
  125. * random 获取字符串
  126. * @param int $length
  127. * @return string $hash
  128. */
  129. function random($length = 6, $type = 0) {
  130. $hash = '';
  131. $chararr = array ('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', '0123456789', '23456789ABCDEFGHJKLMNPQRSTUVWXYZ' );
  132. $chars = $chararr [$type];
  133. $max = strlen ( $chars ) - 1;
  134. PHP_VERSION < '4.2.0' && mt_srand ( ( double ) microtime () * 1000000 );
  135. for($i = 0; $i < $length; $i ++) {
  136. $hash .= $chars [mt_rand ( 0, $max )];
  137. }
  138. return $hash;
  139. }
  140. /**
  141. * 获取用户等级
  142. */
  143. function get_lv($credit = 0) {
  144. $lv = array ();
  145. if (0 <= $credit && $credit <= 80) {
  146. array_push ( $lv, 1 );
  147. array_push ( $lv, 80 );
  148. return $lv;
  149. } elseif (81 <= $credit && $credit <= 400) {
  150. array_push ( $lv, 2 );
  151. array_push ( $lv, 400 );
  152. return $lv;
  153. } elseif (401 <= $credit && $credit <= 800) {
  154. array_push ( $lv, 3 );
  155. array_push ( $lv, 800 );
  156. return $lv;
  157. } elseif (801 <= $credit && $credit <= 2000) {
  158. array_push ( $lv, 4 );
  159. array_push ( $lv, 2000 );
  160. return $lv;
  161. } elseif (2001 <= $credit && $credit <= 4000) {
  162. array_push ( $lv, 5 );
  163. array_push ( $lv, 4000 );
  164. return $lv;
  165. } elseif (4001 <= $credit && $credit <= 7000) {
  166. array_push ( $lv, 6 );
  167. array_push ( $lv, 7000 );
  168. return $lv;
  169. } elseif (7001 <= $credit && $credit <= 10000) {
  170. array_push ( $lv, 7 );
  171. array_push ( $lv, 10000 );
  172. return $lv;
  173. } elseif (10001 <= $credit && $credit <= 14000) {
  174. array_push ( $lv, 8 );
  175. array_push ( $lv, 14000 );
  176. return $lv;
  177. } elseif (14001 <= $credit && $credit <= 18000) {
  178. array_push ( $lv, 9 );
  179. array_push ( $lv, 18000 );
  180. return $lv;
  181. } elseif (18001 <= $credit && $credit <= 22000) {
  182. array_push ( $lv, 10 );
  183. array_push ( $lv, 22000 );
  184. return $lv;
  185. } elseif (22001 <= $credit && $credit <= 32000) {
  186. array_push ( $lv, 11 );
  187. array_push ( $lv, 32000 );
  188. return $lv;
  189. } elseif (32001 <= $credit && $credit <= 45000) {
  190. array_push ( $lv, 12 );
  191. array_push ( $lv, 45000 );
  192. return $lv;
  193. } elseif (45001 <= $credit && $credit <= 60000) {
  194. array_push ( $lv, 13 );
  195. array_push ( $lv, 60000 );
  196. return $lv;
  197. } elseif (60001 <= $credit && $credit <= 100000) {
  198. array_push ( $lv, 14 );
  199. array_push ( $lv, 100000 );
  200. return $lv;
  201. } elseif (100001 <= $credit && $credit <= 150000) {
  202. array_push ( $lv, 15 );
  203. array_push ( $lv, 150000 );
  204. return $lv;
  205. } elseif (150001 <= $credit && $credit <= 250000) {
  206. array_push ( $lv, 16 );
  207. array_push ( $lv, 250000 );
  208. return $lv;
  209. } elseif (250001 <= $credit && $credit <= 400000) {
  210. array_push ( $lv, 17 );
  211. array_push ( $lv, 400000 );
  212. return $lv;
  213. } elseif (400001 <= $credit && $credit <= 700000) {
  214. array_push ( $lv, 18 );
  215. array_push ( $lv, 8700000 );
  216. return $lv;
  217. } elseif (700001 <= $credit && $credit <= 1000000) {
  218. array_push ( $lv, 19 );
  219. array_push ( $lv, 1000000 );
  220. return $lv;
  221. } elseif (1000000 <= $credit && $credit <= 9999999) {
  222. array_push ( $lv, 20 );
  223. array_push ( $lv, 9999999 );
  224. return $lv;
  225. }
  226. }
  227. /**
  228. * 多少天以前
  229. */
  230. function day_ago($day = 7) {
  231. $now = strtotime ( "now" );
  232. }
  233. /**
  234. * 获取天数差
  235. * @param unknown_type $nowtime 现在时间
  236. * @param unknown_type $future 过去时间
  237. */
  238. function count_days($nowtime, $futuretime) {
  239. $a_dt = getdate ( $nowtime );
  240. $b_dt = getdate ( $futuretime );
  241. $a_new = mktime ( 12, 0, 0, $a_dt ['mon'], $a_dt ['mday'], $a_dt ['year'] );
  242. $b_new = mktime ( 12, 0, 0, $b_dt ['mon'], $b_dt ['mday'], $b_dt ['year'] );
  243. return round ( abs ( $a_new - $b_new ) / 86400 );
  244. }
  245. /**
  246. * 获取最后关闭时间
  247. */
  248. function get_last_close_day($sortime = 0, $period = 7) {
  249. $result = count_days ( strtotime ( "now" ), $sortime );
  250. $day = ($period - $result) <= 0 ? 0 : ($period - $result);
  251. return $day;
  252. }
  253. function my_date_format($timestamp, $format = "Y-m-d H:i:s") {
  254. $SystemConfig = ConfigHandler::get ();
  255. $timezone = $SystemConfig ['timezone'];
  256. return gmdate ( $format, ($timestamp + $timezone * 3600) );
  257. }
  258. //格式化时间函数
  259. function my_date_format2($time = 0, $format = 'Y-m-d') {
  260. $time = isset ( $time ) ? $time : 0;
  261. $now = time ();
  262. $t = $now - $time;
  263. if ($t < 0) {
  264. $time = format_date ( $time, $format );
  265. return $time;
  266. }
  267. if ($t >= 3600) {
  268. $time = format_date ( $time, $format );
  269. }
  270. elseif ($t < 3600 && $t >= 60) {
  271. $time = floor ( $t / 60 ) . "分钟前";
  272. } else {
  273. $time = "刚刚";
  274. }
  275. return $time;
  276. }
  277. /**
  278. * 计算剩下时间2天
  279. * @param unknown_type $time 剩下值
  280. */
  281. function do_vip_answer_time($time = 0) {
  282. if ($time <= 0) {
  283. return "超过回答时间";
  284. }
  285. if ($time <= 432000 && $time >= 86400) {
  286. $day = floor ( $time / 86400 );
  287. $time = $time - ($day * 86400);
  288. if ($time <= 0)
  289. return $day . "天";
  290. $t = round ( $time / 3600 );
  291. return $day . "天" . $t . "小时";
  292. }
  293. if ($time < 86400 && $time >= 3600) {
  294. $t = floor ( $time / 3600 );
  295. return $t . "小时";
  296. }
  297. if ($time < 3600 && $time > 0) {
  298. $t = floor ( $time / 60 );
  299. return $t . "分钟";
  300. }
  301. }
  302. /**
  303. * 获取文件后缀名
  304. * @param unknown_type $filename
  305. */
  306. function _GetFileEXT($filename) {
  307. $pics = explode ( '.', $filename );
  308. $num = count ( $pics );
  309. return $pics [$num - 1];
  310. }
  311. /**
  312. * 获取上一页
  313. * @param unknown_type $on_page
  314. */
  315. function get_previous($on_page = 1) {
  316. return $on_page != 0 ? $on_page - 1 : $on_page;
  317. }
  318. /**
  319. * 加密或解密指定字符串
  320. * @param string $string 要加密或解密的字符串
  321. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  322. * @param string $key 加解密的key
  323. * @param $expiry 超时值
  324. * */
  325. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  326. $ckey_length = 4;
  327. if (! $key) {
  328. $key = AUTH;
  329. }
  330. $key = md5 ( $key );
  331. $keya = md5 ( substr ( $key, 0, 16 ) );
  332. $keyb = md5 ( substr ( $key, 16, 16 ) );
  333. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  334. $cryptkey = $keya . md5 ( $keya . $keyc );
  335. $key_length = strlen ( $cryptkey );
  336. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  337. $string_length = strlen ( $string );
  338. $result = '';
  339. $box = range ( 0, 255 );
  340. $rndkey = array ();
  341. for($i = 0; $i <= 255; $i ++) {
  342. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  343. }
  344. for($j = $i = 0; $i < 256; $i ++) {
  345. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  346. $tmp = $box [$i];
  347. $box [$i] = $box [$j];
  348. $box [$j] = $tmp;
  349. }
  350. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  351. $a = ($a + 1) % 256;
  352. $j = ($j + $box [$a]) % 256;
  353. $tmp = $box [$a];
  354. $box [$a] = $box [$j];
  355. $box [$j] = $tmp;
  356. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  357. }
  358. if ($operation == 'DECODE') {
  359. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  360. return substr ( $result, 26 );
  361. } else {
  362. return '';
  363. }
  364. } else {
  365. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  366. }
  367. }
  368. /**
  369. * 将一个字符串写入文件
  370. * @param string $filename 文件名
  371. * @param string $data 要写入文件的内容
  372. * @param mixed $flag 文件的写入标识,如果为FILE_APPEND或'FILE_APPEND'表示追加,否则为新建
  373. * @return int 返回写入文件的字节数
  374. * */
  375. function files_put_contents($filename, $data, $flag = false) {
  376. $mode = ($flag == FILE_APPEND || strtoupper ( $flag ) == 'FILE_APPEND') ? 'ab' : 'wb';
  377. $f = @fopen ( $filename, $mode );
  378. if ($f === false) {
  379. return 0;
  380. } else {
  381. if (is_array ( $data )) {
  382. $data = implode ( '', $data );
  383. }
  384. $bytes_written = @fwrite ( $f, $data );
  385. @fclose ( $f );
  386. return $bytes_written;
  387. }
  388. }
  389. function cut_str($string, $length, $dot = ' ...') {
  390. if (strlen ( $string ) <= $length) {
  391. return $string;
  392. }
  393. $strcut = '';
  394. $sys_config = "utf-8";
  395. if (strtolower ( $sys_config ) == 'utf-8') {
  396. $n = $tn = $noc = 0;
  397. while ( $n < strlen ( $string ) ) {
  398. $t = ord ( $string [$n] );
  399. if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  400. $tn = 1;
  401. $n ++;
  402. $noc ++;
  403. } elseif (194 <= $t && $t <= 223) {
  404. $tn = 2;
  405. $n += 2;
  406. $noc += 2;
  407. } elseif (224 <= $t && $t < 239) {
  408. $tn = 3;
  409. $n += 3;
  410. $noc += 2;
  411. } elseif (240 <= $t && $t <= 247) {
  412. $tn = 4;
  413. $n += 4;
  414. $noc += 2;
  415. } elseif (248 <= $t && $t <= 251) {
  416. $tn = 5;
  417. $n += 5;
  418. $noc += 2;
  419. } elseif ($t == 252 || $t == 253) {
  420. $tn = 6;
  421. $n += 6;
  422. $noc += 2;
  423. } else {
  424. $n ++;
  425. }
  426. if ($noc >= $length) {
  427. break;
  428. }
  429. }
  430. if ($noc > $length) {
  431. $n -= $tn;
  432. }
  433. $strcut = substr ( $string, 0, $n );
  434. } else {
  435. for($i = 0; $i < $length; $i ++) {
  436. $strcut .= ord ( $string [$i] ) > 127 ? $string [$i] . $string [++ $i] : $string [$i];
  437. }
  438. }
  439. return $strcut . $dot;
  440. }
  441. function cutstr($string, $length, $dot = '') {
  442. Return cut_str ( $string, $length, $dot );
  443. }
  444. ;
  445. ?>