PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/include/parser.php

https://github.com/Dratone/EveBB
PHP | 951 lines | 645 code | 151 blank | 155 comment | 176 complexity | b3a301a3f648cf0d32eeebab637b5208 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2010 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. // Make sure no one attempts to run this script "directly"
  8. if (!defined('PUN'))
  9. exit;
  10. // Global variables
  11. /* regular expression to match nested BBCode LIST tags
  12. '%
  13. \[list # match opening bracket and tag name of outermost LIST tag
  14. (?:=([1a*]))?+ # optional attribute capture in group 1
  15. \] # closing bracket of outermost opening LIST tag
  16. ( # capture contents of LIST tag in group 2
  17. (?: # non capture group for either contents or whole nested LIST
  18. [^\[]*+ # unroll the loop! consume everything up to next [ (normal *)
  19. (?: # (See "Mastering Regular Expressions" chapter 6 for details)
  20. (?! # negative lookahead ensures we are NOT on [LIST*] or [/LIST]
  21. \[list # opening LIST tag
  22. (?:=[1a*])?+ # with optional attribute
  23. \] # closing bracket of opening LIST tag
  24. | # or...
  25. \[/list\] # a closing LIST tag
  26. ) # end negative lookahead assertion (we are not on a LIST tag)
  27. \[ # match the [ which is NOT the start of LIST tag (special)
  28. [^\[]*+ # consume everything up to next [ (normal *)
  29. )*+ # finish up "unrolling the loop" technique (special (normal*))*
  30. | # or...
  31. (?R) # recursively match a whole nested LIST element
  32. )* # as many times as necessary until deepest nested LIST tag grabbed
  33. ) # end capturing contents of LIST tag into group 2
  34. \[/list\] # match outermost closing LIST tag
  35. %iex' */
  36. $re_list = '%\[list(?:=([1a*]))?+\]((?:[^\[]*+(?:(?!\[list(?:=[1a*])?+\]|\[/list\])\[[^\[]*+)*+|(?R))*)\[/list\]%ie';
  37. // Here you can add additional smilies if you like (please note that you must escape single quote and backslash)
  38. /*$smilies = array(
  39. ':)' => 'smile.png',
  40. '=)' => 'smile.png',
  41. ':|' => 'neutral.png',
  42. '=|' => 'neutral.png',
  43. ':(' => 'sad.png',
  44. '=(' => 'sad.png',
  45. ':D' => 'big_smile.png',
  46. '=D' => 'big_smile.png',
  47. ':o' => 'yikes.png',
  48. ':O' => 'yikes.png',
  49. ';)' => 'wink.png',
  50. ':/' => 'hmm.png',
  51. ':P' => 'tongue.png',
  52. ':p' => 'tongue.png',
  53. ':lol:' => 'lol.png',
  54. ':mad:' => 'mad.png',
  55. ':rolleyes:' => 'roll.png',
  56. ':cool:' => 'cool.png');*/
  57. //EZBBC Smilies.
  58. require PUN_ROOT.'plugins/ezbbc/ezbbc_smilies1.php';
  59. //
  60. // Make sure all BBCodes are lower case and do a little cleanup
  61. //
  62. function preparse_bbcode($text, &$errors, $is_signature = false)
  63. {
  64. global $pun_config, $lang_common, $lang_post, $re_list;
  65. if ($is_signature)
  66. {
  67. global $lang_profile;
  68. if (preg_match('%\[/?(?:quote|code|list|h|video)\b[^\]]*\]%i', $text))
  69. $errors[] = $lang_profile['Signature quote/code/list/h'];
  70. }
  71. // If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
  72. if (strpos($text, '[code]') !== false && strpos($text, '[/code]') !== false)
  73. {
  74. list($inside, $outside) = split_text($text, '[code]', '[/code]', $errors);
  75. $text = implode("\1", $outside);
  76. }
  77. // Tidy up lists
  78. $temp = preg_replace($re_list, 'preparse_list_tag(\'$2\', \'$1\', $errors)', $text);
  79. // If the regex failed
  80. if ($temp === null)
  81. $errors[] = $lang_common['BBCode list size error'];
  82. else
  83. $text = str_replace('*'."\0".']', '*]', $temp);
  84. if ($pun_config['o_make_links'] == '1')
  85. $text = do_clickable($text);
  86. // If we split up the message before we have to concatenate it together again (code tags)
  87. if (isset($inside))
  88. {
  89. $outside = explode("\1", $text);
  90. $text = '';
  91. $num_tokens = count($outside);
  92. for ($i = 0; $i < $num_tokens; ++$i)
  93. {
  94. $text .= $outside[$i];
  95. if (isset($inside[$i]))
  96. $text .= '[code]'.$inside[$i].'[/code]';
  97. }
  98. unset($inside);
  99. }
  100. $temp_text = false;
  101. if (empty($errors))
  102. $temp_text = preparse_tags($text, $errors, $is_signature);
  103. if ($temp_text !== false)
  104. $text = $temp_text;
  105. // Remove empty tags
  106. while (($new_text = strip_empty_bbcode($text, $errors)) !== false)
  107. {
  108. if ($new_text != $text)
  109. {
  110. $text = $new_text;
  111. if ($new_text == '')
  112. {
  113. $errors[] = $lang_post['Empty after strip'];
  114. break;
  115. }
  116. }
  117. else
  118. break;
  119. }
  120. return pun_trim($text);
  121. }
  122. //
  123. // Strip empty bbcode tags from some text
  124. //
  125. function strip_empty_bbcode($text, &$errors)
  126. {
  127. // If the message contains a code tag we have to split it up (empty tags within [code][/code] are fine)
  128. if (strpos($text, '[code]') !== false && strpos($text, '[/code]') !== false)
  129. {
  130. list($inside, $outside) = split_text($text, '[code]', '[/code]', $errors);
  131. $text = implode("\1", $outside);
  132. }
  133. // Remove empty tags
  134. while (($new_text = preg_replace('/\[(b|u|s|ins|del|em|i|h|colou?r|quote|img|url|email|list|video)(?:\=[^\]]*)?\]\s*\[\/\1\]/', '', $text)) !== NULL)
  135. {
  136. if ($new_text != $text)
  137. $text = $new_text;
  138. else
  139. break;
  140. }
  141. // If we split up the message before we have to concatenate it together again (code tags)
  142. if (isset($inside))
  143. {
  144. $outside = explode("\1", $text);
  145. $text = '';
  146. $num_tokens = count($outside);
  147. for ($i = 0; $i < $num_tokens; ++$i)
  148. {
  149. $text .= $outside[$i];
  150. if (isset($inside[$i]))
  151. $text .= '[code]'.$inside[$i].'[/code]';
  152. }
  153. }
  154. // Remove empty code tags
  155. while (($new_text = preg_replace('/\[(code)\]\s*\[\/\1\]/', '', $text)) !== NULL)
  156. {
  157. if ($new_text != $text)
  158. $text = $new_text;
  159. else
  160. break;
  161. }
  162. return $text;
  163. }
  164. //
  165. // Check the structure of bbcode tags and fix simple mistakes where possible
  166. //
  167. function preparse_tags($text, &$errors, $is_signature = false)
  168. {
  169. global $lang_common, $pun_config;
  170. // Start off by making some arrays of bbcode tags and what we need to do with each one
  171. // List of all the tags
  172. $tags = array('quote', 'code', 'b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'url', 'email', 'img', 'list', '*', 'h', 'size', 'video', 'char', 'contract', 'item', 'route', 'market', 'chat', 'dest', 'fitting');
  173. // List of tags that we need to check are open (You could not put b,i,u in here then illegal nesting like [b][i][/b][/i] would be allowed)
  174. $tags_opened = $tags;
  175. // and tags we need to check are closed (the same as above, added it just in case)
  176. $tags_closed = $tags;
  177. // Tags we can nest and the depth they can be nested to
  178. $tags_nested = array('quote' => $pun_config['o_quote_depth'], 'list' => 5, '*' => 5);
  179. // Tags to ignore the contents of completely (just code)
  180. $tags_ignore = array('code');
  181. // Block tags, block tags can only go within another block tag, they cannot be in a normal tag
  182. $tags_block = array('quote', 'code', 'list', 'h', '*');
  183. // Inline tags, we do not allow new lines in these
  184. $tags_inline = array('b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'h', 'size', 'video', 'char', 'contract', 'item', 'route', 'market', 'chat', 'fitting');
  185. // Tags we trim interior space
  186. $tags_trim = array('img', 'video');
  187. // Tags we remove quotes from the argument
  188. $tags_quotes = array('url', 'email', 'img');
  189. // Tags we limit bbcode in
  190. $tags_limit_bbcode = array(
  191. '*' => array('b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'url', 'email', 'list', 'img', 'code'),
  192. 'list' => array('*'),
  193. 'url' => array('b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'img'),
  194. 'email' => array('b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'img'),
  195. 'img' => array(),
  196. 'h' => array('b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'url', 'email'),
  197. 'size' => array('b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'url', 'email'),
  198. video => array()
  199. );
  200. // Tags we can automatically fix bad nesting
  201. $tags_fix = array('quote', 'b', 'i', 'u', 's', 'ins', 'del', 'em', 'color', 'colour', 'url', 'email', 'h', 'size');
  202. $split_text = preg_split("/(\[[\*a-zA-Z0-9-\/]*?(?:=.*?)?\])/", $text, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  203. $open_tags = array('post');
  204. $open_args = array('');
  205. $opened_tag = 0;
  206. $new_text = '';
  207. $current_ignore = '';
  208. $current_nest = '';
  209. $current_depth = array();
  210. $limit_bbcode = $tags;
  211. foreach ($split_text as $current)
  212. {
  213. if ($current == '')
  214. continue;
  215. // Are we dealing with a tag?
  216. if (substr($current, 0, 1) != '[' || substr($current, -1, 1) != ']')
  217. {
  218. // It's not a bbcode tag so we put it on the end and continue
  219. // If we are nested too deeply don't add to the end
  220. if ($current_nest)
  221. continue;
  222. $current = str_replace("\r\n", "\n", $current);
  223. $current = str_replace("\r", "\n", $current);
  224. if (in_array($open_tags[$opened_tag], $tags_inline) && strpos($current, "\n") !== false)
  225. {
  226. // Deal with new lines
  227. $split_current = preg_split("/(\n\n+)/", $current, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
  228. $current = '';
  229. if (!pun_trim($split_current[0], "\n")) // The first part is a linebreak so we need to handle any open tags first
  230. array_unshift($split_current, '');
  231. for ($i = 1; $i < count($split_current); $i += 2)
  232. {
  233. $temp_opened = array();
  234. $temp_opened_arg = array();
  235. $temp = $split_current[$i - 1];
  236. while (!empty($open_tags))
  237. {
  238. $temp_tag = array_pop($open_tags);
  239. $temp_arg = array_pop($open_args);
  240. if (in_array($temp_tag , $tags_inline))
  241. {
  242. array_push($temp_opened, $temp_tag);
  243. array_push($temp_opened_arg, $temp_arg);
  244. $temp .= '[/'.$temp_tag.']';
  245. }
  246. else
  247. {
  248. array_push($open_tags, $temp_tag);
  249. array_push($open_args, $temp_arg);
  250. break;
  251. }
  252. }
  253. $current .= $temp.$split_current[$i];
  254. $temp = '';
  255. while (!empty($temp_opened))
  256. {
  257. $temp_tag = array_pop($temp_opened);
  258. $temp_arg = array_pop($temp_opened_arg);
  259. if (empty($temp_arg))
  260. $temp .= '['.$temp_tag.']';
  261. else
  262. $temp .= '['.$temp_tag.'='.$temp_arg.']';
  263. array_push($open_tags, $temp_tag);
  264. array_push($open_args, $temp_arg);
  265. }
  266. $current .= $temp;
  267. }
  268. if (array_key_exists($i - 1, $split_current))
  269. $current .= $split_current[$i - 1];
  270. }
  271. if (in_array($open_tags[$opened_tag], $tags_trim))
  272. $new_text .= pun_trim($current);
  273. else
  274. $new_text .= $current;
  275. continue;
  276. }
  277. // Get the name of the tag
  278. $current_arg = '';
  279. if (strpos($current, '/') === 1)
  280. {
  281. $current_tag = substr($current, 2, -1);
  282. }
  283. else if (strpos($current, '=') === false)
  284. {
  285. $current_tag = substr($current, 1, -1);
  286. }
  287. else
  288. {
  289. $current_tag = substr($current, 1, strpos($current, '=')-1);
  290. $current_arg = substr($current, strpos($current, '=')+1, -1);
  291. }
  292. $current_tag = strtolower($current_tag);
  293. // Is the tag defined?
  294. if (!in_array($current_tag, $tags))
  295. {
  296. // It's not a bbcode tag so we put it on the end and continue
  297. if (!$current_nest)
  298. $new_text .= $current;
  299. continue;
  300. }
  301. // We definitely have a bbcode tag
  302. // Make the tag string lower case
  303. if ($equalpos = strpos($current,'='))
  304. {
  305. // We have an argument for the tag which we don't want to make lowercase
  306. if (strlen(substr($current, $equalpos)) == 2)
  307. {
  308. // Empty tag argument
  309. $errors[] = sprintf($lang_common['BBCode error empty attribute'], $current_tag);
  310. return false;
  311. }
  312. $current = strtolower(substr($current, 0, $equalpos)).substr($current, $equalpos);
  313. }
  314. else
  315. $current = strtolower($current);
  316. // This is if we are currently in a tag which escapes other bbcode such as code
  317. if ($current_ignore)
  318. {
  319. if ('[/'.$current_ignore.']' == $current)
  320. {
  321. // We've finished the ignored section
  322. $current = '[/'.$current_tag.']';
  323. $current_ignore = '';
  324. }
  325. $new_text .= $current;
  326. continue;
  327. }
  328. if ($current_nest)
  329. {
  330. // We are currently too deeply nested so lets see if we are closing the tag or not
  331. if ($current_tag != $current_nest)
  332. continue;
  333. if (substr($current, 1, 1) == '/')
  334. $current_depth[$current_nest]--;
  335. else
  336. $current_depth[$current_nest]++;
  337. if ($current_depth[$current_nest] <= $tags_nested[$current_nest])
  338. $current_nest = '';
  339. continue;
  340. }
  341. // Check the current tag is allowed here
  342. if (!in_array($current_tag, $limit_bbcode) && $current_tag != $open_tags[$opened_tag])
  343. {
  344. $errors[] = sprintf($lang_common['BBCode error invalid nesting'], $current_tag, $open_tags[$opened_tag]);
  345. return false;
  346. }
  347. if (substr($current, 1, 1) == '/')
  348. {
  349. // This is if we are closing a tag
  350. if ($opened_tag == 0 || !in_array($current_tag, $open_tags))
  351. {
  352. // We tried to close a tag which is not open
  353. if (in_array($current_tag, $tags_opened))
  354. {
  355. $errors[] = sprintf($lang_common['BBCode error no opening tag'], $current_tag);
  356. return false;
  357. }
  358. }
  359. else
  360. {
  361. // Check nesting
  362. while (true)
  363. {
  364. // Nesting is ok
  365. if ($open_tags[$opened_tag] == $current_tag)
  366. {
  367. array_pop($open_tags);
  368. array_pop($open_args);
  369. $opened_tag--;
  370. break;
  371. }
  372. // Nesting isn't ok, try to fix it
  373. if (in_array($open_tags[$opened_tag], $tags_closed) && in_array($current_tag, $tags_closed))
  374. {
  375. if (in_array($current_tag, $open_tags))
  376. {
  377. $temp_opened = array();
  378. $temp_opened_arg = array();
  379. $temp = '';
  380. while (!empty($open_tags))
  381. {
  382. $temp_tag = array_pop($open_tags);
  383. $temp_arg = array_pop($open_args);
  384. if (!in_array($temp_tag, $tags_fix))
  385. {
  386. // We couldn't fix nesting
  387. $errors[] = sprintf($lang_common['BBCode error no closing tag'], array_pop($temp_opened));
  388. return false;
  389. }
  390. array_push($temp_opened, $temp_tag);
  391. array_push($temp_opened_arg, $temp_arg);
  392. if ($temp_tag == $current_tag)
  393. break;
  394. else
  395. $temp .= '[/'.$temp_tag.']';
  396. }
  397. $current = $temp.$current;
  398. $temp = '';
  399. array_pop($temp_opened);
  400. array_pop($temp_opened_arg);
  401. while (!empty($temp_opened))
  402. {
  403. $temp_tag = array_pop($temp_opened);
  404. $temp_arg = array_pop($temp_opened_arg);
  405. if (empty($temp_arg))
  406. $temp .= '['.$temp_tag.']';
  407. else
  408. $temp .= '['.$temp_tag.'='.$temp_arg.']';
  409. array_push($open_tags, $temp_tag);
  410. array_push($open_args, $temp_arg);
  411. }
  412. $current .= $temp;
  413. $opened_tag--;
  414. break;
  415. }
  416. else
  417. {
  418. // We couldn't fix nesting
  419. $errors[] = sprintf($lang_common['BBCode error no opening tag'], $current_tag);
  420. return false;
  421. }
  422. }
  423. else if (in_array($open_tags[$opened_tag], $tags_closed))
  424. break;
  425. else
  426. {
  427. array_pop($open_tags);
  428. array_pop($open_args);
  429. $opened_tag--;
  430. }
  431. }
  432. }
  433. if (in_array($current_tag, array_keys($tags_nested)))
  434. {
  435. if (isset($current_depth[$current_tag]))
  436. $current_depth[$current_tag]--;
  437. }
  438. if (in_array($open_tags[$opened_tag], array_keys($tags_limit_bbcode)))
  439. $limit_bbcode = $tags_limit_bbcode[$open_tags[$opened_tag]];
  440. else
  441. $limit_bbcode = $tags;
  442. $new_text .= $current;
  443. continue;
  444. }
  445. else
  446. {
  447. // We are opening a tag
  448. if (in_array($current_tag, array_keys($tags_limit_bbcode)))
  449. $limit_bbcode = $tags_limit_bbcode[$current_tag];
  450. else
  451. $limit_bbcode = $tags;
  452. if (in_array($current_tag, $tags_block) && !in_array($open_tags[$opened_tag], $tags_block) && $opened_tag != 0)
  453. {
  454. // We tried to open a block tag within a non-block tag
  455. $errors[] = sprintf($lang_common['BBCode error invalid nesting'], $current_tag, $open_tags[$opened_tag]);
  456. return false;
  457. }
  458. if (in_array($current_tag, $tags_ignore))
  459. {
  460. // It's an ignore tag so we don't need to worry about what's inside it
  461. $current_ignore = $current_tag;
  462. $new_text .= $current;
  463. continue;
  464. }
  465. // Deal with nested tags
  466. if (in_array($current_tag, $open_tags) && !in_array($current_tag, array_keys($tags_nested)))
  467. {
  468. // We nested a tag we shouldn't
  469. $errors[] = sprintf($lang_common['BBCode error invalid self-nesting'], $current_tag);
  470. return false;
  471. }
  472. else if (in_array($current_tag, array_keys($tags_nested)))
  473. {
  474. // We are allowed to nest this tag
  475. if (isset($current_depth[$current_tag]))
  476. $current_depth[$current_tag]++;
  477. else
  478. $current_depth[$current_tag] = 1;
  479. // See if we are nested too deep
  480. if ($current_depth[$current_tag] > $tags_nested[$current_tag])
  481. {
  482. $current_nest = $current_tag;
  483. continue;
  484. }
  485. }
  486. // Remove quotes from arguments for certain tags
  487. if (strpos($current, '=') !== false && in_array($current_tag, $tags_quotes))
  488. {
  489. $current = preg_replace('#\['.$current_tag.'=("|\'|)(.*?)\\1\]\s*#i', '['.$current_tag.'=$2]', $current);
  490. }
  491. if (in_array($current_tag, array_keys($tags_limit_bbcode)))
  492. $limit_bbcode = $tags_limit_bbcode[$current_tag];
  493. $open_tags[] = $current_tag;
  494. $open_args[] = $current_arg;
  495. $opened_tag++;
  496. $new_text .= $current;
  497. continue;
  498. }
  499. }
  500. // Check we closed all the tags we needed to
  501. foreach ($tags_closed as $check)
  502. {
  503. if (in_array($check, $open_tags))
  504. {
  505. // We left an important tag open
  506. $errors[] = sprintf($lang_common['BBCode error no closing tag'], $check);
  507. return false;
  508. }
  509. }
  510. if ($current_ignore)
  511. {
  512. // We left an ignore tag open
  513. $errors[] = sprintf($lang_common['BBCode error no closing tag'], $current_ignore);
  514. return false;
  515. }
  516. return $new_text;
  517. }
  518. //
  519. // Preparse the contents of [list] bbcode
  520. //
  521. function preparse_list_tag($content, $type = '*', &$errors)
  522. {
  523. global $lang_common, $re_list;
  524. if (strlen($type) != 1)
  525. $type = '*';
  526. if (strpos($content,'[list') !== false)
  527. {
  528. $content = preg_replace($re_list, 'preparse_list_tag(\'$2\', \'$1\', $errors)', $content);
  529. }
  530. $items = explode('[*]', str_replace('\"', '"', $content));
  531. $content = '';
  532. foreach ($items as $item)
  533. {
  534. if (pun_trim($item) != '')
  535. $content .= '[*'."\0".']'.str_replace('[/*]', '', pun_trim($item)).'[/*'."\0".']'."\n";
  536. }
  537. return '[list='.$type.']'."\n".$content.'[/list]';
  538. }
  539. //
  540. // Truncate URL if longer than 55 characters (add http:// or ftp:// if missing)
  541. //
  542. function handle_url_tag($url, $link = '', $bbcode = false)
  543. {
  544. $url = pun_trim($url);
  545. $full_url = str_replace(array(' ', '\'', '`', '"'), array('%20', '', '', ''), $url);
  546. if (strpos($url, 'www.') === 0) // If it starts with www, we add http://
  547. $full_url = 'http://'.$full_url;
  548. else if (strpos($url, 'ftp.') === 0) // Else if it starts with ftp, we add ftp://
  549. $full_url = 'ftp://'.$full_url;
  550. else if (!preg_match('#^([a-z0-9]{3,6})://#', $url)) // Else if it doesn't start with abcdef://, we add http://
  551. $full_url = 'http://'.$full_url;
  552. // Ok, not very pretty :-)
  553. if ($bbcode)
  554. {
  555. if ($full_url == $link)
  556. return '[url]'.$link.'[/url]';
  557. else
  558. return '[url='.$full_url.']'.$link.'[/url]';
  559. }
  560. else
  561. {
  562. if ($link == '' || $link == $url)
  563. {
  564. $url = pun_htmlspecialchars_decode($url);
  565. $link = utf8_strlen($url) > 55 ? utf8_substr($url, 0 , 39).' … '.utf8_substr($url, -10) : $url;
  566. $link = pun_htmlspecialchars($link);
  567. }
  568. else
  569. $link = stripslashes($link);
  570. return '<a href="'.$full_url.'">'.$link.'</a>';
  571. }
  572. }
  573. //
  574. // Turns an URL from the [img] tag into an <img> tag or a <a href...> tag
  575. //
  576. function handle_img_tag($url, $is_signature = false, $alt = null)
  577. {
  578. global $lang_common, $pun_user;
  579. if ($alt == null)
  580. $alt = basename($url);
  581. $img_tag = '<a href="'.$url.'">&lt;'.$lang_common['Image link'].' - '.$alt.'&gt;</a>';
  582. if ($is_signature && $pun_user['show_img_sig'] != '0') {
  583. $img_tag = '<img class="sigimage" src="'.$url.'" alt="'.$alt.'" />';
  584. } else if (!$is_signature && $pun_user['show_img'] != '0') {
  585. $img_tag = '<span class="postimg"><img src="'.$url.'" alt="'.$alt.'" /></span>';
  586. } //End if - else.
  587. return $img_tag;
  588. }
  589. //
  590. // Parse the contents of [list] bbcode
  591. //
  592. function handle_list_tag($content, $type = '*')
  593. {
  594. global $re_list;
  595. if (strlen($type) != 1) {
  596. $type = '*';
  597. } //End if .
  598. if (strpos($content,'[list') !== false)
  599. {
  600. $content = preg_replace($re_list, 'handle_list_tag(\'$2\', \'$1\')', $content);
  601. }
  602. $content = preg_replace('#\s*\[\*\](.*?)\[/\*\]\s*#s', '<li><p>$1</p></li>', pun_trim($content));
  603. if ($type == '*') {
  604. $content = '<ul>'.$content.'</ul>';
  605. }
  606. else
  607. if ($type == 'a')
  608. $content = '<ol class="alpha">'.$content.'</ol>';
  609. else
  610. $content = '<ol class="decimal">'.$content.'</ol>';
  611. return '</p>'.$content.'<p>';
  612. }
  613. //
  614. // Convert BBCodes to their HTML equivalent
  615. //
  616. function do_bbcode($text, $is_signature = false)
  617. {
  618. global $lang_common, $pun_user, $pun_config, $re_list;
  619. if (strpos($text, '[quote') !== false)
  620. {
  621. $text = preg_replace('#\[quote\]\s*#', '</p><div class="quotebox"><blockquote><div><p>', $text);
  622. $text = preg_replace('#\[quote=(&quot;|&\#039;|"|\'|)(.*?)\\1\]#se', '"</p><div class=\"quotebox\"><cite>".str_replace(array(\'[\', \'\\"\'), array(\'&#91;\', \'"\'), \'$2\')." ".$lang_common[\'wrote\']."</cite><blockquote><div><p>"', $text);
  623. $text = preg_replace('#\s*\[\/quote\]#S', '</p></div></blockquote></div><p>', $text);
  624. }
  625. if (!$is_signature)
  626. {
  627. $pattern[] = $re_list;
  628. $replace[] = 'handle_list_tag(\'$2\', \'$1\')';
  629. }
  630. $pattern[] = '#\[b\](.*?)\[/b\]#ms';
  631. $pattern[] = '#\[i\](.*?)\[/i\]#ms';
  632. $pattern[] = '#\[u\](.*?)\[/u\]#ms';
  633. $pattern[] = '#\[s\](.*?)\[/s\]#ms';
  634. $pattern[] = '#\[del\](.*?)\[/del\]#ms';
  635. $pattern[] = '#\[ins\](.*?)\[/ins\]#ms';
  636. $pattern[] = '#\[em\](.*?)\[/em\]#ms';
  637. $pattern[] = '#\[colou?r=([a-zA-Z]{3,20}|\#[0-9a-fA-F]{6}|\#[0-9a-fA-F]{3})](.*?)\[/colou?r\]#ms';
  638. $pattern[] = '#\[h\](.*?)\[/h\]#ms';
  639. $pattern[] = '#\[size=([0-9]{1,2})\](.*?)\[/size\]#ms';
  640. $pattern[] = '#\[video\]([^\[<]*?)/video/([^_\[<]*?)_([^\[<]*?)\[/video\]#ms';
  641. $pattern[] = '#\[video=([0-9]+),([0-9]+)\]([^\[<]*?)/video/([^_\[<]*?)_([^\[<]*?)\[/video\]#ms';
  642. $pattern[] = '#\[video\]([^\[<]*?)/(v/|watch\?v=)([^\[<]*?)\[/video\]#ms';
  643. $pattern[] = '#\[video=([0-9]+),([0-9]+)\]([^\[<]*?)/(v/|watch\?v=)([^\[<]*?)\[/video\]#ms';
  644. $pattern[] = '#\[char=([0-9]+)\](.*?)\[/char\]#ms';
  645. $pattern[] = '#\[contract=([0-9]+),([0-9]+)\](.*?)\[/contract\]#ms';
  646. $pattern[] = '#\[item=([0-9]+)\](.*?)\[/item\]#ms';
  647. $pattern[] = '#\[route=([0-9]+)\,([0-9]+)\](.*?)\[/route\]#ms';
  648. $pattern[] = '#\[route=([0-9]+)\](.*?)\[/route\]#ms';
  649. $pattern[] = '#\[dest=([0-9]+)\](.*?)\[/dest\]#ms';
  650. $pattern[] = '#\[market=([0-9]+)\](.*?)\[/market\]#ms';
  651. $pattern[] = '#\[map=([0-9]+)\](.*?)\[/map\]#ms';
  652. $pattern[] = '#\[chat=([\s\d\w\-\.]+)\](.*?)\[/chat\]#ms';
  653. $pattern[] = '#\[fitting=([0-9\:\;]+)\](.*?)\[/fitting\]#ms';
  654. $replace[] = '<strong>$1</strong>';
  655. $replace[] = '<em>$1</em>';
  656. $replace[] = '<span class="bbu">$1</span>';
  657. $replace[] = '<span class="bbs">$1</span>';
  658. $replace[] = '<del>$1</del>';
  659. $replace[] = '<ins>$1</ins>';
  660. $replace[] = '<em>$1</em>';
  661. $replace[] = '<span style="color: $1">$2</span>';
  662. $replace[] = '</p><h5>$1</h5><p>';
  663. $replace[] = '<span style="font-size: $1px;">$2</span>';
  664. $replace[] = '<object type="application/x-shockwave-flash" data="http://www.dailymotion.com/swf/video/$2" width="480" height="384"><param name="movie" value="http://www.dailymotion.com/swf/video/$2" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><p>Flash required</p></object>';
  665. $replace[] = '<object type="application/x-shockwave-flash" data="http://www.dailymotion.com/swf/video/$4" width="$1" height="$2"><param name="movie" value="http://www.dailymotion.com/swf/video/$4" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><p>Flash required</p></object>';
  666. $replace[] = '<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/$3" width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$3" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><p>Flash required</p></object>';
  667. $replace[] = '<object type="application/x-shockwave-flash" data="http://www.youtube.com/v/$5" width="$1" height="$2"><param name="movie" value="http://www.youtube.com/v/$5" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><p>Flash required</p></object>';
  668. $replace[] = '<a href="#" class="eve_char_link" onclick="CCPEVE.showInfo(1377, $1); return false">$2</a>';
  669. $replace[] = '<a href="#" class="eve_contract_link" onclick="CCPEVE.showContract($1, $2); return false">$3</a>';
  670. $replace[] = '<a href="#" class="eve_item_link" onclick="CCPEVE.showInfo($1); return false">$2</a>';
  671. $replace[] = '<a href="#" class="eve_route_link" onclick="CCPEVE.showRouteTo($1, $2); return false">$3</a>';
  672. $replace[] = '<a href="#" class="eve_route_link" onclick="CCPEVE.showRouteTo($1); return false">$2</a>';
  673. $replace[] = '<a href="#" class="eve_dest_link" onclick="CCPEVE.setDestination($1); return false">$2</a>';
  674. $replace[] = '<a href="#" class="eve_market_link" onclick="CCPEVE.showMarketDetails($1); return false">$2</a>';
  675. $replace[] = '<a href="#" class="eve_map_link" onclick="CCPEVE.showMap($1); return false">$2</a>';
  676. $replace[] = '<a href="#" class="eve_chat_link" onclick="CCPEVE.joinChannel(\'$1\'); return false">$2</a>';
  677. $replace[] = '<a href="#" class="eve_fitting_link" onclick="CCPEVE.showFitting(\'$1\'); return false">$2</a>';
  678. if (($is_signature && $pun_config['p_sig_img_tag'] == '1') || (!$is_signature && $pun_config['p_message_img_tag'] == '1'))
  679. {
  680. $pattern[] = '#\[img\]((ht|f)tps?://)([^\s<"]*?)\[/img\]#e';
  681. $pattern[] = '#\[img=([^\[]*?)\]((ht|f)tps?://)([^\s<"]*?)\[/img\]#e';
  682. if ($is_signature)
  683. {
  684. $replace[] = 'handle_img_tag(\'$1$3\', false)';
  685. $replace[] = 'handle_img_tag(\'$2$4\', false, \'$1\')';
  686. }
  687. else
  688. {
  689. $replace[] = 'handle_img_tag(\'$1$3\', false)';
  690. $replace[] = 'handle_img_tag(\'$2$4\', false, \'$1\')';
  691. }
  692. }
  693. $pattern[] = '#\[url\]([^\[]*?)\[/url\]#e';
  694. $pattern[] = '#\[url=([^\[]+?)\](.*?)\[/url\]#e';
  695. $pattern[] = '#\[email\]([^\[]*?)\[/email\]#';
  696. $pattern[] = '#\[email=([^\[]+?)\](.*?)\[/email\]#';
  697. $replace[] = 'handle_url_tag(\'$1\')';
  698. $replace[] = 'handle_url_tag(\'$1\', \'$2\')';
  699. $replace[] = '<a href="mailto:$1">$1</a>';
  700. $replace[] = '<a href="mailto:$1">$2</a>';
  701. // This thing takes a while! :)
  702. $text = preg_replace($pattern, $replace, $text);
  703. return $text;
  704. }
  705. //
  706. // Make hyperlinks clickable
  707. //
  708. function do_clickable($text)
  709. {
  710. $text = ' '.$text;
  711. $text = ucp_preg_replace('#(?<=[\s\]\)])(<)?(\[)?(\()?([\'"]?)(https?|ftp|news){1}://([\p{L}\p{N}\-]+\.([\p{L}\p{N}\-]+\.)*[\p{L}\p{N}]+(:[0-9]+)?(/[^\s\[]*[^\s.,?!\[;:-])?)\4(?(3)(\)))(?(2)(\]))(?(1)(>))(?![^\s]*\[/(?:url|img)\])#uie', 'stripslashes(\'$1$2$3$4\').handle_url_tag(\'$5://$6\', \'$5://$6\', true).stripslashes(\'$4$10$11$12\')', $text);
  712. $text = ucp_preg_replace('#(?<=[\s\]\)])(<)?(\[)?(\()?([\'"]?)(www|ftp)\.(([\p{L}\p{N}\-]+\.)*[\p{L}\p{N}]+(:[0-9]+)?(/[^\s\[]*[^\s.,?!\[;:-])?)\4(?(3)(\)))(?(2)(\]))(?(1)(>))(?![^\s]*\[/(?:url|img)\])#uie', 'stripslashes(\'$1$2$3$4\').handle_url_tag(\'$5.$6\', \'$5.$6\', true).stripslashes(\'$4$10$11$12\')', $text);
  713. return substr($text, 1);
  714. }
  715. //
  716. // Convert a series of smilies to images
  717. //
  718. function do_smilies($text)
  719. {
  720. global $pun_config, $smilies;
  721. $text = ' '.$text.' ';
  722. foreach ($smilies as $smiley_text => $smiley_img)
  723. {
  724. if (strpos($text, $smiley_text) !== false)
  725. require PUN_ROOT.'plugins/ezbbc/ezbbc_smilies2.php'; //EZBBC smilies
  726. //$text = ucp_preg_replace('#(?<=[>\s])'.preg_quote($smiley_text, '#').'(?=[^\p{L}\p{N}])#um', '<img src="'.pun_htmlspecialchars(get_base_url(true).'/img/smilies/'.$smiley_img).'" width="15" height="15" alt="'.substr($smiley_img, 0, strrpos($smiley_img, '.')).'" />', $text);
  727. }
  728. return substr($text, 1, -1);
  729. }
  730. //
  731. // Parse message text
  732. //
  733. function parse_message($text, $hide_smilies)
  734. {
  735. global $pun_config, $lang_common, $pun_user;
  736. if ($pun_config['o_censoring'] == '1')
  737. $text = censor_words($text);
  738. // Convert applicable characters to HTML entities
  739. $text = pun_htmlspecialchars($text);
  740. // If the message contains a code tag we have to split it up (text within [code][/code] shouldn't be touched)
  741. if (strpos($text, '[code]') !== false && strpos($text, '[/code]') !== false)
  742. {
  743. list($inside, $outside) = split_text($text, '[code]', '[/code]', $errors);
  744. $text = implode("\1", $outside);
  745. }
  746. if ($pun_config['p_message_bbcode'] == '1' && strpos($text, '[') !== false && strpos($text, ']') !== false)
  747. $text = do_bbcode($text);
  748. if ($pun_config['o_smilies'] == '1' && $pun_user['show_smilies'] == '1' && $hide_smilies == '0')
  749. $text = do_smilies($text);
  750. // Deal with newlines, tabs and multiple spaces
  751. $pattern = array("\n", "\t", ' ', ' ');
  752. $replace = array('<br />', '&#160; &#160; ', '&#160; ', ' &#160;');
  753. $text = str_replace($pattern, $replace, $text);
  754. // If we split up the message before we have to concatenate it together again (code tags)
  755. if (isset($inside))
  756. {
  757. $outside = explode("\1", $text);
  758. $text = '';
  759. $num_tokens = count($outside);
  760. for ($i = 0; $i < $num_tokens; ++$i)
  761. {
  762. $text .= $outside[$i];
  763. if (isset($inside[$i]))
  764. // $text .= '</p><div class="codebox"><pre><code>'.pun_trim($inside[$i], "\n\r").'</code></pre></div><p>';
  765. {
  766. $num_lines = (substr_count($inside[$i], "\n"));
  767. $text .= '</p><div class="codebox"><pre'.(($num_lines > 28) ? ' class="vscroll"' : '').'><code>'.pun_trim($inside[$i], "\n\r").'</code></pre></div><p>';
  768. }
  769. }
  770. }
  771. // Add paragraph tag around post, but make sure there are no empty paragraphs
  772. $text = preg_replace('#<br />\s*?<br />((\s*<br />)*)#i', "</p>$1<p>", $text);
  773. $text = str_replace('<p><br />', '<p>', $text);
  774. $text = str_replace('<p></p>', '', '<p>'.$text.'</p>');
  775. return $text;
  776. }
  777. //
  778. // Parse signature text
  779. //
  780. function parse_signature($text)
  781. {
  782. global $pun_config, $lang_common, $pun_user;
  783. if ($pun_config['o_censoring'] == '1')
  784. $text = censor_words($text);
  785. // Convert applicable characters to HTML entities
  786. $text = pun_htmlspecialchars($text);
  787. if ($pun_config['p_sig_bbcode'] == '1' && strpos($text, '[') !== false && strpos($text, ']') !== false)
  788. $text = do_bbcode($text, true);
  789. if ($pun_config['o_smilies_sig'] == '1' && $pun_user['show_smilies'] == '1')
  790. $text = do_smilies($text);
  791. // Deal with newlines, tabs and multiple spaces
  792. $pattern = array("\n", "\t", ' ', ' ');
  793. $replace = array('<br />', '&#160; &#160; ', '&#160; ', ' &#160;');
  794. $text = str_replace($pattern, $replace, $text);
  795. // Add paragraph tag around post, but make sure there are no empty paragraphs
  796. $text = preg_replace('#<br />\s*?<br />((\s*<br />)*)#i', "</p>$1<p>", $text);
  797. $text = str_replace('<p><br />', '<p>', $text);
  798. $text = str_replace('<p></p>', '', '<p>'.$text.'</p>');
  799. return $text;
  800. }