global.func.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. class Load {
  3. static function controller($name) {
  4. return include (ROOT_PATH . 'protected/controller/' . $name . '.php');
  5. }
  6. static function logic($name) {
  7. return include ('protected/logic/' . $name . 'Logic.php');
  8. }
  9. static function lib($name) {
  10. return include (ROOT_PATH . 'protected/class/' . $name . '.han.php');
  11. }
  12. static function dao($name) {
  13. return include (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. function getTrainTimeByStatus($date=""){
  25. $date=explode(":", $date);
  26. $today=date("Y-m-d");
  27. if($today>date("Y-m-d",strtotime($date[1]))){
  28. return "end";
  29. }elseif($today>=date("Y-m-d",strtotime($date[0]))&&$today<=date("Y-m-d",strtotime($date[1]))){
  30. return "doing";
  31. }elseif($today<date("Y-m-d",strtotime($date[0]))){
  32. return "apply";
  33. }else{
  34. "已结束<br/>".$date[1];
  35. }
  36. }
  37. function getTrainTime($date=""){
  38. $date=explode(":", $date);
  39. $today=date("Y-m-d");
  40. if($today>date("Y-m-d",strtotime($date[1]))){
  41. return "<p class='tData'>结束 ".$date[1]."</p>";
  42. }elseif($today>=date("Y-m-d",strtotime($date[0]))&&$today<=date("Y-m-d",strtotime($date[1]))){
  43. return "<span class='tIcon'>进行中</span>";
  44. }elseif($today<date("Y-m-d",strtotime($date[0]))){
  45. $time = strtotime($date[0]) - 60*60*24;
  46. return "<span class='tIcon'>报名中</span><p class='tData'>截止".date('Y-m-d',$time)."</p>";
  47. }else{
  48. "已结束<br/>".$date[1];
  49. }
  50. }
  51. function getTrainTime2($date=""){
  52. $date=explode(":", $date);
  53. $today=date("Y-m-d");
  54. if($today>date("Y-m-d",strtotime($date[1]))){
  55. return "<p class='tData'>结束 ".$date[1]."</p>";
  56. }elseif($today>=date("Y-m-d",strtotime($date[0]))&&$today<=date("Y-m-d",strtotime($date[1]))){
  57. return "";
  58. //return "<span class='tIcon'>进行中</span>";
  59. }elseif($today<date("Y-m-d",strtotime($date[0]))){
  60. $time = strtotime($date[0]) - 60*60*24;
  61. return "<p class='tData'>截止".date('Y-m-d',$time)."</p>";
  62. }else{
  63. "结束<br/>".$date[1];
  64. }
  65. }
  66. function getTrainTime3($date=""){
  67. $date=explode(":", $date);
  68. $today=date("Y-m-d");
  69. if($today>date("Y-m-d",strtotime($date[1]))){
  70. return '<span class="colGray">已结束</span>&nbsp;'.$date[1];
  71. }elseif($today>=date("Y-m-d",strtotime($date[0]))&&$today<=date("Y-m-d",strtotime($date[1]))){
  72. return '<span class="colGreen">正在进行</span>';
  73. }elseif($today<date("Y-m-d",strtotime($date[0]))){
  74. $time = strtotime($date[0]) - 60*60*24;
  75. return '<span class="colOrange">报名中</span>&nbsp;截止&nbsp;'.$date[1];
  76. }else{
  77. return '<span class="colGray">已结束</span>&nbsp;'.$date[1];
  78. }
  79. }
  80. function getTrainTimeTk($date=""){
  81. $date=explode(":", $date);
  82. $today=date("Y-m-d");
  83. if($today>date("Y-m-d",strtotime($date[1]))){
  84. return "结束 ".$date[1];
  85. }elseif($today>=date("Y-m-d",strtotime($date[0]))&&$today<=date("Y-m-d",strtotime($date[1]))){
  86. return "进行中";
  87. }elseif($today<date("Y-m-d",strtotime($date[0]))){
  88. $time = strtotime($date[0]) - 60*60*24;
  89. return "<b class='colOrange'>报名中</b>&nbsp;截止".date('Y-m-d',$time);
  90. }else{
  91. "已结束 &nbsp;".$date[1];
  92. }
  93. }
  94. function getColumnType($c_type=1){
  95. if($c_type==1)
  96. return "系统";
  97. elseif ($c_type==2)
  98. return "链接";
  99. elseif ($c_type==3)
  100. return "介绍";
  101. }
  102. /**
  103. * 获取上一页
  104. * @param unknown_type $on_page
  105. */
  106. function get_previous($on_page = 1) {
  107. return $on_page != 0 ? $on_page - 1 : $on_page;
  108. }
  109. /**
  110. * 获取几个小时后的时间戳
  111. * @param unknown_type $hours
  112. */
  113. function get_hours($hours = 0) {
  114. if ($hours != 0)
  115. return strtotime ( "+" . $hours . " hours" );
  116. return strtotime ( "now" );
  117. }
  118. /**
  119. * 获取几个分钟后的时间戳
  120. * @param unknown_type $hours
  121. */
  122. function get_seconds($seconds = 0) {
  123. if ($seconds != 0)
  124. return strtotime ( "+" . $seconds . " seconds" );
  125. return strtotime ( "now" );
  126. }
  127. /**
  128. * 获取时间戳
  129. */
  130. function get_time($time = 0) {
  131. return strtotime ( $time );
  132. }
  133. /**
  134. * 格式化时间为年月日时分秒
  135. * @param unknown_type $strtotime
  136. */
  137. function format_time($strtotime = 0, $format = "Y-m-d H:i:s") {
  138. return date ( $format, $strtotime );
  139. }
  140. /**
  141. * 获取客户端IP地址
  142. */
  143. function client_ip() {
  144. if (getenv ( 'HTTP_CLIENT_IP' ) && strcasecmp ( getenv ( 'HTTP_CLIENT_IP' ), 'unknown' )) {
  145. $onlineip = getenv ( 'HTTP_CLIENT_IP' );
  146. } elseif (getenv ( 'HTTP_X_FORWARDED_FOR' ) && strcasecmp ( getenv ( 'HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
  147. $onlineip = getenv ( 'HTTP_X_FORWARDED_FOR' );
  148. } elseif (getenv ( 'REMOTE_ADDR' ) && strcasecmp ( getenv ( 'REMOTE_ADDR' ), 'unknown' )) {
  149. $onlineip = getenv ( 'REMOTE_ADDR' );
  150. } elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], 'unknown' )) {
  151. $onlineip = $_SERVER ['REMOTE_ADDR'];
  152. }
  153. preg_match ( '/[\d\.]{7,15}/', $onlineip, $onlineipmatches );
  154. $onlineip = ($onlineipmatches [0] ? $onlineipmatches [0] : 'unknown');
  155. return $onlineip;
  156. }
  157. /**
  158. * random 获取字符串
  159. * @param int $length
  160. * @return string $hash
  161. */
  162. function random($length = 6, $type = 0) {
  163. $hash = '';
  164. $chararr = array ('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz', '0123456789', '23456789ABCDEFGHJKLMNPQRSTUVWXYZ' );
  165. $chars = $chararr [$type];
  166. $max = strlen ( $chars ) - 1;
  167. PHP_VERSION < '4.2.0' && mt_srand ( ( double ) microtime () * 1000000 );
  168. for($i = 0; $i < $length; $i ++) {
  169. $hash .= $chars [mt_rand ( 0, $max )];
  170. }
  171. return $hash;
  172. }
  173. /**
  174. * 多少天以前
  175. */
  176. function day_ago($day = 7) {
  177. $now = strtotime ( "now" );
  178. }
  179. /**
  180. * 获取天数差
  181. * @param unknown_type $nowtime 现在时间
  182. * @param unknown_type $future 过去时间
  183. */
  184. function count_days($nowtime, $futuretime) {
  185. $a_dt = getdate ( $nowtime );
  186. $b_dt = getdate ( $futuretime );
  187. $a_new = mktime ( 12, 0, 0, $a_dt ['mon'], $a_dt ['mday'], $a_dt ['year'] );
  188. $b_new = mktime ( 12, 0, 0, $b_dt ['mon'], $b_dt ['mday'], $b_dt ['year'] );
  189. return round ( abs ( $a_new - $b_new ) / 86400 );
  190. }
  191. function my_date_format($timestamp, $format = "Y-m-d H:i:s") {
  192. $SystemConfig = ConfigHandler::get ();
  193. $timezone = $SystemConfig ['timezone'];
  194. return gmdate ( $format, ($timestamp + $timezone * 3600) );
  195. }
  196. //格式化时间函数
  197. function my_date_format2($time = 0, $format = 'Y-m-d') {
  198. $time = isset ( $time ) ? $time : 0;
  199. $now = time ();
  200. $t = $now - $time;
  201. if ($t < 0) {
  202. $time = format_date ( $time, $format );
  203. return $time;
  204. }
  205. if ($t >= 3600) {
  206. $time = format_date ( $time, $format );
  207. }
  208. elseif ($t < 3600 && $t >= 60) {
  209. $time = floor ( $t / 60 ) . "分钟前";
  210. } else {
  211. $time = "刚刚";
  212. }
  213. return $time;
  214. }
  215. /**
  216. * 获取文件后缀名
  217. * @param unknown_type $filename
  218. */
  219. function _GetFileEXT($filename) {
  220. $pics = explode ( '.', $filename );
  221. $num = count ( $pics );
  222. return $pics [$num - 1];
  223. }
  224. /**
  225. * 加密或解密指定字符串
  226. * @param string $string 要加密或解密的字符串
  227. * @param string $operation 当取值为'DECODE'时表示解密,否则为加密
  228. * @param string $key 加解密的key
  229. * @param $expiry 超时值
  230. * */
  231. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  232. $ckey_length = 4;
  233. if (! $key) {
  234. $key = AUTH;
  235. }
  236. $key = md5 ( $key );
  237. $keya = md5 ( substr ( $key, 0, 16 ) );
  238. $keyb = md5 ( substr ( $key, 16, 16 ) );
  239. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  240. $cryptkey = $keya . md5 ( $keya . $keyc );
  241. $key_length = strlen ( $cryptkey );
  242. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  243. $string_length = strlen ( $string );
  244. $result = '';
  245. $box = range ( 0, 255 );
  246. $rndkey = array ();
  247. for($i = 0; $i <= 255; $i ++) {
  248. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  249. }
  250. for($j = $i = 0; $i < 256; $i ++) {
  251. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  252. $tmp = $box [$i];
  253. $box [$i] = $box [$j];
  254. $box [$j] = $tmp;
  255. }
  256. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  257. $a = ($a + 1) % 256;
  258. $j = ($j + $box [$a]) % 256;
  259. $tmp = $box [$a];
  260. $box [$a] = $box [$j];
  261. $box [$j] = $tmp;
  262. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  263. }
  264. if ($operation == 'DECODE') {
  265. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  266. return substr ( $result, 26 );
  267. } else {
  268. return '';
  269. }
  270. } else {
  271. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  272. }
  273. }
  274. /**
  275. * 将一个字符串写入文件
  276. * @param string $filename 文件名
  277. * @param string $data 要写入文件的内容
  278. * @param mixed $flag 文件的写入标识,如果为FILE_APPEND或'FILE_APPEND'表示追加,否则为新建
  279. * @return int 返回写入文件的字节数
  280. * */
  281. function files_put_contents($filename, $data, $flag = false) {
  282. $mode = ($flag == FILE_APPEND || strtoupper ( $flag ) == 'FILE_APPEND') ? 'ab' : 'wb';
  283. $f = @fopen ( $filename, $mode );
  284. if ($f === false) {
  285. return 0;
  286. } else {
  287. if (is_array ( $data )) {
  288. $data = implode ( '', $data );
  289. }
  290. $bytes_written = @fwrite ( $f, $data );
  291. @fclose ( $f );
  292. return $bytes_written;
  293. }
  294. }
  295. function cut_str($string, $length, $dot = ' ...') {
  296. if (strlen ( $string ) <= $length) {
  297. return $string;
  298. }
  299. $strcut = '';
  300. $sys_config = "utf-8";
  301. if (strtolower ( $sys_config ) == 'utf-8') {
  302. $n = $tn = $noc = 0;
  303. while ( $n < strlen ( $string ) ) {
  304. $t = ord ( $string [$n] );
  305. if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  306. $tn = 1;
  307. $n ++;
  308. $noc ++;
  309. } elseif (194 <= $t && $t <= 223) {
  310. $tn = 2;
  311. $n += 2;
  312. $noc += 2;
  313. } elseif (224 <= $t && $t < 239) {
  314. $tn = 3;
  315. $n += 3;
  316. $noc += 2;
  317. } elseif (240 <= $t && $t <= 247) {
  318. $tn = 4;
  319. $n += 4;
  320. $noc += 2;
  321. } elseif (248 <= $t && $t <= 251) {
  322. $tn = 5;
  323. $n += 5;
  324. $noc += 2;
  325. } elseif ($t == 252 || $t == 253) {
  326. $tn = 6;
  327. $n += 6;
  328. $noc += 2;
  329. } else {
  330. $n ++;
  331. }
  332. if ($noc >= $length) {
  333. break;
  334. }
  335. }
  336. if ($noc > $length) {
  337. $n -= $tn;
  338. }
  339. $strcut = substr ( $string, 0, $n );
  340. } else {
  341. for($i = 0; $i < $length; $i ++) {
  342. $strcut .= ord ( $string [$i] ) > 127 ? $string [$i] . $string [++ $i] : $string [$i];
  343. }
  344. }
  345. return $strcut . $dot;
  346. }
  347. function cutstr($string, $length, $dot = '') {
  348. Return cut_str ( $string, $length, $dot );
  349. }
  350. ;
  351. ?>