]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/EmailUIAjax.php
Release 6.4.1
[Github/sugarcrm.git] / modules / Emails / EmailUIAjax.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38  /*********************************************************************************
39
40   * Description:
41   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc. All Rights
42   * Reserved. Contributor(s): ______________________________________..
43   *********************************************************************************/
44   //increate timeout for phpo script execution
45   ini_set('max_execution_time',300);
46   //ajaxInit();
47
48
49   require_once("include/OutboundEmail/OutboundEmail.php");
50   require_once("include/ytree/Tree.php");
51   require_once("include/ytree/ExtNode.php");
52
53   $email = new Email();
54   $email->email2init();
55   $ie = new InboundEmail();
56   $ie->email = $email;
57   $json = getJSONobj();
58
59
60   $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
61
62  if (isset($_REQUEST['emailUIAction'])) {
63   switch($_REQUEST['emailUIAction']) {
64
65
66         ///////////////////////////////////////////////////////////////////////////
67         ////    COMPOSE REPLY FORWARD
68         // this is used in forward/reply
69     case "composeEmail":
70         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: composeEmail");
71         if(isset($_REQUEST['sugarEmail']) && $_REQUEST['sugarEmail'] == 'true' && isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
72             $ie->email->retrieve($_REQUEST['uid']);
73             $ie->email->from_addr = $ie->email->from_addr_name;
74             $ie->email->to_addrs = to_html($ie->email->to_addrs_names);
75             $ie->email->cc_addrs = to_html($ie->email->cc_addrs_names);
76             $ie->email->bcc_addrs = $ie->email->bcc_addrs_names;
77             $ie->email->from_name = $ie->email->from_addr;
78             $email = $ie->email->et->handleReplyType($ie->email, $_REQUEST['composeType']);
79             $ret = $ie->email->et->displayComposeEmail($email);
80             $ret['description'] = empty($email->description_html) ?  str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
81                         //get the forward header and add to description
82             $forward_header = $email->getForwardHeader();
83
84             $ret['description'] = $forward_header . $ret['description'];
85             if ($_REQUEST['composeType'] == 'forward') {
86                 $ret = $ie->email->et->getDraftAttachments($ret);
87             }
88             $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
89             $ret['from'] = from_html($ret['from']);
90             $ret['name'] = from_html($ret['name']);
91             $out = $json->encode($ret, true);
92             echo $out;
93         } elseif(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
94             $ie->retrieve($_REQUEST['ieId']);
95             $ie->mailbox = $_REQUEST['mbox'];
96                         global $timedate;
97             $ie->setEmailForDisplay($_REQUEST['uid']);
98                         $ie->email->date_start = $timedate->to_display_date($ie->email->date_sent);
99                         $ie->email->time_start = $timedate->to_display_time($ie->email->date_sent);
100             $ie->email->date_sent = $timedate->to_display_date_time($ie->email->date_sent);
101             $email = $ie->email->et->handleReplyType($ie->email, $_REQUEST['composeType']);
102             $ret = $ie->email->et->displayComposeEmail($email);
103             if ($_REQUEST['composeType'] == 'forward') {
104                 $ret = $ie->email->et->createCopyOfInboundAttachment($ie, $ret, $_REQUEST['uid']);
105             }
106             $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
107             $ret['from'] = from_html($ret['from']);
108             $ret['name'] = from_html($ret['name']);
109             $ret['ieId'] = $_REQUEST['ieId'];
110             $ret['mbox'] = $_REQUEST['mbox'];
111             $out = $json->encode($ret, true);
112             echo $out;
113         }
114         break;
115
116         /**
117          * sendEmail handles both send and save draft duties
118          */
119     case "sendEmail":
120         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: sendEmail");
121
122
123         $sea = new SugarEmailAddress();
124
125         $email->type = 'out';
126         $email->status = 'sent';
127
128         if(isset($_REQUEST['email_id']) && !empty($_REQUEST['email_id'])) {// && isset($_REQUEST['saveDraft']) && !empty($_REQUEST['saveDraft'])) {
129             $email->retrieve($_REQUEST['email_id']); // uid is GUID in draft cases
130         }
131         if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
132                 $email->uid = $_REQUEST['uid'];
133         }
134
135         if ($email->email2Send($_REQUEST)) {
136             $ret = array(
137                 'composeLayoutId'  => $_REQUEST['composeLayoutId'],
138             );
139                 $out = $json->encode($ret, true);
140             echo $out; // async call to close the proper compose tab
141         }
142     break;
143
144     case "uploadAttachment":
145         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: uploadAttachment");
146         $metadata = $email->email2saveAttachment();
147
148         if(!empty($metadata)) {
149             $out = $json->encode($metadata);
150             echo $out;
151         }
152         break;
153
154     case "removeUploadedAttachment":
155         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: removeUploadedAttachment");
156         $fileFromRequest = from_html($_REQUEST['file']);
157         $fileGUID = substr($fileFromRequest, 0, 36);
158         $fileName = $email->et->userCacheDir . "/" . $fileGUID;
159         $filePath = clean_path($fileName);
160         unlink($filePath);
161         break;
162
163     case "fillComposeCache": // fills client-side compose email cache with signatures and email templates
164         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: fillComposeCache");
165         $out = array();
166         $email_templates_arr = $email->et->getEmailTemplatesArray();
167         natcasesort($email_templates_arr);
168         $out['emailTemplates'] = $email_templates_arr;
169         $sigs = $current_user->getSignaturesArray();
170         // clean "none"
171         foreach($sigs as $k => $v) {
172             if($k == "") {
173                  $sigs[$k] = $app_strings['LBL_NONE'];
174                         } else if (is_array($v) && isset($v['name'])){
175                                 $sigs[$k] = $v['name'];
176                         } else{
177                             $sigs[$k] = $v;
178                         }
179         }
180         $out['signatures'] = $sigs;
181         $out['fromAccounts'] = $email->et->getFromAccountsArray($ie);
182         $out['errorArray'] = array();
183
184         $oe = new OutboundEmail();
185         if( $oe->doesUserOverrideAccountRequireCredentials($current_user->id) )
186         {
187             $overideAccount = $oe->getUsersMailerForSystemOverride($current_user->id);
188             //If the user override account has not been created yet, create it for the user.
189             if($overideAccount == null)
190                 $overideAccount = $oe->createUserSystemOverrideAccount($current_user->id);
191
192                     $out['errorArray'] = array($overideAccount->id => $app_strings['LBL_EMAIL_WARNING_MISSING_USER_CREDS']);
193         }
194
195         $ret = $json->encode($out);
196         echo $ret;
197         break;
198
199     case "getSignature":
200         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSignature");
201         if(isset($_REQUEST['id'])) {
202             $signature = $current_user->getSignature($_REQUEST['id']);
203             $signature['signature_html'] = from_html($signature['signature_html']);
204             $out = $json->encode($signature);
205             echo $out;
206         } else {
207             die();
208         }
209         break;
210
211     case "deleteSignature":
212         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteSignature");
213         if(isset($_REQUEST['id'])) {
214                         require_once("modules/Users/UserSignature.php");
215                 $us = new UserSignature();
216                 $us->mark_deleted($_REQUEST['id']);
217             $signatureArray = $current_user->getSignaturesArray();
218                 // clean "none"
219                 foreach($signatureArray as $k => $v) {
220                     if($k == "") {
221                  $sigs[$k] = $app_strings['LBL_NONE'];
222                     } else if (is_array($v) && isset($v['name'])){
223                         $sigs[$k] = $v['name'];
224                     } else{
225                         $sigs[$k] = $v;
226                     }
227                 }
228                 $out['signatures'] = $signatureArray;
229             $ret = $json->encode($out);
230             echo $ret;
231         } else {
232             die();
233         }
234         break;
235     case "getTemplateAttachments":
236         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getTemplateAttachments");
237         if(isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id'])) {
238
239
240             $where = "parent_id='{$_REQUEST['parent_id']}'";
241             $order = "";
242             $seed = new Note();
243             $fullList = $seed->get_full_list($order, $where, '');
244             $all_fields = array_merge($seed->column_fields, $seed->additional_column_fields);
245
246             $js_fields_arr = array();
247
248             $i=1; // js doesn't like 0 index?
249             if (!empty($fullList)) {
250                 foreach($fullList as $note) {
251                     $js_fields_arr[$i] = array();
252
253                     foreach($all_fields as $field) {
254                         if(isset($note->$field)) {
255                             $note->$field = from_html($note->$field);
256                             $note->$field = preg_replace('/\r\n/','<BR>',$note->$field);
257                             $note->$field = preg_replace('/\n/','<BR>',$note->$field);
258                             $js_fields_arr[$i][$field] = addslashes($note->$field);
259                         }
260                     }
261                     $i++;
262                 }
263             }
264
265             $out = $json->encode($js_fields_arr);
266             echo $out;
267         }
268         break;
269         ////    END COMPOSE REPLY FORWARD
270         ///////////////////////////////////////////////////////////////////////////
271
272
273
274         ///////////////////////////////////////////////////////////////////////////
275         ////    MESSAGE HANDLING
276     case "displayView":
277         $ret = array();
278         $ie->retrieve($_REQUEST['ieId']);
279         $ie->mailbox = $_REQUEST['mailbox'];
280         $ie->connectMailserver();
281
282         switch($_REQUEST['type']) {
283             case "headers":
284                 $title = "{$app_strings['LBL_EMAIL_VIEW_HEADERS']}";
285                 $text = $ie->getFormattedHeaders($_REQUEST['uid']);
286                 break;
287
288             case "raw":
289                 $title = "{$app_strings['LBL_EMAIL_VIEW_RAW']}";
290                 $text = $ie->getFormattedRawSource($_REQUEST['uid']);
291                 break;
292
293             case "printable":
294
295                 break;
296         }
297
298         $ret['html'] = $text;
299         $ret['title'] = $title;
300
301         $out = $json->encode($ret, true);
302         echo $out;
303         break;
304
305     case "getQuickCreateForm":
306         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getQuickCreateForm");
307         if(isset($_REQUEST['qc_module']) && !empty($_REQUEST['qc_module'])) {
308                 if (!ACLController::checkAccess($_REQUEST['qc_module'],'edit', true)) {
309                         echo trim($json->encode(array('html' => translate('LBL_NO_ACCESS', 'ACL')), true));
310                         break;
311                 }
312             $people = array("Users", "Contacts", "Leads", "Prospects");
313             $showSaveToAddressBookButton = false;//(in_array($_REQUEST['qc_module'], $people)) ? true : false;
314
315             if(isset($_REQUEST['sugarEmail']) && !empty($_REQUEST['sugarEmail'])) {
316                 $ie->email->retrieve($_REQUEST['uid']); // uid is a sugar GUID in this case
317             } else {
318                 $ie->retrieve($_REQUEST['ieId']);
319                 $ie->mailbox = $_REQUEST['mailbox'];
320                 $ie->setEmailForDisplay($_REQUEST['uid']);
321             }
322             $ret = $email->et->getQuickCreateForm($_REQUEST, $ie->email, $showSaveToAddressBookButton);
323             $ret['ieId'] = $_REQUEST['ieId'];
324             $ret['mbox'] = $_REQUEST['mailbox'];
325             $ret['uid'] = $_REQUEST['uid'];
326             $ret['module'] = $_REQUEST['qc_module'];
327             if (!isset($_REQUEST['iframe'])) {
328                 $out = trim($json->encode($ret, false));
329             } else {
330                 $out = $ret['html'];
331             }
332             echo $out;
333         }
334         break;
335
336     case 'saveQuickCreate':
337         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveQuickCreate");
338         require_once('include/MVC/Controller/ControllerFactory.php');
339         if (isset($_REQUEST['qcmodule'])) {
340             $GLOBALS['log']->debug("********** QCmodule was set: {$_REQUEST['qcmodule']}");
341         }
342         $controller = ControllerFactory::getController($_REQUEST['qcmodule']);
343         $controller->loadBean();
344         $controller->pre_save();
345         $GLOBALS['log']->debug("********** saving new {$controller->module}");
346         $controller->action_save();
347         //Relate the email to the new bean
348         if(isset($_REQUEST['sugarEmail']) && $_REQUEST['sugarEmail'] == 'true' && isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
349             $ie->email->retrieve($_REQUEST['uid']);
350         } elseif(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
351             $GLOBALS['log']->debug("********** Quick Create from non-imported message");
352             $ie->retrieve($_REQUEST['ieId']);
353             $ie->mailbox = $_REQUEST['mbox'];
354             $ie->connectMailserver();
355             $uid = $_REQUEST['uid'];
356             if($ie->protocol == 'imap') {
357                 $_REQUEST['uid'] = imap_msgno($ie->conn, $_REQUEST['uid']);
358             } else {
359                 $_REQUEST['uid'] = $ie->getCorrectMessageNoForPop3($_REQUEST['uid']);
360             }
361
362             if (!$ie->importOneEmail($_REQUEST['uid'], $uid)) {
363                 $ie->getDuplicateEmailId($_REQUEST['uid'], $uid);
364             } // id
365             $ie->email->retrieve($ie->email->id);
366             $GLOBALS['log']->debug("**********Imported Email");
367             $ie->email->assigned_user_id = $controller->bean->assigned_user_id;
368             $ie->email->assigned_user_name = $controller->bean->assigned_user_name;
369         }
370         if (isset($ie->email->id)) {
371                 if (empty($ie->email->parent_id)) {
372                     $ie->email->parent_id = $controller->bean->id;
373                     $ie->email->parent_type = $controller->module;
374                 } // if
375             $ie->email->status = 'read';
376             $ie->email->save();
377             $mod = strtolower($controller->module);
378             $ie->email->load_relationship($mod);
379             $ie->email->$mod->add($controller->bean->id);
380             if($controller->bean->load_relationship('emails')) {
381                 $controller->bean->emails->add($ie->email->id);
382             }
383             if ($controller->bean->module_dir == 'Cases') {
384                     if($controller->bean->load_relationship('contacts')) {
385                         $emailAddressWithName = $ie->email->from_addr_name;
386                         if (!empty($ie->email->reply_to_addr)) {
387                                 $emailAddressWithName = $ie->email->reply_to_addr;
388                         } // if
389
390                         $emailAddress = SugarEmailAddress::_cleanAddress($emailAddressWithName);
391                         $contactIds = $ie->email->emailAddress->getRelatedId($emailAddress, 'contacts');
392                         if (!empty($contactIds)) {
393                                 $controller->bean->contacts->add($contactIds);
394                         } // if
395                     } // if
396             } // if
397             echo $json->encode(array('id' => $ie->email->id));
398         }
399         break;
400
401     case "getImportForm":
402         $ie->retrieve($_REQUEST['ieId']);
403         //            $ie->mailbox = $_REQUEST['mailbox'];
404         $ie->setEmailForDisplay($_REQUEST['uid']);
405         $ret = $email->et->getImportForm($_REQUEST, $ie->email);
406         $out = trim($json->encode($ret, false));
407         echo $out;
408         break;
409
410     case "getRelateForm":
411         if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
412                 $uids = $json->decode(from_html($_REQUEST['uid']));
413                 $email->retrieve($uids[0]);
414             $ret = $email->et->getImportForm(array('showTeam' => false, 'showAssignTo' => false, 'showDelete' => false), $email,'RelateEditView');
415             $out = trim($json->encode($ret, false));
416         echo $out;
417         }
418     break;
419
420     case "getEmail2DetailView":
421         if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
422             $ret = $email->et->getDetailViewForEmail2($_REQUEST['uid']);
423             if( !isset($_REQUEST['print']) ||  $_REQUEST['print'] === FALSE)
424             {
425                 $out = trim($json->encode($ret, false));
426                 echo $out;
427             }
428             else
429                 echo $ret['html'];
430
431         }
432     break;
433
434     case "relateEmails":
435         if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) &&
436                isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id']) &&
437                isset($_REQUEST['parent_type']) && !empty($_REQUEST['parent_type'])) {
438             $uids = explode($app_strings['LBL_EMAIL_DELIMITER'], $_REQUEST['uid']);
439             $mod = strtolower($_REQUEST['parent_type']);
440             $modId = $_REQUEST['parent_id'];
441             foreach($uids as $id) {
442                 $email = new Email();
443                 $email->retrieve($id);
444                 $email->parent_id = $modId;
445                 $email->parent_type = $_REQUEST['parent_type'];
446                 $email->status = 'read';
447                 $email->save();
448                 $email->load_relationship($mod);
449                 $email->$mod->add($modId);
450             }
451         }
452     break;
453
454
455     case "getAssignmentDialogContent":
456         $out = $email->distributionForm("");
457         $out = trim($json->encode($out, false));
458         echo $out;
459         break;
460     case "doAssignmentAssign":
461         $out = $email->et->doAssignment($_REQUEST['distribute_method'], $_REQUEST['ieId'], $_REQUEST['folder'], $_REQUEST['uids'], $_REQUEST['users']);
462         echo $out;
463         break;
464     case "doAssignmentDelete";
465     $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: doAssignmentDelete");
466     if(isset($_REQUEST['uids']) && !empty($_REQUEST['uids']) &&
467     isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId']) &&
468     isset($_REQUEST['folder']) && !empty($_REQUEST['folder'])) {
469         $email->et->markEmails("deleted", $_REQUEST['ieId'], $_REQUEST['folder'], $_REQUEST['uids']);
470     } else {
471     }
472     break;
473     case "markEmail":
474         global $app_strings;
475         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: markEmail");
476         if(isset($_REQUEST['uids']) && !empty($_REQUEST['uids']) &&
477         isset($_REQUEST['type']) && !empty($_REQUEST['type']) &&
478         isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId']) &&
479         isset($_REQUEST['folder']) && !empty($_REQUEST['folder'])) {
480                 $uid = $json->decode(from_html($_REQUEST['uids']));
481                 $uids = array();
482                 if(is_array($uid)) {
483                         $uids = $uid;
484                 } else {
485                                 if(strpos($uid, $app_strings['LBL_EMAIL_DELIMITER']) !== false) {
486                                         $uids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uid);
487                                 } else {
488                                         $uids[] = $uid;
489                                 }
490                 }   // else
491                 $uids = implode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
492                 $GLOBALS['log']->debug("********** EMAIL 2.0 - Marking emails $uids as {$_REQUEST['type']}");
493
494                 $ret = array();
495                 if(strpos($_REQUEST['folder'], 'sugar::') !== false && ($_REQUEST['type'] == 'deleted') && !ACLController::checkAccess('Emails', 'delete')) {
496                         $ret['status'] = false;
497                         $ret['message'] = $app_strings['LBL_EMAIL_DELETE_ERROR_DESC'];
498                 } else {
499                 $email->et->markEmails($_REQUEST['type'], $_REQUEST['ieId'], $_REQUEST['folder'], $uids);
500                         $ret['status'] = true;
501                 }
502                 $out = trim($json->encode($ret, false));
503                 echo $out;
504         } else {
505         }
506         break;
507
508     case "checkEmail2":
509         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: checkEmail2");
510
511         $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
512
513         $ret = array();
514         $ret['numberAccounts'] = count($showFolders);
515
516         $GLOBALS['log']->info("EMAIL2.0: async checkEmail - found [ ".$ret['numberAccounts']." ] accounts to check");
517
518         if(!empty($showFolders) && is_array($showFolders)) {
519             foreach($showFolders as $ieId) {
520                 $ieId = trim($ieId);
521
522                 if(!empty($ieId)) {
523                     $GLOBALS['log']->info("INBOUNDEMAIL: trying to check email for GUID [ {$ieId} ]");
524                     $ie->disconnectMailserver();
525                     $ie->retrieve($ieId);
526
527                     $ret[$ieId] = $ie->checkEmail2_meta();
528                 }
529             }
530         } else {
531             $GLOBALS['log']->info("EMAIL2.0: at checkEmail() async call - not subscribed accounts to check.");
532         }
533
534
535
536         $out = $json->encode($ret, true);
537         echo $out;
538         break;
539
540     case "checkEmail":
541         $GLOBALS['log']->info("[EMAIL] - Start checkEmail action for user [{$current_user->user_name}]");
542         if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
543             $ie->retrieve($_REQUEST['ieId']);
544             $ie->mailbox = (isset($_REQUEST['mbox']) && !empty($_REQUEST['mbox'])) ? $_REQUEST['mbox'] : "INBOX";
545             $ie->checkEmail(false);
546         } elseif(isset($_REQUEST['all']) && !empty($_REQUEST['all'])) {
547             $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
548
549             $GLOBALS['log']->info("[EMAIL] - checkEmail found ".count($showFolders)." accounts to check for user [{$current_user->user_name}]");
550
551             if(!empty($showFolders) && is_array($showFolders)) {
552                 foreach($showFolders as $ieId) {
553                     $ieId = trim($ieId);
554                     if(!empty($ieId)) {
555                         $GLOBALS['log']->info("[EMAIL] - Start checking email for GUID [{$ieId}] for user [{$current_user->user_name}]");
556                         $ie->disconnectMailserver();
557                         $ie->retrieve($ieId);
558                         $ie->checkEmail(false);
559                         $GLOBALS['log']->info("[EMAIL] - Done checking email for GUID [{$ieId}] for user [{$current_user->user_name}]");
560                     }
561                 }
562             } else {
563                 $GLOBALS['log']->info("EMAIL2.0: at checkEmail() async call - not subscribed accounts to check.");
564             }
565         }
566
567         $tree = $email->et->getMailboxNodes(true); // preserve cache files
568         $return = $tree->generateNodesRaw();
569         $out = $json->encode($return);
570         echo $out;
571         $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
572         break;
573
574     case "checkEmailProgress":
575         $GLOBALS['log']->info("[EMAIL] - Start checkEmail action for user [{$current_user->user_name}]");
576         if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
577             $ie->retrieve($_REQUEST['ieId']);
578             $ie->mailbox = (isset($_REQUEST['mbox']) && !empty($_REQUEST['mbox'])) ? $_REQUEST['mbox'] : "INBOX";
579             $synch = (isset($_REQUEST['synch']) && ($_REQUEST['synch'] == "true"));
580             if (!$ie->is_personal) {
581                 $return = array('status' => "done");
582             } else {
583                     if ($ie->protocol == "pop3") {
584                         $return = $ie->pop3_checkPartialEmail($synch);
585                     } else {
586                         $return = $ie->checkEmailIMAPPartial(false, $synch);
587                     } // else
588             } // if
589             $return['ieid'] = $ie->id;
590             $return['synch'] = $synch;
591                         if(isset($_REQUEST['totalcount']) && !empty($_REQUEST['totalcount']) && $_REQUEST['totalcount'] >= 0) {
592                                 if ($ie->protocol == "pop3") {
593                                         $return['totalcount'] = $_REQUEST['totalcount'];
594                                 } // else
595                         }
596             echo $json->encode($return);
597         } // if
598         break;
599
600     case "getAllFoldersTree":
601         $tree = $email->et->getMailboxNodes(true); // preserve cache files
602         $return = $tree->generateNodesRaw();
603         $out = $json->encode($return);
604         echo $out;
605         $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
606         break;
607
608     case "synchronizeEmail":
609         $GLOBALS['log']->info("[EMAIL] Start action synchronizeEmail for user [{$current_user->user_name}]");
610         $ie->syncEmail(true);
611         $tree = $email->et->getMailboxNodes(true);
612         $return = $tree->generateNodesRaw();
613         $out = $json->encode($return);
614         echo $out;
615         $GLOBALS['log']->info("[EMAIL] Done action synchronizeEmail for user [{$current_user->user_name}]");
616         break;
617
618     case "importEmail":
619         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: importEmail");
620         $ie->retrieve($_REQUEST['ieId']);
621         $ie->mailbox = $_REQUEST['mbox'];
622         $ie->connectMailserver();
623         $return = array();
624         $status = true;
625                 $count = 1;
626         if(strpos($_REQUEST['uid'], $app_strings['LBL_EMAIL_DELIMITER']) !== false) {
627             $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $_REQUEST['uid']);
628             foreach($exUids as $msgNo) {
629                 $uid = $msgNo;
630                 if($ie->protocol == 'imap') {
631                     $msgNo = imap_msgno($ie->conn, $msgNo);
632                     $status = $ie->importOneEmail($msgNo, $uid);
633                 } else {
634                         $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $uid);
635                 } // else
636                 $return[] = $app_strings['LBL_EMAIL_MESSAGE_NO'] . " " . $count . ", " . $app_strings['LBL_STATUS'] . " " . ($status ? $app_strings['LBL_EMAIL_IMPORT_SUCCESS'] : $app_strings['LBL_EMAIL_IMPORT_FAIL']);
637                 $count++;
638                     if(($_REQUEST['delete'] == 'true') && $status && ($current_user->is_admin == 1 || $ie->group_id == $current_user->id)) {
639                         $ie->deleteMessageOnMailServer($uid);
640                         $ie->deleteMessageFromCache($uid);
641                         } // if
642             } // for
643         } else {
644             $msgNo = $_REQUEST['uid'];
645             if($ie->protocol == 'imap') {
646                 $msgNo = imap_msgno($ie->conn, $_REQUEST['uid']);
647                 $status = $ie->importOneEmail($msgNo, $_REQUEST['uid']);
648             } else {
649                 $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $_REQUEST['uid']);
650             } // else
651             $return[] = $app_strings['LBL_EMAIL_MESSAGE_NO'] . " " . $count . ", " . $app_strings['LBL_STATUS'] . " " . ($status ? $app_strings['LBL_EMAIL_IMPORT_SUCCESS'] : $app_strings['LBL_EMAIL_IMPORT_FAIL']);
652
653             if(($_REQUEST['delete'] == 'true') && $status && ($current_user->is_admin == 1 || $ie->group_id == $current_user->id)) {
654                 $ie->deleteMessageOnMailServer($_REQUEST['uid']);
655                 $ie->deleteMessageFromCache($_REQUEST['uid']);
656             } // if
657         } // else
658         echo $json->encode($return);
659         break;
660
661     case "setReadFlag":
662         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setReadFlag");
663         $ie->retrieve($_REQUEST['ieId']);
664         $ie->setReadFlagOnFolderCache($_REQUEST['mbox'], $_REQUEST['uid']);
665         $email->et->getListEmails($_REQUEST['ieId'], $_REQUEST['mbox'], 0, 'true');
666         //unlink("{$cacheRoot}/{$_REQUEST['ieId']}/folders/{$_REQUEST['mbox']}.php");
667         break;
668
669     case "deleteMessage":
670         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteMessage");
671         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
672             $ie->retrieve($_REQUEST['ieId']);
673             $ie->mailbox = $_REQUEST['mbox'];
674
675             if($current_user->is_admin == 1 || $ie->group_id == $current_user->id) {
676                 $ie->deleteMessageOnMailServer($_REQUEST['uid']);
677                 $ie->deleteMessageFromCache($_REQUEST['uid']);
678             } else {
679                 $GLOBALS['log']->debug("*** ERROR: tried to delete an email for an account for which {$current_user->full_name} is not the owner!");
680                 echo "NOOP: error see log";
681             }
682         } else {
683             echo "error: missing credentials";
684         }
685         break;
686
687     case "getSingleMessage":
688         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSingleMessage");
689         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
690             $out = $email->et->getSingleMessage($ie);
691             //Bug: 
692             //echo $json->encode($out);
693             //header("Content-Type: application/json; charset=UTF-8");
694             echo $json->encode(utf8_recursive_encode($out));
695         } else {
696             echo "error: no UID";
697         }
698         break;
699
700     case "getSingleMessageFromSugar":
701         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSingleMessageFromSugar");
702         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
703             $email->retrieve($_REQUEST['uid']);
704             //$email->description_html = from_html($email->description_html);
705             $ie->email = $email;
706
707             if($email->status == 'draft' || $email->type == 'draft') {
708                 // forcing an editview since we are looking at a draft message
709                 global $current_user;
710                 $ret = $ie->email->et->displayComposeEmail($email);
711
712                 $ret = $ie->email->et->getDraftAttachments($ret, $ie);
713                 $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
714
715
716                 $out = $json->encode($ret, true);
717                 echo $out;
718             } else {
719                 $out = $ie->displayOneEmail($_REQUEST['uid'], $_REQUEST['mbox']);
720                 $out['meta']['email']['description'] =
721                         empty($email->description_html) ? str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
722                 $out['meta']['email']['date_start'] = $email->date_start;
723                 $out['meta']['email']['time_start'] = $email->time_start;
724                 $out['meta']['ieId'] = $_REQUEST['ieId'];
725                 $out['meta']['mbox'] = $_REQUEST['mbox'];
726                 $out['meta']['email']['toaddrs'] = $email->et->generateExpandableAddrs($out['meta']['email']['toaddrs']);
727                         if(!empty($out['meta']['email']['cc_addrs'])) {
728                     $ccs = $email->et->generateExpandableAddrs($out['meta']['email']['cc_addrs']);
729                             $out['meta']['email']['cc'] = <<<eoq
730                                         <tr>
731                                                 <td NOWRAP valign="top" class="displayEmailLabel">
732                                                         {$app_strings['LBL_EMAIL_CC']}:
733                                                 </td>
734                                                 <td class="displayEmailValue">
735                                                         {$ccs}
736                                                 </td>
737                                         </tr>
738 eoq;
739                         }
740                 echo $json->encode($out);
741             }
742         } else {
743             echo "error: no UID";
744         }
745         break;
746
747     case "getMultipleMessages":
748         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessages");
749         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
750             $exUids = explode(",", $_REQUEST['uid']);
751
752             $out = array();
753             foreach($exUids as $k => $uid) {
754                 if($email->et->validCacheFileExists($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php")) {
755                     $msg = $email->et->getCacheValue($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php", 'out');
756                 } else {
757                     $ie->retrieve($_REQUEST['ieId']);
758                     $ie->mailbox = $_REQUEST['mbox'];
759                     $ie->setEmailForDisplay($uid, false, true);
760                     $msg = $ie->displayOneEmail($uid, $_REQUEST['mbox']);
761                     $email->et->writeCacheFile('out', $msg, $_REQUEST['ieId'], 'messages', "{$_REQUEST['mbox']}{$uid}.php");
762                 }
763
764                 $out[] = $msg;
765             }
766             echo $json->encode($out);
767         } else {
768             echo "error: no UID";
769         }
770         break;
771
772     case "getMultipleMessagesFromSugar":
773         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessagesFromSugar");
774         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
775             $exIds = explode(",", $_REQUEST['uid']);
776             $out = array();
777
778             foreach($exIds as $id) {
779                 $e = new Email();
780                 $e->retrieve($id);
781                 $e->description_html = from_html($e->description_html);
782                 $ie->email = $e;
783                 $out[] = $ie->displayOneEmail($id, $_REQUEST['mbox']);
784             }
785
786             echo $json->encode($out);
787         }
788
789         break;
790         ////    END MESSAGE HANDLING
791         ///////////////////////////////////////////////////////////////////////////
792
793
794
795         ///////////////////////////////////////////////////////////////////////////
796         ////    LIST VIEW
797     case "getMessageCount":
798         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageCount");
799
800         $out = $ie->getCacheCount($_REQUEST['mbox']);
801         echo $json->encode($out);
802         break;
803
804     case "getMessageList":
805         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListJSON");
806         if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
807             // user view preferences
808             $email->et->saveListView($_REQUEST['ieId'], $_REQUEST['mbox']);
809             // list output
810             $ie->retrieve($_REQUEST['ieId']);
811             if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
812                     $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
813                 } else {
814                         $page = 1;
815                 }
816             $list = $ie->displayFolderContents($_REQUEST['mbox'], $_REQUEST['forceRefresh'], $page);
817             $count = $ie->getCacheCount($_REQUEST['mbox']);
818             $unread = $ie->getCacheUnread($_REQUEST['mbox']);
819             $out = $email->et->jsonOuput($list, 'Email', $count, true, $unread);
820
821             @ob_end_clean();
822             ob_start();
823             echo $out;
824             ob_end_flush();
825             //die();
826         } else {
827             echo "error: no ieID";
828         }
829         break;
830
831     case "getMessageListSugarFolders":
832         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListSugarFoldersJSON");
833         if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
834             // user view preferences
835             $email->et->saveListView($_REQUEST['ieId'], "SUGAR.{$_REQUEST['mbox']}");
836             if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
837                     $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
838                 } else {
839                         $page = 1;
840                 }
841             if(!isset($_REQUEST['sort']) || !isset($_REQUEST['dir'])) {
842                 $_REQUEST['sort'] = '';
843                 $_REQUEST['dir']  = '';
844             }
845             $emailSettings = $current_user->getPreference('emailSettings', 'Emails');
846             // cn: default to a low number until user specifies otherwise
847             if(empty($emailSettings['showNumInList'])) {
848                 $emailSettings['showNumInList'] = 20;
849             }
850
851                         // jchi #9424, get sort and direction from user preference
852                         $sort = 'date';
853                         $direction = 'desc';
854                         $sortSerial = $current_user->getPreference('folderSortOrder', 'Emails');
855                         if(!empty($sortSerial) && !empty($_REQUEST['ieId']) && !empty($_REQUEST['mbox'])) {
856                                 $sortArray = unserialize($sortSerial);
857                                 $GLOBALS['log']->debug("********** EMAIL 2.0********** ary=".print_r($sortArray,true).' id='.$_REQUEST['ieId'].'; box='.$_REQUEST['mbox']);
858                                 $sort = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['sort'];
859                                 $direction = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['direction'];
860                         }
861                         //set sort and direction to user predference
862                         if(!empty($_REQUEST['sort']) && !empty($_REQUEST['dir'])) {
863                                 $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['mbox'], $_REQUEST['sort'], $_REQUEST['dir']);
864                                 $sort = $_REQUEST['sort'];
865                                 $direction = $_REQUEST['dir'];
866                         } else {
867                                 $_REQUEST['sort'] = '';
868                                 $_REQUEST['dir'] = '';
869                         }
870                         //end
871
872             $metalist = $email->et->folder->getListItemsForEmailXML($_REQUEST['ieId'], $page,
873             $emailSettings['showNumInList'], $sort, $direction);
874             $count = $email->et->folder->getCountItems($_REQUEST['ieId']);
875             $unread = $email->et->folder->getCountUnread($_REQUEST['ieId']);
876             $out = $email->et->jsonOuput($metalist, 'Email', $count, false, $unread);
877
878             @ob_end_clean();
879             ob_start();
880             echo $out;
881             ob_end_flush();
882         } else {
883             echo "error: no ieID";
884         }
885         break;
886         ////    END LIST VIEW
887         ///////////////////////////////////////////////////////////////////////////
888
889
890
891         ///////////////////////////////////////////////////////////////////////////
892         ////    FOLDER ACTIONS
893     case "emptyTrash":
894         $email->et->emptyTrash($ie);
895         break;
896
897     case "clearInboundAccountCache":
898         $email->et->clearInboundAccountCache($_REQUEST['ieId']);
899         break;
900
901     case "updateSubscriptions":
902         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: updateSubscriptions");
903         if(isset($_REQUEST['subscriptions']) && !empty($_REQUEST['subscriptions']))
904         {
905             $subs = explode("::", $_REQUEST['subscriptions']);
906             $childrenSubs = array();
907             //Find all children of the group folder subscribed to and add
908             //them to the list of folders to show.
909             foreach ($subs as $singleSub)
910                 $email->et->folder->findAllChildren($singleSub, $childrenSubs);
911
912             $subs = array_merge($subs, $childrenSubs);
913             $email->et->folder->setSubscriptions($subs);
914         }
915         elseif(empty($_REQUEST['subscriptions'])) {
916             $email->et->folder->clearSubscriptions();
917         }
918         break;
919
920     case "refreshSugarFolders":
921         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: refreshSugarFolders");
922         $rootNode = new ExtNode('','');
923         $folderOpenState = $current_user->getPreference('folderOpenState', 'Emails');
924         $folderOpenState = (empty($folderOpenState)) ? "" : $folderOpenState;
925         $ret = $email->et->folder->getUserFolders($rootNode, unserialize($folderOpenState), $current_user, true);
926         $out = $json->encode($ret);
927         echo $out;
928         break;
929
930
931
932     case "getFoldersForSettings":
933         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getFoldersForSettings");
934         $ret = $email->et->folder->getFoldersForSettings($current_user);
935         $out = $json->encode($ret);
936         echo $out;
937         break;
938
939     case "moveEmails":
940         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveEmails");
941         $ie->moveEmails($_REQUEST['sourceIeId'], $_REQUEST['sourceFolder'], $_REQUEST['destinationIeId'], $_REQUEST['destinationFolder'], $_REQUEST['emailUids']);
942         break;
943
944     case "saveNewFolder":
945         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveNewFolder");
946         if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
947             switch($_REQUEST['folderType']) {
948                 case "sugar":
949                     $ret = $email->et->saveNewFolder($_REQUEST['nodeLabel'], $_REQUEST['parentId']);
950                     $out = $json->encode($ret);
951                     echo $out;
952                     break;
953
954                 case "imap":
955                     $ie->retrieve($_REQUEST['ieId']);
956                     $ie->connectMailserver();
957                     $ie->saveNewFolder($_REQUEST['newFolderName'], $_REQUEST['mbox']);
958                     break;
959             }
960         } else {
961             echo "NOOP: no folderType defined";
962         }
963         break;
964
965     case "setFolderViewSelection": // flows into next case statement
966         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderViewSelection");
967         $viewFolders = $_REQUEST['ieIdShow'];
968         $current_user->setPreference('showFolders', base64_encode(serialize($viewFolders)), '', 'Emails');
969         $tree = $email->et->getMailboxNodes(false);
970         $return = $tree->generateNodesRaw();
971         $out = $json->encode($return);
972         echo $out;
973         break;
974
975     case "deleteFolder":
976         $v = $app_strings['LBL_NONE'];
977         $return = array();
978         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteFolder");
979         if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
980             switch($_REQUEST['folderType']) {
981                 case "sugar":
982                     $status = $email->et->folder->deleteChildrenCascade($_REQUEST['folder_id']);
983                     if ($status == true) {
984                         $return['status'] = true;
985                         $return['folder_id'] = $_REQUEST['folder_id'];
986                     } else {
987                         $return['status'] = false;
988                         $return['errorMessage'] = $app_strings['LBL_EMAIL_ERROR_DELETE_GROUP_FOLDER'];
989                     }
990                     break;
991
992                 case "imap":
993                     $ie->retrieve($_REQUEST['ieId']);
994                     $ie->connectMailserver();
995                     $returnArray = $ie->deleteFolder($_REQUEST['mbox']);
996                     $status = $returnArray['status'];
997                     $errorMessage = $returnArray['errorMessage'];
998                     if ($status == true) {
999                         $return['status'] = true;
1000                         $return['mbox'] = $_REQUEST['mbox'];
1001                         $return['ieId'] = $_REQUEST['ieId'];
1002                     } else {
1003                         $return['status'] = false;
1004                         $return['errorMessage'] = $errorMessage;
1005                     }
1006                     break;
1007             }
1008         } else {
1009                 $return['status'] = false;
1010                 $return['errorMessage'] =  "NOOP: no folderType defined";
1011         }
1012         $out = $json->encode($return);
1013         echo $out;
1014         break;
1015     case "renameFolder":
1016         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: renameFolder");
1017
1018         if(isset($_REQUEST['ieId']) && isset($_REQUEST['oldFolderName']) && !empty($_REQUEST['oldFolderName'])
1019         && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1020             $ie->retrieve($_REQUEST['ieId']);
1021             $ie->renameFolder($_REQUEST['oldFolderName'], $_REQUEST['newFolderName']);
1022         } elseif(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1023             if(is_guid($_REQUEST['folderId'])) {
1024                 $email->et->folder->retrieve($_REQUEST['folderId']);
1025                 $email->et->folder->name = $_REQUEST['newFolderName'];
1026                 $email->et->folder->save();
1027             } else {
1028                 echo "NOOP - not a Sugar Folder";
1029             }
1030         }
1031     case "moveFolder":
1032         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveFolder");
1033         if(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newParentId']) && !empty($_REQUEST['newParentId']) && $_REQUEST['newParentId'] != $_REQUEST['folderId']) {
1034             if(is_guid($_REQUEST['folderId']) && is_guid($_REQUEST['newParentId'])) {
1035                 $email->et->folder->retrieve($_REQUEST['folderId']);
1036                 $email->et->folder->updateFolder(array(
1037                     "record"        => $_REQUEST['folderId'],
1038                     "name"          => $email->et->folder->name,
1039                     "parent_folder" => $_REQUEST['newParentId'],
1040                     "team_id"       => $email->et->folder->team_id,
1041                     "team_set_id"       => $email->et->folder->team_set_id,
1042                 ));
1043             } else {
1044                 echo "NOOP - not a Sugar Folder";
1045             }
1046         }
1047         break;
1048     case "getGroupFolder":
1049             $email->et->folder->retrieve($_REQUEST['folderId']);
1050             $_REQUEST['record'] = $_REQUEST['folderId'];
1051             $ret = array();
1052             $ret['folderId'] = $email->et->folder->id;
1053             $ret['folderName'] = $email->et->folder->name;
1054             $ret['parentFolderId'] = $email->et->folder->parent_folder;
1055             $out = $json->encode($ret);
1056             echo $out;
1057         break;
1058
1059
1060     case "rebuildFolders":
1061         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildFolders");
1062         $tree = $email->et->getMailboxNodes(false);
1063         $return = $tree->generateNodesRaw();
1064         $out = $json->encode($return);
1065         echo $out;
1066         break;
1067
1068     case "setFolderOpenState":
1069         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderOpenState");
1070         $email->et->saveFolderOpenState($_REQUEST['focusFolder'], $_REQUEST['focusFolderOpen']);
1071         break;
1072
1073     case "saveListViewSortOrder":
1074         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveListViewSortOrder");
1075         $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['focusFolder'], $_REQUEST['sortBy'], $_REQUEST['reverse']);
1076         break;
1077         ////    END FOLDER ACTIONS
1078         ///////////////////////////////////////////////////////////////////////////
1079
1080         ///////////////////////////////////////////////////////////////////////////
1081         ////    INBOUND EMAIL ACCOUNTS
1082
1083     case "retrieveAllOutbound":
1084         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: retrieveAllOutbound");
1085         global $current_user;
1086         $oe = new OutboundEmail();
1087                 $outbounds = $oe->getUserMailers($current_user);
1088                 $results = array('outbound_account_list' => $outbounds, 'count' => count($outbounds));
1089                 $out = $json->encode($results, false);
1090                 echo $out;
1091
1092         break;
1093
1094     case "editOutbound":
1095         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editOutbound");
1096         if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email'])) {
1097             $oe = new OutboundEmail();
1098             $oe->retrieve($_REQUEST['outbound_email']);
1099
1100             $ret = array();
1101
1102             foreach($oe->field_defs as $def) {
1103                 $ret[$def] = $oe->$def;
1104             }
1105             $ret['mail_smtppass']=''; // don't send back the password
1106             $ret['has_password'] =  isset($oe->mail_smtppass);
1107
1108             $out = $json->encode($ret, true);
1109             echo $out;
1110
1111         } else {
1112             echo "NOOP";
1113         }
1114         break;
1115
1116     case "deleteOutbound":
1117         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteOutbound");
1118         if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email']))
1119         {
1120             $oe = new OutboundEmail();
1121             global $current_user;
1122             $oe->retrieve($_REQUEST['outbound_email']);
1123             $affectedInboundAccounts = $oe->getAssociatedInboundAccounts($current_user);
1124
1125             //Check if the user has confirmed he wants to delete the email account even if associated to an inbound accnt.
1126             $confirmedDelete = ( isset($_REQUEST['confirm']) && $_REQUEST['confirm'] ) ? TRUE : FALSE;
1127
1128             if( count($affectedInboundAccounts) > 0 && !$confirmedDelete)
1129             {
1130                 $results = array('is_error' => true, 'error_message' => $app_strings['LBL_EMAIL_REMOVE_SMTP_WARNING'] , 'outbound_email' => $_REQUEST['outbound_email']);
1131             }
1132             else
1133             {
1134                 $oe->delete();
1135                 $results = array('is_error' => false, 'error_message' => '');
1136             }
1137
1138             $out = $json->encode($results);
1139             @ob_end_clean();
1140             ob_start();
1141             echo $out;
1142             ob_end_flush();
1143             die();
1144         }
1145         else
1146         {
1147             echo "NOOP";
1148         }
1149         break;
1150
1151     case "saveOutbound":
1152         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveOutbound");
1153
1154         $oe = new OutboundEmail();
1155         $oe->id = $_REQUEST['mail_id'];
1156         $oe->retrieve($oe->id);
1157         $oe->name = $_REQUEST['mail_name'];
1158         $type = empty($_REQUEST['type']) ? 'user' : $_REQUEST['type'];
1159         $oe->type = $type;
1160         $oe->user_id = $current_user->id;
1161         $oe->mail_sendtype = "SMTP";
1162         $oe->mail_smtptype = $_REQUEST['mail_smtptype'];
1163         $oe->mail_smtpserver = $_REQUEST['mail_smtpserver'];
1164         $oe->mail_smtpport = $_REQUEST['mail_smtpport'];
1165         $oe->mail_smtpssl = $_REQUEST['mail_smtpssl'];
1166         $oe->mail_smtpauth_req = isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0;
1167         $oe->mail_smtpuser = $_REQUEST['mail_smtpuser'];
1168         if(!empty($_REQUEST['mail_smtppass'])) {
1169             $oe->mail_smtppass = $_REQUEST['mail_smtppass'];
1170         }
1171         $oe = $oe->save();
1172                 echo $oe->id;
1173         break;
1174
1175     case "saveDefaultOutbound":
1176                 global $current_user;
1177         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveDefaultOutbound");
1178         $outbound_id = empty($_REQUEST['id']) ? "" : $_REQUEST['id'];
1179         $ie = new InboundEmail();
1180                 $ie->setUsersDefaultOutboundServerId($current_user, $outbound_id);
1181         break;
1182     case "testOutbound":
1183         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: testOutbound");
1184
1185         $pass = '';
1186         if(!empty($_REQUEST['mail_smtppass'])) {
1187             $pass = $_REQUEST['mail_smtppass'];
1188         } elseif(isset($_REQUEST['mail_name'])) {
1189             $oe = new OutboundEmail();
1190             $oe = $oe->getMailerByName($current_user, $_REQUEST['mail_name']);
1191             if(!empty($oe)) {
1192                 $pass = $oe->mail_smtppass;
1193             }
1194         }
1195         $out = $email->sendEmailTest($_REQUEST['mail_smtpserver'], $_REQUEST['mail_smtpport'], $_REQUEST['mail_smtpssl'],
1196                                                                 (isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0), $_REQUEST['mail_smtpuser'],
1197                                                                 $pass, $_REQUEST['outboundtest_from_address'], $_REQUEST['outboundtest_from_address']);
1198                                                                 
1199         $out = $json->encode($out);
1200         echo $out;
1201         break;
1202
1203     case "rebuildShowAccount":
1204         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1205         $ret = $email->et->getShowAccountsOptions($ie);
1206         $results = array('account_list' => $ret, 'count' => count($ret));
1207         $out = $json->encode($results);
1208         echo $out;
1209         break;
1210
1211     case "rebuildShowAccountForSearch":
1212         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1213         $ret = $email->et->getShowAccountsOptionsForSearch($ie);
1214         $out = $json->encode($ret);
1215         echo $out;
1216         break;
1217
1218     case "deleteIeAccount":
1219         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteIeAccount");
1220         if(isset($_REQUEST['group_id']) && $_REQUEST['group_id'] == $current_user->id) {
1221             $ret = array();
1222             $updateFolders = array();
1223                 $ret['id'] = $_REQUEST['ie_id'];
1224             $out = $json->encode($ret);
1225             $ie->hardDelete($_REQUEST['ie_id']);
1226             $out = $json->encode(array('id' => $_REQUEST['ie_id']));
1227             echo $out;
1228
1229             foreach ($showFolders as $id) {
1230                 if ($id != $_REQUEST['ie_id']) {
1231                     $updateFolders[] = $id;
1232                 }
1233             }
1234
1235             $showStore = base64_encode(serialize($updateFolders));
1236             $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1237         }
1238         break;
1239
1240     case "saveIeAccount":
1241         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveIeAccount");
1242         if(isset($_REQUEST['server_url']) && !empty($_REQUEST['server_url'])) {
1243             if(false === $ie->savePersonalEmailAccount($current_user->id, $current_user->user_name, false)) {
1244                 $ret = array('error' => 'error');
1245                 $out = $json->encode($ret);
1246                 echo $out;
1247             } else {
1248                 $ie->retrieve($_REQUEST['ie_id']);
1249                                 if (!isset($ie->created_by_link)) {
1250                                         $ie->created_by_link = null;
1251                                 }
1252                                 if (!isset($ie->modified_user_id_link)) {
1253                                         $ie->modified_user_id_link = null;
1254                                 }
1255                                 if (!is_array($showFolders)) {
1256                                         $showFolders = array();
1257                                 }
1258                 if(!in_array($ie->id, $showFolders)) {
1259                     $showFolders[] = $ie->id;
1260                     $showStore = base64_encode(serialize($showFolders));
1261                     $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1262                 }
1263
1264                 foreach($ie->field_defs as $k => $v) {
1265                         if (isset($v['type']) && ($v['type'] == 'link')) {
1266                                 continue;
1267                         }
1268                     if($k == 'stored_options') {
1269                         $ie->$k = unserialize(base64_decode($ie->$k));
1270                             if (isset($ie->stored_options['from_name'])) {
1271                                 $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1272                             }
1273                     } elseif($k == 'service') {
1274                         $service = explode("::", $ie->$k);
1275                         $retService = array();
1276
1277                         foreach($service as $serviceK => $serviceV) {
1278                             if(!empty($serviceV)) {
1279                                 $retService[$serviceK] = $serviceV;
1280                             }
1281                         }
1282
1283                         $ie->$k = $retService;
1284                     }
1285
1286                     if (isset($ie->$k))
1287                     $ret[$k] = $ie->$k;
1288                 }
1289
1290                 $out = $json->encode($ret);
1291                 echo $out;
1292             }
1293
1294             //If the user is saving the username/password then we need to update the outbound account.
1295             $outboundMailUser = (isset($_REQUEST['mail_smtpuser'])) ? $_REQUEST['mail_smtpuser'] : "";
1296             $outboundMailPass = (isset($_REQUEST['mail_smtppass'])) ? $_REQUEST['mail_smtppass'] : "";
1297             $outboundMailId = (isset($_REQUEST['outbound_email'])) ? $_REQUEST['outbound_email'] : "";
1298
1299             if( !empty($outboundMailUser) && !empty($outboundMailPass) && !empty($outboundMailId) )
1300             {
1301                 $oe = new OutboundEmail();
1302                 $oe->retrieve($outboundMailId);
1303                 $oe->mail_smtpuser = $outboundMailUser;
1304                 $oe->mail_smtppass = $outboundMailPass;
1305                 $oe->save();
1306             }
1307
1308         } else {
1309             echo "NOOP";
1310         }
1311         break;
1312
1313     case "getIeAccount":
1314         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getIeAccount");
1315         $ie->retrieve($_REQUEST['ieId']);
1316         if($ie->group_id == $current_user->id) {
1317             $ret = array();
1318
1319             foreach($ie->field_defs as $k => $v) {
1320                 if($k == 'stored_options') {
1321                     $ie->$k = unserialize(base64_decode($ie->$k));
1322                     if (isset($ie->stored_options['from_name'])) {
1323                         $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1324                     }
1325                 } elseif($k == 'service') {
1326                     $service = explode("::", $ie->$k);
1327                     $retService = array();
1328                     foreach($service as $serviceK => $serviceV) {
1329                         if(!empty($serviceV)) {
1330                             $retService[$serviceK] = $serviceV;
1331                         }
1332                     }
1333
1334                     $ie->$k = $retService;
1335                 }
1336
1337                 $ret[$k] = $ie->$k;
1338             }
1339             unset($ret['email_password']); // no need to send the password out
1340
1341             $out = $json->encode($ret);
1342         } else {
1343             $out = "NOOP: ID mismatch";
1344         }
1345         echo $out;
1346         break;
1347         ////    END INBOUND EMAIL ACCOUNTS
1348         ///////////////////////////////////////////////////////////////////////////
1349
1350
1351
1352         ///////////////////////////////////////////////////////////////////////////
1353         ////    SEARCH
1354     case "search":
1355         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: search");
1356         if(isset($_REQUEST['subject']) && !empty($_REQUEST['subject']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
1357             $metalist = $ie->search($_REQUEST['ieId'], $_REQUEST['subject']);
1358             if (!isset($_REQUEST['page'])) {
1359                 $_REQUEST['page'] = 1;
1360             }
1361             $_REQUEST['pageSize'] = count($metalist['out']);
1362             $out = $email->et->xmlOutput($metalist, 'Email', false);
1363             @ob_end_clean();
1364             ob_start();
1365             header("Content-type: text/xml");
1366             echo $out;
1367             ob_end_flush();
1368             die();
1369         } else {
1370             echo "NOOP: no search criteria found";
1371         }
1372         break;
1373
1374     case "searchAdvanced":
1375         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: searchAdvanced");
1376
1377         $metalist = $email->searchImportedEmails();
1378         $out = $email->et->jsonOuput($metalist, 'Email', $metalist['totalCount']);
1379
1380         @ob_end_clean();
1381         ob_start();
1382         echo $out;
1383         ob_end_flush();
1384         die();
1385
1386         break;
1387         ////    END SEARCH
1388         ///////////////////////////////////////////////////////////////////////////
1389
1390
1391
1392         ///////////////////////////////////////////////////////////////////////////
1393         ////    SETTINGS
1394     case "loadPreferences":
1395         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: loadPreferences");
1396         $prefs = $email->et->getUserPrefsJS();
1397         $out = $json->encode($prefs);
1398         echo $out;
1399         break;
1400
1401     case "saveSettingsGeneral":
1402         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveSettingsGeneral");
1403         $emailSettings = array();
1404         $emailSettings['emailCheckInterval'] = $_REQUEST['emailCheckInterval'];
1405         //$emailSettings['autoImport'] = isset($_REQUEST['autoImport']) ? '1' : '0';
1406         $emailSettings['alwaysSaveOutbound'] = '1';
1407         $emailSettings['sendPlainText'] = isset($_REQUEST['sendPlainText']) ? '1' : '0';
1408         $emailSettings['showNumInList'] = $_REQUEST['showNumInList'];
1409         $emailSettings['defaultOutboundCharset'] = $_REQUEST['default_charset'];
1410         $current_user->setPreference('emailSettings', $emailSettings, '', 'Emails');
1411
1412         // signature
1413         $current_user->setPreference('signature_default', $_REQUEST['signature_id']);
1414         $current_user->setPreference('signature_prepend', (isset($_REQUEST['signature_prepend'])) ? true : false);
1415
1416         $out = $json->encode($email->et->getUserPrefsJS());
1417         echo $out;
1418         break;
1419
1420     case "setPreference":
1421         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setPreference");
1422         if(isset($_REQUEST['prefName']) && isset($_REQUEST['prefValue'])) {
1423             // handle potential JSON encoding
1424             if(isset($_REQUEST['decode'])) {
1425                 $_REQUEST['prefValue'] = $json->decode(from_html($_REQUEST['prefValue']));
1426             }
1427
1428             $current_user->setPreference($_REQUEST['prefName'], $_REQUEST['prefValue'], '', 'Emails');
1429         }
1430         break;
1431         ////    END SETTINGS
1432         ///////////////////////////////////////////////////////////////////////////
1433
1434
1435
1436
1437         ///////////////////////////////////////////////////////////////////////////
1438         ////    ADDRESS BOOK
1439
1440     case "editContact":
1441         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editContact");
1442         if(isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
1443             $module = "Contacts";
1444             $ret = $email->et->getEditContact($_REQUEST['id'], $module);
1445         }
1446         $out = $json->encode($ret);
1447         echo $out;
1448         break;
1449
1450
1451         /* The four calls below all have the same return signature */
1452     case "removeContact":
1453         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: removeContacts");
1454         if(strpos($_REQUEST['ids'], "::") !== false) {
1455             $removeIds = explode("::", $_REQUEST['ids']);
1456         } else {
1457             $removeIds[] = $_REQUEST['ids'];
1458         }
1459         $email->et->removeContacts($removeIds);
1460
1461     case "saveContactEdit":
1462         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveContactEdit");
1463         if(isset($_REQUEST['args']) && !empty($_REQUEST['args'])) {
1464             $email->et->saveContactEdit($_REQUEST['args']);
1465         }
1466         // flow into getUserContacts();
1467     case "addContact":
1468         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1469         $contacts = array();
1470
1471         if(isset($_REQUEST['bean_module']) && !empty($_REQUEST['bean_module']) && isset($_REQUEST['bean_id']) && !empty($_REQUEST['bean_id'])) {
1472             $contacts[$_REQUEST['bean_id']] = array(
1473             'id' => $_REQUEST['bean_id'],
1474             'module' => $_REQUEST['bean_module']
1475             );
1476             $email->et->setContacts($contacts);
1477         }
1478
1479     case "addContactsMultiple":
1480         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1481         if (isset($_REQUEST['contactData'])) {
1482             $contacts = $json->decode(from_HTML($_REQUEST['contactData']));
1483             if ($contacts) {
1484                 //_ppd($contacts);
1485                 $email->et->setContacts($contacts);
1486             }
1487         }
1488
1489     case "getUserContacts":
1490         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getUserContacts");
1491         $contacts = $email->et->getContacts();
1492
1493         if(is_array($contacts) && !empty($contacts)) {
1494             $ret = $email->et->getUserContacts($contacts, $current_user);
1495             $out = $json->encode($ret);
1496             echo $out;
1497         } else {
1498             echo "{}";
1499         }
1500    break;
1501
1502
1503         // address book search
1504     case "getUnionData":
1505         $wheres = array();
1506         $person;
1507         if(isset($_REQUEST['first_name']) && !empty($_REQUEST['first_name'])) {
1508             $wheres['first_name'] = $_REQUEST['first_name'];
1509         }
1510         if(isset($_REQUEST['last_name']) && !empty($_REQUEST['last_name'])) {
1511             $wheres['last_name'] = $_REQUEST['last_name'];
1512         }
1513         if(isset($_REQUEST['email_address']) && !empty($_REQUEST['email_address'])) {
1514             $wheres['email_address'] = $_REQUEST['email_address'];
1515         }
1516                 if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1517             $person = $_REQUEST['person'];
1518         }
1519         $q = $email->et->_getPeopleUnionQuery($wheres , $person);
1520         $r = $ie->db->limitQuery($q, 0, 25, true);
1521
1522         $out = array();
1523         while($a = $ie->db->fetchByAssoc($r)) {
1524             $person = array();
1525             $person['id'] = $a['id'];
1526             $person['module'] = $a['module'];
1527             $person['full_name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1528             $person['email_address'] = $a['email_address'];
1529             $out[] = $person;
1530         }
1531
1532         $ret = $json->encode($out, true);
1533         echo $ret;
1534         break;
1535
1536     case "getAddressSearchResults":
1537         $wheres = array();
1538         $person = 'contacts';
1539         $relatedBeanInfo = '';
1540                 if(isset($_REQUEST['related_bean_id']) && !empty($_REQUEST['related_bean_id'])) {
1541             $isRelatedSearch = true;
1542             $relatedBeanInfo['related_bean_id'] = $_REQUEST['related_bean_id'];
1543             $relatedBeanInfo['related_bean_type'] = ucfirst($_REQUEST['related_bean_type']);
1544         }
1545
1546         if (isset($_REQUEST['search_field'])) {
1547                 $wheres['first_name'] = $_REQUEST['search_field'];
1548                 $wheres['last_name'] = $_REQUEST['search_field'];
1549                 $wheres['email_address'] = $_REQUEST['search_field'];
1550         }
1551
1552         if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1553             $person = $_REQUEST['person'];
1554         }
1555         if(isset($_REQUEST['start']) && !empty($_REQUEST['start'])) {
1556             $start = $_REQUEST['start'];
1557         } else {
1558                 $start = 0;
1559         }
1560
1561         $qArray = $email->et->getRelatedEmail($person, $wheres, $relatedBeanInfo);
1562         $out = array();
1563         $count = 0;
1564         if (!empty($qArray['query'])) {
1565                 $countq = $qArray['countQuery'];
1566                 $time = microtime(true);
1567                 $r = $ie->db->query($countq);
1568                 $GLOBALS['log']->debug("***QUERY counted in " . (microtime(true) - $time) . " milisec\n");
1569                 if($row = $GLOBALS['db']->fetchByAssoc($r)){
1570                     $count = $row['c'];
1571                 }
1572                 $time = microtime(true);
1573
1574                 //Handle sort and order requests
1575                 $sort = !empty($_REQUEST['sort']) ? $_REQUEST['sort'] : "id";
1576                 $sort = ($sort == 'bean_id') ? 'id' : $sort;
1577                 $sort = ($sort == 'email') ? 'email_address' : $sort;
1578                 $sort = ($sort == 'name') ? 'last_name' : $sort;
1579                 $direction = !empty($_REQUEST['dir']) ? $_REQUEST['dir'] : "asc";
1580                 $order = ( !empty($sort) && !empty($direction) ) ? " ORDER BY {$sort} {$direction}" : "";
1581
1582                 $r = $ie->db->limitQuery($qArray['query'] . " $order ", $start, 25, true);
1583                 $GLOBALS['log']->debug("***QUERY Got results in " . (microtime(true) - $time) . " milisec\n");
1584
1585
1586                 while($a = $ie->db->fetchByAssoc($r)) {
1587                     $person = array();
1588                     $person['bean_id'] = $a['id'];
1589                     $person['bean_module'] = $a['module'];
1590                     $person['name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1591                     $person['email'] = $a['email_address'];
1592                     $out[] = $person;
1593                 }
1594         }
1595         $ret = $email->et->jsonOuput(array('out' => $out), 'Person', $count);
1596
1597         @ob_end_clean();
1598         ob_start();
1599         echo $ret;
1600         ob_end_flush();
1601     break;
1602
1603         ////    END ADDRESS BOOK
1604         ///////////////////////////////////////////////////////////////////////////
1605
1606
1607
1608         ///////////////////////////////////////////////////////////////////////////
1609         ////    MISC
1610
1611     default:
1612         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: default");
1613         echo "NOOP";
1614         break;
1615   } // switch
1616  } // if