3
* The MIME_Viewer:: class provides an abstracted interface to
4
* render out MIME types into HTML format. It depends on a
5
* set of MIME_Viewer_* drivers which handle the actual rendering,
6
* and also a configuration file to map MIME types to drivers.
8
* $Horde: framework/MIME/MIME/Viewer.php,v 1.64.10.1 2005/01/03 12:19:05 jan Exp $
10
* Copyright 1999-2005 Anil Madhavapeddy <anil@recoil.org>
12
* See the enclosed file COPYING for license information (LGPL). If you
13
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
15
* @author Anil Madhavapeddy <anil@recoil.org>
16
* @version $Revision: 1.64.10.1 $
18
* @package Horde_MIME_Viewer
23
* The MIME_Part object to render.
25
* @var object MIME_Part $mime_part
30
* Configuration parameters.
39
* @var array $_driverCache
41
var $_driverCache = array();
44
* Attempts to return a concrete MIME_Viewer_* object based on the
45
* type of MIME_Part passed onto it.
47
* @param object MIME_Part &$mime_part Reference to a MIME_Part object
48
* with the information to be
50
* @param optional string $mime_type Use this MIME type instead of the
51
* type stored in the $mime_part.
53
* @return object MIME_Viewer The MIME_Viewer object.
54
* Returns false on error.
56
function &factory(&$mime_part, $mime_type = null)
58
/* Check that we have a valid MIME_Part object */
59
if (!is_a($mime_part, 'MIME_Part')) {
63
/* Determine driver type from the MIME type */
64
if (empty($mime_type)) {
65
$mime_type = $mime_part->getType();
66
if (empty($mime_type)) {
71
/* Spawn the relevant driver, and return it (or false on failure) */
72
if (($ob = MIME_Viewer::includeDriver($mime_type))) {
73
$class = (($ob->module == 'horde') ? '' : $ob->module . '_') . 'MIME_Viewer_' . $ob->driver;
74
if (class_exists($class)) {
75
return $ret = &new $class($mime_part, $GLOBALS['mime_drivers'][$ob->module][$ob->driver]);
83
* Include the code for the relevant driver.
87
* @param string $mime_type The Content-type of the part to be rendered.
89
* @return object stdClass See MIME_Driver::getDriver().
91
function includeDriver($mime_type)
95
/* Figure the correct driver for this MIME type. If there is no
96
application-specific module, a general Horde one will attempt to
98
if (($ob = MIME_Viewer::getDriver($mime_type, $registry->getApp()))) {
99
/* Include the class. */
100
require_once MIME_Viewer::resolveDriver($ob->driver, $ob->module);
107
* Constructor for MIME_Viewer
111
* @param object MIME_Part &$mime_part Reference to a MIME_Part object
112
* with the information to be rendered
114
function MIME_Viewer(&$mime_part, $conf = array())
116
$this->mime_part = &$mime_part;
117
$this->_conf = $conf;
121
* Sets the MIME_Part object for the class.
125
* @param object MIME_Part &$mime_part Reference to a MIME_Part object
126
* with the information to be rendered
128
function setMIMEPart(&$mime_part)
130
$this->mime_part = &$mime_part;
134
* Return the MIME type of the rendered content. This can be
135
* overridden by the individual drivers, depending on what format
136
* they output in. By default, it passes through the MIME type of
137
* the object, or replaces custom extension types with
138
* 'text/plain' to let the browser do a best-guess render.
142
* @return string MIME-type of the output content.
146
if ($this->mime_part->getPrimaryType() == 'x-extension') {
149
return $this->mime_part->getType(true);
154
* Return the rendered version of the object.
155
* Should be overridden by individual drivers to perform custom tasks.
156
* The $mime_part class variable has the information to render,
157
* encapsulated in a MIME_Part object.
161
* @param mixed $params Any optional parameters this driver needs at
164
* @return string Rendered version of the object.
166
function render($params = null)
168
return $this->mime_part->getContents();
172
* Return text/html output used as alternative output when the fully
173
* rendered object can not (or should not) be displayed. For example,
174
* this function should be used for MIME attachments that can not be
175
* viewed inline, where the user may be given options on how to view
177
* Should be overridden by individual drivers to perform custom tasks.
178
* The $mime_part class variable has the information to render,
179
* encapsulated in a MIME_Part object.
183
* @param mixed $params Any optional parameters this driver needs at
186
* @return string Text/html rendered information.
188
function renderAttachmentInfo()
193
* Can this driver render the the data inline?
197
* @return boolean True if the driver can display inline.
199
function canDisplayInline()
201
if ($this->getConfigParam('inline')) {
209
* Given a driver and an application, this returns the fully
210
* qualified filesystem path to the driver source file.
214
* @param optional string $driver Driver name.
215
* @param optional string $app Application name.
217
* @return string Filesystem path of the driver/application queried.
219
function resolveDriver($driver = 'default', $app = 'horde')
221
if ($app == 'horde') {
222
return dirname(__FILE__) . '/Viewer/' . $driver . '.php';
224
return $GLOBALS['registry']->applications[$app]['fileroot'] . '/lib/MIME/Viewer/' . $driver . '.php';
229
* Given an input MIME type and a module name, this function
230
* resolves it into a specific output driver which can handle it.
232
* @param string $mimeType MIME type to resolve.
233
* @param string $module Module in which to search for the driver.
235
* @return object stdClass Object with the following items:
237
* 'driver' -- Name of driver (e.g. 'enscript')
238
* 'exact' -- Was the driver and exact match? (true/false)
239
* 'module' -- The module containing driver (e.g. 'horde')
241
* Returns false if driver could not be found.
243
function getDriver($mimeType, $module = 'horde')
245
global $mime_drivers, $mime_drivers_map;
247
$cacheName = $mimeType . '|' . $module;
248
if (isset($this) && isset($this->_driverCache[$cacheName])) {
249
return $this->_driverCache[$cacheName];
253
$exactDriver = false;
255
list($primary_type, ) = explode('/', $mimeType, 2);
256
$allSub = $primary_type . '/*';
258
/* If the module doesn't exist in $mime_drivers_map, check for
260
if (!isset($mime_drivers_map[$module]) && $module != 'horde') {
261
return MIME_Viewer::getDriver($mimeType, 'horde');
264
$dr = &$mime_drivers[$module];
265
$map = &$mime_drivers_map[$module];
267
/* If an override exists for this MIME type, then use that */
268
if (isset($map['overrides'][$mimeType])) {
269
$driver = $map['overrides'][$mimeType];
271
} elseif (isset($map['overrides'][$allSub])) {
272
$driver = $map['overrides'][$allSub];
274
} elseif (isset($map['registered'])) {
275
/* Iterate through the list of registered drivers, and see if
276
this MIME type exists in the MIME types that they claim to
277
handle. If the driver handles it, then assign it as the
278
rendering driver. If we find a generic handler, keep iterating
279
to see if we can find a specific handler. */
280
foreach ($map['registered'] as $val) {
281
if (in_array($mimeType, $dr[$val]['handles'])) {
285
} elseif (in_array($allSub, $dr[$val]['handles'])) {
291
/* If this is an application specific module, and an exact match was
292
not found, search for a Horde-wide specific driver. Only use the
293
Horde-specific driver if it is NOT the 'default' driver AND the
294
Horde driver is an exact match. */
295
if (!$exactDriver && $module != 'horde') {
296
$ob = MIME_Viewer::getDriver($mimeType, 'horde');
297
if (empty($driver) ||
298
(($ob->driver != 'default') && $ob->exact)) {
299
$driver = $ob->driver;
304
/* If the 'default' driver exists in this module, fall back to that. */
305
if (empty($driver) &&
306
@is_file(MIME_Viewer::resolveDriver('default', $module))) {
310
if (empty($driver)) {
311
$this->_driverCache[$cacheName] = false;
315
$ob->driver = $driver;
316
$ob->exact = $exactDriver;
317
$ob->module = $module;
319
$this->_driverCache[$cacheName] = $ob;
326
* Given a MIME type, this function will return an appropriate
331
* @param string $mimeType The MIME type that we need an icon for.
333
* @return string The URL to the appropriate icon.
335
function getIcon($mimeType)
337
$app = $GLOBALS['registry']->getApp();
338
$ob = MIME_Viewer::_getIcon($mimeType, $app);
340
if (!isset($ob) && ($app != 'horde')) {
341
$obHorde = MIME_Viewer::_getIcon($mimeType, 'horde');
342
$icon = $obHorde->url;
343
} elseif (!isset($ob)) {
345
} elseif (($ob->match !== 0) && ($app != 'horde')) {
346
$obHorde = MIME_Viewer::_getIcon($mimeType, 'horde');
347
if (!is_null($ob->match) && ($ob->match <= $obHorde->match)) {
350
$icon = $obHorde->url;
360
* Given an input MIME type and module, this function returns the
361
* URL of an icon that can be associated with it
365
* @param string $mimeType MIME type to get the icon for.
367
* @return object stdClass url: URL to an icon, or null if none
369
* exact: How exact the match is.
370
* 0 => 'exact', 1 => 'primary',
371
* 2 => 'driver', 3 => 'default'
374
function _getIcon($mimeType, $module = 'horde')
376
global $mime_drivers;
378
$ob = MIME_Viewer::getDriver($mimeType, $module);
379
if (!is_object($ob)) {
380
return array(false, null);
382
$driver = $ob->driver;
384
list($primary_type,) = explode('/', $mimeType, 2);
385
$allSub = $primary_type . '/*';
386
$retOb = &new stdClass();
387
$retOb->match = null;
390
/* If the module doesn't exist in $mime_drivers, return now. */
391
if (!isset($mime_drivers[$module])) {
394
$dr = &$mime_drivers[$module];
396
/* If a specific icon for this driver and mimetype is defined,
398
if (isset($dr[$driver]['icons'])) {
399
$icondr = &$mime_drivers[$module][$driver]['icons'];
400
$iconList = array($mimeType => 0, $allSub => 1, 'default' => 2);
401
foreach ($iconList as $key => $val) {
402
if (isset($icondr[$key])) {
403
$retOb->url = $icondr[$key];
404
$retOb->match = $val;
410
/* Try to use a default icon if none already obtained. */
411
if (is_null($retOb->url) && isset($dr['default'])) {
412
$dr = &$mime_drivers[$module]['default'];
413
if (isset($dr['icons']['default'])) {
414
$retOb->url = $dr['default']['icons']['default'];
419
if (!is_null($retOb->url)) {
420
$retOb->url = $GLOBALS['registry']->getImageDir($module) . '/mime/' . $retOb->url;
427
* Returns the character set used for the Viewer.
428
* Should be overridden by individual drivers to perform custom tasks.
432
* @return string The character set used by this Viewer.
434
function getCharset()
436
return $this->mime_part->getCharset();
440
* Return a configuration parameter for the current viewer.
444
* @param string $param The parameter name.
446
* @return mixed The value of the parameter; returns null if the parameter
449
function getConfigParam($param)
451
return (isset($this->_conf[$param])) ? $this->_conf[$param] : null;