global.func.php 13 KB

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