PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Upload/inc/class_parser.php

https://gitlab.com/mybbpl/ppm-1.6
PHP | 1503 lines | 1017 code | 161 blank | 325 comment | 117 complexity | 332555d79b73d58343fbbc0f2e5a4403 MD5 | raw file
  1. <?php
  2. /**
  3. * MyBB 1.6
  4. * Copyright 2010 MyBB Group, All Rights Reserved
  5. *
  6. * Website: http://mybb.com
  7. * License: http://mybb.com/about/license
  8. *
  9. * $Id$
  10. */
  11. /*
  12. options = array(
  13. allow_html
  14. allow_smilies
  15. allow_mycode
  16. nl2br
  17. filter_badwords
  18. me_username
  19. shorten_urls
  20. highlight
  21. filter_cdata
  22. )
  23. */
  24. class postParser
  25. {
  26. /**
  27. * Internal cache of MyCode.
  28. *
  29. * @access public
  30. * @var mixed
  31. */
  32. public $mycode_cache = 0;
  33. /**
  34. * Internal cache of smilies
  35. *
  36. * @access public
  37. * @var mixed
  38. */
  39. public $smilies_cache = 0;
  40. /**
  41. * Internal cache of badwords filters
  42. *
  43. * @access public
  44. * @var mixed
  45. */
  46. public $badwords_cache = 0;
  47. /**
  48. * Base URL for smilies
  49. *
  50. * @access public
  51. * @var string
  52. */
  53. public $base_url;
  54. /**
  55. * Parsed Highlights cache
  56. *
  57. * @access public
  58. * @var array
  59. */
  60. public $highlight_cache = array();
  61. /**
  62. * Options for this parsed message (Private - set by parse_message argument)
  63. *
  64. * @access public
  65. * @var array
  66. */
  67. public $options;
  68. /**
  69. * Internal cache for nested lists
  70. *
  71. * @access public
  72. * @var array
  73. */
  74. public $list_elements;
  75. /**
  76. * Internal counter for nested lists
  77. *
  78. * @access public
  79. * @var int
  80. */
  81. public $list_count;
  82. /**
  83. * Parses a message with the specified options.
  84. *
  85. * @param string The message to be parsed.
  86. * @param array Array of yes/no options - allow_html,filter_badwords,allow_mycode,allow_smilies,nl2br,me_username,filter_cdata.
  87. * @return string The parsed message.
  88. */
  89. function parse_message($message, $options=array())
  90. {
  91. global $plugins, $mybb;
  92. // Set base URL for parsing smilies
  93. $this->base_url = $mybb->settings['bburl'];
  94. if($this->base_url != "")
  95. {
  96. if(my_substr($this->base_url, my_strlen($this->base_url) -1) != "/")
  97. {
  98. $this->base_url = $this->base_url."/";
  99. }
  100. }
  101. // Set the options
  102. $this->options = $options;
  103. $message = $plugins->run_hooks("parse_message_start", $message);
  104. // Get rid of cartridge returns for they are the workings of the devil
  105. $message = str_replace("\r", "", $message);
  106. // Filter bad words if requested.
  107. if($this->options['filter_badwords'])
  108. {
  109. $message = $this->parse_badwords($message);
  110. }
  111. // Filter CDATA tags if requested (syndication.php).
  112. if(!empty($this->options['filter_cdata']))
  113. {
  114. $message = $this->parse_cdata($message);
  115. }
  116. if($this->options['allow_html'] != 1)
  117. {
  118. $message = $this->parse_html($message);
  119. }
  120. else
  121. {
  122. while(preg_match("#<s(cript|tyle)(.*)>(.*)</s(cript|tyle)(.*)>#is", $message))
  123. {
  124. $message = preg_replace("#<s(cript|tyle)(.*)>(.*)</s(cript|tyle)(.*)>#is", "&lt;s$1$2&gt;$3&lt;/s$4$5&gt;", $message);
  125. }
  126. $find = array('<?php', '<!--', '-->', '?>', "<br />\n", "<br>\n");
  127. $replace = array('&lt;?php', '&lt;!--', '--&gt;', '?&gt;', "\n", "\n");
  128. $message = str_replace($find, $replace, $message);
  129. }
  130. // If MyCode needs to be replaced, first filter out [code] and [php] tags.
  131. if($this->options['allow_mycode'])
  132. {
  133. preg_match_all("#\[(code|php)\](.*?)\[/\\1\](\r\n?|\n?)#si", $message, $code_matches, PREG_SET_ORDER);
  134. $message = preg_replace("#\[(code|php)\](.*?)\[/\\1\](\r\n?|\n?)#si", "<mybb-code>\n", $message);
  135. }
  136. // Always fix bad Javascript in the message.
  137. $message = $this->fix_javascript($message);
  138. // Replace "me" code and slaps if we have a username
  139. if($this->options['me_username'])
  140. {
  141. global $lang;
  142. $message = preg_replace('#(>|^|\r|\n)/me ([^\r\n<]*)#i', "\\1<span style=\"color: red;\">* {$this->options['me_username']} \\2</span>", $message);
  143. $message = preg_replace('#(>|^|\r|\n)/slap ([^\r\n<]*)#i', "\\1<span style=\"color: red;\">* {$this->options['me_username']} {$lang->slaps} \\2 {$lang->with_trout}</span>", $message);
  144. }
  145. // If we can, parse smilies
  146. if($this->options['allow_smilies'])
  147. {
  148. $message = $this->parse_smilies($message, $this->options['allow_html']);
  149. }
  150. // Replace MyCode if requested.
  151. if($this->options['allow_mycode'])
  152. {
  153. $message = $this->parse_mycode($message, $this->options);
  154. }
  155. // Parse Highlights
  156. if(!empty($this->options['highlight']))
  157. {
  158. $message = $this->highlight_message($message, $this->options['highlight']);
  159. }
  160. // Run plugin hooks
  161. $message = $plugins->run_hooks("parse_message", $message);
  162. if($this->options['allow_mycode'])
  163. {
  164. // Now that we're done, if we split up any code tags, parse them and glue it all back together
  165. if(count($code_matches) > 0)
  166. {
  167. foreach($code_matches as $text)
  168. {
  169. // Fix up HTML inside the code tags so it is clean
  170. if($options['allow_html'] != 0)
  171. {
  172. $text[2] = $this->parse_html($text[2]);
  173. }
  174. if(my_strtolower($text[1]) == "code")
  175. {
  176. $code = $this->mycode_parse_code($text[2]);
  177. }
  178. elseif(my_strtolower($text[1]) == "php")
  179. {
  180. $code = $this->mycode_parse_php($text[2]);
  181. }
  182. $message = preg_replace("#\<mybb-code>\n?#", $code, $message, 1);
  183. }
  184. }
  185. }
  186. // Replace meta and base tags in our post - these are > dangerous <
  187. if($this->options['allow_html'])
  188. {
  189. $message = preg_replace_callback("#<((m[^a])|(b[^diloru>])|(s[^aemptu>]))(\s*[^>]*)>#si", create_function(
  190. '$matches',
  191. 'return htmlspecialchars_uni($matches[0]);'
  192. ), $message);
  193. }
  194. if(!isset($options['nl2br']) || $options['nl2br'] != 0)
  195. {
  196. $message = nl2br($message);
  197. // Fix up new lines and block level elements
  198. $message = preg_replace("#(</?(?:html|head|body|div|p|form|table|thead|tbody|tfoot|tr|td|th|ul|ol|li|div|p|blockquote|cite|hr)[^>]*>)\s*<br />#i", "$1", $message);
  199. $message = preg_replace("#(&nbsp;)+(</?(?:html|head|body|div|p|form|table|thead|tbody|tfoot|tr|td|th|ul|ol|li|div|p|blockquote|cite|hr)[^>]*>)#i", "$2", $message);
  200. }
  201. $message = my_wordwrap($message);
  202. $message = $plugins->run_hooks("parse_message_end", $message);
  203. return $message;
  204. }
  205. /**
  206. * Converts HTML in a message to their specific entities whilst allowing unicode characters.
  207. *
  208. * @param string The message to be parsed.
  209. * @return string The formatted message.
  210. */
  211. function parse_html($message)
  212. {
  213. $message = preg_replace("#&(?!\#[0-9]+;)#si", "&amp;", $message); // fix & but allow unicode
  214. $message = str_replace("<","&lt;",$message);
  215. $message = str_replace(">","&gt;",$message);
  216. return $message;
  217. }
  218. /**
  219. * Generates a cache of MyCode, both standard and custom.
  220. *
  221. * @access private
  222. */
  223. private function cache_mycode()
  224. {
  225. global $cache, $lang;
  226. $this->mycode_cache = array();
  227. $standard_mycode = $callback_mycode = array();
  228. $standard_mycode['b']['regex'] = "#\[b\](.*?)\[/b\]#si";
  229. $standard_mycode['b']['replacement'] = "<span style=\"font-weight: bold;\">$1</span>";
  230. $standard_mycode['u']['regex'] = "#\[u\](.*?)\[/u\]#si";
  231. $standard_mycode['u']['replacement'] = "<span style=\"text-decoration: underline;\">$1</span>";
  232. $standard_mycode['i']['regex'] = "#\[i\](.*?)\[/i\]#si";
  233. $standard_mycode['i']['replacement'] = "<span style=\"font-style: italic;\">$1</span>";
  234. $standard_mycode['s']['regex'] = "#\[s\](.*?)\[/s\]#si";
  235. $standard_mycode['s']['replacement'] = "<del>$1</del>";
  236. $standard_mycode['copy']['regex'] = "#\(c\)#i";
  237. $standard_mycode['copy']['replacement'] = "&copy;";
  238. $standard_mycode['tm']['regex'] = "#\(tm\)#i";
  239. $standard_mycode['tm']['replacement'] = "&#153;";
  240. $standard_mycode['reg']['regex'] = "#\(r\)#i";
  241. $standard_mycode['reg']['replacement'] = "&reg;";
  242. $callback_mycode['url_simple']['regex'] = "#\[url\]([a-z]+?://)([^\r\n\"<]+?)\[/url\]#si";
  243. $callback_mycode['url_simple']['replacement'] = array($this, 'mycode_parse_url_callback1');
  244. $callback_mycode['url_simple2']['regex'] = "#\[url\]([^\r\n\"<]+?)\[/url\]#i";
  245. $callback_mycode['url_simple2']['replacement'] = array($this, 'mycode_parse_url_callback2');
  246. $callback_mycode['url_complex']['regex'] = "#\[url=([a-z]+?://)([^\r\n\"<]+?)\](.+?)\[/url\]#si";
  247. $callback_mycode['url_complex']['replacement'] = array($this, 'mycode_parse_url_callback1');
  248. $callback_mycode['url_complex2']['regex'] = "#\[url=([^\r\n\"<&\(\)]+?)\](.+?)\[/url\]#si";
  249. $callback_mycode['url_complex2']['replacement'] = array($this, 'mycode_parse_url_callback2');
  250. $callback_mycode['email_simple']['regex'] = "#\[email\](.*?)\[/email\]#i";
  251. $callback_mycode['email_simple']['replacement'] = array($this, 'mycode_parse_email_callback');
  252. $callback_mycode['email_complex']['regex'] = "#\[email=(.*?)\](.*?)\[/email\]#i";
  253. $callback_mycode['email_complex']['replacement'] = array($this, 'mycode_parse_email_callback');
  254. $standard_mycode['hr']['regex'] = "#\[hr\]#si";
  255. $standard_mycode['hr']['replacement'] = "<hr />";
  256. $nestable_mycode['color']['regex'] = "#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})](.*?)\[/color\]#si";
  257. $nestable_mycode['color']['replacement'] = "<span style=\"color: $1;\">$2</span>";
  258. $nestable_mycode['size']['regex'] = "#\[size=(xx-small|x-small|small|medium|large|x-large|xx-large)\](.*?)\[/size\]#si";
  259. $nestable_mycode['size']['replacement'] = "<span style=\"font-size: $1;\">$2</span>";
  260. $callback_mycode['size_int']['regex'] = "#\[size=([0-9\+\-]+?)\](.*?)\[/size\]#si";
  261. $callback_mycode['size_int']['replacement'] = array($this, 'mycode_handle_size_callback');
  262. $nestable_mycode['font']['regex'] = "#\[font=([a-z ]+?)\](.+?)\[/font\]#si";
  263. $nestable_mycode['font']['replacement'] = "<span style=\"font-family: $1;\">$2</span>";
  264. $nestable_mycode['align']['regex'] = "#\[align=(left|center|right|justify)\](.*?)\[/align\]#si";
  265. $nestable_mycode['align']['replacement'] = "<div style=\"text-align: $1;\">$2</div>";
  266. $custom_mycode = $cache->read("mycode");
  267. // If there is custom MyCode, load it.
  268. if(is_array($custom_mycode))
  269. {
  270. foreach($custom_mycode as $key => $mycode)
  271. {
  272. $mycode['regex'] = str_replace("\x0", "", $mycode['regex']);
  273. $custom_mycode[$key]['regex'] = "#".$mycode['regex']."#si";
  274. }
  275. $mycode = array_merge($standard_mycode, $custom_mycode);
  276. }
  277. else
  278. {
  279. $mycode = $standard_mycode;
  280. }
  281. // Assign the MyCode to the cache.
  282. foreach($mycode as $code)
  283. {
  284. $this->mycode_cache['standard']['find'][] = $code['regex'];
  285. $this->mycode_cache['standard']['replacement'][] = $code['replacement'];
  286. }
  287. // Assign the nestable MyCode to the cache.
  288. foreach($nestable_mycode as $code)
  289. {
  290. $this->mycode_cache['nestable'][] = array('find' => $code['regex'], 'replacement' => $code['replacement']);
  291. }
  292. // Assign the nestable MyCode to the cache.
  293. foreach($callback_mycode as $code)
  294. {
  295. $this->mycode_cache['callback'][] = array('find' => $code['regex'], 'replacement' => $code['replacement']);
  296. }
  297. }
  298. /**
  299. * Parses MyCode tags in a specific message with the specified options.
  300. *
  301. * @param string The message to be parsed.
  302. * @param array Array of options in yes/no format. Options are allow_imgcode.
  303. * @return string The parsed message.
  304. */
  305. function parse_mycode($message, $options=array())
  306. {
  307. global $lang;
  308. // Cache the MyCode globally if needed.
  309. if($this->mycode_cache == 0)
  310. {
  311. $this->cache_mycode();
  312. }
  313. // Parse quotes first
  314. $message = $this->mycode_parse_quotes($message);
  315. $message = $this->mycode_auto_url($message);
  316. $message = str_replace('$', '&#36;', $message);
  317. // Replace the rest
  318. $message = preg_replace($this->mycode_cache['standard']['find'], $this->mycode_cache['standard']['replacement'], $message);
  319. foreach($this->mycode_cache['callback'] as $replace)
  320. {
  321. $message = preg_replace_callback($replace['find'], $replace['replacement'], $message);
  322. }
  323. // Replace the nestable mycode's
  324. foreach($this->mycode_cache['nestable'] as $mycode)
  325. {
  326. while(preg_match($mycode['find'], $message))
  327. {
  328. $message = preg_replace($mycode['find'], $mycode['replacement'], $message);
  329. }
  330. }
  331. // Reset list cache
  332. $this->list_elements = array();
  333. $this->list_count = 0;
  334. // Find all lists
  335. $message = preg_replace_callback("#(\[list(=(a|A|i|I|1))?\]|\[/list\])#si", array($this, 'mycode_prepare_list'), $message);
  336. // Replace all lists
  337. for($i = $this->list_count; $i > 0; $i--)
  338. {
  339. // Ignores missing end tags
  340. $message = preg_replace_callback("#\s?\[list(=(a|A|i|I|1))?&{$i}\](.*?)(\[/list&{$i}\]|$)(\r\n?|\n?)#si", array($this, 'mycode_parse_list_callback'), $message, 1);
  341. }
  342. // Convert images when allowed.
  343. if($options['allow_imgcode'] != 0)
  344. {
  345. $message = preg_replace_callback("#\[img\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback1'), $message);
  346. $message = preg_replace_callback("#\[img=([0-9]{1,3})x([0-9]{1,3})\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback2'), $message);
  347. $message = preg_replace_callback("#\[img align=([a-z]+)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback3'), $message);
  348. $message = preg_replace_callback("#\[img=([0-9]{1,3})x([0-9]{1,3}) align=([a-z]+)\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is", array($this, 'mycode_parse_img_callback4'), $message);
  349. }
  350. // Convert videos when allow.
  351. if($options['allow_videocode'] != 0)
  352. {
  353. $message = preg_replace_callback("#\[video=(.*?)\](.*?)\[/video\]#i", array($this, 'mycode_parse_video_callback'), $message);
  354. }
  355. return $message;
  356. }
  357. /**
  358. * Generates a cache of smilies
  359. *
  360. * @access private
  361. */
  362. private function cache_smilies()
  363. {
  364. global $cache, $mybb;
  365. $this->smilies_cache = array();
  366. $smilies = $cache->read("smilies");
  367. if(is_array($smilies))
  368. {
  369. foreach($smilies as $sid => $smilie)
  370. {
  371. if(defined("IN_ARCHIVE") && substr($smilie['image'], 0, 4) != "http")
  372. {
  373. // We're in the archive and not using an outside image, add in our address
  374. $smilie['image'] = $mybb->settings['bburl']."/".$smilie['image'];
  375. }
  376. $this->smilies_cache[$smilie['find']] = "<img src=\"{$smilie['image']}\" style=\"vertical-align: middle;\" border=\"0\" alt=\"{$smilie['name']}\" title=\"{$smilie['name']}\" />";
  377. }
  378. }
  379. }
  380. /**
  381. * Parses smilie code in the specified message.
  382. *
  383. * @param string The message being parsed.
  384. * @param string Base URL for the image tags created by smilies.
  385. * @param string Yes/No if HTML is allowed in the post
  386. * @return string The parsed message.
  387. */
  388. function parse_smilies($message, $allow_html=0)
  389. {
  390. if($this->smilies_cache == 0)
  391. {
  392. $this->cache_smilies();
  393. }
  394. $message = ' ' . $message . ' ';
  395. // First we take out any of the tags we don't want parsed between (url= etc)
  396. preg_match_all("#\[(url(=[^\]]*)?\]|quote=([^\]]*)?\])#i", $message, $bad_matches, PREG_PATTERN_ORDER);
  397. $message = preg_replace("#\[(url(=[^\]]*)?\]|quote=([^\]]*)?\])#si", "<mybb-bad-sm>", $message);
  398. // Impose a hard limit of 500 smilies per message as to not overload the parser
  399. $remaining = 500;
  400. if(is_array($this->smilies_cache))
  401. {
  402. foreach($this->smilies_cache as $find => $replace)
  403. {
  404. $orig_message = $message;
  405. $find = $this->parse_html($find);
  406. $find = preg_quote($find, "#");
  407. $replace = strip_tags($replace, "<img>");
  408. // Fix issues for smileys starting with a ";"
  409. $orig_find = $find;
  410. if(substr($find, 0, 1) == ";")
  411. {
  412. $find = "(?<!&gt|&lt|&amp)".$find;
  413. }
  414. $message = @preg_replace("#(?<=[^\"])".$find."(?=.\W|\"|\W.|\W$)#si", $replace, $message, $remaining, $replacements);
  415. if($message == null)
  416. {
  417. $message = preg_replace("#(?<=[^&;\"])".$orig_find."(?=.\W|\"|\W.|\W$)#si", $replace, $orig_message, $remaining);
  418. }
  419. $remaining -= $replacements;
  420. if($remaining <= 0)
  421. {
  422. break; // Reached the limit
  423. }
  424. }
  425. unset($orig_message, $orig_find);
  426. }
  427. // If we matched any tags previously, swap them back in
  428. if(count($bad_matches[0]) > 0)
  429. {
  430. foreach($bad_matches[0] as $match)
  431. {
  432. $match = str_replace('$', '\$', $match);
  433. $message = preg_replace("#<mybb-bad-sm>#", $match, $message, 1);
  434. }
  435. }
  436. return trim($message);
  437. }
  438. /**
  439. * Generates a cache of badwords filters.
  440. *
  441. * @access private
  442. */
  443. private function cache_badwords()
  444. {
  445. global $cache;
  446. $this->badwords_cache = array();
  447. $this->badwords_cache = $cache->read("badwords");
  448. }
  449. /**
  450. * Parses a list of filtered/badwords in the specified message.
  451. *
  452. * @param string The message to be parsed.
  453. * @param array Array of parser options in yes/no format.
  454. * @return string The parsed message.
  455. */
  456. function parse_badwords($message, $options=array())
  457. {
  458. if($this->badwords_cache == 0)
  459. {
  460. $this->cache_badwords();
  461. }
  462. if(is_array($this->badwords_cache))
  463. {
  464. reset($this->badwords_cache);
  465. foreach($this->badwords_cache as $bid => $badword)
  466. {
  467. if(!$badword['replacement'])
  468. {
  469. $badword['replacement'] = "*****";
  470. }
  471. // Take into account the position offset for our last replacement.
  472. $index = substr_count($badword['badword'], '*')+2;
  473. $badword['badword'] = str_replace('\*', '([a-zA-Z0-9_]{1})', preg_quote($badword['badword'], "#"));
  474. // Ensure we run the replacement enough times but not recursively (i.e. not while(preg_match..))
  475. $count = preg_match_all("#(^|\W)".$badword['badword']."(\W|$)#i", $message, $matches);
  476. for($i=0; $i < $count; ++$i)
  477. {
  478. $message = preg_replace("#(^|\W)".$badword['badword']."(\W|$)#i", "\\1".$badword['replacement'].'\\'.$index, $message);
  479. }
  480. }
  481. }
  482. if(isset($options['strip_tags']) && $options['strip_tags'] == 1)
  483. {
  484. $message = strip_tags($message);
  485. }
  486. return $message;
  487. }
  488. /**
  489. * Resolves nested CDATA tags in the specified message.
  490. *
  491. * @param string The message to be parsed.
  492. * @return string The parsed message.
  493. */
  494. function parse_cdata($message)
  495. {
  496. $message = str_replace(']]>', ']]]]><![CDATA[>', $message);
  497. return $message;
  498. }
  499. /**
  500. * Attempts to move any javascript references in the specified message.
  501. *
  502. * @param string The message to be parsed.
  503. * @return string The parsed message.
  504. */
  505. function fix_javascript($message)
  506. {
  507. $js_array = array(
  508. "#(&\#(0*)106;?|&\#(0*)74;?|&\#x(0*)4a;?|&\#x(0*)6a;?|j)((&\#(0*)97;?|&\#(0*)65;?|a)(&\#(0*)118;?|&\#(0*)86;?|v)(&\#(0*)97;?|&\#(0*)65;?|a)(\s)?(&\#(0*)115;?|&\#(0*)83;?|s)(&\#(0*)99;?|&\#(0*)67;?|c)(&\#(0*)114;?|&\#(0*)82;?|r)(&\#(0*)105;?|&\#(0*)73;?|i)(&\#112;?|&\#(0*)80;?|p)(&\#(0*)116;?|&\#(0*)84;?|t)(&\#(0*)58;?|\:))#i",
  509. "#(o)(nmouseover\s?=)#i",
  510. "#(o)(nmouseout\s?=)#i",
  511. "#(o)(nmousedown\s?=)#i",
  512. "#(o)(nmousemove\s?=)#i",
  513. "#(o)(nmouseup\s?=)#i",
  514. "#(o)(nclick\s?=)#i",
  515. "#(o)(ndblclick\s?=)#i",
  516. "#(o)(nload\s?=)#i",
  517. "#(o)(nsubmit\s?=)#i",
  518. "#(o)(nblur\s?=)#i",
  519. "#(o)(nchange\s?=)#i",
  520. "#(o)(nfocus\s?=)#i",
  521. "#(o)(nselect\s?=)#i",
  522. "#(o)(nunload\s?=)#i",
  523. "#(o)(nkeypress\s?=)#i",
  524. "#(o)(nerror\s?=)#i",
  525. "#(o)(nreset\s?=)#i",
  526. "#(o)(nabort\s?=)#i"
  527. );
  528. $message = preg_replace($js_array, "$1<strong></strong>$2$4", $message);
  529. return $message;
  530. }
  531. /**
  532. * Handles fontsize.
  533. *
  534. * @param string The original size.
  535. * @param string The text within a size tag.
  536. * @return string The parsed text.
  537. */
  538. function mycode_handle_size($size, $text)
  539. {
  540. $size = intval($size)+10;
  541. if($size > 50)
  542. {
  543. $size = 50;
  544. }
  545. $text = "<span style=\"font-size: {$size}pt;\">".str_replace("\'", "'", $text)."</span>";
  546. return $text;
  547. }
  548. /**
  549. * Handles fontsize.
  550. *
  551. * @param array Matches.
  552. * @return string The parsed text.
  553. */
  554. function mycode_handle_size_callback($matches)
  555. {
  556. return $this->mycode_handle_size($matches[1], $matches[2]);
  557. }
  558. /**
  559. * Parses quote MyCode.
  560. *
  561. * @param string The message to be parsed
  562. * @param boolean Are we formatting as text?
  563. * @return string The parsed message.
  564. */
  565. function mycode_parse_quotes($message, $text_only=false)
  566. {
  567. global $lang, $templates, $theme, $mybb;
  568. // Assign pattern and replace values.
  569. $pattern = "#\[quote\](.*?)\[\/quote\](\r\n?|\n?)#si";
  570. $pattern_callback = "#\[quote=([\"']|&quot;|)(.*?)(?:\\1)(.*?)(?:[\"']|&quot;)?\](.*?)\[/quote\](\r\n?|\n?)#si";
  571. if($text_only == false)
  572. {
  573. $replace = "<blockquote><cite>$lang->quote</cite>$1</blockquote>\n";
  574. $replace_callback = array($this, 'mycode_parse_post_quotes_callback1');
  575. }
  576. else
  577. {
  578. $replace = "\n{$lang->quote}\n--\n$1\n--\n";
  579. $replace_callback = array($this, 'mycode_parse_post_quotes_callback2');
  580. }
  581. do
  582. {
  583. // preg_replace has erased the message? Restore it...
  584. $previous_message = $message;
  585. $message = preg_replace($pattern, $replace, $message, -1, $count);
  586. $message = preg_replace_callback($pattern_callback, $replace_callback, $message, -1, $count_callback);
  587. if(!$message)
  588. {
  589. $message = $previous_message;
  590. break;
  591. }
  592. } while($count || $count_callback);
  593. if($text_only == false)
  594. {
  595. $find = array(
  596. "#(\r\n*|\n*)<\/cite>(\r\n*|\n*)#",
  597. "#(\r\n*|\n*)<\/blockquote>#"
  598. );
  599. $replace = array(
  600. "</cite><br />",
  601. "</blockquote>"
  602. );
  603. $message = preg_replace($find, $replace, $message);
  604. }
  605. return $message;
  606. }
  607. /**
  608. * Parses quotes with post id and/or dateline.
  609. *
  610. * @param string The message to be parsed
  611. * @param string The username to be parsed
  612. * @param boolean Are we formatting as text?
  613. * @return string The parsed message.
  614. */
  615. function mycode_parse_post_quotes($message, $username, $text_only=false)
  616. {
  617. global $lang, $templates, $theme, $mybb;
  618. $linkback = $date = "";
  619. $message = trim($message);
  620. $message = preg_replace("#(^<br(\s?)(\/?)>|<br(\s?)(\/?)>$)#i", "", $message);
  621. if(!$message)
  622. {
  623. return '';
  624. }
  625. $username .= "'";
  626. $delete_quote = true;
  627. preg_match("#pid=(?:&quot;|\"|')?([0-9]+)[\"']?(?:&quot;|\"|')?#i", $username, $match);
  628. if(intval($match[1]))
  629. {
  630. $pid = intval($match[1]);
  631. $url = $mybb->settings['bburl']."/".get_post_link($pid)."#pid$pid";
  632. if(defined("IN_ARCHIVE"))
  633. {
  634. $linkback = " <a href=\"{$url}\">[ -> ]</a>";
  635. }
  636. else
  637. {
  638. eval("\$linkback = \" ".$templates->get("postbit_gotopost", 1, 0)."\";");
  639. }
  640. $username = preg_replace("#(?:&quot;|\"|')? pid=(?:&quot;|\"|')?[0-9]+[\"']?(?:&quot;|\"|')?#i", '', $username);
  641. $delete_quote = false;
  642. }
  643. unset($match);
  644. preg_match("#dateline=(?:&quot;|\"|')?([0-9]+)(?:&quot;|\"|')?#i", $username, $match);
  645. if(intval($match[1]))
  646. {
  647. if($match[1] < TIME_NOW)
  648. {
  649. $postdate = my_date($mybb->settings['dateformat'], intval($match[1]));
  650. $posttime = my_date($mybb->settings['timeformat'], intval($match[1]));
  651. $date = " ({$postdate} {$posttime})";
  652. }
  653. $username = preg_replace("#(?:&quot;|\"|')? dateline=(?:&quot;|\"|')?[0-9]+(?:&quot;|\"|')?#i", '', $username);
  654. $delete_quote = false;
  655. }
  656. if($delete_quote)
  657. {
  658. $username = my_substr($username, 0, my_strlen($username)-1);
  659. }
  660. if($text_only)
  661. {
  662. return "\n".htmlspecialchars_uni($username)." $lang->wrote{$date}\n--\n{$message}\n--\n";
  663. }
  664. else
  665. {
  666. $span = "";
  667. if(!$delete_quote)
  668. {
  669. $span = "<span>{$date}</span>";
  670. }
  671. return "<blockquote><cite>{$span}".htmlspecialchars_uni($username)." $lang->wrote{$linkback}</cite>{$message}</blockquote>\n";
  672. }
  673. }
  674. /**
  675. * Parses quotes with post id and/or dateline.
  676. *
  677. * @param array Matches.
  678. * @return string The parsed message.
  679. */
  680. function mycode_parse_post_quotes_callback1($matches)
  681. {
  682. return $this->mycode_parse_post_quotes($matches[4],$matches[2].$matches[3]);
  683. }
  684. /**
  685. * Parses quotes with post id and/or dateline.
  686. *
  687. * @param array Matches.
  688. * @return string The parsed message.
  689. */
  690. function mycode_parse_post_quotes_callback2($matches)
  691. {
  692. return $this->mycode_parse_post_quotes($matches[4],$matches[2].$matches[3], true);
  693. }
  694. /**
  695. * Parses code MyCode.
  696. *
  697. * @param string The message to be parsed
  698. * @param boolean Are we formatting as text?
  699. * @return string The parsed message.
  700. */
  701. function mycode_parse_code($code, $text_only=false)
  702. {
  703. global $lang;
  704. if($text_only == true)
  705. {
  706. return "\n{$lang->code}\n--\n{$code}\n--\n";
  707. }
  708. // Clean the string before parsing.
  709. $code = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $code);
  710. $code = rtrim($code);
  711. $original = preg_replace('#^\t*#', '', $code);
  712. if(empty($original))
  713. {
  714. return;
  715. }
  716. $code = str_replace('$', '&#36;', $code);
  717. $code = preg_replace('#\$([0-9])#', '\\\$\\1', $code);
  718. $code = str_replace('\\', '&#92;', $code);
  719. $code = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $code);
  720. $code = str_replace(" ", '&nbsp;&nbsp;', $code);
  721. return "<div class=\"codeblock\">\n<div class=\"title\">".$lang->code."\n</div><div class=\"body\" dir=\"ltr\"><code>".$code."</code></div></div>\n";
  722. }
  723. /**
  724. * Parses code MyCode.
  725. *
  726. * @param array Matches.
  727. * @return string The parsed message.
  728. */
  729. function mycode_parse_code_callback($matches)
  730. {
  731. return $this->mycode_parse_code($matches[1], true);
  732. }
  733. /**
  734. * Parses PHP code MyCode.
  735. *
  736. * @param string The message to be parsed
  737. * @param boolean Whether or not it should return it as pre-wrapped in a div or not.
  738. * @param boolean Are we formatting as text?
  739. * @return string The parsed message.
  740. */
  741. function mycode_parse_php($str, $bare_return = false, $text_only = false)
  742. {
  743. global $lang;
  744. if($text_only == true)
  745. {
  746. return "\n{$lang->php_code}\n--\n$str\n--\n";
  747. }
  748. // Clean the string before parsing except tab spaces.
  749. $str = preg_replace('#^(\t*)(\n|\r|\0|\x0B| )*#', '\\1', $str);
  750. $str = rtrim($str);
  751. $original = preg_replace('#^\t*#', '', $str);
  752. if(empty($original))
  753. {
  754. return;
  755. }
  756. $str = str_replace('&amp;', '&', $str);
  757. $str = str_replace('&lt;', '<', $str);
  758. $str = str_replace('&gt;', '>', $str);
  759. // See if open and close tags are provided.
  760. $added_open_tag = false;
  761. if(!preg_match("#^\s*<\?#si", $str))
  762. {
  763. $added_open_tag = true;
  764. $str = "<?php \n".$str;
  765. }
  766. $added_end_tag = false;
  767. if(!preg_match("#\?>\s*$#si", $str))
  768. {
  769. $added_end_tag = true;
  770. $str = $str." \n?>";
  771. }
  772. $code = @highlight_string($str, true);
  773. // Do the actual replacing.
  774. $code = preg_replace('#<code>\s*<span style="color: \#000000">\s*#i', "<code>", $code);
  775. $code = preg_replace("#</span>\s*</code>#", "</code>", $code);
  776. $code = preg_replace("#</span>(\r\n?|\n?)</code>#", "</span></code>", $code);
  777. $code = str_replace("\\", '&#092;', $code);
  778. $code = str_replace('$', '&#36;', $code);
  779. $code = preg_replace("#&amp;\#([0-9]+);#si", "&#$1;", $code);
  780. if($added_open_tag)
  781. {
  782. $code = preg_replace("#<code><span style=\"color: \#([A-Z0-9]{6})\">&lt;\?php( |&nbsp;)(<br />?)#", "<code><span style=\"color: #$1\">", $code);
  783. }
  784. if($added_end_tag)
  785. {
  786. $code = str_replace("?&gt;</span></code>", "</span></code>", $code);
  787. // Wait a minute. It fails highlighting? Stupid highlighter.
  788. $code = str_replace("?&gt;</code>", "</code>", $code);
  789. }
  790. $code = preg_replace("#<span style=\"color: \#([A-Z0-9]{6})\"></span>#", "", $code);
  791. $code = str_replace("<code>", "<div dir=\"ltr\"><code>", $code);
  792. $code = str_replace("</code>", "</code></div>", $code);
  793. $code = preg_replace("# *$#", "", $code);
  794. if($bare_return)
  795. {
  796. return $code;
  797. }
  798. // Send back the code all nice and pretty
  799. return "<div class=\"codeblock phpcodeblock\"><div class=\"title\">$lang->php_code\n</div><div class=\"body\">".$code."</div></div>\n";
  800. }
  801. /**
  802. * Parses PHP code MyCode.
  803. *
  804. * @param array Matches.
  805. * @return string The parsed message.
  806. */
  807. function mycode_parse_php_callback($matches)
  808. {
  809. return $this->mycode_parse_php($matches[1], false, true);
  810. }
  811. /**
  812. * Parses URL MyCode.
  813. *
  814. * @param string The URL to link to.
  815. * @param string The name of the link.
  816. * @return string The built-up link.
  817. */
  818. function mycode_parse_url($url, $name="")
  819. {
  820. if(!preg_match("#^[a-z0-9]+://#i", $url))
  821. {
  822. $url = "http://".$url;
  823. }
  824. $fullurl = $url;
  825. $url = str_replace('&amp;', '&', $url);
  826. $name = str_replace('&amp;', '&', $name);
  827. if(!$name)
  828. {
  829. $name = $url;
  830. }
  831. if($name == $url && (!isset($this->options['shorten_urls']) || $this->options['shorten_urls'] != 0))
  832. {
  833. if(my_strlen($url) > 55)
  834. {
  835. $name = my_substr($url, 0, 40)."...".my_substr($url, -10);
  836. }
  837. }
  838. $nofollow = '';
  839. if(isset($this->options['nofollow_on']))
  840. {
  841. $nofollow = " rel=\"nofollow\"";
  842. }
  843. // Fix some entities in URLs
  844. $entities = array('$' => '%24', '&#36;' => '%24', '^' => '%5E', '`' => '%60', '[' => '%5B', ']' => '%5D', '{' => '%7B', '}' => '%7D', '"' => '%22', '<' => '%3C', '>' => '%3E', ' ' => '%20');
  845. $fullurl = str_replace(array_keys($entities), array_values($entities), $fullurl);
  846. $name = preg_replace("#&amp;\#([0-9]+);#si", "&#$1;", $name); // Fix & but allow unicode
  847. $link = "<a href=\"$fullurl\" target=\"_blank\"{$nofollow}>$name</a>";
  848. return $link;
  849. }
  850. /**
  851. * Parses URL MyCode.
  852. *
  853. * @param array Matches.
  854. * @return string The built-up link.
  855. */
  856. function mycode_parse_url_callback1($matches)
  857. {
  858. if(!isset($matches[3]))
  859. {
  860. $matches[3] = '';
  861. }
  862. return $this->mycode_parse_url($matches[1].$matches[2], $matches[3]);
  863. }
  864. /**
  865. * Parses URL MyCode.
  866. *
  867. * @param array Matches.
  868. * @return string The built-up link.
  869. */
  870. function mycode_parse_url_callback2($matches)
  871. {
  872. if(!isset($matches[2]))
  873. {
  874. $matches[2] = '';
  875. }
  876. return $this->mycode_parse_url($matches[1], $matches[2]);
  877. }
  878. /**
  879. * Parses IMG MyCode.
  880. *
  881. * @param string The URL to the image
  882. * @param array Optional array of dimensions
  883. */
  884. function mycode_parse_img($url, $dimensions=array(), $align='')
  885. {
  886. global $lang;
  887. $url = trim($url);
  888. $url = str_replace("\n", "", $url);
  889. $url = str_replace("\r", "", $url);
  890. if($align == "right")
  891. {
  892. $css_align = " style=\"float: right;\"";
  893. }
  894. else if($align == "left")
  895. {
  896. $css_align = " style=\"float: left;\"";
  897. }
  898. $alt = htmlspecialchars_uni(basename($url));
  899. if(my_strlen($alt) > 55)
  900. {
  901. $alt = my_substr($alt, 0, 40)."...".my_substr($alt, -10);
  902. }
  903. $alt = $lang->sprintf($lang->posted_image, $alt);
  904. if($dimensions[0] > 0 && $dimensions[1] > 0)
  905. {
  906. return "<img src=\"{$url}\" width=\"{$dimensions[0]}\" height=\"{$dimensions[1]}\" border=\"0\" alt=\"{$alt}\"{$css_align} />";
  907. }
  908. else
  909. {
  910. return "<img src=\"{$url}\" border=\"0\" alt=\"{$alt}\"{$css_align} />";
  911. }
  912. }
  913. /**
  914. * Parses IMG MyCode.
  915. *
  916. * @param array Matches.
  917. * @return string Image code.
  918. */
  919. function mycode_parse_img_callback1($matches)
  920. {
  921. return $this->mycode_parse_img($matches[2]);
  922. }
  923. /**
  924. * Parses IMG MyCode.
  925. *
  926. * @param array Matches.
  927. * @return string Image code.
  928. */
  929. function mycode_parse_img_callback2($matches)
  930. {
  931. return $this->mycode_parse_img($matches[4], array($matches[1], $matches[2]));
  932. }
  933. /**
  934. * Parses IMG MyCode.
  935. *
  936. * @param array Matches.
  937. * @return string Image code.
  938. */
  939. function mycode_parse_img_callback3($matches)
  940. {
  941. return $this->mycode_parse_img($matches[3], array(), $matches[1]);
  942. }
  943. /**
  944. * Parses IMG MyCode.
  945. *
  946. * @param array Matches.
  947. * @return string Image code.
  948. */
  949. function mycode_parse_img_callback4($matches)
  950. {
  951. return $this->mycode_parse_img($matches[5], array($matches[1], $matches[2]), $matches[3]);
  952. }
  953. /**
  954. * Parses email MyCode.
  955. *
  956. * @param string The email address to link to.
  957. * @param string The name for the link.
  958. * @return string The built-up email link.
  959. */
  960. function mycode_parse_email($email, $name="")
  961. {
  962. if(!$name)
  963. {
  964. $name = $email;
  965. }
  966. if(preg_match("/^([a-zA-Z0-9-_\+\.]+?)@[a-zA-Z0-9-]+\.[a-zA-Z0-9\.-]+$/si", $email))
  967. {
  968. return "<a href=\"mailto:$email\">".$name."</a>";
  969. }
  970. else
  971. {
  972. return $email;
  973. }
  974. }
  975. /**
  976. * Parses email MyCode.
  977. *
  978. * @param array Matches
  979. * @return string The built-up email link.
  980. */
  981. function mycode_parse_email_callback($matches)
  982. {
  983. if(!isset($matches[2]))
  984. {
  985. $matches[2] = '';
  986. }
  987. return $this->mycode_parse_email($matches[1], $matches[2]);
  988. }
  989. /**
  990. * Parses video MyCode.
  991. *
  992. * @param string The video provider.
  993. * @param string The video to link to.
  994. * @return string The built-up video code.
  995. */
  996. function mycode_parse_video($video, $url)
  997. {
  998. global $templates;
  999. if(empty($video) || empty($url))
  1000. {
  1001. return "[video={$video}]{$url}[/video]";
  1002. }
  1003. $parsed_url = @parse_url(urldecode($url));
  1004. if($parsed_url == false)
  1005. {
  1006. return "[video={$video}]{$url}[/video]";
  1007. }
  1008. $fragments = array();
  1009. if($parsed_url['fragment'])
  1010. {
  1011. $fragments = explode("&", $parsed_url['fragment']);
  1012. }
  1013. $queries = explode("&", $parsed_url['query']);
  1014. $input = array();
  1015. foreach($queries as $query)
  1016. {
  1017. list($key, $value) = explode("=", $query);
  1018. $key = str_replace("amp;", "", $key);
  1019. $input[$key] = $value;
  1020. }
  1021. $path = explode('/', $parsed_url['path']);
  1022. switch($video)
  1023. {
  1024. case "dailymotion":
  1025. list($id, ) = split("_", $path[2], 1); // http://www.dailymotion.com/video/fds123_title-goes-here
  1026. break;
  1027. case "metacafe":
  1028. $id = $path[2]; // http://www.metacafe.com/watch/fds123/title_goes_here/
  1029. $title = htmlspecialchars_uni($path[3]);
  1030. break;
  1031. case "myspacetv":
  1032. $id = $path[4]; // http://www.myspace.com/video/fds/fds/123
  1033. break;
  1034. case "yahoo":
  1035. $id = $path[1]; // http://xy.screen.yahoo.com/fds-123.html
  1036. // Support for localized portals
  1037. $domain = explode('.', $parsed_url['host']);
  1038. if($domain[0] != 'screen' && preg_match('#^([a-z-]+)$#', $domain[0]))
  1039. {
  1040. $local = "{$domain[0]}.";
  1041. }
  1042. else
  1043. {
  1044. $local = '';
  1045. }
  1046. break;
  1047. case "vimeo":
  1048. $id = $path[1]; // http://vimeo.com/fds123
  1049. break;
  1050. case "youtube":
  1051. if($fragments[0])
  1052. {
  1053. $id = str_replace('!v=', '', $fragments[0]); // http://www.youtube.com/watch#!v=fds123
  1054. }
  1055. elseif($input['v'])
  1056. {
  1057. $id = $input['v']; // http://www.youtube.com/watch?v=fds123
  1058. }
  1059. else
  1060. {
  1061. $id = $path[1]; // http://www.youtu.be/fds123
  1062. }
  1063. break;
  1064. default:
  1065. return "[video={$video}]{$url}[/video]";
  1066. }
  1067. if(empty($id))
  1068. {
  1069. return "[video={$video}]{$url}[/video]";
  1070. }
  1071. $id = htmlspecialchars_uni($id);
  1072. eval("\$video_code = \"".$templates->get("video_{$video}_embed")."\";");
  1073. return $video_code;
  1074. }
  1075. /**
  1076. * Parses video MyCode.
  1077. *
  1078. * @param array Matches.
  1079. * @return string The built-up video code.
  1080. */
  1081. function mycode_parse_video_callback($matches)
  1082. {
  1083. return $this->mycode_parse_video($matches[1], $matches[2]);
  1084. }
  1085. /**
  1086. * Parses URLs automatically.
  1087. *
  1088. * @param string The message to be parsed
  1089. * @return string The parsed message.
  1090. */
  1091. function mycode_auto_url($message)
  1092. {
  1093. $message = " ".$message;
  1094. // Links should end with slashes, numbers, characters and braces but not with dots, commas or question marks
  1095. $message = preg_replace_callback("#([\>\s\(\)])(http|https|ftp|news){1}://([^\/\"\s\<\[\.]+\.([^\/\"\s\<\[\.]+\.)*[\w]+(:[0-9]+)?(/[^\"\s<\[]*)?)#iu", array($this, 'mycode_auto_url_callback'), $message);
  1096. $message = preg_replace_callback("#([\>\s\(\)])(www|ftp)\.(([^\/\"\s\<\[\.]+\.)*[\w]+(:[0-9]+)?(/[^\"\s<\[]*)?)#iu", array($this, 'mycode_auto_url_callback'), $message);
  1097. $message = my_substr($message, 1);
  1098. return $message;
  1099. }
  1100. /**
  1101. * Parses URLs automatically.
  1102. *
  1103. * @param array Matches
  1104. * @return string The parsed message.
  1105. */
  1106. function mycode_auto_url_callback($matches)
  1107. {
  1108. $external = '';
  1109. // Allow links like http://en.wikipedia.org/wiki/PHP_(disambiguation) but detect mismatching braces
  1110. while(my_substr($matches[3], -1) == ')')
  1111. {
  1112. if(substr_count($matches[3], ')') > substr_count($matches[3], '('))
  1113. {
  1114. $matches[3] = my_substr($matches[3], 0, -1);
  1115. $external = ')'.$external;
  1116. }
  1117. else
  1118. {
  1119. break;
  1120. }
  1121. // Example: ([...] http://en.wikipedia.org/Example_(disambiguation).)
  1122. $last_char = my_substr($matches[3], -1);
  1123. while($last_char == '.' || $last_char == ',' || $last_char == '?' || $last_char == '!')
  1124. {
  1125. $matches[3] = my_substr($matches[3], 0, -1);
  1126. $external = $last_char.$external;
  1127. $last_char = my_substr($matches[3], -1);
  1128. }
  1129. }
  1130. if($matches[2] == 'www' || $matches[2] == 'ftp')
  1131. {
  1132. return "{$matches[1]}[url]{$matches[2]}.{$matches[3]}[/url]{$external}";
  1133. }
  1134. else
  1135. {
  1136. return "{$matches[1]}[url]{$matches[2]}://{$matches[3]}[/url]{$external}";
  1137. }
  1138. }
  1139. /**
  1140. * Parses list MyCode.
  1141. *
  1142. * @param string The message to be parsed
  1143. * @param string The list type
  1144. * @return string The parsed message.
  1145. */
  1146. function mycode_parse_list($message, $type="")
  1147. {
  1148. // No list elements? That's invalid HTML
  1149. if(strpos($message, '[*]') === false)
  1150. {
  1151. $message = "[*]{$message}";
  1152. }
  1153. $message = preg_replace("#\s*\[\*\]\s*#", "</li>\n<li>", $message);
  1154. $message .= "</li>";
  1155. if($type)
  1156. {
  1157. $list = "\n<ol type=\"$type\">$message</ol>\n";
  1158. }
  1159. else
  1160. {
  1161. $list = "<ul>$message</ul>\n";
  1162. }
  1163. $list = preg_replace("#<(ol type=\"$type\"|ul)>\s*</li>#", "<$1>", $list);
  1164. return $list;
  1165. }
  1166. /**
  1167. * Parses list MyCode.
  1168. *
  1169. * @param array Matches
  1170. * @return string The parsed message.
  1171. */
  1172. function mycode_parse_list_callback($matches)
  1173. {
  1174. return $this->mycode_parse_list($matches[3], $matches[2]);
  1175. }
  1176. /**
  1177. * Prepares list MyCode by finding the matching list tags.
  1178. *
  1179. * @param array Matches
  1180. * @return string Temporary replacements.
  1181. */
  1182. function mycode_prepare_list($matches)
  1183. {
  1184. // Append number to identify matching list tags
  1185. if($matches[1] == '[/list]')
  1186. {
  1187. $count = array_pop($this->list_elements);
  1188. if($count !== NULL)
  1189. {
  1190. return "[/list&{$count}]";
  1191. }
  1192. else
  1193. {
  1194. // No open list tag...
  1195. return $matches[0];
  1196. }
  1197. }
  1198. else
  1199. {
  1200. ++$this->list_count;
  1201. $this->list_elements[] = $this->list_count;
  1202. if(!empty($matches[2]))
  1203. {
  1204. return "[list{$matches[2]}&{$this->list_count}]";
  1205. }
  1206. else
  1207. {
  1208. return "[list&{$this->list_count}]";
  1209. }
  1210. }
  1211. }
  1212. /**
  1213. * Strips smilies from a string
  1214. *
  1215. * @param string The message for smilies to be stripped from
  1216. * @return string The message with smilies stripped
  1217. */
  1218. function strip_smilies($message)
  1219. {
  1220. if($this->smilies_cache == 0)
  1221. {
  1222. $this->cache_smilies();
  1223. }
  1224. if(is_array($this->smilies_cache))
  1225. {
  1226. $message = str_replace($this->smilies_cache, array_keys($this->smilies_cache), $message);
  1227. }
  1228. return $message;
  1229. }
  1230. /**
  1231. * Highlights a string
  1232. *
  1233. * @param string The message to be highligted
  1234. * @param string The highlight keywords
  1235. * @return string The message with highlight bbcodes
  1236. */
  1237. function highlight_message($message, $highlight)
  1238. {
  1239. if(empty($this->highlight_cache))
  1240. {
  1241. $this->highlight_cache = build_highlight_array($highlight);
  1242. }
  1243. if(is_array($this->highlight_cache) && !empty($this->highlight_cache))
  1244. {
  1245. $message = preg_replace(array_keys($this->highlight_cache), $this->highlight_cache, $message);
  1246. }
  1247. return $message;
  1248. }
  1249. /**
  1250. * Parses message to plain text equivalents of MyCode.
  1251. *
  1252. * @param string The message to be parsed
  1253. * @return string The parsed message.
  1254. */
  1255. function text_parse_message($message, $options=array())
  1256. {
  1257. global $plugins;
  1258. // Filter bad words if requested.
  1259. if($options['filter_badwords'] != 0)
  1260. {
  1261. $message = $this->parse_badwords($message);
  1262. }
  1263. // Parse quotes first
  1264. $message = $this->mycode_parse_quotes($message, true);
  1265. $message = preg_replace_callback("#\[php\](.*?)\[/php\](\r\n?|\n?)#is", array($this, 'mycode_parse_php_callback'), $message);
  1266. $message = preg_replace_callback("#\[code\](.*?)\[/code\](\r\n?|\n?)#is", array($this, 'mycode_parse_code_callback'), $message);
  1267. $find = array(
  1268. "#\[(b|u|i|s|url|email|color|img)\](.*?)\[/\\1\]#is",
  1269. "#\[img=([0-9]{1,3})x([0-9]{1,3})\](\r\n?|\n?)(https?://([^<>\"']+?))\[/img\]#is",
  1270. "#\[url=([a-z]+?://)([^\r\n\"<]+?)\](.+?)\[/url\]#si",
  1271. "#\[url=([^\r\n\"<&\(\)]+?)\](.+?)\[/url\]#si",
  1272. );
  1273. $replace = array(
  1274. "$2",
  1275. "$4",
  1276. "$3 ($1$2)",
  1277. "$2 ($1)",
  1278. );
  1279. $message = preg_replace($find, $replace, $message);
  1280. // Replace "me" code and slaps if we have a username
  1281. if($options['me_username'])
  1282. {
  1283. global $lang;
  1284. $message = preg_replace('#(>|^|\r|\n)/me ([^\r\n<]*)#i', "\\1* {$options['me_username']} \\2", $message);
  1285. $message = preg_replace('#(>|^|\r|\n)/slap ([^\r\n<]*)#i', "\\1* {$options['me_username']} {$lang->slaps} \\2 {$lang->with_trout}", $message);
  1286. }
  1287. // Reset list cache
  1288. $this->list_elements = array();
  1289. $this->list_count = 0;
  1290. // Find all lists
  1291. $message = preg_replace_callback("#(\[list(=(a|A|i|I|1))?\]|\[/list\])#si", array($this, 'mycode_prepare_list'), $message);
  1292. // Replace all lists
  1293. for($i = $this->list_count; $i > 0; $i--)
  1294. {
  1295. // Ignores missing end tags
  1296. $message = preg_replace_callback("#\s?\[list(=(a|A|i|I|1))?&{$i}\](.*?)(\[/list&{$i}\]|$)(\r\n?|\n?)#si", array($this, 'mycode_parse_list_callback'), $message, 1);
  1297. }
  1298. // Run plugin hooks
  1299. $message = $plugins->run_hooks("text_parse_message", $message);
  1300. return $message;
  1301. }
  1302. }
  1303. ?>