dokusyntax.inc.php 38.3 KB
Newer Older
Feu's avatar
test  
Feu committed
1 2 3 4 5
<?php

/** @file
 *
 * @brief Classe de traduction dokuwiki -> html
6
 *
Feu's avatar
test  
Feu committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 */

/* Copyright 2007,2008
 *
 * - Simon Lopez <simon POINT lopez AT ayolo POINT org>
 * - Julien Etelain < julien at pmad dot net >
 *
 * Ce fichier fait partie du site de l'Association des Étudiants de
 * l'UTBM, http://ae.utbm.fr.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

Bate's avatar
Bate committed
33
require_once($topdir . "include/cts/planning2.inc.php");
Bate's avatar
Bate committed
34
require_once($topdir . "include/entities/planning2.inc.php");
35
require_once($topdir . "include/taiste.inc.php");
Feu's avatar
test  
Feu committed
36 37 38 39 40 41 42 43 44

class dokusyntax
{

  /**
   * La fonction qui fait tout, tu lui file de la syntaxe wiki
   * elle te pond du xhtml, c'est de la magie en boite de concerve
   * @param $text le texte que tu veux qu'il resorte cossu :p
   */
45
  function doku2xhtml($text,$summury=false,$extern=false)
Feu's avatar
test  
Feu committed
46
  {
Ayolo's avatar
bleh  
Ayolo committed
47
    global $parser,$timing,$conf;
Ayolo's avatar
bleh  
Ayolo committed
48
    $this->uid=gen_uid();
49
    $this->extern=$extern;
Feu's avatar
test  
Feu committed
50 51 52 53
    $timing["doku2xhtml"] -= microtime(true);
    $js = false;
    $table   = array();
    $hltable = array();
54

Feu's avatar
test  
Feu committed
55 56
    //preparse
    $text = $this->preparse($text,$hltable);
57

Feu's avatar
test  
Feu committed
58 59
    //fix : je sais pas comment changer le vin en eau dsl
    $text  = "\n".$text."\n";
60

Feu's avatar
test  
Feu committed
61 62
    // last revision
    //$text = str_replace("«««site-rev»»»", get_rev(), $text);
63

Feu's avatar
test  
Feu committed
64 65 66 67 68 69 70 71 72
    /*les liens (à la base y'en a plein de suportés j'ai fait le ménage
     * ex : telnet, gopher, irc, ...
     */
    $urls = '(https?|file|ftp)';
    $ltrs = '\w';
    $gunk = '/\#~:.?+=&%@!\-';
    $punc = '.:?\-';
    $host = $ltrs.$punc;
    $any  = $ltrs.$gunk.$punc;
73

Feu's avatar
test  
Feu committed
74
    /* first pass */
Ayolo's avatar
bleh  
Ayolo committed
75

Feu's avatar
test  
Feu committed
76 77 78 79 80
    // textes préformatés
    $this->firstpass($table,$text,"#<nowiki>(.*?)</nowiki>#se","\$this->preformat('\\1','nowiki')");
    $this->firstpass($table,$text,"#%%(.*?)%%#se","\$this->preformat('\\1','nowiki')");
    $this->firstpass($table,$text,"#<code( (\w+))?>(.*?)</code>#se","\$this->preformat('\\3','code','\\2')");
    $this->firstpass($table,$text,"#<file>(.*?)</file>#se","\$this->preformat('\\1','file')");
Ayolo's avatar
bleh  
Ayolo committed
81

Feu's avatar
test  
Feu committed
82 83 84
    // je sais pas si ça servira mais bon ...
    $this->firstpass($table,$text,"#<html>(.*?)</html>#se","\$this->preformat('\\1','html')");
    $this->firstpass($table,$text,"#<php>(.*?)</php>#se","\$this->preformat('\\1','php')");
85

Feu's avatar
Feu committed
86
    $this->firstpass($table,$text,"#<file>(.*?)</file>#se","\$this->preformat('\\1','file')");
87 88 89



Feu's avatar
test  
Feu committed
90 91
    // block de code
    $this->firstpass($table,$text,"/(\n( {2,}|\t)[^\*\-\n ][^\n]+)(\n( {2,}|\t)[^\n]*)*/se","\$this->preformat('\\0','block')","\n");
92

Feu's avatar
test  
Feu committed
93 94 95 96 97 98 99 100 101 102 103
    //check if toc is wanted
    if(!isset($parser['toc'])){
      if(strpos($text,'~~NOTOC~~')!== false)
      {
        $text = str_replace('~~NOTOC~~','',$text);
        $parser['toc']  = false;
      }
      else
        $parser['toc']  = true;
    }
    if(!isset($parser['secedit'])) $parser['secedit'] = true;
104 105


Feu's avatar
test  
Feu committed
106 107
    //headlines
    $this->format_headlines($table,$hltable,$text);
Ayolo's avatar
bleh  
Ayolo committed
108

Feu's avatar
test  
Feu committed
109 110
    // links
    $this->firstpass($table,$text,"#\[\[([^\]]+?)\]\]#ie","\$this->linkformat('\\1')");
111

Feu's avatar
test  
Feu committed
112 113
    // media
    $this->firstpass($table,$text,"#\{\{([^\}]+?)\}\}#ie","\$this->mediaformat('\\1')");
114

Ayolo's avatar
bleh  
Ayolo committed
115 116 117
    // flash
    $this->firstpass($table,$text,"#\{\[([^\}]+?)\]\}#ie","\$this->flashformat('\\1')");

Feu's avatar
test  
Feu committed
118 119
    // cherche les url complètes
    $this->firstpass($table,$text,"#(\b)($urls:[$any]+?)([$punc]*[^$any])#ie","\$this->linkformat('\\2')",'\1','\4');
120

Feu's avatar
test  
Feu committed
121
    // url www version courte
Bate's avatar
Bate committed
122
    $this->firstpass($table,$text,"#(\b)(www\.[$host]+?\.[$host]+?[$any]+?)([$punc]*[^$any])#ie","\$this->linkformat('https://\\2')",'\1','\3');
123 124

    // windows shares
Feu's avatar
test  
Feu committed
125
    $this->firstpass($table,$text,"#([$gunk$punc\s])(\\\\\\\\[$host]+?\\\\[$any]+?)([$punc]*[^$any])#ie","\$this->linkformat('\\2')",'\1','\3');
126 127

    // url ftp version courtes
Feu's avatar
test  
Feu committed
128
    $this->firstpass($table,$text,"#(\b)(ftp\.[$host]+?\.[$host]+?[$any]+?)([$punc]*[^$any])#ie","\$this->linkformat('ftp://\\2')",'\1','\3');
129

Feu's avatar
test  
Feu committed
130
    // les n'emails
Ayolo's avatar
mail  
Ayolo committed
131
    $this->firstpass($table,$text,"#([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#ie", "\$this->linkformat('\\1@\\2')");
132

Ayolo's avatar
bleh  
Ayolo committed
133 134
    if(isset($conf["macrofunction"]) && is_array($conf["macrofunction"]) && is_callable($conf["macrofunction"]))
      $this->firstpass($table,$text,"#@@([^@]+)@@#ie","\$this->wikimacro('\\1')");
135

Feu's avatar
test  
Feu committed
136
    $text = htmlspecialchars($text);
137

Bate's avatar
Bate committed
138
    
Feu's avatar
test  
Feu committed
139 140 141 142 143
    //citation
    $text = str_replace( CHR(10), "__slash_n__" , $text );
    while( preg_match("/\[quote=(.*?)\](.*?)\[\/quote\]/i",$text) )
    {
      $text = preg_replace("/\[quote=(.*?)\](.*?)\[\/quote\]/i",
Feu's avatar
Feu committed
144
                           "<div class=\"cita\">
Feu's avatar
test  
Feu committed
145
    <b>Citation de $1 :</b>
Feu's avatar
Feu committed
146
    <div class=\"cits\">$2___</div>___</div>___",
Feu's avatar
test  
Feu committed
147 148 149 150 151
             $text);
    }
    while( preg_match("/\[quote\](.*?)\[\/quote\]/i",$text) )
    {
      $text = preg_replace("/\[quote\](.*?)\[\/quote\]/i",
Feu's avatar
Feu committed
152
                           "<div class=\"cita\">
Feu's avatar
test  
Feu committed
153
    <b>Citation :</b>
Feu's avatar
Feu committed
154
    <div class=\"cits\">$1___</div>___</div>___",
Feu's avatar
test  
Feu committed
155 156 157 158
                          $text);
    }
    $text= str_replace('___</div>___</div>___','</div></div>'.CHR(10),$text);
    $text= str_replace('__slash_n__',CHR(10),$text);
Ayolo's avatar
bleh  
Ayolo committed
159

Ayolo's avatar
bleh  
Ayolo committed
160
    $text=preg_replace("/#\#\#([0-9]+?)\#\#\#/","<div class=\"progressbar\">\n<div class=\"progressbar_prog\" style=\"width:$1%;\"></div>\n<div class=\"progressbar_value\">$1%</div>\n</div>",$text);
161

Ayolo's avatar
bleh  
Ayolo committed
162
    if(isset($conf['bookmarks']) && $conf['bookmarks'])
Ayolo's avatar
bleh  
Ayolo committed
163 164 165
    {
      global $wwwtopdir;
      $anchor=$wwwtopdir."images/forum/anchor.png";
Ayolo's avatar
Ayolo committed
166
      while( preg_match("/&lt;bookmark:(.*?)&gt;/i",$text) )
Ayolo's avatar
bleh  
Ayolo committed
167 168 169 170
        $text=preg_replace("/&lt;bookmark:(\S+)&gt;/i",
                           "<img src=\"$anchor\" alt=\"$1\" title=\"$1\" /><a name=\"$1\"></a>",
                           $text);
    }
Ayolo's avatar
bleh  
Ayolo committed
171

Feu's avatar
test  
Feu committed
172 173
    /* deuxième pass pour les formatages simples */
    $text = $this->simpleformat($text);
174

Bate's avatar
Bate committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    while( preg_match("/\[planning=(.*?)\/(false|true)\/(.*?)\]/i",$text,$matches) )
    {
      $site = $GLOBALS['site'];
      $planningv = new planningv("",$site->db,intval($matches[1]), time(), time()+7*24*3600, $site,$matches[2]==="true",false,intval($matches[3]));
      $text = preg_replace("/\[planning=(.*?)\]/i",
                           $planningv->get_buffer(),
             $text);
    }


    while( preg_match("/\[planning=(.*?)\]/i",$text,$matches) )
    {
      $site = $GLOBALS['site'];
      $planningv = new planningv("",$site->db,intval($matches[1]), time(), time()+7*24*3600, $site);
      $text = preg_replace("/\[planning=(.*?)\]/i",
                           $planningv->get_buffer(),
             $text);
    }


Feu's avatar
test  
Feu committed
195 196 197 198
    /* troisième pass - insert les trucs de la première pass */
    reset($table);
    while (list($key, $val) = each($table))
      $text = str_replace($key,$val,$text);
199

Feu's avatar
test  
Feu committed
200
    $text = trim($text);
201

Feu's avatar
test  
Feu committed
202
    $timing["doku2xhtml"] += microtime(true);
Ayolo's avatar
Ayolo committed
203

Ayolo's avatar
Ayolo committed
204
    $text=str_replace('__dot__','[dot]',str_replace('__at__','[at]',$text));
Ayolo's avatar
Ayolo committed
205

Feu's avatar
test  
Feu committed
206 207
    if ( $summury )
      return array($js.$text,$hltable);
208

Feu's avatar
test  
Feu committed
209 210
    return $js.$text;
  }
