1: <?php
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: class IMP_Ajax_Application extends Horde_Core_Ajax_Application
34: {
35: 36: 37: 38: 39:
40: public $indices;
41:
42: 43: 44: 45: 46:
47: public $queue;
48:
49: 50:
51: protected function _init()
52: {
53: global $injector, $registry;
54:
55: $this->queue = $injector->getInstance('IMP_Ajax_Queue');
56:
57: switch ($registry->getView()) {
58: case $registry::VIEW_BASIC:
59: $this->addHandler('IMP_Ajax_Application_Handler_Mboxtoggle');
60: $this->addHandler('IMP_Ajax_Application_Handler_Passphrase');
61: $this->addHandler('IMP_Ajax_Application_Handler_Search');
62: if ($injector->getInstance('IMP_Factory_Imap')->create()->access(IMP_Imap::ACCESS_REMOTE)) {
63: $this->addHandler('IMP_Ajax_Application_Handler_RemotePrefs');
64: }
65: break;
66:
67: case $registry::VIEW_DYNAMIC:
68: $this->addHandler('IMP_Ajax_Application_Handler_Common');
69: $this->addHandler('IMP_Ajax_Application_Handler_ComposeAttach');
70: $this->addHandler('IMP_Ajax_Application_Handler_Draft');
71: $this->addHandler('IMP_Ajax_Application_Handler_Dynamic');
72: $this->addHandler('IMP_Ajax_Application_Handler_Mboxtoggle');
73: $this->addHandler('IMP_Ajax_Application_Handler_Passphrase');
74: $this->addHandler('IMP_Ajax_Application_Handler_Search');
75: if ($injector->getInstance('IMP_Factory_Imap')->create()->access(IMP_Imap::ACCESS_REMOTE)) {
76: $this->addHandler('IMP_Ajax_Application_Handler_Remote');
77: $this->addHandler('IMP_Ajax_Application_Handler_RemotePrefs');
78: }
79: break;
80:
81: case $registry::VIEW_SMARTMOBILE:
82: $this->addHandler('IMP_Ajax_Application_Handler_Common');
83: $this->addHandler('IMP_Ajax_Application_Handler_ComposeAttach');
84: $this->addHandler('IMP_Ajax_Application_Handler_Draft')->disabled = array(
85: 'autoSaveDraft',
86: 'saveTemplate'
87: );
88: $this->addHandler('IMP_Ajax_Application_Handler_Smartmobile');
89: break;
90: }
91:
92: $this->addHandler('IMP_Ajax_Application_Handler_ImageUnblock');
93: $this->addHandler('Horde_Core_Ajax_Application_Handler_Imple');
94: $this->addHandler('Horde_Core_Ajax_Application_Handler_Prefs');
95:
96: 97:
98: if (isset($this->_vars->view)) {
99: $this->_vars->mailbox = $this->_vars->view;
100: }
101:
102: $this->indices = new IMP_Indices_Mailbox($this->_vars);
103:
104:
105: $vp = isset($this->_vars->viewport)
106: ? json_decode($this->_vars->viewport)
107: : new stdClass;
108: $this->_vars->viewport = new Horde_Support_ObjectStub($vp);
109:
110:
111:
112:
113: if (isset($this->_vars->msgload)) {
114: $this->queue->message($this->indices->mailbox->fromBuids(array($this->_vars->msgload)), true, true);
115: }
116:
117:
118: if (isset($this->_vars->poll)) {
119: $poll = json_decode($this->_vars->poll);
120: $this->queue->poll(
121: empty($poll)
122: ? $injector->getInstance('IMP_Ftree')->poll->getPollList()
123: : IMP_Mailbox::formFrom($poll),
124: true
125: );
126: }
127: }
128:
129: 130:
131: public function send()
132: {
133: $this->getTasks();
134: parent::send();
135: }
136:
137: 138: 139: 140: 141:
142: public function getTasks()
143: {
144: $this->queue->add($this);
145:
146:
147: $name = 'imp:viewport';
148: if (isset($this->tasks->$name)) {
149: $this->tasks->$name = $this->tasks->$name->toObject();
150: }
151:
152: return $this->tasks;
153: }
154:
155:
156:
157: 158: 159: 160: 161: 162: 163: 164:
165: public function initCompose()
166: {
167: global $injector;
168:
169: $ob = new stdClass;
170:
171: $ob->compose = $injector->getInstance('IMP_Factory_Compose')->create($this->_vars->imp_compose);
172: $ob->ajax = new IMP_Ajax_Application_Compose($ob->compose, $this->_vars->type);
173:
174: if (!($ob->contents = $ob->compose->getContentsOb()) &&
175: count($this->indices)) {
176: $ob->contents = $injector->getInstance('IMP_Factory_Contents')->create($this->indices);
177: }
178:
179: $this->queue->compose($ob->compose);
180:
181: return $ob;
182: }
183:
184: 185: 186: 187: 188:
189: public function checkUidvalidity()
190: {
191: try {
192: $this->indices->mailbox->uidvalid;
193: } catch (IMP_Exception $e) {
194: $this->addTask('viewport', $this->viewPortData(true));
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: public function viewPortData($change)
223: {
224: $args = array(
225: 'change' => $change,
226: 'mbox' => strval($this->indices->mailbox)
227: );
228:
229: $params = array(
230: 'applyfilter', 'cache', 'cacheid', 'delhide', 'initial', 'qsearch',
231: 'qsearchfield', 'qsearchfilter', 'qsearchflag', 'qsearchflagnot',
232: 'qsearchmbox', 'rangeslice', 'sortby', 'sortdir'
233: );
234:
235: $vp = $this->_vars->viewport;
236:
237: foreach ($params as $val) {
238: $args[$val] = $vp->$val;
239: }
240:
241: if ($vp->search || $args['initial']) {
242: $args += array(
243: 'after' => intval($vp->after),
244: 'before' => intval($vp->before)
245: );
246: }
247:
248: if ($vp->search) {
249: $search = json_decode($vp->search);
250: $args += array(
251: 'search_buid' => isset($search->buid) ? $search->buid : null,
252: 'search_unseen' => isset($search->unseen) ? $search->unseen : null
253: );
254: } else {
255: list($slice_start, $slice_end) = explode(':', $vp->slice, 2);
256: $args += array(
257: 'slice_start' => intval($slice_start),
258: 'slice_end' => intval($slice_end)
259: );
260: }
261:
262: return $GLOBALS['injector']->getInstance('IMP_Ajax_Application_ListMessages')->listMessages($args);
263: }
264:
265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276:
277: public function changed($rw = null)
278: {
279:
280: if ($this->_vars->viewport->force) {
281: return true;
282: }
283:
284:
285: if ($this->indices->mailbox->search) {
286: return !empty($this->_vars->forceUpdate);
287: }
288:
289: if (!$this->_vars->viewport->cacheid) {
290: return false;
291: }
292:
293: 294:
295: if (!is_null($rw)) {
296: try {
297: $this->indices->mailbox->imp_imap->openMailbox($this->indices->mailbox, $rw ? Horde_Imap_Client::OPEN_READWRITE : Horde_Imap_Client::OPEN_AUTO);
298: } catch (IMP_Imap_Exception $e) {
299: $e->notify();
300: return null;
301: }
302: }
303:
304: return ($this->indices->mailbox->cacheid_date != $this->_vars->viewport->cacheid);
305: }
306:
307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335:
336: public function composeSetup($action)
337: {
338: global $injector, $prefs;
339:
340:
341: $identity = $injector->getInstance('IMP_Identity');
342: if (isset($this->_vars->identity) &&
343: !$prefs->isLocked('default_identity')) {
344: $identity->setDefault($this->_vars->identity);
345: }
346:
347: $addr = $this->getAddrFields();
348:
349: $headers = array(
350:
351: 'from' => strval($identity->getFromLine(null, $this->_vars->from)),
352: 'to' => implode(',', $addr['to']['addr']),
353: 'cc' => implode(',', $addr['cc']['addr']),
354: 'bcc' => implode(',', $addr['bcc']['addr']),
355: 'redirect_to' => implode(',', $addr['redirect_to']['addr']),
356: 'subject' => $this->_vars->subject
357: );
358:
359: $imp_compose = $injector->getInstance('IMP_Factory_Compose')->create($this->_vars->composeCache);
360:
361: $result = new stdClass;
362: $result->action = $action;
363: $result->success = 1;
364:
365: return array($result, $imp_compose, $headers, $identity);
366: }
367:
368: 369: 370: 371: 372: 373: 374:
375: public function getAddrFields()
376: {
377: $out = array();
378:
379: foreach (array('to', 'cc', 'bcc', 'redirect_to') as $val) {
380: $data = $this->_vars->get($val);
381: $data_ac = $this->_vars->get($val . '_ac');
382: $tmp = array(
383: 'addr' => array(),
384: 'map' => false
385: );
386:
387: if (strlen($data)) {
388: if (strlen($data_ac)) {
389: $tmp['map'] = true;
390: foreach (json_decode($data_ac, true) as $val2) {
391: $tmp['addr'][$val2[1]] = $val2[0];
392: }
393: } else {
394: $tmp['addr'][] = $data;
395: }
396: }
397:
398: $out[$val] = $tmp;
399: }
400:
401: return $out;
402: }
403:
404: 405: 406: 407: 408: 409: 410: 411: 412:
413: public function deleteMsgs(IMP_Indices $indices, $changed, $force = false)
414: {
415:
416: if (!$changed) {
417: $changed = ($this->indices->mailbox->getSort()->sortby == Horde_Imap_Client::SORT_THREAD);
418: }
419:
420: if ($changed) {
421: $this->addTask('viewport', $this->viewPortData(true));
422: } elseif (($indices instanceof IMP_Indices_Mailbox) &&
423: ($force || $this->indices->mailbox->hideDeletedMsgs(true))) {
424: $vp = new IMP_Ajax_Application_Viewport($this->indices->mailbox);
425: $vp->disappear = $indices->buids[strval($this->indices->mailbox)];
426: $this->addTask('viewport', $vp);
427: }
428:
429: $this->queue->poll(array_keys($indices->indices()));
430: }
431:
432: }
433: