PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/functions.php

http://viet-group.googlecode.com/
PHP | 1712 lines | 1208 code | 139 blank | 365 comment | 226 complexity | d71b5a3e4131e6f4817519627a7f2af0 MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @Project NUKEVIET 3.0
  4. * @Author VINADES.,JSC (contact@vinades.vn)
  5. * @Copyright (C) 2010 VINADES.,JSC. All rights reserved
  6. * @Createdate 1/9/2010, 23:48
  7. */
  8. if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' );
  9. require_once ( NV_ROOTDIR . '/includes/utf8/' . $sys_info['string_handler'] . '_string_handler.php' );
  10. require_once ( NV_ROOTDIR . '/includes/utf8/utf8_functions.php' );
  11. require_once ( NV_ROOTDIR . '/includes/core/filesystem_functions.php' );
  12. require_once ( NV_ROOTDIR . '/includes/core/cache_functions.php' );
  13. if ( ! function_exists( 'array_intersect_key' ) )
  14. {
  15. /**
  16. * array_intersect_key()
  17. *
  18. * @param mixed $isec
  19. * @param mixed $keys
  20. * @return
  21. */
  22. function array_intersect_key ( $isec, $keys )
  23. {
  24. $num = func_num_args();
  25. if ( $num > 2 )
  26. {
  27. for ( $i = 1; ! empty( $isec ) && $i < $num; $i ++ )
  28. {
  29. $arr = func_get_arg( $i );
  30. foreach ( array_keys( $isec ) as $key )
  31. {
  32. if ( ! isset( $arr[$key] ) ) unset( $isec[$key] );
  33. }
  34. }
  35. return $isec;
  36. }
  37. else
  38. {
  39. $res = array();
  40. foreach ( array_keys( $isec ) as $key )
  41. {
  42. if ( isset( $keys[$key] ) ) $res[$key] = $isec[$key];
  43. }
  44. return $res;
  45. }
  46. }
  47. }
  48. if ( ! function_exists( 'array_diff_key' ) )
  49. {
  50. /**
  51. * array_diff_key()
  52. *
  53. * @return
  54. */
  55. function array_diff_key ( )
  56. {
  57. $arrs = func_get_args();
  58. $result = array_shift( $arrs );
  59. foreach ( $arrs as $array )
  60. {
  61. foreach ( $result as $key => $v )
  62. {
  63. if ( array_key_exists( $key, $array ) )
  64. {
  65. unset( $result[$key] );
  66. }
  67. }
  68. }
  69. return $result;
  70. }
  71. }
  72. /**
  73. * nv_object2array()
  74. *
  75. * @param mixed $data
  76. * @return
  77. */
  78. function nv_object2array ( $data )
  79. {
  80. if ( is_object( $data ) ) $data = get_object_vars( $data );
  81. return is_array( $data ) ? array_map( __function__, $data ) : $data;
  82. }
  83. /**
  84. * nv_getenv()
  85. *
  86. * @param mixed $key
  87. * @return
  88. */
  89. function nv_getenv ( $key )
  90. {
  91. if ( isset( $_SERVER[$key] ) ) return $_SERVER[$key];
  92. elseif ( isset( $_ENV[$key] ) ) return $_ENV[$key];
  93. elseif ( @getenv( $key ) ) return @getenv( $key );
  94. elseif ( function_exists( 'apache_getenv' ) && apache_getenv( $key, true ) ) return apache_getenv( $key, true );
  95. return "";
  96. }
  97. /**
  98. * nv_preg_quote()
  99. *
  100. * @param mixed $string
  101. * @return
  102. */
  103. function nv_preg_quote ( $string )
  104. {
  105. return preg_quote( $string, "/" );
  106. }
  107. /**
  108. * nv_is_myreferer()
  109. *
  110. * @param string $referer
  111. * @return
  112. */
  113. function nv_is_myreferer ( $referer = "" )
  114. {
  115. if ( empty( $referer ) ) $referer = urldecode( nv_getenv( 'HTTP_REFERER' ) );
  116. if ( empty( $referer ) ) return 2;
  117. $server_name = preg_replace( '/^www\./e', '', nv_getenv( "HTTP_HOST" ) );
  118. $referer_nohttp = preg_replace( array( '/^[a-zA-Z]+\:\/\//e', '/www\./e' ), array( '', '' ), $referer );
  119. if ( preg_match( "/^" . preg_quote( $server_name ) . "/", $referer_nohttp ) ) return 1;
  120. return 0;
  121. }
  122. /**
  123. * nv_is_blocker_proxy()
  124. *
  125. * @param mixed $is_proxy
  126. * @param mixed $proxy_blocker
  127. * @return
  128. */
  129. function nv_is_blocker_proxy ( $is_proxy, $proxy_blocker )
  130. {
  131. $blocker = false;
  132. switch ( $proxy_blocker )
  133. {
  134. case 1:
  135. if ( $is_proxy == 'Strong' ) $blocker = true;
  136. break;
  137. case 2:
  138. if ( $is_proxy == 'Strong' || $is_proxy == 'Mild' ) $blocker = true;
  139. break;
  140. case 3:
  141. if ( $is_proxy != 'No' ) $blocker = true;
  142. break;
  143. }
  144. return $blocker;
  145. }
  146. /**
  147. * nv_is_banIp()
  148. *
  149. * @param mixed $ip
  150. * @return
  151. */
  152. function nv_is_banIp ( $ip )
  153. {
  154. global $global_config;
  155. if ( file_exists( NV_ROOTDIR . "/" . NV_DATADIR . "/banip.php" ) )
  156. {
  157. include ( NV_ROOTDIR . "/" . NV_DATADIR . "/banip.php" );
  158. $array_banip = ( defined( 'NV_ADMIN' ) ) ? $array_banip_admin : $array_banip_site;
  159. foreach ( $array_banip as $ip_i => $array_ip )
  160. {
  161. if ( $array_ip['begintime'] < NV_CURRENTTIME and ( $array_ip['endtime'] == 0 or $array_ip['endtime'] > NV_CURRENTTIME ) )
  162. {
  163. if ( preg_replace( $array_ip['mask'], "", $ip ) == preg_replace( $array_ip['mask'], "", $ip_i ) )
  164. {
  165. return true;
  166. }
  167. }
  168. }
  169. }
  170. return false;
  171. }
  172. /**
  173. * nv_checkagent()
  174. *
  175. * @param mixed $agent
  176. * @return
  177. */
  178. function nv_checkagent ( $agent )
  179. {
  180. $agent = htmlspecialchars( substr( $agent, 0, 255 ) );
  181. $agent = str_replace( ",", "-", $agent );
  182. $agent = str_replace( "<", "(", $agent );
  183. $agent = ( ! empty( $agent ) and $agent != "-" ) ? $agent : "none";
  184. return $agent;
  185. }
  186. /**
  187. * nv_check_bot()
  188. *
  189. * @return
  190. */
  191. function nv_check_bot ( )
  192. {
  193. global $client_info;
  194. $bot_info = array();
  195. $file_bots = NV_ROOTDIR . "/" . NV_DATADIR . "/bots.config";
  196. $bots = ( file_exists( $file_bots ) and filesize( $file_bots ) != 0 ) ? unserialize( file_get_contents( $file_bots ) ) : array();
  197. if ( empty( $bots ) and file_exists( NV_ROOTDIR . "/includes/bots.php" ) )
  198. {
  199. include ( NV_ROOTDIR . "/includes/bots.php" );
  200. }
  201. if ( ! empty( $bots ) )
  202. {
  203. $bot = array();
  204. foreach ( $bots as $name => $values )
  205. {
  206. $bot = false;
  207. if ( $values['agent'] and preg_match( '#' . str_replace( '\*', '.*?', nv_preg_quote( $values['agent'], '#' ) ) . '#i', $client_info['agent'] ) ) $bot = true;
  208. if ( ! empty( $values['ips'] ) and ( $bot or ! $values['agent'] ) )
  209. {
  210. $bot = false;
  211. $ips = implode( "|", array_map( "nv_preg_quote", explode( "|", $values['ips'] ) ) );
  212. if ( preg_match( "/^" . $ips . "/", $client_info['ip'] ) ) $bot = true;
  213. }
  214. if ( $bot )
  215. {
  216. $bot_info = array( 'name' => $name, 'agent' => $values['agent'], 'ip' => $client_info['ip'], 'allowed' => $values['allowed'] );
  217. break;
  218. }
  219. }
  220. }
  221. return $bot_info;
  222. }
  223. /**
  224. * nv_checkmobile()
  225. *
  226. * @return
  227. */
  228. function nv_checkmobile ( )
  229. {
  230. if ( isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) || isset( $_SERVER['HTTP_PROFILE'] ) || isset( $_SERVER['X-OperaMini-Features'] ) || isset( $_SERVER['UA-pixels'] ) ) return 1;
  231. if ( isset( $_SERVER['HTTP_ACCEPT'] ) && preg_match( "/wap\.|\.wap/i", $_SERVER["HTTP_ACCEPT"] ) ) return 1;
  232. if ( preg_match( "/Creative\ AutoUpdate/i", NV_USER_AGENT ) ) return 0;
  233. $uamatches = array( "midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto" );
  234. foreach ( $uamatches as $uastring )
  235. {
  236. if ( preg_match( "/" . $uastring . "/i", NV_USER_AGENT ) ) return 1;
  237. }
  238. return 0;
  239. }
  240. /**
  241. * nv_getBrowser()
  242. *
  243. * @param mixed $agent
  244. * @param mixed $brinifile
  245. * @return
  246. */
  247. function nv_getBrowser ( $agent, $brinifile )
  248. {
  249. $browsers = nv_parse_ini_file( $brinifile, true );
  250. foreach ( $browsers as $key => $info )
  251. {
  252. if ( preg_match( "#" . $info['rule'] . "#i", $agent, $results ) )
  253. {
  254. if ( isset( $results[1] ) ) return ( $key . '|' . $info['name'] . ' v' . $results[1] );
  255. else return ( $key . '|' . $info['name'] );
  256. }
  257. }
  258. return ( "Unknown|Unknown" );
  259. }
  260. /**
  261. * nv_getOs()
  262. *
  263. * @param mixed $agent
  264. * @param mixed $osinifile
  265. * @return
  266. */
  267. function nv_getOs ( $agent, $osinifile )
  268. {
  269. $os = nv_parse_ini_file( $osinifile, true );
  270. foreach ( $os as $key => $info )
  271. {
  272. if ( preg_match( "#" . $info['rule'] . "#i", $agent, $results ) )
  273. {
  274. if ( strstr( $key, "win" ) ) return ( $key . '|' . $info['name'] );
  275. elseif ( isset( $results[1] ) ) return ( $key . '|' . $info['name'] . ' ' . $results[1] );
  276. else return ( $key . '|' . $info['name'] );
  277. }
  278. }
  279. return ( "Unspecified|Unspecified" );
  280. }
  281. /**
  282. * nv_convertfromBytes()
  283. *
  284. * @param mixed $size
  285. * @return
  286. */
  287. function nv_convertfromBytes ( $size )
  288. {
  289. if ( $size <= 0 ) return '0 bytes';
  290. if ( $size == 1 ) return '1 bytes';
  291. if ( $size < 1024 ) return $size . ' bytes';
  292. $i = 0;
  293. $iec = array( "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" );
  294. while ( ( $size / 1024 ) > 1 )
  295. {
  296. $size = $size / 1024;
  297. $i ++;
  298. }
  299. return number_format( $size, 2 ) . ' ' . $iec[$i];
  300. }
  301. /**
  302. * nv_convertfromSec()
  303. *
  304. * @param mixed $sec
  305. * @return
  306. */
  307. function nv_convertfromSec ( $sec )
  308. {
  309. global $lang_global;
  310. $sec = intval( $sec );
  311. $min = 60;
  312. $hour = 60 * $min;
  313. $day = 24 * $hour;
  314. $year = 365 * $day;
  315. if ( $sec == 0 )
  316. {
  317. return "";
  318. }
  319. elseif ( $sec < $min )
  320. {
  321. return $sec . " " . $lang_global['sec'];
  322. }
  323. elseif ( $sec < $hour )
  324. {
  325. $sd = $sec % $min;
  326. return trim( floor( $sec / $min ) . " " . " " . $lang_global['min'] . ( $sd ? " " . nv_convertfromSec( $sd ) : "" ) );
  327. }
  328. elseif ( $sec < $day )
  329. {
  330. $sd = $sec % $hour;
  331. return trim( floor( $sec / $hour ) . " " . $lang_global['hour'] . ( $sd ? " " . nv_convertfromSec( $sd ) : "" ) );
  332. }
  333. elseif ( $sec < $year )
  334. {
  335. $sd = $sec % $day;
  336. return trim( floor( $sec / $day ) . " " . $lang_global['day'] . ( $sd ? " " . nv_convertfromSec( $sd ) : "" ) );
  337. }
  338. else
  339. {
  340. $sd = $sec % $year;
  341. return trim( floor( $sec / $year ) . " " . $lang_global['year'] . ( $sd ? " " . nv_convertfromSec( $sd ) : "" ) );
  342. }
  343. }
  344. /**
  345. * nv_converttoBytes()
  346. *
  347. * @param mixed $string
  348. * @return
  349. */
  350. function nv_converttoBytes ( $string )
  351. {
  352. if ( preg_match( '/^([0-9\.]+)[ ]*([b|k|m|g|t|p|e|z|y]*)/i', $string, $matches ) )
  353. {
  354. $suffixes = array( "B" => 0, "K" => 1, "M" => 2, "G" => 3, "T" => 4, "P" => 5, "E" => 6, "Z" => 7, "Y" => 8 );
  355. if ( empty( $matches[2] ) ) return $matches[1];
  356. if ( isset( $suffixes[strtoupper( $matches[2] )] ) ) return round( $matches[1] * pow( 1024, $suffixes[strtoupper( $matches[2] )] ) );
  357. }
  358. return false;
  359. }
  360. /**
  361. * nv_base64_encode()
  362. *
  363. * @param mixed $input
  364. * @return
  365. */
  366. function nv_base64_encode ( $input )
  367. {
  368. return strtr( base64_encode( $input ), '+/=', '-_,' );
  369. }
  370. /**
  371. * nv_base64_decode()
  372. *
  373. * @param mixed $input
  374. * @return
  375. */
  376. function nv_base64_decode ( $input )
  377. {
  378. return base64_decode( strtr( $input, '-_,', '+/=' ) );
  379. }
  380. /**
  381. * nv_function_exists()
  382. *
  383. * @param mixed $funcName
  384. * @return
  385. */
  386. function nv_function_exists ( $funcName )
  387. {
  388. global $sys_info;
  389. return ( function_exists( $funcName ) and ! in_array( $funcName, $sys_info['disable_functions'] ) );
  390. }
  391. /**
  392. * nv_class_exists()
  393. *
  394. * @param mixed $clName
  395. * @return
  396. */
  397. function nv_class_exists ( $clName )
  398. {
  399. global $sys_info;
  400. return ( class_exists( $clName ) and ! in_array( $clName, $sys_info['disable_functions'] ) );
  401. }
  402. /**
  403. * nv_check_valid_login()
  404. *
  405. * @param mixed $login
  406. * @param mixed $max
  407. * @param mixed $min
  408. * @return
  409. */
  410. function nv_check_valid_login ( $login, $max, $min )
  411. {
  412. global $lang_global, $global_config;
  413. $login = strip_tags( trim( $login ) );
  414. if ( empty( $login ) ) return $lang_global['username_empty'];
  415. elseif ( strlen( $login ) > $max ) return sprintf( $lang_global['usernamelong'], $login, $max );
  416. elseif ( strlen( $login ) < $min ) return sprintf( $lang_global['usernameadjective'], $login, $min );
  417. else return "";
  418. }
  419. /**
  420. * nv_check_valid_pass()
  421. *
  422. * @param mixed $pass
  423. * @param mixed $max
  424. * @param mixed $min
  425. * @return
  426. */
  427. function nv_check_valid_pass ( $pass, $max, $min )
  428. {
  429. global $lang_global, $global_config;
  430. $pass = strip_tags( trim( $pass ) );
  431. if ( empty( $pass ) ) return $lang_global['password_empty'];
  432. elseif ( strlen( $pass ) > $max ) return sprintf( $lang_global['passwordlong'], $pass, $max );
  433. elseif ( strlen( $pass ) < $min ) return sprintf( $lang_global['passwordadjective'], $pass, $min );
  434. else return "";
  435. }
  436. /**
  437. * nv_check_valid_email()
  438. *
  439. * @param mixed $mail
  440. * @return
  441. */
  442. function nv_check_valid_email ( $mail )
  443. {
  444. global $lang_global, $global_config;
  445. $mail = strip_tags( trim( $mail ) );
  446. if ( empty( $mail ) ) return $lang_global['email_empty'];
  447. if ( function_exists( 'filter_var' ) and filter_var( $mail, FILTER_VALIDATE_EMAIL ) === false )
  448. {
  449. return sprintf( $lang_global['email_incorrect'], $mail );
  450. }
  451. elseif ( ! preg_match( $global_config['check_email'], $mail ) )
  452. {
  453. return sprintf( $lang_global['email_incorrect'], $mail );
  454. }
  455. if ( ! preg_match( "/\.(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$/", $mail ) )
  456. {
  457. return sprintf( $lang_global['email_incorrect'], $mail );
  458. }
  459. return "";
  460. }
  461. /**
  462. * nv_capcha_txt()
  463. *
  464. * @param mixed $seccode
  465. * @param string $scaptcha
  466. * @return
  467. */
  468. function nv_capcha_txt ( $seccode, $scaptcha = "captcha" )
  469. {
  470. global $sys_info, $global_config, $nv_Request;
  471. $scaptcha = preg_replace( '/[^a-z0-9]/', '', $scaptcha );
  472. $skeycaptcha = ( $scaptcha == "captcha" ) ? "random_num" : "random_" . substr( $scaptcha, 0, 20 );
  473. $seccode = strtoupper( $seccode );
  474. $return = false;
  475. if ( ! $sys_info['gd_support'] ) return true;
  476. $random_num = $nv_Request->get_string( $skeycaptcha, 'session', 0 );
  477. $datekey = date( "F j" );
  478. $rcode = strtoupper( md5( NV_USER_AGENT . $global_config['sitekey'] . $random_num . $datekey ) );
  479. if ( preg_match( "/^[a-zA-Z0-9]{" . NV_GFX_NUM . "}$/", $seccode ) and $seccode == substr( $rcode, 2, NV_GFX_NUM ) )
  480. {
  481. $return = true;
  482. }
  483. mt_srand( ( double )microtime() * 1000000 );
  484. $maxran = 1000000;
  485. $random_num = mt_rand( 0, $maxran );
  486. $nv_Request->set_Session( $skeycaptcha, $random_num );
  487. return $return;
  488. }
  489. /**
  490. * nv_genpass()
  491. *
  492. * @param integer $length
  493. * @return
  494. */
  495. function nv_genpass ( $length = 8 )
  496. {
  497. $pass = chr( mt_rand( 65, 90 ) );
  498. for ( $k = 0; $k < $length - 1; $k ++ )
  499. {
  500. $probab = mt_rand( 1, 10 );
  501. $pass .= ( $probab <= 8 ) ? chr( mt_rand( 97, 122 ) ) : chr( mt_rand( 48, 57 ) );
  502. }
  503. return $pass;
  504. }
  505. /**
  506. * nv_EncodeEmail()
  507. *
  508. * @param mixed $strEmail
  509. * @param string $strDisplay
  510. * @param bool $blnCreateLink
  511. * @return
  512. */
  513. function nv_EncodeEmail ( $strEmail, $strDisplay = '', $blnCreateLink = true )
  514. {
  515. $strMailto = "&#109;&#097;&#105;&#108;&#116;&#111;&#058;";
  516. $strEncodedEmail = "";
  517. for ( $i = 0; $i < strlen( $strEmail ); $i ++ )
  518. {
  519. $strEncodedEmail .= "&#" . ord( substr( $strEmail, $i ) ) . ";";
  520. }
  521. $strDisplay = ( strlen( trim( $strDisplay ) ) > 0 ) ? $strDisplay : $strEncodedEmail;
  522. if ( $blnCreateLink ) return "<a href=\"" . $strMailto . $strEncodedEmail . "\">" . $strDisplay . "</a>";
  523. else return $strDisplay;
  524. }
  525. /**
  526. * nv_user_groups()
  527. *
  528. * @param mixed $in_groups
  529. * @return
  530. */
  531. function nv_user_groups ( $in_groups )
  532. {
  533. global $db;
  534. if ( empty( $in_groups ) ) return "";
  535. $groups = array();
  536. $sql = "SELECT `group_id` FROM `" . NV_GROUPS_GLOBALTABLE . "` WHERE `group_id` IN (" . $in_groups . ") AND `act`=1 AND (`exp_time`=0 OR `exp_time` >= " . NV_CURRENTTIME . ")";
  537. $result = $db->sql_query( $sql );
  538. while ( $row = $db->sql_fetchrow( $result ) )
  539. {
  540. $groups[] = $row['group_id'];
  541. }
  542. if ( empty( $groups ) ) return "";
  543. return implode( ",", $groups );
  544. }
  545. /**
  546. * nv_is_in_groups()
  547. *
  548. * @param mixed $in_groups
  549. * @param mixed $groups
  550. * @return
  551. */
  552. function nv_is_in_groups ( $in_groups, $groups )
  553. {
  554. if ( empty( $groups ) || empty( $in_groups ) ) return false;
  555. $in_groups = explode( ",", $in_groups );
  556. $groups = explode( ",", $groups );
  557. if ( array_intersect( $in_groups, $groups ) != array() ) return true;
  558. return false;
  559. }
  560. /**
  561. * nv_set_allow()
  562. *
  563. * @param mixed $who
  564. * @param mixed $groups
  565. * @return
  566. */
  567. function nv_set_allow ( $who, $groups )
  568. {
  569. global $user_info;
  570. if ( ! $who or ( $who == 1 and defined( 'NV_IS_USER' ) ) or ( $who == 2 and defined( 'NV_IS_ADMIN' ) ) )
  571. {
  572. return true;
  573. }
  574. elseif ( $who == 3 and ! empty( $groups ) and defined( 'NV_IS_USER' ) and nv_is_in_groups( $user_info['in_groups'], $groups ) )
  575. {
  576. return true;
  577. }
  578. return false;
  579. }
  580. /**
  581. * nv_date()
  582. *
  583. * @param mixed $format
  584. * @param integer $time
  585. * @return
  586. */
  587. function nv_date ( $format, $time = 0 )
  588. {
  589. global $lang_global;
  590. if ( ! $time ) $time = NV_CURRENTTIME;
  591. $return = date( $format, $time );
  592. $searchs = array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' );
  593. $replaces = array();
  594. foreach ( $searchs as $search )
  595. {
  596. $replaces[] = $lang_global[strtolower( $search )];
  597. }
  598. $return = str_replace( $searchs, $replaces, $return );
  599. $searchs = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );
  600. $replaces = array();
  601. foreach ( $searchs as $search )
  602. {
  603. $replaces[] = $lang_global[strtolower( $search )];
  604. }
  605. $return = str_replace( $searchs, $replaces, $return );
  606. return $return;
  607. }
  608. /**
  609. * nv_monthname()
  610. *
  611. * @param mixed $i
  612. * @return
  613. */
  614. function nv_monthname ( $i )
  615. {
  616. global $lang_global;
  617. $month_names = array( $lang_global['january'], $lang_global['february'], $lang_global['march'], $lang_global['april'], $lang_global['may'], $lang_global['june'], $lang_global['july'], $lang_global['august'], $lang_global['september'], $lang_global['october'], $lang_global['november'], $lang_global['december'] );
  618. $i = $i - 1;
  619. return $month_names[$i];
  620. }
  621. /**
  622. * nv_unhtmlspecialchars()
  623. *
  624. * @param mixed $string
  625. * @return
  626. */
  627. function nv_unhtmlspecialchars ( $string )
  628. {
  629. if ( empty( $string ) ) return $string;
  630. if ( is_array( $string ) )
  631. {
  632. foreach ( array_keys( $string ) as $key )
  633. {
  634. $string[$key] = nv_unhtmlspecialchars( $string[$key] );
  635. }
  636. }
  637. else
  638. {
  639. $search = array( '&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&#x005C;', '&#x002F;', '&#40;', '&#41;', '&#42;', '&#91;', '&#93;', '&#33;', '&#x3D;', '&#x23;', '&#x25;', '&#x5E;', '&#x3A;', '&#x7B;', '&#x7D;', '&#x60;', '&#x7E;' );
  640. $replace = array( '&', '\'', '"', '<', '>', '\\', '/', '(', ')', '*', '[', ']', '!', '=', '#', '%', '^', ':', '{', '}', '`', '~' );
  641. $string = str_replace( $search, $replace, $string );
  642. }
  643. return $string;
  644. }
  645. /**
  646. * nv_htmlspecialchars()
  647. *
  648. * @param mixed $string
  649. * @return
  650. */
  651. function nv_htmlspecialchars ( $string )
  652. {
  653. if ( empty( $string ) ) return $string;
  654. if ( is_array( $string ) )
  655. {
  656. foreach ( array_keys( $string ) as $key )
  657. {
  658. $string[$key] = nv_htmlspecialchars( $string[$key] );
  659. }
  660. }
  661. else
  662. {
  663. $search = array( '&', '\'', '"', '<', '>', '\\', '/', '(', ')', '*', '[', ']', '!', '=', '%', '^', ':', '{', '}', '`', '~' );
  664. $replace = array( '&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&#x005C;', '&#x002F;', '&#40;', '&#41;', '&#42;', '&#91;', '&#93;', '&#33;', '&#x3D;', '&#x25;', '&#x5E;', '&#x3A;', '&#x7B;', '&#x7D;', '&#x60;', '&#x7E;' );
  665. $string = str_replace( $replace, $search, $string );
  666. $string = str_replace( "&#x23;", "#", $string );
  667. $string = str_replace( $search, $replace, $string );
  668. $string = preg_replace( "/([^\&]+)\#/", "\\1&#x23;", $string );
  669. }
  670. return $string;
  671. }
  672. /**
  673. * strip_punctuation()
  674. *
  675. * @param mixed $text
  676. * @return
  677. */
  678. function strip_punctuation ( $text )
  679. {
  680. $urlbrackets = '\[\]\(\)';
  681. $urlspacebefore = ':;\'_\*%@&?!' . $urlbrackets;
  682. $urlspaceafter = '\.,:;\'\-_\*@&\/\\\\\?!#' . $urlbrackets;
  683. $urlall = '\.,:;\'\-_\*%@&\/\\\\\?!#' . $urlbrackets;
  684. $specialquotes = '\'"\*<>';
  685. $fullstop = '\x{002E}\x{FE52}\x{FF0E}';
  686. $comma = '\x{002C}\x{FE50}\x{FF0C}';
  687. $arabsep = '\x{066B}\x{066C}';
  688. $numseparators = $fullstop . $comma . $arabsep;
  689. $numbersign = '\x{0023}\x{FE5F}\x{FF03}';
  690. $percent = '\x{066A}\x{0025}\x{066A}\x{FE6A}\x{FF05}\x{2030}\x{2031}';
  691. $prime = '\x{2032}\x{2033}\x{2034}\x{2057}';
  692. $nummodifiers = $numbersign . $percent . $prime;
  693. return preg_replace( array( // Remove separator, control, formatting, surrogate, open/close quotes.
  694. '/[\p{Z}\p{Cc}\p{Cf}\p{Cs}\p{Pi}\p{Pf}]/u', // Remove other punctuation except special cases
  695. '/\p{Po}(?<![' . $specialquotes . $numseparators . $urlall . $nummodifiers . '])/u', // Remove non-URL open/close brackets, except URL brackets.
  696. '/[\p{Ps}\p{Pe}](?<![' . $urlbrackets . '])/u', // Remove special quotes, dashes, connectors, number separators, and URL characters followed by a space
  697. '/[' . $specialquotes . $numseparators . $urlspaceafter . '\p{Pd}\p{Pc}]+((?= )|$)/u', // Remove special quotes, connectors, and URL characters preceded by a space
  698. '/((?<= )|^)[' . $specialquotes . $urlspacebefore . '\p{Pc}]+/u', // Remove dashes preceded by a space, but not followed by a number
  699. '/((?<= )|^)\p{Pd}+(?![\p{N}\p{Sc}])/u', // Remove consecutive spaces
  700. '/ +/' ), ' ', $text );
  701. }
  702. /**
  703. * nv_nl2br()
  704. *
  705. * @param mixed $text
  706. * @param string $replacement
  707. * @return
  708. */
  709. function nv_nl2br ( $text, $replacement = '<br />' )
  710. {
  711. if ( empty( $text ) ) return '';
  712. return strtr( $text, array( "\r\n" => trim( $replacement ), "\r" => trim( $replacement ), "\n" => trim( $replacement ) ) );
  713. }
  714. /**
  715. * nv_br2nl()
  716. *
  717. * @param mixed $text
  718. * @return
  719. */
  720. function nv_br2nl ( $text )
  721. {
  722. if ( empty( $text ) ) return '';
  723. return preg_replace( '/\<br(\s*)?\/?(\s*)?\>/i', chr( 13 ) . chr( 10 ), $text );
  724. }
  725. /**
  726. * nv_editor_nl2br()
  727. *
  728. * @param mixed $text
  729. * @return
  730. */
  731. function nv_editor_nl2br ( $text )
  732. {
  733. if ( empty( $text ) ) return '';
  734. $replacement = defined( 'NV_EDITOR' ) ? '' : '<br />';
  735. return strtr( $text, array( "\r\n" => trim( $replacement ), "\r" => trim( $replacement ), "\n" => trim( $replacement ) ) );
  736. }
  737. /**
  738. * nv_editor_br2nl()
  739. *
  740. * @param mixed $text
  741. * @return
  742. */
  743. function nv_editor_br2nl ( $text )
  744. {
  745. if ( empty( $text ) ) return '';
  746. if ( defined( 'NV_EDITOR' ) ) return $text;
  747. return preg_replace( '/\<br(\s*)?\/?(\s*)?\>/i', chr( 13 ) . chr( 10 ), $text );
  748. }
  749. /**
  750. * filter_text_input()
  751. *
  752. * @param mixed $inputname
  753. * @param string $mode
  754. * @param string $default
  755. * @param bool $specialchars
  756. * @param integer $maxlength
  757. * @param mixed $preg_replace
  758. * @return
  759. */
  760. function filter_text_input ( $inputname, $mode = 'request', $default = '', $specialchars = false, $maxlength = 0, $preg_replace = array() )
  761. {
  762. global $nv_Request;
  763. $value = $nv_Request->get_string( $inputname, $mode, $default );
  764. $value = strip_tags( $value );
  765. if ( ( bool )$specialchars == true )
  766. {
  767. $value = nv_htmlspecialchars( $value );
  768. }
  769. if ( ( int )$maxlength > 0 )
  770. {
  771. $value = nv_substr( $value, 0, $maxlength );
  772. }
  773. if ( ! empty( $preg_replace ) )
  774. {
  775. if ( isset( $preg_replace['pattern'] ) and ! empty( $preg_replace['pattern'] ) and isset( $preg_replace['replacement'] ) )
  776. {
  777. $value = preg_replace( $preg_replace['pattern'], $preg_replace['replacement'], $value );
  778. }
  779. }
  780. return trim( $value );
  781. }
  782. /**
  783. * filter_text_textarea()
  784. *
  785. * @param mixed $inputname
  786. * @param string $default
  787. * @param string $allowed_html_tags
  788. * @param bool $save
  789. * @param string $nl2br_replacement
  790. * @return
  791. */
  792. function filter_text_textarea ( $inputname, $default = '', $allowed_html_tags = '', $save = false, $nl2br_replacement = '<br />' )
  793. {
  794. global $nv_Request;
  795. $value = $nv_Request->get_string( $inputname, 'post', $default );
  796. if ( empty( $value ) ) return $value;
  797. if ( ! empty( $allowed_html_tags ) )
  798. {
  799. $allowed_html_tags = array_map( "trim", explode( ",", $allowed_html_tags ) );
  800. $allowed_html_tags = "<" . implode( "><", $allowed_html_tags ) . ">";
  801. $value = strip_tags( $value, $allowed_html_tags );
  802. }
  803. if ( ( bool )$save ) $value = nv_nl2br( $value, $nl2br_replacement );
  804. return $value;
  805. }
  806. /**
  807. * nv_editor_filter_textarea()
  808. *
  809. * @param mixed $inputname
  810. * @param string $default
  811. * @param string $allowed_html_tags
  812. * @param bool $save
  813. * @param string $nl2br_replacement
  814. * @return
  815. */
  816. function nv_editor_filter_textarea ( $inputname, $default = '', $allowed_html_tags = '', $save = false, $nl2br_replacement = '<br />' )
  817. {
  818. global $nv_Request;
  819. $value = $nv_Request->get_string( $inputname, 'post', $default );
  820. if ( strip_tags( $value ) == '' ) return '';
  821. if ( ! empty( $allowed_html_tags ) and ! defined( 'NV_EDITOR' ) )
  822. {
  823. $allowed_html_tags = array_map( "trim", explode( ",", $allowed_html_tags ) );
  824. $allowed_html_tags = "<" . implode( "><", $allowed_html_tags ) . ">";
  825. $value = strip_tags( $value, $allowed_html_tags );
  826. }
  827. if ( ( bool )$save )
  828. {
  829. $value = nv_editor_nl2br( $value, $nl2br_replacement );
  830. }
  831. return $value;
  832. }
  833. /**
  834. * nv_get_keywords()
  835. *
  836. * @param string $content
  837. * @return
  838. */
  839. function nv_get_keywords ( $content = "" )
  840. {
  841. if ( empty( $content ) ) return ( "" );
  842. $content = strip_tags( $content );
  843. $content = nv_unhtmlspecialchars( $content );
  844. $content = strip_punctuation( $content );
  845. $content = trim( $content );
  846. $content = nv_strtolower( $content );
  847. $content = " " . $content . " ";
  848. $pattern_word = array();
  849. if ( NV_SITEWORDS_MIN_3WORDS_LENGTH > 0 and NV_SITEWORDS_MIN_3WORDS_PHRASE_OCCUR > 0 )
  850. {
  851. $pattern_word[] = "/[\s]+([\S]{" . NV_SITEWORDS_MIN_3WORDS_LENGTH . ",}\s[\S]{" . NV_SITEWORDS_MIN_3WORDS_LENGTH . ",}\s[\S]{" . NV_SITEWORDS_MIN_3WORDS_LENGTH . ",})(\s.*\\1){" . NV_SITEWORDS_MIN_3WORDS_PHRASE_OCCUR . ",}[\s]+/uis";
  852. }
  853. if ( NV_SITEWORDS_MIN_2WORDS_LENGTH > 0 and NV_SITEWORDS_MIN_2WORDS_PHRASE_OCCUR > 0 )
  854. {
  855. $pattern_word[] = "/[\s]+([\S]{" . NV_SITEWORDS_MIN_2WORDS_LENGTH . ",}\s[\S]{" . NV_SITEWORDS_MIN_2WORDS_LENGTH . ",})(\s.*\\1){" . NV_SITEWORDS_MIN_2WORDS_PHRASE_OCCUR . ",}[\s]+/uis";
  856. }
  857. if ( NV_SITEWORDS_MIN_WORD_LENGTH > 0 and NV_SITEWORDS_MIN_WORD_OCCUR > 0 )
  858. {
  859. $pattern_word[] = "/[\s]+([\S]{" . NV_SITEWORDS_MIN_WORD_LENGTH . ",})(\s.*\\1){" . NV_SITEWORDS_MIN_WORD_OCCUR . ",}[\s]+/uis";
  860. }
  861. if ( empty( $pattern_word ) ) return ( "" );
  862. $keywords = array();
  863. $lenght = 0;
  864. $max_strlen = min( NV_SITEWORDS_MAX_STRLEN, 300 );
  865. foreach ( $pattern_word as $pattern )
  866. {
  867. unset( $matches );
  868. while ( preg_match( $pattern, $content, $matches ) )
  869. {
  870. $keywords[] = $matches[1];
  871. $lenght += nv_strlen( $matches[1] );
  872. $content = preg_replace( "/[\s]+(" . preg_quote( $matches[1] ) . ")[\s]+/uis", " ", $content );
  873. if ( $lenght >= $max_strlen ) break;
  874. }
  875. if ( $lenght >= $max_strlen ) break;
  876. }
  877. $keywords = array_unique( $keywords );
  878. $keywords = implode( ",", $keywords );
  879. return $keywords;
  880. }
  881. /**
  882. * nv_sendmail()
  883. *
  884. * @param mixed $from
  885. * @param mixed $to
  886. * @param mixed $subject
  887. * @param mixed $message
  888. * @param string $files
  889. * @return
  890. */
  891. function nv_sendmail ( $from, $to, $subject, $message, $files = '' )
  892. {
  893. global $global_config, $lang_global, $sys_info;
  894. $sendmail_from = ini_get( 'sendmail_from' );
  895. require_once ( NV_ROOTDIR . '/includes/phpmailer/class.phpmailer.php' );
  896. try
  897. {
  898. $mail = new PHPMailer( true );
  899. $mail->CharSet = $global_config['site_charset'];
  900. $mailer_mode = strtolower( $global_config['mailer_mode'] );
  901. if ( $mailer_mode == 'smtp' )
  902. {
  903. $mail->IsSMTP();
  904. $mail->SMTPAuth = true;
  905. $mail->Port = $global_config['smtp_port'];
  906. $mail->Host = $global_config['smtp_host'];
  907. $mail->Username = $global_config['smtp_username'];
  908. $mail->Password = $global_config['smtp_password'];
  909. $SMTPSecure = intval( $global_config['smtp_ssl'] );
  910. switch ( $SMTPSecure )
  911. {
  912. case 1:
  913. $mail->SMTPSecure = 'ssl';
  914. break;
  915. case 2:
  916. $mail->SMTPSecure = 'tls';
  917. break;
  918. default:
  919. $mail->SMTPSecure = '';
  920. }
  921. }
  922. elseif ( $mailer_mode == 'sendmail' )
  923. {
  924. $mail->IsSendmail();
  925. }
  926. elseif ( ! in_array( 'mail', $sys_info['disable_functions'] ) )
  927. {
  928. $mail->IsMail();
  929. }
  930. else
  931. {
  932. return false;
  933. }
  934. $message = nv_change_buffer( $message );
  935. $message = nv_unhtmlspecialchars( $message );
  936. $subject = nv_unhtmlspecialchars( $subject );
  937. $mail->From = $sendmail_from;
  938. $mail->FromName = $global_config['site_name'];
  939. if ( is_array( $from ) )
  940. {
  941. $mail->AddReplyTo( $from[1], $from[0] );
  942. }
  943. else
  944. {
  945. $mail->AddReplyTo( $from );
  946. }
  947. $mail->AddAddress( $to );
  948. $mail->Subject = $subject;
  949. $mail->WordWrap = 120;
  950. $mail->MsgHTML( $message );
  951. $mail->IsHTML( true );
  952. if ( ! empty( $files ) )
  953. {
  954. $files = array_map( "trim", explode( ",", $files ) );
  955. foreach ( $files as $file )
  956. {
  957. $mail->AddAttachment( $file );
  958. }
  959. }
  960. $send = $mail->Send();
  961. if ( ! $send )
  962. {
  963. trigger_error( $mail->ErrorInfo, E_USER_WARNING );
  964. }
  965. return $send;
  966. }
  967. catch ( phpmailerException $e )
  968. {
  969. trigger_error( $e->errorMessage(), E_USER_WARNING );
  970. return false;
  971. }
  972. }
  973. /**
  974. * nv_generate_page()
  975. *
  976. * @param mixed $base_url
  977. * @param mixed $num_items
  978. * @param mixed $per_page
  979. * @param mixed $start_item
  980. * @param bool $add_prevnext_text
  981. * @param bool $onclick
  982. * @param string $js_func_name
  983. * @param string $containerid
  984. * @return
  985. */
  986. function nv_generate_page ( $base_url, $num_items, $per_page, $start_item, $add_prevnext_text = true, $onclick = false, $js_func_name = 'nv_urldecode_ajax', $containerid = 'generate_page' )
  987. {
  988. global $lang_global;
  989. $total_pages = ceil( $num_items / $per_page );
  990. if ( $total_pages == 1 ) return '';
  991. @$on_page = floor( $start_item / $per_page ) + 1;
  992. if ( ! is_array( $base_url ) )
  993. {
  994. $amp = preg_match( "/\?/", $base_url ) ? "&amp;" : "?";
  995. $amp .= "page=";
  996. }
  997. else
  998. {
  999. $amp = $base_url['amp'];
  1000. $base_url = $base_url['link'];
  1001. }
  1002. $page_string = "";
  1003. if ( $total_pages > 10 )
  1004. {
  1005. $init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;
  1006. for ( $i = 1; $i <= $init_page_max; $i ++ )
  1007. {
  1008. $href = ! $onclick ? "href=\"" . $base_url . $amp . ( ( $i - 1 ) * $per_page ) . "\"" : "href=\"javascript:void(0)\" onclick=\"" . $js_func_name . "('" . rawurlencode( nv_unhtmlspecialchars( $base_url . $amp . ( ( $i - 1 ) * $per_page ) ) ) . "','" . $containerid . "')\"";
  1009. $page_string .= ( $i == $on_page ) ? "<strong>" . $i . "</strong>" : "<a " . $href . ">" . $i . "</a>";
  1010. if ( $i < $init_page_max ) $page_string .= ", ";
  1011. }
  1012. if ( $total_pages > 3 )
  1013. {
  1014. if ( $on_page > 1 && $on_page < $total_pages )
  1015. {
  1016. $page_string .= ( $on_page > 5 ) ? " ... " : ", ";
  1017. $init_page_min = ( $on_page > 4 ) ? $on_page : 5;
  1018. $init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;
  1019. for ( $i = $init_page_min - 1; $i < $init_page_max + 2; $i ++ )
  1020. {
  1021. $href = ! $onclick ? "href=\"" . $base_url . $amp . ( ( $i - 1 ) * $per_page ) . "\"" : "href=\"javascript:void(0)\" onclick=\"" . $js_func_name . "('" . rawurlencode( nv_unhtmlspecialchars( $base_url . $amp . ( ( $i - 1 ) * $per_page ) ) ) . "','" . $containerid . "')\"";
  1022. $page_string .= ( $i == $on_page ) ? "<strong>" . $i . "</strong>" : "<a " . $href . ">" . $i . "</a>";
  1023. if ( $i < $init_page_max + 1 )
  1024. {
  1025. $page_string .= ", ";
  1026. }
  1027. }
  1028. $page_string .= ( $on_page < $total_pages - 4 ) ? " ... " : ", ";
  1029. }
  1030. else
  1031. {
  1032. $page_string .= " ... ";
  1033. }
  1034. for ( $i = $total_pages - 2; $i < $total_pages + 1; $i ++ )
  1035. {
  1036. $href = ! $onclick ? "href=\"" . $base_url . $amp . ( ( $i - 1 ) * $per_page ) . "\"" : "href=\"javascript:void(0)\" onclick=\"" . $js_func_name . "('" . rawurlencode( nv_unhtmlspecialchars( $base_url . $amp . ( ( $i - 1 ) * $per_page ) ) ) . "','" . $containerid . "')\"";
  1037. $page_string .= ( $i == $on_page ) ? "<strong>" . $i . "</strong>" : "<a " . $href . ">" . $i . "</a>";
  1038. if ( $i < $total_pages )
  1039. {
  1040. $page_string .= ", ";
  1041. }
  1042. }
  1043. }
  1044. }
  1045. else
  1046. {
  1047. for ( $i = 1; $i < $total_pages + 1; $i ++ )
  1048. {
  1049. $href = ! $onclick ? "href=\"" . $base_url . $amp . ( ( $i - 1 ) * $per_page ) . "\"" : "href=\"javascript:void(0)\" onclick=\"" . $js_func_name . "('" . rawurlencode( nv_unhtmlspecialchars( $base_url . $amp . ( ( $i - 1 ) * $per_page ) ) ) . "','" . $containerid . "')\"";
  1050. $page_string .= ( $i == $on_page ) ? "<strong>" . $i . "</strong>" : "<a " . $href . ">" . $i . "</a>";
  1051. if ( $i < $total_pages )
  1052. {
  1053. $page_string .= ", ";
  1054. }
  1055. }
  1056. }
  1057. if ( $add_prevnext_text )
  1058. {
  1059. if ( $on_page > 1 )
  1060. {
  1061. $href = ! $onclick ? "href=\"" . $base_url . $amp . ( ( $on_page - 2 ) * $per_page ) . "\"" : "href=\"javascript:void(0)\" onclick=\"" . $js_func_name . "('" . rawurlencode( nv_unhtmlspecialchars( $base_url . $amp . ( ( $on_page - 2 ) * $per_page ) ) ) . "','" . $containerid . "')\"";
  1062. $page_string = "&nbsp;&nbsp;<span><a " . $href . ">" . $lang_global['pageprev'] . "</a></span>&nbsp;&nbsp;" . $page_string;
  1063. }
  1064. if ( $on_page < $total_pages )
  1065. {
  1066. $href = ! $onclick ? "href=\"" . $base_url . $amp . ( $on_page * $per_page ) . "\"" : "href=\"javascript:void(0)\" onclick=\"" . $js_func_name . "('" . rawurlencode( nv_unhtmlspecialchars( $base_url . $amp . ( $on_page * $per_page ) ) ) . "','" . $containerid . "')\"";
  1067. $page_string .= "&nbsp;&nbsp;<span><a " . $href . ">" . $lang_global['pagenext'] . "</a></span>";
  1068. }
  1069. }
  1070. return $page_string;
  1071. }
  1072. /**
  1073. * nv_is_url()
  1074. *
  1075. * @param mixed $url
  1076. * @return
  1077. */
  1078. function nv_is_url ( $url )
  1079. {
  1080. global $ips;
  1081. $url = substr( $url, - 1 ) == "/" ? substr( $url, 0, - 1 ) : $url;
  1082. if ( empty( $url ) ) return false;
  1083. $url = nv_strtolower( $url );
  1084. if ( ! ( $parts = @parse_url( $url ) ) ) return false;
  1085. else
  1086. {
  1087. if ( ! isset( $parts['scheme'] ) or ! isset( $parts['host'] ) or ( $parts['scheme'] != "http" && $parts['scheme'] != "https" && $parts['scheme'] != "ftp" && $parts['scheme'] != "gopher" ) )
  1088. {
  1089. return false;
  1090. }
  1091. elseif ( ! preg_match( "/^[0-9a-z]([\-\.]?[0-9a-z])*\.(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$/", $parts['host'] ) and ! $ips->nv_validip( $parts['host'] ) )
  1092. {
  1093. return false;
  1094. }
  1095. elseif ( isset( $parts['user'] ) and ! preg_match( "/^([0-9a-z\-]|[\_])*$/", $parts['user'] ) )
  1096. {
  1097. return false;
  1098. }
  1099. elseif ( isset( $parts['pass'] ) and ! preg_match( "/^([0-9a-z\-]|[\_])*$/", $parts['pass'] ) )
  1100. {
  1101. return false;
  1102. }
  1103. elseif ( isset( $parts['path'] ) and ! preg_match( "/^[0-9A-Za-z\/\_\.\@\~\-\%\\s]*$/", $parts['path'] ) )
  1104. {
  1105. return false;
  1106. }
  1107. elseif ( isset( $parts['query'] ) and ! preg_match( "/^[0-9a-z\-\_\/\?\&\=\#\.\,\;\%\\s]*$/", $parts['query'] ) )
  1108. {
  1109. return false;
  1110. }
  1111. }
  1112. return true;
  1113. }
  1114. /**
  1115. * nv_check_url()
  1116. *
  1117. * @param mixed $url
  1118. * @param bool $is_200
  1119. * @return
  1120. */
  1121. function nv_check_url ( $url, $is_200 = 0 )
  1122. {
  1123. if ( empty( $url ) ) return false;
  1124. $url = str_replace( " ", "%20", $url );
  1125. $allow_url_fopen = ( ini_get( 'allow_url_fopen' ) == '1' || strtolower( ini_get( 'allow_url_fopen' ) ) == 'on' ) ? 1 : 0;
  1126. if ( nv_function_exists( 'get_headers' ) and $allow_url_fopen == 1 )
  1127. {
  1128. $res = get_headers( $url );
  1129. }
  1130. elseif ( nv_function_exists( 'curl_init' ) and nv_function_exists( 'curl_exec' ) )
  1131. {
  1132. $url_info = @parse_url( $url );
  1133. $port = isset( $url_info['port'] ) ? intval( $url_info['port'] ) : 80;
  1134. $userAgents = array( //
  1135. 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0', //
  1136. 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', //
  1137. 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)', //
  1138. 'Mozilla/4.8 [en] (Windows NT 6.0; U)', //
  1139. 'Opera/9.25 (Windows NT 6.0; U; en)' ); //
  1140. $safe_mode = ( ini_get( 'safe_mode' ) == '1' || strtolower( ini_get( 'safe_mode' ) ) == 'on' ) ? 1 : 0;
  1141. $open_basedir = ( ini_get( 'open_basedir' ) == '1' || strtolower( ini_get( 'open_basedir' ) ) == 'on' ) ? 1 : 0;
  1142. srand( ( float )microtime() * 10000000 );
  1143. $rand = array_rand( $userAgents );
  1144. $agent = $userAgents[$rand];
  1145. $curl = curl_init( $url );
  1146. curl_setopt( $curl, CURLOPT_HEADER, true );
  1147. curl_setopt( $curl, CURLOPT_NOBODY, true );
  1148. curl_setopt( $curl, CURLOPT_PORT, $port );
  1149. if ( ! $safe_mode and $open_basedir )
  1150. {
  1151. curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  1152. }
  1153. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  1154. curl_setopt( $curl, CURLOPT_TIMEOUT, 15 );
  1155. curl_setopt( $curl, CURLOPT_USERAGENT, $agent );
  1156. $response = curl_exec( $curl );
  1157. curl_close( $curl );
  1158. if ( $response === false )
  1159. {
  1160. trigger_error( curl_error( $curl ), E_USER_WARNING );
  1161. return false;
  1162. }
  1163. else
  1164. {
  1165. $res = explode( "\n", $response );
  1166. }
  1167. }
  1168. elseif ( nv_function_exists( 'fsockopen' ) and nv_function_exists( 'fgets' ) )
  1169. {
  1170. $res = array();
  1171. $url_info = parse_url( $url );
  1172. $port = isset( $url_info['port'] ) ? intval( $url_info['port'] ) : 80;
  1173. $fp = fsockopen( $url_info['host'], $port, $errno, $errstr, 15 );
  1174. if ( $fp )
  1175. {
  1176. $path = ! empty( $url_info['path'] ) ? $url_info['path'] : '/';
  1177. $path .= ! empty( $url_info['query'] ) ? '?' . $url_info['query'] : '';
  1178. fputs( $fp, "HEAD " . $path . " HTTP/1.0\r\n" );
  1179. fputs( $fp, "Host: " . $url_info['host'] . ":" . $port . "\r\n" );
  1180. fputs( $fp, "Connection: close\r\n\r\n" );
  1181. while ( ! feof( $fp ) )
  1182. {
  1183. if ( $header = trim( fgets( $fp, 1024 ) ) )
  1184. {
  1185. $res[] = $header;
  1186. }
  1187. }
  1188. }
  1189. else
  1190. {
  1191. trigger_error( $errstr, E_USER_WARNING );
  1192. return false;
  1193. }
  1194. }
  1195. else
  1196. {
  1197. trigger_error( 'error server no support check url', E_USER_WARNING );
  1198. return false;
  1199. }
  1200. if ( empty( $res ) ) return false;
  1201. if ( preg_match( "/(200)/", $res[0] ) ) return true;
  1202. if ( $is_200 > 5 ) return false;
  1203. if ( preg_match( "/(301)|(302)|(303)/", $res[0] ) )
  1204. {
  1205. foreach ( $res as $k => $v )
  1206. {
  1207. unset( $matches );
  1208. if ( preg_match( "/location:\s(.*?)$/is", $v, $matches ) )
  1209. {
  1210. $is_200 ++;
  1211. $location = trim( $matches[1] );
  1212. return nv_check_url( $location, $is_200 );
  1213. }
  1214. }
  1215. }
  1216. return false;
  1217. }
  1218. // function IP
  1219. /**
  1220. * nv_ParseIP()
  1221. *
  1222. * @param mixed $ip
  1223. * @return
  1224. */
  1225. function nv_ParseIP ( $ip )
  1226. {
  1227. global $sys_info;
  1228. if ( $ip == '127.0.0.1' || $ip == '0.0.0.1' ) return "localhost";
  1229. if ( ! function_exists( "fsockopen" ) or in_array( 'fsockopen', $sys_info['disable_functions'] ) ) return false;
  1230. if ( ! $fp = @fsockopen( "whois.arin.net", 43, $errno, $errstr, 10 ) ) return false;
  1231. if ( @fwrite( $fp, $ip . "\r\n" ) === false )
  1232. {
  1233. @fclose( $fp );
  1234. return false;
  1235. }
  1236. $response = "";
  1237. while ( ! @feof( $fp ) )
  1238. {
  1239. $response .= @fgets( $fp, 4096 );
  1240. }
  1241. @fclose( $fp );
  1242. $extra = "";
  1243. $nextServer = "";
  1244. if ( preg_match( "/" . preg_quote( "nic.ad.jp" ) . "/", $response ) )
  1245. {
  1246. $nextServer = "whois.nic.ad.jp";
  1247. $extra = "/e";
  1248. }
  1249. else
  1250. {
  1251. if ( preg_match( "/" . preg_quote( "whois.registro.br" ) . "/", $response ) ) $nextServer = "whois.registro.br";
  1252. elseif ( preg_match( "/" . preg_quote( "whois.apnic.net" ) . "/", $response ) ) $nextServer = "whois.apnic.net";
  1253. elseif ( preg_match( "/" . preg_quote( "ripe.net" ) . "/", $response ) ) $nextServer = "whois.ripe.net";
  1254. }
  1255. if ( ! empty( $nextServer ) )
  1256. {
  1257. $response = "";
  1258. if ( ! $fp = @fsockopen( $nextServer, 43, $errno, $errstr, 10 ) ) return false;
  1259. if ( @fwrite( $fp, $ip . $extra . "\r\n" ) === false )
  1260. {
  1261. @fclose( $fp );
  1262. return false;
  1263. }
  1264. while ( ! @feof( $fp ) )
  1265. {
  1266. $response .= @fgets( $fp, 4096 );
  1267. }
  1268. @fclose( $fp );
  1269. }
  1270. return $response;
  1271. }
  1272. /**
  1273. * nv_getCountry()
  1274. *
  1275. * @param mixed $ip
  1276. * @return
  1277. */
  1278. function nv_getCountry ( $ip )
  1279. {
  1280. $result = nv_ParseIP( $ip );
  1281. if ( empty( $result ) or $result == "localhost" )
  1282. {
  1283. return array( "unkown", "", "" );
  1284. }
  1285. unset( $arr );
  1286. if ( preg_match( '/^\x20*country\x20*:\x20*([A-Z]{2})/im', $result, $arr ) )
  1287. {
  1288. include ( NV_ROOTDIR . "/includes/ip_files/countries.php" );
  1289. $code = strtoupper( $arr[1] );
  1290. if ( isset( $countries[$code] ) )
  1291. {
  1292. return array( $arr[1], $countries[$code][0], $countries[$code][1] );
  1293. }
  1294. }
  1295. return array( "unkown", "", "" );
  1296. }
  1297. /**
  1298. * nv_getCountry_from_file()
  1299. *
  1300. * @param mixed $ip
  1301. * @return
  1302. */
  1303. function nv_getCountry_from_file ( $ip )
  1304. {
  1305. $numbers = preg_split( "/\./", $ip );
  1306. $ranges = $countries = array();
  1307. $two_letter_country_code = $three_letter_country_code = $country_name = "";
  1308. include ( NV_ROOTDIR . "/includes/ip_files/" . $numbers[0] . ".php" );
  1309. $code = ( $numbers[0] * 16777216 ) + ( $numbers[1] * 65536 ) + ( $numbers[2] * 256 ) + ( $numbers[3] );
  1310. if ( ! empty( $ranges ) )
  1311. {
  1312. foreach ( $ranges as $key => $value )
  1313. {
  1314. if ( $key <= $code )
  1315. {
  1316. if ( $ranges[$key][0] >= $code )
  1317. {
  1318. $two_letter_country_code = $ranges[$key][1];
  1319. break;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. if ( $two_letter_country_code == "" )
  1325. {
  1326. return nv_getCountry( $ip );
  1327. }
  1328. else
  1329. {
  1330. include ( NV_ROOTDIR . "/includes/ip_files/countries.php" );
  1331. $three_letter_country_code = $countries[$two_letter_country_code][0];
  1332. $country_name = $countries[$two_letter_country_code][1];
  1333. }
  1334. return array( $two_letter_country_code, $three_letter_country_code, $country_name );
  1335. }
  1336. /**
  1337. * nv_check_rewrite_file()
  1338. *
  1339. * @return
  1340. */
  1341. function nv_check_rewrite_file ( )
  1342. {
  1343. global $sys_info;
  1344. if ( $sys_info['supports_rewrite'] == 'rewrite_mode_apache' )
  1345. {
  1346. if ( ! file_exists( NV_ROOTDIR . '/.htaccess' ) ) return false;
  1347. $htaccess = @file_get_contents( NV_ROOTDIR . '/.htaccess' );
  1348. if ( ! preg_match( "/\#nukeviet\_rewrite\_start(.*)\#nukeviet\_rewrite\_end/s", $htaccess ) ) return false;
  1349. return true;
  1350. }
  1351. elseif ( $sys_info['supports_rewrite'] == 'rewrite_mode_iis' )
  1352. {
  1353. if ( ! file_exists( NV_ROOTDIR . '/web.config' ) ) return false;
  1354. $web_config = @file_get_contents( NV_ROOTDIR . '/web.config' );
  1355. if ( ! preg_match( "/<rule name=\"nv_rule_rewrite\">(.*)<\/rule>/s", $web_config ) ) return false;
  1356. return true;
  1357. }
  1358. else
  1359. {
  1360. return false;
  1361. }
  1362. }
  1363. /**
  1364. * nv_url_rewrite()
  1365. *
  1366. * @param mixed $buffer
  1367. * @param bool $is_url
  1368. * @return
  1369. */
  1370. function nv_url_rewrite ( $buffer, $is_url = false )
  1371. {
  1372. global $global_config, $module_name, $sys_info, $rewrite;
  1373. if ( ! empty( $rewrite ) )
  1374. {
  1375. if ( $is_url ) $buffer = "\"" . $buffer . "\"";
  1376. $buffer = preg_replace( array_keys( $rewrite ), array_values( $rewrite ), $buffer );
  1377. if ( $is_url ) $buffer = substr( $buffer, 1, - 1 );
  1378. }
  1379. return $buffer;
  1380. }
  1381. /**
  1382. * nv_valid_html()
  1383. *
  1384. * @param mixed $html
  1385. * @param mixed $config
  1386. * @param string $encoding
  1387. * @return
  1388. */
  1389. function nv_valid_html ( $html, $config, $encoding = 'utf8' )
  1390. {
  1391. global $sys_info;
  1392. if ( $sys_info['supports_tidy'] == "class" )
  1393. {
  1394. //PHP 5
  1395. $tidy = new tidy();
  1396. $tidy->parseString( $html, $config, $encoding );
  1397. $tidy->cleanRepair();
  1398. return $tidy;
  1399. }
  1400. elseif ( $sys_info['supports_tidy'] == "func" )
  1401. {
  1402. $tidy = tidy_parse_string( $html, $config, $encoding );
  1403. tidy_clean_repair();
  1404. return $tidy;
  1405. }
  1406. return $html;
  1407. }
  1408. /**
  1409. * nv_change_buffer()
  1410. *
  1411. * @param mixed $buffer
  1412. * @return
  1413. */
  1414. function nv_change_buffer ( $buffer )
  1415. {
  1416. global $db, $sys_info, $global_config;
  1417. $buffer = $db->unfixdb( $buffer );
  1418. $buffer = nv_url_rewrite( $buffer );
  1419. if ( defined( "NV_ANTI_IFRAME" ) and NV_ANTI_IFRAME )
  1420. {
  1421. $buffer = preg_replace( "/(<body[^>]*>)/", "$1\r\n<script type=\"text/javascript\">if(window.top!==window.self){document.write=\"\";window.top.location=window.self.location;setTimeout(function(){document.body.innerHTML=\"\"},1);window.self.onload=function(){document.body.innerHTML=\"\"}};</script>", $buffer, 1 );
  1422. }
  1423. if ( ! empty( $global_config['googleAnalyticsID'] ) and preg_match( '/^UA-\d{4,}-\d+$/', $global_config['googleAnalyticsID'] ) )
  1424. {
  1425. $dp = "";
  1426. if ( $global_config['googleAnalyticsSetDomainName'] == 1 )
  1427. {
  1428. $dp .= "_gaq.push([\"_setDomainName\",\"" . $global_config['cookie_domain'] . "\"]);";
  1429. }
  1430. elseif ( $global_config['googleAnalyticsSetDomainName'] == 2 )
  1431. {
  1432. $dp .= "_gaq.push([\"_setDomainName\",\"none\"]);_gaq.push([\"_setAllowLinker\",true]);";
  1433. }
  1434. $googleAnalytics = "<script type=\"text/javascript\">\r\n";
  1435. $googleAnalytics .= "//<![CDATA[\r\n";
  1436. $googleAnalytics .= "var _gaq=_gaq||[];_gaq.push([\"_setAccount\",\"" . $global_config['googleAnalyticsID'] . "\"]);" . $dp . "_gaq.push([\"_trackPageview\"]);(function(){var a=document.createElement(\"script\");a.type=\"text/javascript\";a.async=true;a.src=(\"https:\"==document.location.protocol?\"https://ssl\":\"http://www\")+\".google-analytics.com/ga.…

Large files files are truncated, but you can click here to view the full file