Ayolo's avatar
bleh  
Ayolo committed
211

Feu's avatar
test  
Feu committed
212 213 214
  function wikimacro($match)
  {
    global $conf;
215

Feu's avatar
test  
Feu committed
216 217
    if ( !$conf["macrofunction"] || !is_callable($conf["macrofunction"]) )
      return $match;
218

Feu's avatar
test  
Feu committed
219 220
    return call_user_func( $conf["macrofunction"], $match );
  }
221 222


Feu's avatar
test  
Feu committed
223 224 225 226 227 228
  /**
   * On préparse le texte ligne par ligne.
   */
  function preparse($text,&$hltable)
  {
    $lines = split("\n",$text);
229 230


Feu's avatar
test  
Feu committed
231 232 233
    for ($l=0; $l<count($lines); $l++)
    {
      $line = $lines[$l];
234

Feu's avatar
test  
Feu committed
235 236 237 238 239 240 241 242 243 244
      // on cherche la fin des trucs à ne pas parser qui sont sur plusieurs lignes
      if($noparse){
        if(preg_match("#^.*?$noparse#",$line))
        {
          $noparse = '';
          $line = preg_replace("#^.*?$noparse#",$line,1);
        }
        else
          continue;
      }
245

Feu's avatar
test  
Feu committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      if(!$noparse)
      {
        // abat les indentations \o/
        if(preg_match('#^(  |\t)#',$line)) continue;
        // on enlève les blocs à pas parser qui sont inline
        $line = preg_replace("#<nowiki>(.*?)</nowiki>#","",$line);
        $line = preg_replace("#%%(.*?)%%#","",$line);
        $line = preg_replace("#<code>(.*?)</code>#","",$line);
        $line = preg_replace("#<file>(.*?)</file>#","",$line);
        $line = preg_replace("#<html>(.*?)</html>#","",$line);
        $line = preg_replace("#<php>(.*?)</php>#","",$line);
        // on cherche le début des block "noparse" multilignes
        if(preg_match('#^.*?<(nowiki|code|php|html|file)( (\w+))?>#',$line,$matches))
        {
           list($noparse) = split(" ",$matches[1]); //on vire les options
          $noparse = '</'.$noparse.'>';
          continue;
        }
        elseif(preg_match('#^.*?%%#',$line))
        {
          $noparse = '%%';
          continue;
        }
      }
270

Feu's avatar
test  
Feu committed
271 272 273 274 275 276
      //headlines
      if(preg_match('/^(\s)*(==+)(.+?)(==+)(\s*)$/',$lines[$l],$matches))
      {
        $tk = $this->tokenize_headline($hltable,$matches[2],$matches[3],$l);
        $lines[$l] = $tk;
      }
277

Feu's avatar
test  
Feu committed
278
    }
279

Feu's avatar
test  
Feu committed
280 281
    return join("\n",$lines);
  }
282

