Update php Viewer.
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 04:36:02 +0000 (21:36 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 12 Nov 2008 04:36:02 +0000 (21:36 -0700)
framework/Mime/lib/Horde/Mime/Viewer/php.php

index 3113cce..090050c 100644 (file)
@@ -1,7 +1,4 @@
 <?php
-
-require_once dirname(__FILE__) . '/source.php';
-
 /**
  * The Horde_Mime_Viewer_php class renders out syntax-highlighted PHP code in
  * HTML format.
@@ -14,16 +11,41 @@ require_once dirname(__FILE__) . '/source.php';
  * @author  Chuck Hagenbuch <chuck@horde.org>
  * @package Horde_Mime_Viewer
  */
-class Horde_Mime_Viewer_php extends Horde_Mime_Viewer_source
+class Horde_Mime_Viewer_php extends Horde_Mime_Viewer_Driver
 {
     /**
-     * Renders out the contents.
+     * Can this driver render various views?
      *
-     * @param array $params  Any parameters the Viewer may need.
+     * @var boolean
+     */
+    protected $_capability = array(
+        'embedded' => false,
+        'full' => true,
+        'info' => false,
+        'inline' => true
+    );
+
+    /**
+     * Return the full rendered version of the Horde_Mime_Part object.
      *
-     * @return string  The rendered contents.
+     * @return array  See Horde_Mime_Viewer_Driver::render().
      */
-    public function render($params = array())
+    protected function _render()
+    {
+        $ret = $this->_renderInline();
+        $ret['data'] =  Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-header.inc') .
+            $ret['data'] .
+            Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-footer.inc');
+
+        return $ret;
+    }
+
+    /**
+     * Return the rendered inline version of the Horde_Mime_Part object.
+     *
+     * @return array  See Horde_Mime_Viewer_Driver::render().
+     */
+    protected function _renderInline()
     {
         ini_set('highlight.comment', 'comment');
         ini_set('highlight.default', 'default');
@@ -31,63 +53,52 @@ class Horde_Mime_Viewer_php extends Horde_Mime_Viewer_source
         ini_set('highlight.string', 'string');
         ini_set('highlight.html', 'html');
 
-        $code = $this->mime_part->getContents();
-        if (strpos($code, '<?php') === false) {
-            $results = $this->lineNumber(str_replace('&lt;?php&nbsp;', '', highlight_string('<?php ' . $code, true)));
-        } else {
-            $results = $this->lineNumber(highlight_string($code, true));
-        }
+        $code = $this->_mimepart->getContents();
+        $text = (strpos($code, '<?php') === false)
+            ? $this->_lineNumber(str_replace('&lt;?php&nbsp;', '', highlight_string('<?php ' . $code, true)))
+            : $this->_lineNumber(highlight_string($code, true));
 
-        // Educated guess at whether we are inline or not.
-        if (headers_sent() || ob_get_length()) {
-            return $results;
-        } else {
-            return Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-header.inc')
-                . $results
-                . Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-footer.inc');
-        }
+        return array(
+            'data' => $text,
+            'type' => 'text/html; charset=' . NLS::getCharset()
+        );
     }
 
     /**
      * Add line numbers to a block of code.
      *
      * @param string $code  The code to number.
+     *
+     * @return string  The code with line numbers added.
      */
-    public function lineNumber($code, $linebreak = "\n")
+    protected function _lineNumber($code, $linebreak = "\n")
     {
         // Clean up.
-        $code = preg_replace(array('/<code><span style="color: #?\w+">\s*/',
-                                   '/<code><font color="#?\w+">\s*/',
-                                   '/\s*<\/span>\s*<\/span>\s*<\/code>/',
-                                   '/\s*<\/font>\s*<\/font>\s*<\/code>/'),
-                             '',
-                             $code);
-        $code = str_replace(array('&nbsp;',
-                                  '&amp;',
-                                  '<br />',
-                                  '<span style="color: ',
-                                  '<font color="',
-                                  '</font>',
-                            ),
-                            array(' ',
-                                  '&#38;',
-                                  "\n",
-                                  '<span class="',
-                                  '<span class="',
-                                  '</span>',
-                            ),
-                            $code);
+        $code = preg_replace(
+            array(
+                '/<code><span style="color: #?\w+">\s*/',
+                '/<code><font color="#?\w+">\s*/',
+                '/\s*<\/span>\s*<\/span>\s*<\/code>/',
+                '/\s*<\/font>\s*<\/font>\s*<\/code>/'
+            ), '', $code);
+
+        $code = str_replace(
+            array('&nbsp;', '&amp;', '<br />', '<span style="color: ', '<font color="', '</font>',),
+            array(' ', '&#38;', "\n", '<span class="', '<span class="', '</span>',),
+            $code);
+
         $code = trim($code);
 
         // Normalize newlines.
         $code = str_replace("\r", '', $code);
         $code = preg_replace('/\n\n\n+/', "\n\n", $code);
 
-        $lines = explode("\n", $code);
-
         $results = array('<ol class="code-listing striped">');
         $previous = false;
-        foreach ($lines as $lineno => $line) {
+
+        $lines = explode("\n", $code);
+        reset($lines);
+        while (list($lineno, $line) = each($lines)) {
             if (substr($line, 0, 7) == '</span>') {
                 $previous = false;
                 $line = substr($line, 7);
@@ -133,6 +144,7 @@ class Horde_Mime_Viewer_php extends Horde_Mime_Viewer_source
         }
 
         $results[] = '</ol>';
+
         return implode("\n", $results);
     }
 }