1: <?php
2: /**
3: * Copyright 2013-2014 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 2013-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * Defines AJAX actions used to attach files to a compose message.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2013-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Ajax_Application_Handler_ComposeAttach extends Horde_Core_Ajax_Application_Handler
24: {
25: /**
26: * AJAX action: Add an attachment to a compose message.
27: *
28: * Variables used:
29: * - composeCache: (string) The IMP_Compose cache identifier.
30: * - file_id: (integer) Browser ID of file.
31: * - img_data: (boolean) If true, return image data.
32: * - json_return: (boolean) If true, returns JSON. Otherwise, JSON-HTML.
33: *
34: * @return object False on failure, or an object with the following
35: * properties:
36: * - action: (string) The action.
37: * - file_id: (integer) Browser ID of file.
38: * - img: (object) Image data, if 'img_data' is set. Properties:
39: * related, src
40: * - success: (integer) 1 on success (at least one successful attached
41: * file), 0 on failure.
42: */
43: public function addAttachment()
44: {
45: global $injector, $notification;
46:
47: $result = new stdClass;
48: $result->action = 'addAttachment';
49: if (isset($this->vars->file_id)) {
50: $result->file_id = $this->vars->file_id;
51: }
52: $result->success = 0;
53:
54: /* A max POST size failure will result in ALL HTTP parameters being
55: * empty. Catch that here. */
56: if (!isset($this->vars->composeCache)) {
57: $notification->push(_("Your attachment was not uploaded. Most likely, the file exceeded the maximum size allowed by the server configuration."), 'horde.warning');
58: } else {
59: $imp_compose = $injector->getInstance('IMP_Factory_Compose')->create($this->vars->composeCache);
60:
61: if ($imp_compose->canUploadAttachment()) {
62: try {
63: foreach ($imp_compose->addAttachmentFromUpload('file_upload') as $val) {
64: if ($val instanceof IMP_Compose_Exception) {
65: $notification->push($val, 'horde.error');
66: } else {
67: $result->success = 1;
68:
69: /* This currently only occurs when
70: * pasting/dropping image into HTML editor. */
71: if ($this->vars->img_data) {
72: $result->img = new stdClass;
73: $result->img->src = strval($val->viewUrl()->setRaw(true));
74:
75: $temp1 = new DOMDocument();
76: $temp2 = $temp1->createElement('span');
77: $imp_compose->addRelatedAttachment($val, $temp2, 'src');
78: $result->img->related = array(
79: $imp_compose::RELATED_ATTR,
80: $temp2->getAttribute($imp_compose::RELATED_ATTR)
81: );
82: } else {
83: $this->_base->queue->attachment($val);
84: $notification->push(sprintf(_("Added \"%s\" as an attachment."), $val->getPart()->getName()), 'horde.success');
85: }
86: }
87: }
88:
89: $this->_base->queue->compose($imp_compose);
90: } catch (IMP_Compose_Exception $e) {
91: $notification->push($e, 'horde.error');
92: }
93: } else {
94: $notification->push(_("Uploading attachments has been disabled on this server."), 'horde.error');
95: }
96: }
97:
98: return $this->vars->json_return
99: ? $result
100: : new Horde_Core_Ajax_Response_HordeCore_JsonHtml($result);
101: }
102:
103: }
104: