~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/MIME/Viewer.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
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.
 
7
 *
 
8
 * $Horde: framework/MIME/MIME/Viewer.php,v 1.64.10.1 2005/01/03 12:19:05 jan Exp $
 
9
 *
 
10
 * Copyright 1999-2005 Anil Madhavapeddy <anil@recoil.org>
 
11
 *
 
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.
 
14
 *
 
15
 * @author  Anil Madhavapeddy <anil@recoil.org>
 
16
 * @version $Revision: 1.64.10.1 $
 
17
 * @since   Horde 1.3
 
18
 * @package Horde_MIME_Viewer
 
19
 */
 
20
class MIME_Viewer {
 
21
 
 
22
    /**
 
23
     * The MIME_Part object to render.
 
24
     *
 
25
     * @var object MIME_Part $mime_part
 
26
     */
 
27
    var $mime_part;
 
28
 
 
29
    /**
 
30
     * Configuration parameters.
 
31
     *
 
32
     * @var array $_conf
 
33
     */
 
34
    var $_conf = array();
 
35
 
 
36
    /**
 
37
     * getDriver cache.
 
38
     *
 
39
     * @var array $_driverCache
 
40
     */
 
41
    var $_driverCache = array();
 
42
 
 
43
    /**
 
44
     * Attempts to return a concrete MIME_Viewer_* object based on the
 
45
     * type of MIME_Part passed onto it.
 
46
     *
 
47
     * @param object MIME_Part &$mime_part  Reference to a MIME_Part object
 
48
     *                                      with the information to be
 
49
     *                                      rendered.
 
50
     * @param optional string $mime_type    Use this MIME type instead of the
 
51
     *                                      type stored in the $mime_part.
 
52
     *
 
53
     * @return object MIME_Viewer  The MIME_Viewer object.
 
54
     *                             Returns false on error.
 
55
     */
 
56
    function &factory(&$mime_part, $mime_type = null)
 
57
    {
 
58
        /* Check that we have a valid MIME_Part object */
 
59
        if (!is_a($mime_part, 'MIME_Part')) {
 
60
            return false;
 
61
        }
 
62
 
 
63
        /* Determine driver type from the MIME type */
 
64
        if (empty($mime_type)) {
 
65
            $mime_type = $mime_part->getType();
 
66
            if (empty($mime_type)) {
 
67
                return false;
 
68
            }
 
69
        }
 
70
 
 
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]);
 
76
            }
 
77
        }
 
78
 
 
79
        return false;
 
80
    }
 
81
 
 
82
    /**
 
83
     * Include the code for the relevant driver.
 
84
     *
 
85
     * @access public
 
86
     *
 
87
     * @param string $mime_type  The Content-type of the part to be rendered.
 
88
     *
 
89
     * @return object stdClass  See MIME_Driver::getDriver().
 
90
     */
 
91
    function includeDriver($mime_type)
 
92
    {
 
93
        global $registry;
 
94
 
 
95
        /* Figure the correct driver for this MIME type. If there is no
 
96
           application-specific module, a general Horde one will attempt to
 
97
           be used. */
 
98
        if (($ob = MIME_Viewer::getDriver($mime_type, $registry->getApp()))) {
 
99
            /* Include the class. */
 
100
            require_once MIME_Viewer::resolveDriver($ob->driver, $ob->module);
 
101
        }
 
102
 
 
103
        return $ob;
 
104
    }
 
105
 
 
106
    /**
 
107
     * Constructor for MIME_Viewer
 
108
     *
 
109
     * @access public
 
110
     *
 
111
     * @param object MIME_Part &$mime_part  Reference to a MIME_Part object
 
112
     *                                      with the information to be rendered
 
113
     */
 
114
    function MIME_Viewer(&$mime_part, $conf = array())
 
115
    {
 
116
        $this->mime_part = &$mime_part;
 
117
        $this->_conf = $conf;
 
118
    }
 
119
 
 
120
    /**
 
121
     * Sets the MIME_Part object for the class.
 
122
     *
 
123
     * @access public
 
124
     *
 
125
     * @param object MIME_Part &$mime_part  Reference to a MIME_Part object
 
126
     *                                      with the information to be rendered
 
127
     */
 
128
    function setMIMEPart(&$mime_part)
 
129
    {
 
130
        $this->mime_part = &$mime_part;
 
131
    }
 
132
 
 
133
    /**
 
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.
 
139
     *
 
140
     * @access public
 
141
     *
 
142
     * @return string  MIME-type of the output content.
 
143
     */
 
144
    function getType()
 
145
    {
 
146
        if ($this->mime_part->getPrimaryType() == 'x-extension') {
 
147
            return 'text/plain';
 
148
        } else {
 
149
            return $this->mime_part->getType(true);
 
150
        }
 
151
    }
 
