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-2011 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 /*********************************************************************************
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);
49 require_once("include/OutboundEmail/OutboundEmail.php");
50 require_once("include/ytree/Tree.php");
51 require_once("include/ytree/ExtNode.php");
55 $ie = new InboundEmail();
60 $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
62 if (isset($_REQUEST['emailUIAction'])) {
63 switch($_REQUEST['emailUIAction']) {
66 ///////////////////////////////////////////////////////////////////////////
67 //// COMPOSE REPLY FORWARD
68 // this is used in forward/reply
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();
84 $ret['description'] = $forward_header . $ret['description'];
85 if ($_REQUEST['composeType'] == 'forward') {
86 $ret = $ie->email->et->getDraftAttachments($ret);
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);
93 } elseif(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
94 $ie->retrieve($_REQUEST['ieId']);
95 $ie->mailbox = $_REQUEST['mbox'];
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']);
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);
117 * sendEmail handles both send and save draft duties
120 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: sendEmail");
123 $sea = new SugarEmailAddress();
125 $email->type = 'out';
126 $email->status = 'sent';
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
131 if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
132 $email->uid = $_REQUEST['uid'];
135 if ($email->email2Send($_REQUEST)) {
137 'composeLayoutId' => $_REQUEST['composeLayoutId'],
139 $out = $json->encode($ret, true);
140 echo $out; // async call to close the proper compose tab
144 case "uploadAttachment":
145 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: uploadAttachment");
146 $metadata = $email->email2saveAttachment();
148 if(!empty($metadata)) {
149 $out = $json->encode($metadata);
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);
163 case "fillComposeCache": // fills client-side compose email cache with signatures and email templates
164 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: fillComposeCache");
166 $email_templates_arr = $email->et->getEmailTemplatesArray();
167 natcasesort($email_templates_arr);
168 $out['emailTemplates'] = $email_templates_arr;
169 $sigs = $current_user->getSignaturesArray();
171 foreach($sigs as $k => $v) {
173 $sigs[$k] = $app_strings['LBL_NONE'];
174 } else if (is_array($v) && isset($v['name'])){
175 $sigs[$k] = $v['name'];
180 $out['signatures'] = $sigs;
181 $out['fromAccounts'] = $email->et->getFromAccountsArray($ie);
182 $out['errorArray'] = array();
184 $oe = new OutboundEmail();
185 if( $oe->doesUserOverrideAccountRequireCredentials($current_user->id) )
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);
192 $out['errorArray'] = array($overideAccount->id => $app_strings['LBL_EMAIL_WARNING_MISSING_USER_CREDS']);
195 $ret = $json->encode($out);
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);
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();
219 foreach($signatureArray as $k => $v) {
221 $sigs[$k] = $app_strings['LBL_NONE'];
222 } else if (is_array($v) && isset($v['name'])){
223 $sigs[$k] = $v['name'];
228 $out['signatures'] = $signatureArray;
229 $ret = $json->encode($out);
235 case "getTemplateAttachments":
236 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getTemplateAttachments");
237 if(isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id'])) {
240 $where = "parent_id='{$_REQUEST['parent_id']}'";
243 $fullList = $seed->get_full_list($order, $where, '');
244 $all_fields = array_merge($seed->column_fields, $seed->additional_column_fields);
246 $js_fields_arr = array();
248 $i=1; // js doesn't like 0 index?
249 if (!empty($fullList)) {
250 foreach($fullList as $note) {
251 $js_fields_arr[$i] = array();
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);
265 $out = $json->encode($js_fields_arr);
269 //// END COMPOSE REPLY FORWARD
270 ///////////////////////////////////////////////////////////////////////////
274 ///////////////////////////////////////////////////////////////////////////
275 //// MESSAGE HANDLING
278 $ie->retrieve($_REQUEST['ieId']);
279 $ie->mailbox = $_REQUEST['mailbox'];
280 $ie->connectMailserver();
282 switch($_REQUEST['type']) {
284 $title = "{$app_strings['LBL_EMAIL_VIEW_HEADERS']}";
285 $text = $ie->getFormattedHeaders($_REQUEST['uid']);
289 $title = "{$app_strings['LBL_EMAIL_VIEW_RAW']}";
290 $text = $ie->getFormattedRawSource($_REQUEST['uid']);
298 $ret['html'] = $text;
299 $ret['title'] = $title;
301 $out = $json->encode($ret, true);
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));
312 $people = array("Users", "Contacts", "Leads", "Prospects");
313 $showSaveToAddressBookButton = false;//(in_array($_REQUEST['qc_module'], $people)) ? true : false;
315 if(isset($_REQUEST['sugarEmail']) && !empty($_REQUEST['sugarEmail'])) {
316 $ie->email->retrieve($_REQUEST['uid']); // uid is a sugar GUID in this case
318 $ie->retrieve($_REQUEST['ieId']);
319 $ie->mailbox = $_REQUEST['mailbox'];
320 $ie->setEmailForDisplay($_REQUEST['uid']);
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));
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']}");
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']);
359 $_REQUEST['uid'] = $ie->getCorrectMessageNoForPop3($_REQUEST['uid']);
362 if (!$ie->importOneEmail($_REQUEST['uid'], $uid)) {
363 $ie->getDuplicateEmailId($_REQUEST['uid'], $uid);
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;
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;
375 $ie->email->status = 'read';
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);
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;
390 $emailAddress = SugarEmailAddress::_cleanAddress($emailAddressWithName);
391 $contactIds = $ie->email->emailAddress->getRelatedId($emailAddress, 'contacts');
392 if (!empty($contactIds)) {
393 $controller->bean->contacts->add($contactIds);
397 echo $json->encode(array('id' => $ie->email->id));
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));
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));
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)
425 $out = trim($json->encode($ret, false));
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';
448 $email->load_relationship($mod);
449 $email->$mod->add($modId);
455 case "getAssignmentDialogContent":
456 $out = $email->distributionForm("");
457 $out = trim($json->encode($out, false));
460 case "doAssignmentAssign":
461 $out = $email->et->doAssignment($_REQUEST['distribute_method'], $_REQUEST['ieId'], $_REQUEST['folder'], $_REQUEST['uids'], $_REQUEST['users']);
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']);
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']));
485 if(strpos($uid, $app_strings['LBL_EMAIL_DELIMITER']) !== false) {
486 $uids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uid);
491 $uids = implode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
492 $GLOBALS['log']->debug("********** EMAIL 2.0 - Marking emails $uids as {$_REQUEST['type']}");
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'];
499 $email->et->markEmails($_REQUEST['type'], $_REQUEST['ieId'], $_REQUEST['folder'], $uids);
500 $ret['status'] = true;
502 $out = trim($json->encode($ret, false));
509 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: checkEmail2");
511 $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
514 $ret['numberAccounts'] = count($showFolders);
516 $GLOBALS['log']->info("EMAIL2.0: async checkEmail - found [ ".$ret['numberAccounts']." ] accounts to check");
518 if(!empty($showFolders) && is_array($showFolders)) {
519 foreach($showFolders as $ieId) {
523 $GLOBALS['log']->info("INBOUNDEMAIL: trying to check email for GUID [ {$ieId} ]");
524 $ie->disconnectMailserver();
525 $ie->retrieve($ieId);
527 $ret[$ieId] = $ie->checkEmail2_meta();
531 $GLOBALS['log']->info("EMAIL2.0: at checkEmail() async call - not subscribed accounts to check.");
536 $out = $json->encode($ret, true);
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')));
549 $GLOBALS['log']->info("[EMAIL] - checkEmail found ".count($showFolders)." accounts to check for user [{$current_user->user_name}]");
551 if(!empty($showFolders) && is_array($showFolders)) {
552 foreach($showFolders as $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}]");
563 $GLOBALS['log']->info("EMAIL2.0: at checkEmail() async call - not subscribed accounts to check.");
567 $tree = $email->et->getMailboxNodes(true); // preserve cache files
568 $return = $tree->generateNodesRaw();
569 $out = $json->encode($return);
571 $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
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");
583 if ($ie->protocol == "pop3") {
584 $return = $ie->pop3_checkPartialEmail($synch);
586 $return = $ie->checkEmailIMAPPartial(false, $synch);
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'];
596 echo $json->encode($return);
600 case "getAllFoldersTree":
601 $tree = $email->et->getMailboxNodes(true); // preserve cache files
602 $return = $tree->generateNodesRaw();
603 $out = $json->encode($return);
605 $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
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);
615 $GLOBALS['log']->info("[EMAIL] Done action synchronizeEmail for user [{$current_user->user_name}]");
619 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: importEmail");
620 $ie->retrieve($_REQUEST['ieId']);
621 $ie->mailbox = $_REQUEST['mbox'];
622 $ie->connectMailserver();
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) {
630 if($ie->protocol == 'imap') {
631 $msgNo = imap_msgno($ie->conn, $msgNo);
632 $status = $ie->importOneEmail($msgNo, $uid);
634 $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $uid);
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']);
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);
644 $msgNo = $_REQUEST['uid'];
645 if($ie->protocol == 'imap') {
646 $msgNo = imap_msgno($ie->conn, $_REQUEST['uid']);
647 $status = $ie->importOneEmail($msgNo, $_REQUEST['uid']);
649 $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $_REQUEST['uid']);
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']);
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']);
658 echo $json->encode($return);
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");
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'];
675 if($current_user->is_admin == 1 || $ie->group_id == $current_user->id) {
676 $ie->deleteMessageOnMailServer($_REQUEST['uid']);
677 $ie->deleteMessageFromCache($_REQUEST['uid']);
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";
683 echo "error: missing credentials";
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 echo $json->encode($out);
693 echo "error: no UID";
697 case "getSingleMessageFromSugar":
698 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSingleMessageFromSugar");
699 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
700 $email->retrieve($_REQUEST['uid']);
701 //$email->description_html = from_html($email->description_html);
704 if($email->status == 'draft' || $email->type == 'draft') {
705 // forcing an editview since we are looking at a draft message
706 global $current_user;
707 $ret = $ie->email->et->displayComposeEmail($email);
709 $ret = $ie->email->et->getDraftAttachments($ret, $ie);
710 $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
713 $out = $json->encode($ret, true);
716 $out = $ie->displayOneEmail($_REQUEST['uid'], $_REQUEST['mbox']);
717 $out['meta']['email']['description'] =
718 empty($email->description_html) ? str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
719 $out['meta']['email']['date_start'] = $email->date_start;
720 $out['meta']['email']['time_start'] = $email->time_start;
721 $out['meta']['ieId'] = $_REQUEST['ieId'];
722 $out['meta']['mbox'] = $_REQUEST['mbox'];
723 $out['meta']['email']['toaddrs'] = $email->et->generateExpandableAddrs($out['meta']['email']['toaddrs']);
724 if(!empty($out['meta']['email']['cc_addrs'])) {
725 $ccs = $email->et->generateExpandableAddrs($out['meta']['email']['cc_addrs']);
726 $out['meta']['email']['cc'] = <<<eoq
728 <td NOWRAP valign="top" class="displayEmailLabel">
729 {$app_strings['LBL_EMAIL_CC']}:
731 <td class="displayEmailValue">
737 echo $json->encode($out);
740 echo "error: no UID";
744 case "getMultipleMessages":
745 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessages");
746 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
747 $exUids = explode(",", $_REQUEST['uid']);
750 foreach($exUids as $k => $uid) {
751 if($email->et->validCacheFileExists($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php")) {
752 $msg = $email->et->getCacheValue($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php", 'out');
754 $ie->retrieve($_REQUEST['ieId']);
755 $ie->mailbox = $_REQUEST['mbox'];
756 $ie->setEmailForDisplay($uid, false, true);
757 $msg = $ie->displayOneEmail($uid, $_REQUEST['mbox']);
758 $email->et->writeCacheFile('out', $msg, $_REQUEST['ieId'], 'messages', "{$_REQUEST['mbox']}{$uid}.php");
763 echo $json->encode($out);
765 echo "error: no UID";
769 case "getMultipleMessagesFromSugar":
770 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessagesFromSugar");
771 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
772 $exIds = explode(",", $_REQUEST['uid']);
775 foreach($exIds as $id) {
778 $e->description_html = from_html($e->description_html);
780 $out[] = $ie->displayOneEmail($id, $_REQUEST['mbox']);
783 echo $json->encode($out);
787 //// END MESSAGE HANDLING
788 ///////////////////////////////////////////////////////////////////////////
792 ///////////////////////////////////////////////////////////////////////////
794 case "getMessageCount":
795 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageCount");
797 $out = $ie->getCacheCount($_REQUEST['mbox']);
798 echo $json->encode($out);
801 case "getMessageList":
802 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListJSON");
803 if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
804 // user view preferences
805 $email->et->saveListView($_REQUEST['ieId'], $_REQUEST['mbox']);
807 $ie->retrieve($_REQUEST['ieId']);
808 if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
809 $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
813 $list = $ie->displayFolderContents($_REQUEST['mbox'], $_REQUEST['forceRefresh'], $page);
814 $count = $ie->getCacheCount($_REQUEST['mbox']);
815 $unread = $ie->getCacheUnread($_REQUEST['mbox']);
816 $out = $email->et->jsonOuput($list, 'Email', $count, true, $unread);
824 echo "error: no ieID";
828 case "getMessageListSugarFolders":
829 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListSugarFoldersJSON");
830 if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
831 // user view preferences
832 $email->et->saveListView($_REQUEST['ieId'], "SUGAR.{$_REQUEST['mbox']}");
833 if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
834 $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
838 if(!isset($_REQUEST['sort']) || !isset($_REQUEST['dir'])) {
839 $_REQUEST['sort'] = '';
840 $_REQUEST['dir'] = '';
842 $emailSettings = $current_user->getPreference('emailSettings', 'Emails');
843 // cn: default to a low number until user specifies otherwise
844 if(empty($emailSettings['showNumInList'])) {
845 $emailSettings['showNumInList'] = 20;
848 // jchi #9424, get sort and direction from user preference
851 $sortSerial = $current_user->getPreference('folderSortOrder', 'Emails');
852 if(!empty($sortSerial) && !empty($_REQUEST['ieId']) && !empty($_REQUEST['mbox'])) {
853 $sortArray = unserialize($sortSerial);
854 $GLOBALS['log']->debug("********** EMAIL 2.0********** ary=".print_r($sortArray,true).' id='.$_REQUEST['ieId'].'; box='.$_REQUEST['mbox']);
855 $sort = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['sort'];
856 $direction = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['direction'];
858 //set sort and direction to user predference
859 if(!empty($_REQUEST['sort']) && !empty($_REQUEST['dir'])) {
860 $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['mbox'], $_REQUEST['sort'], $_REQUEST['dir']);
861 $sort = $_REQUEST['sort'];
862 $direction = $_REQUEST['dir'];
864 $_REQUEST['sort'] = '';
865 $_REQUEST['dir'] = '';
869 $metalist = $email->et->folder->getListItemsForEmailXML($_REQUEST['ieId'], $page,
870 $emailSettings['showNumInList'], $sort, $direction);
871 $count = $email->et->folder->getCountItems($_REQUEST['ieId']);
872 $unread = $email->et->folder->getCountUnread($_REQUEST['ieId']);
873 $out = $email->et->jsonOuput($metalist, 'Email', $count, false, $unread);
880 echo "error: no ieID";
884 ///////////////////////////////////////////////////////////////////////////
888 ///////////////////////////////////////////////////////////////////////////
891 $email->et->emptyTrash($ie);
894 case "clearInboundAccountCache":
895 $email->et->clearInboundAccountCache($_REQUEST['ieId']);
898 case "updateSubscriptions":
899 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: updateSubscriptions");
900 if(isset($_REQUEST['subscriptions']) && !empty($_REQUEST['subscriptions']))
902 $subs = explode("::", $_REQUEST['subscriptions']);
903 $childrenSubs = array();
904 //Find all children of the group folder subscribed to and add
905 //them to the list of folders to show.
906 foreach ($subs as $singleSub)
907 $email->et->folder->findAllChildren($singleSub, $childrenSubs);
909 $subs = array_merge($subs, $childrenSubs);
910 $email->et->folder->setSubscriptions($subs);
912 elseif(empty($_REQUEST['subscriptions'])) {
913 $email->et->folder->clearSubscriptions();
917 case "refreshSugarFolders":
918 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: refreshSugarFolders");
919 $rootNode = new ExtNode('','');
920 $folderOpenState = $current_user->getPreference('folderOpenState', 'Emails');
921 $folderOpenState = (empty($folderOpenState)) ? "" : $folderOpenState;
922 $ret = $email->et->folder->getUserFolders($rootNode, unserialize($folderOpenState), $current_user, true);
923 $out = $json->encode($ret);
929 case "getFoldersForSettings":
930 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getFoldersForSettings");
931 $ret = $email->et->folder->getFoldersForSettings($current_user);
932 $out = $json->encode($ret);
937 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveEmails");
938 $ie->moveEmails($_REQUEST['sourceIeId'], $_REQUEST['sourceFolder'], $_REQUEST['destinationIeId'], $_REQUEST['destinationFolder'], $_REQUEST['emailUids']);
941 case "saveNewFolder":
942 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveNewFolder");
943 if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
944 switch($_REQUEST['folderType']) {
946 $ret = $email->et->saveNewFolder($_REQUEST['nodeLabel'], $_REQUEST['parentId']);
947 $out = $json->encode($ret);
952 $ie->retrieve($_REQUEST['ieId']);
953 $ie->connectMailserver();
954 $ie->saveNewFolder($_REQUEST['newFolderName'], $_REQUEST['mbox']);
958 echo "NOOP: no folderType defined";
962 case "setFolderViewSelection": // flows into next case statement
963 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderViewSelection");
964 $viewFolders = $_REQUEST['ieIdShow'];
965 $current_user->setPreference('showFolders', base64_encode(serialize($viewFolders)), '', 'Emails');
966 $tree = $email->et->getMailboxNodes(false);
967 $return = $tree->generateNodesRaw();
968 $out = $json->encode($return);
973 $v = $app_strings['LBL_NONE'];
975 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteFolder");
976 if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
977 switch($_REQUEST['folderType']) {
979 $status = $email->et->folder->deleteChildrenCascade($_REQUEST['folder_id']);
980 if ($status == true) {
981 $return['status'] = true;
982 $return['folder_id'] = $_REQUEST['folder_id'];
984 $return['status'] = false;
985 $return['errorMessage'] = $app_strings['LBL_EMAIL_ERROR_DELETE_GROUP_FOLDER'];
990 $ie->retrieve($_REQUEST['ieId']);
991 $ie->connectMailserver();
992 $returnArray = $ie->deleteFolder($_REQUEST['mbox']);
993 $status = $returnArray['status'];
994 $errorMessage = $returnArray['errorMessage'];
995 if ($status == true) {
996 $return['status'] = true;
997 $return['mbox'] = $_REQUEST['mbox'];
998 $return['ieId'] = $_REQUEST['ieId'];
1000 $return['status'] = false;
1001 $return['errorMessage'] = $errorMessage;
1006 $return['status'] = false;
1007 $return['errorMessage'] = "NOOP: no folderType defined";
1009 $out = $json->encode($return);
1012 case "renameFolder":
1013 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: renameFolder");
1015 if(isset($_REQUEST['ieId']) && isset($_REQUEST['oldFolderName']) && !empty($_REQUEST['oldFolderName'])
1016 && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1017 $ie->retrieve($_REQUEST['ieId']);
1018 $ie->renameFolder($_REQUEST['oldFolderName'], $_REQUEST['newFolderName']);
1019 } elseif(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1020 if(is_guid($_REQUEST['folderId'])) {
1021 $email->et->folder->retrieve($_REQUEST['folderId']);
1022 $email->et->folder->name = $_REQUEST['newFolderName'];
1023 $email->et->folder->save();
1025 echo "NOOP - not a Sugar Folder";
1029 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveFolder");
1030 if(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newParentId']) && !empty($_REQUEST['newParentId']) && $_REQUEST['newParentId'] != $_REQUEST['folderId']) {
1031 if(is_guid($_REQUEST['folderId']) && is_guid($_REQUEST['newParentId'])) {
1032 $email->et->folder->retrieve($_REQUEST['folderId']);
1033 $email->et->folder->updateFolder(array(
1034 "record" => $_REQUEST['folderId'],
1035 "name" => $email->et->folder->name,
1036 "parent_folder" => $_REQUEST['newParentId'],
1037 "team_id" => $email->et->folder->team_id,
1038 "team_set_id" => $email->et->folder->team_set_id,
1041 echo "NOOP - not a Sugar Folder";
1045 case "getGroupFolder":
1046 $email->et->folder->retrieve($_REQUEST['folderId']);
1047 $_REQUEST['record'] = $_REQUEST['folderId'];
1049 $ret['folderId'] = $email->et->folder->id;
1050 $ret['folderName'] = $email->et->folder->name;
1051 $ret['parentFolderId'] = $email->et->folder->parent_folder;
1052 $out = $json->encode($ret);
1057 case "rebuildFolders":
1058 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildFolders");
1059 $tree = $email->et->getMailboxNodes(false);
1060 $return = $tree->generateNodesRaw();
1061 $out = $json->encode($return);
1065 case "setFolderOpenState":
1066 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderOpenState");
1067 $email->et->saveFolderOpenState($_REQUEST['focusFolder'], $_REQUEST['focusFolderOpen']);
1070 case "saveListViewSortOrder":
1071 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveListViewSortOrder");
1072 $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['focusFolder'], $_REQUEST['sortBy'], $_REQUEST['reverse']);
1074 //// END FOLDER ACTIONS
1075 ///////////////////////////////////////////////////////////////////////////
1077 ///////////////////////////////////////////////////////////////////////////
1078 //// INBOUND EMAIL ACCOUNTS
1080 case "retrieveAllOutbound":
1081 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: retrieveAllOutbound");
1082 global $current_user;
1083 $oe = new OutboundEmail();
1084 $outbounds = $oe->getUserMailers($current_user);
1085 $results = array('outbound_account_list' => $outbounds, 'count' => count($outbounds));
1086 $out = $json->encode($results, false);
1091 case "editOutbound":
1092 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editOutbound");
1093 if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email'])) {
1094 $oe = new OutboundEmail();
1095 $oe->retrieve($_REQUEST['outbound_email']);
1099 foreach($oe->field_defs as $def) {
1100 $ret[$def] = $oe->$def;
1102 $ret['mail_smtppass']=''; // don't send back the password
1103 $ret['has_password'] = isset($oe->mail_smtppass);
1105 $out = $json->encode($ret, true);
1113 case "deleteOutbound":
1114 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteOutbound");
1115 if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email']))
1117 $oe = new OutboundEmail();
1118 global $current_user;
1119 $oe->retrieve($_REQUEST['outbound_email']);
1120 $affectedInboundAccounts = $oe->getAssociatedInboundAccounts($current_user);
1122 //Check if the user has confirmed he wants to delete the email account even if associated to an inbound accnt.
1123 $confirmedDelete = ( isset($_REQUEST['confirm']) && $_REQUEST['confirm'] ) ? TRUE : FALSE;
1125 if( count($affectedInboundAccounts) > 0 && !$confirmedDelete)
1127 $results = array('is_error' => true, 'error_message' => $app_strings['LBL_EMAIL_REMOVE_SMTP_WARNING'] , 'outbound_email' => $_REQUEST['outbound_email']);
1132 $results = array('is_error' => false, 'error_message' => '');
1135 $out = $json->encode($results);
1148 case "saveOutbound":
1149 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveOutbound");
1151 $oe = new OutboundEmail();
1152 $oe->id = $_REQUEST['mail_id'];
1153 $oe->retrieve($oe->id);
1154 $oe->name = $_REQUEST['mail_name'];
1155 $type = empty($_REQUEST['type']) ? 'user' : $_REQUEST['type'];
1157 $oe->user_id = $current_user->id;
1158 $oe->mail_sendtype = "SMTP";
1159 $oe->mail_smtptype = $_REQUEST['mail_smtptype'];
1160 $oe->mail_smtpserver = $_REQUEST['mail_smtpserver'];
1161 $oe->mail_smtpport = $_REQUEST['mail_smtpport'];
1162 $oe->mail_smtpssl = $_REQUEST['mail_smtpssl'];
1163 $oe->mail_smtpauth_req = isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0;
1164 $oe->mail_smtpuser = $_REQUEST['mail_smtpuser'];
1165 if(!empty($_REQUEST['mail_smtppass'])) {
1166 $oe->mail_smtppass = $_REQUEST['mail_smtppass'];
1172 case "saveDefaultOutbound":
1173 global $current_user;
1174 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveDefaultOutbound");
1175 $outbound_id = empty($_REQUEST['id']) ? "" : $_REQUEST['id'];
1176 $ie = new InboundEmail();
1177 $ie->setUsersDefaultOutboundServerId($current_user, $outbound_id);
1179 case "testOutbound":
1180 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: testOutbound");
1183 if(!empty($_REQUEST['mail_smtppass'])) {
1184 $pass = $_REQUEST['mail_smtppass'];
1185 } elseif(isset($_REQUEST['mail_name'])) {
1186 $oe = new OutboundEmail();
1187 $oe = $oe->getMailerByName($current_user, $_REQUEST['mail_name']);
1189 $pass = $oe->mail_smtppass;
1192 $out = $email->sendEmailTest($_REQUEST['mail_smtpserver'], $_REQUEST['mail_smtpport'], $_REQUEST['mail_smtpssl'],
1193 (isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0), $_REQUEST['mail_smtpuser'],
1194 $pass, $_REQUEST['outboundtest_from_address'], $_REQUEST['outboundtest_from_address']);
1196 $out = $json->encode($out);
1200 case "rebuildShowAccount":
1201 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1202 $ret = $email->et->getShowAccountsOptions($ie);
1203 $results = array('account_list' => $ret, 'count' => count($ret));
1204 $out = $json->encode($results);
1208 case "rebuildShowAccountForSearch":
1209 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1210 $ret = $email->et->getShowAccountsOptionsForSearch($ie);
1211 $out = $json->encode($ret);
1215 case "deleteIeAccount":
1216 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteIeAccount");
1217 if(isset($_REQUEST['group_id']) && $_REQUEST['group_id'] == $current_user->id) {
1219 $updateFolders = array();
1220 $ret['id'] = $_REQUEST['ie_id'];
1221 $out = $json->encode($ret);
1222 $ie->hardDelete($_REQUEST['ie_id']);
1223 $out = $json->encode(array('id' => $_REQUEST['ie_id']));
1226 foreach ($showFolders as $id) {
1227 if ($id != $_REQUEST['ie_id']) {
1228 $updateFolders[] = $id;
1232 $showStore = base64_encode(serialize($updateFolders));
1233 $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1237 case "saveIeAccount":
1238 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveIeAccount");
1239 if(isset($_REQUEST['server_url']) && !empty($_REQUEST['server_url'])) {
1240 if(false === $ie->savePersonalEmailAccount($current_user->id, $current_user->user_name, false)) {
1241 $ret = array('error' => 'error');
1242 $out = $json->encode($ret);
1245 $ie->retrieve($_REQUEST['ie_id']);
1246 if (!isset($ie->created_by_link)) {
1247 $ie->created_by_link = null;
1249 if (!isset($ie->modified_user_id_link)) {
1250 $ie->modified_user_id_link = null;
1252 if (!is_array($showFolders)) {
1253 $showFolders = array();
1255 if(!in_array($ie->id, $showFolders)) {
1256 $showFolders[] = $ie->id;
1257 $showStore = base64_encode(serialize($showFolders));
1258 $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1261 foreach($ie->field_defs as $k => $v) {
1262 if (isset($v['type']) && ($v['type'] == 'link')) {
1265 if($k == 'stored_options') {
1266 $ie->$k = unserialize(base64_decode($ie->$k));
1267 if (isset($ie->stored_options['from_name'])) {
1268 $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1270 } elseif($k == 'service') {
1271 $service = explode("::", $ie->$k);
1272 $retService = array();
1274 foreach($service as $serviceK => $serviceV) {
1275 if(!empty($serviceV)) {
1276 $retService[$serviceK] = $serviceV;
1280 $ie->$k = $retService;
1287 $out = $json->encode($ret);
1291 //If the user is saving the username/password then we need to update the outbound account.
1292 $outboundMailUser = (isset($_REQUEST['mail_smtpuser'])) ? $_REQUEST['mail_smtpuser'] : "";
1293 $outboundMailPass = (isset($_REQUEST['mail_smtppass'])) ? $_REQUEST['mail_smtppass'] : "";
1294 $outboundMailId = (isset($_REQUEST['outbound_email'])) ? $_REQUEST['outbound_email'] : "";
1296 if( !empty($outboundMailUser) && !empty($outboundMailPass) && !empty($outboundMailId) )
1298 $oe = new OutboundEmail();
1299 $oe->retrieve($outboundMailId);
1300 $oe->mail_smtpuser = $outboundMailUser;
1301 $oe->mail_smtppass = $outboundMailPass;
1310 case "getIeAccount":
1311 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getIeAccount");
1312 $ie->retrieve($_REQUEST['ieId']);
1313 if($ie->group_id == $current_user->id) {
1316 foreach($ie->field_defs as $k => $v) {
1317 if($k == 'stored_options') {
1318 $ie->$k = unserialize(base64_decode($ie->$k));
1319 if (isset($ie->stored_options['from_name'])) {
1320 $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1322 } elseif($k == 'service') {
1323 $service = explode("::", $ie->$k);
1324 $retService = array();
1325 foreach($service as $serviceK => $serviceV) {
1326 if(!empty($serviceV)) {
1327 $retService[$serviceK] = $serviceV;
1331 $ie->$k = $retService;
1336 unset($ret['email_password']); // no need to send the password out
1338 $out = $json->encode($ret);
1340 $out = "NOOP: ID mismatch";
1344 //// END INBOUND EMAIL ACCOUNTS
1345 ///////////////////////////////////////////////////////////////////////////
1349 ///////////////////////////////////////////////////////////////////////////
1352 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: search");
1353 if(isset($_REQUEST['subject']) && !empty($_REQUEST['subject']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
1354 $metalist = $ie->search($_REQUEST['ieId'], $_REQUEST['subject']);
1355 if (!isset($_REQUEST['page'])) {
1356 $_REQUEST['page'] = 1;
1358 $_REQUEST['pageSize'] = count($metalist['out']);
1359 $out = $email->et->xmlOutput($metalist, 'Email', false);
1362 header("Content-type: text/xml");
1367 echo "NOOP: no search criteria found";
1371 case "searchAdvanced":
1372 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: searchAdvanced");
1374 $metalist = $email->searchImportedEmails();
1375 $out = $email->et->jsonOuput($metalist, 'Email', $metalist['totalCount']);
1385 ///////////////////////////////////////////////////////////////////////////
1389 ///////////////////////////////////////////////////////////////////////////
1391 case "loadPreferences":
1392 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: loadPreferences");
1393 $prefs = $email->et->getUserPrefsJS();
1394 $out = $json->encode($prefs);
1398 case "saveSettingsGeneral":
1399 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveSettingsGeneral");
1400 $emailSettings = array();
1401 $emailSettings['emailCheckInterval'] = $_REQUEST['emailCheckInterval'];
1402 //$emailSettings['autoImport'] = isset($_REQUEST['autoImport']) ? '1' : '0';
1403 $emailSettings['alwaysSaveOutbound'] = '1';
1404 $emailSettings['sendPlainText'] = isset($_REQUEST['sendPlainText']) ? '1' : '0';
1405 $emailSettings['showNumInList'] = $_REQUEST['showNumInList'];
1406 $emailSettings['defaultOutboundCharset'] = $_REQUEST['default_charset'];
1407 $current_user->setPreference('emailSettings', $emailSettings, '', 'Emails');
1410 $current_user->setPreference('signature_default', $_REQUEST['signature_id']);
1411 $current_user->setPreference('signature_prepend', (isset($_REQUEST['signature_prepend'])) ? true : false);
1413 $out = $json->encode($email->et->getUserPrefsJS());
1417 case "setPreference":
1418 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setPreference");
1419 if(isset($_REQUEST['prefName']) && isset($_REQUEST['prefValue'])) {
1420 // handle potential JSON encoding
1421 if(isset($_REQUEST['decode'])) {
1422 $_REQUEST['prefValue'] = $json->decode(from_html($_REQUEST['prefValue']));
1425 $current_user->setPreference($_REQUEST['prefName'], $_REQUEST['prefValue'], '', 'Emails');
1429 ///////////////////////////////////////////////////////////////////////////
1434 ///////////////////////////////////////////////////////////////////////////
1438 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editContact");
1439 if(isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
1440 $module = "Contacts";
1441 $ret = $email->et->getEditContact($_REQUEST['id'], $module);
1443 $out = $json->encode($ret);
1448 /* The four calls below all have the same return signature */
1449 case "removeContact":
1450 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: removeContacts");
1451 if(strpos($_REQUEST['ids'], "::") !== false) {
1452 $removeIds = explode("::", $_REQUEST['ids']);
1454 $removeIds[] = $_REQUEST['ids'];
1456 $email->et->removeContacts($removeIds);
1458 case "saveContactEdit":
1459 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveContactEdit");
1460 if(isset($_REQUEST['args']) && !empty($_REQUEST['args'])) {
1461 $email->et->saveContactEdit($_REQUEST['args']);
1463 // flow into getUserContacts();
1465 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1466 $contacts = array();
1468 if(isset($_REQUEST['bean_module']) && !empty($_REQUEST['bean_module']) && isset($_REQUEST['bean_id']) && !empty($_REQUEST['bean_id'])) {
1469 $contacts[$_REQUEST['bean_id']] = array(
1470 'id' => $_REQUEST['bean_id'],
1471 'module' => $_REQUEST['bean_module']
1473 $email->et->setContacts($contacts);
1476 case "addContactsMultiple":
1477 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1478 if (isset($_REQUEST['contactData'])) {
1479 $contacts = $json->decode(from_HTML($_REQUEST['contactData']));
1482 $email->et->setContacts($contacts);
1486 case "getUserContacts":
1487 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getUserContacts");
1488 $contacts = $email->et->getContacts();
1490 if(is_array($contacts) && !empty($contacts)) {
1491 $ret = $email->et->getUserContacts($contacts, $current_user);
1492 $out = $json->encode($ret);
1500 // address book search
1501 case "getUnionData":
1504 if(isset($_REQUEST['first_name']) && !empty($_REQUEST['first_name'])) {
1505 $wheres['first_name'] = $_REQUEST['first_name'];
1507 if(isset($_REQUEST['last_name']) && !empty($_REQUEST['last_name'])) {
1508 $wheres['last_name'] = $_REQUEST['last_name'];
1510 if(isset($_REQUEST['email_address']) && !empty($_REQUEST['email_address'])) {
1511 $wheres['email_address'] = $_REQUEST['email_address'];
1513 if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1514 $person = $_REQUEST['person'];
1516 $q = $email->et->_getPeopleUnionQuery($wheres , $person);
1517 $r = $ie->db->limitQuery($q, 0, 25, true);
1520 while($a = $ie->db->fetchByAssoc($r)) {
1522 $person['id'] = $a['id'];
1523 $person['module'] = $a['module'];
1524 $person['full_name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1525 $person['email_address'] = $a['email_address'];
1529 $ret = $json->encode($out, true);
1533 case "getAddressSearchResults":
1535 $person = 'contacts';
1536 $relatedBeanInfo = '';
1537 if(isset($_REQUEST['related_bean_id']) && !empty($_REQUEST['related_bean_id'])) {
1538 $isRelatedSearch = true;
1539 $relatedBeanInfo['related_bean_id'] = $_REQUEST['related_bean_id'];
1540 $relatedBeanInfo['related_bean_type'] = ucfirst($_REQUEST['related_bean_type']);
1543 if (isset($_REQUEST['search_field'])) {
1544 $wheres['first_name'] = $_REQUEST['search_field'];
1545 $wheres['last_name'] = $_REQUEST['search_field'];
1546 $wheres['email_address'] = $_REQUEST['search_field'];
1549 if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1550 $person = $_REQUEST['person'];
1552 if(isset($_REQUEST['start']) && !empty($_REQUEST['start'])) {
1553 $start = $_REQUEST['start'];
1558 $qArray = $email->et->getRelatedEmail($person, $wheres, $relatedBeanInfo);
1561 if (!empty($qArray['query'])) {
1562 $countq = $qArray['countQuery'];
1563 $time = microtime(true);
1564 $r = $ie->db->query($countq);
1565 $GLOBALS['log']->debug("***QUERY counted in " . (microtime(true) - $time) . " milisec\n");
1566 if($row = $GLOBALS['db']->fetchByAssoc($r)){
1569 $time = microtime(true);
1571 //Handle sort and order requests
1572 $sort = !empty($_REQUEST['sort']) ? $_REQUEST['sort'] : "id";
1573 $sort = ($sort == 'bean_id') ? 'id' : $sort;
1574 $sort = ($sort == 'email') ? 'email_address' : $sort;
1575 $sort = ($sort == 'name') ? 'last_name' : $sort;
1576 $direction = !empty($_REQUEST['dir']) ? $_REQUEST['dir'] : "asc";
1577 $order = ( !empty($sort) && !empty($direction) ) ? " ORDER BY {$sort} {$direction}" : "";
1579 $r = $ie->db->limitQuery($qArray['query'] . " $order ", $start, 25, true);
1580 $GLOBALS['log']->debug("***QUERY Got results in " . (microtime(true) - $time) . " milisec\n");
1583 while($a = $ie->db->fetchByAssoc($r)) {
1585 $person['bean_id'] = $a['id'];
1586 $person['bean_module'] = $a['module'];
1587 $person['name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1588 $person['email'] = $a['email_address'];
1592 $ret = $email->et->jsonOuput(array('out' => $out), 'Person', $count);
1600 //// END ADDRESS BOOK
1601 ///////////////////////////////////////////////////////////////////////////
1605 ///////////////////////////////////////////////////////////////////////////
1609 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: default");