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 *********************************************************************************/
50 var $field_defs = array(
70 var $type; // user or system
71 var $user_id; // owner
72 var $mail_sendtype; // smtp
75 var $mail_smtpport = 25;
78 var $mail_smtpauth_req; // bool
79 var $mail_smtpssl; // bool
80 var $mail_smtpdisplay; // calculated value, not in DB
81 var $new_with_id = FALSE;
86 function OutboundEmail() {
87 $this->db = DBManagerFactory::getInstance();
91 * Retrieves the mailer for a user if they have overriden the username
92 * and password for the default system account.
94 * @param String $user_id
96 function getUsersMailerForSystemOverride($user_id)
98 $query = "SELECT id FROM outbound_email WHERE user_id = '{$user_id}' AND type = 'system-override' ORDER BY name";
99 $rs = $this->db->query($query);
100 $row = $this->db->fetchByAssoc($rs);
101 if(!empty($row['id']))
103 $oe = new OutboundEmail();
104 $oe->retrieve($row['id']);
112 * Duplicate the system account for a user, setting new parameters specific to the user.
114 * @param string $user_id
115 * @param string $user_name
116 * @param string $user_pass
118 function createUserSystemOverrideAccount($user_id,$user_name = "",$user_pass = "")
120 $ob = $this->getSystemMailerSettings();
121 $ob->id = create_guid();
122 $ob->new_with_id = TRUE;
123 $ob->user_id = $user_id;
124 $ob->type = 'system-override';
125 $ob->mail_smtpuser = $user_name;
126 $ob->mail_smtppass = $user_pass;
133 * Determines if a user needs to set their user name/password for their system
136 * @param unknown_type $user_id
139 function doesUserOverrideAccountRequireCredentials($user_id)
141 $userCredentialsReq = FALSE;
142 $sys = new OutboundEmail();
143 $ob = $sys->getSystemMailerSettings(); //Dirties '$this'
145 //If auth for system account is disabled or user can use system outbound account return false.
146 if($ob->mail_smtpauth_req == 0 || $this->isAllowUserAccessToSystemDefaultOutbound() || $this->mail_sendtype == 'sendmail')
147 return $userCredentialsReq;
149 $userOverideAccount = $this->getUsersMailerForSystemOverride($user_id);
150 if( $userOverideAccount == null || empty($userOverideAccount->mail_smtpuser) || empty($userOverideAccount->mail_smtpuser) )
151 $userCredentialsReq = TRUE;
153 return $userCredentialsReq;
158 * Retrieves name value pairs for opts lists
160 function getUserMailers($user) {
163 $q = "SELECT * FROM outbound_email WHERE user_id = '{$user->id}' AND type = 'user' ORDER BY name";
164 $r = $this->db->query($q);
168 $system = $this->getSystemMailerSettings();
170 //Now add the system default or user override default to the response.
171 if(!empty($system->id) )
173 if ($system->mail_sendtype == 'SMTP')
176 $userSystemOverride = $this->getUsersMailerForSystemOverride($user->id);
178 //If the user is required to to provide a username and password but they have not done so yet,
179 //create the account for them.
180 $autoCreateUserSystemOverride = FALSE;
181 if( $this->doesUserOverrideAccountRequireCredentials($user->id) )
183 $systemErrors = $app_strings['LBL_EMAIL_WARNING_MISSING_USER_CREDS'];
184 $autoCreateUserSystemOverride = TRUE;
187 //Substitute in the users system override if its available.
188 if($userSystemOverride != null)
189 $system = $userSystemOverride;
190 else if ($autoCreateUserSystemOverride)
191 $system = $this->createUserSystemOverrideAccount($user->id,"","");
193 $isEditable = ($system->type == 'system') ? FALSE : TRUE; //User overrides can be edited.
195 if( !empty($system->mail_smtpserver) )
196 $ret[] = array('id' =>$system->id, 'name' => "$system->name", 'mail_smtpserver' => $system->mail_smtpdisplay,
197 'is_editable' => $isEditable, 'type' => $system->type, 'errors' => $systemErrors);
201 $ret[] = array('id' =>$system->id, 'name' => "{$system->name} - sendmail", 'mail_smtpserver' => 'sendmail',
202 'is_editable' => false, 'type' => $system->type, 'errors' => '');
206 while($a = $this->db->fetchByAssoc($r))
209 if($a['mail_sendtype'] != 'SMTP')
213 $oe['name'] = $a['name'];
214 $oe['type'] = $a['type'];
215 $oe['is_editable'] = true;
217 if ( !empty($a['mail_smtptype']) )
218 $oe['mail_smtpserver'] = $this->_getOutboundServerDisplay($a['mail_smtptype'],$a['mail_smtpserver']);
220 $oe['mail_smtpserver'] = $a['mail_smtpserver'];
229 * Retrieves a cascading mailer set
231 * @param string mailer_id
234 function getUserMailerSettings(&$user, $mailer_id='', $ieId='') {
237 if(!empty($mailer_id)) {
238 $mailer = "AND id = '{$mailer_id}'";
239 } elseif(!empty($ieId)) {
240 $q = "SELECT stored_options FROM inbound_email WHERE id = '{$ieId}'";
241 $r = $this->db->query($q);
242 $a = $this->db->fetchByAssoc($r);
245 $opts = unserialize(base64_decode($a['stored_options']));
247 if(isset($opts['outbound_email'])) {
248 $mailer = "AND id = '{$opts['outbound_email']}'";
253 $q = "SELECT id FROM outbound_email WHERE user_id = '{$user->id}' {$mailer}";
254 $r = $this->db->query($q);
255 $a = $this->db->fetchByAssoc($r);
258 $ret = $this->getSystemMailerSettings();
260 $ret = $this->retrieve($a['id']);
266 * Retrieve an array containing inbound emails ids for all inbound email accounts which have
267 * their outbound account set to this object.
269 * @param SugarBean $user
270 * @param string $outbound_id
273 function getAssociatedInboundAccounts($user)
275 $query = "SELECT id,stored_options FROM inbound_email WHERE is_personal='1' AND deleted='0' AND created_by = '{$user->id}'";
276 $rs = $this->db->query($query);
279 while($row = $this->db->fetchByAssoc($rs) )
281 $opts = unserialize(base64_decode($row['stored_options']));
282 if( isset($opts['outbound_email']) && $opts['outbound_email'] == $this->id)
284 $results[] = $row['id'];
291 * Retrieves a cascading mailer set
293 * @param string mailer_id
296 function getInboundMailerSettings($user, $mailer_id='', $ieId='') {
299 if(!empty($mailer_id)) {
300 $mailer = "id = '{$mailer_id}'";
301 } elseif(!empty($ieId)) {
302 $q = "SELECT stored_options FROM inbound_email WHERE id = '{$ieId}'";
303 $r = $this->db->query($q);
304 $a = $this->db->fetchByAssoc($r);
307 $opts = unserialize(base64_decode($a['stored_options']));
309 if(isset($opts['outbound_email'])) {
310 $mailer = "id = '{$opts['outbound_email']}'";
312 $mailer = "id = '{$ieId}'";
315 // its possible that its an system account
316 $mailer = "id = '{$ieId}'";
320 if (empty($mailer)) {
321 $mailer = "type = 'system'";
324 $q = "SELECT id FROM outbound_email WHERE {$mailer}";
325 $r = $this->db->query($q);
326 $a = $this->db->fetchByAssoc($r);
329 $ret = $this->getSystemMailerSettings();
331 $ret = $this->retrieve($a['id']);
337 * Determine if the user is alloweed to use the current system outbound connection.
339 function isAllowUserAccessToSystemDefaultOutbound()
341 $allowAccess = FALSE;
343 // first check that a system default exists
344 $q = "SELECT id FROM outbound_email WHERE type = 'system'";
345 $r = $this->db->query($q);
346 $a = $this->db->fetchByAssoc($r);
348 // next see if the admin preference for using the system outbound is set
349 $admin = new Administration();
350 $admin->retrieveSettings('',TRUE);
351 if (isset($admin->settings['notify_allow_default_outbound'])
352 && $admin->settings['notify_allow_default_outbound'] == 2 )
360 * Retrieves the system's Outbound options
362 function getSystemMailerSettings() {
363 $q = "SELECT id FROM outbound_email WHERE type = 'system'";
364 $r = $this->db->query($q);
365 $a = $this->db->fetchByAssoc($r);
369 $this->name = 'system';
370 $this->type = 'system';
371 $this->user_id = '1';
372 $this->mail_sendtype = 'SMTP';
373 $this->mail_smtptype = 'other';
374 $this->mail_smtpserver = '';
375 $this->mail_smtpport = 25;
376 $this->mail_smtpuser = '';
377 $this->mail_smtppass = '';
378 $this->mail_smtpauth_req = 1;
379 $this->mail_smtpssl = 0;
380 $this->mail_smtpdisplay = $this->_getOutboundServerDisplay($this->mail_smtptype,$this->mail_smtpserver);
384 $ret = $this->retrieve($a['id']);
391 * Populates this instance
393 * @return object $this
395 function retrieve($id) {
396 require_once('include/utils/encryption_utils.php');
397 $q = "SELECT * FROM outbound_email WHERE id = '{$id}'";
398 $r = $this->db->query($q);
399 $a = $this->db->fetchByAssoc($r);
402 foreach($a as $k => $v) {
403 if ($k == 'mail_smtppass' && !empty($v)) {
404 $this->$k = blowfishDecode(blowfishGetKey('OutBoundEmail'), $v);
409 if ( !empty($a['mail_smtptype']) )
410 $this->mail_smtpdisplay = $this->_getOutboundServerDisplay($a['mail_smtptype'],$a['mail_smtpserver']);
412 $this->mail_smtpdisplay = $a['mail_smtpserver'];
418 function populateFromPost() {
419 foreach($this->field_defs as $def) {
420 if(isset($_POST[$def])) {
421 $this->$def = $_POST[$def];
432 require_once('include/utils/encryption_utils.php');
433 if(empty($this->id) || $this->new_with_id) {
435 if( empty($this->id) )
436 $this->id = create_guid();
441 foreach($this->field_defs as $def) {
445 if(!empty($values)) {
449 if ($def == 'mail_smtppass' && !empty($this->$def)) {
450 $this->$def = blowfishEncode(blowfishGetKey('OutBoundEmail'), $this->$def);
452 if($def == 'mail_smtpauth_req' || $def == 'mail_smtpssl'){
453 if(empty($this->$def)){
456 $values .= $this->$def;
458 $values .= "'{$this->$def}'";
462 $q = "INSERT INTO outbound_email ($cols) VALUES ({$values})";
465 foreach($this->field_defs as $def) {
466 if ($def == 'mail_smtppass') {
467 if(!empty($this->mail_smtppass)) {
468 $this->mail_smtppass = blowfishEncode(blowfishGetKey('OutBoundEmail'), $this->mail_smtppass);
470 // ignore empty password unless username is empty too
471 if(!empty($this->mail_smtpuser)) {
476 if(!empty($values)) {
480 if($def == 'mail_smtpauth_req' || $def == 'mail_smtpssl'){
481 if(empty($this->$def)){
484 $values .= "{$def} = {$this->$def}";
486 $values .= "{$def} = '{$this->$def}'";
490 $q = "UPDATE outbound_email SET {$values} WHERE id = '{$this->id}'";
493 $this->db->query($q, true);
498 * Saves system mailer. Presumes all values are filled.
500 function saveSystem() {
501 $q = "SELECT id FROM outbound_email WHERE type = 'system'";
502 $r = $this->db->query($q);
503 $a = $this->db->fetchByAssoc($r);
506 $a['id'] = ''; // trigger insert
509 $this->id = $a['id'];
510 $this->name = 'system';
511 $this->type = 'system';
512 $this->user_id = '1';
515 $this->updateUserSystemOverrideAccounts();
520 * Update the user system override accounts with the system information if anything has changed.
523 function updateUserSystemOverrideAccounts()
525 $updateFields = array('mail_smtptype','mail_sendtype','mail_smtpserver', 'mail_smtpport','mail_smtpauth_req','mail_smtpssl');
527 //Update the username ans password for the override accounts if alloweed access.
528 if( $this->isAllowUserAccessToSystemDefaultOutbound() )
530 $updateFields[] = 'mail_smtpuser';
531 $updateFields[] = 'mail_smtppass';
535 foreach ($updateFields as $singleField)
539 if($singleField == 'mail_smtpauth_req' || $singleField == 'mail_smtpssl')
541 if(empty($this->$singleField))
542 $this->$singleField = 0;
544 $values .= "{$singleField} = {$this->$singleField} ";
547 $values .= "{$singleField} = '{$this->$singleField}' ";
550 $query = "UPDATE outbound_email set {$values} WHERE type='system-override' ";
552 $this->db->query($query);
555 * Remove all of the user override accounts.
558 function removeUserOverrideAccounts()
560 $query = "DELETE FROM outbound_email WHERE type = 'system-override'";
561 return $this->db->query($query);
564 * Deletes an instance
567 if(empty($this->id)) {
571 $q = "DELETE FROM outbound_email WHERE id = '{$this->id}'";
572 return $this->db->query($q);
575 private function _getOutboundServerDisplay(
584 return $app_strings['LBL_SMTPTYPE_YAHOO']; break;
586 return $app_strings['LBL_SMTPTYPE_GMAIL']; break;
588 return $smtpserver . ' - ' . $app_strings['LBL_SMTPTYPE_EXCHANGE']; break;
590 return $smtpserver; break;
595 * Get mailer for current user by name
597 * @param string $name
598 * @return OutboundEmail|false
600 public function getMailerByName($user, $name)
602 if($name == "system" && !$this->isAllowUserAccessToSystemDefaultOutbound()) {
603 $oe = $this->getUsersMailerForSystemOverride($user->id);
604 if(!empty($oe) && !empty($oe->id)) {
608 $res = $this->db->query("SELECT id FROM outbound_email WHERE user_id = '{$user->id}' AND name='".$this->db->quote($name)."'");
609 $a = $this->db->fetchByAssoc($res);
610 if(!isset($a['id'])) {
613 return $this->retrieve($a['id']);