]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/EmailUIAjax.php
Release 6.5.0
[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;
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                         // If I-E not exist - skip check
558                         if (is_null($ie->retrieve($ieId))) {
559                             $GLOBALS['log']->info("[EMAIL] - Inbound with GUID [{$ieId}] not exist");
560                             continue;
561                         }
562                         $ie->checkEmail(false);
563                         $GLOBALS['log']->info("[EMAIL] - Done checking email for GUID [{$ieId}] for user [{$current_user->user_name}]");
564                     }
565                 }
566             } else {
567                 $GLOBALS['log']->info("EMAIL2.0: at checkEmail() async call - not subscribed accounts to check.");
568             }
569         }
570
571         $tree = $email->et->getMailboxNodes(true); // preserve cache files
572         $return = $tree->generateNodesRaw();
573         $out = $json->encode($return);
574         echo $out;
575         $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
576         break;
577
578     case "checkEmailProgress":
579         $GLOBALS['log']->info("[EMAIL] - Start checkEmail action for user [{$current_user->user_name}]");
580         if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
581             $ie->retrieve($_REQUEST['ieId']);
582             $ie->mailbox = (isset($_REQUEST['mbox']) && !empty($_REQUEST['mbox'])) ? $_REQUEST['mbox'] : "INBOX";
583             $synch = (isset($_REQUEST['synch']) && ($_REQUEST['synch'] == "true"));
584             if (!$ie->is_personal) {
585                 $return = array('status' => "done");
586             } else {
587                     if ($ie->protocol == "pop3") {
588                         $return = $ie->pop3_checkPartialEmail($synch);
589                     } else {
590                         $return = $ie->checkEmailIMAPPartial(false, $synch);
591                     } // else
592             } // if
593             $return['ieid'] = $ie->id;
594             $return['synch'] = $synch;
595                         if(isset($_REQUEST['totalcount']) && !empty($_REQUEST['totalcount']) && $_REQUEST['totalcount'] >= 0) {
596                                 if ($ie->protocol == "pop3") {
597                                         $return['totalcount'] = $_REQUEST['totalcount'];
598                                 } // else
599                         }
600             echo $json->encode($return);
601         } // if
602         break;
603
604     case "getAllFoldersTree":
605         $tree = $email->et->getMailboxNodes(true); // preserve cache files
606         $return = $tree->generateNodesRaw();
607         $out = $json->encode($return);
608         echo $out;
609         $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
610         break;
611
612     case "synchronizeEmail":
613         $GLOBALS['log']->info("[EMAIL] Start action synchronizeEmail for user [{$current_user->user_name}]");
614         $ie->syncEmail(true);
615         $tree = $email->et->getMailboxNodes(true);
616         $return = $tree->generateNodesRaw();
617         $out = $json->encode($return);
618         echo $out;
619         $GLOBALS['log']->info("[EMAIL] Done action synchronizeEmail for user [{$current_user->user_name}]");
620         break;
621
622     case "importEmail":
623         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: importEmail");
624         $ie->retrieve($_REQUEST['ieId']);
625         $ie->mailbox = $_REQUEST['mbox'];
626         $ie->connectMailserver();
627         $return = array();
628         $status = true;
629                 $count = 1;
630         if(strpos($_REQUEST['uid'], $app_strings['LBL_EMAIL_DELIMITER']) !== false) {
631             $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $_REQUEST['uid']);
632             foreach($exUids as $msgNo) {
633                 $uid = $msgNo;
634                 if($ie->protocol == 'imap') {
635                     $msgNo = imap_msgno($ie->conn, $msgNo);
636                     $status = $ie->importOneEmail($msgNo, $uid);
637                 } else {
638                         $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $uid);
639                 } // else
640                 $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']);
641                 $count++;
642                     if(($_REQUEST['delete'] == 'true') && $status && ($current_user->is_admin == 1 || $ie->group_id == $current_user->id)) {
643                         $ie->deleteMessageOnMailServer($uid);
644                         $ie->deleteMessageFromCache($uid);
645                         } // if
646             } // for
647         } else {
648             $msgNo = $_REQUEST['uid'];
649             if($ie->protocol == 'imap') {
650                 $msgNo = imap_msgno($ie->conn, $_REQUEST['uid']);
651                 $status = $ie->importOneEmail($msgNo, $_REQUEST['uid']);
652             } else {
653                 $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $_REQUEST['uid']);
654             } // else
655             $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']);
656
657             if(($_REQUEST['delete'] == 'true') && $status && ($current_user->is_admin == 1 || $ie->group_id == $current_user->id)) {
658                 $ie->deleteMessageOnMailServer($_REQUEST['uid']);
659                 $ie->deleteMessageFromCache($_REQUEST['uid']);
660             } // if
661         } // else
662         echo $json->encode($return);
663         break;
664
665     case "setReadFlag":
666         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setReadFlag");
667         $ie->retrieve($_REQUEST['ieId']);
668         $ie->setReadFlagOnFolderCache($_REQUEST['mbox'], $_REQUEST['uid']);
669         $email->et->getListEmails($_REQUEST['ieId'], $_REQUEST['mbox'], 0, 'true');
670         //unlink("{$cacheRoot}/{$_REQUEST['ieId']}/folders/{$_REQUEST['mbox']}.php");
671         break;
672
673     case "deleteMessage":
674         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteMessage");
675         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
676             $ie->retrieve($_REQUEST['ieId']);
677             $ie->mailbox = $_REQUEST['mbox'];
678
679             if($current_user->is_admin == 1 || $ie->group_id == $current_user->id) {
680                 $ie->deleteMessageOnMailServer($_REQUEST['uid']);
681                 $ie->deleteMessageFromCache($_REQUEST['uid']);
682             } else {
683                 $GLOBALS['log']->debug("*** ERROR: tried to delete an email for an account for which {$current_user->full_name} is not the owner!");
684                 echo "NOOP: error see log";
685             }
686         } else {
687             echo "error: missing credentials";
688         }
689         break;
690
691     case "getSingleMessage":
692         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSingleMessage");
693         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
694             // this method needs to guarantee UTF-8 charset - encoding detection
695             // and conversion is unreliable, and can break valid UTF-8 text
696             $out = $email->et->getSingleMessage($ie);
697
698             echo $json->encode($out);
699         } else {
700             echo "error: no UID";
701         }
702         break;
703
704     case "getSingleMessageFromSugar":
705         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSingleMessageFromSugar");
706         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
707             $email->retrieve($_REQUEST['uid']);
708             //$email->description_html = from_html($email->description_html);
709             $ie->email = $email;
710
711             if($email->status == 'draft' || $email->type == 'draft') {
712                 // forcing an editview since we are looking at a draft message
713                 global $current_user;
714                 $ret = $ie->email->et->displayComposeEmail($email);
715
716                 $ret = $ie->email->et->getDraftAttachments($ret, $ie);
717                 $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
718
719
720                 $out = $json->encode($ret, true);
721                 echo $out;
722             } else {
723                 $out = $ie->displayOneEmail($_REQUEST['uid'], $_REQUEST['mbox']);
724                 $out['meta']['email']['description'] =
725                         empty($email->description_html) ? str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
726                 $out['meta']['email']['date_start'] = $email->date_start;
727                 $out['meta']['email']['time_start'] = $email->time_start;
728                 $out['meta']['ieId'] = $_REQUEST['ieId'];
729                 $out['meta']['mbox'] = $_REQUEST['mbox'];
730                 $out['meta']['email']['toaddrs'] = $email->et->generateExpandableAddrs($out['meta']['email']['toaddrs']);
731                         if(!empty($out['meta']['email']['cc_addrs'])) {
732                     $ccs = $email->et->generateExpandableAddrs($out['meta']['email']['cc_addrs']);
733                             $out['meta']['email']['cc'] = <<<eoq
734                                         <tr>
735                                                 <td NOWRAP valign="top" class="displayEmailLabel">
736                                                         {$app_strings['LBL_EMAIL_CC']}:
737                                                 </td>
738                                                 <td class="displayEmailValue">
739                                                         {$ccs}
740                                                 </td>
741                                         </tr>
742 eoq;
743                         }
744                 echo $json->encode($out);
745             }
746         } else {
747             echo "error: no UID";
748         }
749         break;
750
751     case "getMultipleMessages":
752         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessages");
753         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
754             $exUids = explode(",", $_REQUEST['uid']);
755
756             $out = array();
757             foreach($exUids as $k => $uid) {
758                 if($email->et->validCacheFileExists($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php")) {
759                     $msg = $email->et->getCacheValue($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php", 'out');
760                 } else {
761                     $ie->retrieve($_REQUEST['ieId']);
762                     $ie->mailbox = $_REQUEST['mbox'];
763                     $ie->setEmailForDisplay($uid, false, true);
764                     $msg = $ie->displayOneEmail($uid, $_REQUEST['mbox']);
765                     $email->et->writeCacheFile('out', $msg, $_REQUEST['ieId'], 'messages', "{$_REQUEST['mbox']}{$uid}.php");
766                 }
767
768                 $out[] = $msg;
769             }
770             echo $json->encode($out);
771         } else {
772             echo "error: no UID";
773         }
774         break;
775
776     case "getMultipleMessagesFromSugar":
777         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessagesFromSugar");
778         if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
779             $exIds = explode(",", $_REQUEST['uid']);
780             $out = array();
781
782             foreach($exIds as $id) {
783                 $e = new Email();
784                 $e->retrieve($id);
785                 $e->description_html = from_html($e->description_html);
786                 $ie->email = $e;
787                 $out[] = $ie->displayOneEmail($id, $_REQUEST['mbox']);
788             }
789
790             echo $json->encode($out);
791         }
792
793         break;
794         ////    END MESSAGE HANDLING
795         ///////////////////////////////////////////////////////////////////////////
796
797
798
799         ///////////////////////////////////////////////////////////////////////////
800         ////    LIST VIEW
801     case "getMessageCount":
802         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageCount");
803
804         $out = $ie->getCacheCount($_REQUEST['mbox']);
805         echo $json->encode($out);
806         break;
807
808     case "getMessageList":
809         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListJSON");
810         if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
811             // user view preferences
812             $email->et->saveListView($_REQUEST['ieId'], $_REQUEST['mbox']);
813             // list output
814             $ie->retrieve($_REQUEST['ieId']);
815             if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
816                     $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
817                 } else {
818                         $page = 1;
819                 }
820             $list = $ie->displayFolderContents($_REQUEST['mbox'], $_REQUEST['forceRefresh'], $page);
821             $count = $ie->getCacheCount($_REQUEST['mbox']);
822             $unread = $ie->getCacheUnread($_REQUEST['mbox']);
823             $out = $email->et->jsonOuput($list, 'Email', $count, true, $unread);
824
825             @ob_end_clean();
826             ob_start();
827             echo $out;
828             ob_end_flush();
829             //die();
830         } else {
831             echo "error: no ieID";
832         }
833         break;
834
835     case "getMessageListSugarFolders":
836         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListSugarFoldersJSON");
837         if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
838             // user view preferences
839             $email->et->saveListView($_REQUEST['ieId'], "SUGAR.{$_REQUEST['mbox']}");
840             if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
841                     $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
842                 } else {
843                         $page = 1;
844                 }
845             if(!isset($_REQUEST['sort']) || !isset($_REQUEST['dir'])) {
846                 $_REQUEST['sort'] = '';
847                 $_REQUEST['dir']  = '';
848             }
849             $emailSettings = $current_user->getPreference('emailSettings', 'Emails');
850             // cn: default to a low number until user specifies otherwise
851             if(empty($emailSettings['showNumInList'])) {
852                 $emailSettings['showNumInList'] = 20;
853             }
854
855                         // jchi #9424, get sort and direction from user preference
856                         $sort = 'date';
857                         $direction = 'desc';
858                         $sortSerial = $current_user->getPreference('folderSortOrder', 'Emails');
859                         if(!empty($sortSerial) && !empty($_REQUEST['ieId']) && !empty($_REQUEST['mbox'])) {
860                                 $sortArray = unserialize($sortSerial);
861                                 $GLOBALS['log']->debug("********** EMAIL 2.0********** ary=".print_r($sortArray,true).' id='.$_REQUEST['ieId'].'; box='.$_REQUEST['mbox']);
862                                 $sort = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['sort'];
863                                 $direction = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['direction'];
864                         }
865                         //set sort and direction to user predference
866                         if(!empty($_REQUEST['sort']) && !empty($_REQUEST['dir'])) {
867                                 $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['mbox'], $_REQUEST['sort'], $_REQUEST['dir']);
868                                 $sort = $_REQUEST['sort'];
869                                 $direction = $_REQUEST['dir'];
870                         } else {
871                                 $_REQUEST['sort'] = '';
872                                 $_REQUEST['dir'] = '';
873                         }
874                         //end
875
876             $metalist = $email->et->folder->getListItemsForEmailXML($_REQUEST['ieId'], $page,
877             $emailSettings['showNumInList'], $sort, $direction);
878             $count = $email->et->folder->getCountItems($_REQUEST['ieId']);
879             $unread = $email->et->folder->getCountUnread($_REQUEST['ieId']);
880             $out = $email->et->jsonOuput($metalist, 'Email', $count, false, $unread);
881
882             @ob_end_clean();
883             ob_start();
884             echo $out;
885             ob_end_flush();
886         } else {
887             echo "error: no ieID";
888         }
889         break;
890         ////    END LIST VIEW
891         ///////////////////////////////////////////////////////////////////////////
892
893
894
895         ///////////////////////////////////////////////////////////////////////////
896         ////    FOLDER ACTIONS
897     case "emptyTrash":
898         $email->et->emptyTrash($ie);
899         break;
900
901     case "clearInboundAccountCache":
902         $email->et->clearInboundAccountCache($_REQUEST['ieId']);
903         break;
904
905     case "updateSubscriptions":
906         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: updateSubscriptions");
907         if(isset($_REQUEST['subscriptions']) && !empty($_REQUEST['subscriptions']))
908         {
909             $subs = explode("::", $_REQUEST['subscriptions']);
910             $childrenSubs = array();
911             //Find all children of the group folder subscribed to and add
912             //them to the list of folders to show.
913             foreach ($subs as $singleSub)
914                 $email->et->folder->findAllChildren($singleSub, $childrenSubs);
915
916             $subs = array_merge($subs, $childrenSubs);
917             $email->et->folder->setSubscriptions($subs);
918         }
919         elseif(empty($_REQUEST['subscriptions'])) {
920             $email->et->folder->clearSubscriptions();
921         }
922         break;
923
924     case "refreshSugarFolders":
925         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: refreshSugarFolders");
926         $rootNode = new ExtNode('','');
927         $folderOpenState = $current_user->getPreference('folderOpenState', 'Emails');
928         $folderOpenState = (empty($folderOpenState)) ? "" : $folderOpenState;
929         $ret = $email->et->folder->getUserFolders($rootNode, unserialize($folderOpenState), $current_user, true);
930         $out = $json->encode($ret);
931         echo $out;
932         break;
933
934
935
936     case "getFoldersForSettings":
937         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getFoldersForSettings");
938         $ret = $email->et->folder->getFoldersForSettings($current_user);
939         $out = $json->encode($ret);
940         echo $out;
941         break;
942
943     case "moveEmails":
944         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveEmails");
945         $ie->moveEmails($_REQUEST['sourceIeId'], $_REQUEST['sourceFolder'], $_REQUEST['destinationIeId'], $_REQUEST['destinationFolder'], $_REQUEST['emailUids']);
946         break;
947
948     case "saveNewFolder":
949         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveNewFolder");
950         if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
951             switch($_REQUEST['folderType']) {
952                 case "sugar":
953                     $ret = $email->et->saveNewFolder($_REQUEST['nodeLabel'], $_REQUEST['parentId']);
954                     $out = $json->encode($ret);
955                     echo $out;
956                     break;
957
958                 case "imap":
959                     $ie->retrieve($_REQUEST['ieId']);
960                     $ie->connectMailserver();
961                     $ie->saveNewFolder($_REQUEST['newFolderName'], $_REQUEST['mbox']);
962                     break;
963             }
964         } else {
965             echo "NOOP: no folderType defined";
966         }
967         break;
968
969     case "setFolderViewSelection": // flows into next case statement
970         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderViewSelection");
971         $viewFolders = $_REQUEST['ieIdShow'];
972         $current_user->setPreference('showFolders', base64_encode(serialize($viewFolders)), '', 'Emails');
973         $tree = $email->et->getMailboxNodes(false);
974         $return = $tree->generateNodesRaw();
975         $out = $json->encode($return);
976         echo $out;
977         break;
978
979     case "deleteFolder":
980         $v = $app_strings['LBL_NONE'];
981         $return = array();
982         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteFolder");
983         if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
984             switch($_REQUEST['folderType']) {
985                 case "sugar":
986                     $status = $email->et->folder->deleteChildrenCascade($_REQUEST['folder_id']);
987                     if ($status == true) {
988                         $return['status'] = true;
989                         $return['folder_id'] = $_REQUEST['folder_id'];
990                     } else {
991                         $return['status'] = false;
992                         $return['errorMessage'] = $app_strings['LBL_EMAIL_ERROR_DELETE_GROUP_FOLDER'];
993                     }
994                     break;
995
996                 case "imap":
997                     $ie->retrieve($_REQUEST['ieId']);
998                     $ie->connectMailserver();
999                     $returnArray = $ie->deleteFolder($_REQUEST['mbox']);
1000                     $status = $returnArray['status'];
1001                     $errorMessage = $returnArray['errorMessage'];
1002                     if ($status == true) {
1003                         $return['status'] = true;
1004                         $return['mbox'] = $_REQUEST['mbox'];
1005                         $return['ieId'] = $_REQUEST['ieId'];
1006                     } else {
1007                         $return['status'] = false;
1008                         $return['errorMessage'] = $errorMessage;
1009                     }
1010                     break;
1011             }
1012         } else {
1013                 $return['status'] = false;
1014                 $return['errorMessage'] =  "NOOP: no folderType defined";
1015         }
1016         $out = $json->encode($return);
1017         echo $out;
1018         break;
1019     case "renameFolder":
1020         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: renameFolder");
1021
1022         if(isset($_REQUEST['ieId']) && isset($_REQUEST['oldFolderName']) && !empty($_REQUEST['oldFolderName'])
1023         && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1024             $ie->retrieve($_REQUEST['ieId']);
1025             $ie->renameFolder($_REQUEST['oldFolderName'], $_REQUEST['newFolderName']);
1026         } elseif(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1027             if(is_guid($_REQUEST['folderId'])) {
1028                 $email->et->folder->retrieve($_REQUEST['folderId']);
1029                 $email->et->folder->name = $_REQUEST['newFolderName'];
1030                 $email->et->folder->save();
1031             } else {
1032                 echo "NOOP - not a Sugar Folder";
1033             }
1034         }
1035     case "moveFolder":
1036         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveFolder");
1037         if(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newParentId']) && !empty($_REQUEST['newParentId']) && $_REQUEST['newParentId'] != $_REQUEST['folderId']) {
1038             if(is_guid($_REQUEST['folderId']) && is_guid($_REQUEST['newParentId'])) {
1039                 $email->et->folder->retrieve($_REQUEST['folderId']);
1040                 $email->et->folder->updateFolder(array(
1041                     "record"        => $_REQUEST['folderId'],
1042                     "name"          => $email->et->folder->name,
1043                     "parent_folder" => $_REQUEST['newParentId'],
1044                     "team_id"       => $email->et->folder->team_id,
1045                     "team_set_id"       => $email->et->folder->team_set_id,
1046                 ));
1047             } else {
1048                 echo "NOOP - not a Sugar Folder";
1049             }
1050         }
1051         break;
1052     case "getGroupFolder":
1053             $email->et->folder->retrieve($_REQUEST['folderId']);
1054             $_REQUEST['record'] = $_REQUEST['folderId'];
1055             $ret = array();
1056             $ret['folderId'] = $email->et->folder->id;
1057             $ret['folderName'] = $email->et->folder->name;
1058             $ret['parentFolderId'] = $email->et->folder->parent_folder;
1059             $out = $json->encode($ret);
1060             echo $out;
1061         break;
1062
1063
1064     case "rebuildFolders":
1065         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildFolders");
1066         $tree = $email->et->getMailboxNodes(false);
1067         $return = $tree->generateNodesRaw();
1068         $out = $json->encode($return);
1069         echo $out;
1070         break;
1071
1072     case "setFolderOpenState":
1073         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderOpenState");
1074         $email->et->saveFolderOpenState($_REQUEST['focusFolder'], $_REQUEST['focusFolderOpen']);
1075         break;
1076
1077     case "saveListViewSortOrder":
1078         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveListViewSortOrder");
1079         $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['focusFolder'], $_REQUEST['sortBy'], $_REQUEST['reverse']);
1080         break;
1081         ////    END FOLDER ACTIONS
1082         ///////////////////////////////////////////////////////////////////////////
1083
1084         ///////////////////////////////////////////////////////////////////////////
1085         ////    INBOUND EMAIL ACCOUNTS
1086
1087     case "retrieveAllOutbound":
1088         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: retrieveAllOutbound");
1089         global $current_user;
1090         $oe = new OutboundEmail();
1091                 $outbounds = $oe->getUserMailers($current_user);
1092                 $results = array('outbound_account_list' => $outbounds, 'count' => count($outbounds));
1093                 $out = $json->encode($results, false);
1094                 echo $out;
1095
1096         break;
1097
1098     case "editOutbound":
1099         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editOutbound");
1100         if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email'])) {
1101             $oe = new OutboundEmail();
1102             $oe->retrieve($_REQUEST['outbound_email']);
1103
1104             $ret = array();
1105
1106             foreach($oe->field_defs as $def) {
1107                 $ret[$def] = $oe->$def;
1108             }
1109             $ret['mail_smtppass']=''; // don't send back the password
1110             $ret['has_password'] =  isset($oe->mail_smtppass);
1111
1112             $out = $json->encode($ret, true);
1113             echo $out;
1114
1115         } else {
1116             echo "NOOP";
1117         }
1118         break;
1119
1120     case "deleteOutbound":
1121         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteOutbound");
1122         if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email']))
1123         {
1124             $oe = new OutboundEmail();
1125             global $current_user;
1126             $oe->retrieve($_REQUEST['outbound_email']);
1127             $affectedInboundAccounts = $oe->getAssociatedInboundAccounts($current_user);
1128
1129             //Check if the user has confirmed he wants to delete the email account even if associated to an inbound accnt.
1130             $confirmedDelete = ( isset($_REQUEST['confirm']) && $_REQUEST['confirm'] ) ? TRUE : FALSE;
1131
1132             if( count($affectedInboundAccounts) > 0 && !$confirmedDelete)
1133             {
1134                 $results = array('is_error' => true, 'error_message' => $app_strings['LBL_EMAIL_REMOVE_SMTP_WARNING'] , 'outbound_email' => $_REQUEST['outbound_email']);
1135             }
1136             else
1137             {
1138                 $oe->delete();
1139                 $results = array('is_error' => false, 'error_message' => '');
1140             }
1141
1142             $out = $json->encode($results);
1143             @ob_end_clean();
1144             ob_start();
1145             echo $out;
1146             ob_end_flush();
1147             die();
1148         }
1149         else
1150         {
1151             echo "NOOP";
1152         }
1153         break;
1154
1155     case "saveOutbound":
1156         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveOutbound");
1157
1158         $oe = new OutboundEmail();
1159         $oe->id = $_REQUEST['mail_id'];
1160         $oe->retrieve($oe->id);
1161         $oe->name = $_REQUEST['mail_name'];
1162         $type = empty($_REQUEST['type']) ? 'user' : $_REQUEST['type'];
1163         $oe->type = $type;
1164         $oe->user_id = $current_user->id;
1165         $oe->mail_sendtype = "SMTP";
1166         $oe->mail_smtptype = $_REQUEST['mail_smtptype'];
1167         $oe->mail_smtpserver = $_REQUEST['mail_smtpserver'];
1168         $oe->mail_smtpport = $_REQUEST['mail_smtpport'];
1169         $oe->mail_smtpssl = $_REQUEST['mail_smtpssl'];
1170         $oe->mail_smtpauth_req = isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0;
1171         $oe->mail_smtpuser = $_REQUEST['mail_smtpuser'];
1172         if(!empty($_REQUEST['mail_smtppass'])) {
1173             $oe->mail_smtppass = $_REQUEST['mail_smtppass'];
1174         }
1175         $oe = $oe->save();
1176                 echo $oe->id;
1177         break;
1178
1179     case "saveDefaultOutbound":
1180                 global $current_user;
1181         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveDefaultOutbound");
1182         $outbound_id = empty($_REQUEST['id']) ? "" : $_REQUEST['id'];
1183         $ie = new InboundEmail();
1184                 $ie->setUsersDefaultOutboundServerId($current_user, $outbound_id);
1185         break;
1186     case "testOutbound":
1187         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: testOutbound");
1188
1189         $pass = '';
1190         if(!empty($_REQUEST['mail_smtppass'])) {
1191             $pass = $_REQUEST['mail_smtppass'];
1192         } elseif(isset($_REQUEST['mail_name'])) {
1193             $oe = new OutboundEmail();
1194             $oe = $oe->getMailerByName($current_user, $_REQUEST['mail_name']);
1195             if(!empty($oe)) {
1196                 $pass = $oe->mail_smtppass;
1197             }
1198         }
1199         $out = $email->sendEmailTest($_REQUEST['mail_smtpserver'], $_REQUEST['mail_smtpport'], $_REQUEST['mail_smtpssl'],
1200                                                                 (isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0), $_REQUEST['mail_smtpuser'],
1201                                                                 $pass, $_REQUEST['outboundtest_from_address'], $_REQUEST['outboundtest_from_address']);
1202
1203         $out = $json->encode($out);
1204         echo $out;
1205         break;
1206
1207     case "rebuildShowAccount":
1208         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1209         $ret = $email->et->getShowAccountsOptions($ie);
1210         $results = array('account_list' => $ret, 'count' => count($ret));
1211         $out = $json->encode($results);
1212         echo $out;
1213         break;
1214
1215     case "rebuildShowAccountForSearch":
1216         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1217         $ret = $email->et->getShowAccountsOptionsForSearch($ie);
1218         $out = $json->encode($ret);
1219         echo $out;
1220         break;
1221
1222     case "deleteIeAccount":
1223         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteIeAccount");
1224         if(isset($_REQUEST['group_id']) && $_REQUEST['group_id'] == $current_user->id) {
1225             $ret = array();
1226             $updateFolders = array();
1227                 $ret['id'] = $_REQUEST['ie_id'];
1228             $out = $json->encode($ret);
1229             $ie->hardDelete($_REQUEST['ie_id']);
1230             $out = $json->encode(array('id' => $_REQUEST['ie_id']));
1231             echo $out;
1232
1233             foreach ($showFolders as $id) {
1234                 if ($id != $_REQUEST['ie_id']) {
1235                     $updateFolders[] = $id;
1236                 }
1237             }
1238
1239             $showStore = base64_encode(serialize($updateFolders));
1240             $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1241         }
1242         break;
1243
1244     case "saveIeAccount":
1245         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveIeAccount");
1246         if(isset($_REQUEST['server_url']) && !empty($_REQUEST['server_url'])) {
1247             if(false === $ie->savePersonalEmailAccount($current_user->id, $current_user->user_name, false)) {
1248                 $ret = array('error' => 'error');
1249                 $out = $json->encode($ret);
1250                 echo $out;
1251             } else {
1252                 $ie->retrieve($_REQUEST['ie_id']);
1253                                 if (!isset($ie->created_by_link)) {
1254                                         $ie->created_by_link = null;
1255                                 }
1256                                 if (!isset($ie->modified_user_id_link)) {
1257                                         $ie->modified_user_id_link = null;
1258                                 }
1259                                 if (!is_array($showFolders)) {
1260                                         $showFolders = array();
1261                                 }
1262                 if(!in_array($ie->id, $showFolders)) {
1263                     $showFolders[] = $ie->id;
1264                     $showStore = base64_encode(serialize($showFolders));
1265                     $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1266                 }
1267
1268                 foreach($ie->field_defs as $k => $v) {
1269                         if (isset($v['type']) && ($v['type'] == 'link')) {
1270                                 continue;
1271                         }
1272                     if($k == 'stored_options') {
1273                         $ie->$k = unserialize(base64_decode($ie->$k));
1274                             if (isset($ie->stored_options['from_name'])) {
1275                                 $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1276                             }
1277                     } elseif($k == 'service') {
1278                         $service = explode("::", $ie->$k);
1279                         $retService = array();
1280
1281                         foreach($service as $serviceK => $serviceV) {
1282                             if(!empty($serviceV)) {
1283                                 $retService[$serviceK] = $serviceV;
1284                             }
1285                         }
1286
1287                         $ie->$k = $retService;
1288                     }
1289
1290                     if (isset($ie->$k))
1291                     $ret[$k] = $ie->$k;
1292                 }
1293
1294                 $out = $json->encode($ret);
1295                 echo $out;
1296             }
1297
1298             //If the user is saving the username/password then we need to update the outbound account.
1299             $outboundMailUser = (isset($_REQUEST['mail_smtpuser'])) ? $_REQUEST['mail_smtpuser'] : "";
1300             $outboundMailPass = (isset($_REQUEST['mail_smtppass'])) ? $_REQUEST['mail_smtppass'] : "";
1301             $outboundMailId = (isset($_REQUEST['outbound_email'])) ? $_REQUEST['outbound_email'] : "";
1302
1303             if( !empty($outboundMailUser) && !empty($outboundMailPass) && !empty($outboundMailId) )
1304             {
1305                 $oe = new OutboundEmail();
1306                 $oe->retrieve($outboundMailId);
1307                 $oe->mail_smtpuser = $outboundMailUser;
1308                 $oe->mail_smtppass = $outboundMailPass;
1309                 $oe->save();
1310             }
1311
1312         } else {
1313             echo "NOOP";
1314         }
1315         break;
1316
1317     case "getIeAccount":
1318         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getIeAccount");
1319         $ie->retrieve($_REQUEST['ieId']);
1320         if($ie->group_id == $current_user->id) {
1321             $ret = array();
1322
1323             foreach($ie->field_defs as $k => $v) {
1324                 if($k == 'stored_options') {
1325                     $ie->$k = unserialize(base64_decode($ie->$k));
1326                     if (isset($ie->stored_options['from_name'])) {
1327                         $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1328                     }
1329                 } elseif($k == 'service') {
1330                     $service = explode("::", $ie->$k);
1331                     $retService = array();
1332                     foreach($service as $serviceK => $serviceV) {
1333                         if(!empty($serviceV)) {
1334                             $retService[$serviceK] = $serviceV;
1335                         }
1336                     }
1337
1338                     $ie->$k = $retService;
1339                 }
1340
1341                 $ret[$k] = $ie->$k;
1342             }
1343             unset($ret['email_password']); // no need to send the password out
1344
1345             $out = $json->encode($ret);
1346         } else {
1347             $out = "NOOP: ID mismatch";
1348         }
1349         echo $out;
1350         break;
1351         ////    END INBOUND EMAIL ACCOUNTS
1352         ///////////////////////////////////////////////////////////////////////////
1353
1354
1355
1356         ///////////////////////////////////////////////////////////////////////////
1357         ////    SEARCH
1358     case "search":
1359         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: search");
1360         if(isset($_REQUEST['subject']) && !empty($_REQUEST['subject']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
1361             $metalist = $ie->search($_REQUEST['ieId'], $_REQUEST['subject']);
1362             if (!isset($_REQUEST['page'])) {
1363                 $_REQUEST['page'] = 1;
1364             }
1365             $_REQUEST['pageSize'] = count($metalist['out']);
1366             $out = $email->et->xmlOutput($metalist, 'Email', false);
1367             @ob_end_clean();
1368             ob_start();
1369             header("Content-type: text/xml");
1370             echo $out;
1371             ob_end_flush();
1372             die();
1373         } else {
1374             echo "NOOP: no search criteria found";
1375         }
1376         break;
1377
1378     case "searchAdvanced":
1379         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: searchAdvanced");
1380
1381         $metalist = $email->searchImportedEmails();
1382         $out = $email->et->jsonOuput($metalist, 'Email', $metalist['totalCount']);
1383
1384         @ob_end_clean();
1385         ob_start();
1386         echo $out;
1387         ob_end_flush();
1388         die();
1389
1390         break;
1391         ////    END SEARCH
1392         ///////////////////////////////////////////////////////////////////////////
1393
1394
1395
1396         ///////////////////////////////////////////////////////////////////////////
1397         ////    SETTINGS
1398     case "loadPreferences":
1399         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: loadPreferences");
1400         $prefs = $email->et->getUserPrefsJS();
1401         $out = $json->encode($prefs);
1402         echo $out;
1403         break;
1404
1405     case "saveSettingsGeneral":
1406         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveSettingsGeneral");
1407         $emailSettings = array();
1408         $emailSettings['emailCheckInterval'] = $_REQUEST['emailCheckInterval'];
1409         //$emailSettings['autoImport'] = isset($_REQUEST['autoImport']) ? '1' : '0';
1410         $emailSettings['alwaysSaveOutbound'] = '1';
1411         $emailSettings['sendPlainText'] = isset($_REQUEST['sendPlainText']) ? '1' : '0';
1412         $emailSettings['showNumInList'] = $_REQUEST['showNumInList'];
1413         $emailSettings['defaultOutboundCharset'] = $_REQUEST['default_charset'];
1414         $current_user->setPreference('emailSettings', $emailSettings, '', 'Emails');
1415
1416         // signature
1417         $current_user->setPreference('signature_default', $_REQUEST['signature_id']);
1418         $current_user->setPreference('signature_prepend', (isset($_REQUEST['signature_prepend'])) ? true : false);
1419
1420         $out = $json->encode($email->et->getUserPrefsJS());
1421         echo $out;
1422         break;
1423
1424     case "setPreference":
1425         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setPreference");
1426         if(isset($_REQUEST['prefName']) && isset($_REQUEST['prefValue'])) {
1427             // handle potential JSON encoding
1428             if(isset($_REQUEST['decode'])) {
1429                 $_REQUEST['prefValue'] = $json->decode(from_html($_REQUEST['prefValue']));
1430             }
1431
1432             $current_user->setPreference($_REQUEST['prefName'], $_REQUEST['prefValue'], '', 'Emails');
1433         }
1434         break;
1435         ////    END SETTINGS
1436         ///////////////////////////////////////////////////////////////////////////
1437
1438
1439
1440
1441         ///////////////////////////////////////////////////////////////////////////
1442         ////    ADDRESS BOOK
1443
1444     case "editContact":
1445         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editContact");
1446         if(isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
1447             $module = "Contacts";
1448             $ret = $email->et->getEditContact($_REQUEST['id'], $module);
1449         }
1450         $out = $json->encode($ret);
1451         echo $out;
1452         break;
1453
1454
1455         /* The four calls below all have the same return signature */
1456     case "removeContact":
1457         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: removeContacts");
1458         if(strpos($_REQUEST['ids'], "::") !== false) {
1459             $removeIds = explode("::", $_REQUEST['ids']);
1460         } else {
1461             $removeIds[] = $_REQUEST['ids'];
1462         }
1463         $email->et->removeContacts($removeIds);
1464
1465     case "saveContactEdit":
1466         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveContactEdit");
1467         if(isset($_REQUEST['args']) && !empty($_REQUEST['args'])) {
1468             $email->et->saveContactEdit($_REQUEST['args']);
1469         }
1470         // flow into getUserContacts();
1471     case "addContact":
1472         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1473         $contacts = array();
1474
1475         if(isset($_REQUEST['bean_module']) && !empty($_REQUEST['bean_module']) && isset($_REQUEST['bean_id']) && !empty($_REQUEST['bean_id'])) {
1476             $contacts[$_REQUEST['bean_id']] = array(
1477             'id' => $_REQUEST['bean_id'],
1478             'module' => $_REQUEST['bean_module']
1479             );
1480             $email->et->setContacts($contacts);
1481         }
1482
1483     case "addContactsMultiple":
1484         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1485         if (isset($_REQUEST['contactData'])) {
1486             $contacts = $json->decode(from_HTML($_REQUEST['contactData']));
1487             if ($contacts) {
1488                 //_ppd($contacts);
1489                 $email->et->setContacts($contacts);
1490             }
1491         }
1492
1493     case "getUserContacts":
1494         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getUserContacts");
1495         $contacts = $email->et->getContacts();
1496
1497         if(is_array($contacts) && !empty($contacts)) {
1498             $ret = $email->et->getUserContacts($contacts, $current_user);
1499             $out = $json->encode($ret);
1500             echo $out;
1501         } else {
1502             echo "{}";
1503         }
1504    break;
1505
1506
1507         // address book search
1508     case "getUnionData":
1509         $wheres = array();
1510         $person;
1511         if(isset($_REQUEST['first_name']) && !empty($_REQUEST['first_name'])) {
1512             $wheres['first_name'] = $_REQUEST['first_name'];
1513         }
1514         if(isset($_REQUEST['last_name']) && !empty($_REQUEST['last_name'])) {
1515             $wheres['last_name'] = $_REQUEST['last_name'];
1516         }
1517         if(isset($_REQUEST['email_address']) && !empty($_REQUEST['email_address'])) {
1518             $wheres['email_address'] = $_REQUEST['email_address'];
1519         }
1520                 if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1521             $person = $_REQUEST['person'];
1522         }
1523         $q = $email->et->_getPeopleUnionQuery($wheres , $person);
1524         $r = $ie->db->limitQuery($q, 0, 25, true);
1525
1526         $out = array();
1527         while($a = $ie->db->fetchByAssoc($r)) {
1528             $person = array();
1529             $person['id'] = $a['id'];
1530             $person['module'] = $a['module'];
1531             $person['full_name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1532             $person['email_address'] = $a['email_address'];
1533             $out[] = $person;
1534         }
1535
1536         $ret = $json->encode($out, true);
1537         echo $ret;
1538         break;
1539
1540     case "getAddressSearchResults":
1541         $wheres = array();
1542         $person = 'contacts';
1543         $relatedBeanInfo = '';
1544                 if(isset($_REQUEST['related_bean_id']) && !empty($_REQUEST['related_bean_id'])) {
1545             $isRelatedSearch = true;
1546             $relatedBeanInfo['related_bean_id'] = $_REQUEST['related_bean_id'];
1547             $relatedBeanInfo['related_bean_type'] = ucfirst($_REQUEST['related_bean_type']);
1548         }
1549
1550         if (isset($_REQUEST['search_field'])) {
1551                 $wheres['first_name'] = $_REQUEST['search_field'];
1552                 $wheres['last_name'] = $_REQUEST['search_field'];
1553                 $wheres['email_address'] = $_REQUEST['search_field'];
1554         }
1555
1556         if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1557             $person = $_REQUEST['person'];
1558         }
1559         if(isset($_REQUEST['start']) && !empty($_REQUEST['start'])) {
1560             $start = $_REQUEST['start'];
1561         } else {
1562                 $start = 0;
1563         }
1564
1565         $qArray = $email->et->getRelatedEmail($person, $wheres, $relatedBeanInfo);
1566         $out = array();
1567         $count = 0;
1568         if (!empty($qArray['query'])) {
1569                 $countq = $qArray['countQuery'];
1570                 $time = microtime(true);
1571                 $r = $ie->db->query($countq);
1572                 $GLOBALS['log']->debug("***QUERY counted in " . (microtime(true) - $time) . " milisec\n");
1573                 if($row = $GLOBALS['db']->fetchByAssoc($r)){
1574                     $count = $row['c'];
1575                 }
1576                 $time = microtime(true);
1577
1578                 //Handle sort and order requests
1579                 $sort = !empty($_REQUEST['sort']) ? $_REQUEST['sort'] : "id";
1580                 $sort = ($sort == 'bean_id') ? 'id' : $sort;
1581                 $sort = ($sort == 'email') ? 'email_address' : $sort;
1582                 $sort = ($sort == 'name') ? 'last_name' : $sort;
1583                 $direction = !empty($_REQUEST['dir']) ? $_REQUEST['dir'] : "asc";
1584                 $order = ( !empty($sort) && !empty($direction) ) ? " ORDER BY {$sort} {$direction}" : "";
1585
1586                 $r = $ie->db->limitQuery($qArray['query'] . " $order ", $start, 25, true);
1587                 $GLOBALS['log']->debug("***QUERY Got results in " . (microtime(true) - $time) . " milisec\n");
1588
1589
1590                 while($a = $ie->db->fetchByAssoc($r)) {
1591                     $person = array();
1592                     $person['bean_id'] = $a['id'];
1593                     $person['bean_module'] = $a['module'];
1594                     $person['name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1595                     $person['email'] = $a['email_address'];
1596                     $out[] = $person;
1597                 }
1598         }
1599         $ret = $email->et->jsonOuput(array('out' => $out), 'Person', $count);
1600
1601         @ob_end_clean();
1602         ob_start();
1603         echo $ret;
1604         ob_end_flush();
1605     break;
1606
1607         ////    END ADDRESS BOOK
1608         ///////////////////////////////////////////////////////////////////////////
1609
1610
1611
1612         ///////////////////////////////////////////////////////////////////////////
1613         ////    MISC
1614
1615     default:
1616         $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: default");
1617         echo "NOOP";
1618         break;
1619   } // switch
1620  } // if