Feu's avatar
test  
Feu committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  /**
   * Cette fonction ajoute quelques infos à propos du "headline" qui lui est passé
   * comme ça on pourra faire de la marmelade après (un sommaire)
   */
  function tokenize_headline(&$hltable,$pre,$hline,$lno)
  {
    switch (strlen($pre))
    {
      case 2:
        $lvl = 5;
        break;
      case 3:
        $lvl = 4;
        break;
      case 4:
        $lvl = 3;
        break;
      case 5:
        $lvl = 2;
        break;
      default:
        $lvl = 1;
        break;
    }
Feu's avatar
test  
Feu committed
307
    $token = $this->mkToken();
jlaval's avatar
Foo  
jlaval committed
308
    $name = trim($hline);
jlaval's avatar
jlaval committed
309 310 311 312 313
    $collapsed = false;
    // If name starts with a tilde (~) that means we hide it by default
    // while also adding a little expander button to show it
    if (strlen ($name) > 0 && $name[0] == '~') {
        $collapsed = true;
jlaval's avatar
Foo  
jlaval committed
314
        $name = substr ($name, 1);
jlaval's avatar
jlaval committed
315
    }
jlaval's avatar
Foo  
jlaval committed
316
    $hltable[] = array( 'name'  => htmlspecialchars($name),
Feu's avatar
test  
Feu committed
317 318
                        'level' => $lvl,
                        'line'  => $lno,
jlaval's avatar
jlaval committed
319 320
                        'token' => $token,
                        'collapsed' => $collapsed);
Feu's avatar
test  
Feu committed
321 322
    return $token;
  }
323

Feu's avatar
test  
Feu committed
324 325 326 327 328 329
  function format_headlines(&$table,&$hltable,&$text)
  {
    global $parser;
    global $conf;
    global $lang;
    global $ID;
330
    $uid=$this->uid;
Feu's avatar
test  
Feu committed
331
    $lang = 'fr'; // bein quoi ?
jlaval's avatar
jlaval committed
332
    $collapseCurrent = 100;
333

Feu's avatar
test  
Feu committed
334 335 336 337 338 339 340
    $last = 0;
    $cnt  = 0;
    foreach($hltable as $hl)
    {
      $cnt++;
      $headline   = '';
      if($cnt - 1) $headline .= '</div>';
Ayolo's avatar
Ayolo committed
341
      $headline  .= '<a name="h_'.$uid.'_'.($cnt).'"></a>';
Feu's avatar
test  
Feu committed
342
      $headline  .= '<a name="'.preg_replace("/[^a-z0-9\-_:#]/","_",strtolower(utf8_enleve_accents($hl['name']))).'"></a>';
jlaval's avatar
jlaval committed
343
      $headline  .= '<h'.$hl['level'].($hl['level'] > $collapseCurrent ? ' style="display: none;"' : '').'>';
Feu's avatar
test  
Feu committed
344 345
      $headline  .= $hl['name'];
      $headline  .= '</h'.$hl['level'].'>';
jlaval's avatar
jlaval committed
346 347 348 349
      if ($hl['level'] <= $collapseCurrent)
          $collapseCurrent = 100;
      if ($hl['collapsed']) {
          $collapseCurrent = $hl['level'];
jlaval's avatar
jlaval committed
350
          $headline  .= '<a onclick="toggleSectionVisibility(this);">[+]</a>';
jlaval's avatar
jlaval committed
351 352
      }
      $headline  .= '<div class="level'.$hl['level'].'"'.(($hl['collapsed'] || $hl['level'] > $collapseCurrent) ? ' style="display: none;"' : '').'>';
353

Feu's avatar
test  
Feu committed
354 355
      if($hl['level'] <= $conf['maxtoclevel'])
      {
Ayolo's avatar
Ayolo committed
356
        $content[]  = array('id'    => 'h_'.$uid.'_'.$cnt,
Feu's avatar
test  
Feu committed
357 358 359
                            'name'  => $hl['name'],
                            'level' => $hl['level']);
      }
360

Feu's avatar
test  
Feu committed
361 362
      $table[$hl['token']] = $headline;
    }
363

Feu's avatar
test  
Feu committed
364 365 366 367 368 369
    if ($cnt)
    {
      $token = $this->mktoken();
      $text .= $token;
      $table[$token] = '</div>';
    }
370

Feu's avatar
test  
Feu committed
371 372 373 374 375 376 377
    if ($parser['toc'] && count($content) > 2)
    {
      $token = $this->mktoken();
      $text  = $token.$text;
      $table[$token] = $this->html_toc($content);
    }
  }
Ayolo's avatar
bleh  
Ayolo committed
378

Ayolo's avatar
bleh  
Ayolo committed
379 380
  function html_toc($toc)
  {
Ayolo's avatar
bleh  
Ayolo committed
381
    global $topdir;
Ayolo's avatar
bleh  
Ayolo committed
382
    $ret  = "\n";
Ayolo's avatar
bleh  
Ayolo committed
383
    $ret .= '<div class="toc">'."\n";
Ayolo's avatar
bleh  
Ayolo committed
384 385
    $ret .= '<div class="tocheader">'."\n";
    $ret .= '<a href="#" onclick="on_off_icon(\'toc\',\''.$topdir.'\'); ';
Ayolo's avatar
bleh  
Ayolo committed
386
    $ret .= 'return false;"><img src="'.$topdir.'images/fld.png" alt="togle" class="icon" id="toc_icon" />'."\n";
Ayolo's avatar
bleh  
Ayolo committed
387 388 389
    $ret .= 'Table des matières'."\n";
    $ret .= '</div>'."\n";
    $ret .= '<div id="toc_contents">'."\n";
Ayolo's avatar
bleh  
Ayolo committed
390
    $ret .= $this->html_buildlist($toc,'toc','html_list_toc');
Ayolo's avatar
bleh  
Ayolo committed
391 392
    $ret .= '</div>'."\n";
    $ret .= '</div>'."\n";
Ayolo's avatar
bleh  
Ayolo committed
393 394 395 396 397
    return $ret;
  }

  function html_buildlist($data,$class,$func)
  {
Antoine Tenart's avatar
Antoine Tenart committed
398 399
    $level = array();
    $k = 0;
Ayolo's avatar
bleh  
Ayolo committed
400 401
    $opens = 0;
    $ret   = '';
Antoine Tenart's avatar
Antoine Tenart committed
402 403

    $level[0] = 0;
Ayolo's avatar
bleh  
Ayolo committed
404 405
    foreach ($data as $item)
    {
Antoine Tenart's avatar
Antoine Tenart committed
406
      if( $item['level'] > $level[$k] ){
Ayolo's avatar
bleh  
Ayolo committed
407
        $ret .= "\n<ul class=\"$class\">\n";
Antoine Tenart's avatar
Antoine Tenart committed
408 409 410
        $k++;
      }
      elseif( $item['level'] < $level[$k] )
Ayolo's avatar
bleh  
Ayolo committed
411
      {
Antoine Tenart's avatar
bleh  
Antoine Tenart committed
412
        $ret .= "</li>\n";
Antoine Tenart's avatar
Antoine Tenart committed
413
        for (; $level[$k] > $item['level']; $k--)
Ayolo's avatar
bleh  
Ayolo committed
414 415 416 417
          $ret .= "</ul>\n</li>\n";
      }
      else
        $ret .= "</li>\n";
Antoine Tenart's avatar
Antoine Tenart committed
418
      $level[$k] = $item['level'];
Ayolo's avatar
bleh  
Ayolo committed
419 420 421 422 423
      $ret .= '<li class="level'.$item['level'].'">';
      $ret .= '<span class="li">';
      $ret .= $this->$func($item); //user function
      $ret .= '</span>';
    }
Antoine Tenart's avatar
Antoine Tenart committed
424
    for (; $k > 0; $k--)
Ayolo's avatar
bleh  
Ayolo committed
425 426 427 428 429 430 431 432 433 434 435
      $ret .= "</li></ul>\n";
    return $ret;
  }

  function html_list_toc($item)
  {
    $ret = '<a href="#'.$item['id'].'" class="toc">';
    $ret .= $item['name'];
    $ret .= '</a>';
    return $ret;
  }
436

