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: * Attachment data for an outgoing 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: * @property-read boolean $linked Should this attachment be linked?
24: * @property-read Horde_Url $link_url The URL, if the attachment is linked.
25: * @property-read IMP_Compose_Attachment_Storage $storage The storage object.
26: * @property-read string $tmpfile The temporary file location on the local
27: * filesystem.
28: */
29: class IMP_Compose_Attachment implements Serializable
30: {
31: /**
32: * Force this attachment to be linked?
33: *
34: * @var boolean
35: */
36: public $forceLinked = false;
37:
38: /**
39: * Attachment ID.
40: *
41: * @var integer
42: */
43: public $id;
44:
45: /**
46: * Is this part associated with multipart/related data?
47: *
48: * @var boolean
49: */
50: public $related = false;
51:
52: /**
53: * Compose object cache ID.
54: *
55: * @var string
56: */
57: protected $_composeCache;
58:
59: /**
60: * Does the part contain the attachment contents?
61: *
62: * @var boolean
63: */
64: protected $_isBuilt = false;
65:
66: /**
67: * Should this attachment be linked?
68: *
69: * @var boolean
70: */
71: protected $_linked = null;
72:
73: /**
74: * MIME part object.
75: *
76: * @var Horde_Mime_Part
77: */
78: protected $_part;
79:
80: /**
81: * The unique identifier for the file.
82: *
83: * @var string
84: */
85: protected $_uuid = null;
86:
87: /**
88: * Constructor.
89: *
90: * @param IMP_Compose $ob Compose object.
91: * @param Horde_Mime_Part $part MIME part object.
92: * @param string $tmp_file Temporary filename containing the data.
93: */
94: public function __construct(IMP_Compose $ob, Horde_Mime_Part $part,
95: $tmp_file)
96: {
97: $this->id = ++$ob->atcId;
98: $this->_composeCache = strval($ob);
99: $this->_part = $part;
100: $this->_uuid = strval(new Horde_Support_Uuid());
101:
102: $storage = $this->storage;
103: $storage->write($tmp_file, $this->getPart());
104: /* Need to save this information now, since it is possible that
105: * storage backends change their linked status based on the data
106: * written to the backend. */
107: $this->_linked = $storage->linked;
108: }
109:
110: /**
111: */
112: public function __get($name)
113: {
114: global $injector;
115:
116: switch ($name) {
117: case 'linked':
118: return ($this->forceLinked || ($this->_linked === true));
119:
120: case 'link_url':
121: return $this->storage->link_url;
122:
123: case 'storage':
124: $linked = $this->linked
125: ? 'linked'
126: : (is_null($this->_linked) ? null : 'atc');
127: return $injector->getInstance('IMP_Factory_ComposeAtc')->create(null, $this->_uuid, $linked);
128:
129: case 'tmpfile':
130: return $this->storage->getTempFile();
131: }
132: }
133:
134: /**
135: * Return the MIME part object.
136: *
137: * @param boolean $build If true, ensures the part contains the data.
138: *
139: * @return Horde_Mime_Part MIME part object.
140: * @throws IMP_Compose_Exception
141: */
142: public function getPart($build = false)
143: {
144: if ($build && !$this->_isBuilt) {
145: $this->_part->setContents(
146: $this->storage->read()->stream,
147: array('stream' => true)
148: );
149: $this->_isBuilt = true;
150: }
151:
152: return $this->_part;
153: }
154:
155: /**
156: * Delete the attachment data.
157: */
158: public function delete()
159: {
160: if (!$this->linked) {
161: try {
162: $this->storage->delete();
163: } catch (Exception $e) {}
164: }
165: }
166:
167: /**
168: * Get a URL of the data.
169: *
170: * @return Horde_Url URL to display the attachment data.
171: */
172: public function viewUrl()
173: {
174: return Horde::url('view.php', true)->add(array(
175: 'actionID' => 'compose_attach_preview',
176: 'composeCache' => strval($GLOBALS['injector']->getInstance('IMP_Factory_Compose')->create($this->_composeCache)),
177: 'id' => $this->id
178: ));
179: }
180:
181: /* Serializable methods. */
182:
183: /**
184: */
185: public function serialize()
186: {
187: /* Don't store Mime_Part data. Can't use clone here ATM, since there
188: * appears to be a PHP bug. Since this is an object specific to IMP
189: * (and we are only using in a certain predictable way), it should
190: * be ok to directly alter the MIME part object without any ill
191: * effects. */
192: $this->_part->clearContents();
193: $this->_isBuilt = false;
194:
195: return $GLOBALS['injector']->getInstance('Horde_Pack')->pack(
196: array(
197: $this->_composeCache,
198: $this->id,
199: $this->_linked,
200: $this->_part,
201: $this->related,
202: $this->_uuid
203: ), array(
204: 'compression' => false,
205: 'phpob' => true
206: )
207: );
208: }
209:
210: /**
211: */
212: public function unserialize($data)
213: {
214: list(
215: $this->_composeCache,
216: $this->id,
217: $this->_linked,
218: $this->_part,
219: $this->related,
220: $this->_uuid
221: ) = $GLOBALS['injector']->getInstance('Horde_Pack')->unpack($data);
222: }
223:
224: }
225: