Source for file etc.php

Documentation is available at etc.php

  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Copyright (C) ReloadCMS Development Team //
  4. // http://reloadcms.sf.net //
  5. // //
  6. // This program is distributed in the hope that it will be useful, //
  7. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  9. // //
  10. // This product released under GNU General Public License v2 //
  11. ////////////////////////////////////////////////////////////////////////////////
  12.  
  13.  
  14.  
  15. /**
  16. * Misc ReloadCMS packages
  17. *
  18. * @author DruidVAV
  19. * @package ReloadCMS
  20. */
  21.  
  22. /**
  23. * Function recursively check if $needle is present in $haystack
  24. *
  25. * @param mixed $needle
  26. * @param array $haystack
  27. * @return boolean
  28. */
  29. function rcms_in_array_recursive($needle, $haystack) {
  30. foreach ($haystack as $value){
  31. if(is_array($value)) return rcms_in_array_recursive($needle, $value);
  32. else return in_array($needle, $haystack);
  33. }
  34. }
  35.  
  36. function in_array_i($needle, $haystack) {
  37. $needle = strtolower(htmlentities($needle));
  38. if(!is_array($haystack)) return false;
  39. foreach ($haystack as $value){
  40. $value = strtolower(htmlentities($value));
  41. if($needle == $value) return true;
  42. }
  43. return false;
  44. }
  45.  
  46. function rcms_htmlspecialchars_recursive($array) {
  47. foreach ($array as $key =>$value){
  48. if(is_array($value)) $array[$key] = rcms_htmlspecialchars_recursive($value);
  49. else $array[$key] = htmlspecialchars($value);
  50. }
  51. return $array;
  52. }
  53. /**
  54. * Recursively stripslashes array.
  55. *
  56. * @param array $array
  57. * @return boolean
  58. */
  59. function stripslash_array(&$array){
  60. foreach ($array as $key => $value) {
  61. if(is_array($array[$key])) stripslash_array($array[$key]);
  62. else $array[$key] = stripslashes($value);
  63. }
  64. return true;
  65. }
  66.  
  67. /**
  68. * Shows redirection javascript.
  69. *
  70. * @param string $url
  71. */
  72. function rcms_redirect($url, $header = false) {
  73. if($header){
  74. @header('Location: ' . $url);
  75. } else {
  76. echo '<script language="javascript">document.location.href="' . $url . '";</script>';
  77. }
  78. }
  79.  
  80. /**
  81. * Sends e-mail.
  82. *
  83. * @param string $to
  84. * @param string $from
  85. * @param string $sender
  86. * @param string $encoding
  87. * @param string $subj
  88. * @param string $text
  89. * @return boolean
  90. */
  91. function rcms_send_mail($to, $from, $sender, $encoding, $subj, $text) {
  92. $headers = 'From: ' . $sender . ' <' . $from . ">\n";
  93. $headers .= "MIME-Version: 1.0\n";
  94. $headers .= 'Message-ID: <' . md5(uniqid(time())) . "@" . $sender . ">\n";
  95. $headers .= 'Date: ' . gmdate('D, d M Y H:i:s T', time()) . "\n";
  96. $headers .= "Content-type: text/plain; charset={$encoding}\n";
  97. $headers .= "Content-transfer-encoding: 8bit\n";
  98. $headers .= "X-Mailer: ReloadCMS\n";
  99. $headers .= "X-MimeOLE: ReloadCMS\n";
  100. return mail($to, $subj, $text, $headers);
  101. }
  102.  
  103. /**
  104. * Returns random string with selected length
  105. *
  106. * @param integer $num_chars
  107. * @return string
  108. */
  109. function rcms_random_string($num_chars) {
  110. $chars = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  111.  
  112. list($usec, $sec) = explode(' ', microtime());
  113. mt_srand($sec * $usec);
  114.  
  115. $max_chars = sizeof($chars) - 1;
  116. $rand_str = '';
  117. for ($i = 0; $i < $num_chars; $i++) {
  118. $rand_str .= $chars[mt_rand(0, $max_chars)];
  119. }
  120.  
  121. return $rand_str;
  122. }
  123.  
  124. /**
  125. * Just returns current time
  126. *
  127. * @return integer
  128. */
  129. function rcms_get_time(){
  130. return mktime();
  131. }
  132.  
  133. /**
  134. * Function that formats date. Similar to date() function but
  135. * uses timezone and returns localised string
  136. *
  137. * @param string $format
  138. * @param integer $gmepoch
  139. * @param integer $tz
  140. * @return string
  141. */
  142. function rcms_format_time($format, $gmepoch, $tz = ''){
  143. global $lang, $system;
  144.  
  145. if(empty($tz)) $tz = $system->user['tz'];
  146. if ($system->language != 'english'){
  147. @reset($lang['datetime']);
  148. while (list($match, $replace) = @each($lang['datetime'])){
  149. $translate[$match] = $replace;
  150. }
  151. }
  152. return ( !empty($translate) ) ? strtr(@gmdate($format, $gmepoch + (3600 * $tz)), $translate) : @gmdate($format, $gmepoch + (3600 * $tz));
  153. }
  154.  
  155. /**
  156. * Return localised date from string generated by date()
  157. *
  158. * @param string $string
  159. * @return string
  160. */
  161. function rcms_date_localise($string){
  162. global $lang, $system;
  163. if ($system->language != 'english'){
  164. @reset($lang['datetime']);
  165. while (list($match, $replace) = @each($lang['datetime'])){
  166. $translate[$match] = $replace;
  167. }
  168. }
  169. return ( !empty($translate) ) ? strtr($string, $translate) : $string;
  170. }
  171.  
  172. function rcms_parse_text_by_mode($str, $mode){
  173. switch ($mode){
  174. default:
  175. case 'check':
  176. return rcms_parse_text($str, false, false, false, false, false, false);
  177. break;
  178. case 'text':
  179. return rcms_parse_text($str, true, false, true, false, true, true);
  180. break;
  181. case 'text-safe':
  182. return rcms_parse_text($str, true, false, true, false, false, false);
  183. break;
  184. case 'html':
  185. return rcms_parse_text($str, false, true, false, false, true, true);
  186. break;
  187. case 'htmlbb':
  188. return rcms_parse_text($str, true, true, false, false, true, true);
  189. break;
  190. }
  191. }
  192.  
  193. /**
  194. * Just a stub for backward compatibility.
  195. *
  196. * @param string $str
  197. * @param boolean $bbcode
  198. * @param boolean $html
  199. * @param boolean $nl2br
  200. * @param boolean $wordwrap
  201. * @param boolean $imgbbcode
  202. * @return string
  203. */
  204. function rcms_parse_text($str, $bbcode = true, $html = false, $nl2br = true, $wordwrap = false, $imgbbcode = false, $htmlbbcode = false){
  205. $level = intval($bbcode);
  206. if($imgbbcode && $bbcode && $level < 2) $level = 2;
  207. if($htmlbbcode && $bbcode && $level < 3) $level = 3;
  208. $message = new message($str, $level, $html, $nl2br);
  209. $message->init_bbcodes();
  210. $message->parse();
  211. if($wordwrap) {
  212. return '<div style="overflow: auto;">' . $message->str . '</div>';
  213. } else {
  214. return $message->str;
  215. }
  216. }
  217.  
  218. /**
  219. * Validates e-mail
  220. *
  221. * @param string $text
  222. * @return boolean
  223. */
  224. function rcms_is_valid_email($text) {
  225. if(preg_match('/^([a-zA-Z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[a-zA-Z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+)*)@((([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))\.)*(([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))$/', $text))
  226. return true;
  227. else return false;
  228. }
  229.  
  230. /**
  231. * Returns bbcode panel code for selected textarea
  232. *
  233. * @param string $textarea
  234. * @return string
  235. */
  236. function rcms_show_bbcode_panel($textarea){
  237. return rcms_parse_module_template('bbcodes-panel.tpl', array('textarea' => $textarea));
  238. }
  239.  
  240. function get_animated_to_array()
  241. {
  242. $arr=rcms_scandir(SMILES_PATH);
  243. $arr2 = array();
  244. foreach ($arr as $key) {
  245. if (file_exists(SMILES_PATH.basename($key, ".gif").".gif")){
  246. $arr2['#\['.basename($key, ".gif").'\]#is'] = '<img src="'.SMILES_PATH.$key.'" alt = "'.basename($key, ".gif").'">';
  247. }
  248. }
  249. return $arr2;
  250. }
  251.  
  252. function return_hidden_bb_text() {
  253. if (LOGGED_IN) {
  254. return '<div class="hidden">\\1</div>';
  255. } else {
  256. return '<div class="hidden">'.__('This block only for registered users').'</div>';
  257. }
  258. }
  259.  
  260. /**
  261. * Message parser class
  262. *
  263. * @package ReloadCMS
  264. */
  265. class message{
  266. /**
  267. * Message container
  268. *
  269. * @var string
  270. */
  271. var $str = '';
  272. /**
  273. * Level of bbcode security.
  274. *
  275. * @var integer
  276. */
  277. var $bbcode_level = 0; // 0 - no bbcode, 1 - save bbcodes, 2 - all bbcodes
  278. /**
  279. * Allow HTML in message
  280. *
  281. * @var boolean
  282. */
  283. var $html = false;
  284. /**
  285. * Perform nl2br in message
  286. *
  287. * @var boolean
  288. */
  289. var $nl2br = false;
  290. /**
  291. * Array of regexps for bbcodes
  292. *
  293. * @var array
  294. */
  295. var $regexp = array();
  296. var $sr_temp = array();
  297. /**
  298. * Class constructor
  299. *
  300. * @param string $message
  301. * @param integer $bbcode_level
  302. * @param boolean $html
  303. * @param boolean $nl2br
  304. * @return message
  305. */
  306. function message($message, $bbcode_level = 0, $html = false, $nl2br = false){
  307. $this->str = $message;
  308. $this->nl2br = $nl2br;
  309. $this->bbcode_level = $bbcode_level;
  310. $this->html = $html;
  311. }
  312. /**
  313. * BBCodes initialisation. Filling in message::regexp array
  314. *
  315. */
  316. function init_bbcodes(){
  317. $this->regexp[0] = array();
  318. $this->regexp[1] = array(
  319. "#\[b\](.*?)\[/b\]#is" => '<span style="font-weight: bold">\\1</span>',
  320. "#\[i\](.*?)\[/i\]#is" => '<span style="font-style: italic">\\1</span>',
  321. "#\[u\](.*?)\[/u\]#is" => '<span style="text-decoration: underline">\\1</span>',
  322. "#\[del\](.*?)\[/del\]#is" => '<span style="text-decoration: line-through">\\1</span>',
  323. "#\[url\][\s\n\r]*(((https?|ftp|ed2k|irc)://)[^ \"\n\r\t\<]*)[\s\n\r]*\[/url\]#is" => '<a href="\\1" target="_blank">\\1</a>',
  324. "#\[url\][\s\n\r]*(www\.[^ \"\n\r\t\<]*?)[\s\n\r]*\[/url\]#is" => '<a href="http://\\1" target="_blank">\\1</a>',
  325. "#\[url\][\s\n\r]*((ftp)\.[^ \"\n\r\t\<]*?)[\s\n\r]*\[/url\]#is" => '<a href="\\2://\\1" target="_blank">\\1</a>',
  326. "#\[url=(\"|&quot;|)(((https?|ftp|ed2k|irc)://)[^ \"\n\r\t\<]*?)(\"|&quot;|)\](.*?)\[/url\]#is" => '<a href="\\2" target="_blank">\\6</a>',
  327. "#\[url=(\"|&quot;|)(www\.[^ \"\n\r\t\<]*?)(\"|&quot;|)\](.*?)\[/url\]#is" => '<a href="http://\\2" target="_blank">\\4</a>',
  328. "#\[url=(\"|&quot;|)((ftp)\.[^ \"\n\r\t\<]*?)(\"|&quot;|)\](.*?)\[/url\]#is" => '<a href="\\3://\\2" target="_blank">\\5</a>',
  329. "#\[mailto\][\s\n\r]*([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)[\s\n\r]*\[/mailto\]#is" => '<a href="mailto:\\1">\\1</a>',
  330. "#\[mailto=(\"|&quot;|)([a-z0-9&\-_.]+?@[\w\-]+\.([\w\-\.]+\.)?[\w]+)(\"|&quot;|)\](.*?)\[/mailto\]#is" => '<a href="mailto:\\2">\\5</a>',
  331. "#\[color=(\"|&quot;|)([\#\w]*)(\"|&quot;|)\](.*?)\[/color(.*?)\]#is" => '<span style="color:\\2">\\4</span>',
  332. "#\[size=(\"|&quot;|)([0-9]*)(\"|&quot;|)\](.*?)\[/size(.*?)\]#is" => '<span style="font-size: \\2pt">\\4</span>',
  333. "#\[align=(\"|&quot;|)(left|right|center|justify)(\"|&quot;|)\](.*?)\[/align(.*?)\]#is" => '<div style="text-align: \\2">\\4</span>',
  334. "#\[user\]([\d\w]*?)\[/user\]#is" => ' [ <a href="' . RCMS_ROOT_PATH . '?module=user.list&amp;user=\\1">\\1</a> ] ',
  335. "#\[user=([\d\w]*?)\](.*?)\[/user\]#is" => ' [ <a href="' . RCMS_ROOT_PATH . '?module=user.list&amp;user=\\1">\\2</a> ] ',
  336. "#\[hidden\](.*?)\[/hidden\]#is" => return_hidden_bb_text()
  337. );
  338. $this->regexp[1] = array_merge(get_animated_to_array(), $this->regexp[1]);
  339. $this->regexp[2] = array(
  340. "#[\s\n\r]*\[img\][\s\n\r]*([\w]+?://[^ \"\n\r\t<]*?)\.(gif|png|jpe?g)[\s\n\r]*\[/img\][\s\n\r]*#is" => '<br /><img src="\\1.\\2" alt="" /><br />',
  341. "#[\s\n\r]*\[img=(\"|&quot;|)(left|right)(\"|&quot;|)\][\s\n\r]*([\w]+?://[^ \"\n\r\t<]*?)\.(gif|png|jpe?g)[\s\n\r]*\[/img\][\s\n\r]*#is" => '<img src="\\4.\\5" alt="" align="\\2" />',
  342. );
  343. }
  344. /**
  345. * Main parse method. Parses message::str
  346. *
  347. */
  348. function parse(){
  349. $old = $this->str;
  350. if(!$this->html) $this->str = htmlspecialchars($this->str);
  351. if(!empty($this->bbcode_level)){
  352. $this->str = preg_replace(array_keys($this->regexp[0]), array_values($this->regexp[0]), ' ' . $this->str . ' ');
  353. if($this->bbcode_level > 0){
  354. $this->parseCodeTag();
  355. $this->parseQuoteTag();
  356. $this->str = preg_replace_callback("#\[spoiler(=(\"|&quot;|)(.*?)(\"|&quot;|)|)\](.*?)\[/spoiler\]#is", 'rcms_spoiler_tag', $this->str);
  357. $this->str = preg_replace(array_keys($this->regexp[1]), array_values($this->regexp[1]), ' ' . $this->str . ' ');
  358. }
  359. if($this->bbcode_level > 1){
  360. $this->str = preg_replace(array_keys($this->regexp[2]), array_values($this->regexp[2]), ' ' . $this->str . ' ');
  361. }
  362. if($this->bbcode_level > 2){
  363. $this->str = preg_replace_callback("#\[html\](.*?)\[/html\]#is", 'rcms_html_tag', $this->str);
  364. }
  365. if($this->nl2br){
  366. $this->str = nl2br($this->str);
  367. }
  368. $this->parseUrls();
  369. }
  370. $this->str = str_replace(array_keys($this->sr_temp), array_values($this->sr_temp), $this->str);
  371. $this->result = $this->str;
  372. $rhis->str = $old;
  373. }
  374. /**
  375. * Parses message::str [qoute|quote="Who"]..[/qoute] bbtag
  376. *
  377. */
  378. function parseQuoteTag(){
  379. $this->str = preg_replace("#[\s\n\r]*\[quote\][\s\n\r]*(.*?)[\s\n\r]*\[/quote\][\s\n\r]*#is", '<div class="codetitle"><b>' . __('Quote') . ':</b></div><div class="codetext">\\1</div>', $this->str);
  380. $this->str = preg_replace("#[\s\n\r]*\[quote=(\"|&quot;|)(.*?)(\"|&quot;|)\][\s\n\r]*(.*?)[\s\n\r]*\[/quote\][\s\n\r]*#is", '<div class="codetitle"><b>\\2 ' . __('wrote') . ':</b></div><div class="codetext">\\4</div>', $this->str);
  381. }
  382. /**
  383. * Parses message::str [code]..[/code] bbtag
  384. *
  385. */
  386. function parseCodeTag(){
  387. preg_match_all("#[\s\n\r]*\[code\][\s\n\r]*(.*?)[\s\n\r]*\[/code\][\s\n\r]*#is", $this->str, $matches);
  388. foreach($matches[1] as $oldpart) {
  389. $newpart = preg_replace("#[\n\r]+#", '', highlight_string(strtr($oldpart, array_flip(get_html_translation_table(HTML_SPECIALCHARS))), true));
  390. $newpart = preg_replace(array('#\[#', '#\]#'), array('&#91;', '&#93;'), $newpart);
  391. $tmp = '{SR:' . rcms_random_string(6) . '}';
  392. $this->sr_temp[$tmp] = $newpart;
  393. $this->str = str_replace($oldpart, $tmp, $this->str);
  394. }
  395. $this->str = preg_replace("#[\s\n\r]*\[code\][\s\n\r]*(.*?)[\s\n\r]*\[/code\][\s\n\r]*#is", '<div class="codetitle"><b>' . __('Code') . ':</b></div><div class="codetext" style="overflow: auto; white-space: nowrap;">\\1</div>', $this->str);
  396. }
  397. function parseUrls(){
  398. $this->str = $this->highlightUrls($this->str);
  399. return true;
  400. }
  401.  
  402. function highlightUrls($string){
  403. $string = ' ' . $string;
  404. $string = preg_replace_callback("#(^|[\n\s\r])((https?|ftp|ed2k|irc)://[^ \"\n\r\t<]*)#is", 'rcms_prc_link', $string);
  405. $string = preg_replace_callback("#(^|[\n\s\r])((www|ftp)\.[^ \"\t\n\r<]*)#is", 'rcms_prc_link_short', $string);
  406. $string = preg_replace_callback("#(^|[\n\s\r])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", 'rcms_prc_mail', $string);
  407. $string = substr($string, 1);
  408. return $string;
  409. }
  410.  
  411. }
  412.  
  413. /**
  414. * Callback for link replacement
  415. *
  416. * @param array $matches
  417. * @return string
  418. */
  419. function rcms_prc_link($matches){
  420. if(strlen($matches[2])>25){
  421. return ' <a href="' . $matches[2] . '" target="_blank">' . substr($matches[2], 0, 11) . '...' . substr($matches[2], -11) . '</a>';
  422. } else return ' <a href="' . $matches[2] . '" target="_blank">' . $matches[2] . '</a>';
  423. }
  424.  
  425. /**
  426. * Callback for short link replacement
  427. *
  428. * @param array $matches
  429. * @return string
  430. */
  431. function rcms_prc_link_short($matches){
  432. if(strlen($matches[2])>25){
  433. return ' <a href="http://' . $matches[2] . '" target="_blank">' . substr($matches[2], 0, 11) . '...' . substr($matches[2], -11) . '</a>';
  434. } else return ' <a href="http://' . $matches[2] . '" target="_blank">' . $matches[2] . '</a>';
  435. }
  436.  
  437. /**
  438. * Callback for e-mail replacement
  439. *
  440. * @param array $matches
  441. * @return string
  442. */
  443. function rcms_prc_mail($matches){
  444. if(strlen($matches[2])>25){
  445. return ' <a href="mailto:' . $matches[2] . '@' . $matches[3] . '" target="_blank">' . substr($matches[2], 0, 11) . '...' . substr($matches[2], -11) . '</a>';
  446. } else return ' <a href="mailto:' . $matches[2] . '@' . $matches[3] . '" target="_blank">' . $matches[2] . '</a>';
  447. }
  448.  
  449. function rcms_spoiler_tag($matches){
  450. $id1 = rcms_random_string('6');
  451. $id2 = rcms_random_string('6');
  452. if(!empty($matches[3])) $title = __('Spoiler') . ': ' . $matches[3]; else $title = __('Spoiler') . ' (' . __('click to view') . ')';
  453. return '<div id="' . $id1 . '" class="codetitle"><a onClick="javascript:document.getElementById(\'' . $id2 . '\').style.display=\'block\';">' . $title . '</a></div><div id="' . $id2 . '" style="display: none;" class="codetext">' . $matches[5] . '</div>';
  454. }
  455.  
  456. function rcms_html_tag($matches){
  457. return str_replace(array('[', ']'), array('&#91', '&#93'), strtr($matches[1], array_flip(get_html_translation_table(HTML_SPECIALCHARS))));
  458. }
  459.  
  460. function rcms_remove_index($key, &$array, $preserve_keys = false) {
  461. $temp_array = $array;
  462. $array = array();
  463. foreach ($temp_array as $ckey => $value){
  464. if($key != $ckey){
  465. if($preserve_keys) {
  466. $array[$ckey] = $value;
  467. } else {
  468. $array[] = $value;
  469. }
  470. }
  471. }
  472. }
  473. ?>

Documentation generated on Fri, 08 Jun 2007 12:21:22 +0300 by phpDocumentor 1.3.0RC3