File: images.php

package info (click to toggle)
imp4 4.2-4lenny3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,252 kB
  • ctags: 5,316
  • sloc: php: 21,340; xml: 19,302; makefile: 68; sql: 14
file content (283 lines) | stat: -rw-r--r-- 9,809 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php

require_once 'Horde/MIME/Viewer/images.php';

/**
 * The IMP_MIME_Viewer_images class allows images to be displayed
 * inline in a message.
 *
 * $Horde: imp/lib/MIME/Viewer/images.php,v 1.43.10.22 2008/01/02 11:31:37 jan Exp $
 *
 * Copyright 2002-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (GPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @author  Michael Slusarz <slusarz@horde.org>
 * @package Horde_MIME_Viewer
 */
class IMP_MIME_Viewer_images extends MIME_Viewer_images {

    /**
     * The content-type of the generated data.
     *
     * @var string
     */
    var $_contentType;

    /**
     * Render out the currently set contents.
     *
     * @param array $params  An array with a reference to a MIME_Contents
     *                       object.
     *
     * @return string  The rendered information.
     */
    function render($params)
    {
        $contents = $params[0];

        global $browser;

        /* If calling page is asking us to output data, do that without any
         * further delay and exit. */
        if (Util::getFormData('img_data')) {
            $this->_contentType = parent::getType();
            return parent::render();
        }

        /* Convert the image to browser-viewable format and display. */
        if (Util::getFormData('images_view_convert')) {
            return $this->_viewConvert();
        }

        /* Create the thumbnail and display. */
        if (Util::getFormData('images_view_thumbnail')) {
            return $this->_viewConvert(true);
        }

        if (Util::getFormData('images_load_convert')) {
            return $this->_popupImageWindow();
        }

        if ($contents->viewAsAttachment()) {
            if (!$browser->hasFeature('javascript') ||
                !$contents->viewAsAttachment(true)) {
                /* If either:
                   + The browser doesn't support javascript
                   + We are not viewing in a popup window
                   Then simply render the image data. */
                $this->_contentType = parent::getType();
                return parent::render();
            } elseif ($browser->isViewable(parent::getType())) {
                /* The browser can display the image type directly - just
                   output the javascript code to render the auto resize popup
                   image window. */
                return $this->_popupImageWindow();
            }
        }

        if ($browser->isViewable($this->mime_part->getType())) {
            /* If we are viewing inline, and the browser can handle the image
               type directly, output an <img> tag to load the image. */
            $alt = $this->mime_part->getName(false, true);
            return Horde::img($contents->urlView($this->mime_part, 'view_attach'), $alt, null, '');
        } else {
            /* If we have made it this far, than the browser cannot view this
               image inline.  Inform the user of this and, possibly, ask user
               if we should convert to another image type. */
            $msg = _("Your browser does not support inline display of this image type.");

            if ($contents->viewAsAttachment()) {
                $msg .= '<br />' . sprintf(_("Click %s to download the image."), $contents->linkView($this->mime_part, 'download_attach', _("HERE"), array('viewparams' => array('img_data' => 1)), true));
            }

            /* See if we can convert to an inline browser viewable form. */
            $img = $this->_getHordeImageOb(false);
            if ($img && $browser->isViewable($img->getContentType())) {
                if ($contents->viewAsAttachment()) {
                    $convert_link = Horde::link($contents->urlView($this->mime_part, 'view_attach', array('images_load_convert' => 1, 'popup_view' => 1))) . _("HERE") . '</a>';
                } else {
                    $convert_link = $contents->linkViewJS($this->mime_part, 'view_attach', _("HERE"), null, null, array('images_load_convert' => 1));
                }
                $msg .= '<br />' . sprintf(_("Click %s to convert the image file into a format your browser can view."), $convert_link);
            }

            return $contents->formatStatusMsg($msg);
        }
    }

    /**
     * Return the content-type
     *
     * @return string  The content-type of the output.
     */
    function getType()
    {
        if ($this->_contentType) {
            return $this->_contentType;
        } else {
            return 'text/html; charset=' . NLS::getCharset();
        }
    }

