PageRenderTime 48ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/lib/lib_misc.php

http://pixie-cms.googlecode.com/
PHP | 910 lines | 578 code | 157 blank | 175 comment | 127 complexity | 6617236b83b6f6d8673d2078fbbef469 MD5 | raw file
  1. <?php
  2. if (!defined('DIRECT_ACCESS')) {
  3. header('Location: ../../');
  4. exit();
  5. }
  6. /**
  7. * Pixie: The Small, Simple, Site Maker.
  8. *
  9. * Licence: GNU General Public License v3
  10. * Copyright (C) 2010, Scott Evans
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see http://www.gnu.org/licenses/
  24. *
  25. * Title: lib_misc
  26. *
  27. * @package Pixie
  28. * @copyright 2008-2010 Scott Evans
  29. * @author Scott Evans
  30. * @author Sam Collett
  31. * @author Tony White
  32. * @author Isa Worcs
  33. * @link http://www.getpixie.co.uk
  34. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3
  35. *
  36. */
  37. // ------------------------------------------------------------------
  38. /* Set up debugging */
  39. // ------------------------------------------------------------------
  40. if (defined('PIXIE_DEBUG')) {
  41. pixieExit();
  42. exit();
  43. }
  44. define('PIXIE_DEBUG', 'no');
  45. /* Set debug to yes to log errors */
  46. // ------------------------------------------------------------------
  47. /**
  48. * Re-implementation of PHP 5's stripos()
  49. *
  50. * Borrowed from simplepie for php4 (admin/lib/lib_simplepie.php)
  51. *
  52. * Returns the numeric position of the first occurrence of needle in the
  53. * haystack string.
  54. *
  55. * @static
  56. * @access string
  57. * @param object $haystack
  58. * @param string $needle Note that the needle may be a string of one or more
  59. * characters. If needle is not a string, it is converted to an integer
  60. * and applied as the ordinal value of a character.
  61. * @param int $offset The optional offset parameter allows you to specify which
  62. * character in haystack to start searching. The position returned is still
  63. * relative to the beginning of haystack.
  64. * @return bool If needle is not found, stripos() will return boolean false.
  65. */
  66. if (!function_exists('stripos')) {
  67. function stripos($haystack, $needle, $offset = 0) {
  68. if (is_string($needle)) {
  69. $needle = strtolower($needle);
  70. }
  71. elseif (is_int($needle) || is_bool($needle) || is_double($needle)) {
  72. $needle = strtolower(chr($needle));
  73. }
  74. else {
  75. trigger_error('needle is not a string or an integer', E_USER_WARNING);
  76. return false;
  77. }
  78. return strpos(strtolower($haystack), $needle, $offset);
  79. }
  80. }
  81. /* An exit on error function */
  82. // ------------------------------------------------------------------
  83. function pixieExit() {
  84. header('Status: 503 Service Unavailable'); /* 503 status might discourage search engines from indexing or caching the error message */
  85. return <<<eod
  86. <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
  87. <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
  88. <head>
  89. <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />
  90. <title>Pixie (www.getpixie.co.uk) - Security Warning</title>
  91. <style type=\"text/css\">
  92. body{font-family:Arial,'Lucida Grande',Verdana,Sans-Serif;color: #333;}
  93. a, a:visited{text-decoration: none;color: #0497d3;}
  94. a:hover{color: #191919;text-decoration: none;}
  95. .helper{position: relative;top: 60px;border: 5px solid #e1e1e1;clear: left;padding: 15px 30px;margin: 0 auto;background-color: #F0F0F0;width: 500px;line-height: 15pt;}
  96. </style>
  97. </head>
  98. <body>
  99. <div class=\"helper\"><h3>Security Warning</h3>
  100. <p><a href=\"http://www.getpixie.co.uk\" alt=\"Get Pixie!\">Pixie</a> has blocked your request to this site due to security concerns. The site administrator has been notified of your details. Please try to visit this site again later if you have recieved this message in error.</p>
  101. </div>
  102. </body>
  103. </html>
  104. eod;
  105. }
  106. // ------------------------------------------------------------------
  107. /* Generate a new password */
  108. // ------------------------------------------------------------------
  109. function generate_password( $length = 10 )
  110. {
  111. $pass = "";
  112. $chars = '023456789bcdfghjkmnpqrstvwxyz';
  113. $i = 0;
  114. while ( $i < $length ) {
  115. $char = substr( $chars, mt_rand( 0, strlen( $chars ) - 1 ), 1 );
  116. if ( !strstr( $pass, $char ) ) {
  117. $pass .= $char;
  118. $i++;
  119. }
  120. }
  121. return $pass;
  122. }
  123. // ------------------------------------------------------------------
  124. /* Get the first word in a _ seperated string */
  125. // ------------------------------------------------------------------
  126. function first_word( $theString )
  127. {
  128. $stringParts = explode( '_', $theString );
  129. return $stringParts[0];
  130. }
  131. // ------------------------------------------------------------------
  132. /* Get the last word in a _ seperated string */
  133. // ------------------------------------------------------------------
  134. function last_word( $theString )
  135. {
  136. $stringParts = explode( '_', $theString );
  137. return array_pop( $stringParts );
  138. }
  139. // ------------------------------------------------------------------
  140. /* Get the first word in a string */
  141. // ------------------------------------------------------------------
  142. function firstword( $theString )
  143. {
  144. $stringParts = explode( " ", $theString );
  145. return $stringParts[0];
  146. }
  147. // ------------------------------------------------------------------
  148. /* Get the last word in a string */
  149. // ------------------------------------------------------------------
  150. function lastword( $theString )
  151. {
  152. $stringParts = explode( " ", $theString );
  153. return array_pop( $stringParts );
  154. }
  155. // ------------------------------------------------------------------
  156. /* Get a var from $_SERVER global array, or create it */
  157. // ------------------------------------------------------------------
  158. function serverSet( $thing )
  159. {
  160. return ( isset( $_SERVER[$thing] ) ) ? $_SERVER[$thing] : '';
  161. }
  162. // ------------------------------------------------------------------
  163. /* Protection against those who'd bomb the site by GET */
  164. // ------------------------------------------------------------------
  165. function bombShelter()
  166. {
  167. $in = serverset( 'REQUEST_URI' );
  168. $ip = $_SERVER['REMOTE_ADDR'];
  169. if ( strlen( $in ) > 260 ) {
  170. pixieExit();
  171. }
  172. }
  173. // ------------------------------------------------------------------
  174. /* Prevents the super global $_REQUEST array's variables from poisioning */
  175. // ------------------------------------------------------------------
  176. function globalSec( $page_location, $sec_check )
  177. {
  178. global $clean_urls;
  179. /* .htaccess already has a rule for this, we don't need to do it twice */
  180. if ( ( $clean_urls != 'yes' ) && ( $sec_check === 1 ) ) {
  181. if ( isset( $_REQUEST['_GET'] ) ) {
  182. pixieExit();
  183. }
  184. if ( isset( $_REQUEST['_POST'] ) ) {
  185. pixieExit();
  186. }
  187. if ( isset( $_REQUEST['_COOKIE'] ) ) {
  188. pixieExit();
  189. }
  190. if ( isset( $_REQUEST['_SESSION'] ) ) {
  191. pixieExit();
  192. }
  193. if ( isset( $_REQUEST['GLOBALS'] ) ) {
  194. pixieExit();
  195. }
  196. if ( isset( $_REQUEST['_FILES'] ) ) {
  197. pixieExit();
  198. }
  199. if ( isset( $_REQUEST['_REQUEST'] ) ) {
  200. pixieExit();
  201. }
  202. if ( isset( $_REQUEST['_SERVER'] ) ) {
  203. pixieExit();
  204. }
  205. }
  206. }
  207. // ------------------------------------------------------------------
  208. /* A workaround for old versions of php */
  209. // ------------------------------------------------------------------
  210. function doSlash( $in )
  211. {
  212. if ( phpversion() >= '4.3.0' ) {
  213. return doArray( $in, 'mysql_real_escape_string' );
  214. } else {
  215. return doArray( $in, 'mysql_escape_string' );
  216. }
  217. }
  218. // ------------------------------------------------------------------
  219. /* An array function */
  220. // ------------------------------------------------------------------
  221. function doArray( $in, $function )
  222. {
  223. return is_array( $in ) ? array_map( $function, $in ) : $function( $in );
  224. }
  225. //-------------------------------------------------------------------
  226. /* A function to simply string in item_name format */
  227. // ------------------------------------------------------------------
  228. function simplify( $string )
  229. {
  230. $out = str_replace( '_', " ", $string );
  231. $strlen = strlen( $out );
  232. $max = 150; // find somwhere better for this?
  233. if ( $strlen > $max ) {
  234. $out = substr( $out, 0, $max ) . '...';
  235. }
  236. return ucfirst( $out );
  237. }
  238. //-------------------------------------------------------------------
  239. /* A function chop length of string */
  240. // ------------------------------------------------------------------
  241. function chopme( $string, $length )
  242. {
  243. $strlen = strlen( $string );
  244. $max = $length;
  245. if ( $strlen > $max ) {
  246. $string = substr( $string, 0, $max ) . '...';
  247. }
  248. return $string;
  249. }
  250. //-------------------------------------------------------------------
  251. /* A function for checking if its 404 time */
  252. //-------------------------------------------------------------------
  253. function check_404( $section )
  254. {
  255. $check = file_exists( "admin/modules/mod_{$section}.php" );
  256. if ( $check ) {
  257. return $section;
  258. } else {
  259. $section = '404';
  260. return $section;
  261. }
  262. }
  263. //-------------------------------------------------------------------
  264. /* A Function for checking if its 404 time from public hit */
  265. //-------------------------------------------------------------------
  266. function public_check_404( $section )
  267. {
  268. $section = str_replace( '<x>', "", $section );
  269. if ( $section === 'rss' ) {
  270. $check = safe_row( '*', 'pixie_core', "page_name = '$section' AND public = 'yes' limit 0,1" );
  271. } else {
  272. $check = safe_row( '*', 'pixie_core', "page_name = '$section' AND public = 'yes' AND page_type != 'plugin' limit 0,1" );
  273. }
  274. if ( $check ) {
  275. return $section;
  276. } else {
  277. $section = '404';
  278. return $section;
  279. }
  280. }
  281. //-------------------------------------------------------------------
  282. /* A function for checking what type of page we are dealing with */
  283. //-------------------------------------------------------------------
  284. function check_type( $section )
  285. {
  286. extract( safe_row( '*', 'pixie_core', "page_name = '$section' AND public = 'yes' limit 0,1" ) );
  287. if ( $page_type ) {
  288. return $page_type;
  289. } else {
  290. return 'Unable to find type of page in pixie_core. Has the page been deleted?';
  291. }
  292. }
  293. //-------------------------------------------------------------------
  294. /* A function for deleting a file */
  295. //-------------------------------------------------------------------
  296. function file_delete( $file )
  297. {
  298. if ( unlink( $file ) ) {
  299. return TRUE;
  300. } else {
  301. return FALSE;
  302. }
  303. }
  304. //-------------------------------------------------------------------
  305. /* A function to return current directory */
  306. //-------------------------------------------------------------------
  307. function current_dir()
  308. {
  309. $path = dirname( $_SERVER['PHP_SELF'] );
  310. $position = strrpos( $path, '/' ) + 1;
  311. return substr( $path, $position );
  312. }
  313. //-------------------------------------------------------------------
  314. /* A function to return current page id */
  315. //-------------------------------------------------------------------
  316. function get_page_id( $section )
  317. {
  318. $page_id = safe_field( 'page_id', 'pixie_core', "page_name = '$section' AND public = 'yes' limit 0,1" );
  319. if ( $page_id ) {
  320. return $page_id;
  321. }
  322. }
  323. //-------------------------------------------------------------------
  324. /* function to create a safe string from special characters like those found in non-English languages */
  325. //-------------------------------------------------------------------
  326. function safe_string( $string )
  327. {
  328. $from = explode( ',', '&lt;,&gt;,&#039;,&amp;,&quot;,Ŕ,Á,Â,Ă,Ä,&Auml;,Ĺ,?,?,?,Ć,Ç,?,?,?,?,?,?,Đ,Č,É,Ę,Ë,?,?,?,?,?,?,?,?,?,?,?,Ě,Í,Î,Ď,?,?,?,?,?,?,?,?,?,?,?,?,?,Ń,?,?,?,?,Ň,Ó,Ô,Ő,Ö,&Ouml;,Ř,?,?,?,Œ,?,?,?,?,Š,?,?,?,?,?,?,?,Ů,Ú,Ű,Ü,?,&Uuml;,?,?,?,?,?,?,Ý,?,Ÿ,?,Ž,?,Ţ,Ţ,ŕ,á,â,ă,ä,&auml;,ĺ,?,?,?,ć,ç,?,?,?,?,?,?,đ,č,é,ę,ë,?,?,?,?,?,ƒ,?,?,?,?,?,?,ě,í,î,ď,?,?,?,?,?,?,?,?,?,?,?,?,?,?,ń,?,?,?,?,?,ň,ó,ô,ő,ö,&ouml;,ř,?,?,?,œ,?,?,?,š,ů,ú,ű,ü,?,&uuml;,?,?,?,?,?,?,ý,˙,?,ž,?,?,ţ,ß,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?' );
  329. $to = explode( ',', ',,,,,A,A,A,A,Ae,A,A,A,A,A,Ae,C,C,C,C,C,D,D,D,E,E,E,E,E,E,E,E,E,G,G,G,G,H,H,I,I,I,I,I,I,I,I,I,IJ,J,K,K,K,K,K,K,N,N,N,N,N,O,O,O,O,Oe,Oe,O,O,O,O,OE,R,R,R,S,S,S,S,S,T,T,T,T,U,U,U,Ue,U,Ue,U,U,U,U,U,W,Y,Y,Y,Z,Z,Z,T,T,a,a,a,a,ae,ae,a,a,a,a,ae,c,c,c,c,c,d,d,d,e,e,e,e,e,e,e,e,e,f,g,g,g,g,h,h,i,i,i,i,i,i,i,i,i,ij,j,k,k,l,l,l,l,l,n,n,n,n,n,n,o,o,o,o,oe,oe,o,o,o,o,oe,r,r,r,s,u,u,u,ue,u,ue,u,u,u,u,u,w,y,y,y,z,z,z,t,ss,ss,A,B,V,G,D,E,YO,ZH,Z,I,Y,K,L,M,N,O,P,R,S,T,U,F,H,C,CH,SH,SCH,Y,Y,E,YU,YA,a,b,v,g,d,e,yo,zh,z,i,y,k,l,m,n,o,p,r,s,t,u,f,h,c,ch,sh,sch,y,y,e,yu,ya' );
  330. $string = urldecode( str_replace( $from, $to, $string ) );
  331. $string = preg_replace( '/[^a-zA-Z0-9 ]/', "", $string );
  332. return ( $string );
  333. }
  334. //-------------------------------------------------------------------
  335. /* function to return a slug from post name */
  336. //-------------------------------------------------------------------
  337. function make_slug( $slug )
  338. {
  339. $slug = safe_string( $slug );
  340. $slug = str_replace( ' ', '-', $slug );
  341. $dash = array(
  342. '--',
  343. '---',
  344. '----',
  345. '-----'
  346. );
  347. $slug = strtolower( str_replace( $dash, '-', $slug ) );
  348. return ( $slug );
  349. }
  350. //-------------------------------------------------------------------
  351. /* A function to correctly form tags */
  352. //-------------------------------------------------------------------
  353. function make_tag( $tags )
  354. {
  355. if ( isset( $tags ) ) {
  356. $tags = explode( " ", $tags );
  357. for ( $count = 0; $count < ( count( $tags ) ); $count++ ) {
  358. $current = $tags[$count];
  359. if ( $current != "" ) {
  360. $current = safe_string( $current );
  361. if ( ( isset( $all_tag ) ) ) {
  362. } else {
  363. $all_tag = NULL;
  364. }
  365. $all_tag .= $current . " ";
  366. }
  367. }
  368. return rtrim( $all_tag );
  369. }
  370. }
  371. //-------------------------------------------------------------------
  372. /* A function to revert slug / used for tags only */
  373. //-------------------------------------------------------------------
  374. function squash_slug( $title )
  375. {
  376. $slug = str_replace( '-', " ", $title );
  377. return strtolower( $slug );
  378. }
  379. //-------------------------------------------------------------------
  380. /* A function to check if a page is installed and public */
  381. //-------------------------------------------------------------------
  382. function public_page_exists( $page_name )
  383. {
  384. $rs = safe_row( '*', 'pixie_core', "page_name = '$page_name' AND public = 'yes' limit 0,1" );
  385. if ( $rs ) {
  386. return TRUE;
  387. } else {
  388. return FALSE;
  389. }
  390. }
  391. //-------------------------------------------------------------------
  392. /* A function to check if a number is odd or even */
  393. //-------------------------------------------------------------------
  394. function is_even( $number )
  395. {
  396. $result = $number % 2;
  397. if ( $result == 0 ) {
  398. return TRUE;
  399. } else {
  400. return FALSE;
  401. }
  402. }
  403. //-------------------------------------------------------------------
  404. /* Allow PHP/HTML to be written into textarea */
  405. //-------------------------------------------------------------------
  406. function textarea_encode( $html_code )
  407. {
  408. $from = array(
  409. '<',
  410. '>'
  411. );
  412. $to = array(
  413. '#&50',
  414. '#&52'
  415. );
  416. $html_code = str_replace( $from, $to, $html_code );
  417. return $html_code;
  418. }
  419. //-------------------------------------------------------------------
  420. /* Output title of current section for admin area */
  421. //-------------------------------------------------------------------
  422. function build_admin_title()
  423. {
  424. global $version, $lang, $s, $m, $x, $do;
  425. /* myaccount */
  426. if ( ( isset( $s ) ) && ( $s == 'myaccount' ) ) {
  427. $title = $lang['nav1_home'] . ' - ' . $lang['nav2_home'];
  428. }
  429. if ( ( isset( $s ) ) && ( $s == 'myaccount' ) && ( $x == 'myprofile' ) ) {
  430. $title = $lang['nav1_home'] . ' - ' . $lang['nav2_profile'];
  431. }
  432. if ( ( isset( $s ) ) && ( $s == 'myaccount' ) && ( $x == 'myprofile' ) && ( $do == 'security' ) ) {
  433. $title = $lang['nav1_home'] . ' - ' . $lang['nav2_security'];
  434. }
  435. /* publish - (needs expanding!) */
  436. if ( ( isset( $s ) ) && ( $s == 'publish' ) ) {
  437. $title = $lang['nav1_publish'];
  438. }
  439. if ( ( isset( $s ) ) && ( $s == 'publish' ) && ( $x == 'filemanager' ) ) {
  440. $title = $lang['nav1_publish'] . ' - ' . $lang['nav2_files'];
  441. }
  442. /* settings - needs expanding! */
  443. if ( ( isset( $s ) ) && ( $s == 'settings' ) ) {
  444. $title = $lang['nav1_settings'];
  445. }
  446. if ( ( isset( $s ) ) && ( $s == 'settings' ) && ( $m == 'theme' ) ) {
  447. $title = $lang['nav1_settings'] . ' - ' . $lang['nav2_theme'];
  448. }
  449. if ( ( isset( $s ) ) && ( $s == 'settings' ) && ( $m == 'users' ) ) {
  450. $title = $lang['nav1_settings'] . ' - ' . $lang['nav2_users'];
  451. }
  452. if ( ( isset( $s ) ) && ( $s == 'settings' ) && ( $x == 'dbtools' ) ) {
  453. $title = $lang['nav1_settings'] . ' - ' . $lang['nav2_backup'];
  454. }
  455. if ( ( isset( $version ) ) && ( isset( $title ) ) ) {
  456. echo "Pixie v{$version} : {$title}";
  457. } else {
  458. echo "Pixie v{$version}";
  459. }
  460. }
  461. //-------------------------------------------------------------------
  462. /* Create a clean or ugly url based on the Pixie setting */
  463. //-------------------------------------------------------------------
  464. function createURL( $s, $m = '', $x = '', $p = '' )
  465. {
  466. global $site_url, $clean_urls;
  467. if ( $clean_urls === 'yes' ) {
  468. $return = "{$site_url}{$s}/{$m}/{$x}/{$p}";
  469. $slash = array(
  470. '//',
  471. '///',
  472. '////'
  473. );
  474. $return = str_replace( $slash, "", $return );
  475. $return = str_replace( 'http:', 'http://', $return );
  476. $last = $return{strlen( $return ) - 1};
  477. if ( $last != '/' ) {
  478. $return = "{$return}/";
  479. }
  480. return $return;
  481. } else {
  482. $return = "{$site_url}?s={$s}&m={$m}&x={$x}&p={$p}";
  483. $return = str_replace( '&m=&x=&p=', "", $return );
  484. $return = str_replace( '&x=&p=', "", $return );
  485. if ( !$p ) {
  486. $return = str_replace( '&p=', "", $return );
  487. }
  488. $return = htmlspecialchars( $return, ENT_QUOTES, 'UTF-8' );
  489. return $return;
  490. }
  491. }
  492. //-------------------------------------------------------------------
  493. /* Reset the page order */
  494. //-------------------------------------------------------------------
  495. function page_order_reset()
  496. {
  497. $pages = safe_rows( '*', 'pixie_core', "public = 'yes' and in_navigation = 'yes' order by page_order asc" );
  498. $num = count( $pages );
  499. $i = 0;
  500. while ( $i < $num ) {
  501. $out = $pages[$i];
  502. $page_id = $out['page_id'];
  503. safe_update( 'pixie_core', "page_order = $i + 1", "page_id = '$page_id'" );
  504. $i++;
  505. }
  506. }
  507. //-------------------------------------------------------------------
  508. /* Create list of blocks with form adder */
  509. //-------------------------------------------------------------------
  510. function form_blocks()
  511. {
  512. global $s, $m, $x, $site_url, $lang;
  513. $dir = './blocks';
  514. if ( ( is_dir( $dir ) ) ) {
  515. $fd = @opendir( $dir );
  516. if ( $fd ) {
  517. while ( ( $part = @readdir( $fd ) ) == TRUE ) {
  518. if ( ( $part != '.' ) && ( $part != '..' ) ) {
  519. if ( ( $part != 'index.php' ) && ( preg_match( '/^block_.*\.php$/', $part ) ) ) {
  520. $part = str_replace( 'block_', "", $part );
  521. $part = str_replace( '.php', "", $part );
  522. if ( isset( $cloud ) ) {
  523. } else {
  524. $cloud = NULL;
  525. }
  526. $cloud .= "\t\t\t\t\t\t\t\t\t<a href=\"#\" title=\"Add block: $part\">$part</a>\n";
  527. }
  528. }
  529. }
  530. }
  531. }
  532. if ( ( isset( $cloud ) ) && ( $cloud ) ) {
  533. $cloud = substr( $cloud, 0, ( strlen( $cloud ) - 1 ) ) . "";
  534. echo "\t\t\t\t\t\t\t\t<div class=\"form_block_suggestions\" id=\"form_block_list\">";
  535. echo "<span class=\"form_block_suggestions_text\">" . $lang['form_help_current_blocks'] . "</span>\n $cloud\n";
  536. echo "\t\t\t\t\t\t\t\t</div>\n";
  537. }
  538. }
  539. //-------------------------------------------------------------------
  540. /* Protect email from spam bots */
  541. //-------------------------------------------------------------------
  542. function encode_email( $emailaddy, $mailto = 0 )
  543. {
  544. $emailNOSPAMaddy = '';
  545. srand( (float) microtime() * 1000000 );
  546. for ( $i = 0; $i < strlen( $emailaddy ); $i = $i + 1 ) {
  547. $j = floor( rand( 0, 1 + $mailto ) );
  548. if ( $j == 0 ) {
  549. $emailNOSPAMaddy .= '&#' . ord( substr( $emailaddy, $i, 1 ) ) . ';';
  550. } elseif ( $j === 1 ) {
  551. $emailNOSPAMaddy .= substr( $emailaddy, $i, 1 );
  552. } elseif ( $j === 2 ) {
  553. $emailNOSPAMaddy .= '%' . zeroise( dechex( ord( substr( $emailaddy, $i, 1 ) ) ), 2 );
  554. }
  555. }
  556. $emailNOSPAMaddy = str_replace( '@', '&#64;', $emailNOSPAMaddy );
  557. return $emailNOSPAMaddy;
  558. }
  559. //-------------------------------------------------------------------
  560. /* Get extended entry info (<!--more-->) */
  561. //-------------------------------------------------------------------
  562. function get_extended( $post )
  563. {
  564. /* Match the more links */
  565. if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) {
  566. list( $main, $extended ) = explode( $matches[0], $post, 2 );
  567. } else {
  568. $main = $post;
  569. $extended = '';
  570. }
  571. /* Strip leading and trailing whitespace */
  572. $main = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $main );
  573. $extended = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $extended );
  574. return array(
  575. 'main' => $main,
  576. 'extended' => $extended
  577. );
  578. }
  579. //-------------------------------------------------------------------
  580. /* Don't call sterilise unless necessary */
  581. //-------------------------------------------------------------------
  582. function sterilise_txt( $txt, $is_sql = FALSE )
  583. {
  584. if ( !preg_match( '/^[a-zA-ZŔÁÂĂÄĹ???ĆÇ??????ĐČÉĘË???????????ĚÍÎĎ?????????????Ń????ŇÓÔŐÖŘ???Œ????Š???????ŮÚŰÜ???????Ý?Ÿ?Ž?ŢŢŕáâăäĺ???ćç??????đčéęë?????ƒ??????ěíîď??????????????ń?????ňóôőöř???œ???šůúűü???????ý˙?ž??ţß?????????????????????????????????????????????????????????????????0-9\_]+$/', $txt ) )
  585. return sterilise( $txt, $is_sql );
  586. return $txt;
  587. }
  588. //-------------------------------------------------------------------
  589. /* Steralise user input, security against XSS etc */
  590. //-------------------------------------------------------------------
  591. function sterilise( $val, $is_sql = FALSE )
  592. {
  593. /* Remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  594. this prevents some character re-spacing such as <java\0script>
  595. note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs */
  596. $val = preg_replace( '/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val );
  597. /* Straight replacements, the user should never need these since they're normal characters
  598. this prevents like <IMG SRC=&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&#X3A&#X61&#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29> */
  599. $search = 'abcdefghijklmnopqrstuvwxyz';
  600. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  601. $search .= '1234567890!@#$%^&*()';
  602. $search .= '~`";:?+/={}[]-_|\'\\';
  603. $search .= 'ŔÁÂĂÄĹ???ĆÇ??????ĐČÉĘË???????????ĚÍÎĎ?????????????Ń????ŇÓÔŐÖŘ???Œ????Š???????ŮÚŰÜ???????Ý?Ÿ?Ž?ŢŢŕáâăäĺ???ćç??????đčéęë?????ƒ??????ěíîď??????????????ń?????ňóôőöř???œ???šůúűü???????ý˙?ž??ţß?????????????????????????????????????????????????????????????????';
  604. for ( $i = 0; $i < strlen( $search ); $i++ ) {
  605. /* ;? matches the ;, which is optional
  606. 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  607. &#x0040 @ search for the hex values */
  608. $val = preg_replace( '/(&#[xX]0{0,8}' . dechex( ord( $search[$i] ) ) . ';?)/i', $search[$i], $val );
  609. /* With a ; */
  610. /* &#00064 @ 0{0,7} matches '0' zero to seven times */
  611. $val = preg_replace( '/(&#0{0,8}' . ord( $search[$i] ) . ';?)/', $search[$i], $val );
  612. /* With a ; */
  613. }
  614. /* now the only remaining whitespace attacks are \t, \n, and \r */
  615. $ra1 = Array(
  616. 'javascript',
  617. 'vbscript',
  618. 'expression',
  619. 'applet',
  620. 'meta',
  621. 'xml',
  622. 'blink',
  623. 'link',
  624. 'style',
  625. 'script',
  626. 'embed',
  627. 'object',
  628. 'iframe',
  629. 'frame',
  630. 'frameset',
  631. 'ilayer',
  632. 'layer',
  633. 'bgsound',
  634. 'title',
  635. 'base'
  636. );
  637. $ra2 = Array(
  638. 'onabort',
  639. 'onactivate',
  640. 'onafterprint',
  641. 'onafterupdate',
  642. 'onbeforeactivate',
  643. 'onbeforecopy',
  644. 'onbeforecut',
  645. 'onbeforedeactivate',
  646. 'onbeforeeditfocus',
  647. 'onbeforepaste',
  648. 'onbeforeprint',
  649. 'onbeforeunload',
  650. 'onbeforeupdate',
  651. 'onblur',
  652. 'onbounce',
  653. 'oncellchange',
  654. 'onchange',
  655. 'onclick',
  656. 'oncontextmenu',
  657. 'oncontrolselect',
  658. 'oncopy',
  659. 'oncut',
  660. 'ondataavailable',
  661. 'ondatasetchanged',
  662. 'ondatasetcomplete',
  663. 'ondblclick',
  664. 'ondeactivate',
  665. 'ondrag',
  666. 'ondragend',
  667. 'ondragenter',
  668. 'ondragleave',
  669. 'ondragover',
  670. 'ondragstart',
  671. 'ondrop',
  672. 'onerror',
  673. 'onerrorupdate',
  674. 'onfilterchange',
  675. 'onfinish',
  676. 'onfocus',
  677. 'onfocusin',
  678. 'onfocusout',
  679. 'onhelp',
  680. 'onkeydown',
  681. 'onkeypress',
  682. 'onkeyup',
  683. 'onlayoutcomplete',
  684. 'onload',
  685. 'onlosecapture',
  686. 'onmousedown',
  687. 'onmouseenter',
  688. 'onmouseleave',
  689. 'onmousemove',
  690. 'onmouseout',
  691. 'onmouseover',
  692. 'onmouseup',
  693. 'onmousewheel',
  694. 'onmove',
  695. 'onmoveend',
  696. 'onmovestart',
  697. 'onpaste',
  698. 'onpropertychange',
  699. 'onreadystatechange',
  700. 'onreset',
  701. 'onresize',
  702. 'onresizeend',
  703. 'onresizestart',
  704. 'onrowenter',
  705. 'onrowexit',
  706. 'onrowsdelete',
  707. 'onrowsinserted',
  708. 'onscroll',
  709. 'onselect',
  710. 'onselectionchange',
  711. 'onselectstart',
  712. 'onstart',
  713. 'onstop',
  714. 'onsubmit',
  715. 'onunload'
  716. );
  717. $ra = array_merge( $ra1, $ra2 );
  718. $found = TRUE;
  719. /* keep replacing as long as the previous round replaced something */
  720. while ( $found === TRUE ) {
  721. $val_before = $val;
  722. for ( $i = 0; $i < sizeof( $ra ); $i++ ) {
  723. $pattern = '/';
  724. for ( $j = 0; $j < strlen( $ra[$i] ); $j++ ) {
  725. if ( $j > 0 ) {
  726. $pattern .= '(';
  727. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  728. $pattern .= '|';
  729. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  730. $pattern .= ')*';
  731. }
  732. $pattern .= $ra[$i][$j];
  733. }
  734. $pattern .= '/i';
  735. $replacement = substr( $ra[$i], 0, 2 ) . '<x>' . substr( $ra[$i], 2 );
  736. /* Add in <> to nerf the tag */
  737. $val = preg_replace( $pattern, $replacement, $val );
  738. /* Filter out the hex tags */
  739. if ( $val_before == $val ) {
  740. /* No replacements were made, so exit the loop */
  741. $found = FALSE;
  742. }
  743. }
  744. }
  745. if ( $is_sql ) {
  746. $val = mysql_real_escape_string( $val );
  747. }
  748. return $val;
  749. }
  750. ?>