]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/InboundEmail/Save.php
Release 6.2.0
[Github/sugarcrm.git] / modules / InboundEmail / Save.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 require_once('include/SugarFolders/SugarFolders.php');
39
40 global $current_user;
41
42 $focus = new InboundEmail();
43 if(!empty($_REQUEST['record'])) {
44     $focus->retrieve($_REQUEST['record']);
45 } elseif(!empty($_REQUEST['origin_id'])) {
46     $focus->retrieve($_REQUEST['origin_id']);
47     unset($focus->id);
48 }
49 foreach($focus->column_fields as $field) {
50     if($field == 'email_password' && empty($_REQUEST['email_password']) && !empty($_REQUEST['email_user'])) {
51         continue;
52     }
53         if(isset($_REQUEST[$field])) {
54                 if ($field != "group_id") {
55                         $focus->$field = trim($_REQUEST[$field]);
56                 }
57         }
58 }
59 foreach($focus->additional_column_fields as $field) {
60         if(isset($_REQUEST[$field])) {
61                 $value = trim($_REQUEST[$field]);
62                 $focus->$field = $value;
63         }
64 }
65 foreach($focus->required_fields as $field) {
66         if(isset($_REQUEST[$field])) {
67                 $value = trim($_REQUEST[$field]);
68                 $focus->$field = $value;
69         }
70 }
71 if(!empty($_REQUEST['email_password'])) {
72     $focus->email_password = $_REQUEST['email_password'];
73 }
74
75 $focus->protocol = $_REQUEST['protocol'];
76
77 if( isset($_REQUEST['is_create_case']) && $_REQUEST['is_create_case'] == 'on' )
78     $focus->mailbox_type = 'createcase';
79 else
80 {
81     if( empty($focus->mailbox_type) || $focus->mailbox_type == 'createcase' )
82         $focus->mailbox_type = 'pick';
83 }
84
85 /////////////////////////////////////////////////////////
86 ////    SERVICE STRING CONCATENATION
87 $useSsl = (isset($_REQUEST['ssl']) && $_REQUEST['ssl'] == 1) ? true : false;
88 $optimum = $focus->getSessionConnectionString($focus->server_url, $focus->email_user, $focus->port, $focus->protocol);
89 if (empty($optimum)) {
90         $optimum = $focus->findOptimumSettings($useSsl, $focus->email_user, $focus->email_password, $focus->server_url, $focus->port, $focus->protocol, $focus->mailbox);
91 } // if
92 $delimiter = $focus->getSessionInboundDelimiterString($focus->server_url, $focus->email_user, $focus->port, $focus->protocol);
93
94 //added check to ensure the $optimum['serial']) is not empty.
95 if(is_array($optimum) && (count($optimum) > 0) && !empty( $optimum['serial'])) {
96         $focus->service = $optimum['serial'];
97 } else {
98         // no save
99         // allowing bad save to allow Email Campaigns configuration to continue even without IMAP
100         $focus->service = "::::::".$focus->protocol."::::"; // save bogus info.
101         $error = "&error=true";
102 }
103 ////    END SERVICE STRING CONCAT
104 /////////////////////////////////////////////////////////
105
106 if(isset($_REQUEST['mark_read']) && $_REQUEST['mark_read'] == 1) {
107         $focus->delete_seen = 0;
108 } else {
109         $focus->delete_seen = 0;
110 }
111
112 // handle stored_options serialization
113 if(isset($_REQUEST['only_since']) && $_REQUEST['only_since'] == 1) {
114         $onlySince = true;
115 } else {
116         $onlySince = false;
117 }
118 $stored_options = array();
119 $stored_options['from_name'] = trim($_REQUEST['from_name']);
120 $stored_options['from_addr'] = trim($_REQUEST['from_addr']);
121 $stored_options['reply_to_name'] = trim($_REQUEST['reply_to_name']);
122 $stored_options['reply_to_addr'] = trim($_REQUEST['reply_to_addr']);
123 $stored_options['only_since'] = $onlySince;
124 $stored_options['filter_domain'] = $_REQUEST['filter_domain'];
125 $stored_options['email_num_autoreplies_24_hours'] = $_REQUEST['email_num_autoreplies_24_hours'];
126 $stored_options['allow_outbound_group_usage'] = isset($_REQUEST['allow_outbound_group_usage']) ? true : false;
127
128 if (!$focus->isPop3Protocol()) {
129         $stored_options['trashFolder'] = (isset($_REQUEST['trashFolder']) ? trim($_REQUEST['trashFolder']) : "");
130         $stored_options['sentFolder'] = (isset($_REQUEST['sentFolder']) ? trim($_REQUEST['sentFolder']) : "");
131 } // if
132 if ( $focus->isMailBoxTypeCreateCase() || ($focus->mailbox_type == 'createcase' && empty($_REQUEST['id']) ) )
133 {
134         $stored_options['distrib_method'] = (isset($_REQUEST['distrib_method'])) ? $_REQUEST['distrib_method'] : "";
135         $stored_options['create_case_email_template'] = (isset($_REQUEST['create_case_template_id'])) ? $_REQUEST['create_case_template_id'] : "";
136 } // if
137 $storedOptions['folderDelimiter'] = $delimiter;
138
139 ////////////////////////////////////////////////////////////////////////////////
140 ////    CREATE MAILBOX QUEUE
141 ////////////////////////////////////////////////////////////////////////////////
142 if (!isset($focus->id)) {
143         $groupId = "";
144         if (isset($_REQUEST['group_id']) && empty($_REQUEST['group_id'])) {
145                 $groupId = $_REQUEST['group_id'];
146         } else {
147                 $groupId = create_guid();
148         }
149         $focus->group_id = $groupId;
150 }
151
152
153 if( isset($_REQUEST['is_auto_import']) && $_REQUEST['is_auto_import'] == 'on' )
154 {
155     if( empty($focus->groupfolder_id) )
156     {
157         $groupFolderId = $focus->createAutoImportSugarFolder();
158         $focus->groupfolder_id = $groupFolderId;
159     }
160 }
161 else
162 {
163     $focus->groupfolder_id = "";
164     //If the user is turning the auto-import feature off then remove all previous subscriptions.
165     if( !empty($focus->fetched_row['groupfolder_id'] ) )
166     {
167         $GLOBALS['log']->debug("Clearining all subscriptions to folder id: {$focus->fetched_row['groupfolder_id']}");
168         $f = new SugarFolder();
169         $f->clearSubscriptionsForFolder($focus->fetched_row['groupfolder_id']);
170         //Now delete the old group folder.
171         $f->retrieve($focus->fetched_row['groupfolder_id']);
172         $f->delete();
173     }
174 }
175
176 if (!empty($focus->groupfolder_id))
177 {
178         if ($_REQUEST['leaveMessagesOnMailServer'] == "1")
179                 $stored_options['leaveMessagesOnMailServer'] = 1;
180         else
181                 $stored_options['leaveMessagesOnMailServer'] = 0;
182 }
183
184 $focus->stored_options = base64_encode(serialize($stored_options));
185 $GLOBALS['log']->info('----->InboundEmail now saving self');
186
187
188 ////////////////////////////////////////////////////////////////////////////////
189 ////    SEND US TO SAVE DESTINATION
190 ////////////////////////////////////////////////////////////////////////////////
191
192 //When an admin is creating an IE account we do not want their private team to be added
193 //or they may be included in a round robin assignment.
194 $previousTeamAccessCheck = isset($GLOBALS['sugar_config']['disable_team_access_check']) ? $GLOBALS['sugar_config']['disable_team_access_check'] : null;
195 $GLOBALS['sugar_config']['disable_team_access_check'] = TRUE;
196
197 $focus->save();
198
199 //Reset the value so no other saves are affected.
200 $GLOBALS['sugar_config']['disable_team_access_check'] = $previousTeamAccessCheck;
201
202
203
204 //Sync any changes within the IE account that need to be synced with the Sugar Folder.
205 //Need to do this post save so the correct team/teamset id is generated correctly.
206 $monitor_fields = array('name', 'status',
207                         );
208
209 //Only sync IE accounts with a group folder.  Need to sync new records as team set assignment is processed
210 //after save.
211 if( !empty($focus->groupfolder_id) )
212 {
213     foreach ($monitor_fields as $singleField)
214     {
215         //Check if the value is being changed during save.
216         if($focus->fetched_row[$singleField] != $focus->$singleField)
217             syncSugarFoldersWithBeanChanges($singleField, $focus);
218     }
219 }
220
221 $_REQUEST['return_id'] = $focus->id;
222
223
224 $edit='';
225 if(isset($_REQUEST['return_module']) && $_REQUEST['return_module'] != "") {
226         $return_module = $_REQUEST['return_module'];
227 } else {
228         $return_module = "InboundEmail";
229 }
230 if(isset($_REQUEST['return_action']) && $_REQUEST['return_action'] != "") {
231         $return_action = $_REQUEST['return_action'];
232 } else {
233         $return_action = "DetailView";
234 }
235 if(isset($_REQUEST['return_id']) && $_REQUEST['return_id'] != "") {
236         $return_id = $_REQUEST['return_id'];
237 }
238 if(!empty($_REQUEST['edit'])) {
239         $return_id='';
240         $edit='&edit=true';
241 }
242
243 $GLOBALS['log']->debug("Saved record with id of ".$return_id);
244
245
246 header("Location: index.php?module=$return_module&action=$return_action&record=$return_id$edit$error");
247
248
249 /**
250  * Certain updates to the IE account need to be reflected in the related SugarFolder since they are
251  * created automatically.  Only valid for IE accounts with auto import turned on.
252  *
253  * @param string $fieldName The field name that changed
254  * @param SugarBean $focus The InboundEmail bean being saved.
255  */
256 function syncSugarFoldersWithBeanChanges($fieldName, $focus)
257 {
258     $f = new SugarFolder();
259     $f->retrieve($focus->groupfolder_id);
260
261     switch ($fieldName)
262     {
263         case 'name':
264         case 'team_id':
265         case 'team_set_id':
266             $f->$fieldName = $focus->$fieldName;
267             $f->save();
268             break;
269
270         case 'status':
271             if($focus->status == 'Inactive')
272                 $f->clearSubscriptionsForFolder($focus->groupfolder_id);
273             else if($focus->mailbox_type != 'bounce' )
274                 $f->addSubscriptionsToGroupFolder();
275             break;
276     }
277 }
278