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