includes/render.php
changeset 1103 90225c988124
parent 1098 be6cfe79128c
child 1108 c1be67a50d81
equal deleted inserted replaced
1102:faef5e62e1e0 1103:90225c988124
     1 <?php
     1 <?php
     2 
     2 
     3 /*
     3 /*
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     5  * Version 1.1.6 (Caoineag beta 1)
     5  * Copyright (C) 2006-2009 Dan Fuhry
     6  * Copyright (C) 2006-2008 Dan Fuhry
       
     7  * render.php - handles fetching pages and parsing them into HTML
     6  * render.php - handles fetching pages and parsing them into HTML
     8  *
     7  *
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    11  *
    10  *
   616    */
   615    */
   617   
   616   
   618   public static function parse_internal_links($text, $tplcode = false, $do_exist_check = true, $match_page_id = false, $match_namespace = false)
   617   public static function parse_internal_links($text, $tplcode = false, $do_exist_check = true, $match_page_id = false, $match_namespace = false)
   619   {
   618   {
   620     global $db, $session, $paths, $template, $plugins; // Common objects
   619     global $db, $session, $paths, $template, $plugins; // Common objects
   621     
   620   
   622     if ( is_string($tplcode) )
   621     $parser = is_string($tplcode) ? $template->makeParserText($tplcode) : false;
   623     {
       
   624       $parser = $template->makeParserText($tplcode);
       
   625     }
       
   626     
   622     
   627     // stage 1 - links with alternate text
   623     // stage 1 - links with alternate text
   628     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   624     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   629     foreach ( $matches[0] as $i => $match )
   625     foreach ( $matches[0] as $i => $match )
   630     {
   626     {
   631       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   627       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   632       if ( ($pos = strrpos($page_id, '#')) !== false )
   628       $link = self::generate_internal_link($namespace, $page_id, $matches[2][$i], $match, $parser, $do_exist_check, $match_page_id, $match_namespace);
   633       {
       
   634         $hash = substr($page_id, $pos);
       
   635         $page_id = substr($page_id, 0, $pos);
       
   636       }
       
   637       else
       
   638       {
       
   639         $hash = '';
       
   640       }
       
   641       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
       
   642       
       
   643       $url = makeUrl($pid_clean, false, true) . $hash;
       
   644       $inner_text = $matches[2][$i];
       
   645       $quot = '"';
       
   646       $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
       
   647       
       
   648       if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
       
   649         $exists .= ' class="currentpage"';
       
   650       
       
   651       if ( $tplcode )
       
   652       {
       
   653         $parser->assign_vars(array(
       
   654             'HREF' => $url,
       
   655             'FLAGS' => $exists,
       
   656             'TEXT' => $inner_text
       
   657           ));
       
   658         $link = $parser->run();
       
   659       }
       
   660       else
       
   661       {
       
   662         $omatch = self::escape_parser_hint_attrib($match);
       
   663         $link = "<!--#internallink src=\"$omatch\" --><a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
       
   664       }
       
   665       
       
   666       $text = str_replace($match, $link, $text);
   629       $text = str_replace($match, $link, $text);
   667     }
   630     }
   668     
   631     
   669     // stage 2 - links with no alternate text
   632     // stage 2 - links with no alternate text
   670     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\]\]/', $text, $matches);
   633     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\]\]/', $text, $matches);
   671     foreach ( $matches[0] as $i => $match )
   634     foreach ( $matches[0] as $i => $match )
   672     {
   635     {
   673       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   636       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
   674       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   637       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
   675       
       
   676       $url = makeUrl($pid_clean, false, true);
       
   677       $inner_text = ( isPage($pid_clean) ) ? htmlspecialchars(get_page_title($pid_clean)) : htmlspecialchars($matches[1][$i]);
   638       $inner_text = ( isPage($pid_clean) ) ? htmlspecialchars(get_page_title($pid_clean)) : htmlspecialchars($matches[1][$i]);
   678       $quot = '"';
   639       
   679       $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
   640       $link = self::generate_internal_link($namespace, $page_id, $inner_text, $match, $parser, $do_exist_check, $match_page_id, $match_namespace);
   680       
       
   681       if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
       
   682         $exists .= ' class="currentpage"';
       
   683       
       
   684       if ( $tplcode )
       
   685       {
       
   686         $parser->assign_vars(array(
       
   687             'HREF' => $url,
       
   688             'FLAGS' => $exists,
       
   689             'TEXT' => $inner_text
       
   690           ));
       
   691         $link = $parser->run();
       
   692       }
       
   693       else
       
   694       {
       
   695         $omatch = self::escape_parser_hint_attrib($match);
       
   696         $link = "<!--#internallink src=\"$omatch\" --><a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
       
   697       }
       
   698       
   641       
   699       $text = str_replace($match, $link, $text);
   642       $text = str_replace($match, $link, $text);
   700     }
   643     }
   701     
   644     
   702     return $text;
   645     return $text;
       
   646   }
       
   647   
       
   648   /**
       
   649    * Internal link generation function
       
   650    * @access private
       
   651    * @return string HTML
       
   652    */
       
   653   
       
   654   private static function generate_internal_link($namespace, $page_id, $inner_text, $match, $parser = false, $do_exist_check = true, $match_page_id = false, $match_namespace = false)
       
   655   {
       
   656     global $db, $session, $paths, $template, $plugins; // Common objects
       
   657     
       
   658     if ( ($pos = strrpos($page_id, '#')) !== false )
       
   659     {
       
   660       $hash = substr($page_id, $pos);
       
   661       $page_id = substr($page_id, 0, $pos);
       
   662     }
       
   663     else
       
   664     {
       
   665       $hash = '';
       
   666     }
       
   667     
       
   668     if ( $namespace == 'Admin' )
       
   669     {
       
   670       // No linking directly to Admin pages!
       
   671       $get = 'module=' . $paths->nslist[$namespace] . sanitize_page_id($page_id);
       
   672       $pid_clean = $paths->nslist['Special'] . 'Administration';
       
   673       $onclick = ' onclick="ajaxLoginNavTo(\'Special\', \'Administration\', USER_LEVEL_ADMIN, \'' . addslashes($get) . '\'); return false;"';
       
   674     }
       
   675     else
       
   676     {
       
   677       $get = false;
       
   678       $onclick = '';
       
   679       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
       
   680     }
       
   681     
       
   682     $url = makeUrl($pid_clean, $get, true) . $hash;
       
   683     $quot = '"';
       
   684     $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
       
   685     
       
   686     if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
       
   687       $exists .= ' class="currentpage"';
       
   688     
       
   689     if ( $parser )
       
   690     {
       
   691       $parser->assign_vars(array(
       
   692           'HREF' => $url,
       
   693           'FLAGS' => $exists,
       
   694           'TEXT' => $inner_text
       
   695         ));
       
   696       $link = $parser->run();
       
   697     }
       
   698     else
       
   699     {
       
   700       $omatch = self::escape_parser_hint_attrib($match);
       
   701       $link = "<!--#internallink src=\"$omatch\" --><a{$onclick} href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
       
   702     }
       
   703     
       
   704     return $link;
   703   }
   705   }
   704   
   706   
   705   /**
   707   /**
   706    * Parses a partial template tag in wikitext, and return an array with the parameters.
   708    * Parses a partial template tag in wikitext, and return an array with the parameters.
   707    * @param string The portion of the template tag that contains the parameters.
   709    * @param string The portion of the template tag that contains the parameters.