Feu's avatar
test  
Feu committed
437 438 439 440 441 442
  function linkformat($match)
  {
    global $conf;
    global $wwwtopdir,$topdir;
    $ret = '';
    $match = str_replace('\\"','"',$match);
443

BenC's avatar
BenC committed
444
    list($link,$name) = split('\|',$match,2);
Feu's avatar
test  
Feu committed
445 446 447
    $link   = trim($link);
    $name   = trim($name);
    $class  = '';
BenC's avatar
BenC committed
448
    $target = '';
Feu's avatar
test  
Feu committed
449 450 451 452
    $style  = '';
    $pre    = '';
    $post   = '';
    $more   = '';
453

Feu's avatar
test  
Feu committed
454
    $realname = $name;
455

Feu's avatar
test  
Feu committed
456 457 458 459 460 461
    // email
    if(preg_match('#([a-z0-9\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i',$link))
      $this->format_link_email($link,$name,$class,$target,$style,$pre,$post,$more);
    // liens
    else
      $this->format_link($link,$name,$class,$target,$style,$pre,$post,$more);
462 463


Ayolo's avatar
Ayolo committed
464
    if( !strpos($link,'mailto:') && preg_match('/^([a-zA-Z]+):\/\//',$link) ) // liens externe et spéciaux
Feu's avatar
test  
Feu committed
465 466 467 468 469 470 471 472 473 474
    {
      if ( preg_match('/dfile:\/\/([0-9]*)(\/preview|\/info|\/download|\/thumb)?/i',$link,$match) )
      {
        if ( empty($realname) && isset($conf["db"]) )
        {
          require_once($topdir."include/entities/files.inc.php");
          $file = new dfile($conf["db"]);
          $file->load_by_id($match[1]);
          return $file->get_html_link();
        }
Ayolo's avatar
bleh  
Ayolo committed
475
        if ( !isset($match[2]) || $match[2] == "/download" )
476
          $link = "https://ae.utbm.fr". $GLOBALS["wwwtopdir"]."d.php?action=download&amp;id_file=".$match[1];
Ayolo's avatar
bleh  
Ayolo committed
477
        elseif ( $match[2] == "/preview" )
478
          $link = "https://ae.utbm.fr". $GLOBALS["wwwtopdir"]."d.php?action=download&amp;download=preview&amp;id_file=".$match[1];
Ayolo's avatar
bleh  
Ayolo committed
479
        elseif ( $match[2] == "/thumb" )
480
          $link = "https://ae.utbm.fr". $GLOBALS["wwwtopdir"]."d.php?action=download&amp;download=thumb&amp;id_file=".$match[1];
Ayolo's avatar
bleh  
Ayolo committed
481
        elseif ( $match[2] == "/info" )
482
          $link = "https://ae.utbm.fr". $GLOBALS["wwwtopdir"]."d.php?id_file=".$match[1];
Feu's avatar
test  
Feu committed
483 484 485
      }
      else
      {
Ayolo's avatar
bleh  
Ayolo committed
486
        //les article://
487
        $link = preg_replace("/article:\/\//i","https://ae.utbm.fr". $GLOBALS["wwwtopdir"].$GLOBALS["entitiescatalog"]["page"][3]."?name=",$link);
Ayolo's avatar
bleh  
Ayolo committed
488
        //les wiki://
489
        $link = preg_replace("/wiki:\/\//i","https://ae.utbm.fr". $GLOBALS["wwwtopdir"].$GLOBALS["entitiescatalog"]["wiki"][3]."?name=",$link);
Ayolo's avatar
bleh  
Ayolo committed
490
        if( defined('CMS_ID_ASSO') )
Ayolo's avatar
bleh  
Ayolo committed
491
          $link = preg_replace("/sas:\/\//i","images.php?/",$link);
Ayolo's avatar
bleh  
Ayolo committed
492
        else
493
          $link = preg_replace("/sas:\/\//i","https://ae.utbm.fr". $GLOBALS["wwwtopdir"]."sas2/images.php?/",$link);
Feu's avatar
test  
Feu committed
494 495
      }
    }
Ayolo's avatar
Ayolo committed
496
    elseif ( !strpos($link,'mailto:') && !preg_match("#(\.|/)#",$link) )
497
    {
Feu's avatar
test  
Feu committed
498 499 500 501 502 503 504
      $link = preg_replace("/[^a-z0-9\-_:#]/","_",strtolower(utf8_enleve_accents($link)));
      if ( $link{0} != '#' )
      {
        if ( $link{0} == ':' )
          $link = substr($link,1);
        elseif ( !empty($conf["linksscope"]))
          $link = $conf["linksscope"].$link;
505

Feu's avatar
test  
Feu committed
506
        if ( $conf["linkscontext"] == "wiki" )
507
          $link = $wwwtopdir.$GLOBALS["entitiescatalog"]["wiki"][3]."?name=".$link;
Feu's avatar
test  
Feu committed
508 509 510
        else
          $link = $wwwtopdir.$GLOBALS["entitiescatalog"]["page"][3]."?name=".$link;
      }
Ayolo's avatar
Ayolo committed
511
      if(strpos($link,'mailto:'))
Ayolo's avatar
Ayolo committed
512 513
      {
        $pos=strpos($link,'mailto:');
Ayolo's avatar
Ayolo committed
514
        $link=substr($link,$pos);
Ayolo's avatar
Ayolo committed
515
      }
Feu's avatar
test  
Feu committed
516
    }
517 518 519



Feu's avatar
test  
Feu committed
520 521 522 523 524 525 526 527 528 529
    $ret .= $pre;
    $ret .= '<a href="'.$link.'"';
    if($class)  $ret .= ' class="'.$class.'"';
    if($target) $ret .= ' target="'.$target.'"';
    if($style)  $ret .= ' style="'.$style.'"';
    if($more)   $ret .= ' '.$more;
    $ret .= '>';
    $ret .= $name;
    $ret .= '</a>';
    $ret .= $post;
530

Feu's avatar
test  
Feu committed
531 532
    return $ret;
  }
533

