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

/app/inc/clearbricks/text.wiki2xhtml/class.wiki2xhtml.php

https://bitbucket.org/osku/fumblup
PHP | 1298 lines | 917 code | 178 blank | 203 comment | 208 complexity | b0ffba9e2dbce6f99472f970410fd886 MD5 | raw file
  1. <?php
  2. ###########################################################
  3. # /!\ /!\ /!\ EDIT THIS FILE IN UTF8 /!\ /!\ /!\ #
  4. ###########################################################
  5. # ***** BEGIN LICENSE BLOCK *****
  6. # This file is part of Clearbricks.
  7. # Copyright (c) 2006 Olivier Meunier and contributors. All rights
  8. # reserved.
  9. #
  10. # Clearbricks is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # Clearbricks is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with Clearbricks; if not, write to the Free Software
  22. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. #
  24. # ***** END LICENSE BLOCK *****
  25. #
  26. # Contributor(s):
  27. # Stephanie Booth
  28. # Mathieu Pillard
  29. # Christophe Bonijol
  30. # Jean-Charles Bagneris
  31. # Nicolas Chachereau
  32. # Jérôme Lipowicz
  33. #
  34. # Version : 3.2.6
  35. # Release date : 2008-07-16
  36. # History :
  37. #
  38. # 3.2.6
  39. # => Added ``inline html`` support
  40. #
  41. # 3.2.5
  42. # => Changed longdesc by title in images
  43. #
  44. # 3.2.4
  45. # => Auto links
  46. # => Code cleanup
  47. #
  48. # 3.2.3
  49. # Olivier
  50. # => PHP5 Strict
  51. #
  52. # 3.2.2
  53. # Olivier
  54. # => Changement de la gestion des URL spéciales
  55. #
  56. # 3.2.1
  57. # Olivier
  58. # => Changement syntaxe des macros
  59. #
  60. # 3.2
  61. # Olivier
  62. # => Changement de fonctionnement des macros
  63. # => Passage de fonctions externes pour les macros et les mots wiki
  64. #
  65. # 3.1d
  66. # Jérôme Lipowicz
  67. # => antispam
  68. # Olivier
  69. # => centrage d'image
  70. #
  71. # 3.1c
  72. # Olivier
  73. # => Possibilité d'échaper les | dans les marqueurs avec \
  74. #
  75. # 3.1b
  76. # Nicolas Chachereau
  77. # => Changement de regexp pour la correction syntaxique
  78. #
  79. # 3.1a
  80. # Olivier
  81. # => Bug du Call-time pass-by-reference
  82. #
  83. # 3.1
  84. # Olivier
  85. # => Ajout des macros «««..»»»
  86. # => Ajout des blocs vides øøø
  87. # => Ajout du niveau de titre paramétrable
  88. # => Option de blocage du parseur dans les <pre>
  89. # => Titres au format setext (experimental, désactivé)
  90. #
  91. # 3.0
  92. # Olivier => Récriture du parseur inline, plus d'erreur XHTML
  93. # => Ajout d'une vérification d'intégrité pour les listes
  94. # => Les acronymes sont maintenant dans un fichier texte
  95. # => Ajout d'un tag images ((..)), del --..-- et ins ++..++
  96. # => Plus possible de faire des liens JS [lien|javascript:...]
  97. # => Ajout des notes de bas de page §§...§§
  98. # => Ajout des mots wiki
  99. #
  100. # 2.5
  101. # Olivier => Récriture du code, plus besoin du saut de ligne entre blocs !=
  102. #
  103. # 2.0
  104. # Stephanie => correction des PCRE et ajout de fonctionnalités
  105. # Mathieu => ajout du strip-tags, implementation des options, reconnaissance automatique d'url, etc.
  106. # Olivier => chagement de active_link en active_urls
  107. # => ajout des options pour les blocs
  108. # => intégration de l'aide dans le code, avec les options
  109. # => début de quelque chose pour la reconnaissance auto d'url (avec Mat)
  110. # TODO :
  111. # Mathieu => active_wiki_urls (modifier wikiParseUrl ?)
  112. # => active_auto_urls
  113. #
  114. # * => ajouter des options.
  115. # => trouver un meilleur nom pour active_link ? (pour le jour ou ca sera tellement une usine
  116. # a gaz que on generera des tags <link> :)
  117. #
  118. # Wiki2xhtml
  119. class wiki2xhtml
  120. {
  121. public $__version__ = '3.2.6';
  122. public $T;
  123. public $opt;
  124. public $line;
  125. public $acro_table;
  126. public $foot_notes;
  127. public $macros;
  128. public $functions;
  129. public $tags;
  130. public $open_tags;
  131. public $close_tags;
  132. public $custom_tags = array();
  133. public $all_tags;
  134. public $tag_pattern;
  135. public $escape_table;
  136. public $allowed_inline = array();
  137. function __construct()
  138. {
  139. # Mise en place des options
  140. $this->setOpt('active_title',1); # Activation des titres !!!
  141. $this->setOpt('active_setext_title',0); # Activation des titres setext (EXPERIMENTAL)
  142. $this->setOpt('active_hr',1); # Activation des <hr />
  143. $this->setOpt('active_lists',1); # Activation des listes
  144. $this->setOpt('active_quote',1); # Activation du <blockquote>
  145. $this->setOpt('active_pre',1); # Activation du <pre>
  146. $this->setOpt('active_empty',1); # Activation du bloc vide øøø
  147. $this->setOpt('active_auto_urls',0); # Activation de la reconnaissance d'url
  148. $this->setOpt('active_auto_br',0); # Activation du saut de ligne automatique (dans les paragraphes)
  149. $this->setOpt('active_antispam',1); # Activation de l'antispam pour les emails
  150. $this->setOpt('active_urls',1); # Activation des liens []
  151. $this->setOpt('active_auto_img',1); # Activation des images automatiques dans les liens []
  152. $this->setOpt('active_img',1); # Activation des images (())
  153. $this->setOpt('active_anchor',1); # Activation des ancres ~...~
  154. $this->setOpt('active_em',1); # Activation du <em> ''...''
  155. $this->setOpt('active_strong',1); # Activation du <strong> __...__
  156. $this->setOpt('active_br',1); # Activation du <br /> %%%
  157. $this->setOpt('active_q',1); # Activation du <q> {{...}}
  158. $this->setOpt('active_code',1); # Activation du <code> @@...@@
  159. $this->setOpt('active_acronym',1); # Activation des acronymes
  160. $this->setOpt('active_ins',1); # Activation des ins ++..++
  161. $this->setOpt('active_del',1); # Activation des del --..--
  162. $this->setOpt('active_inline_html',1); # Activation du HTML inline ``...``
  163. $this->setOpt('active_footnotes',1); # Activation des notes de bas de page
  164. $this->setOpt('active_wikiwords',0); # Activation des mots wiki
  165. $this->setOpt('active_macros',1); # Activation des macros /// ///
  166. $this->setOpt('parse_pre',1); # Parser l'intérieur de blocs <pre> ?
  167. $this->setOpt('active_fr_syntax',1); # Corrections syntaxe FR
  168. $this->setOpt('first_title_level',3); # Premier niveau de titre <h..>
  169. $this->setOpt('note_prefix','wiki-footnote');
  170. $this->setOpt('note_str','<div class="footnotes"><h4>Notes</h4>%s</div>');
  171. $this->setOpt('words_pattern',
  172. '((?<![A-Za-z0-9])([A-Z][a-z]+){2,}(?![A-Za-z0-9]))');
  173. $this->setOpt('auto_url_pattern',
  174. '%(?<![\[\|])(http://|https://|ftp://|news:)([^"\s\)!]+)%msu');
  175. $this->setOpt('acronyms_file',dirname(__FILE__).'/acronyms.txt');
  176. $this->acro_table = $this->__getAcronyms();
  177. $this->foot_notes = array();
  178. $this->functions = array();
  179. $this->macros = array();
  180. $this->registerFunction('macro:html',array($this,'__macroHTML'));
  181. }
  182. function setOpt($option, $value)
  183. {
  184. $this->opt[$option] = $value;
  185. }
  186. function setOpts($options)
  187. {
  188. if (!is_array($options)) {
  189. return;
  190. }
  191. foreach ($options as $k => $v) {
  192. $this->opt[$k] = $v;
  193. }
  194. }
  195. function getOpt($option)
  196. {
  197. return (!empty($this->opt[$option])) ? $this->opt[$option] : false;
  198. }
  199. function registerFunction($type,$name)
  200. {
  201. if (is_callable($name)) {
  202. $this->functions[$type] = $name;
  203. }
  204. }
  205. function transform($in)
  206. {
  207. # Initialisation des tags
  208. $this->__initTags();
  209. $this->foot_notes = array();
  210. # Récupération des macros
  211. if ($this->getOpt('active_macros')) {
  212. $in = preg_replace('#^///(.*?)///($|\r)#mse',"\\\$this->__getMacro('\\1')",$in);
  213. }
  214. # Vérification du niveau de titre
  215. if ($this->getOpt('first_title_level') > 4) {
  216. $this->setOpt('first_title_level',4);
  217. }
  218. $res = str_replace("\r", '', $in);
  219. $escape_pattern = array();
  220. # traitement des titres à la setext
  221. if ($this->getOpt('active_setext_title') && $this->getOpt('active_title')) {
  222. $res = preg_replace('/^(.*)\n[=]{5,}$/m','!!!$1',$res);
  223. $res = preg_replace('/^(.*)\n[-]{5,}$/m','!!$1',$res);
  224. }
  225. # Transformation des mots Wiki
  226. if ($this->getOpt('active_wikiwords') && $this->getOpt('words_pattern')) {
  227. $res = preg_replace('/'.$this->getOpt('words_pattern').'/msu','¶¶¶$1¶¶¶',$res);
  228. }
  229. # Transformation des URLs automatiques
  230. if ($this->getOpt('active_auto_urls'))
  231. {
  232. $active_urls = $this->getOpt('active_urls');
  233. $this->setOpt('active_urls',1);
  234. $this->__initTags();
  235. # If urls are not active, escape URLs tags
  236. if (!$active_urls)
  237. {
  238. $res = preg_replace(
  239. '%(?<!\\\\)(['.preg_quote(implode('',$this->tags['a'])).'])%msU',
  240. '\\\$1',$res
  241. );
  242. }
  243. # Transforms urls
  244. $res = preg_replace($this->getOpt('auto_url_pattern'),'[$1$2]',$res);
  245. }
  246. $this->T = explode("\n",$res);
  247. $this->T[] = '';
  248. # Parse les blocs
  249. $res = $this->__parseBlocks();
  250. # Line break
  251. if ($this->getOpt('active_br')) {
  252. $res = preg_replace('/(?<!\\\)%%%/', '<br />', $res);
  253. $escape_pattern[] = '%%%';
  254. }
  255. # Nettoyage des \s en trop
  256. $res = preg_replace('/([\s]+)(<\/p>|<\/li>|<\/pre>)/', '$2', $res);
  257. $res = preg_replace('/(<li>)([\s]+)/', '$1', $res);
  258. # On vire les escapes
  259. if (!empty($escape_pattern)) {
  260. $res = preg_replace('/\\\('.implode('|',$escape_pattern).')/','$1',$res);
  261. }
  262. # On vire les MotWiki qui sont resté (dans les url...)
  263. if ($this->getOpt('active_wikiwords') && $this->getOpt('words_pattern')) {
  264. $res = preg_replace('/¶¶¶'.$this->getOpt('words_pattern').'¶¶¶/msu','$1',$res);
  265. }
  266. # On remet les macros
  267. if ($this->getOpt('active_macros')) {
  268. $res = preg_replace('/^##########MACRO#([0-9]+)#$/mse','\$this->__putMacro("$1")',$res);
  269. }
  270. # Auto line break dans les paragraphes
  271. if ($this->getOpt('active_auto_br')) {
  272. $res = preg_replace_callback('%(<p>)(.*?)(</p>)%msu',array($this,'__autoBR'),$res);
  273. }
  274. # On ajoute les notes
  275. if (count($this->foot_notes) > 0)
  276. {
  277. $res_notes = '';
  278. $i = 1;
  279. foreach ($this->foot_notes as $k => $v) {
  280. $res_notes .= "\n".'<p>[<a href="#rev-'.$k.'" id="'.$k.'">'.$i.'</a>] '.$v.'</p>';
  281. $i++;
  282. }
  283. $res .= sprintf("\n".$this->getOpt('note_str')."\n",$res_notes);
  284. }
  285. return $res;
  286. }
  287. /* PRIVATE
  288. --------------------------------------------------- */
  289. function __initTags()
  290. {
  291. $tags = array(
  292. 'em' => array("''","''"),
  293. 'strong' => array('__','__'),
  294. 'acronym' => array('??','??'),
  295. 'a' => array('[',']'),
  296. 'img' => array('((','))'),
  297. 'q' => array('{{','}}'),
  298. 'code' => array('@@','@@'),
  299. 'anchor' => array('~','~'),
  300. 'del' => array('--','--'),
  301. 'ins' => array('++','++'),
  302. 'inline' => array('``','``'),
  303. 'note' => array('$$','$$'),
  304. 'word' => array('¶¶¶','¶¶¶')
  305. );
  306. $this->linetags = array(
  307. 'empty' => 'øøø',
  308. 'title' => '([!]{1,4})',
  309. 'hr' => '[-]{4}[- ]',
  310. 'quote' => '(&gt;|;:)',
  311. 'lists' => '([*#]+)',
  312. 'pre' => '[ ]{1}',
  313. );
  314. $this->tags = array_merge($tags,$this->custom_tags);
  315. # Suppression des tags selon les options
  316. if (!$this->getOpt('active_urls')) {
  317. unset($this->tags['a']);
  318. }
  319. if (!$this->getOpt('active_img')) {
  320. unset($this->tags['img']);
  321. }
  322. if (!$this->getOpt('active_anchor')) {
  323. unset($this->tags['anchor']);
  324. }
  325. if (!$this->getOpt('active_em')) {
  326. unset($this->tags['em']);
  327. }
  328. if (!$this->getOpt('active_strong')) {
  329. unset($this->tags['strong']);
  330. }
  331. if (!$this->getOpt('active_q')) {
  332. unset($this->tags['q']);
  333. }
  334. if (!$this->getOpt('active_code')) {
  335. unset($this->tags['code']);
  336. }
  337. if (!$this->getOpt('active_acronym')) {
  338. unset($this->tags['acronym']);
  339. }
  340. if (!$this->getOpt('active_ins')) {
  341. unset($this->tags['ins']);
  342. }
  343. if (!$this->getOpt('active_del')) {
  344. unset($this->tags['del']);
  345. }
  346. if (!$this->getOpt('active_inline_html')) {
  347. unset($this->tags['inline']);
  348. }
  349. if (!$this->getOpt('active_footnotes')) {
  350. unset($this->tags['note']);
  351. }
  352. if (!$this->getOpt('active_wikiwords')) {
  353. unset($this->tags['word']);
  354. }
  355. # Suppression des tags de début de ligne selon les options
  356. if (!$this->getOpt('active_empty')) {
  357. unset($this->linetags['empty']);
  358. }
  359. if (!$this->getOpt('active_title')) {
  360. unset($this->linetags['title']);
  361. }
  362. if (!$this->getOpt('active_hr')) {
  363. unset($this->linetags['hr']);
  364. }
  365. if (!$this->getOpt('active_quote')) {
  366. unset($this->linetags['quote']);
  367. }
  368. if (!$this->getOpt('active_lists')) {
  369. unset($this->linetags['lists']);
  370. }
  371. if (!$this->getOpt('active_pre')) {
  372. unset($this->linetags['pre']);
  373. }
  374. $this->open_tags = $this->__getTags();
  375. $this->close_tags = $this->__getTags(false);
  376. $this->all_tags = $this->__getAllTags();
  377. $this->tag_pattern = $this->__getTagsPattern();
  378. $this->escape_table = $this->all_tags;
  379. array_walk($this->escape_table,create_function('&$a','$a = \'\\\\\'.$a;'));
  380. }
  381. function __getTags($open=true)
  382. {
  383. $res = array();
  384. foreach ($this->tags as $k => $v) {
  385. $res[$k] = ($open) ? $v[0] : $v[1];
  386. }
  387. return $res;
  388. }
  389. function __getAllTags()
  390. {
  391. $res = array();
  392. foreach ($this->tags as $v) {
  393. $res[] = $v[0];
  394. $res[] = $v[1];
  395. }
  396. return array_values(array_unique($res));
  397. }
  398. function __getTagsPattern($escape=false)
  399. {
  400. $res = $this->all_tags;
  401. array_walk($res,create_function('&$a','$a = preg_quote($a,"/");'));
  402. if (!$escape) {
  403. return '/(?<!\\\)('.implode('|',$res).')/';
  404. } else {
  405. return '('.implode('|',$res).')';
  406. }
  407. }
  408. /* Blocs
  409. --------------------------------------------------- */
  410. function __parseBlocks()
  411. {
  412. $mode = $type = null;
  413. $res = '';
  414. $max = count($this->T);
  415. for ($i=0; $i<$max; $i++)
  416. {
  417. $pre_mode = $mode;
  418. $pre_type = $type;
  419. $end = ($i+1 == $max);
  420. $line = $this->__getLine($i,$type,$mode);
  421. if ($type != 'pre' || $this->getOpt('parse_pre')) {
  422. $line = $this->__inlineWalk($line);
  423. }
  424. $res .= $this->__closeLine($type,$mode,$pre_type,$pre_mode);
  425. $res .= $this->__openLine($type,$mode,$pre_type,$pre_mode);
  426. # P dans les blockquotes
  427. if ($type == 'blockquote' && trim($line) == '' && $pre_type == $type) {
  428. $res .= "</p>\n<p>";
  429. }
  430. # Correction de la syntaxe FR dans tous sauf pre et hr
  431. # Sur idée de Christophe Bonijol
  432. # Changement de regex (Nicolas Chachereau)
  433. if ($this->getOpt('active_fr_syntax') && $type != null && $type != 'pre' && $type != 'hr') {
  434. $line = preg_replace('%[ ]+([:?!;\x{00BB}](\s|$))%u','&nbsp;$1',$line);
  435. $line = preg_replace('%(\x{00AB})[ ]+%u','$1&nbsp;',$line);
  436. }
  437. $res .= $line;
  438. }
  439. return trim($res);
  440. }
  441. function __getLine($i,&$type,&$mode)
  442. {
  443. $pre_type = $type;
  444. $pre_mode = $mode;
  445. $type = $mode = null;
  446. if (empty($this->T[$i])) {
  447. return false;
  448. }
  449. $line = htmlspecialchars($this->T[$i],ENT_NOQUOTES);
  450. # Ligne vide
  451. if (empty($line))
  452. {
  453. $type = null;
  454. }
  455. elseif ($this->getOpt('active_empty') && preg_match('/^øøø(.*)$/',$line,$cap))
  456. {
  457. $type = null;
  458. $line = trim($cap[1]);
  459. }
  460. # Titre
  461. elseif ($this->getOpt('active_title') && preg_match('/^([!]{1,4})(.*)$/',$line,$cap))
  462. {
  463. $type = 'title';
  464. $mode = strlen($cap[1]);
  465. $line = trim($cap[2]);
  466. }
  467. # Ligne HR
  468. elseif ($this->getOpt('active_hr') && preg_match('/^[-]{4}[- ]*$/',$line))
  469. {
  470. $type = 'hr';
  471. $line = null;
  472. }
  473. # Blockquote
  474. elseif ($this->getOpt('active_quote') && preg_match('/^(&gt;|;:)(.*)$/',$line,$cap))
  475. {
  476. $type = 'blockquote';
  477. $line = trim($cap[2]);
  478. }
  479. # Liste
  480. elseif ($this->getOpt('active_lists') && preg_match('/^([*#]+)(.*)$/',$line,$cap))
  481. {
  482. $type = 'list';
  483. $mode = $cap[1];
  484. $valid = true;
  485. # Vérification d'intégrité
  486. $dl = ($type != $pre_type) ? 0 : strlen($pre_mode);
  487. $d = strlen($mode);
  488. $delta = $d-$dl;
  489. if ($delta < 0 && strpos($pre_mode,$mode) !== 0) {
  490. $valid = false;
  491. }
  492. if ($delta > 0 && $type == $pre_type && strpos($mode,$pre_mode) !== 0) {
  493. $valid = false;
  494. }
  495. if ($delta == 0 && $mode != $pre_mode) {
  496. $valid = false;
  497. }
  498. if ($delta > 1) {
  499. $valid = false;
  500. }
  501. if (!$valid) {
  502. $type = 'p';
  503. $mode = null;
  504. $line = '<br />'.$line;
  505. } else {
  506. $line = trim($cap[2]);
  507. }
  508. }
  509. # Préformaté
  510. elseif ($this->getOpt('active_pre') && preg_match('/^[ ]{1}(.*)$/',$line,$cap))
  511. {
  512. $type = 'pre';
  513. $line = $cap[1];
  514. }
  515. # Paragraphe
  516. else {
  517. $type = 'p';
  518. if (preg_match('/^\\\(?<line>(?:('.implode('|',$this->linetags).')).*)$/',$line,$cap)) {
  519. $line = $cap['line'];
  520. }
  521. $line = trim($line);
  522. }
  523. return $line;
  524. }
  525. function __openLine($type,$mode,$pre_type,$pre_mode)
  526. {
  527. $open = ($type != $pre_type);
  528. if ($open && $type == 'p')
  529. {
  530. return "\n<p>";
  531. }
  532. elseif ($open && $type == 'blockquote')
  533. {
  534. return "\n<blockquote><p>";
  535. }
  536. elseif (($open || $mode != $pre_mode) && $type == 'title')
  537. {
  538. $fl = $this->getOpt('first_title_level');
  539. $fl = $fl+3;
  540. $l = $fl-$mode;
  541. return "\n<h".($l).'>';
  542. }
  543. elseif ($open && $type == 'pre')
  544. {
  545. return "\n<pre>";
  546. }
  547. elseif ($open && $type == 'hr')
  548. {
  549. return "\n<hr />";
  550. }
  551. elseif ($type == 'list')
  552. {
  553. $dl = ($open) ? 0 : strlen($pre_mode);
  554. $d = strlen($mode);
  555. $delta = $d-$dl;
  556. $res = '';
  557. if($delta > 0) {
  558. if(substr($mode, -1, 1) == '*') {
  559. $res .= "<ul>\n";
  560. } else {
  561. $res .= "<ol>\n";
  562. }
  563. } elseif ($delta < 0) {
  564. $res .= "</li>\n";
  565. for($j = 0; $j < abs($delta); $j++) {
  566. if (substr($pre_mode,(0 - $j - 1), 1) == '*') {
  567. $res .= "</ul>\n</li>\n";
  568. } else {
  569. $res .= "</ol>\n</li>\n";
  570. }
  571. }
  572. } else {
  573. $res .= "</li>\n";
  574. }
  575. return $res."<li>";
  576. }
  577. else
  578. {
  579. return null;
  580. }
  581. }
  582. function __closeLine($type,$mode,$pre_type,$pre_mode)
  583. {
  584. $close = ($type != $pre_type);
  585. if ($close && $pre_type == 'p')
  586. {
  587. return "</p>\n";
  588. }
  589. elseif ($close && $pre_type == 'blockquote')
  590. {
  591. return "</p></blockquote>\n";
  592. }
  593. elseif (($close || $mode != $pre_mode) && $pre_type == 'title')
  594. {
  595. $fl = $this->getOpt('first_title_level');
  596. $fl = $fl+3;
  597. $l = $fl-$pre_mode;
  598. return '</h'.($l).">\n";
  599. }
  600. elseif ($close && $pre_type == 'pre')
  601. {
  602. return "</pre>\n";
  603. }
  604. elseif ($close && $pre_type == 'list')
  605. {
  606. $res = '';
  607. for($j = 0; $j < strlen($pre_mode); $j++) {
  608. if(substr($pre_mode,(0 - $j - 1), 1) == '*') {
  609. $res .= "</li>\n</ul>";
  610. } else {
  611. $res .= "</li>\n</ol>";
  612. }
  613. }
  614. return $res;
  615. }
  616. else
  617. {
  618. return "\n";
  619. }
  620. }
  621. /* Inline
  622. --------------------------------------------------- */
  623. function __inlineWalk($str,$allow_only=null)
  624. {
  625. $tree = preg_split($this->tag_pattern,$str,-1,PREG_SPLIT_DELIM_CAPTURE);
  626. $res = '';
  627. for ($i=0; $i<count($tree); $i++)
  628. {
  629. $attr = '';
  630. if (in_array($tree[$i],array_values($this->open_tags)) &&
  631. ($allow_only == null || in_array(array_search($tree[$i],$this->open_tags),$allow_only)))
  632. {
  633. $tag = array_search($tree[$i],$this->open_tags);
  634. $tag_type = 'open';
  635. if (($tidy = $this->__makeTag($tree,$tag,$i,$i,$attr,$tag_type)) !== false)
  636. {
  637. if ($tag != '') {
  638. $res .= '<'.$tag.$attr;
  639. $res .= ($tag_type == 'open') ? '>' : ' />';
  640. }
  641. $res .= $tidy;
  642. }
  643. else
  644. {
  645. $res .= $tree[$i];
  646. }
  647. }
  648. else
  649. {
  650. $res .= $tree[$i];
  651. }
  652. }
  653. # Suppression des echappements
  654. $res = str_replace($this->escape_table,$this->all_tags,$res);
  655. return $res;
  656. }
  657. function __makeTag(&$tree,&$tag,$position,&$j,&$attr,&$type)
  658. {
  659. $res = '';
  660. $closed = false;
  661. $itag = $this->close_tags[$tag];
  662. # Recherche fermeture
  663. for ($i=$position+1;$i<count($tree);$i++)
  664. {
  665. if ($tree[$i] == $itag)
  666. {
  667. $closed = true;
  668. break;
  669. }
  670. }
  671. # Résultat
  672. if ($closed)
  673. {
  674. for ($i=$position+1;$i<count($tree);$i++)
  675. {
  676. if ($tree[$i] != $itag)
  677. {
  678. $res .= $tree[$i];
  679. }
  680. else
  681. {
  682. switch ($tag)
  683. {
  684. case 'a':
  685. $res = $this->__parseLink($res,$tag,$attr,$type);
  686. break;
  687. case 'img':
  688. $type = 'close';
  689. $res = $this->__parseImg($res,$attr);
  690. break;
  691. case 'acronym':
  692. $res = $this->__parseAcronym($res,$attr);
  693. break;
  694. case 'q':
  695. $res = $this->__parseQ($res,$attr);
  696. break;
  697. case 'anchor':
  698. $tag = 'a';
  699. $res = $this->__parseAnchor($res,$attr);
  700. break;
  701. case 'note':
  702. $tag = '';
  703. $res = $this->__parseNote($res);
  704. break;
  705. case 'inline':
  706. $tag = '';
  707. $res = $this->__parseInlineHTML($res);
  708. break;
  709. case 'word':
  710. $res = $this->parseWikiWord($res,$tag,$attr,$type);
  711. break;
  712. default :
  713. $res = $this->__inlineWalk($res);
  714. break;
  715. }
  716. if ($type == 'open' && $tag != '') {
  717. $res .= '</'.$tag.'>';
  718. }
  719. $j = $i;
  720. break;
  721. }
  722. }
  723. return $res;
  724. }
  725. else
  726. {
  727. return false;
  728. }
  729. }
  730. function __splitTagsAttr($str)
  731. {
  732. $res = preg_split('/(?<!\\\)\|/',$str);
  733. foreach ($res as $k => $v) {
  734. $res[$k] = str_replace("\|",'|',$v);
  735. }
  736. return $res;
  737. }
  738. # Antispam (Jérôme Lipowicz)
  739. function __antiSpam($str)
  740. {
  741. $encoded = bin2hex($str);
  742. $encoded = chunk_split($encoded, 2, '%');
  743. $encoded = '%'.substr($encoded, 0, strlen($encoded) - 1);
  744. return $encoded;
  745. }
  746. function __parseLink($str,&$tag,&$attr,&$type)
  747. {
  748. $n_str = $this->__inlineWalk($str,array('acronym','img'));
  749. $data = $this->__splitTagsAttr($n_str);
  750. $no_image = false;
  751. # Only URL in data
  752. if (count($data) == 1)
  753. {
  754. $url = trim($str);
  755. $content = strlen($url) > 35 ? substr($url,0,35).'...' : $url;
  756. $lang = '';
  757. $title = $url;
  758. }
  759. elseif (count($data) > 1)
  760. {
  761. $url = trim($data[1]);
  762. $content = $data[0];
  763. $lang = (!empty($data[2])) ? $this->protectAttr($data[2],true) : '';
  764. $title = (!empty($data[3])) ? $data[3] : '';
  765. $no_image = (!empty($data[4])) ? (boolean) $data[4] : false;
  766. }
  767. # Remplacement si URL spéciale
  768. $this->__specialUrls($url,$content,$lang,$title);
  769. # On vire les &nbsp; dans l'url
  770. $url = str_replace('&nbsp;',' ',$url);
  771. if (preg_match('/^(.+)[.](gif|jpg|jpeg|png)$/', $url) && !$no_image && $this->getOpt('active_auto_img'))
  772. {
  773. # On ajoute les dimensions de l'image si locale
  774. # Idée de Stephanie
  775. $img_size = null;
  776. if (!preg_match('#[a-zA-Z]+://#', $url)) {
  777. if (preg_match('#^/#',$url)) {
  778. $path_img = $_SERVER['DOCUMENT_ROOT'] . $url;
  779. } else {
  780. $path_img = $url;
  781. }
  782. $img_size = @getimagesize($path_img);
  783. }
  784. $attr = ' src="'.$this->protectAttr($this->protectUrls($url)).'"'.
  785. $attr .= (count($data) > 1) ? ' alt="'.$this->protectAttr($content).'"' : ' alt=""';
  786. $attr .= ($lang) ? ' lang="'.$lang.'"' : '';
  787. $attr .= ($title) ? ' title="'.$this->protectAttr($title).'"' : '';
  788. $attr .= (is_array($img_size)) ? ' '.$img_size[3] : '';
  789. $tag = 'img';
  790. $type = 'close';
  791. return null;
  792. }
  793. else
  794. {
  795. if ($this->getOpt('active_antispam') && preg_match('/^mailto:/',$url)) {
  796. $content = $content == $url ? preg_replace('%^mailto:%','',$content) : $content;
  797. $url = 'mailto:'.$this->__antiSpam(substr($url,7));
  798. }
  799. $attr = ' href="'.$this->protectAttr($this->protectUrls($url)).'"';
  800. $attr .= ($lang) ? ' hreflang="'.$lang.'"' : '';
  801. $attr .= ($title) ? ' title="'.$this->protectAttr($title).'"' : '';
  802. return $content;
  803. }
  804. }
  805. function __specialUrls(&$url,&$content,&$lang,&$title)
  806. {
  807. foreach ($this->functions as $k => $v)
  808. {
  809. if (strpos($k,'url:') === 0 && strpos($url,substr($k,4)) === 0)
  810. {
  811. $res = call_user_func($v,$url,$content);
  812. $url = isset($res['url']) ? $res['url'] : $url;
  813. $content = isset($res['content']) ? $res['content'] : $content;
  814. $lang = isset($res['lang']) ? $res['lang'] : $lang;
  815. $title = isset($res['title']) ? $res['title'] : $title;
  816. break;
  817. }
  818. }
  819. }
  820. function __parseImg($str,&$attr)
  821. {
  822. $data = $this->__splitTagsAttr($str);
  823. $alt = '';
  824. $url = $data[0];
  825. if (!empty($data[1])) {
  826. $alt = $data[1];
  827. }
  828. $attr = ' src="'.$this->protectAttr($this->protectUrls($url)).'"';
  829. $attr .= ' alt="'.$this->protectAttr($alt).'"';
  830. if (!empty($data[2])) {
  831. $data[2] = strtoupper($data[2]);
  832. if ($data[2] == 'G' || $data[2] == 'L') {
  833. $attr .= ' style="float:left; margin: 0 1em 1em 0;"';
  834. } elseif ($data[2] == 'D' || $data[2] == 'R') {
  835. $attr .= ' style="float:right; margin: 0 0 1em 1em;"';
  836. } elseif ($data[2] == 'C') {
  837. $attr .= ' style="display:block; margin:0 auto;"';
  838. }
  839. }
  840. if (!empty($data[3])) {
  841. $attr .= ' title="'.$this->protectAttr($data[3]).'"';
  842. }
  843. return null;
  844. }
  845. function __parseQ($str,&$attr)
  846. {
  847. $str = $this->__inlineWalk($str);
  848. $data = $this->__splitTagsAttr($str);
  849. $content = $data[0];
  850. $lang = (!empty($data[1])) ? $this->protectAttr($data[1],true) : '';
  851. $attr .= (!empty($lang)) ? ' lang="'.$lang.'"' : '';
  852. $attr .= (!empty($data[2])) ? ' cite="'.$this->protectAttr($this->protectUrls($data[2])).'"' : '';
  853. return $content;
  854. }
  855. function __parseAnchor($str,&$attr)
  856. {
  857. $name = $this->protectAttr($str,true);
  858. if ($name != '') {
  859. $attr = ' name="'.$name.'"';
  860. }
  861. return null;
  862. }
  863. function __parseNote($str)
  864. {
  865. $i = count($this->foot_notes)+1;
  866. $id = $this->getOpt('note_prefix').'-'.$i;
  867. $this->foot_notes[$id] = $this->__inlineWalk($str);
  868. return '<sup>\[<a href="#'.$id.'" id="rev-'.$id.'">'.$i.'</a>\]</sup>';
  869. }
  870. function __parseInlineHTML($str)
  871. {
  872. return str_replace(array('&gt;','&lt;'),array('>','<'),$str);
  873. }
  874. # Obtenir un acronyme
  875. function __parseAcronym($str,&$attr)
  876. {
  877. $data = $this->__splitTagsAttr($str);
  878. $acronym = $data[0];
  879. $title = $lang = '';
  880. if (count($data) > 1)
  881. {
  882. $title = $data[1];
  883. $lang = (!empty($data[2])) ? $this->protectAttr($data[2],true) : '';
  884. }
  885. if ($title == '' && !empty($this->acro_table[$acronym])) {
  886. $title = $this->acro_table[$acronym];
  887. }
  888. $attr = ($title) ? ' title="'.$this->protectAttr($title).'"' : '';
  889. $attr .= ($lang) ? ' lang="'.$lang.'"' : '';
  890. return $acronym;
  891. }
  892. # Définition des acronymes, dans le fichier acronyms.txt
  893. function __getAcronyms()
  894. {
  895. $file = $this->getOpt('acronyms_file');
  896. $res = array();
  897. if (file_exists($file))
  898. {
  899. if (($fc = @file($file)) !== false)
  900. {
  901. foreach ($fc as $v)
  902. {
  903. $v = trim($v);
  904. if ($v != '')
  905. {
  906. $p = strpos($v,':');
  907. $K = (string) trim(substr($v,0,$p));
  908. $V = (string) trim(substr($v,($p+1)));
  909. if ($K) {
  910. $res[$K] = $V;
  911. }
  912. }
  913. }
  914. }
  915. }
  916. return $res;
  917. }
  918. # Mots wiki (pour héritage)
  919. function parseWikiWord($str,&$tag,&$attr,&$type)
  920. {
  921. $tag = $attr = '';
  922. if (isset($this->functions['wikiword'])) {
  923. return call_user_func($this->functions['wikiword'],$str);
  924. }
  925. return $str;
  926. }
  927. /* Protection des attributs */
  928. function protectAttr($str,$name=false)
  929. {
  930. if ($name && !preg_match('/^[A-Za-z][A-Za-z0-9_:.-]*$/',$str)) {
  931. return '';
  932. }
  933. return str_replace(array("'",'"'),array('&#039;','&quot;'),$str);
  934. }
  935. /* Protection des urls */
  936. function protectUrls($str)
  937. {
  938. if (preg_match('/^javascript:/',$str)) {
  939. $str = '#';
  940. }
  941. return $str;
  942. }
  943. /* Auto BR */
  944. function __autoBR($m)
  945. {
  946. return $m[1].str_replace("\n","<br />\n",$m[2]).$m[3];
  947. }
  948. /* Macro
  949. --------------------------------------------------- */
  950. function __getMacro($s)
  951. {
  952. $this->macros[] = str_replace('\"','"',$s);
  953. return 'øøø##########MACRO#'.(count($this->macros)-1).'#';
  954. }
  955. function __putMacro($id)
  956. {
  957. $id = (integer) $id;
  958. if (isset($this->macros[$id]))
  959. {
  960. $content = str_replace("\r",'',$this->macros[$id]);
  961. $c = explode("\n",$content);
  962. # première ligne, premier mot
  963. $fl = trim($c[0]);
  964. $fw = $fl;
  965. if ($fl) {
  966. if (strpos($fl,' ') !== false) {
  967. $fw = substr($fl,0,strpos($fl,' '));
  968. }
  969. $content = implode("\n",array_slice($c,1));
  970. }
  971. if ($c[0] == "\n") {
  972. $content = implode("\n",array_slice($c,1));
  973. }
  974. if ($fw)
  975. {
  976. if (isset($this->functions['macro:'.$fw]))
  977. {
  978. return call_user_func($this->functions['macro:'.$fw],$content,$fl);
  979. }
  980. }
  981. # Si on n'a rien pu faire, on retourne le tout sous
  982. # forme de <pre>
  983. return '<pre>'.htmlspecialchars($this->macros[$id]).'</pre>';
  984. }
  985. return null;
  986. }
  987. function __macroHTML($s)
  988. {
  989. return $s;
  990. }
  991. /* Aide et debug
  992. --------------------------------------------------- */
  993. function help()
  994. {
  995. $help['b'] = array();
  996. $help['i'] = array();
  997. $help['b'][] = 'Laisser une ligne vide entre chaque bloc <em>de même nature</em>.';
  998. $help['b'][] = '<strong>Paragraphe</strong> : du texte et une ligne vide';
  999. if ($this->getOpt('active_title')) {
  1000. $help['b'][] = '<strong>Titre</strong> : <code>!!!</code>, <code>!!</code>, '.
  1001. '<code>!</code> pour des titres plus ou moins importants';
  1002. }
  1003. if ($this->getOpt('active_hr')) {
  1004. $help['b'][] = '<strong>Trait horizontal</strong> : <code>----</code>';
  1005. }
  1006. if ($this->getOpt('active_lists')) {
  1007. $help['b'][] = '<strong>Liste</strong> : ligne débutant par <code>*</code> ou '.
  1008. '<code>#</code>. Il est possible de mélanger les listes '.
  1009. '(<code>*#*</code>) pour faire des listes de plusieurs niveaux. '.
  1010. 'Respecter le style de chaque niveau';
  1011. }
  1012. if ($this->getOpt('active_pre')) {
  1013. $help['b'][] = '<strong>Texte préformaté</strong> : espace devant chaque ligne de texte';
  1014. }
  1015. if ($this->getOpt('active_quote')) {
  1016. $help['b'][] = '<strong>Bloc de citation</strong> : <code>&gt;</code> ou '.
  1017. '<code>;:</code> devant chaque ligne de texte';
  1018. }
  1019. if ($this->getOpt('active_fr_syntax')) {
  1020. $help['i'][] = 'La correction de ponctuation est active. Un espace '.
  1021. 'insécable remplacera automatiquement tout espace '.
  1022. 'précédant les marques ";","?",":" et "!".';
  1023. }
  1024. if ($this->getOpt('active_em')) {
  1025. $help['i'][] = '<strong>Emphase</strong> : deux apostrophes <code>\'\'texte\'\'</code>';
  1026. }
  1027. if ($this->getOpt('active_strong')) {
  1028. $help['i'][] = '<strong>Forte emphase</strong> : deux soulignés <code>__texte__</code>';
  1029. }
  1030. if ($this->getOpt('active_br')) {
  1031. $help['i'][] = '<strong>Retour forcé à la ligne</strong> : <code>%%%</code>';
  1032. }
  1033. if ($this->getOpt('active_ins')) {
  1034. $help['i'][] = '<strong>Insertion</strong> : deux plus <code>++texte++</code>';
  1035. }
  1036. if ($this->getOpt('active_del')) {
  1037. $help['i'][] = '<strong>Suppression</strong> : deux moins <code>--texte--</code>';
  1038. }
  1039. if ($this->getOpt('active_urls')) {
  1040. $help['i'][] = '<strong>Lien</strong> : <code>[url]</code>, <code>[nom|url]</code>, '.
  1041. '<code>[nom|url|langue]</code> ou <code>[nom|url|langue|titre]</code>.';
  1042. $help['i'][] = '<strong>Image</strong> : comme un lien mais avec une extension d\'image.'.
  1043. '<br />Pour désactiver la reconnaissance d\'image mettez 0 dans un dernier '.
  1044. 'argument. Par exemple <code>[image|image.gif||0]</code> fera un lien vers l\'image au '.
  1045. 'lieu de l\'afficher.'.
  1046. '<br />Il est conseillé d\'utiliser la nouvelle syntaxe.';
  1047. }
  1048. if ($this->getOpt('active_img')) {
  1049. $help['i'][] = '<strong>Image</strong> (nouvelle syntaxe) : '.
  1050. '<code>((url|texte alternatif))</code>, '.
  1051. '<code>((url|texte alternatif|position))</code> ou '.
  1052. '<code>((url|texte alternatif|position|description longue))</code>. '.
  1053. '<br />La position peut prendre les valeur L ou G (gauche), R ou D (droite) ou C (centré).';
  1054. }
  1055. if ($this->getOpt('active_anchor')) {
  1056. $help['i'][] = '<strong>Ancre</strong> : <code>~ancre~</code>';
  1057. }
  1058. if ($this->getOpt('active_acronym')) {
  1059. $help['i'][] = '<strong>Acronyme</strong> : <code>??acronyme??</code> ou '.
  1060. '<code>??acronyme|titre??</code>';
  1061. }
  1062. if ($this->getOpt('active_q')) {
  1063. $help['i'][] = '<strong>Citation</strong> : <code>{{citation}}</code>, '.
  1064. '<code>{{citation|langue}}</code> ou <code>{{citation|langue|url}}</code>';
  1065. }
  1066. if ($this->getOpt('active_code')) {
  1067. $help['i'][] = '<strong>Code</strong> : <code>@@code ici@@</code>';
  1068. }
  1069. if ($this->getOpt('active_footnotes')) {
  1070. $help['i'][] = '<strong>Note de bas de page</strong> : <code>$$Corps de la note$$</code>';
  1071. }
  1072. $res = '<dl class="wikiHelp">';
  1073. $res .= '<dt>Blocs</dt><dd>';
  1074. if (count($help['b']) > 0)
  1075. {
  1076. $res .= '<ul><li>';
  1077. $res .= implode('&nbsp;;</li><li>', $help['b']);
  1078. $res .= '.</li></ul>';
  1079. }
  1080. $res .= '</dd>';
  1081. $res .= '<dt>Éléments en ligne</dt><dd>';
  1082. if (count($help['i']) > 0)
  1083. {
  1084. $res .= '<ul><li>';
  1085. $res .= implode('&nbsp;;</li><li>', $help['i']);
  1086. $res .= '.</li></ul>';
  1087. }
  1088. $res .= '</dd>';
  1089. $res .= '</dl>';
  1090. return $res;
  1091. }
  1092. /*
  1093. function debug()
  1094. {
  1095. $mode = $type = null;
  1096. $max = count($this->T);
  1097. $res =
  1098. '<table border="1">'.
  1099. '<tr><th>p-mode</th><th>p-type</th><th>mode</th><th>type</th><th>chaine</th></tr>';
  1100. for ($i=0; $i<$max; $i++)
  1101. {
  1102. $pre_mode = $mode;
  1103. $pre_type = $type;
  1104. $line = $this->__getLine($i,$type,$mode);
  1105. $res .=
  1106. '<tr><td>'.$pre_mode.'</td><td>'.$pre_type.'</td>'.
  1107. '<td>'.$mode.'</td><td>'.$type.'</td><td>'.$line.'</td></tr>';
  1108. }
  1109. $res .= '</table>';
  1110. return $res;
  1111. }
  1112. //*/
  1113. }
  1114. ?>