dokusyntax.inc.php 38 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");
Feu's avatar
test  
Feu committed
35 36 37 38 39 40 41 42 43

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
   */
44
  function doku2xhtml($text,$summury=false,$extern=false)
Feu's avatar
test  
Feu committed
45
  {
Ayolo's avatar
bleh  
Ayolo committed
46
    global $parser,$timing,$conf;
Ayolo's avatar
bleh  
Ayolo committed
47
    $this->uid=gen_uid();
48
    $this->extern=$extern;
Feu's avatar
test  
Feu committed
49 50 51 52
    $timing["doku2xhtml"] -= microtime(true);
    $js = false;
    $table   = array();
    $hltable = array();
53

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

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

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

Feu's avatar
test  
Feu committed
63 64 65 66 67 68 69 70 71
    /*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;
72

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

Feu's avatar
test  
Feu committed
75 76 77 78 79
    // 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
80

Feu's avatar
test  
Feu committed
81 82 83
    // 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')");
84

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



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

Feu's avatar
test  
Feu committed
92 93 94 95 96 97 98 99 100 101 102
    //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;
103 104


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

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

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

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

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

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

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

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

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

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

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

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

Ayolo's avatar
bleh  
Ayolo committed
159
    $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);
160

Ayolo's avatar
bleh  
Ayolo committed
161
    if(isset($conf['bookmarks']) && $conf['bookmarks'])
Ayolo's avatar
bleh  
Ayolo committed
162 163 164
    {
      global $wwwtopdir;
      $anchor=$wwwtopdir."images/forum/anchor.png";
Ayolo's avatar
Ayolo committed
165
      while( preg_match("/&lt;bookmark:(.*?)&gt;/i",$text) )
Ayolo's avatar
bleh  
Ayolo committed
166 167 168 169
        $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
170

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

Bate's avatar
Bate committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    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
194 195 196 197
    /* 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);
198

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

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

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

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

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

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

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

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


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


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

Feu's avatar
test  
Feu committed
234 235 236 237 238 239 240 241 242 243
      // 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;
      }
244

Feu's avatar
test  
Feu committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      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;
        }
      }
269

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

Feu's avatar
test  
Feu committed
277
    }
278

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

Feu's avatar
test  
Feu committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  /**
   * 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
306
    $token = $this->mkToken();
jlaval's avatar
Foo  
jlaval committed
307
    $name = trim($hline);
jlaval's avatar
jlaval committed
308 309 310 311 312
    $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
313
        $name = substr ($name, 1);
jlaval's avatar
jlaval committed
314
    }
jlaval's avatar
Foo  
jlaval committed
315
    $hltable[] = array( 'name'  => htmlspecialchars($name),
Feu's avatar
test  
Feu committed
316 317
                        'level' => $lvl,
                        'line'  => $lno,
jlaval's avatar
jlaval committed
318 319
                        'token' => $token,
                        'collapsed' => $collapsed);
Feu's avatar
test  
Feu committed
320 321
    return $token;
  }
322

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

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

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

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

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

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

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

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

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

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

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

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

Feu's avatar
test  
Feu committed
455 456 457 458 459 460
    // 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);
461 462


Ayolo's avatar
Ayolo committed
463
    if( !strpos($link,'mailto:') && preg_match('/^([a-zA-Z]+):\/\//',$link) ) // liens externe et spéciaux
Feu's avatar
test  
Feu committed
464 465 466 467 468 469 470 471 472 473
    {
      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
474
        if ( !isset($match[2]) || $match[2] == "/download" )
Bate's avatar
Bate committed
475
          $link = "https://ae.utbm.fr/d.php?action=download&amp;id_file=".$match[1];
Ayolo's avatar
bleh  
Ayolo committed
476
        elseif ( $match[2] == "/preview" )
Bate's avatar
Bate committed
477
          $link = "https://ae.utbm.fr/d.php?action=download&amp;download=preview&amp;id_file=".$match[1];
Ayolo's avatar
bleh  
Ayolo committed
478
        elseif ( $match[2] == "/thumb" )
Bate's avatar
Bate committed
479
          $link = "https://ae.utbm.fr/d.php?action=download&amp;download=thumb&amp;id_file=".$match[1];
Ayolo's avatar
bleh  
Ayolo committed
480
        elseif ( $match[2] == "/info" )
Bate's avatar
Bate committed
481
          $link = "https://ae.utbm.fr/d.php?id_file=".$match[1];
Feu's avatar
test  
Feu committed
482 483 484
      }
      else
      {
Ayolo's avatar
bleh  
Ayolo committed
485
        //les article://
Bate's avatar
Bate committed
486
        $link = preg_replace("/article:\/\//i",'https://ae.utbm.fr/'.$GLOBALS["entitiescatalog"]["page"][3]."?name=",$link);
Ayolo's avatar
bleh  
Ayolo committed
487
        //les wiki://
Bate's avatar
Bate committed
488
        $link = preg_replace("/wiki:\/\//i",'https://ae.utbm.fr/'.$GLOBALS["entitiescatalog"]["wiki"][3]."?name=",$link);
Ayolo's avatar
bleh  
Ayolo committed
489
        if( defined('CMS_ID_ASSO') )
Ayolo's avatar
bleh  
Ayolo committed
490
          $link = preg_replace("/sas:\/\//i","images.php?/",$link);
Ayolo's avatar
bleh  
Ayolo committed
491
        else
Bate's avatar
Bate committed
492
          $link = preg_replace("/sas:\/\//i","https://ae.utbm.fr/sas2/images.php?/",$link);
Feu's avatar
test  
Feu committed
493 494
      }
    }
Ayolo's avatar
Ayolo committed
495
    elseif ( !strpos($link,'mailto:') && !preg_match("#(\.|/)#",$link) )
496
    {
Feu's avatar
test  
Feu committed
497 498 499 500 501 502 503
      $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;
504

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



Feu's avatar
test  
Feu committed
519 520 521 522 523 524 525 526 527 528
    $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;
529

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

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

Feu's avatar
test  
Feu committed
540 541
    $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
542
    $text = preg_replace('/\/\/(.*?)\/\//s','<em>\1</em>',$text);  //emphasize
Feu's avatar
test  
Feu committed
543 544 545
    $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
546

Feu's avatar
test  
Feu committed
547 548
    $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);
549

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

Feu's avatar
test  
Feu committed
552 553 554 555 556 557 558
    $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);
559

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

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

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

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

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

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

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

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

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

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

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

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

Feu's avatar
test  
Feu committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
  /**
   * 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>';
    }
617

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

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

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

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

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

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

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

Feu's avatar
test  
Feu committed
705 706 707 708 709 710 711 712
    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);
    }
  }
713

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

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

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

Feu's avatar
test  
Feu committed
738 739 740 741 742 743
      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";
744

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

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

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

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

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

Feu's avatar
test  
Feu committed
766 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
    $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);
    }
806

Feu's avatar
test  
Feu committed
807 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
    // 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 = '';
836

Feu's avatar
test  
Feu committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
        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";
862

Feu's avatar
test  
Feu committed
863 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
    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;
  }
920

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

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

Feu's avatar
test  
Feu committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    $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++;
    }
948

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

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

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

Feu's avatar
test  
Feu committed
958 959 960 961 962 963 964 965 966 967 968 969 970
      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";
971

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

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

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

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

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

Feu's avatar
test  
Feu committed
994 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
    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;
  }
1027

Feu's avatar
test  
Feu committed
1028 1029 1030 1031
  function mediaformat($text)
  {
    global $conf;
    global $wwwtopdir;
Ayolo's avatar
bleh  
Ayolo committed
1032
    global $topdir;
Feu's avatar
test  
Feu committed
1033 1034 1035 1036 1037 1038 1039 1040 1041
    $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://
Bate's avatar
Bate committed
1042 1043 1044
    $img = preg_replace("/dfile:\/\/([0-9]*)\/preview/i","https://ae.utbm.fr/d.php?action=download&download=preview&id_file=$1",$img);
    $img = preg_replace("/dfile:\/\/([0-9]*)\/thumb/i","https://ae.utbm.fr/d.php?action=download&download=thumb&id_file=$1",$img);
    $img = preg_replace("/dfile:\/\//i","https://ae.utbm.fr/d.php?action=download&id_file=",$img);
Ayolo's avatar
bleh  
Ayolo committed
1045
    if( defined('CMS_ID_ASSO') )
Ayolo's avatar
bleh  
Ayolo committed
1046
      $img = preg_replace("/sas:\/\//i","images.php?/",$img);
Ayolo's avatar
bleh  
Ayolo committed
1047
    else
Bate's avatar
Bate committed
1048
      $img = preg_replace("/sas:\/\//i","https://ae.utbm.fr/sas2/images.php?/",$img);
1049

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

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

      if ( !preg_match("/([a-z0-9]+):\/\//",$img) )
Feu's avatar
test  
Feu committed
1059 1060 1061 1062 1063
      {
        if ( substr($img,0,strlen($wwwtopdir)) == $wwwtopdir )
          $img = "../../".substr($img,strlen($wwwtopdir));
        else
          $img = "../../".$img;
1064
      }
Feu's avatar
test  
Feu committed
1065
      $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
1066 1067 1068 1069
      $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
1070 1071
      return $ret;
    }
1072

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

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

Ayolo's avatar
bleh  
Ayolo committed
1124
    $ret='<div class="externflash media'.$format['align'].'">';
Ayolo's avatar
bleh  
Ayolo committed
1125
    $ret .= "<object type=\"application/x-shockwave-flash\" data=\"".$url."\" width=\"$x\" height=\"$y\" class=\"media".$format["align"]."\">";
Ayolo's avatar
bleh  
Ayolo committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    $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);
  }

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

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

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

1151
    $session = curl_init($oembed_url);
1152 1153
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $reponse = curl_exec($session);
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    $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
1165
    return preg_replace("/http\:\/\//","https://",$xml->html);
1166 1167
  }

1168
  function get_oembed_url($url)
Ayolo's avatar
bleh  
Ayolo committed
1169
  {
1170
    // Pour les plus courants on retourne direct l'url
Bate's avatar
Bate committed
1171 1172 1173 1174 1175 1176
    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;
1177

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

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

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

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

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

Feu's avatar
test  
Feu committed
1209 1210 1211 1212 1213 1214 1215 1216 1217
    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'=>"");
  }
1218

Feu's avatar
test  
Feu committed
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
  /**
   * 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
   *
   */
1232

Feu's avatar
test  
Feu committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  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);
      }
    }
  }
1254

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

Feu's avatar
test  
Feu committed
1265
    $name   = htmlspecialchars($name);
1266

Feu's avatar
test  
Feu committed
1267 1268 1269