Feu's avatar
test  
Feu committed
534 535 536 537 538 539
  /**
   * les trucs simples et pas chiant c'est ici
   */
  function simpleformat($text)
  {
    global $conf;
540

Feu's avatar
test  
Feu committed
541 542
    $text = preg_replace('#&lt;del&gt;(.*?)&lt;/del&gt;#is','<s>\1</s>',$text); //del
    $text = preg_replace('/__([^_]+?)__/s','<u>\1</u>',$text);  //underline
slopez's avatar
bleh  
slopez committed
543
    $text = preg_replace('/\/\/(.*?)\/\//s','<em>\1</em>',$text);  //emphasize
Feu's avatar
test  
Feu committed
544 545 546
    $text = preg_replace('/\*\*([^*]+?)\*\*/s','<strong>\1</strong>',$text);  //bold
    $text = preg_replace('/\'\'([^\']+?)\'\'/s','<code>\1</code>',$text);  //code
    $text = preg_replace('/^(\s)*----+(\s*)$/m','<hr noshade="noshade" size="1" />',$text); //hr
547

Feu's avatar
test  
Feu committed
548 549
    $text = preg_replace('#&lt;sub&gt;(.*?)&lt;/sub&gt;#is','<sub>\1</sub>',$text);
    $text = preg_replace('#&lt;sup&gt;(.*?)&lt;/sup&gt;#is','<sup>\1</sup>',$text);
550

Feu's avatar
Feu committed
551
    $text = preg_replace("/\n((&gt;)[^\n]*?\n)+/se","'\n'.\$this->quoteformat('\\0').'\n'",$text);
552

Feu's avatar
test  
Feu committed
553 554 555 556 557 558 559
    $text = preg_replace('/([^-])--([^-])/s','\1&#8211;\2',$text);
    $text = preg_replace('/([^-])---([^-])/s','\1&#8212;\2',$text);
    $text = preg_replace('/&quot;([^\"]+?)&quot;/s','&#8220;\1&#8221;',$text);
    $text = preg_replace('/(\s)\'(\S)/m','\1&#8216;\2',$text);
    $text = preg_replace('/(\S)\'/','\1&#8217;',$text);
    $text = preg_replace('/\.\.\./','\1&#8230;\2',$text);
    $text = preg_replace('/(\d+)x(\d+)/i','\1&#215;\2',$text);
560

Feu's avatar
test  
Feu committed
561 562
    $text = preg_replace('/&gt;&gt;/i','&raquo;',$text);
    $text = preg_replace('/&lt;&lt;/i','&laquo;',$text);
563

Feu's avatar
test  
Feu committed
564 565 566
    $text = preg_replace('/&lt;-&gt;/i','&#8596;',$text);
    $text = preg_replace('/&lt;-/i','&#8592;',$text);
    $text = preg_replace('/-&gt;/i','&#8594;',$text);
567

Feu's avatar
test  
Feu committed
568 569 570
    $text = preg_replace('/&lt;=&gt;/i','&#8660;',$text);
    $text = preg_replace('/&lt;=/i','&#8656;',$text);
    $text = preg_replace('/=&gt;/i','&#8658;',$text);
571

Feu's avatar
test  
Feu committed
572 573 574
    $text = preg_replace('/\(c\)/i','&copy;',$text);
    $text = preg_replace('/\(r\)/i','&reg;',$text);
    $text = preg_replace('/\(tm\)/i','&trade;',$text);
575

Feu's avatar
test  
Feu committed
576 577
    //retours à la ligne forcés
    $text = preg_replace('#\\\\\\\\(\s)#',"<br />\\1",$text);
578

Feu's avatar
test  
Feu committed
579 580 581 582
    // dos2unix
    $text = str_replace("\r\n","\n",$text);
    $text = str_replace("\n\r","\n",$text);
    $text = str_replace("\r","\n",$text);
583

Feu's avatar
test  
Feu committed
584 585
    // lists (blocks leftover after blockformat)
    $text = preg_replace("/(\n( {2,}|\t)[\*\-][^\n]+)(\n( {2,}|\t)[^\n]*)*/se","\"\\n\".\$this->listformat('\\0')",$text);
586

Feu's avatar
test  
Feu committed
587 588
    // tableaux
    $text = preg_replace("/\n(([\|\^][^\n]*?)+[\|\^]\n)+/se","\"\\n\".\$this->tableformat('\\0')",$text);
589

Feu's avatar
test  
Feu committed
590 591
    //smileys
    $text = $this->smileys($text);
592

Feu's avatar
test  
Feu committed
593 594
    // footnotes
    $text = $this->footnotes($text);
595

Feu's avatar
test  
Feu committed
596 597
    // double sauts de ligne = nouveau paragraphe
    $text = str_replace("\n\n","<p />",$text);
598

Feu's avatar
test  
Feu committed
599 600
    return $text;
  }
601

Feu's avatar
test  
Feu committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  /**
   * les footnotes
   */
  function footnotes($text)
  {
    $num = 0;
    while (preg_match('/\(\((.+?)\)\)/s',$text,$match))
    {
      $num++;
      $fn    = $match[1];
      $linkt = '<a href="#fn'.$num.'" name="fnt'.$num.'" class="fn_top">'.$num.')</a>';
      $linkb = '<a href="#fnt'.$num.'" name="fn'.$num.'" class="fn_bot">'.$num.')</a>';
      $text  = preg_replace('/ ?\(\((.+?)\)\)/s',$linkt,$text,1);
      if($num == 1) $text .= '<div class="footnotes">';
      $text .= '<div class="fn">'.$linkb.' '.$fn.'</div>';
    }
618

Feu's avatar
test  
Feu committed
619 620 621
    if($num) $text .= '</div>';
    return $text;
  }
622

Feu's avatar
test  
Feu committed
623 624 625 626 627 628 629
  /**
   * on remplace les smileys :)
   */
  function smileys($text)
  {
    global $wwwtopdir;
    $smileys = array(
630

Feu's avatar
test  
Feu committed
631 632 633 634 635
      ":-o"=>"omg.png",
      ":-O"=>"omg.png",
      ":o"=>"omg.png",
      ":O"=>"omg.png",
      "8-O"=>"omg.png",
636

Feu's avatar
test  
Feu committed
637
      ":-("=>"sad.png",
638 639
      ":("=>"sad.png",

Feu's avatar
test  
Feu committed
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
      ":-)"=>"smile.png",
      ":)"=>"smile.png",
      ":-/"=>"confused.png",
      ":/"=>"confused.png",
      "^_^"=>"happy.png",
      ";)"=>"wink.png",
      ";-)"=>"wink.png",
      ":-|"=>"neutral.png",
      ":|"=>"neutral.png",
      ":-D"=>"lol.png",
      ":D"=>"lol.png",
      "Oo"=>"dizzy.png",
      "O_o"=>"dizzy.png",
      "O_O"=>"dizzy.png",
      "o_o"=>"dizzy.png",
      "o_O"=>"dizzy.png",
      ":&#8217;("=>"cry.png",
      ";-("=>"cry.png",
      ";("=>"cry.png",
      "x)"=>"caca.png",
660 661
      "x-)"=>"caca.png",
      ":caca:"=>"caca.png",
Feu's avatar
test  
Feu committed
662 663 664
      ":-p"=>"tongue.png",
      ":-P"=>"tongue.png",
      ":p"=>"tongue.png",
Ayolo's avatar
bleh  
Ayolo committed
665
      ":P"=>"tongue.png",
Ayolo's avatar
grrr  
Ayolo committed
666
      ':!:'=>'exclaim.gif',
667 668 669
      ':?:'=>'question.gif',
      'FIXME'=>'fixme.gif',
      'DELETEME'=>'delete.gif'
Feu's avatar
test  
Feu committed
670
                     );
Bate's avatar
Bate committed
671
    $smPath = "images/forum/smilies/";
Feu's avatar
test  
Feu committed
672 673
    foreach($smileys as $tag => $img)
    {
Bate's avatar
Bate committed
674
      if ( file_exists(__DIR__."/../../".$smPath . "/" . $img) )
Feu's avatar
test  
Feu committed
675 676 677 678 679 680 681 682 683 684
      {
        $tag = preg_replace('!\]!i', '\]', $tag);
        $tag = preg_replace('!\[!i', '\[', $tag);
        $tag = preg_replace('!\)!i', '\)', $tag);
        $tag = preg_replace('!\(!i', '\(', $tag);
        $tag = preg_replace('!\!!i', '\!', $tag);
        $tag = preg_replace('!\^!i', '\^', $tag);
        $tag = preg_replace('!\$!i', '\$', $tag);
        $tag = preg_replace('!\{!i', '\}', $tag);
        $tag = preg_replace('!\}!i', '\{', $tag);
685
        $tag = preg_replace('#\?#i', '\?', $tag);
Feu's avatar
test  
Feu committed
686 687 688 689
        $tag = preg_replace('!\+!i', '\+', $tag);
        $tag = preg_replace('!\*!i', '\*', $tag);
        $tag = preg_replace('!\.!i', '\.', $tag);
        $tag = preg_replace('!\|!i', '\|', $tag);
Bate's avatar
Bate committed
690
        $text = preg_replace('!( |^|\n)'.$tag.'( |$|\n)!i', "$1<img src=\"".$wwwtopdir.$smPath.$img."\" alt=\"$tag\" />$2", $text);
691

Feu's avatar
test  
Feu committed
692 693 694 695
      }
    }
    return $text;
  }
