includes/render.php
changeset 371 dc6026376919
parent 345 4ccdfeee9a11
child 377 bb3e6c3bd4f4
equal deleted inserted replaced
370:b251818286b1 371:dc6026376919
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    14  */
    14  */
    15  
    15  
    16 class RenderMan {
    16 class RenderMan {
    17   
    17   
    18   function strToPageID($string)
    18   public static function strToPageID($string)
    19   {
    19   {
    20     global $db, $session, $paths, $template, $plugins; // Common objects
    20     global $db, $session, $paths, $template, $plugins; // Common objects
    21     $k = array_keys($paths->nslist);
    21     $k = array_keys($paths->nslist);
    22     $proj_alt = 'Project:';
    22     $proj_alt = 'Project:';
    23     if ( substr($string, 0, (strlen($proj_alt))) == $proj_alt )
    23     if ( substr($string, 0, (strlen($proj_alt))) == $proj_alt )
    36       }
    36       }
    37     }
    37     }
    38     return Array($pg, $ns);
    38     return Array($pg, $ns);
    39   }
    39   }
    40   
    40   
    41   function getPage($page_id, $namespace, $wiki = 1, $smilies = true, $filter_links = true, $redir = true, $render = true)
    41   public static function getPage($page_id, $namespace, $wiki = 1, $smilies = true, $filter_links = true, $redir = true, $render = true)
    42   {
    42   {
    43     global $db, $session, $paths, $template, $plugins; // Common objects
    43     global $db, $session, $paths, $template, $plugins; // Common objects
    44     
    44     
    45     $perms =& $session;
    45     $perms =& $session;
    46     
    46     
   110     }
   110     }
   111     $session->disallow_password_grab();
   111     $session->disallow_password_grab();
   112     return ($render) ? RenderMan::render($message, $wiki, $smilies, $filter_links) : $message;
   112     return ($render) ? RenderMan::render($message, $wiki, $smilies, $filter_links) : $message;
   113   }
   113   }
   114   
   114   
   115   function getTemplate($id, $parms)
   115   public static function getTemplate($id, $parms)
   116   {
   116   {
   117     global $db, $session, $paths, $template, $plugins; // Common objects
   117     global $db, $session, $paths, $template, $plugins; // Common objects
   118     if(!isset($paths->pages[$paths->nslist['Template'].$id])) 
   118     if(!isset($paths->pages[$paths->nslist['Template'].$id])) 
   119     {
   119     {
   120       return '[['.$paths->nslist['Template'].$id.']]';
   120       return '[['.$paths->nslist['Template'].$id.']]';
   148     }
   148     }
   149     $text = RenderMan::include_templates($text);
   149     $text = RenderMan::include_templates($text);
   150     return $text;
   150     return $text;
   151   }
   151   }
   152   
   152   
   153   function fetch_template_text($id)
   153   public static function fetch_template_text($id)
   154   {
   154   {
   155     global $db, $session, $paths, $template, $plugins; // Common objects
   155     global $db, $session, $paths, $template, $plugins; // Common objects
   156     if(!isset($paths->pages[$paths->nslist['Template'].$id])) 
   156     if(!isset($paths->pages[$paths->nslist['Template'].$id])) 
   157     {
   157     {
   158       return '[['.$paths->nslist['Template'].$id.']]';
   158       return '[['.$paths->nslist['Template'].$id.']]';
   174     }
   174     }
   175     
   175     
   176     return $text;
   176     return $text;
   177   }
   177   }
   178   
   178   
   179   function render($text, $wiki = 1, $smilies = true, $filter_links = true)
   179   public static function render($text, $wiki = 1, $smilies = true, $filter_links = true)
   180   {
   180   {
   181     global $db, $session, $paths, $template, $plugins; // Common objects
   181     global $db, $session, $paths, $template, $plugins; // Common objects
   182     if($smilies)
   182     if($smilies)
   183     {
   183     {
   184       $text = RenderMan::smilieyize($text);
   184       $text = RenderMan::smilieyize($text);
   192       $text = $template->tplWikiFormat($text);
   192       $text = $template->tplWikiFormat($text);
   193     }
   193     }
   194     return $text;
   194     return $text;
   195   }
   195   }
   196   
   196   
   197   function PlainTextRender($text, $wiki = 1, $smilies = false, $filter_links = true)
   197   public static function PlainTextRender($text, $wiki = 1, $smilies = false, $filter_links = true)
   198   {
   198   {
   199     global $db, $session, $paths, $template, $plugins; // Common objects
   199     global $db, $session, $paths, $template, $plugins; // Common objects
   200     if($smilies)
   200     if($smilies)
   201     {
   201     {
   202       $text = RenderMan::smilieyize($text);
   202       $text = RenderMan::smilieyize($text);
   210       $text = $template->tplWikiFormat($text);
   210       $text = $template->tplWikiFormat($text);
   211     }
   211     }
   212     return $text;
   212     return $text;
   213   }
   213   }
   214   
   214   
   215   function next_gen_wiki_format($text, $plaintext = false, $filter_links = true, $do_params = false)
   215   public static function next_gen_wiki_format($text, $plaintext = false, $filter_links = true, $do_params = false)
   216   {
   216   {
   217     global $db, $session, $paths, $template, $plugins; // Common objects
   217     global $db, $session, $paths, $template, $plugins; // Common objects
   218     $random_id = md5( time() . mt_rand() );
   218     $random_id = md5( time() . mt_rand() );
   219     
   219     
   220     // Strip out <nowiki> sections and PHP code
   220     // Strip out <nowiki> sections and PHP code
   276     $text = preg_replace('/<\/(h[0-9]|div|p)>([\s]+)<(h[0-9]|div|p)( .+?)?>/i', '</\\1><\\3\\4>', $text);
   276     $text = preg_replace('/<\/(h[0-9]|div|p)>([\s]+)<(h[0-9]|div|p)( .+?)?>/i', '</\\1><\\3\\4>', $text);
   277     
   277     
   278     $text = process_tables($text);
   278     $text = process_tables($text);
   279     $text = RenderMan::parse_internal_links($text);
   279     $text = RenderMan::parse_internal_links($text);
   280     
   280     
   281     $wiki =& Text_Wiki::singleton('Mediawiki');
   281     $wiki = Text_Wiki::singleton('Mediawiki');
   282     if($plaintext)
   282     if($plaintext)
   283     {
   283     {
   284       $wiki->setRenderConf('Plain', 'wikilink', 'view_url', contentPath);
   284       $wiki->setRenderConf('Plain', 'wikilink', 'view_url', contentPath);
   285       $result = $wiki->transform($text, 'Plain');
   285       $result = $wiki->transform($text, 'Plain');
   286     }
   286     }
   326     
   326     
   327     return $result;
   327     return $result;
   328     
   328     
   329   }
   329   }
   330   
   330   
   331   function wikiFormat($message, $filter_links = true, $do_params = false, $plaintext = false)
   331   public static function wikiFormat($message, $filter_links = true, $do_params = false, $plaintext = false)
   332   {
   332   {
   333     global $db, $session, $paths, $template, $plugins; // Common objects
   333     global $db, $session, $paths, $template, $plugins; // Common objects
   334     
   334     
   335     return RenderMan::next_gen_wiki_format($message, $plaintext, $filter_links, $do_params);
   335     return RenderMan::next_gen_wiki_format($message, $plaintext, $filter_links, $do_params);
   336     
   336     
   409     $result = str_replace('</nowiki>', '&lt;/nowiki&gt;', $result);
   409     $result = str_replace('</nowiki>', '&lt;/nowiki&gt;', $result);
   410     
   410     
   411     return $result;
   411     return $result;
   412   }
   412   }
   413   
   413   
   414   function destroy_javascript($message, $_php = false)
   414   public static function destroy_javascript($message, $_php = false)
   415   {
   415   {
   416     $message = preg_replace('#<(script|object|applet|embed|iframe|frame|form|input|select)(.*?)>#is', '&lt;\\1\\2&gt;', $message);
   416     $message = preg_replace('#<(script|object|applet|embed|iframe|frame|form|input|select)(.*?)>#is', '&lt;\\1\\2&gt;', $message);
   417     $message = preg_replace('#</(script|object|applet|embed|iframe|frame|form|input|select)(.*?)>#is', '&lt;/\\1\\2&gt;', $message);
   417     $message = preg_replace('#</(script|object|applet|embed|iframe|frame|form|input|select)(.*?)>#is', '&lt;/\\1\\2&gt;', $message);
   418     $message = preg_replace('#(javascript|script|activex|chrome|about|applet):#is', '\\1&#058;', $message);
   418     $message = preg_replace('#(javascript|script|activex|chrome|about|applet):#is', '\\1&#058;', $message);
   419     if ( $_php )
   419     if ( $_php )
   426       $message = preg_replace('#<([a-zA-Z:\-]+) (.*?)on([A-Za-z]*)=(.*?)>#is', '&lt;\\1\\2on\\3=\\4&gt;', $message);
   426       $message = preg_replace('#<([a-zA-Z:\-]+) (.*?)on([A-Za-z]*)=(.*?)>#is', '&lt;\\1\\2on\\3=\\4&gt;', $message);
   427     }
   427     }
   428     return $message;
   428     return $message;
   429   }
   429   }
   430   
   430   
   431   function strip_php($message)
   431   public static function strip_php($message)
   432   {
   432   {
   433     return RenderMan::destroy_javascript($message, true);
   433     return RenderMan::destroy_javascript($message, true);
   434   }
   434   }
   435   
   435   
   436   function sanitize_html($text)
   436   public static function sanitize_html($text)
   437   {
   437   {
   438     $text = htmlspecialchars($text);
   438     $text = htmlspecialchars($text);
   439     $allowed_tags = Array('b', 'i', 'u', 'pre', 'code', 'tt', 'br', 'p', 'nowiki', '!--([\w\W]+)--');
   439     $allowed_tags = Array('b', 'i', 'u', 'pre', 'code', 'tt', 'br', 'p', 'nowiki', '!--([\w\W]+)--');
   440     foreach($allowed_tags as $t)
   440     foreach($allowed_tags as $t)
   441     {
   441     {
   450    * Parses internal links (wikilinks) in a block of text.
   450    * Parses internal links (wikilinks) in a block of text.
   451    * @param string Text to process
   451    * @param string Text to process
   452    * @return string
   452    * @return string
   453    */
   453    */
   454   
   454   
   455   function parse_internal_links($text)
   455   public static function parse_internal_links($text)
   456   {
   456   {
   457     global $db, $session, $paths, $template, $plugins; // Common objects
   457     global $db, $session, $paths, $template, $plugins; // Common objects
   458     
   458     
   459     // stage 1 - links with alternate text
   459     // stage 1 - links with alternate text
   460     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   460     preg_match_all('/\[\[([^\[\]<>\{\}\|]+)\|(.+?)\]\]/', $text, $matches);
   504    * @return array Example:
   504    * @return array Example:
   505    * [foo] => lorem ipsum
   505    * [foo] => lorem ipsum
   506    * [bar] => dolor sit amet
   506    * [bar] => dolor sit amet
   507    */
   507    */
   508   
   508   
   509   function parse_template_vars($input)
   509   public static function parse_template_vars($input)
   510   {
   510   {
   511     if ( !preg_match('/^(\|[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?))*$/is', trim($input)) )
   511     if ( !preg_match('/^(\|[ ]*([A-z0-9_]+)([ ]*)=([ ]*)(.+?))*$/is', trim($input)) )
   512     {
   512     {
   513       $using_pipes = false;
   513       $using_pipes = false;
   514       $input = explode("\n", trim( $input ));
   514       $input = explode("\n", trim( $input ));
   571      }}';
   571      }}';
   572    $text = RenderMan::include_templates($text);
   572    $text = RenderMan::include_templates($text);
   573    * </code>
   573    * </code>
   574    */
   574    */
   575   
   575   
   576   function include_templates($text)
   576   public static function include_templates($text)
   577   {
   577   {
   578     global $db, $session, $paths, $template, $plugins; // Common objects
   578     global $db, $session, $paths, $template, $plugins; // Common objects
   579     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   579     // $template_regex = "/\{\{([^\]]+?)((\n([ ]*?)[A-z0-9]+([ ]*?)=([ ]*?)(.+?))*)\}\}/is";
   580     $template_regex = "/\{\{(.+)(((\n|[ ]*\|)[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   580     $template_regex = "/\{\{(.+)(((\n|[ ]*\|)[ ]*([A-z0-9]+)[ ]*=[ ]*(.+))*)\}\}/isU";
   581     if ( $count = preg_match_all($template_regex, $text, $matches) )
   581     if ( $count = preg_match_all($template_regex, $text, $matches) )
   610   /**
   610   /**
   611    * Preprocesses an HTML text string prior to being sent to MySQL.
   611    * Preprocesses an HTML text string prior to being sent to MySQL.
   612    * @param string $text
   612    * @param string $text
   613    * @param bool $strip_all_php - if true, strips all PHP regardless of user permissions. Else, strips PHP only if user level < USER_LEVEL_ADMIN.
   613    * @param bool $strip_all_php - if true, strips all PHP regardless of user permissions. Else, strips PHP only if user level < USER_LEVEL_ADMIN.
   614    */
   614    */
   615   function preprocess_text($text, $strip_all_php = true, $sqlescape = true)
   615   public static function preprocess_text($text, $strip_all_php = true, $sqlescape = true)
   616   {
   616   {
   617     global $db, $session, $paths, $template, $plugins; // Common objects
   617     global $db, $session, $paths, $template, $plugins; // Common objects
   618     $random_id = md5( time() . mt_rand() );
   618     $random_id = md5( time() . mt_rand() );
   619     
   619     
   620     $can_do_php = ( $session->get_permissions('php_in_pages') && !$strip_all_php );
   620     $can_do_php = ( $session->get_permissions('php_in_pages') && !$strip_all_php );
   665     $text = ( $sqlescape ) ? $db->escape($text) : $text;
   665     $text = ( $sqlescape ) ? $db->escape($text) : $text;
   666     
   666     
   667     return $text;
   667     return $text;
   668   }
   668   }
   669   
   669   
   670   function smilieyize($text, $complete_urls = false)
   670   public static function smilieyize($text, $complete_urls = false)
   671   {
   671   {
   672     
   672     
   673     $random_id = md5( time() . mt_rand() );
   673     $random_id = md5( time() . mt_rand() );
   674     
   674     
   675     // Smileys array - eventually this will be fetched from the database by
   675     // Smileys array - eventually this will be fetched from the database by
   765    * Replaces some critical characters in a string with MySQL-safe equivalents
   765    * Replaces some critical characters in a string with MySQL-safe equivalents
   766    * @param $text string the text to escape
   766    * @param $text string the text to escape
   767    * @return array key 0 is the escaped text, key 1 is the character tag
   767    * @return array key 0 is the escaped text, key 1 is the character tag
   768    * /
   768    * /
   769    
   769    
   770   function escape_page_text($text)
   770   public static function escape_page_text($text)
   771   {
   771   {
   772     $char_tag = md5(microtime() . mt_rand());
   772     $char_tag = md5(microtime() . mt_rand());
   773     $text = str_replace("'",  "{APOS:$char_tag}",  $text);
   773     $text = str_replace("'",  "{APOS:$char_tag}",  $text);
   774     $text = str_replace('"',  "{QUOT:$char_tag}",  $text);
   774     $text = str_replace('"',  "{QUOT:$char_tag}",  $text);
   775     $text = str_replace("\\", "{SLASH:$char_tag}", $text);
   775     $text = str_replace("\\", "{SLASH:$char_tag}", $text);
   782    * @param $text string the text to unescape
   782    * @param $text string the text to unescape
   783    * @param $char_tag string the character tag
   783    * @param $char_tag string the character tag
   784    * @return string
   784    * @return string
   785    * /
   785    * /
   786    
   786    
   787   function unescape_page_text($text, $char_tag)
   787   public static function unescape_page_text($text, $char_tag)
   788   {
   788   {
   789     $text = str_replace("{APOS:$char_tag}",  "'",  $text);
   789     $text = str_replace("{APOS:$char_tag}",  "'",  $text);
   790     $text = str_replace("{QUOT:$char_tag}",  '"',  $text);
   790     $text = str_replace("{QUOT:$char_tag}",  '"',  $text);
   791     $text = str_replace("{SLASH:$char_tag}", "\\", $text);
   791     $text = str_replace("{SLASH:$char_tag}", "\\", $text);
   792     return $text;
   792     return $text;
   797    * Generates a summary of the differences between two texts, and formats it as XHTML.
   797    * Generates a summary of the differences between two texts, and formats it as XHTML.
   798    * @param $str1 string the first block of text
   798    * @param $str1 string the first block of text
   799    * @param $str2 string the second block of text
   799    * @param $str2 string the second block of text
   800    * @return string
   800    * @return string
   801    */
   801    */
   802   function diff($str1, $str2)
   802   public static function diff($str1, $str2)
   803   {
   803   {
   804     global $db, $session, $paths, $template, $plugins; // Common objects
   804     global $db, $session, $paths, $template, $plugins; // Common objects
   805     $str1 = explode("\n", $str1);
   805     $str1 = explode("\n", $str1);
   806     $str2 = explode("\n", $str2);
   806     $str2 = explode("\n", $str2);
   807     $diff = new Diff($str1, $str2);
   807     $diff = new Diff($str1, $str2);
   814    * @param string The wikitext to process
   814    * @param string The wikitext to process
   815    * @param array Will be overwritten with the list of HTML tags (the system uses tokens for TextWiki compatibility)
   815    * @param array Will be overwritten with the list of HTML tags (the system uses tokens for TextWiki compatibility)
   816    * @return string
   816    * @return string
   817    */
   817    */
   818   
   818   
   819   function process_image_tags($text, &$taglist)
   819   public static function process_image_tags($text, &$taglist)
   820   {
   820   {
   821     global $db, $session, $paths, $template, $plugins; // Common objects
   821     global $db, $session, $paths, $template, $plugins; // Common objects
   822     
   822     
   823     $s_delim = "\xFF";
   823     $s_delim = "\xFF";
   824     $f_delim = "\xFF";
   824     $f_delim = "\xFF";
   966    * Finalizes processing of image tags.
   966    * Finalizes processing of image tags.
   967    * @param string The preprocessed text
   967    * @param string The preprocessed text
   968    * @param array The list of image tags created by RenderMan::process_image_tags()
   968    * @param array The list of image tags created by RenderMan::process_image_tags()
   969    */
   969    */
   970    
   970    
   971   function process_imgtags_stage2($text, $taglist)
   971   public static function process_imgtags_stage2($text, $taglist)
   972   {
   972   {
   973     $s_delim = "\xFF";
   973     $s_delim = "\xFF";
   974     $f_delim = "\xFF";
   974     $f_delim = "\xFF";
   975     foreach ( $taglist as $i => $tag )
   975     foreach ( $taglist as $i => $tag )
   976     {
   976     {