152
 
 
153
    /**
 
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.
 
158
     *
 
159
     * @access public
 
160
     *
 
161
     * @param mixed $params  Any optional parameters this driver needs at
 
162
     *                       runtime.
 
163
     *
 
164
     * @return string  Rendered version of the object.
 
165
     */
 
166
    function render($params = null)
 
167
    {
 
168
        return $this->mime_part->getContents();
 
169
    }
 
170
 
 
171
    /**
 
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
 
176
     * the attachment.
 
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.
 
180
     *
 
181
     * @access public
 
182
     *
 
183
     * @param mixed $params  Any optional parameters this driver needs at
 
184
     *                       runtime.
 
185
     *
 
186
     * @return string  Text/html rendered information.
 
187
     */
 
188
    function renderAttachmentInfo()
 
189
    {
 
190
    }
 
191
 
 
192
    /**
 
193
     * Can this driver render the the data inline?
 
194
     *
 
195
     * @access public
 
196
     *
 
197
     * @return boolean  True if the driver can display inline.
 
198
     */
 
199
    function canDisplayInline()
 
200
    {
 
201
        if ($this->getConfigParam('inline')) {
 
202
            return true;
 
203
        } else {
 
204
            return false;
 
205
        }
 
206
    }
 
207
 
 
208
    /**
 
209
     * Given a driver and an application, this returns the fully
 
210
     * qualified filesystem path to the driver source file.
 
211
     *
 
212
     * @access public
 
213
     *
 
214
     * @param optional string $driver  Driver name.
 
215
     * @param optional string $app     Application name.
 
216
     *
 
217
     * @return string  Filesystem path of the driver/application queried.
 
218
     */
 
219
    function resolveDriver($driver = 'default', $app = 'horde')
 
220
    {
 
221
        if ($app == 'horde') {
 
222
            return dirname(__FILE__) . '/Viewer/' . $driver . '.php';
 
223
        } else {
 
224
            return $GLOBALS['registry']->applications[$app]['fileroot'] . '/lib/MIME/Viewer/' . $driver . '.php';
 
225
        }
 
226
    }
 
227
 
 
228
    /**
 
229
     * Given an input MIME type and a module name, this function
 
230
     * resolves it into a specific output driver which can handle it.
 
231
     *
 
232
     * @param string $mimeType  MIME type to resolve.
 
233
     * @param string $module    Module in which to search for the driver.
 
234
     *
 
235
     * @return object stdClass  Object with the following items:
 
236
     * <pre>
 
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')
 
240
     * </pre>
 
241
     * Returns false if driver could not be found.
 
242
     */
 
243
    function getDriver($mimeType, $module = 'horde')
 
244
    {
 
245
        global $mime_drivers, $mime_drivers_map;
 
246
 
 
247
        $cacheName = $mimeType . '|' . $module;
 
248
        if (isset($this) && isset($this->_driverCache[$cacheName])) {
 
249
            return $this->_driverCache[$cacheName];
 
250
        }
 
251
 
 
252
        $driver = '';
 
253
        $exactDriver = false;
 
254
 
 
255
        list($primary_type, ) = explode('/', $mimeType, 2);
 
256
        $allSub = $primary_type . '/*';
 
257
 
 
258
        /* If the module doesn't exist in $mime_drivers_map, check for
 
259
           Horde viewers. */
 
260
        if (!isset($mime_drivers_map[$module]) && $module != 'horde') {
 
261
            return MIME_Viewer::getDriver($mimeType, 'horde');
 
262
        }
 
263
 
 
264
        $dr = &$mime_drivers[$module];
 
265
        $map = &$mime_drivers_map[$module];
 
266
 
 
267
        /* If an override exists for this MIME type, then use that */
 
268
        if (isset($map['overrides'][$mimeType])) {
 
269
            $driver = $map['overrides'][$mimeType];
 
270
            $exactDriver = true;
 
271
        } elseif (isset($map['overrides'][$allSub])) {
 
272
            $driver = $map['overrides'][$allSub];
 
273
            $exactDriver = true;
 
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'])) {
 
282
                    $driver = $val;
 
283
                    $exactDriver = true;
 
284
                    break;
 
285
                } elseif (in_array($allSub, $dr[$val]['handles'])) {
 
286
                    $driver = $val;
 
287
                }
 
288
            }
 
289
        }
 
290
 
 
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;
 
300
                $module = 'horde';
 
301
            }
 
302
        }
 
303
 
 
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))) {
 
307
            $driver = 'default';
 
308
        }
 
