1<?php
2/**
3 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (GPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/gpl.
7 *
8 * @category  Horde
9 * @copyright 2010-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * A Horde_Injector based Horde_Mime_Viewer factory for IMP drivers.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2010-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/gpl GPL
21 * @package   IMP
22 */
23class IMP_Factory_MimeViewer extends Horde_Core_Factory_MimeViewer
24{
25    /**
26     * Temporary storage for IMP_Contents object.
27     *
28     * @var IMP_Contents
29     */
30    private $_contents;
31
32    /**
33     * Instances.
34     *
35     * @var array
36     */
37    private $_instances = array();
38
39    /**
40     * Attempts to return a concrete Horde_Mime_Viewer object based on the
41     * MIME type.
42     *
43     * @param Horde_Mime_Part $mime  An object with the data to be rendered.
44     * @param array $opts            Additional options:
45     * <pre>
46     *   - contents: (IMP_Contents) Object associated with $mime.
47     *   - type: (string) The MIME type to use for loading.
48     * </pre>
49     *
50     * @return Horde_Mime_Viewer_Base  The newly created instance.
51     * @throws Horde_Mime_Viewer_Exception
52     */
53    public function create(Horde_Mime_Part $mime, array $opts = array())
54    {
55        $opts = array_merge(array(
56            'contents' => null,
57            'type' => null
58        ), $opts);
59
60        $sig = implode('|', array(
61            spl_object_hash($mime),
62            $opts['contents'] ? spl_object_hash($opts['contents']) : '',
63            strval($opts['type'])
64        ));
65
66        if (!isset($this->_instances[$sig])) {
67            $this->_contents = $opts['contents'];
68            $this->_instances[$sig] = parent::create($mime, array_filter(array(
69                'app' => 'imp',
70                'type' => $opts['type']
71            )));
72            unset($this->_contents);
73        }
74
75        return $this->_instances[$sig];
76    }
77
78    /**
79     * Callback used to return a MIME Viewer object from within certain
80     * Viewer drivers.
81     *
82     * @param Horde_Mime_Viewer_Base $viewer  The MIME Viewer driver
83     *                                        requesting the new object.
84     * @param Horde_Mime_Part $mime           An object with the data to be
85     *                                        rendered.
86     * @param string $type                    The MIME type to use for
87     *                                        rendering.
88     *
89     * @return Horde_Mime_Viewer_Base  The newly created instance.
90     * @throws Horde_Mime_Viewer_Exception
91     */
92    public function createCallback(Horde_Mime_Viewer_Base $viewer,
93                                   Horde_Mime_Part $mime, $type)
94    {
95        return $this->create($mime, array(
96            'contents' => $viewer->getConfigParam('imp_contents'),
97            'type' => $type
98        ));
99    }
100
101    /**
102     */
103    public function getViewerConfig($type, $app)
104    {
105        list($driver, $params) = parent::getViewerConfig($type, $app);
106
107        switch ($driver) {
108        case 'Horde_Mime_Viewer_Report':
109        case 'Horde_Mime_Viewer_Security':
110        case 'Report':
111        case 'Security':
112            $params['viewer_callback'] = array($this, 'createCallback');
113            break;
114        }
115
116        $params['imp_contents'] = $this->_contents;
117        $params['type'] = $type;
118
119        return array($driver, $params);
120    }
121
122}
123