2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM 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 switch($_REQUEST['emailUIAction']) {
65 ///////////////////////////////////////////////////////////////////////////
66 //// COMPOSE REPLY FORWARD
67 // this is used in forward/reply
69 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: composeEmail");
70 if(isset($_REQUEST['sugarEmail']) && $_REQUEST['sugarEmail'] == 'true' && isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
71 $ie->email->retrieve($_REQUEST['uid']);
72 $ie->email->from_addr = $ie->email->from_addr_name;
73 $ie->email->to_addrs = to_html($ie->email->to_addrs_names);
74 $ie->email->cc_addrs = to_html($ie->email->cc_addrs_names);
75 $ie->email->bcc_addrs = $ie->email->bcc_addrs_names;
76 $ie->email->from_name = $ie->email->from_addr;
77 $email = $ie->email->et->handleReplyType($ie->email, $_REQUEST['composeType']);
78 $ret = $ie->email->et->displayComposeEmail($email);
79 $ret['description'] = empty($email->description_html) ? str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
80 //get the forward header and add to description
81 $forward_header = $email->getForwardHeader();
83 $ret['description'] = $forward_header . $ret['description'];
84 if ($_REQUEST['composeType'] == 'forward') {
85 $ret = $ie->email->et->getDraftAttachments($ret);
87 $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
88 $ret['from'] = from_html($ret['from']);
89 $ret['name'] = from_html($ret['name']);
90 $out = $json->encode($ret, true);
92 } elseif(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
93 $ie->retrieve($_REQUEST['ieId']);
94 $ie->mailbox = $_REQUEST['mbox'];
96 $ie->setEmailForDisplay($_REQUEST['uid']);
97 $ie->email->date_start = $timedate->to_display_date($ie->email->date_sent);
98 $ie->email->time_start = $timedate->to_display_time($ie->email->date_sent);
99 $ie->email->date_sent = $timedate->to_display_date_time($ie->email->date_sent);
100 $email = $ie->email->et->handleReplyType($ie->email, $_REQUEST['composeType']);
101 $ret = $ie->email->et->displayComposeEmail($email);
102 if ($_REQUEST['composeType'] == 'forward') {
103 $ret = $ie->email->et->createCopyOfInboundAttachment($ie, $ret, $_REQUEST['uid']);
105 $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
106 $ret['from'] = from_html($ret['from']);
107 $ret['name'] = from_html($ret['name']);
108 $ret['ieId'] = $_REQUEST['ieId'];
109 $ret['mbox'] = $_REQUEST['mbox'];
110 $out = $json->encode($ret, true);
116 * sendEmail handles both send and save draft duties
119 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: sendEmail");
122 $sea = new SugarEmailAddress();
124 $email->type = 'out';
125 $email->status = 'sent';
127 if(isset($_REQUEST['email_id']) && !empty($_REQUEST['email_id'])) {// && isset($_REQUEST['saveDraft']) && !empty($_REQUEST['saveDraft'])) {
128 $email->retrieve($_REQUEST['email_id']); // uid is GUID in draft cases
130 if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
131 $email->uid = $_REQUEST['uid'];
134 if ($email->email2Send($_REQUEST)) {
136 'composeLayoutId' => $_REQUEST['composeLayoutId'],
138 $out = $json->encode($ret, true);
139 echo $out; // async call to close the proper compose tab
143 case "uploadAttachment":
144 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: uploadAttachment");
145 $metadata = $email->email2saveAttachment();
147 if(!empty($metadata)) {
148 $out = $json->encode($metadata);
153 case "removeUploadedAttachment":
154 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: removeUploadedAttachment");
155 $fileFromRequest = from_html($_REQUEST['file']);
156 $fileGUID = substr($fileFromRequest, 0, 36);
157 $fileName = $email->et->userCacheDir . "/" . $fileGUID;
158 $filePath = clean_path($fileName);
162 case "fillComposeCache": // fills client-side compose email cache with signatures and email templates
163 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: fillComposeCache");
165 $email_templates_arr = $email->et->getEmailTemplatesArray();
166 natcasesort($email_templates_arr);
167 $out['emailTemplates'] = $email_templates_arr;
168 $sigs = $current_user->getSignaturesArray();
170 foreach($sigs as $k => $v) {
172 $sigs[$k] = $app_strings['LBL_NONE'];
173 } else if (is_array($v) && isset($v['name'])){
174 $sigs[$k] = $v['name'];
179 $out['signatures'] = $sigs;
180 $out['fromAccounts'] = $email->et->getFromAccountsArray($ie);
181 $out['errorArray'] = array();
183 $oe = new OutboundEmail();
184 if( $oe->doesUserOverrideAccountRequireCredentials($current_user->id) )
186 $overideAccount = $oe->getUsersMailerForSystemOverride($current_user->id);
187 //If the user override account has not been created yet, create it for the user.
188 if($overideAccount == null)
189 $overideAccount = $oe->createUserSystemOverrideAccount($current_user->id);
191 $out['errorArray'] = array($overideAccount->id => $app_strings['LBL_EMAIL_WARNING_MISSING_USER_CREDS']);
194 $ret = $json->encode($out);
199 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSignature");
200 if(isset($_REQUEST['id'])) {
201 $signature = $current_user->getSignature($_REQUEST['id']);
202 $signature['signature_html'] = from_html($signature['signature_html']);
203 $out = $json->encode($signature);
210 case "deleteSignature":
211 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteSignature");
212 if(isset($_REQUEST['id'])) {
213 require_once("modules/Users/UserSignature.php");
214 $us = new UserSignature();
215 $us->mark_deleted($_REQUEST['id']);
216 $signatureArray = $current_user->getSignaturesArray();
218 foreach($signatureArray as $k => $v) {
220 $sigs[$k] = $app_strings['LBL_NONE'];
221 } else if (is_array($v) && isset($v['name'])){
222 $sigs[$k] = $v['name'];
227 $out['signatures'] = $signatureArray;
228 $ret = $json->encode($out);
234 case "getTemplateAttachments":
235 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getTemplateAttachments");
236 if(isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id'])) {
239 $where = "parent_id='{$_REQUEST['parent_id']}'";
242 $fullList = $seed->get_full_list($order, $where, '');
243 $all_fields = array_merge($seed->column_fields, $seed->additional_column_fields);
245 $js_fields_arr = array();
247 $i=1; // js doesn't like 0 index?
248 if (!empty($fullList)) {
249 foreach($fullList as $note) {
250 $js_fields_arr[$i] = array();
252 foreach($all_fields as $field) {
253 if(isset($note->$field)) {
254 $note->$field = from_html($note->$field);
255 $note->$field = preg_replace('/\r\n/','<BR>',$note->$field);
256 $note->$field = preg_replace('/\n/','<BR>',$note->$field);
257 $js_fields_arr[$i][$field] = addslashes($note->$field);
264 $out = $json->encode($js_fields_arr);
268 //// END COMPOSE REPLY FORWARD
269 ///////////////////////////////////////////////////////////////////////////
273 ///////////////////////////////////////////////////////////////////////////
274 //// MESSAGE HANDLING
277 $ie->retrieve($_REQUEST['ieId']);
278 $ie->mailbox = $_REQUEST['mailbox'];
279 $ie->connectMailserver();
281 switch($_REQUEST['type']) {
283 $title = "{$app_strings['LBL_EMAIL_VIEW_HEADERS']}";
284 $text = $ie->getFormattedHeaders($_REQUEST['uid']);
288 $title = "{$app_strings['LBL_EMAIL_VIEW_RAW']}";
289 $text = $ie->getFormattedRawSource($_REQUEST['uid']);
297 $ret['html'] = $text;
298 $ret['title'] = $title;
300 $out = $json->encode($ret, true);
304 case "getQuickCreateForm":
305 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getQuickCreateForm");
306 if(isset($_REQUEST['qc_module']) && !empty($_REQUEST['qc_module'])) {
307 if (!ACLController::checkAccess($_REQUEST['qc_module'],'edit', true)) {
308 echo trim($json->encode(array('html' => translate('LBL_NO_ACCESS', 'ACL')), true));
311 $people = array("Users", "Contacts", "Leads", "Prospects");
312 $showSaveToAddressBookButton = false;//(in_array($_REQUEST['qc_module'], $people)) ? true : false;
314 if(isset($_REQUEST['sugarEmail']) && !empty($_REQUEST['sugarEmail'])) {
315 $ie->email->retrieve($_REQUEST['uid']); // uid is a sugar GUID in this case
317 $ie->retrieve($_REQUEST['ieId']);
318 $ie->mailbox = $_REQUEST['mailbox'];
319 $ie->setEmailForDisplay($_REQUEST['uid']);
321 $ret = $email->et->getQuickCreateForm($_REQUEST, $ie->email, $showSaveToAddressBookButton);
322 $ret['ieId'] = $_REQUEST['ieId'];
323 $ret['mbox'] = $_REQUEST['mailbox'];
324 $ret['uid'] = $_REQUEST['uid'];
325 $ret['module'] = $_REQUEST['qc_module'];
326 if (!isset($_REQUEST['iframe'])) {
327 $out = trim($json->encode($ret, false));
335 case 'saveQuickCreate':
336 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveQuickCreate");
337 require_once('include/MVC/Controller/ControllerFactory.php');
338 if (isset($_REQUEST['qcmodule'])) {
339 $GLOBALS['log']->debug("********** QCmodule was set: {$_REQUEST['qcmodule']}");
341 $controller = ControllerFactory::getController($_REQUEST['qcmodule']);
342 $controller->loadBean();
343 $controller->pre_save();
344 $GLOBALS['log']->debug("********** saving new {$controller->module}");
345 $controller->action_save();
346 //Relate the email to the new bean
347 if(isset($_REQUEST['sugarEmail']) && $_REQUEST['sugarEmail'] == 'true' && isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
348 $ie->email->retrieve($_REQUEST['uid']);
349 } elseif(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
350 $GLOBALS['log']->debug("********** Quick Create from non-imported message");
351 $ie->retrieve($_REQUEST['ieId']);
352 $ie->mailbox = $_REQUEST['mbox'];
353 $ie->connectMailserver();
354 $uid = $_REQUEST['uid'];
355 if($ie->protocol == 'imap') {
356 $_REQUEST['uid'] = imap_msgno($ie->conn, $_REQUEST['uid']);
358 $_REQUEST['uid'] = $ie->getCorrectMessageNoForPop3($_REQUEST['uid']);
361 if (!$ie->importOneEmail($_REQUEST['uid'], $uid)) {
362 $ie->getDuplicateEmailId($_REQUEST['uid'], $uid);
364 $ie->email->retrieve($ie->email->id);
365 $GLOBALS['log']->debug("**********Imported Email");
366 $ie->email->assigned_user_id = $controller->bean->assigned_user_id;
367 $ie->email->assigned_user_name = $controller->bean->assigned_user_name;
369 if (isset($ie->email->id)) {
370 if (empty($ie->email->parent_id)) {
371 $ie->email->parent_id = $controller->bean->id;
372 $ie->email->parent_type = $controller->module;
374 $ie->email->status = 'read';
376 $mod = strtolower($controller->module);
377 $ie->email->load_relationship($mod);
378 $ie->email->$mod->add($controller->bean->id);
379 if($controller->bean->load_relationship('emails')) {
380 $controller->bean->emails->add($ie->email->id);
382 if ($controller->bean->module_dir == 'Cases') {
383 if($controller->bean->load_relationship('contacts')) {
384 $emailAddressWithName = $ie->email->from_addr_name;
385 if (!empty($ie->email->reply_to_addr)) {
386 $emailAddressWithName = $ie->email->reply_to_addr;
389 $emailAddress = SugarEmailAddress::_cleanAddress($emailAddressWithName);
390 $contactIds = $ie->email->emailAddress->getRelatedId($emailAddress, 'contacts');
391 if (!empty($contactIds)) {
392 $controller->bean->contacts->add($contactIds);
396 echo $json->encode(array('id' => $ie->email->id));
400 case "getImportForm":
401 $ie->retrieve($_REQUEST['ieId']);
402 // $ie->mailbox = $_REQUEST['mailbox'];
403 $ie->setEmailForDisplay($_REQUEST['uid']);
404 $ret = $email->et->getImportForm($_REQUEST, $ie->email);
405 $out = trim($json->encode($ret, false));
409 case "getRelateForm":
410 if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
411 $uids = $json->decode(from_html($_REQUEST['uid']));
412 $email->retrieve($uids[0]);
413 $ret = $email->et->getImportForm(array('showTeam' => false, 'showAssignTo' => false, 'showDelete' => false), $email,'RelateEditView');
414 $out = trim($json->encode($ret, false));
419 case "getEmail2DetailView":
420 if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
421 $ret = $email->et->getDetailViewForEmail2($_REQUEST['uid']);
422 if( !isset($_REQUEST['print']) || $_REQUEST['print'] === FALSE)
424 $out = trim($json->encode($ret, false));
434 if (isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) &&
435 isset($_REQUEST['parent_id']) && !empty($_REQUEST['parent_id']) &&
436 isset($_REQUEST['parent_type']) && !empty($_REQUEST['parent_type'])) {
437 $uids = explode($app_strings['LBL_EMAIL_DELIMITER'], $_REQUEST['uid']);
438 $mod = strtolower($_REQUEST['parent_type']);
439 $modId = $_REQUEST['parent_id'];
440 foreach($uids as $id) {
441 $email = new Email();
442 $email->retrieve($id);
443 $email->parent_id = $modId;
444 $email->parent_type = $_REQUEST['parent_type'];
445 $email->status = 'read';
447 $email->load_relationship($mod);
448 $email->$mod->add($modId);
454 case "getAssignmentDialogContent":
455 $out = $email->distributionForm("");
456 $out = trim($json->encode($out, false));
459 case "doAssignmentAssign":
460 $out = $email->et->doAssignment($_REQUEST['distribute_method'], $_REQUEST['ieId'], $_REQUEST['folder'], $_REQUEST['uids'], $_REQUEST['users']);
463 case "doAssignmentDelete";
464 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: doAssignmentDelete");
465 if(isset($_REQUEST['uids']) && !empty($_REQUEST['uids']) &&
466 isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId']) &&
467 isset($_REQUEST['folder']) && !empty($_REQUEST['folder'])) {
468 $email->et->markEmails("deleted", $_REQUEST['ieId'], $_REQUEST['folder'], $_REQUEST['uids']);
474 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: markEmail");
475 if(isset($_REQUEST['uids']) && !empty($_REQUEST['uids']) &&
476 isset($_REQUEST['type']) && !empty($_REQUEST['type']) &&
477 isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId']) &&
478 isset($_REQUEST['folder']) && !empty($_REQUEST['folder'])) {
479 $uid = $json->decode(from_html($_REQUEST['uids']));
484 if(strpos($uid, $app_strings['LBL_EMAIL_DELIMITER']) !== false) {
485 $uids = explode($app_strings['LBL_EMAIL_DELIMITER'], $uid);
490 $uids = implode($app_strings['LBL_EMAIL_DELIMITER'], $uids);
491 $GLOBALS['log']->debug("********** EMAIL 2.0 - Marking emails $uids as {$_REQUEST['type']}");
494 if(strpos($_REQUEST['folder'], 'sugar::') !== false && ($_REQUEST['type'] == 'deleted') && !ACLController::checkAccess('Emails', 'delete')) {
495 $ret['status'] = false;
496 $ret['message'] = $app_strings['LBL_EMAIL_DELETE_ERROR_DESC'];
498 $email->et->markEmails($_REQUEST['type'], $_REQUEST['ieId'], $_REQUEST['folder'], $uids);
499 $ret['status'] = true;
501 $out = trim($json->encode($ret, false));
508 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: checkEmail2");
510 $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
513 $ret['numberAccounts'] = count($showFolders);
515 $GLOBALS['log']->info("EMAIL2.0: async checkEmail - found [ ".$ret['numberAccounts']." ] accounts to check");
517 if(!empty($showFolders) && is_array($showFolders)) {
518 foreach($showFolders as $ieId) {
522 $GLOBALS['log']->info("INBOUNDEMAIL: trying to check email for GUID [ {$ieId} ]");
523 $ie->disconnectMailserver();
524 $ie->retrieve($ieId);
526 $ret[$ieId] = $ie->checkEmail2_meta();
530 $GLOBALS['log']->info("EMAIL2.0: at checkEmail() async call - not subscribed accounts to check.");
535 $out = $json->encode($ret, true);
540 $GLOBALS['log']->info("[EMAIL] - Start checkEmail action for user [{$current_user->user_name}]");
541 if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
542 $ie->retrieve($_REQUEST['ieId']);
543 $ie->mailbox = (isset($_REQUEST['mbox']) && !empty($_REQUEST['mbox'])) ? $_REQUEST['mbox'] : "INBOX";
544 $ie->checkEmail(false);
545 } elseif(isset($_REQUEST['all']) && !empty($_REQUEST['all'])) {
546 $showFolders = unserialize(base64_decode($current_user->getPreference('showFolders', 'Emails')));
548 $GLOBALS['log']->info("[EMAIL] - checkEmail found ".count($showFolders)." accounts to check for user [{$current_user->user_name}]");
550 if(!empty($showFolders) && is_array($showFolders)) {
551 foreach($showFolders as $ieId) {
554 $GLOBALS['log']->info("[EMAIL] - Start checking email for GUID [{$ieId}] for user [{$current_user->user_name}]");
555 $ie->disconnectMailserver();
556 $ie->retrieve($ieId);
557 $ie->checkEmail(false);
558 $GLOBALS['log']->info("[EMAIL] - Done checking email for GUID [{$ieId}] for user [{$current_user->user_name}]");
562 $GLOBALS['log']->info("EMAIL2.0: at checkEmail() async call - not subscribed accounts to check.");
566 $tree = $email->et->getMailboxNodes(true); // preserve cache files
567 $return = $tree->generateNodesRaw();
568 $out = $json->encode($return);
570 $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
573 case "checkEmailProgress":
574 $GLOBALS['log']->info("[EMAIL] - Start checkEmail action for user [{$current_user->user_name}]");
575 if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
576 $ie->retrieve($_REQUEST['ieId']);
577 $ie->mailbox = (isset($_REQUEST['mbox']) && !empty($_REQUEST['mbox'])) ? $_REQUEST['mbox'] : "INBOX";
578 $synch = (isset($_REQUEST['synch']) && ($_REQUEST['synch'] == "true"));
579 if (!$ie->is_personal) {
580 $return = array('status' => "done");
582 if ($ie->protocol == "pop3") {
583 $return = $ie->pop3_checkPartialEmail($synch);
585 $return = $ie->checkEmailIMAPPartial(false, $synch);
588 $return['ieid'] = $ie->id;
589 $return['synch'] = $synch;
590 if(isset($_REQUEST['totalcount']) && !empty($_REQUEST['totalcount']) && $_REQUEST['totalcount'] >= 0) {
591 if ($ie->protocol == "pop3") {
592 $return['totalcount'] = $_REQUEST['totalcount'];
595 echo $json->encode($return);
599 case "getAllFoldersTree":
600 $tree = $email->et->getMailboxNodes(true); // preserve cache files
601 $return = $tree->generateNodesRaw();
602 $out = $json->encode($return);
604 $GLOBALS['log']->info("[EMAIL] - Done checkEmail action for user [{$current_user->user_name}]");
607 case "synchronizeEmail":
608 $GLOBALS['log']->info("[EMAIL] Start action synchronizeEmail for user [{$current_user->user_name}]");
609 $ie->syncEmail(true);
610 $tree = $email->et->getMailboxNodes(true);
611 $return = $tree->generateNodesRaw();
612 $out = $json->encode($return);
614 $GLOBALS['log']->info("[EMAIL] Done action synchronizeEmail for user [{$current_user->user_name}]");
618 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: importEmail");
619 $ie->retrieve($_REQUEST['ieId']);
620 $ie->mailbox = $_REQUEST['mbox'];
621 $ie->connectMailserver();
625 if(strpos($_REQUEST['uid'], $app_strings['LBL_EMAIL_DELIMITER']) !== false) {
626 $exUids = explode($app_strings['LBL_EMAIL_DELIMITER'], $_REQUEST['uid']);
627 foreach($exUids as $msgNo) {
629 if($ie->protocol == 'imap') {
630 $msgNo = imap_msgno($ie->conn, $msgNo);
631 $status = $ie->importOneEmail($msgNo, $uid);
633 $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $uid);
635 $return[] = $app_strings['LBL_EMAIL_MESSAGE_NO'] . " " . $count . ", " . $app_strings['LBL_STATUS'] . " " . ($status ? $app_strings['LBL_EMAIL_IMPORT_SUCCESS'] : $app_strings['LBL_EMAIL_IMPORT_FAIL']);
637 if(($_REQUEST['delete'] == 'true') && $status && ($current_user->is_admin == 1 || $ie->group_id == $current_user->id)) {
638 $ie->deleteMessageOnMailServer($uid);
639 $ie->deleteMessageFromCache($uid);
643 $msgNo = $_REQUEST['uid'];
644 if($ie->protocol == 'imap') {
645 $msgNo = imap_msgno($ie->conn, $_REQUEST['uid']);
646 $status = $ie->importOneEmail($msgNo, $_REQUEST['uid']);
648 $status = $ie->importOneEmail($ie->getCorrectMessageNoForPop3($msgNo), $_REQUEST['uid']);
650 $return[] = $app_strings['LBL_EMAIL_MESSAGE_NO'] . " " . $count . ", " . $app_strings['LBL_STATUS'] . " " . ($status ? $app_strings['LBL_EMAIL_IMPORT_SUCCESS'] : $app_strings['LBL_EMAIL_IMPORT_FAIL']);
652 if(($_REQUEST['delete'] == 'true') && $status && ($current_user->is_admin == 1 || $ie->group_id == $current_user->id)) {
653 $ie->deleteMessageOnMailServer($_REQUEST['uid']);
654 $ie->deleteMessageFromCache($_REQUEST['uid']);
657 echo $json->encode($return);
661 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setReadFlag");
662 $ie->retrieve($_REQUEST['ieId']);
663 $ie->setReadFlagOnFolderCache($_REQUEST['mbox'], $_REQUEST['uid']);
664 $email->et->getListEmails($_REQUEST['ieId'], $_REQUEST['mbox'], 0, 'true');
665 //unlink("{$cacheRoot}/{$_REQUEST['ieId']}/folders/{$_REQUEST['mbox']}.php");
668 case "deleteMessage":
669 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteMessage");
670 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
671 $ie->retrieve($_REQUEST['ieId']);
672 $ie->mailbox = $_REQUEST['mbox'];
674 if($current_user->is_admin == 1 || $ie->group_id == $current_user->id) {
675 $ie->deleteMessageOnMailServer($_REQUEST['uid']);
676 $ie->deleteMessageFromCache($_REQUEST['uid']);
678 $GLOBALS['log']->debug("*** ERROR: tried to delete an email for an account for which {$current_user->full_name} is not the owner!");
679 echo "NOOP: error see log";
682 echo "error: missing credentials";
686 case "getSingleMessage":
687 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSingleMessage");
688 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
689 $out = $email->et->getSingleMessage($ie);
690 echo $json->encode($out);
692 echo "error: no UID";
696 case "getSingleMessageFromSugar":
697 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getSingleMessageFromSugar");
698 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
699 $email->retrieve($_REQUEST['uid']);
700 //$email->description_html = from_html($email->description_html);
703 if($email->status == 'draft' || $email->type == 'draft') {
704 // forcing an editview since we are looking at a draft message
705 global $current_user;
706 $ret = $ie->email->et->displayComposeEmail($email);
708 $ret = $ie->email->et->getDraftAttachments($ret, $ie);
709 $ret = $ie->email->et->getFromAllAccountsArray($ie, $ret);
712 $out = $json->encode($ret, true);
715 $out = $ie->displayOneEmail($_REQUEST['uid'], $_REQUEST['mbox']);
716 $out['meta']['email']['description'] =
717 empty($email->description_html) ? str_replace("\n", "\n<BR/>", $email->description) : $email->description_html;
718 $out['meta']['email']['date_start'] = $email->date_start;
719 $out['meta']['email']['time_start'] = $email->time_start;
720 $out['meta']['ieId'] = $_REQUEST['ieId'];
721 $out['meta']['mbox'] = $_REQUEST['mbox'];
722 $out['meta']['email']['toaddrs'] = $email->et->generateExpandableAddrs($out['meta']['email']['toaddrs']);
723 if(!empty($out['meta']['email']['cc_addrs'])) {
724 $ccs = $email->et->generateExpandableAddrs($out['meta']['email']['cc_addrs']);
725 $out['meta']['cc'] = <<<eoq
727 <td NOWRAP valign="top" class="displayEmailLabel">
728 {$app_strings['LBL_EMAIL_CC']}:
730 <td class="displayEmailValue">
736 echo $json->encode($out);
739 echo "error: no UID";
743 case "getMultipleMessages":
744 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessages");
745 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
746 $exUids = explode(",", $_REQUEST['uid']);
749 foreach($exUids as $k => $uid) {
750 if($email->et->validCacheFileExists($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php")) {
751 $msg = $email->et->getCacheValue($_REQUEST['ieId'], 'messages', $_REQUEST['mbox'].$uid.".php", 'out');
753 $ie->retrieve($_REQUEST['ieId']);
754 $ie->mailbox = $_REQUEST['mbox'];
755 $ie->setEmailForDisplay($uid, false, true);
756 $msg = $ie->displayOneEmail($uid, $_REQUEST['mbox']);
757 $email->et->writeCacheFile('out', $msg, $_REQUEST['ieId'], 'messages', "{$_REQUEST['mbox']}{$uid}.php");
762 echo $json->encode($out);
764 echo "error: no UID";
768 case "getMultipleMessagesFromSugar":
769 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMultipleMessagesFromSugar");
770 if(isset($_REQUEST['uid']) && !empty($_REQUEST['uid'])) {
771 $exIds = explode(",", $_REQUEST['uid']);
774 foreach($exIds as $id) {
777 $e->description_html = from_html($e->description_html);
779 $out[] = $ie->displayOneEmail($id, $_REQUEST['mbox']);
782 echo $json->encode($out);
786 //// END MESSAGE HANDLING
787 ///////////////////////////////////////////////////////////////////////////
791 ///////////////////////////////////////////////////////////////////////////
793 case "getMessageCount":
794 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageCount");
796 $out = $ie->getCacheCount($_REQUEST['mbox']);
797 echo $json->encode($out);
800 case "getMessageList":
801 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListJSON");
802 if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
803 // user view preferences
804 $email->et->saveListView($_REQUEST['ieId'], $_REQUEST['mbox']);
806 $ie->retrieve($_REQUEST['ieId']);
807 if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
808 $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
812 $list = $ie->displayFolderContents($_REQUEST['mbox'], $_REQUEST['forceRefresh'], $page);
813 $count = $ie->getCacheCount($_REQUEST['mbox']);
814 $unread = $ie->getCacheUnread($_REQUEST['mbox']);
815 $out = $email->et->jsonOuput($list, 'Email', $count, true, $unread);
823 echo "error: no ieID";
827 case "getMessageListSugarFolders":
828 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getMessageListSugarFoldersJSON");
829 if(isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
830 // user view preferences
831 $email->et->saveListView($_REQUEST['ieId'], "SUGAR.{$_REQUEST['mbox']}");
832 if(isset($_REQUEST['start']) && isset($_REQUEST['limit'])) {
833 $page = ceil($_REQUEST['start'] / $_REQUEST['limit']) + 1;
837 if(!isset($_REQUEST['sort']) || !isset($_REQUEST['dir'])) {
838 $_REQUEST['sort'] = '';
839 $_REQUEST['dir'] = '';
841 $emailSettings = $current_user->getPreference('emailSettings', 'Emails');
842 // cn: default to a low number until user specifies otherwise
843 if(empty($emailSettings['showNumInList'])) {
844 $emailSettings['showNumInList'] = 20;
847 // jchi #9424, get sort and direction from user preference
850 $sortSerial = $current_user->getPreference('folderSortOrder', 'Emails');
851 if(!empty($sortSerial) && !empty($_REQUEST['ieId']) && !empty($_REQUEST['mbox'])) {
852 $sortArray = unserialize($sortSerial);
853 $GLOBALS['log']->debug("********** EMAIL 2.0********** ary=".print_r($sortArray,true).' id='.$_REQUEST['ieId'].'; box='.$_REQUEST['mbox']);
854 $sort = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['sort'];
855 $direction = $sortArray[$_REQUEST['ieId']][$_REQUEST['mbox']]['current']['direction'];
857 //set sort and direction to user predference
858 if(!empty($_REQUEST['sort']) && !empty($_REQUEST['dir'])) {
859 $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['mbox'], $_REQUEST['sort'], $_REQUEST['dir']);
860 $sort = $_REQUEST['sort'];
861 $direction = $_REQUEST['dir'];
863 $_REQUEST['sort'] = '';
864 $_REQUEST['dir'] = '';
868 $metalist = $email->et->folder->getListItemsForEmailXML($_REQUEST['ieId'], $page,
869 $emailSettings['showNumInList'], $sort, $direction);
870 $count = $email->et->folder->getCountItems($_REQUEST['ieId']);
871 $unread = $email->et->folder->getCountUnread($_REQUEST['ieId']);
872 $out = $email->et->jsonOuput($metalist, 'Email', $count, false, $unread);
879 echo "error: no ieID";
883 ///////////////////////////////////////////////////////////////////////////
887 ///////////////////////////////////////////////////////////////////////////
890 $email->et->emptyTrash($ie);
893 case "clearInboundAccountCache":
894 $email->et->clearInboundAccountCache($_REQUEST['ieId']);
897 case "updateSubscriptions":
898 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: updateSubscriptions");
899 if(isset($_REQUEST['subscriptions']) && !empty($_REQUEST['subscriptions']))
901 $subs = explode("::", $_REQUEST['subscriptions']);
902 $childrenSubs = array();
903 //Find all children of the group folder subscribed to and add
904 //them to the list of folders to show.
905 foreach ($subs as $singleSub)
906 $email->et->folder->findAllChildren($singleSub, $childrenSubs);
908 $subs = array_merge($subs, $childrenSubs);
909 $email->et->folder->setSubscriptions($subs);
911 elseif(empty($_REQUEST['subscriptions'])) {
912 $email->et->folder->clearSubscriptions();
916 case "refreshSugarFolders":
917 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: refreshSugarFolders");
918 $rootNode = new ExtNode('','');
919 $folderOpenState = $current_user->getPreference('folderOpenState', 'Emails');
920 $folderOpenState = (empty($folderOpenState)) ? "" : $folderOpenState;
921 $ret = $email->et->folder->getUserFolders($rootNode, unserialize($folderOpenState), $current_user, true);
922 $out = $json->encode($ret);
928 case "getFoldersForSettings":
929 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getFoldersForSettings");
930 $ret = $email->et->folder->getFoldersForSettings($current_user);
931 $out = $json->encode($ret);
936 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveEmails");
937 $ie->moveEmails($_REQUEST['sourceIeId'], $_REQUEST['sourceFolder'], $_REQUEST['destinationIeId'], $_REQUEST['destinationFolder'], $_REQUEST['emailUids']);
940 case "saveNewFolder":
941 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveNewFolder");
942 if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
943 switch($_REQUEST['folderType']) {
945 $ret = $email->et->saveNewFolder($_REQUEST['nodeLabel'], $_REQUEST['parentId']);
946 $out = $json->encode($ret);
951 $ie->retrieve($_REQUEST['ieId']);
952 $ie->connectMailserver();
953 $ie->saveNewFolder($_REQUEST['newFolderName'], $_REQUEST['mbox']);
957 echo "NOOP: no folderType defined";
961 case "setFolderViewSelection": // flows into next case statement
962 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderViewSelection");
963 $viewFolders = $_REQUEST['ieIdShow'];
964 $current_user->setPreference('showFolders', base64_encode(serialize($viewFolders)), '', 'Emails');
965 $tree = $email->et->getMailboxNodes(false);
966 $return = $tree->generateNodesRaw();
967 $out = $json->encode($return);
972 $v = $app_strings['LBL_NONE'];
974 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteFolder");
975 if(isset($_REQUEST['folderType']) && !empty($_REQUEST['folderType'])) {
976 switch($_REQUEST['folderType']) {
978 $status = $email->et->folder->deleteChildrenCascade($_REQUEST['folder_id']);
979 if ($status == true) {
980 $return['status'] = true;
981 $return['folder_id'] = $_REQUEST['folder_id'];
983 $return['status'] = false;
984 $return['errorMessage'] = $app_strings['LBL_EMAIL_ERROR_DELETE_GROUP_FOLDER'];
989 $ie->retrieve($_REQUEST['ieId']);
990 $ie->connectMailserver();
991 $returnArray = $ie->deleteFolder($_REQUEST['mbox']);
992 $status = $returnArray['status'];
993 $errorMessage = $returnArray['errorMessage'];
994 if ($status == true) {
995 $return['status'] = true;
996 $return['mbox'] = $_REQUEST['mbox'];
997 $return['ieId'] = $_REQUEST['ieId'];
999 $return['status'] = false;
1000 $return['errorMessage'] = $errorMessage;
1005 $return['status'] = false;
1006 $return['errorMessage'] = "NOOP: no folderType defined";
1008 $out = $json->encode($return);
1011 case "renameFolder":
1012 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: renameFolder");
1014 if(isset($_REQUEST['ieId']) && isset($_REQUEST['oldFolderName']) && !empty($_REQUEST['oldFolderName'])
1015 && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1016 $ie->retrieve($_REQUEST['ieId']);
1017 $ie->renameFolder($_REQUEST['oldFolderName'], $_REQUEST['newFolderName']);
1018 } elseif(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newFolderName']) && !empty($_REQUEST['newFolderName'])) {
1019 if(is_guid($_REQUEST['folderId'])) {
1020 $email->et->folder->retrieve($_REQUEST['folderId']);
1021 $email->et->folder->name = $_REQUEST['newFolderName'];
1022 $email->et->folder->save();
1024 echo "NOOP - not a Sugar Folder";
1028 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: moveFolder");
1029 if(isset($_REQUEST['folderId']) && !empty($_REQUEST['folderId']) && isset($_REQUEST['newParentId']) && !empty($_REQUEST['newParentId']) && $_REQUEST['newParentId'] != $_REQUEST['folderId']) {
1030 if(is_guid($_REQUEST['folderId']) && is_guid($_REQUEST['newParentId'])) {
1031 $email->et->folder->retrieve($_REQUEST['folderId']);
1032 $email->et->folder->updateFolder(array(
1033 "record" => $_REQUEST['folderId'],
1034 "name" => $email->et->folder->name,
1035 "parent_folder" => $_REQUEST['newParentId'],
1036 "team_id" => $email->et->folder->team_id,
1037 "team_set_id" => $email->et->folder->team_set_id,
1040 echo "NOOP - not a Sugar Folder";
1044 case "getGroupFolder":
1045 $email->et->folder->retrieve($_REQUEST['folderId']);
1046 $_REQUEST['record'] = $_REQUEST['folderId'];
1048 $ret['folderId'] = $email->et->folder->id;
1049 $ret['folderName'] = $email->et->folder->name;
1050 $ret['parentFolderId'] = $email->et->folder->parent_folder;
1051 $out = $json->encode($ret);
1056 case "rebuildFolders":
1057 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildFolders");
1058 $tree = $email->et->getMailboxNodes(false);
1059 $return = $tree->generateNodesRaw();
1060 $out = $json->encode($return);
1064 case "setFolderOpenState":
1065 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setFolderOpenState");
1066 $email->et->saveFolderOpenState($_REQUEST['focusFolder'], $_REQUEST['focusFolderOpen']);
1069 case "saveListViewSortOrder":
1070 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveListViewSortOrder");
1071 $email->et->saveListViewSortOrder($_REQUEST['ieId'], $_REQUEST['focusFolder'], $_REQUEST['sortBy'], $_REQUEST['reverse']);
1073 //// END FOLDER ACTIONS
1074 ///////////////////////////////////////////////////////////////////////////
1076 ///////////////////////////////////////////////////////////////////////////
1077 //// INBOUND EMAIL ACCOUNTS
1079 case "retrieveAllOutbound":
1080 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: retrieveAllOutbound");
1081 global $current_user;
1082 $oe = new OutboundEmail();
1083 $outbounds = $oe->getUserMailers($current_user);
1084 $results = array('outbound_account_list' => $outbounds, 'count' => count($outbounds));
1085 $out = $json->encode($results, false);
1090 case "editOutbound":
1091 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editOutbound");
1092 if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email'])) {
1093 $oe = new OutboundEmail();
1094 $oe->retrieve($_REQUEST['outbound_email']);
1098 foreach($oe->field_defs as $def) {
1099 $ret[$def] = $oe->$def;
1101 $ret['mail_smtppass']=''; // don't send back the password
1103 $out = $json->encode($ret, true);
1111 case "deleteOutbound":
1112 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteOutbound");
1113 if(isset($_REQUEST['outbound_email']) && !empty($_REQUEST['outbound_email']))
1115 $oe = new OutboundEmail();
1116 global $current_user;
1117 $oe->retrieve($_REQUEST['outbound_email']);
1118 $affectedInboundAccounts = $oe->getAssociatedInboundAccounts($current_user);
1120 //Check if the user has confirmed he wants to delete the email account even if associated to an inbound accnt.
1121 $confirmedDelete = ( isset($_REQUEST['confirm']) && $_REQUEST['confirm'] ) ? TRUE : FALSE;
1123 if( count($affectedInboundAccounts) > 0 && !$confirmedDelete)
1125 $results = array('is_error' => true, 'error_message' => $app_strings['LBL_EMAIL_REMOVE_SMTP_WARNING'] , 'outbound_email' => $_REQUEST['outbound_email']);
1130 $results = array('is_error' => false, 'error_message' => '');
1133 $out = $json->encode($results);
1146 case "saveOutbound":
1147 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveOutbound");
1149 $oe = new OutboundEmail();
1150 $oe->id = $_REQUEST['mail_id'];
1151 $oe->retrieve($oe->id);
1152 $oe->name = $_REQUEST['mail_name'];
1153 $type = empty($_REQUEST['type']) ? 'user' : $_REQUEST['type'];
1155 $oe->user_id = $current_user->id;
1156 $oe->mail_sendtype = "SMTP";
1157 $oe->mail_smtptype = $_REQUEST['mail_smtptype'];
1158 $oe->mail_smtpserver = $_REQUEST['mail_smtpserver'];
1159 $oe->mail_smtpport = $_REQUEST['mail_smtpport'];
1160 $oe->mail_smtpssl = $_REQUEST['mail_smtpssl'];
1161 $oe->mail_smtpauth_req = isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0;
1162 $oe->mail_smtpuser = $_REQUEST['mail_smtpuser'];
1163 if(!empty($_REQUEST['mail_smtppass'])) {
1164 $oe->mail_smtppass = $_REQUEST['mail_smtppass'];
1170 case "saveDefaultOutbound":
1171 global $current_user;
1172 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveDefaultOutbound");
1173 $outbound_id = empty($_REQUEST['id']) ? "" : $_REQUEST['id'];
1174 $ie = new InboundEmail();
1175 $ie->setUsersDefaultOutboundServerId($current_user, $outbound_id);
1177 case "testOutbound":
1178 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: testOutbound");
1181 if(!empty($_REQUEST['mail_smtppass'])) {
1182 $pass = $_REQUEST['mail_smtppass'];
1183 } elseif(isset($_REQUEST['mail_name'])) {
1184 $oe = new OutboundEmail();
1185 $oe = $oe->getMailerByName($current_user, $_REQUEST['mail_name']);
1187 $pass = $oe->mail_smtppass;
1190 $out = $email->sendEmailTest($_REQUEST['mail_smtpserver'], $_REQUEST['mail_smtpport'], $_REQUEST['mail_smtpssl'],
1191 (isset($_REQUEST['mail_smtpauth_req']) ? 1 : 0), $_REQUEST['mail_smtpuser'],
1192 $pass, $_REQUEST['outboundtest_from_address'], $_REQUEST['outboundtest_from_address']);
1194 $out = $json->encode($out);
1198 case "rebuildShowAccount":
1199 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1200 $ret = $email->et->getShowAccountsOptions($ie);
1201 $results = array('account_list' => $ret, 'count' => count($ret));
1202 $out = $json->encode($results);
1206 case "rebuildShowAccountForSearch":
1207 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: rebuildShowAccount");
1208 $ret = $email->et->getShowAccountsOptionsForSearch($ie);
1209 $out = $json->encode($ret);
1213 case "deleteIeAccount":
1214 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: deleteIeAccount");
1215 if(isset($_REQUEST['group_id']) && $_REQUEST['group_id'] == $current_user->id) {
1217 $ret['id'] = $_REQUEST['ie_id'];
1218 $out = $json->encode($ret);
1219 $ie->hardDelete($_REQUEST['ie_id']);
1220 $out = $json->encode(array('id' => $_REQUEST['ie_id']));
1223 foreach($showFolders as $id) {
1224 if($id != $_REQUEST['ie_id'])
1225 $updateFolders[] = $id;
1228 $showStore = base64_encode(serialize($updateFolders));
1229 $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1233 case "saveIeAccount":
1234 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveIeAccount");
1235 if(isset($_REQUEST['server_url']) && !empty($_REQUEST['server_url'])) {
1236 if(false === $ie->savePersonalEmailAccount($current_user->id, $current_user->user_name, false)) {
1237 $ret = array('error' => 'error');
1238 $out = $json->encode($ret);
1241 $ie->retrieve($_REQUEST['ie_id']);
1242 if (!isset($ie->created_by_link)) {
1243 $ie->created_by_link = null;
1245 if (!isset($ie->modified_user_id_link)) {
1246 $ie->modified_user_id_link = null;
1248 if (!is_array($showFolders)) {
1249 $showFolders = array();
1251 if(!in_array($ie->id, $showFolders)) {
1252 $showFolders[] = $ie->id;
1253 $showStore = base64_encode(serialize($showFolders));
1254 $current_user->setPreference('showFolders', $showStore, 0, 'Emails');
1257 foreach($ie->field_defs as $k => $v) {
1258 if (isset($v['type']) && ($v['type'] == 'link')) {
1261 if($k == 'stored_options') {
1262 $ie->$k = unserialize(base64_decode($ie->$k));
1263 if (isset($ie->stored_options['from_name'])) {
1264 $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1266 } elseif($k == 'service') {
1267 $service = explode("::", $ie->$k);
1268 $retService = array();
1270 foreach($service as $serviceK => $serviceV) {
1271 if(!empty($serviceV)) {
1272 $retService[$serviceK] = $serviceV;
1276 $ie->$k = $retService;
1283 $out = $json->encode($ret);
1287 //If the user is saving the username/password then we need to update the outbound account.
1288 $outboundMailUser = (isset($_REQUEST['mail_smtpuser'])) ? $_REQUEST['mail_smtpuser'] : "";
1289 $outboundMailPass = (isset($_REQUEST['mail_smtppass'])) ? $_REQUEST['mail_smtppass'] : "";
1290 $outboundMailId = (isset($_REQUEST['outbound_email'])) ? $_REQUEST['outbound_email'] : "";
1292 if( !empty($outboundMailUser) && !empty($outboundMailPass) && !empty($outboundMailId) )
1294 $oe = new OutboundEmail();
1295 $oe->retrieve($outboundMailId);
1296 $oe->mail_smtpuser = $outboundMailUser;
1297 $oe->mail_smtppass = $outboundMailPass;
1306 case "getIeAccount":
1307 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getIeAccount");
1308 $ie->retrieve($_REQUEST['ieId']);
1309 if($ie->group_id == $current_user->id) {
1312 foreach($ie->field_defs as $k => $v) {
1313 if($k == 'stored_options') {
1314 $ie->$k = unserialize(base64_decode($ie->$k));
1315 if (isset($ie->stored_options['from_name'])) {
1316 $ie->stored_options['from_name'] = from_html($ie->stored_options['from_name']);
1318 } elseif($k == 'service') {
1319 $service = explode("::", $ie->$k);
1320 $retService = array();
1321 foreach($service as $serviceK => $serviceV) {
1322 if(!empty($serviceV)) {
1323 $retService[$serviceK] = $serviceV;
1327 $ie->$k = $retService;
1332 unset($ret['email_password']); // no need to send the password out
1334 $out = $json->encode($ret);
1336 $out = "NOOP: ID mismatch";
1340 //// END INBOUND EMAIL ACCOUNTS
1341 ///////////////////////////////////////////////////////////////////////////
1345 ///////////////////////////////////////////////////////////////////////////
1348 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: search");
1349 if(isset($_REQUEST['subject']) && !empty($_REQUEST['subject']) && isset($_REQUEST['ieId']) && !empty($_REQUEST['ieId'])) {
1350 $metalist = $ie->search($_REQUEST['ieId'], $_REQUEST['subject']);
1351 if (!isset($_REQUEST['page'])) {
1352 $_REQUEST['page'] = 1;
1354 $_REQUEST['pageSize'] = count($metalist['out']);
1355 $out = $email->et->xmlOutput($metalist, 'Email', false);
1358 header("Content-type: text/xml");
1363 echo "NOOP: no search criteria found";
1367 case "searchAdvanced":
1368 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: searchAdvanced");
1370 $metalist = $email->searchImportedEmails();
1371 $out = $email->et->jsonOuput($metalist, 'Email', $metalist['totalCount']);
1381 ///////////////////////////////////////////////////////////////////////////
1385 ///////////////////////////////////////////////////////////////////////////
1387 case "loadPreferences":
1388 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: loadPreferences");
1389 $prefs = $email->et->getUserPrefsJS();
1390 $out = $json->encode($prefs);
1394 case "saveSettingsGeneral":
1395 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveSettingsGeneral");
1396 $emailSettings = array();
1397 $emailSettings['emailCheckInterval'] = $_REQUEST['emailCheckInterval'];
1398 //$emailSettings['autoImport'] = isset($_REQUEST['autoImport']) ? '1' : '0';
1399 $emailSettings['alwaysSaveOutbound'] = '1';
1400 $emailSettings['sendPlainText'] = isset($_REQUEST['sendPlainText']) ? '1' : '0';
1401 $emailSettings['showNumInList'] = $_REQUEST['showNumInList'];
1402 $emailSettings['defaultOutboundCharset'] = $_REQUEST['default_charset'];
1403 $current_user->setPreference('emailSettings', $emailSettings, '', 'Emails');
1406 $current_user->setPreference('signature_default', $_REQUEST['signature_id']);
1407 $current_user->setPreference('signature_prepend', (isset($_REQUEST['signature_prepend'])) ? true : false);
1409 $out = $json->encode($email->et->getUserPrefsJS());
1413 case "setPreference":
1414 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: setPreference");
1415 if(isset($_REQUEST['prefName']) && isset($_REQUEST['prefValue'])) {
1416 // handle potential JSON encoding
1417 if(isset($_REQUEST['decode'])) {
1418 $_REQUEST['prefValue'] = $json->decode(from_html($_REQUEST['prefValue']));
1421 $current_user->setPreference($_REQUEST['prefName'], $_REQUEST['prefValue'], '', 'Emails');
1425 ///////////////////////////////////////////////////////////////////////////
1430 ///////////////////////////////////////////////////////////////////////////
1434 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: editContact");
1435 if(isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
1436 $module = "Contacts";
1437 $ret = $email->et->getEditContact($_REQUEST['id'], $module);
1439 $out = $json->encode($ret);
1444 /* The four calls below all have the same return signature */
1445 case "removeContact":
1446 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: removeContacts");
1447 if(strpos($_REQUEST['ids'], "::") !== false) {
1448 $removeIds = explode("::", $_REQUEST['ids']);
1450 $removeIds[] = $_REQUEST['ids'];
1452 $email->et->removeContacts($removeIds);
1454 case "saveContactEdit":
1455 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: saveContactEdit");
1456 if(isset($_REQUEST['args']) && !empty($_REQUEST['args'])) {
1457 $email->et->saveContactEdit($_REQUEST['args']);
1459 // flow into getUserContacts();
1461 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1462 $contacts = array();
1464 if(isset($_REQUEST['bean_module']) && !empty($_REQUEST['bean_module']) && isset($_REQUEST['bean_id']) && !empty($_REQUEST['bean_id'])) {
1465 $contacts[$_REQUEST['bean_id']] = array(
1466 'id' => $_REQUEST['bean_id'],
1467 'module' => $_REQUEST['bean_module']
1469 $email->et->setContacts($contacts);
1472 case "addContactsMultiple":
1473 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: addContacts");
1474 if (isset($_REQUEST['contactData'])) {
1475 $contacts = $json->decode(from_HTML($_REQUEST['contactData']));
1478 $email->et->setContacts($contacts);
1482 case "getUserContacts":
1483 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: getUserContacts");
1484 $contacts = $email->et->getContacts();
1486 if(is_array($contacts) && !empty($contacts)) {
1487 $ret = $email->et->getUserContacts($contacts, $current_user);
1488 $out = $json->encode($ret);
1496 // address book search
1497 case "getUnionData":
1500 if(isset($_REQUEST['first_name']) && !empty($_REQUEST['first_name'])) {
1501 $wheres['first_name'] = $_REQUEST['first_name'];
1503 if(isset($_REQUEST['last_name']) && !empty($_REQUEST['last_name'])) {
1504 $wheres['last_name'] = $_REQUEST['last_name'];
1506 if(isset($_REQUEST['email_address']) && !empty($_REQUEST['email_address'])) {
1507 $wheres['email_address'] = $_REQUEST['email_address'];
1509 if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1510 $person = $_REQUEST['person'];
1512 $q = $email->et->_getPeopleUnionQuery($wheres , $person);
1513 $r = $ie->db->limitQuery($q, 0, 25, true);
1516 while($a = $ie->db->fetchByAssoc($r)) {
1518 $person['id'] = $a['id'];
1519 $person['module'] = $a['module'];
1520 $person['full_name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1521 $person['email_address'] = $a['email_address'];
1525 $ret = $json->encode($out, true);
1529 case "getAddressSearchResults":
1531 $person = 'contacts';
1532 $relatedBeanInfo = '';
1533 if(isset($_REQUEST['related_bean_id']) && !empty($_REQUEST['related_bean_id'])) {
1534 $isRelatedSearch = true;
1535 $relatedBeanInfo['related_bean_id'] = $_REQUEST['related_bean_id'];
1536 $relatedBeanInfo['related_bean_type'] = ucfirst($_REQUEST['related_bean_type']);
1539 if (isset($_REQUEST['search_field'])) {
1540 $wheres['first_name'] = $_REQUEST['search_field'];
1541 $wheres['last_name'] = $_REQUEST['search_field'];
1542 $wheres['email_address'] = $_REQUEST['search_field'];
1545 if(isset($_REQUEST['person']) && !empty($_REQUEST['person'])) {
1546 $person = $_REQUEST['person'];
1548 if(isset($_REQUEST['start']) && !empty($_REQUEST['start'])) {
1549 $start = $_REQUEST['start'];
1554 $qArray = $email->et->getRelatedEmail($person, $wheres, $relatedBeanInfo);
1557 if (!empty($qArray['query'])) {
1558 $countq = $qArray['countQuery'];
1559 $time = microtime(true);
1560 $r = $ie->db->query($countq);
1561 $GLOBALS['log']->debug("***QUERY counted in " . (microtime(true) - $time) . " milisec\n");
1562 if($row = $GLOBALS['db']->fetchByAssoc($r)){
1565 $time = microtime(true);
1567 //Handle sort and order requests
1568 $sort = !empty($_REQUEST['sort']) ? $_REQUEST['sort'] : "id";
1569 $sort = ($sort == 'bean_id') ? 'id' : $sort;
1570 $sort = ($sort == 'email') ? 'email_address' : $sort;
1571 $sort = ($sort == 'name') ? 'last_name' : $sort;
1572 $direction = !empty($_REQUEST['dir']) ? $_REQUEST['dir'] : "asc";
1573 $order = ( !empty($sort) && !empty($direction) ) ? " ORDER BY {$sort} {$direction}" : "";
1575 $r = $ie->db->limitQuery($qArray['query'] . " $order ", $start, 25, true);
1576 $GLOBALS['log']->debug("***QUERY Got results in " . (microtime(true) - $time) . " milisec\n");
1579 while($a = $ie->db->fetchByAssoc($r)) {
1581 $person['bean_id'] = $a['id'];
1582 $person['bean_module'] = $a['module'];
1583 $person['name'] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name'],'');
1584 $person['email'] = $a['email_address'];
1588 $ret = $email->et->jsonOuput(array('out' => $out), 'Person', $count);
1596 //// END ADDRESS BOOK
1597 ///////////////////////////////////////////////////////////////////////////
1601 ///////////////////////////////////////////////////////////////////////////
1605 $GLOBALS['log']->debug("********** EMAIL 2.0 - Asynchronous - at: default");