PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/libraries/global.func.php

https://github.com/sahilbabu/phpb2b
PHP | 916 lines | 810 code | 76 blank | 30 comment | 172 complexity | e442ac7b627548c64e051a45440acad1 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * [PHPB2B] Copyright (C) 2007-2099, Ualink Inc. All Rights Reserved.
  4. * The contents of this file are subject to the License; you may not use this file except in compliance with the License.
  5. *
  6. * @version $Revision: 2218 $
  7. */
  8. function da($arr_str, $exit = false)
  9. {
  10. $x = "<pre>";
  11. $x .= print_r($arr_str, 1);
  12. $x .= "</pre>";
  13. print $x;
  14. ($exit)?exit:'';
  15. }
  16. function pb_getenv($key) {
  17. if ($key == 'HTTPS') {
  18. if (isset($_SERVER['HTTPS'])) {
  19. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');
  20. }
  21. return (strpos(pb_getenv('SCRIPT_URI'), 'https://') === 0);
  22. }
  23. if ($key == 'SCRIPT_NAME') {
  24. if (pb_getenv('CGI_MODE') && isset($_ENV['SCRIPT_URL'])) {
  25. $key = 'SCRIPT_URL';
  26. }
  27. }
  28. $val = null;
  29. if (isset($_SERVER[$key])) {
  30. $val = $_SERVER[$key];
  31. } elseif (isset($_ENV[$key])) {
  32. $val = $_ENV[$key];
  33. } elseif (getenv($key) !== false) {
  34. $val = getenv($key);
  35. }
  36. if ($key === 'REMOTE_ADDR' && $val === pb_getenv('SERVER_ADDR')) {
  37. $addr = pb_getenv('HTTP_PC_REMOTE_ADDR');
  38. if ($addr !== null) {
  39. $val = $addr;
  40. }
  41. }
  42. if ($val !== null) {
  43. return $val;
  44. }
  45. switch ($key) {
  46. case 'SCRIPT_FILENAME':
  47. if (defined('SERVER_IIS') && SERVER_IIS === true) {
  48. return str_replace('\\\\', '\\', pb_getenv('PATH_TRANSLATED'));
  49. }
  50. break;
  51. case 'DOCUMENT_ROOT':
  52. $name = pb_getenv('SCRIPT_NAME');
  53. $filename = pb_getenv('SCRIPT_FILENAME');
  54. $offset = 0;
  55. if (!strpos($name, '.php')) {
  56. $offset = 4;
  57. }
  58. return substr($filename, 0, strlen($filename) - (strlen($name) + $offset));
  59. break;
  60. case 'PHP_SELF':
  61. return str_replace(pb_getenv('DOCUMENT_ROOT'), '', pb_getenv('SCRIPT_FILENAME'));
  62. break;
  63. case "REQUEST_URI":
  64. if(!isset($_SERVER['REQUEST_URI'])) {
  65. $_SERVER['REQUEST_URI'] = substr($_SERVER['argv'][0], strpos($_SERVER['argv'][0], ';') + 1);
  66. }
  67. return $_SERVER['REQUEST_URI'];
  68. break;
  69. case 'CGI_MODE':
  70. return (PHP_SAPI === 'cgi');
  71. break;
  72. case 'HTTP_BASE':
  73. $host = pb_getenv('HTTP_HOST');
  74. if (substr_count($host, '.') !== 1) {
  75. return preg_replace('/^([^.])*/i', null, pb_getenv('HTTP_HOST'));
  76. }
  77. return '.' . $host;
  78. break;
  79. case 'HTTP_HOST':
  80. if(isset($_SERVER['SERVER_NAME'])){
  81. return $_SERVER['SERVER_NAME'];
  82. }else{
  83. return $_SERVER['HTTP_HOST'];
  84. }
  85. break;
  86. }
  87. return null;
  88. }
  89. function pb_strcomp($str1,$str2)
  90. {
  91. if (strcmp(trim($str1),trim($str2)) == 0) {
  92. return true;
  93. }else {
  94. return false;
  95. }
  96. }
  97. function pb_radom($len=6,$recycle=1){
  98. $str = 'ABCDEFGHJKMNPQRSTUVWXYabcdefghjkmnpqrstuvwxy';
  99. $str.= '123456789';
  100. $str = str_repeat($str,$recycle);
  101. return substr(str_shuffle($str),0,$len);
  102. }
  103. function setvar($name,$var)
  104. {
  105. global $smarty;
  106. $smarty->assign($name,$var);
  107. }
  108. function uaAssign($names)
  109. {
  110. global $smarty;
  111. if (is_array($names)) {
  112. foreach ($names as $smt_key=>$smt_val) {
  113. $smarty->assign($smt_key,$smt_val);
  114. }
  115. }
  116. }
  117. function pheader($string, $replace = true, $http_response_code = 0) {
  118. $string = str_replace(array("\r", "\n"), array('', ''), $string);
  119. if(empty($http_response_code) || PHP_VERSION < '4.3' ) {
  120. @header($string, $replace);
  121. } else {
  122. @header($string, $replace, $http_response_code);
  123. }
  124. if(preg_match('/^\s*location:/is', $string)) {
  125. exit();
  126. }
  127. }
  128. function flash($message_title = '', $back_url = '', $pause = 3, $extra = '')
  129. {
  130. global $smarty;
  131. if (empty($back_url)) {
  132. if (defined('CURSCRIPT')) {
  133. $back_url = CURSCRIPT. ".php";
  134. }elseif (isset($_SERVER['HTTP_REFERER'])){
  135. $back_url = $_SERVER['HTTP_REFERER'];
  136. }else{
  137. $back_url = "javascript:;";
  138. }
  139. }
  140. $return = $smarty->flash($message_title, $back_url, $pause, $extra);
  141. }
  142. function pb_create_folder($dir)
  143. {
  144. return is_dir($dir) or (pb_create_folder(dirname($dir)) and mkdir($dir, 0777));
  145. //@chmod($dir, 0777);
  146. }
  147. function pb_get_cache($models, $path = '')
  148. {
  149. if (is_array($models)) {
  150. foreach ($models as $model) {
  151. $cache_file = $path?CACHE_PATH.$path."cache_".$model.".php":CACHE_PATH."cache_".$model.".php";
  152. if (file_exists($cache_file)) {
  153. include $cache_file;
  154. }
  155. }
  156. }else{
  157. $cache_file = $path?CACHE_PATH.$path."cache_".$models.".php":CACHE_PATH."cache_".$models.".php";
  158. if (file_exists($cache_file)) {
  159. include $cache_file;
  160. }
  161. }
  162. }
  163. function render($filename = null, $exit = false)
  164. {
  165. global $smarty,
  166. $viewhelper,
  167. $theme_name,
  168. $cache_id,
  169. $dir_name,
  170. $default_html_filename,
  171. $re_create_file;
  172. $return = false;
  173. $tmp_themename = '';
  174. $smarty->assign('position', $viewhelper->getPosition());
  175. $smarty->assign('page_title', $viewhelper->getTitle());
  176. $tpl = $theme_name.DS.$filename.$smarty->tpl_ext;
  177. if ($theme_name=='blue' || !$smarty->template_exists($tpl)) {
  178. $tmp_themename = 'default';
  179. $tpl = 'default'.DS.$filename.$smarty->tpl_ext;
  180. }
  181. if (empty($filename)) {
  182. //Todo:auto select template default
  183. }
  184. $smarty->assign('ThemeName', $tmp_themename?$tmp_themename:$theme_name);
  185. if (!empty($viewhelper->metaDescription)) {
  186. $smarty->assign("metadescription", $viewhelper->metaDescription);
  187. }
  188. if (!empty($viewhelper->metaKeyword)) {
  189. $smarty->assign("metakeywords", $viewhelper->metaKeyword);
  190. }elseif (!empty($viewhelper->metaDescription)){
  191. $viewhelper->setMetaKeyword($viewhelper->metaDescription);
  192. $smarty->assign("metakeywords", $viewhelper->metaKeyword);
  193. }
  194. if ($smarty->caching) {
  195. $cache_id = $_GET['page']."|".$_GET['id']."|".$_GET['pos'];
  196. }
  197. if (defined("SMARTY_CACHE") && SMARTY_CACHE){
  198. $smarty->caching = 1;
  199. }
  200. if ($smarty->caching) {
  201. $cache_id = substr(md5(pb_getenv('REQUEST_URI').$cache_id), 0, 16);
  202. }
  203. $return = $smarty->display($tpl, $cache_id);
  204. //for the static homepage,since 1/1/2011
  205. if ($re_create_file) {
  206. if (! is_dir ( $dir_name )) {
  207. pb_create_folder ( $dir_name );
  208. }
  209. $content = ob_get_contents ();
  210. file_put_contents ( $default_html_filename, $content );
  211. }
  212. if ($exit) {
  213. exit;
  214. }
  215. if (isset($htmlize) && !$htmlize) {
  216. return $return;
  217. }
  218. return $return;
  219. }
  220. function template($filename = null, $exit = false)
  221. {
  222. global $smarty;
  223. $return = false;
  224. $return = $smarty->display($filename.$smarty->tpl_ext);
  225. if ($exit) {
  226. exit;
  227. }
  228. return $return;
  229. }
  230. function pb_check_email($email){
  231. $return = false;
  232. if(strstr($email, '@') && strstr($email, '.')){
  233. if(preg_match("/^([_a-z0-9]+([\._a-z0-9-]+)*)@([a-z0-9]{2,}(\.[a-z0-9-]{2,})*\.[a-z]{2,4})$/", $email)){
  234. $return = true;
  235. }
  236. }
  237. return $return;
  238. }
  239. function usetcookie($var, $value, $life_time = 0, $prefix = 1) {
  240. global $cookiepre, $cookiepath, $time_stamp, $cookiedomain;
  241. return setcookie(($prefix ? $cookiepre : '').$var, $value,
  242. $life_time ? $time_stamp + $life_time : 0, $cookiepath,
  243. $cookiedomain, $_SERVER['SERVER_PORT'] == 443 ? 1 : 0);
  244. }
  245. function uclearcookies() {
  246. return usetcookie('auth', '', -86400 * 365);
  247. }
  248. function fileext($filename) {
  249. return substr(($t=strrchr($filename,'.'))!==false?".".$t:'',1);
  250. }
  251. function pb_htmlspecialchar($string) {
  252. if(is_array($string)) {
  253. foreach($string as $key => $val) {
  254. $string[$key] = pb_htmlspecialchar($val);
  255. }
  256. } else {
  257. $string = preg_replace('/&amp;((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
  258. str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
  259. }
  260. return $string;
  261. }
  262. function pb_get_client_ip($type = "long")
  263. {
  264. if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
  265. $onlineip = getenv('HTTP_CLIENT_IP');
  266. } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
  267. $onlineip = getenv('HTTP_X_FORWARDED_FOR');
  268. } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
  269. $onlineip = getenv('REMOTE_ADDR');
  270. } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
  271. $onlineip = $_SERVER['REMOTE_ADDR'];
  272. }
  273. preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
  274. $onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
  275. if($onlineip=='unknown') return $onlineip;
  276. if($type=="long"){
  277. return pb_ip2long($onlineip);
  278. }else{
  279. return $onlineip;
  280. }
  281. }
  282. function pb_ip2long($ip)
  283. {
  284. return sprintf("%u",ip2long($ip));
  285. }
  286. function pb_addslashes($string) {
  287. if(is_array($string)) {
  288. foreach($string as $key => $val) {
  289. $string[$key] = pb_addslashes($val);
  290. }
  291. } else {
  292. $string = addslashes($string);
  293. }
  294. return $string;
  295. }
  296. function stripslashes_recursive(&$array) {
  297. while(list($key,$var) = each($array)) {
  298. if ($key != 'argc' && $key != 'argv' && (strtoupper($key) != $key || ''.intval($key) == "$key")) {
  299. if (is_string($var)) {
  300. $array[$key] = stripslashes($var);
  301. }
  302. if (is_array($var)) {
  303. $array[$key] = stripslashes_recursive($var);
  304. }
  305. }
  306. }
  307. return $array;
  308. }
  309. function stripslashes_deep($value)
  310. {
  311. if(isset($value)) {
  312. $value = is_array($value) ?
  313. array_map('stripslashes_deep', $value) :
  314. stripslashes($value);
  315. }
  316. return $value;
  317. }
  318. if (!function_exists('getmicrotime')) {
  319. function getmicrotime() {
  320. list($usec, $sec) = explode(' ', microtime());
  321. return ((float)$usec + (float)$sec);
  322. }
  323. }
  324. function pb_get_host($http = true)
  325. {
  326. if ( isset( $_SERVER['HTTPS'] ) && ( strtolower( $_SERVER['HTTPS'] ) != 'off' ) ) {
  327. $ul_protocol = 'https';
  328. }else{
  329. $ul_protocol = 'http';
  330. }
  331. if($http) {
  332. $return = $ul_protocol."://".$_SERVER['HTTP_HOST'];
  333. } else {
  334. $return = $_SERVER['HTTP_HOST'];
  335. }
  336. return $return;
  337. }
  338. function uses() {
  339. $args = func_get_args();
  340. foreach($args as $arg) {
  341. $class_name = strtolower($arg);
  342. include_once(LIB_PATH . "core/controllers/".$class_name. '_controller.php');
  343. if(is_file($model_file = LIB_PATH . "core/models/".$class_name. '.php')) include_once($model_file);
  344. }
  345. }
  346. function using() {
  347. $args = func_get_args();
  348. foreach($args as $arg) {
  349. $class_name = strtolower($arg);
  350. require_once(LIB_PATH . "core/models/".$class_name. '.php');
  351. }
  352. }
  353. function pb_strip_spaces($string)
  354. {
  355. $str = preg_replace('#\s+#', ' ', trim($string));
  356. return $str;
  357. }
  358. function pb_get_member_info()
  359. {
  360. global $cookiepre;
  361. $user = array();
  362. if (!empty($_COOKIE[$cookiepre."auth"])) {
  363. list($user['pb_userid'], $user['pb_username'], $user['pb_userpasswd'], $user['is_admin']) = explode("\t", authcode($_COOKIE[$cookiepre."auth"], 'DECODE'));
  364. return $user;
  365. }else{
  366. return false;
  367. }
  368. }
  369. function authcode($string, $operation = "ENCODE", $key = '', $expire = 0) {
  370. global $phpb2b_auth_key;
  371. $ckey_length = 4;
  372. $key = md5($key ? $key : $phpb2b_auth_key);
  373. $keya = md5(substr($key, 0, 16));
  374. $keyb = md5(substr($key, 16, 16));
  375. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
  376. $cryptkey = $keya.md5($keya.$keyc);
  377. $key_length = strlen($cryptkey);
  378. $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expire ? $expire + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  379. $string_length = strlen($string);
  380. $result = '';
  381. $box = range(0, 255);
  382. $rndkey = array();
  383. for($i = 0; $i <= 255; $i++) {
  384. $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  385. }
  386. for($j = $i = 0; $i < 256; $i++) {
  387. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  388. $tmp = $box[$i];
  389. $box[$i] = $box[$j];
  390. $box[$j] = $tmp;
  391. }
  392. for($a = $j = $i = 0; $i < $string_length; $i++) {
  393. $a = ($a + 1) % 256;
  394. $j = ($j + $box[$a]) % 256;
  395. $tmp = $box[$a];
  396. $box[$a] = $box[$j];
  397. $box[$j] = $tmp;
  398. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  399. }
  400. if($operation == 'DECODE') {
  401. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  402. return substr($result, 26);
  403. } else {
  404. return '';
  405. }
  406. } else {
  407. return $keyc.str_replace('=', '', base64_encode($result));
  408. }
  409. }
  410. function ___L() {
  411. global $arrTemplate;
  412. $args = func_get_args();
  413. if ($args[1] == "msg") {
  414. unset($arrTemplate);
  415. if(is_file(CACHE_LANG_PATH. "lang_message.php")){
  416. require(CACHE_LANG_PATH. "lang_message.php");
  417. }
  418. }
  419. $return = $arrTemplate['_'.$args[0]];
  420. if (is_array($args[2])) {
  421. $return = vsprintf($return, $args[2]);
  422. }else{
  423. $return = sprintf($return, $args[2]);
  424. }
  425. return (!empty($return))?pb_lang_split($return):$args[0];
  426. }
  427. function L($key, $type = "msg", $extra = null){
  428. return call_user_func_array("___L", array($key, $type, $extra));
  429. }
  430. if (!function_exists('file_get_contents')) {
  431. function file_get_contents($filename, $incpath = false, $resource_context = null)
  432. {
  433. if (false === $fh = fopen($filename, 'rb', $incpath)) {
  434. trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);
  435. return false;
  436. }
  437. clearstatcache();
  438. if ($fsize = @filesize($filename)) {
  439. $data = fread($fh, $fsize);
  440. } else {
  441. $data = '';
  442. while (!feof($fh)) {
  443. $data .= fread($fh, 8192);
  444. }
  445. }
  446. fclose($fh);
  447. return $data;
  448. }
  449. }
  450. if (!function_exists('file_put_contents')) {
  451. function file_put_contents($filename, $data) {
  452. $f = @fopen($filename, 'w');
  453. if (!$f) {
  454. return false;
  455. } else {
  456. $bytes = fwrite($f, $data);
  457. fclose($f);
  458. return $bytes;
  459. }
  460. }
  461. }
  462. if (!function_exists('http_build_query')) {
  463. function http_build_query($data, $prefix = null, $argSep = null, $baseKey = null) {
  464. if (empty($argSep)) {
  465. $argSep = ini_get('arg_separator.output');
  466. }
  467. if (is_object($data)) {
  468. $data = get_object_vars($data);
  469. }
  470. $out = array();
  471. foreach ((array)$data as $key => $v) {
  472. if (is_numeric($key) && !empty($prefix)) {
  473. $key = $prefix . $key;
  474. }
  475. $key = urlencode($key);
  476. if (!empty($baseKey)) {
  477. $key = $baseKey . '[' . $key . ']';
  478. }
  479. if (is_array($v) || is_object($v)) {
  480. $out[] = http_build_query($v, $prefix, $argSep, $key);
  481. } else {
  482. $out[] = $key . '=' . urlencode($v);
  483. }
  484. }
  485. return implode($argSep, $out);
  486. }
  487. }
  488. function formhash() {
  489. global $time_stamp, $phpb2b_auth_key;
  490. return substr(md5(substr($time_stamp, 0, -4).$phpb2b_auth_key), 16);
  491. }
  492. function pb_submit_check($var) {
  493. $referer = pb_getenv('HTTP_REFERER');
  494. if (is_file(DATA_PATH.'antispam'.DS.'index.php')) {
  495. require(DATA_PATH.'antispam'.DS.'index.php');
  496. }
  497. if(!empty($_POST[$var]) && $_SERVER['REQUEST_METHOD'] == 'POST') {
  498. if((empty($referer) || preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $referer) == preg_replace("/([^\:]+).*/", "\\1", pb_getenv('HTTP_HOST'))) && $_POST['formhash'] == formhash()) {
  499. return true;
  500. }
  501. }
  502. header_sent(L("invalid_submit"));
  503. exit;
  504. }
  505. function parse_highlight($highlight, $return_color = false) {
  506. if($highlight) {
  507. //as like to colorPicker, viewHelper
  508. $colorarray = array('#000000', '#FF0000', '#FFA500', '#FFFF00', '#008000', '#00FFFF', '#0000FF', '#800080', '#808080');
  509. $string = sprintf('%02d', $highlight);
  510. $stylestr = sprintf('%03b', $string[0]);
  511. if ($return_color) {
  512. $r['bold'] = $stylestr[0];
  513. $r['italic'] = $stylestr[1];
  514. $r['underline'] = $stylestr[2];
  515. $r['color'] = $colorarray[intval($string[1])];
  516. return $r;
  517. }
  518. $style = ' style="';
  519. $style .= $stylestr[0] ? 'font-weight: bold;' : '';
  520. $style .= $stylestr[1] ? 'font-style: italic;' : '';
  521. $style .= $stylestr[2] ? 'text-decoration: underline;' : '';
  522. $style .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
  523. $style .= '"';
  524. } else {
  525. $style = '';
  526. }
  527. return $style;
  528. }
  529. function pb_get_attachmenturl($src, $path = '', $size = '', $force = false)
  530. {
  531. global $attachment_dir, $attachment_url;
  532. $default_thumb_img = 'images/nopicture_small.gif';
  533. switch ($size) {
  534. case "small":
  535. $scope = ".".$size;
  536. break;
  537. case "middle":
  538. $scope = ".".$size;
  539. break;
  540. case "country":
  541. return '<img src="images/country/'.$src.'"/>';
  542. break;
  543. case "group":
  544. return '<img src="images/group/'.$src.'"/>';
  545. break;
  546. default:
  547. $scope = "";
  548. break;
  549. }
  550. if (!empty($scope)) {
  551. $default_thumb_img = 'images/nopicture_'.$size.'.gif';
  552. }
  553. if ($force) {
  554. $default_thumb_img = 'images/nopicture_'.$force.'.gif';
  555. }
  556. $img = $src ? $attachment_url.$src : $default_thumb_img;
  557. if ($scope && ($img!=$default_thumb_img)) {
  558. $img.="{$scope}.jpg";
  559. }
  560. return $path.$img;
  561. }
  562. function capt_check($capt_name)
  563. {
  564. global $_POST, $_PB_CACHE, $smarty, $charset;
  565. $capt_require = array(
  566. "capt_logging",
  567. "capt_register",
  568. "capt_post_free",
  569. "capt_add_market",
  570. "capt_login_admin",
  571. "capt_apply_friendlink",
  572. "capt_service"
  573. );
  574. if (in_array($capt_name, $capt_require)) {
  575. $t = decbin($_PB_CACHE['setting']['capt_auth']);
  576. $capt_auth = sprintf("%07d", $t);
  577. $key = array_search($capt_name, $capt_require);
  578. if($capt_auth[$key]){
  579. if (!empty($_POST['data'])) {
  580. include(LIB_PATH. "securimage/securimage.php");
  581. $img = new Securimage();
  582. $post_code = trim($_POST['data'][$capt_name]);
  583. header('Content-Type: text/html; charset='.$charset);
  584. if(!$img->check($post_code)){
  585. flash('invalid_capt', null, 0);
  586. }
  587. }
  588. $smarty->assign("ifcapt", true);
  589. }else{
  590. $smarty->assign("ifcapt", false);
  591. }
  592. }
  593. }
  594. function am() {
  595. $r = array();
  596. $args = func_get_args();
  597. foreach ($args as $a) {
  598. if (!is_array($a)) {
  599. $a = array($a);
  600. }
  601. $r = array_merge($r, $a);
  602. }
  603. return $r;
  604. }
  605. function header_sent($msg)
  606. {
  607. global $charset;
  608. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  609. <html xmlns="http://www.w3.org/1999/xhtml">
  610. <head>
  611. <meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />
  612. </head>
  613. <body>
  614. <div style="font:normal normal normal 12px/1.2em Arial Black;">
  615. '.$msg.'
  616. </div>
  617. </body>
  618. </html>';
  619. }
  620. if (!function_exists("array_combine")) {
  621. function array_combine($arr1, $arr2) {
  622. $out = array();
  623. $arr1 = array_values($arr1);
  624. $arr2 = array_values($arr2);
  625. foreach($arr1 as $key1 => $value1) {
  626. $out[(string)$value1] = $arr2[$key1];
  627. }
  628. return $out;
  629. }
  630. }
  631. function check_proxy(){
  632. $v = getenv("HTTP_VIA");
  633. $f = getenv("HTTP_X_FORWARDED_FOR");
  634. $c = getenv("HTTP_XROXY_CONNECTION");
  635. $o = getenv("HTTP_PRAGMA");
  636. if ( ($v=="")&&($f=="")&&($c=="")&&($o=="") ) return false;
  637. return true;
  638. }
  639. function cache_read($file = null, $item = null, $prefix = true, $remove_params = null) {
  640. global $app_lang;
  641. $return = false;
  642. if($prefix) $file_name = CACHE_COMMON_PATH. 'cache_'.$file.'.php';
  643. else $file_name = CACHE_COMMON_PATH. $file.'.php';
  644. // $file_name = CACHE_ROOT. $app_lang. DS. 'cache_'.$file.'.php';
  645. if(!is_file($file_name)) return $return;
  646. $_required = require($file_name);
  647. if (empty($item)) {
  648. $item = $file;
  649. }
  650. if(isset($_PB_CACHE[$item])){
  651. $return = $_PB_CACHE[$item];
  652. }elseif(!empty($_PB_CACHE[$file])){
  653. $return = $_PB_CACHE[$file];
  654. }elseif(!empty($_PB_CACHE)){
  655. $return = $_PB_CACHE;
  656. }else{
  657. $return = $_required;
  658. }
  659. if (is_array($return)) {
  660. $return = array_map_recursive("pb_lang_split", $return);
  661. }
  662. if (!empty($remove_params)) {
  663. if (is_array($remove_params)) {
  664. foreach ($remove_params as $val) {
  665. unset($return[$val]);
  666. }
  667. }else{
  668. unset($return[$remove_params]);
  669. }
  670. }
  671. return $return;
  672. }
  673. function array_map_recursive($fn, $arr) {
  674. $ret = array();
  675. if (!empty($arr)) {
  676. foreach ($arr as $key => $val) {
  677. $ret[$key] = is_array($val)
  678. ? array_map_recursive($fn, $val)
  679. : $fn($val);
  680. }
  681. }
  682. return $ret;
  683. }
  684. function df($timestamp = null, $format = null)
  685. {
  686. global $time_stamp;
  687. $return = '';
  688. if (empty($timestamp)) {
  689. $timestamp = $time_stamp;
  690. }
  691. if (!empty($format)) {
  692. $return = date($format, $timestamp);
  693. }else{
  694. $return = date("Y-m-d", $timestamp);
  695. }
  696. return $return;
  697. }
  698. function sens_str($content, $to = "***")
  699. {
  700. $str = $content;
  701. $badword = cache_read("words", "words");
  702. if(!empty($badword)){
  703. $badword1 = array_combine($badword,array_fill(0,count($badword),$to));
  704. $str = strtr($content, $badword1);
  705. }
  706. return $str;
  707. }
  708. function pb_lang_enabled($lang_name, $languages) {
  709. return in_array($lang_name, $languages);
  710. }
  711. /**
  712. * merge multi language title to one column
  713. *
  714. * @param array $inputs multi language title
  715. * @return string one title
  716. */
  717. function pb_lang_merge($inputs)
  718. {
  719. global $G, $app_lang;
  720. // if(!empty($G['languages']))
  721. $_languages = array_keys(unserialize($G['languages']));
  722. // else
  723. // $_languages[] = $app_lang;
  724. $ret = '';
  725. $inputs = array_filter($inputs);
  726. foreach ($inputs as $key=>$val) {
  727. if(pb_lang_enabled($key, $_languages)) $ret.="[:".$key."]".$val;
  728. }
  729. return $ret;
  730. }
  731. function pb_lang_split_recursive($arr)
  732. {
  733. $ret = array();
  734. if(!empty($arr))
  735. $ret = array_map_recursive("pb_lang_split", $arr);
  736. return $ret;
  737. }
  738. /**
  739. * split lang title
  740. *
  741. * @param string $text title
  742. * @param string $lang_name language name
  743. * @param boolean $all if return all
  744. * @return string or array
  745. */
  746. function pb_lang_split($text, $all = false) {
  747. global $G, $app_lang;
  748. $lang_name = $app_lang;
  749. //convert to array
  750. if(!empty($G['languages']))
  751. $_languages = array_keys(unserialize($G['languages']));
  752. else
  753. $_languages[] = $lang_name;
  754. $ret = '';
  755. $split_regex = "#(<!--[^>]*[^\/]-->|\[:[-:a-z]{3,5}\])#ism";
  756. $current_language = "";
  757. $result = array();
  758. foreach($_languages as $language) {
  759. $result[$language] = "";
  760. }
  761. $blocks = preg_split($split_regex, $text, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
  762. foreach($blocks as $block) {
  763. if(preg_match("#^\[:([-:a-z]{3,5})\]$#ism", $block, $matches)) {
  764. if(pb_lang_enabled($matches[1], $_languages)) {
  765. $current_language = $matches[1];
  766. } else {
  767. $current_language = "invalid";
  768. }
  769. continue;
  770. }
  771. if($current_language == "") {
  772. foreach($_languages as $language) {
  773. $result[$language] .= $block;
  774. }
  775. } elseif($current_language != "invalid") {
  776. $result[$current_language] .= $block;
  777. }
  778. }
  779. if($all) return $result;
  780. else{
  781. $result = array_filter($result);
  782. if(isset($result[$lang_name])) $ret = $result[$lang_name];
  783. if(!empty($ret)) return $ret;
  784. elseif(current($result)) return current($result);//get the first
  785. // elseif(current($result)) return current($result)."[".key($result)."]";
  786. else return $text;//return orignal
  787. }
  788. }
  789. function clear_html($string)
  790. {
  791. $farr = array(
  792. "/\s+/",
  793. "/<(\/?)(script|i?frame|style|html|body|title|link|meta|\?|\%)([^>]*?)>/isU",
  794. "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
  795. );
  796. $tarr = array(
  797. " ",
  798. "<\1\2\3>",
  799. "\1\2",
  800. );
  801. if(is_array($string)) {
  802. foreach($string as $key => $val) {
  803. $str[$key] = clear_html($val);
  804. }
  805. } else {
  806. $str = preg_replace( $farr,$tarr,$string);
  807. }
  808. return $str;
  809. }
  810. function pb_hidestr($string)
  811. {
  812. $ret = '';
  813. if(empty($string)){
  814. return false;
  815. }
  816. $show = array('m_0', 'm_1', 'm_2', 'm_3', 'm_8', 'm_9', 'm_10', 'm_16', 'm_19', 'm_20', 'm_22', 'm_33', 'm_34', 'm_37', 'm_38', 'm_40', 'm_44', 'm_45', 'm_46', 'm_48');
  817. $hide = array('m_4', 'm_5', 'm_6', 'm_7', 'm_11', 'm_12', 'm_13', 'm_14', 'm_15', 'm_17', 'm_18', 'm_21', 'm_23', 'm_24', 'm_25', 'm_26', 'm_27', 'm_28', 'm_29', 'm_30', 'm_31', 'm_32', 'm_35', 'm_36', 'm_39', 'm_41', 'm_42', 'm_43', 'm_47', 'm_49');
  818. for($i=0;$i<strlen($string);$i++){
  819. $flag = mt_rand(0,1);
  820. if($flag){
  821. $show_style = array_rand($show);
  822. $ret .="<span class='".$show[$show_style]."'>".$string[$i]."</span>";
  823. }else{
  824. $hide_style = array_rand($hide);
  825. $ret .="<span class='".$hide[$hide_style]."'>".mt_rand(0,1000)."</span>";
  826. $i--;
  827. }
  828. }
  829. return $ret;
  830. }
  831. function is_mobile(){
  832. $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
  833. $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
  834. $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
  835. $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
  836. $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
  837. $regex_match.=")/i";
  838. return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
  839. }
  840. ?>