Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. import('content/parser/content_parser');
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. class BBCodeParser extends ContentParser
  39. {
  40.  
  41.  
  42.  
  43. function BBCodeParser()
  44. {
  45. parent::ContentParser();
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. function parse()
  53. {
  54. global $User;
  55.  
  56.  
  57. if(!in_array('code',$this->forbidden_tags))
  58. {
  59. $this->_pick_up_tag('code','=[A-Za-z0-9#+-]+(?:,[01]){0,2}');
  60. }
  61.  
  62.  
  63. if(!in_array('html',$this->forbidden_tags)&&$User->check_auth($this->html_auth,1))
  64. {
  65. $this->_pick_up_tag('html');
  66. }
  67.  
  68.  
  69. $this->content=' '.$this->content.' ';
  70.  
  71.  
  72. $this->_protect_content();
  73.  
  74.  
  75. $this->_parse_smilies();
  76.  
  77.  
  78. $this->content=nl2br($this->content);
  79.  
  80.  
  81. $this->_parse_simple_tags();
  82.  
  83.  
  84. if(!in_array('table',$this->forbidden_tags)&&strpos($this->content,'[table')!==false)
  85. {
  86. $this->_parse_table();
  87. }
  88.  
  89.  
  90. if(!in_array('list',$this->forbidden_tags)&&strpos($this->content,'[list')!==false)
  91. {
  92. $this->_parse_list();
  93. }
  94.  
  95.  
  96.  
  97. if(!empty($this->array_tags['html']))
  98. {
  99. $this->array_tags['html']=array_map(create_function('$string','return str_replace("[html]", "<!-- START HTML -->\n", str_replace("[/html]", "\n<!-- END HTML -->", $string));'),$this->array_tags['html']);
  100. $this->_reimplant_tag('html');
  101. }
  102.  
  103. parent::parse();
  104.  
  105.  
  106. if(!empty($this->array_tags['code']))
  107. {
  108. $this->array_tags['code']=array_map(create_function('$string','return preg_replace(\'`^\[code(=.+)?\](.+)\[/code\]$`isU\', \'[[CODE$1]]$2[[/CODE]]\', htmlspecialchars($string, ENT_NOQUOTES));'),$this->array_tags['code']);
  109. $this->_reimplant_tag('code');
  110. }
  111. }
  112.  
  113. ## Private ##
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. function _protect_content()
  123. {
  124.  
  125. $this->content=htmlspecialchars($this->content,ENT_NOQUOTES);
  126. $this->content=strip_tags($this->content);
  127.  
  128.  
  129. $this->content=preg_replace('`&amp;((?:#[0-9]{2,5})|(?:[a-z0-9]{2,8}));`i',"&$1;",$this->content);
  130.  
  131.  
  132. $array_str=array(
  133. '€','‚','ƒ','„','…','†','‡','ˆ','‰',
  134. 'Š','‹','Œ','Ž','‘','’','“','”','•',
  135. '–','—','˜','™','š','›','œ','ž','Ÿ'
  136. );
  137.  
  138. $array_str_replace=array(
  139. '&#8364;','&#8218;','&#402;','&#8222;','&#8230;','&#8224;','&#8225;','&#710;','&#8240;',
  140. '&#352;','&#8249;','&#338;','&#381;','&#8216;','&#8217;','&#8220;','&#8221;','&#8226;',
  141. '&#8211;','&#8212;','&#732;','&#8482;','&#353;','&#8250;','&#339;','&#382;','&#376;'
  142. );
  143. $this->content=str_replace($array_str,$array_str_replace,$this->content);
  144. }
  145.  
  146.  
  147.  
  148.  
  149. function _parse_smilies()
  150. {
  151. @include(PATH_TO_ROOT.'/cache/smileys.php');
  152. if(!empty($_array_smiley_code))
  153. {
  154.  
  155. foreach($_array_smiley_code as $code=>$img)
  156. {
  157. $smiley_code[]='`(?:(?![a-z0-9]))(?<!&[a-z]{4}|&[a-z]{5}|&[a-z]{6}|")('.preg_quote($code).')(?:(?![a-z0-9]))`';
  158. $smiley_img_url[]='<img src="/images/smileys/'.$img.'" alt="'.addslashes($code).'" class="smiley" />';
  159. }
  160. $this->content=preg_replace($smiley_code,$smiley_img_url,$this->content);
  161. }
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. function _parse_simple_tags()
  170. {
  171. global $LANG;
  172. import('util/url');
  173. $array_preg=array(
  174. 'b'=>'`\[b\](.+)\[/b\]`isU',
  175. 'i'=>'`\[i\](.+)\[/i\]`isU',
  176. 'u'=>'`\[u\](.+)\[/u\]`isU',
  177. 's'=>'`\[s\](.+)\[/s\]`isU',
  178. 'sup'=>'`\[sup\](.+)\[/sup\]`isU',
  179. 'sub'=>'`\[sub\](.+)\[/sub\]`isU',
  180. 'img'=>'`\[img(?:=(top|middle|bottom))?\]((?:[./]+|(?:https?|ftps?)://(?:[a-z0-9-]+\.)*[a-z0-9-]+(?:\.[a-z]{2,4})?(?::[0-9]{1,5})?/?)[^,\n\r\t\f]+\.(jpg|jpeg|bmp|gif|png|tiff|svg))\[/img\]`iU',
  181. 'color'=>'`\[color=((?:white|black|red|green|blue|yellow|purple|orange|maroon|pink)|(?:#[0-9a-f]{6}))\](.+)\[/color\]`isU',
  182. 'bgcolor'=>'`\[bgcolor=((?:white|black|red|green|blue|yellow|purple|orange|maroon|pink)|(?:#[0-9a-f]{6}))\](.+)\[/bgcolor\]`isU',
  183. 'size'=>'`\[size=([1-9]|(?:[1-4][0-9]))\](.+)\[/size\]`isU',
  184. 'font'=>'`\[font=(arial|times|courier(?: new)?|impact|geneva|optima)\](.+)\[/font\]`isU',
  185. 'pre'=>'`\[pre\](.+)\[/pre\]`isU',
  186. 'align'=>'`\[align=(left|center|right|justify)\](.+)\[/align\]`isU',
  187. 'float'=>'`\[float=(left|right)\](.+)\[/float\]`isU',
  188. 'anchor'=>'`\[anchor=([a-z_][a-z0-9_-]*)\](.*)\[/anchor\]`isU',
  189. 'acronym'=>'`\[acronym=([^\n[\]<]+)\](.*)\[/acronym\]`isU',
  190. 'style'=>'`\[style=(success|question|notice|warning|error)\](.+)\[/style\]`isU',
  191. 'swf'=>'`\[swf=([0-9]{1,3}),([0-9]{1,3})\](((?:[./]+|(?:https?|ftps?)://([a-z0-9-]+\.)*[a-z0-9-]+\.[a-z]{2,4})+(?:[a-z0-9~_-]+/)*[a-z0-9_+.:?/=#%@&;,-]*))\[/swf\]`iU',
  192. 'movie'=>'`\[movie=([0-9]{1,3}),([0-9]{1,3})\]([a-z0-9_+.:?/=#%@&;,-]*)\[/movie\]`iU',
  193. 'sound'=>'`\[sound\]([a-z0-9_+.:?/=#%@&;,-]*)\[/sound\]`iU',
  194. 'math'=>'`\[math\](.+)\[/math\]`iU',
  195. 'mail'=>'`(?<=\s|^)([a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4})(?=\s|\n|\r|<|$)`iU',
  196. 'mail2'=>'`\[mail=([a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4})\]([^\n\r\t\f]+)\[/mail\]`iU',
  197. 'url1'=>'`\[url\]((?!javascript:)'.Url::get_wellformness_regex().')\[/url\]`isU',
  198. 'url2'=>'`\[url=((?!javascript:)'.Url::get_wellformness_regex().')\]([^\n\r\t\f]+)\[/url\]`isU',
  199. 'url3'=>'`(\s+)('.Url::get_wellformness_regex(REGEX_MULTIPLICITY_REQUIRED).')(\s|<+)`isU',
  200. 'url4'=>'`(\s+)(www\.'.Url::get_wellformness_regex(REGEX_MULTIPLICITY_NOT_USED).')(\s|<+)`isU',
  201.  
  202. 'lightbox1'=>'`\[lightbox\]((?!javascript:)'.Url::get_wellformness_regex().')\[/lightbox\]`isU',
  203. 'lightbox2'=>'`\[lightbox=((?!javascript:)'.Url::get_wellformness_regex().')\]([^\n\r\t\f]+)\[/lightbox\]`isU',
  204. 'lightbox3'=>'`(\s+)('.Url::get_wellformness_regex(REGEX_MULTIPLICITY_REQUIRED).')(\s|<+)`isU',
  205. 'lightbox4'=>'`(\s+)(www\.'.Url::get_wellformness_regex(REGEX_MULTIPLICITY_NOT_USED).')(\s|<+)`isU',
  206. );
  207.  
  208. $array_preg_replace=array(
  209. 'b'=>"<strong>$1</strong>",
  210. 'i'=>"<em>$1</em>",
  211. 'u'=>"<span style=\"text-decoration: underline;\">$1</span>",
  212. 's'=>"<strike>$1</strike>",
  213. 'sup'=>'<sup>$1</sup>',
  214. 'sub'=>'<sub>$1</sub>',
  215. 'img'=>"<img src=\"$2\" alt=\"\" class=\"valign_$1\" />",
  216. 'color'=>"<span style=\"color:$1;\">$2</span>",
  217. 'bgcolor'=>"<span style=\"background-color:$1;\">$2</span>",
  218. 'size'=>"<span style=\"font-size: $1px;\">$2</span>",
  219. 'font'=>"<span style=\"font-family: $1;\">$2</span>",
  220. 'pre'=>"<pre>$1</pre>",
  221. 'align'=>"<p style=\"text-align:$1\">$2</p>",
  222. 'float'=>"<p class=\"float_$1\">$2</p>",
  223. 'anchor'=>"<span id=\"$1\">$2</span>",
  224. 'acronym'=>"<acronym title=\"$1\" class=\"bb_acronym\">$2</acronym>",
  225. 'style'=>"<span class=\"$1\">$2</span>",
  226. 'swf'=>'[[MEDIA]]insertSwfPlayer(\'$3\', $1, $2);[[/MEDIA]]',
  227. 'movie'=>'[[MEDIA]]insertMoviePlayer(\'$3\', $1, $2);[[/MEDIA]]',
  228. 'sound'=>'[[MEDIA]]insertSoundPlayer(\'$1\');[[/MEDIA]]',
  229. 'math'=>'[[MATH]]$1[[/MATH]]',
  230. 'mail'=>"<a href=\"mailto:$1\">$1</a>",
  231. 'mail2'=>"<a href=\"mailto:$1\">$2</a>",
  232. 'url1'=>'<a href="$1">$1</a>',
  233. 'url2'=>'<a href="$1">$2</a>',
  234. 'url3'=>'$1<a href="$2">$2</a>$3',
  235. 'url4'=>'$1<a href="$2">$2</a>$3',
  236. 'lightbox1'=>'<a href="$1" rel=lightbox[roadtrip]>$1</a>',
  237. 'lightbox2'=>'<a href="$1" rel=lightbox[roadtrip]>$2</a>',
  238. 'lightbox3'=>'$1<a href="$2" rel=lightbox[roadtrip]>$2</a>$3',
  239. 'lightbox4'=>'$1<a href="$2" rel=lightbox[roadtrip]>$2</a>$3'
  240. );
  241.  
  242. $parse_line=true;
  243.  
  244.  
  245. if(in_array('lightbox',$this->forbidden_tags))
  246. {
  247. $this->forbidden_tags[]='lightbox1';
  248. $this->forbidden_tags[]='lightbox2';
  249. $this->forbidden_tags[]='lightbox3';
  250. $this->forbidden_tags[]='lightbox4';
  251. }
  252.  
  253. if(in_array('url',$this->forbidden_tags))
  254. {
  255. $this->forbidden_tags[]='url1';
  256. $this->forbidden_tags[]='url2';
  257. $this->forbidden_tags[]='url3';
  258. $this->forbidden_tags[]='url4';
  259. }
  260. if(in_array('mail',$this->forbidden_tags))
  261. {
  262. $this->forbidden_tags[]='mail';
  263. $this->forbidden_tags[]='mail2';
  264. }
  265.  
  266. foreach($this->forbidden_tags as $key=>$tag)
  267. {
  268. if($tag=='line')
  269. {
  270. $parse_line=false;
  271. }
  272. else
  273. {
  274. unset($array_preg[$tag]);
  275. unset($array_preg_replace[$tag]);
  276. }
  277. }
  278. }
  279.  
  280.  
  281. $this->content=preg_replace($array_preg,$array_preg_replace,$this->content);
  282.  
  283.  
  284. if($parse_line)
  285. $this->content=str_replace('[line]','<hr class="bb_hr" />',$this->content);
  286.  
  287.  
  288. if(!in_array('title',$this->forbidden_tags))
  289. {
  290. $this->content=preg_replace_callback('`\[title=([1-4])\](.+)\[/title\]`iU',array(&$this,'_parse_title'),$this->content);
  291. }
  292.  
  293.  
  294. if(!in_array('wikipedia',$this->forbidden_tags))
  295. {
  296. $this->content=preg_replace_callback('`\[wikipedia(?: page="([^"]+)")?(?: lang="([a-z]+)")?\](.+)\[/wikipedia\]`isU',array(&$this,'_parse_wikipedia_links'),$this->content);
  297. }
  298.  
  299. ##Parsage des balises imbriquées.
  300.  
  301. if(!in_array('quote',$this->forbidden_tags))
  302. {
  303. $this->_parse_imbricated('[quote]','`\[quote\](.+)\[/quote\]`sU','<span class="text_blockquote">'.$LANG['quotation'].':</span><div class="blockquote">$1</div>',$this->content);
  304. $this->_parse_imbricated('[quote=','`\[quote=([^\]]+)\](.+)\[/quote\]`sU','<span class="text_blockquote">$1:</span><div class="blockquote">$2</div>',$this->content);
  305. }
  306.  
  307.  
  308. if(!in_array('hide',$this->forbidden_tags))
  309. {
  310. $this->_parse_imbricated('[hide]','`\[hide\](.+)\[/hide\]`sU','<span class="text_hide">'.$LANG['hide'].':</span><div class="hide" onclick="bb_hide(this)"><div class="hide2">$1</div></div>',$this->content);
  311. }
  312.  
  313.  
  314. if(!in_array('indent',$this->forbidden_tags))
  315. {
  316. $this->_parse_imbricated('[indent]','`\[indent\](.+)\[/indent\]`sU','<div class="indent">$1</div>',$this->content);
  317. }
  318.  
  319.  
  320. if(!in_array('block',$this->forbidden_tags))
  321. {
  322. $this->_parse_imbricated('[block]','`\[block\](.+)\[/block\]`sU','<div class="bb_block">$1</div>',$this->content);
  323. $this->_parse_imbricated('[block style=','`\[block style="([^"]+)"\](.+)\[/block\]`sU','<div class="bb_block" style="$1">$2</div>',$this->content);
  324. }
  325.  
  326.  
  327. if(!in_array('fieldset',$this->forbidden_tags))
  328. {
  329. $this->_parse_imbricated('[fieldset','`\[fieldset(?: legend="(.*)")?(?: style="([^"]*)")?\](.+)\[/fieldset\]`sU','<fieldset class="bb_fieldset" style="$2"><legend>$1</legend>$3</fieldset>',$this->content);
  330. }
  331. }
  332.  
  333.  
  334.  
  335.  
  336.  
  337. function _parse_imbricated_table(&$content)
  338. {
  339. if(is_array($content))
  340. {
  341. $string_content='';
  342. $nbr_occur=count($content);
  343. for($i=0;$i<$nbr_occur;$i++)
  344. {
  345.  
  346. if($i%3===2)
  347. {
  348.  
  349. $this->_parse_imbricated_table($content[$i]);
  350.  
  351. if(preg_match('`^(?:\s|<br />)*\[row(?: style="[^"]+")?\](?:\s|<br />)*\[(?:col|head)(?: colspan="[0-9]+")?(?: rowspan="[0-9]+")?(?: style="[^"]+")?\].*\[/(?:col|head)\](?:\s|<br />)*\[/row\](?:\s|<br />)*$`sU',$content[$i]))
  352. {
  353.  
  354. $content[$i]=preg_replace_callback('`^(\s|<br />)+\[row.*\]`U',array(&$this,'clear_html_br'),$content[$i]);
  355. $content[$i]=preg_replace_callback('`\[/row\](\s|<br />)+$`U',array(&$this,'clear_html_br'),$content[$i]);
  356. $content[$i]=preg_replace_callback('`\[/row\](\s|<br />)+\[row.*\]`U',array(&$this,'clear_html_br'),$content[$i]);
  357. $content[$i]=preg_replace_callback('`\[row\](\s|<br />)+\[col.*\]`Us',array(&$this,'clear_html_br'),$content[$i]);
  358. $content[$i]=preg_replace_callback('`\[row\](\s|<br />)+\[head[^]]*\]`U',array(&$this,'clear_html_br'),$content[$i]);
  359. $content[$i]=preg_replace_callback('`\[/col\](\s|<br />)+\[col.*\]`Us',array(&$this,'clear_html_br'),$content[$i]);
  360. $content[$i]=preg_replace_callback('`\[/col\](\s|<br />)+\[head[^]]*\]`U',array(&$this,'clear_html_br'),$content[$i]);
  361. $content[$i]=preg_replace_callback('`\[/head\](\s|<br />)+\[col.*\]`Us',array(&$this,'clear_html_br'),$content[$i]);
  362. $content[$i]=preg_replace_callback('`\[/head\](\s|<br />)+\[head[^]]*\]`U',array(&$this,'clear_html_br'),$content[$i]);
  363. $content[$i]=preg_replace_callback('`\[/head\](\s|<br />)+\[/row\]`U',array(&$this,'clear_html_br'),$content[$i]);
  364. $content[$i]=preg_replace_callback('`\[/col\](\s|<br />)+\[/row\]`U',array(&$this,'clear_html_br'),$content[$i]);
  365.  
  366. $content[$i]=preg_replace('`\[row( style="[^"]+")?\](.*)\[/row\]`sU','<tr class="bb_table_row"$1>$2</tr>',$content[$i]);
  367. $content[$i]=preg_replace('`\[col((?: colspan="[0-9]+")?(?: rowspan="[0-9]+")?(?: style="[^"]+")?)?\](.*)\[/col\]`sU','<td class="bb_table_col"$1>$2</td>',$content[$i]);
  368. $content[$i]=preg_replace('`\[head((?: colspan="[0-9]+")?(?: style="[^"]+")?)?\](.*)\[/head\]`sU','<th class="bb_table_head"$1>$2</th>',$content[$i]);
  369.  
  370. $content[$i]='<table class="bb_table"'.$content[$i-1].'>'.$content[$i].'</table>';
  371.  
  372. }
  373. else
  374. {
  375.  
  376. $content[$i]=str_replace(array('[col','[row','[/col','[/row','[head','[/head'),array('[\col','[\row','[\/col','[\/row','[\head','[\/head'),$content[$i]);
  377. $content[$i]='[table'.$content[$i-1].']'.$content[$i].'[/table]';
  378. }
  379. }
  380.  
  381. if($i%3!==1)
  382. $string_content.=$content[$i];
  383. }
  384. $content=$string_content;
  385. }
  386. }
  387.  
  388.  
  389.  
  390.  
  391. function _parse_table()
  392. {
  393.  
  394. $this->_split_imbricated_tag($this->content,'table',' style="[^"]+"');
  395. $this->_parse_imbricated_table($this->content);
  396.  
  397. $this->content=str_replace(array('[\col','[\row','[\/col','[\/row','[\head','[\/head'),array('[col','[row','[/col','[/row','[head','[/head'),$this->content);
  398. }
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405. function _parse_imbricated_list(&$content)
  406. {
  407. if(is_array($content))
  408. {
  409. $string_content='';
  410. $nbr_occur=count($content);
  411. for($i=0;$i<$nbr_occur;$i++)
  412. {
  413.  
  414. if($i%3===2)
  415. {
  416.  
  417. if(is_array($content[$i]))
  418. $this->_parse_imbricated_list($content[$i]);
  419.  
  420. if(strpos($content[$i],'[*]')!==false)
  421. {
  422.  
  423. $content[$i]=preg_replace_callback('`\[\*\]((?:\s|<br />)+)`',array(&$this,'clear_html_br'),$content[$i]);
  424. $content[$i]=preg_replace_callback('`((?:\s|<br />)+)\[\*\]`',array(&$this,'clear_html_br'),$content[$i]);
  425. if(substr($content[$i-1],0,8)=='=ordered')
  426. {
  427. $list_tag='ol';
  428. $content[$i-1]=substr($content[$i-1],8);
  429. }
  430. else
  431. {
  432. $list_tag='ul';
  433. }
  434. $content[$i]=preg_replace_callback('`^((?:\s|<br />)*)\[\*\]`U',create_function('$var','return str_replace("<br />", "", str_replace("[*]", "<li class=\"bb_li\">", $var[0]));'),$content[$i]);
  435. $content[$i]='<'.$list_tag.$content[$i-1].' class="bb_'.$list_tag.'">'.str_replace('[*]','</li><li class="bb_li">',$content[$i]).'</li></'.$list_tag.'>';
  436. }
  437. }
  438.  
  439. if($i%3!==1)
  440. $string_content.=$content[$i];
  441. }
  442. $content=$string_content;
  443. }
  444. }
  445.  
  446.  
  447.  
  448.  
  449. function _parse_list()
  450. {
  451.  
  452.  
  453. if(preg_match('`\[list(=(?:un)?ordered)?( style="[^"]+")?\](\s|<br />)*\[\*\].*\[/list\]`s',$this->content))
  454. {
  455. $this->_split_imbricated_tag($this->content,'list','(?:=ordered)?(?: style="[^"]+")?');
  456. $this->_parse_imbricated_list($this->content);
  457. }
  458. }
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465. function _parse_title($matches)
  466. {
  467. $level=(int)$matches[1];
  468. if($level<=2)
  469. return '<h3 class="title'.$level.'">'.$matches[2].'</h3>';
  470. else
  471. return '<br /><h4 class="stitle'.($level-2).'">'.$matches[2].'</h4><br />';
  472. }
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479. function _parse_wikipedia_links($matches)
  480. {
  481. global $LANG;
  482.  
  483.  
  484. $lang=$LANG['wikipedia_subdomain'];
  485. if(!empty($matches[2]))
  486. $lang=$matches[2];
  487.  
  488. $page_url=!empty($matches[1])?$matches[1]:$matches[3];
  489.  
  490. return '<a href="http://'.$lang.'.wikipedia.org/wiki/'.$page_url.'" class="wikipedia_link">'.$matches[3].'</a>';
  491. }
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498. function clear_html_br($matches)
  499. {
  500. return str_replace("<br />","",$matches[0]);
  501. }
  502. }
  503.  
  504. ?>