696

Feu's avatar
test  
Feu committed
697 698 699 700 701 702 703 704
  function firstpass(&$table,&$text,$regexp,$replace,$lpad='',$rpad='')
  {
    $ext='';
    if(substr($regexp,-1) == 'e')
    {
      $ext='e';
      $regexp = substr($regexp,0,-1);
    }
705

Feu's avatar
test  
Feu committed
706 707 708 709 710 711 712 713
    while(preg_match($regexp,$text,$matches))
    {
      $token = $this->mkToken();
      $match = $matches[0];
      $text  = preg_replace($regexp,$lpad.$token.$rpad,$text,1);
      $table[$token] = preg_replace($regexp.$ext,$replace,$match);
    }
  }
714

Feu's avatar
test  
Feu committed
715 716 717 718
  function mkToken()
  {
    return '~'.md5(uniqid(rand(), true)).'~';
  }
719

Feu's avatar
test  
Feu committed
720 721 722 723 724 725 726
  /**
   * quote quote quodec !
   */
  function quoteformat($block)
  {
    $block = trim($block);
    $lines = split("\n",$block);
727

Feu's avatar
test  
Feu committed
728 729 730 731 732 733 734 735 736 737
    $lvl = 0;
    $ret = "";
    foreach ($lines as $line)
    {
      $cnt = 0;
      while(substr($line,0,4) == '&gt;')
      {
        $line = substr($line,4);
        $cnt++;
      }
738

Feu's avatar
test  
Feu committed
739 740 741 742 743 744
      if($cnt > $lvl)
        for ($i=0; $i< $cnt - $lvl; $i++)
          $ret .= '<div class="quote">';
      elseif($cnt < $lvl)
        for ($i=0; $i< $lvl - $cnt; $i++)
          $ret .= "</div>\n";
745

Feu's avatar
test  
Feu committed
746 747 748
      $ret .= ltrim($line)."\n";
      $lvl = $cnt;
    }
749

Feu's avatar
test  
Feu committed
750 751
    for ($i=0; $i< $lvl; $i++)
      $ret .= "</div>\n";
752

Feu's avatar
test  
Feu committed
753 754
    return $ret;
  }
755

Feu's avatar
test  
Feu committed
756 757 758
  function tableformat($block)
  {
    $block = trim($block);
759

Feu's avatar
test  
Feu committed
760 761 762 763 764 765
    if(preg_match("/@(.+?)@/",$block))
    {
      preg_match("/@(.+?)@/s",$block,$match);
      $class = str_replace('@', '', $match[0]);
      $block = preg_replace('/@(.+?)@/s', '', $block);
    }
766

Feu's avatar
test  
Feu committed
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    $lines = split("\n",$block);
    $ret = "";
    $rows = array();
    $gen_graph="not_yet";
    $graph=array();
    for($r=0; $r < count($lines); $r++)
    {
      $line = $lines[$r];
      $line = preg_replace('/[\|\^]\s*$/', '', $line);
      $c = -1;
      for($chr=0; $chr < strlen($line); $chr++)
      {
        if($line[$chr] == '^')
        {
          $c++;
          $rows[$r][$c]['head'] = true;
          $rows[$r][$c]['data'] = '';
        }
        elseif($line[$chr] == '|')
        {
          $c++;
          $rows[$r][$c]['head'] = false;
          $rows[$r][$c]['data'] = '';
        }
        else
          $rows[$r][$c]['data'].= $line[$chr];
      }
      if ($c==1 && $gen_stat == "not_yet")
        $gen_stat=true;
      elseif($c>1 && $gen_stat == "not_yet")
      {
        $gen_graph=false;
        unset($graph);
      }
    }
    if($gen_stat == "not_yet" )
    {
      $gen_graph=false;
      unset($graph);
    }
807

Feu's avatar
test  
Feu committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
    // et là les tables de la loi furent !
    if(isset($class))
      $ret .= "<table class=\"".$class."\">\n";
    else
      $ret .= "<table class=\"inline dokutable\">\n";
    for($r=0; $r < count($rows); $r++)
    {
      $ret .= "  <tr>\n";
      for ($c=0; $c < count($rows[$r]); $c++)
      {
        $cspan=1;
        $format=$this->alignment($rows[$r][$c]['data']);
        $format=$format['align'];
        $data = trim($rows[$r][$c]['data']);
        $data = $this->smileys($data);
        $head = $rows[$r][$c]['head'];
        while($c < count($rows[$r])-1 && $rows[$r][$c+1]['data'] == '')
        {
          $c++;
          $cspan++;
        }
        if($cspan > 1)
        {
          $gen_graph=false;
          unset($graph);
          $cspan = 'colspan="'.$cspan.'"';
        }
        else
          $cspan = '';
837

Feu's avatar
test  
Feu committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
        if ($head)
          $ret .= "    <th class=\"inline $format\" $cspan>$data</th>\n";
        else
          $ret .= "    <td class=\"inline $format\" $cspan>$data</td>\n";
      }
      $ret .= "  </tr>\n";
      if($gen_graph && !$head)
      {
        if(!ereg("^[0-9]+(\,[0-9]{1,2})?\%$",$rows[$r][1]["data"]))
        {
          $gen_graph=false;
          unset($graph);
        }
        elseif(!isset($graph[$rows[$r][0]["data"]]))
          $graph[$rows[$r][0]["data"]]=(float)str_replace("%", "",str_replace(",",".",$rows[$r][1]["data"]));
        else
          $graph[$rows[$r][0]["data"]]=$graph[$rows[$r][0]["data"]]+(float)str_replace("%", "",str_replace(",",".",$rows[$r][1]["data"]));
      }
      elseif($gen_graph && $r>0)
      {
        $gen_graph==false;
        unset($graph);
      }
    }
    $ret .= "</table>\n\n";
863

Feu's avatar
test  
Feu committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    if($gen_graph)
    {
      global $js;
      if(!$GLOBALS['js'])
      {
        $GLOBALS['js']=true;
        $_js = "<script language=\"JavaScript\">\n";
        $_js.= "function switchid(obj,id1,id2){\n";
        $_js.= "tohide = document.getElementById(id1);\n";
        $_js.= "tohide.style.display = 'none';\n";
        $_js.= "toshow = document.getElementById(id2);\n";
        $_js.= "toshow.style.display = 'block';\n";
        $_js.= "spanunselect = document.getElementById(id1+0);\n";
        $_js.= "spanunselect.className='';\n";
        $_js.= "aunselect = document.getElementById(id1+1);\n";
        $_js.= "aunselect.className='';\n";
        $_js.= "spanselect = document.getElementById(id2+0);\n";
        $_js.= "spanselect.className='selected';\n";
        $_js.= "aselect = document.getElementById(id2+1);\n";
        $_js.= "aselect.className='selected';\n";
        $_js.= "}\n";
        $_js.="</script>\n";
      }
      else
        $_js="";
      global $topdir;
      require_once($topdir . "include/graph.inc.php");
      if(!empty($graph))
      {
        $total=0;
        $data="";
        foreach($graph as $key => $value)
        {
          $value=str_replace(",",".",$value);
          if(!empty($data))
            $data.=";".rawurlencode($key)."|".$value;
          else
            $data=rawurlencode($key)."|".$value;
          $total=$total+(float)$value;
        }
        $total=round($total,0);
        $id=substr(md5(microtime(true)), 0, 6);
        $_ret ="<div id=\"".$id."\" class=\"tabs\">\n";
        $_ret.="<span id=\"".$id."10\" class=\"selected\"><a href=\"javascript:switchid(this,'".$id."2','".$id."1');\" id=\"".$id."11\" class=\"selected\" title=\"Tableau\">Tableau</a></span>\n";
        $_ret.="<span id=\"".$id."20\"><a href=\"javascript:switchid(this,'".$id."1','".$id."2');\" id=\"".$id."21\" title=\"Graph\">Graph</a></span>\n";
        $_ret.="</div>\n";
        $_ret.="<div id=\"".$id."1\" style=\"display:block;\">".$ret."</div>\n";
        $_ret.="<div id=\"".$id."2\" style=\"display:none;\">";
        if($total==100)
          $_ret.= "<img src=\"".$topdir."gen_graph.php?action=cam&values=".$data."\" /></div>\n";
        else
          $_ret.= "<img src=\"".$topdir."gen_graph.php?action=bar&values=".$data."\" /></div>\n";
        $ret = $_js.$_ret;
      }
    }
    return $ret;
  }
