PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/include/extensions/utils/Fn.php

http://chenjin.googlecode.com/
PHP | 636 lines | 434 code | 64 blank | 138 comment | 92 complexity | 656ff24062342163b7efba9b8563d6eb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * ??????
  4. *
  5. * @author mole <mole.chen@foxmail.com>
  6. * @version $Id: Fn.php 244 2012-04-01 02:25:09Z mole1230 $
  7. */
  8. class Fn
  9. {
  10. /**
  11. * ??????????? mb_string ?????
  12. *
  13. * @param mixed $str ??
  14. * @param string $encoding ?????????
  15. * @return mixed ??????
  16. */
  17. public static function encodingConvert($str, $encoding = 'UTF-8')
  18. {
  19. if (is_array($str)) {
  20. $arr = array();
  21. foreach ($str as $key => $val) {
  22. $arr[$key] = self::encodingConvert($val, $encoding);
  23. }
  24. return $arr;
  25. }
  26. $_encoding = mb_detect_encoding($str, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
  27. if ($_encoding == $encoding) {
  28. return $str;
  29. }
  30. return mb_convert_encoding($str, $encoding, $_encoding);
  31. }
  32. /**
  33. * ????????
  34. *
  35. * @param string $string
  36. * @param string $key
  37. * @param string $operation encode|decode
  38. * @return string
  39. */
  40. public static function crypt($string, $key, $operation = 'encode')
  41. {
  42. $keyLength = strlen($key);
  43. $string = (strtolower($operation) == 'decode') ? base64_decode($string) : substr(md5($string . $key) , 0, 8) . $string;
  44. $stringLength = strlen($string);
  45. $rndkey = $box = array();
  46. $result = '';
  47. for ($i = 0; $i <= 255; $i++) {
  48. $rndkey[$i] = ord($key[$i % $keyLength]);
  49. $box[$i] = $i;
  50. }
  51. for ($j = $i = 0; $i < 256; $i++) {
  52. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  53. $tmp = $box[$i];
  54. $box[$i] = $box[$j];
  55. $box[$j] = $tmp;
  56. }
  57. for ($a = $j = $i = 0; $i < $stringLength; $i++) {
  58. $a = ($a + 1) % 256;
  59. $j = ($j + $box[$a]) % 256;
  60. $tmp = $box[$a];
  61. $box[$a] = $box[$j];
  62. $box[$j] = $tmp;
  63. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  64. }
  65. if (strtolower($operation) == 'decode') {
  66. if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key) , 0, 8)) {
  67. return substr($result, 8);
  68. } else {
  69. return '';
  70. }
  71. } else {
  72. return base64_encode($result);
  73. }
  74. }
  75. /**
  76. * ??IP?????????IP???
  77. *
  78. * @return string
  79. */
  80. public static function getIp()
  81. {
  82. static $ip = false;
  83. if (false != $ip) {
  84. return $ip;
  85. }
  86. $keys = array(
  87. 'HTTP_CLIENT_IP',
  88. 'HTTP_X_FORWARDED_FOR',
  89. 'HTTP_X_FORWARDED',
  90. 'HTTP_FORWARDED_FOR',
  91. 'HTTP_FORWARDED'
  92. );
  93. foreach ($keys as $item) {
  94. if (!isset($_SERVER[$item])) {
  95. continue;
  96. }
  97. $curIp = $_SERVER[$item];
  98. $curIp = explode('.', $curIp);
  99. if (count($curIp) != 4) {
  100. continue;
  101. }
  102. foreach ($curIp as & $sub) {
  103. if (($sub = intval($sub)) < 0 || $sub > 255) {
  104. continue 2;
  105. }
  106. }
  107. return $ip = implode('.', $curIp);
  108. }
  109. return $ip = $_SERVER['REMOTE_ADDR'];
  110. }
  111. /**
  112. * ?byte????????????????? byte?
  113. *
  114. * <code>
  115. * echo formatBytes('1M'); // 1048576
  116. * echo formatBytes('1048576'); // 1M
  117. * </code>
  118. *
  119. * @param int|string $bytes
  120. * @return string|int
  121. */
  122. public static function formatBytes($bytes)
  123. {
  124. if (is_numeric($bytes)) {
  125. if ($bytes >= 1073741824) {
  126. $bytes = round($bytes / 1073741824 * 100) / 100 . 'GB';
  127. } elseif ($bytes >= 1048576) {
  128. $bytes = round($bytes / 1048576 * 100) / 100 . 'MB';
  129. } elseif ($bytes >= 1024) {
  130. $bytes = round($bytes / 1024 * 100) / 100 . 'KB';
  131. } else {
  132. $bytes = $bytes . 'Bytes';
  133. }
  134. } else {
  135. $match = array();
  136. if (preg_match('/((?P<type>G|M|K|GB|MB|KB))/i', $bytes, $match)) {
  137. $bytes = floatval($bytes);
  138. switch (strtoupper($match['type'])) {
  139. case 'G':
  140. case 'GB':
  141. $bytes *= 1073741824;
  142. break;
  143. case 'M':
  144. case 'MB':
  145. $bytes *= 1048576;
  146. break;
  147. case 'K':
  148. case 'KB':
  149. $bytes *= 1024;
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. }
  156. return $bytes;
  157. }
  158. /**
  159. * ????????????
  160. *
  161. * @param string $name ???
  162. * @param string $maxsize ????? (e.g. 1M)
  163. * @param string $exts ???????? (e.g. jpg,jpeg,gif,png)
  164. * @return string ????????
  165. */
  166. public static function checkUploadFile($name, $maxsize, $exts)
  167. {
  168. $err = '';
  169. $upfile = @$_FILES[$name];
  170. if (!isset($upfile)) {
  171. $err = 'B00201';
  172. } elseif (!empty($upfile['error'])) {
  173. switch ($upfile['error']) {
  174. case '1':
  175. $err = 'B00202';
  176. break;
  177. case '2':
  178. $err = 'B00203';
  179. break;
  180. case '3':
  181. $err = 'B00204';
  182. break;
  183. case '4':
  184. $err = 'B00205';
  185. break;
  186. case '6':
  187. $err = 'B00206';
  188. break;
  189. case '7':
  190. $err = 'B00207';
  191. break;
  192. case '8':
  193. $err = 'B00208';
  194. break;
  195. case '999':
  196. default:
  197. $err = 'B00209';
  198. break;
  199. }
  200. } elseif (empty($upfile['tmp_name']) || $upfile['tmp_name'] == 'none') {
  201. $err = 'B00205';
  202. } elseif ($upfile['size'] > self::formatBytes($maxsize)) {
  203. $err = 'B00210';
  204. } else {
  205. $ext = pathinfo($upfile['name'], PATHINFO_EXTENSION);
  206. $pattern = '/' . str_replace(',', '|', $exts) . '/i';
  207. if (!preg_match($pattern, $ext)) {
  208. $err = 'B00211';
  209. }
  210. }
  211. return $err;
  212. }
  213. /**
  214. * ??PHP???? trim ???????
  215. *
  216. * @param string|array $data
  217. * @param string $charlist
  218. */
  219. public static function retrim($data, $charlist = null)
  220. {
  221. if (is_array($data)) {
  222. foreach ($data as $item) {
  223. $data = self::retrim($item);
  224. }
  225. } else {
  226. $data = trim($data, $charlist);
  227. }
  228. return $data;
  229. }
  230. /**
  231. * ????????????????????????
  232. *
  233. * @param int $weeks ??????????
  234. * @param int $start ???????????(0????-6???)
  235. * @return array
  236. */
  237. public static function pastWeek($weeks = -1, $start = 0)
  238. {
  239. $time = time();
  240. $index = date('w', $time);
  241. $endTime = strtotime(date('Y-m-d', $time)) - ($index - $start) * 86400;
  242. $startTime = strtotime($weeks . ' week', $endTime);
  243. return array('start' => $startTime, 'end' => $endTime);
  244. }
  245. /**
  246. * ? mb_strimwidth ??????????????
  247. *
  248. * @param string $str
  249. * @param int $start
  250. * @param int $width
  251. * @param string $trimmarker
  252. * @return string
  253. */
  254. public static function wsubstr($str, $start, $width, $trimmarker = '...')
  255. {
  256. $_encoding = mb_detect_encoding($str, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
  257. return mb_strimwidth($str, $start, $width, $trimmarker, $_encoding);
  258. }
  259. /**
  260. * ????HTML????????
  261. *
  262. * @param string $str
  263. * @param int $num
  264. * @param bool $more
  265. * @return string
  266. */
  267. public static function substrHtml($str, $num, $more = false)
  268. {
  269. $_encoding = strtoupper(mb_detect_encoding($str, array('ASCII','UTF-8','GB2312','GBK','BIG5')));
  270. if ($_encoding != 'UTF-8') {
  271. $str = mb_convert_encoding($str, 'UTF-8', $_encoding);
  272. }
  273. $str = str_replace('&nbsp;', chr(1), $str);
  274. $length = strlen($str);
  275. if ($num >= $length) {
  276. return $str;
  277. }
  278. $word = 0;
  279. $i = 0;
  280. $stag = array();
  281. $etag = array();
  282. $sp = 0;
  283. $ep = 0;
  284. while ($word != $num && $i < $length) {
  285. $code = ord($str[$i]);
  286. if ($code > 128) {
  287. if ($code > 240) {
  288. $i += 4;
  289. } else if ($code > 224) {
  290. $i += 3;
  291. } else {
  292. $i += 2;
  293. }
  294. $word++;
  295. } else if ($str[$i] == '<') {
  296. if ($str[$i + 1] == '!') {
  297. for (; $i < $length; $i++) {
  298. if ($str[i] == '>') {
  299. break;
  300. }
  301. }
  302. $i += 1;
  303. }
  304. if ($str[$i + 1] == '/') {
  305. $ptag = &$etag;
  306. $k = &$ep;
  307. $i += 2;
  308. } else {
  309. $ptag = &$stag;
  310. $k = &$sp;
  311. $i += 1;
  312. }
  313. $ptag[$k] = array();
  314. for (; $i < $length; $i++) {
  315. if ($str[$i] == ' ') {
  316. for (; $i < $length; $i++) {
  317. if ($str[$i] == '>') {
  318. break;
  319. }
  320. }
  321. }
  322. if ($str[$i] != '>') {
  323. $ptag[$k][] = $str[$i];
  324. continue;
  325. } else {
  326. $ptag[$k] = implode('', $ptag[$k]);
  327. $k++;
  328. break;
  329. }
  330. }
  331. $i++;
  332. continue;
  333. } else {
  334. $word++;
  335. $i++;
  336. }
  337. }
  338. foreach ($etag as $val) {
  339. $key = array_search($val, $stag);
  340. if ($key !== false) {
  341. unset($stag[$key]);
  342. }
  343. }
  344. foreach ($stag as $key => $val) {
  345. if (in_array($val, array('br', 'img', 'hr', 'input'))) {
  346. unset($stag[$key]);
  347. }
  348. }
  349. $stag = array_reverse($stag);
  350. if (!empty($stag)) {
  351. $ends = '</' . implode('></', $stag) . '>';
  352. } else {
  353. $ends = '';
  354. }
  355. $re = substr($str, 0, $i) . $ends;
  356. if ($more) {
  357. $re .= '...';
  358. }
  359. $re = str_replace(chr(1), '&nbsp;', $re);
  360. if ($_encoding != 'UTF-8') {
  361. $re = mb_convert_encoding($re, $_encoding, 'UTF-8');
  362. }
  363. return $re;
  364. }
  365. /**
  366. * ??CURL??POST????
  367. *
  368. * @param string $postUrl url address
  369. * @param array $data post data
  370. * @param int $timeout connect time out
  371. */
  372. public static function curlPost($postUrl, $data = array(), $timeout = 30)
  373. {
  374. $ch = curl_init();
  375. curl_setopt($ch, CURLOPT_URL, $postUrl);
  376. curl_setopt($ch, CURLOPT_POST, true);
  377. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  378. curl_setopt($ch, CURLOPT_HEADER, false);
  379. curl_setopt($ch, CURLINFO_HEADER_OUT, false);
  380. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  381. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, 'pre_', '&'));
  382. $result = curl_exec($ch);
  383. curl_close($ch);
  384. if ($result === false) {
  385. return $result;
  386. }
  387. return trim($result);
  388. }
  389. /**
  390. * ???????
  391. *
  392. * @param int $time
  393. * @return array|false
  394. */
  395. public static function sec2Time($time)
  396. {
  397. if (is_numeric($time)) {
  398. $value = array('years' => 0, 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0);
  399. if ($time >= 31556926) {
  400. $value['years'] = floor($time / 31556926);
  401. $time = ($time % 31556926);
  402. }
  403. if ($time >= 86400) {
  404. $value['days'] = floor($time / 86400);
  405. $time = ($time % 86400);
  406. }
  407. if ($time >= 3600) {
  408. $value['hours'] = floor($time / 3600);
  409. $time = ($time % 3600);
  410. }
  411. if ($time >= 60) {
  412. $value['minutes'] = floor($time / 60);
  413. $time = ($time % 60);
  414. }
  415. $value['seconds'] = floor($time);
  416. return $value;
  417. } else {
  418. return false;
  419. }
  420. }
  421. /**
  422. * ?? Yiidebugtb ????
  423. */
  424. public static function debug()
  425. {
  426. define('YII_DEBUG_TB', true);
  427. }
  428. /**
  429. * ???????????????????
  430. * ???????????????????????ID???
  431. *
  432. * @param array $array
  433. * @param string $key ???
  434. * @return array
  435. */
  436. public static function arrayCollectField($array, $key)
  437. {
  438. $data = array();
  439. foreach ($array as $item) {
  440. $data[] = $item[$key];
  441. }
  442. return $data;
  443. }
  444. /**
  445. * ??????????????????????
  446. * ?????????????????
  447. *
  448. * @param array $array
  449. * @param string $keyKey ???
  450. * @param string $valueKey ???
  451. * @return array
  452. */
  453. public static function arrayCollectPair($array, $keyKey, $valueKey)
  454. {
  455. $data = array();
  456. foreach ($array as $item) {
  457. $data[$item[$keyKey]] = $item[$valueKey];
  458. }
  459. return $data;
  460. }
  461. /**
  462. * ????????
  463. *
  464. * @param array $array
  465. * @param string $class ??????stdClass?
  466. * @return mixed
  467. */
  468. public static function arrayToObject($array, $class = 'stdClass')
  469. {
  470. $object = new $class();
  471. foreach ($array as $key => $value) {
  472. if (is_array($value)) {
  473. // Convert the array to an object
  474. $value = self::toObject($value, $class);
  475. }
  476. // Add the value to the object
  477. $object->{$key} = $value;
  478. }
  479. return $object;
  480. }
  481. /**
  482. * ????????????????????
  483. *
  484. * @return array
  485. */
  486. public static function arrayMerge()
  487. {
  488. $total = func_num_args();
  489. $result = array();
  490. for ($i = 0; $i < $total; $i++) {
  491. foreach (func_get_arg($i) as $key => $val) {
  492. if (isset($result[$key])) {
  493. if (is_array($val)) {
  494. // Arrays are merged recursively
  495. $result[$key] = self::merge($result[$key], $val);
  496. } elseif (is_int($key)) {
  497. // Indexed arrays are appended
  498. array_push($result, $val);
  499. } else {
  500. // Associative arrays are replaced
  501. $result[$key] = $val;
  502. }
  503. } else {
  504. // New values are added
  505. $result[$key] = $val;
  506. }
  507. }
  508. }
  509. return $result;
  510. }
  511. /**
  512. * ????????????????????????????????
  513. *
  514. * @param array $array1
  515. * @return array
  516. */
  517. public static function arrayOverwrite($array1)
  518. {
  519. foreach (array_slice(func_get_args(), 1) as $array2) {
  520. foreach ($array2 as $key => $value) {
  521. if (array_key_exists($key, $array1)) {
  522. $array1[$key] = $value;
  523. }
  524. }
  525. }
  526. return $array1;
  527. }
  528. /**
  529. * ????????????
  530. *
  531. * @param array $array
  532. * @return array
  533. */
  534. public static function arrayFlat($array)
  535. {
  536. $data = array ();
  537. if (is_array($array)) {
  538. foreach ( $array as $value ) {
  539. $data = array_merge($data, self::arrayFlat($value));
  540. }
  541. } else {
  542. $data[] = $array;
  543. }
  544. return $data;
  545. }
  546. public static function ip2long6($ipv6)
  547. {
  548. $ipv6long = '';
  549. $ipn = inet_pton($ipv6);
  550. $bits = 15;
  551. while ($bits >= 0) {
  552. $bin = sprintf("%08b", (ord($ipn[$bits])));
  553. $ipv6long = $bin . $ipv6long;
  554. $bits--;
  555. }
  556. return gmp_strval(gmp_init($ipv6long, 2), 10);
  557. }
  558. public static function long2ip6($ipv6long)
  559. {
  560. $bin = gmp_strval(gmp_init($ipv6long, 10), 2);
  561. if (strlen($bin) < 128) {
  562. $bin = str_pad($bin, 128 - strlen($bin), '0', STR_PAD_LEFT);
  563. }
  564. $bits = 0;
  565. while ($bits <= 7) {
  566. $bin_part = substr($bin, ($bits * 16), 16);
  567. $ipv6 .= dechex(bindec($bin_part)) . ":";
  568. $bits++;
  569. }
  570. return inet_ntop(inet_pton(substr($ipv6, 0, -1)));
  571. }
  572. }