309
 
 
310
        if (empty($driver)) {
 
311
            $this->_driverCache[$cacheName] = false;
 
312
            return false;
 
313
        } else {
 
314
            $ob = new stdClass;
 
315
            $ob->driver = $driver;
 
316
            $ob->exact  = $exactDriver;
 
317
            $ob->module = $module;
 
318
            if (isset($this)) {
 
319
                $this->_driverCache[$cacheName] = $ob;
 
320
            }
 
321
            return $ob;
 
322
        }
 
323
    }
 
324
 
 
325
    /**
 
326
     * Given a MIME type, this function will return an appropriate
 
327
     * icon.
 
328
     *
 
329
     * @access public
 
330
     *
 
331
     * @param string $mimeType  The MIME type that we need an icon for.
 
332
     *
 
333
     * @return string  The URL to the appropriate icon.
 
334
     */
 
335
    function getIcon($mimeType)
 
336
    {
 
337
        $app = $GLOBALS['registry']->getApp();
 
338
        $ob = MIME_Viewer::_getIcon($mimeType, $app);
 
339
 
 
340
        if (!isset($ob) && ($app != 'horde')) {
 
341
            $obHorde = MIME_Viewer::_getIcon($mimeType, 'horde');
 
342
            $icon = $obHorde->url;
 
343
        } elseif (!isset($ob)) {
 
344
            return null;
 
345
        } elseif (($ob->match !== 0) && ($app != 'horde')) {
 
346
            $obHorde = MIME_Viewer::_getIcon($mimeType, 'horde');
 
347
            if (!is_null($ob->match) && ($ob->match <= $obHorde->match)) {
 
348
                $icon = $ob->url;
 
349
            } else {
 
350
                $icon = $obHorde->url;
 
351
            }
 
352
        } else {
 
353
            $icon = $ob->url;
 
354
        }
 
355
 
 
356
        return $icon;
 
357
    }
 
358
 
 
359
    /**
 
360
     * Given an input MIME type and module, this function returns the
 
361
     * URL of an icon that can be associated with it
 
362
     *
 
363
     * @access private
 
364
     *
 
365
     * @param string $mimeType  MIME type to get the icon for.
 
366
     *
 
367
     * @return object stdClass  url:   URL to an icon, or null if none
 
368
     *                                 could be found.
 
369
     *                          exact: How exact the match is.
 
370
     *                                 0 => 'exact', 1 => 'primary',
 
371
     *                                 2 => 'driver', 3 => 'default'
 
372
     *                                 or null.
 
373
     */
 
374
    function _getIcon($mimeType, $module = 'horde')
 
375
    {
 
376
        global $mime_drivers;
 
377
 
 
378
        $ob = MIME_Viewer::getDriver($mimeType, $module);
 
379
        if (!is_object($ob)) {
 
380
            return array(false, null);
 
381
        }
 
382
        $driver = $ob->driver;
 
383
 
 
384
        list($primary_type,) = explode('/', $mimeType, 2);
 
385
        $allSub = $primary_type . '/*';
 
386
        $retOb = &new stdClass();
 
387
        $retOb->match = null;
 
388
        $retOb->url = null;
 
389
 
 
390
        /* If the module doesn't exist in $mime_drivers, return now. */
 
391
        if (!isset($mime_drivers[$module])) {
 
392
            return null;
 
393
        }
 
394
        $dr = &$mime_drivers[$module];
 
395
 
 
396
        /* If a specific icon for this driver and mimetype is defined,
 
397
           then use that. */
 
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;
 
405
                    break;
 
406
                }
 
407
            }
 
408
        }
 
409
 
 
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'];
 
415
                $retOb->match = 3;
 
416
            }
 
417
        }
 
418
 
 
419
        if (!is_null($retOb->url)) {
 
420
            $retOb->url = $GLOBALS['registry']->getImageDir($module) . '/mime/' . $retOb->url;
 
421
        }
 
422
 
 
423
        return $retOb;
 
424
    }
 
425
 
 
426
    /**
 
427
     * Returns the character set used for the Viewer.
 
428
     * Should be overridden by individual drivers to perform custom tasks.
 
429
     *
 
430
     * @access public
 
431
     *
 
432
     * @return string  The character set used by this Viewer.
 
433
     */
 
434
    function getCharset()
 
435
    {
 
436
        return $this->mime_part->getCharset();
 
437
    }
 
438
 
 
439
    /**
 
440
     * Return a configuration parameter for the current viewer.
 
441
     *
 
442
     * @access public
 
443
     *
 
444
     * @param string $param  The parameter name.
 
445
     *
 
446
     * @return mixed  The value of the parameter; returns null if the parameter
 
447
     *                doesn't exist.
 
448
     */
 
449
    function getConfigParam($param)
 
450
    {
 
451
        return (isset($this->_conf[$param])) ? $this->_conf[$param] : null;
 
452
    }
 
453
 
 
454
}