1 2 3 4 5 6 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
<?php
require_once dirname(__FILE__) . '/source.php';
/**
* The MIME_Viewer_enscript class renders out various content
* in HTML format by using GNU Enscript.
*
* $Horde: framework/MIME/MIME/Viewer/enscript.php,v 1.39.10.8 2006/06/01 03:08:53 chuck Exp $
*
* Copyright 1999-2006 Anil Madhavapeddy <anil@recoil.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Anil Madhavapeddy <anil@recoil.org>
* @since Horde 1.3
* @package Horde_MIME_Viewer
*/
class MIME_Viewer_enscript extends MIME_Viewer_source {
/**
* Render out the data using Enscript.
*
* @param array $params Any parameters the Viewer may need.
*
* @return string The rendered data.
*/
function render($params = array())
{
global $mime_drivers;
/* Check to make sure the program actually exists. */
if (!file_exists($mime_drivers['horde']['enscript']['location'])) {
return '<pre>' . sprintf(_("The program used to view this data type (%s) was not found on the system."), $mime_drivers['horde']['enscript']['location']) . '</pre>';
}
/* Create temporary files for input to Enscript. Note that we
cannot use a pipe, since enscript must have access to the
whole file to determine its type for coloured syntax
highlighting. */
$tmpin = Horde::getTempFile('EnscriptIn');
/* Write the contents of our buffer to the temporary input file. */
$contents = $this->mime_part->getContents();
$fh = fopen($tmpin, 'wb');
fwrite($fh, $contents, strlen($contents));
fclose($fh);
/* Execute the enscript command. */
$lang = escapeshellarg($this->_typeToLang($this->mime_part->getType()));
$results = shell_exec($mime_drivers['horde']['enscript']['location'] . " -E$lang --language=html --color --output=- < $tmpin");
/* Strip out the extraneous HTML from Enscript, and output it. */
$res_arr = preg_split('/\<\/?pre\>/i', $results);
if (count($res_arr) == 3) {
$results = trim($res_arr[1]);
}
/* Educated Guess at whether we are inline or not. */
if (headers_sent() || ob_get_length()) {
return $this->lineNumber($results);
} else {
return Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-header.inc') .
$this->lineNumber($results) .
Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-footer.inc');
}
}
/**
* Attempts to determine what language to use for the enscript program
* from a MIME type.
*
* @access private
*
* @param string $type The MIME type.
*
* @return string The enscript 'language' parameter string.
*/
function _typeToLang($type)
{
include_once dirname(__FILE__) . '/../Magic.php';
$ext = MIME_Magic::MIMEToExt($type);
switch ($ext) {
case 'cs':
return 'java';
case 'el':
return 'elisp';
case 'h':
return 'c';
case 'C':
case 'H':
case 'cc':
case 'hh':
case 'c++':
case 'cxx':
case 'cpp':
return 'cpp';
case 'htm':
case 'shtml':
case 'xml':
return 'html';
case 'js':
return 'javascript';
case 'pas':
return 'pascal';
case 'al':
case 'cgi':
case 'pl':
case 'pm':
return 'perl';
case 'ps':
return 'postscript';
case 'vb':
return 'vba';
case 'vhd':
return 'vhdl';
case 'patch':
case 'diff':
return 'diffu';
default:
return $ext;
}
}
}
|