    /**
     * Render out attachment information.
     *
     * @param array $params  An array with a reference to a MIME_Contents
     *                       object.
     *
     * @return string  The rendered text in HTML.
     */
    function renderAttachmentInfo($params)
    {
        $contents = &$params[0];

        /* Display the thumbnail link only if we show thumbs for all images or
           if image is over 50 KB. */
        if (!$this->getConfigParam('allthumbs') &&
            ($this->mime_part->getBytes() < 51200)) {
            return '';
        }

        if (is_a($contents, 'IMP_Contents')) {
            $this->mime_part = &$contents->getDecodedMIMEPart($this->mime_part->getMIMEId(), true);
        }

        /* Check to see if convert utility is available. */
        if (!$this->_getHordeImageOb(false)) {
            return '';
        }

        $status = array(
            sprintf(_("An image named %s is attached to this message. A thumbnail is below."),
                    $this->mime_part->getName(true)),
        );

        if (!$GLOBALS['browser']->hasFeature('javascript')) {
            $status[] = Horde::link($contents->urlView($this->mime_part,
                            'view_attach', array('popup_view' => 1))) .
                        Horde::img($contents->urlView($this->mime_part,
                            'view_attach', array('images_view_thumbnail' => 1), false),
                            _("View Attachment"), null, '') . '</a>';
        } else {
            $status[] = $contents->linkViewJS($this->mime_part, 'view_attach',
                        Horde::img($contents->urlView($this->mime_part,
                            'view_attach', array('images_view_thumbnail' => 1),
                            false), _("View Attachment"), null, ''), null, null,
                            null);
        }

        return $contents->formatStatusMsg($status, Horde::img('mime/image.png',
                    _("Thumbnail of attached image"), null, $GLOBALS['registry']->getImageDir('horde')), false);
    }

    /**
     * Generate the HTML output for the JS auto-resize view window.
     *
     * @access private
     *
     * @return string  The HTML output.
     */
    function _popupImageWindow()
    {
        $params = $remove_params = array();
        if (Util::getFormData('images_load_convert')) {
            $params['images_view_convert'] = 1;
            $remove_params[] = 'images_load_convert';
        } else {
            $params['img_data'] = 1;
        }
        $self_url = Util::addParameter(Util::removeParameter(Horde::selfUrl(true), $remove_params), $params);
        $title = MIME::decode($this->mime_part->getName(false, true));
        return parent::_popupImageWindow($self_url, $title);
    }

    /**
     * View thumbnail sized image.
     *
     * @access private
     *
     * @param boolean $thumb  View thumbnail size?
     *
     * @return string  The image data.
     */
    function _viewConvert($thumb = false)
    {
        $mime = $this->mime_part;
        $img = $this->_getHordeImageOb();

        if ($img) {
            if ($thumb) {
                $img->resize(96, 96, true);
            }
            $type = $img->getContentType();
            $data = $img->raw(true);
        }

        if (!$img || !$data) {
            $type = 'image/png';
            $data = file_get_contents(IMP_BASE . '/themes/graphics/mini-error.png');
        }

        $mime->setType($type);
        $this->_contentType = $type;
        $mime->setContents($data);

        return $mime->getContents();
    }

    /**
     * Return a Horde_Image object.
     *
     * @access private
     *
     * @param boolean $load  Whether to load the image data.
     *
     * @return Horde_Image  The requested object.
     */
    function _getHordeImageOb($load = true)
    {
        include_once 'Horde/Image.php';
        $params = array('temp' => Horde::getTempdir());
        if (!empty($GLOBALS['conf']['image']['convert'])) {
            $img = &Horde_Image::singleton('im', $params);
        } elseif (Util::extensionExists('gd')) {
            $img = &Horde_Image::singleton('gd', $params);
        } else {
            return false;
        }

        if (is_a($img, 'PEAR_Error')) {
            return false;
        }

        if ($load) {
            $ret = $img->loadString(1, $this->mime_part->getContents());
            if (is_a($ret, 'PEAR_Error')) {
                return false;
            }
        }

        return $img;
    }

    /**
    * Can this driver render the the data inline?
    *
    * @return boolean  True if the driver can display inline.
    */
    function canDisplayInline()
    {
        /* Only display the image inline if the configuration parameter is set,
           the browser can actually display it, and the size of the image is
           small. */
        global $browser;
        if (($this->getConfigParam('inline')) && ($browser->isViewable($this->mime_part->getType())) &&
            ($this->mime_part->getBytes() < 51200)) {
            return true;
        } else {
            return false;
        }
    }
}