includes/render.php
changeset 1103 90225c988124
parent 1098 be6cfe79128c
child 1108 c1be67a50d81
--- a/includes/render.php	Tue Aug 25 01:43:11 2009 -0400
+++ b/includes/render.php	Tue Aug 25 01:43:40 2009 -0400
@@ -2,8 +2,7 @@
 
 /*
  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
- * Version 1.1.6 (Caoineag beta 1)
- * Copyright (C) 2006-2008 Dan Fuhry
+ * Copyright (C) 2006-2009 Dan Fuhry
  * render.php - handles fetching pages and parsing them into HTML
  *
  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
@@ -618,51 +617,15 @@
   public static function parse_internal_links($text, $tplcode = false, $do_exist_check = true, $match_page_id = false, $match_namespace = false)
   {
     global $db, $session, $paths, $template, $plugins; // Common objects
-    
-    if ( is_string($tplcode) )
-    {
-      $parser = $template->makeParserText($tplcode);
-    }
+  
+    $parser = is_string($tplcode) ? $template->makeParserText($tplcode) : false;
     
     // stage 1 - links with alternate text
     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
     foreach ( $matches[0] as $i => $match )
     {
       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
-      if ( ($pos = strrpos($page_id, '#')) !== false )
-      {
-        $hash = substr($page_id, $pos);
-        $page_id = substr($page_id, 0, $pos);
-      }
-      else
-      {
-        $hash = '';
-      }
-      $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
-      
-      $url = makeUrl($pid_clean, false, true) . $hash;
-      $inner_text = $matches[2][$i];
-      $quot = '"';
-      $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
-      
-      if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
-        $exists .= ' class="currentpage"';
-      
-      if ( $tplcode )
-      {
-        $parser->assign_vars(array(
-            'HREF' => $url,
-            'FLAGS' => $exists,
-            'TEXT' => $inner_text
-          ));
-        $link = $parser->run();
-      }
-      else
-      {
-        $omatch = self::escape_parser_hint_attrib($match);
-        $link = "<!--#internallink src=\"$omatch\" --><a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
-      }
-      
+      $link = self::generate_internal_link($namespace, $page_id, $matches[2][$i], $match, $parser, $do_exist_check, $match_page_id, $match_namespace);
       $text = str_replace($match, $link, $text);
     }
     
@@ -672,29 +635,9 @@
     {
       list($page_id, $namespace) = RenderMan::strToPageID($matches[1][$i]);
       $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
-      
-      $url = makeUrl($pid_clean, false, true);
       $inner_text = ( isPage($pid_clean) ) ? htmlspecialchars(get_page_title($pid_clean)) : htmlspecialchars($matches[1][$i]);
-      $quot = '"';
-      $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
-      
-      if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
-        $exists .= ' class="currentpage"';
       
-      if ( $tplcode )
-      {
-        $parser->assign_vars(array(
-            'HREF' => $url,
-            'FLAGS' => $exists,
-            'TEXT' => $inner_text
-          ));
-        $link = $parser->run();
-      }
-      else
-      {
-        $omatch = self::escape_parser_hint_attrib($match);
-        $link = "<!--#internallink src=\"$omatch\" --><a href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
-      }
+      $link = self::generate_internal_link($namespace, $page_id, $inner_text, $match, $parser, $do_exist_check, $match_page_id, $match_namespace);
       
       $text = str_replace($match, $link, $text);
     }
@@ -703,6 +646,65 @@
   }
   
   /**
+   * Internal link generation function
+   * @access private
+   * @return string HTML
+   */
+  
+  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)
+  {
+    global $db, $session, $paths, $template, $plugins; // Common objects
+    
+    if ( ($pos = strrpos($page_id, '#')) !== false )
+    {
+      $hash = substr($page_id, $pos);
+      $page_id = substr($page_id, 0, $pos);
+    }
+    else
+    {
+      $hash = '';
+    }
+    
+    if ( $namespace == 'Admin' )
+    {
+      // No linking directly to Admin pages!
+      $get = 'module=' . $paths->nslist[$namespace] . sanitize_page_id($page_id);
+      $pid_clean = $paths->nslist['Special'] . 'Administration';
+      $onclick = ' onclick="ajaxLoginNavTo(\'Special\', \'Administration\', USER_LEVEL_ADMIN, \'' . addslashes($get) . '\'); return false;"';
+    }
+    else
+    {
+      $get = false;
+      $onclick = '';
+      $pid_clean = $paths->nslist[$namespace] . sanitize_page_id($page_id);
+    }
+    
+    $url = makeUrl($pid_clean, $get, true) . $hash;
+    $quot = '"';
+    $exists = ( ($do_exist_check && isPage($pid_clean)) || !$do_exist_check ) ? '' : ' class="wikilink-nonexistent"';
+    
+    if ( $match_page_id && $match_namespace && $pid_clean === $paths->get_pathskey($match_page_id, $match_namespace) )
+      $exists .= ' class="currentpage"';
+    
+    if ( $parser )
+    {
+      $parser->assign_vars(array(
+          'HREF' => $url,
+          'FLAGS' => $exists,
+          'TEXT' => $inner_text
+        ));
+      $link = $parser->run();
+    }
+    else
+    {
+      $omatch = self::escape_parser_hint_attrib($match);
+      $link = "<!--#internallink src=\"$omatch\" --><a{$onclick} href={$quot}{$url}{$quot}{$exists}>{$inner_text}</a><!--#/internallink-->";
+    }
+    
+    return $link;
+  }
+  
+  /**
    * Parses a partial template tag in wikitext, and return an array with the parameters.
    * @param string The portion of the template tag that contains the parameters.
    * @example