921

Feu's avatar
test  
Feu committed
922 923 924 925
  function listformat($block)
  {
    $block = substr($block,1);
    $text = str_replace('\\"','"',$text);
926

Feu's avatar
test  
Feu committed
927 928 929 930 931
    $ret='';
    $lst=0;
    $lvl=0;
    $enc=0;
    $lines = split("\n",$block);
932

Feu's avatar
test  
Feu committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
    $cnt=0;
    $items = array();
    foreach ($lines as $line)
    {
      $lvl  = 0;
      $lvl += floor(strspn($line,' ')/2);
      $lvl += strspn($line,"\t");
      $line = preg_replace('/^[ \t]+/','',$line);
      (substr($line,0,1) == '-') ? $type='ol' : $type='ul';
      $line = preg_replace('/^[*\-]\s*/','',$line);
      $line = $this->smileys($line);
      $items[$cnt]['level'] = $lvl;
      $items[$cnt]['type']  = $type;
      $items[$cnt]['text']  = $line;
      $cnt++;
    }
949

Feu's avatar
test  
Feu committed
950 951
    $current['level'] = 0;
    $current['type']  = '';
952

Feu's avatar
test  
Feu committed
953 954
    $level = 0;
    $opens = array();
955

Feu's avatar
test  
Feu committed
956 957
    foreach ($items as $item)
    {
958

Feu's avatar
test  
Feu committed
959 960 961 962 963 964 965 966 967 968 969 970 971
      if( $item['level'] > $level )
      {
        $ret .= "\n<".$item['type'].">\n";
        array_push($opens,$item['type']);
      }
      elseif( $item['level'] < $level )
      {
        $ret .= "</li>\n";
        for ($i=0; $i<($level - $item['level']); $i++)
          $ret .= '</'.array_pop($opens).">\n</li>\n";
      }
      else
        $ret .= "</li>\n";
972

Feu's avatar
test  
Feu committed
973
      $level = $item['level'];
974

Feu's avatar
test  
Feu committed
975 976 977
      $ret .= '<li class="level'.$item['level'].'">';
      $ret .= '<span class="li">'.$item['text'].'</span>';
    }
978

Feu's avatar
test  
Feu committed
979 980 981 982 983 984 985
    while ($open = array_pop($opens))
    {
      $ret .= "</li>\n";
      $ret .= '</'.$open.">\n";
    }
    return $ret;
  }
986

Feu's avatar
test  
Feu committed
987 988 989 990
  function preformat($text,$type,$option='')
  {
    global $conf;
    $text = str_replace('\\"','"',$text);
991

Feu's avatar
test  
Feu committed
992 993
    if($type == 'php' && !$conf['phpok']) $type='file';
    if($type == 'html' && !$conf['htmlok']) $type='file';
994

Feu's avatar
test  
Feu committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
    switch ($type)
    {
      case 'php':
          ob_start();
          eval($text);
          $text = ob_get_contents();
          ob_end_clean();
        break;
      case 'html':
        break;
      case 'nowiki':
        $text = htmlspecialchars($text);
        break;
      case 'file':
        $text = htmlspecialchars($text);
        $text = '<pre class="file">'.$text.'</pre>';
        break;
      case 'code':
        $text = htmlspecialchars($text);
        $text = '<pre class="code">'.$text.'</pre>';
        break;
      case 'block':
        $text  = substr($text,1);
        $lines = split("\n",$text);
        $text  = '';
        foreach($lines as $line)
          $text .= substr($line,2)."\n";
        $text = htmlspecialchars($text);
        $text = '<pre class="pre">'.$text.'</pre>';
        break;
    }
    return $text;
  }
1028

Feu's avatar
test  
Feu committed
1029 1030 1031 1032
  function mediaformat($text)
  {
    global $conf;
    global $wwwtopdir;
Ayolo's avatar
bleh  
Ayolo committed
1033
    global $topdir;
Feu's avatar
test  
Feu committed
1034 1035 1036 1037 1038 1039 1040 1041 1042
    $name = str_replace('\\"','"',$text);
    $ret .= $pre;
    $format=$this->alignment($name);
    list($img,$name) = split('\|',$format['src'],2);
    $img=trim($img);
    list($img,$sizes) = split('\?',$img,2);
    list($width,$height) = split('x',$sizes,2);
    $name=trim($name);
    //les dfiles://
1043 1044 1045
    $img = preg_replace("/dfile:\/\/([0-9]*)\/preview/i","https://ae.utbm.fr".$wwwtopdir."d.php?action=download&download=preview&id_file=$1",$img);
    $img = preg_replace("/dfile:\/\/([0-9]*)\/thumb/i","https://ae.utbm.fr".$wwwtopdir."d.php?action=download&download=thumb&id_file=$1",$img);
    $img = preg_replace("/dfile:\/\//i","https://ae.utbm.fr".$wwwtopdir."d.php?action=download&id_file=",$img);
Ayolo's avatar
bleh  
Ayolo committed
1046
    if( defined('CMS_ID_ASSO') )
Ayolo's avatar
bleh  
Ayolo committed
1047
      $img = preg_replace("/sas:\/\//i","images.php?/",$img);
Ayolo's avatar
bleh  
Ayolo committed
1048
    else
1049
      $img = preg_replace("/sas:\/\//i","https://ae.utbm.fr".$wwwtopdir."sas2/images.php?/",$img);
1050

Feu's avatar
test  
Feu committed
1051 1052 1053 1054
    if ( preg_match("/\.flv$/i",$img) )
    {
      if ( !$width )
        $width=400;
1055

Feu's avatar
test  
Feu committed
1056 1057
      if ( !$height )
        $height=300;
1058 1059

      if ( !preg_match("/([a-z0-9]+):\/\//",$img) )
Feu's avatar
test  
Feu committed
1060 1061 1062 1063 1064
      {
        if ( substr($img,0,strlen($wwwtopdir)) == $wwwtopdir )
          $img = "../../".substr($img,strlen($wwwtopdir));
        else
          $img = "../../".$img;
1065
      }
Feu's avatar
test  
Feu committed
1066
      $ret .= "<object type=\"application/x-shockwave-flash\" data=\"".$wwwtopdir."images/flash/flvplayer.swf\" width=\"400\" height=\"300\" class=\"media".$format["align"]."\">";
Ayolo's avatar
bleh  
Ayolo committed
1067 1068 1069 1070
      $ret .="<param name=\"movie\" value=\"".$wwwtopdir."images/flash/flvplayer.swf\" />";
      $ret .="<param name=\"FlashVars\" value=\"flv=".$img."\" />";
      $ret .="<param name=\"wmode\" value=\"transparent\" />";
      $ret .="</object>";
Feu's avatar
test  
Feu committed
1071 1072
      return $ret;
    }
1073

Feu's avatar
test  
Feu committed
1074 1075
    $ret .= '<img src="'.$img.'"';
    $ret .= ' class="media'.$format['align'].'"';
BenC's avatar
BenC committed
1076 1077 1078 1079
    if(!empty($width))
      $ret .= ' width="'.$width.'"';
    if(!empty($height))
      $ret .= ' height="'.$height.'"';
Feu's avatar
test  
Feu committed
1080 1081 1082
    $ret .= ' alt="'.$name.'" title="'.$name.'" />';
    return $ret;
  }
1083

Ayolo's avatar
bleh  
Ayolo committed
1084 1085 1086 1087 1088 1089 1090 1091
  /*
   * format :
   * url(|param,valeur(;param,valeur(...)))
   *
   */
  function flashformat($text)
  {
    $format=$this->alignment($text);
Ayolo's avatar
bleh  
Ayolo committed
1092
    list($url,$sizes,$params) = split('\|',$format['src'],3);
Ayolo's avatar
bleh  
Ayolo committed
1093
    $sizes=trim($sizes);
Ayolo's avatar
bleh  
Ayolo committed
1094 1095
    $params=trim($params);
    $url=trim($url);
Bate's avatar
Bate committed
1096
    if(!preg_match("/^(https?:\/\/)?([^\/]+)/i",$url))
Ayolo's avatar
bleh  
Ayolo committed
1097
      return '';
Ayolo's avatar
bleh  
Ayolo committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    $sizes=trim($sizes);
    $x=0;
    $y=0;
    if(!empty($sizes))
    {
      $sizes=explode(';',$sizes);
      foreach($sizes as $size)
      {
        list($name,$value)=split('=',$size,3);
        if($name=='x')
          $x=intval($value);
        elseif($name=='y')
          $y=intval($value);
      }
    }
    else
    {
      $x=400;
      $y=300;
    }
1118 1119 1120 1121 1122 1123
    if(empty($params))
    {
      $oembed_content = $this->oembed_fetch($url, $x);
      if (!empty($oembed_content))
        return $oembed_content;
    }
Ayolo's avatar
bleh  
Ayolo committed
1124

Ayolo's avatar
bleh  
Ayolo committed
1125
    $ret='<div class="externflash media'.$format['align'].'">';
Ayolo's avatar
bleh  
Ayolo committed
1126
    $ret .= "<object type=\"application/x-shockwave-flash\" data=\"".$url."\" width=\"$x\" height=\"$y\" class=\"media".$format["align"]."\">";
Ayolo's avatar
bleh  
Ayolo committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    $ret .= "<param name=\"movie\" value=\"".$url."\" />";
    if(!empty($params))
    {
      $params=explode(';',$params);
      foreach($params as $param)
      {
        list($name,$value)=split(',',$param,2);
        $name=trim($name);
        if(!empty($name) && $name!='movie')
          $ret .= "<param name=\"".$name."\" value=\"".$value."\" />";
      }
    }
    return $ret.'</object></div>'.chr(13);
  }

1142
  function oembed_fetch($url, $maxwidth=False)
1143
  {
1144
    $oembed_url = $this->get_oembed_url($url);
1145 1146 1147 1148

    if (empty($oembed_url))
      return '';

1149 1150 1151
    if ($maxwidth)
      $oembed_url .= "&maxwidth=".$maxwidth;

1152
    $session = curl_init($oembed_url);
1153 1154
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $reponse = curl_exec($session);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
    $error = curl_error($session);
    curl_close($session);

    if ( !empty($error) )
      return 'Impossible de récupérer le fichier oEmbed de la vidéo.';

    $xml = simplexml_load_string($reponse);

    if (empty($xml->html))
      return 'Fichier oEmbed non supporté.';

Bate's avatar
Bate committed
1166
    return preg_replace("/http\:\/\//","https://",$xml->html);
1167 1168
  }

1169
  function get_oembed_url($url)
Ayolo's avatar
bleh  
Ayolo committed
1170
  {
1171
    // Pour les plus courants on retourne direct l'url
Bate's avatar
Bate committed
1172 1173 1174 1175 1176 1177
    if(preg_match('/https?\:\/\/.*\.youtube\.[^.]*\//',$url))
      return "https://www.youtube.com/oembed?format=xml&url=".$url;
    elseif(preg_match('/https?\:\/\/.*\.dailymotion.[^.]*\//',$url))
      return "https://www.dailymotion.com/services/oembed?format=xml&url=".$url;
    elseif(preg_match('/https?\:\/\/.*\.vimeo.[^.]*\//',$url))
      return "https://vimeo.com/api/oembed.xml?url=".$url;
1178

1179 1180 1181
    // Mais ta gueule !
    libxml_use_internal_errors(true);
    $dom = new DOMDocument;
1182

1183
    if (!$dom->loadHTMLFile($url))
1184
      return '';
Ayolo's avatar
bleh  
Ayolo committed
1185 1186
    else
    {
1187
      $xml = simplexml_import_dom($dom);
1188

1189
      foreach($xml->head->link as $lnk)
Ayolo's avatar
bleh  
Ayolo committed
1190
      {
1191
        if (($lnk['rel'] == "alternate") && (($lnk['type'] == "text/xml+oembed") || ($lnk['type'] == "application/xml+oembed")))
1192
          return $lnk['href'];
Ayolo's avatar
bleh  
Ayolo committed
1193 1194 1195
      }
    }

1196 1197
    return '';
  }
Ayolo's avatar
bleh  
Ayolo committed
1198

Feu's avatar
test  
Feu committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  function alignment($texte)
  {
    $r=false;
    $l=false;
    $left=rtrim($texte);
    $right=ltrim($texte);
    if($texte != $right)
      $r=true;
    if($texte != $left)
      $l=true;
1209

Feu's avatar
test  
Feu committed
1210 1211 1212 1213 1214 1215 1216 1217 1218
    if ($l && $r)
      return array('src'=>$texte, 'align'=>"center");
    elseif($r)
      return array('src'=>$texte, 'align'=>"right");
    elseif($l)
      return array('src'=>$texte, 'align'=>"left");
    else
      return array('src'=>$texte, 'align'=>"");
  }
1219

Feu's avatar
test  
Feu committed
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
  /**
   * formatage des liens
   *
   * $link   URL pour le href=""
   * $name
   * $class  CSS class du lien
   * $target la cible (blank) pour la fenetre courante
   * $style  style aditionnels style=""
   * $pre
   * $post
   * $more
   *
   */
1233

Feu's avatar
test  
Feu committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
  function format_link(&$link,&$name,&$class,&$target,&$style,&$pre,&$post,&$more)
  {
    $class  = 'urlextern';
    $target = $conf['target']['extern'];
    $pre    = '';
    $post   = '';
    $style  = '';
    $more   = '';
    $link   = $link;
    if(!$name)
      $name = htmlspecialchars($link);
    else
    {
      if(preg_match("#\{\{([^\}]+?)\}\}#ie",$name))
      {
        $name=preg_replace("/\{\{/s","",$name);
        $name=preg_replace("/\}\}/s","",$name);
        $name=str_replace($name, $this->mediaformat($name), $name);
      }
    }
  }
1255

Feu's avatar
test  
Feu committed
1256 1257
  function format_link_email(&$link,&$name,&$class,&$target,&$style,&$pre,&$post,&$more)
  {
Ayolo's avatar
mail  
Ayolo committed
1258
    global $conf;
Feu's avatar
test  
Feu committed
1259 1260 1261 1262 1263 1264
    $class  = 'mail';
    $target = '';
    $pre    = '';
    $post   = '';
    $style  = '';
    $more   = '';
1265

Feu's avatar
test