コード例 #1
0
 /**
  * Edit email account
  *
  * @access public
  * @param void
  * @return null
  */
 function edit_account()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $this->setTemplate('add_account', $this->plugin_name);
     $mailAccount = MailAccounts::findById(get_id());
     if (!$mailAccount instanceof MailAccount) {
         flash_error(lang('mailAccount dnx'));
         ajx_current("empty");
         return;
     }
     // if
     if (!$mailAccount->canEdit(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     // get mail account users
     $mailAccountUsers = MailAccountContacts::getByAccount($mailAccount);
     $mau = array();
     foreach ($mailAccountUsers as $au) {
         $contact = $au->getContact();
         if (!$contact instanceof Contact) {
             continue;
         }
         $mau[$au->getContactId()] = array('name' => $contact->getObjectName(), 'can_edit' => $au->getCanEdit());
     }
     tpl_assign('mailAccountUsers', $mau);
     $is_admin = logged_user()->isAdministrator();
     tpl_assign('is_admin', $is_admin);
     $mailAccount_data = array_var($_POST, 'mailAccount');
     if (!is_array($mailAccount_data)) {
         $mailAccount_data = array('user_id' => logged_user()->getId(), 'name' => $mailAccount->getName(), 'email' => $mailAccount->getEmail(), 'email_addr' => $mailAccount->getEmailAddress(), 'password' => MailUtilities::ENCRYPT_DECRYPT($mailAccount->getPassword()), 'server' => $mailAccount->getServer(), 'is_imap' => $mailAccount->getIsImap(), 'incoming_ssl' => $mailAccount->getIncomingSsl(), 'incoming_ssl_port' => $mailAccount->getIncomingSslPort(), 'smtp_server' => $mailAccount->getSmtpServer(), 'smtp_port' => $mailAccount->getSmtpPort(), 'smtp_username' => $mailAccount->getSmtpUsername(), 'smtp_password' => MailUtilities::ENCRYPT_DECRYPT($mailAccount->getSmtpPassword()), 'smtp_use_auth' => $mailAccount->getSmtpUseAuth(), 'del_from_server' => $mailAccount->getDelFromServer(), 'mark_read_on_server' => $mailAccount->getMarkReadOnServer(), 'outgoing_transport_type' => $mailAccount->getOutgoingTrasnportType());
         // array
         if (config_option('sent_mails_sync')) {
             $sync_details = array('sync_server' => $mailAccount->getSyncServer(), 'sync_addr' => $mailAccount->getSyncAddr(), 'sync_pass' => MailUtilities::ENCRYPT_DECRYPT($mailAccount->getSyncPass()), 'sync_ssl' => $mailAccount->getSyncSsl(), 'sync_sslport' => $mailAccount->getSyncSslPort());
             $mailAccount_data = array_merge($mailAccount_data, $sync_details);
         }
     } else {
         if (!isset($mailAccount_data['sync_ssl'])) {
             $mailAccount_data['sync_ssl'] = false;
         }
         if (!isset($mailAccount_data['incoming_ssl'])) {
             $mailAccount_data['incoming_ssl'] = false;
         }
         if (!isset($mailAccount_data['is_default'])) {
             $mailAccount_data['is_default'] = false;
         }
     }
     if ($mailAccount->getIsImap()) {
         /*try {
         			$real_folders = MailUtilities::getImapFolders($mailAccount);
         			DB::beginWork();
         			foreach ($real_folders as $folder_name) {
         				if (!MailAccountImapFolders::findById(array('account_id' => $mailAccount->getId(), 'folder_name' => $folder_name))) {
         					$acc_folder = new MailAccountImapFolder();
         					$acc_folder->setAccountId($mailAccount->getId());
         					$acc_folder->setFolderName($folder_name);
         					$acc_folder->setCheckFolder($folder_name == 'INBOX');// By default only INBOX is checked
         				 
         					$acc_folder->save();
         				}
         			}
         			DB::commit();
         		} catch (Exception $e) {
         			DB::rollback();
         			flash_error($e->getMessage());
         		}*/
         $imap_folders = MailAccountImapFolders::getMailAccountImapFolders($mailAccount->getId());
         tpl_assign('imap_folders', $imap_folders);
     }
     tpl_assign('mailAccount', $mailAccount);
     tpl_assign('mailAccount_data', $mailAccount_data);
     if (array_var($_POST, 'submitted')) {
         try {
             $user_changed = false;
             $selected_user = array_var($_POST, 'users_select_box');
             if (!$is_admin) {
                 $selected_user = $mailAccount->getContactId();
             }
             $mail_account_user = Contacts::findById($selected_user);
             if ($mail_account_user instanceof Contact) {
                 $old_user_id = $mailAccount->getContactId();
                 if ($old_user_id != $mail_account_user->getId()) {
                     $user_changed = true;
                 }
                 $mailAccount_data['user_id'] = $mail_account_user->getId();
             }
             $mailAccount_data['sync_ssl'] = array_var($mailAccount_data, 'sync_ssl') == "checked";
             DB::beginWork();
             $logged_user_settings = MailAccountContacts::getByAccountAndContact($mailAccount, logged_user());
             $logged_user_can_edit = $logged_user_settings instanceof MailAccountContact && $logged_user_settings->getCanEdit() || $mailAccount->getContactId() == logged_user()->getId() || logged_user()->isAdministrator();
             if ($logged_user_can_edit || $is_admin) {
                 if (!array_var($mailAccount_data, 'del_mails_from_server', false)) {
                     $mailAccount_data['del_from_server'] = 0;
                 }
                 if (!array_var($mailAccount_data, 'mark_read_on_server', false)) {
                     $mailAccount_data['mark_read_on_server'] = 0;
                 }
                 $mailAccount->setFromAttributes($mailAccount_data);
                 $mailAccount->setServer(trim($mailAccount->getServer()));
                 $mailAccount->setPassword(MailUtilities::ENCRYPT_DECRYPT($mailAccount->getPassword()));
                 $mailAccount->setSmtpPassword(MailUtilities::ENCRYPT_DECRYPT($mailAccount->getSmtpPassword()));
                 $outbox_folder = array_var($_POST, 'outbox_select_box');
                 if (config_option("sent_mails_sync") && isset($outbox_folder)) {
                     $mailAccount->setSyncPass(MailUtilities::ENCRYPT_DECRYPT($mailAccount_data['sync_pass']));
                     $mailAccount->setSyncFolder($outbox_folder);
                 }
                 //in case there is a new owner of the email account
                 if ($user_changed && $mail_account_user instanceof Contact) {
                     DB::executeAll("UPDATE " . TABLE_PREFIX . "objects SET created_by_id=" . $mail_account_user->getId() . " WHERE  \r\n\t\t\t\t\t\t\t`created_by_id` = '{$old_user_id}' AND (select `account_id` FROM " . TABLE_PREFIX . "mail_contents mc WHERE mc.object_id=id) = " . $mailAccount->getId());
                     $mailAccount->setContactId($mail_account_user->getId());
                 }
                 //If imap, save folders to check
                 if ($mailAccount->getIsImap() && is_array(array_var($_POST, 'check'))) {
                     $checks = array_var($_POST, 'check');
                     $names = array();
                     foreach ($checks as $name => $checked) {
                         $name = str_replace(array('¡', '!'), array('[', ']'), $name);
                         //to avoid a mistaken array if name contains [
                         $names[] = $name;
                         $imap_folder = MailAccountImapFolders::instance()->findOne(array('conditions' => array('folder_name = ? AND account_id = ?', $name, $mailAccount->getId())));
                         if (!$imap_folder instanceof MailAccountImapFolder) {
                             $imap_folder = new MailAccountImapFolder();
                             $imap_folder->setAccountId($mailAccount->getId());
                             $imap_folder->setFolderName($name);
                         }
                         $imap_folder->setCheckFolder($checked == 'checked');
                         $imap_folder->save();
                     }
                     if (count($names) > 0) {
                         DB::execute("UPDATE " . TABLE_PREFIX . "mail_account_imap_folder SET check_folder=0 WHERE account_id=" . $mailAccount->getId() . " AND folder_name NOT IN ('" . implode("','", $names) . "')");
                     }
                 }
                 $member_ids = json_decode(array_var($_POST, 'members'));
                 $member_ids_str = "";
                 foreach ($member_ids as $mid) {
                     if (is_numeric($mid)) {
                         $member_ids_str .= ($member_ids_str == "" ? "" : ",") . $mid;
                     }
                 }
                 $mailAccount->setMemberId($member_ids_str);
                 $mailAccount->save();
                 // process users
                 $account_users = Contacts::findAll();
                 $user_access = array_var($_POST, 'user_access');
                 foreach ($account_users as $account_user) {
                     $user_id = $account_user->getId();
                     $access = array_var($user_access, $user_id, 'none');
                     $account_user = MailAccountContacts::getByAccountAndContact($mailAccount, $account_user);
                     if ($mail_account_user instanceof Contact && ($access != 'none' || $user_id == $mail_account_user->getId())) {
                         if (!$account_user instanceof MailAccountContact) {
                             $account_user = new MailAccountContact();
                             $account_user->setAccountId($mailAccount->getId());
                             $account_user->setContactId($user_id);
                         }
                         $account_user->setCanEdit($access == 'write');
                         $account_user->save();
                     } else {
                         if ($account_user instanceof MailAccountContact) {
                             $account_user->delete();
                         }
                     }
                 }
                 /*// delete any remaining ones
                 		$account_users = MailAccountContacts::getByAccount($mailAccount);
                 		foreach ($account_users as $account_user) {
                 			if ($access = array_var($user_access, $account_user->getId(), 'none') == 'none') {
                 				$account_user->delete();
                 			}
                 		}*/
                 evt_add("mail account edited", array("id" => $mailAccount->getId(), "name" => $mailAccount->getName(), "email" => $mailAccount->getEmail()));
             }
             // personal settings
             if (array_var($_POST, 'is_default')) {
                 $user_accounts = MailAccountContacts::getByContact(logged_user());
                 foreach ($user_accounts as $acc) {
                     if ($acc->getAccountId() != $mailAccount->getId()) {
                         $acc->setIsDefault(false);
                         $acc->save();
                     } else {
                         $acc->setIsDefault(true);
                         $acc->save();
                     }
                 }
             }
             $logged_user_settings = MailAccountContacts::getByAccountAndContact($mailAccount, logged_user());
             if ($logged_user_settings instanceof MailAccountContact) {
                 $logged_user_settings->setSignature(array_var($_POST, 'signature'));
                 $logged_user_settings->setSenderName(array_var($_POST, 'sender_name'));
                 $logged_user_settings->save();
             }
             DB::commit();
             flash_success(lang('success edit mail account', $mailAccount->getName()));
             ajx_current("back");
             // Error...
         } catch (Exception $e) {
             DB::rollback();
             ajx_current("empty");
             flash_error($e->getMessage());
         }
         // try
     }
     // if
 }
コード例 #2
0
	/**
	 * Edit email account
	 *
	 * @access public
	 * @param void
	 * @return null
	 */
	function edit_account() {
		if (logged_user()->isGuest()) {
			flash_error(lang('no access permissions'));
			ajx_current("empty");
			return;
		}
		$this->setTemplate('add_account', $this->plugin_name);

		$mailAccount = MailAccounts::findById(get_id());
		if(!($mailAccount instanceof MailAccount)) {
			flash_error(lang('mailAccount dnx'));
			ajx_current("empty");
			return;
		} // if

		if(!$mailAccount->canEdit(logged_user())) {
			flash_error(lang('no access permissions'));
			ajx_current("empty");
			return;
		} // if

		// get mail account users
		$mailAccountUsers = MailAccountContacts::getByAccount($mailAccount);
		$mau = array();
		foreach ($mailAccountUsers as $au) {
			$contact = $au->getContact();
			if (!$contact instanceof Contact) continue;
			
			$mau[$au->getContactId()] = array(
				'name' => $contact->getObjectName(),
				'can_edit' => $au->getCanEdit(),
			);
		}
		tpl_assign('mailAccountUsers', $mau);
		
		$is_admin = logged_user()->isAdministrator();
		tpl_assign('is_admin', $is_admin);
		
		$mailAccount_data = array_var($_POST, 'mailAccount');
		if(!is_array($mailAccount_data)) {
			$mailAccount_data = array(
		          'user_id' => logged_user()->getId(),
		          'name' => $mailAccount->getName(),
		          'email' => $mailAccount->getEmail(),
		          'email_addr' => $mailAccount->getEmailAddress(),
		          'password' => MailUtilities::ENCRYPT_DECRYPT($mailAccount->getPassword()),
		          'server' => $mailAccount->getServer(),
		          'is_imap' => $mailAccount->getIsImap(),
		          'incoming_ssl' => $mailAccount->getIncomingSsl(),
		          'incoming_ssl_port' => $mailAccount->getIncomingSslPort(),
		          'smtp_server' => $mailAccount->getSmtpServer(),
		          'smtp_port' => $mailAccount->getSmtpPort(),
		          'smtp_username' => $mailAccount->getSmtpUsername(),
		          'smtp_password' => MailUtilities::ENCRYPT_DECRYPT($mailAccount->getSmtpPassword()),
		          'smtp_use_auth' => $mailAccount->getSmtpUseAuth(),
		          'del_from_server' => $mailAccount->getDelFromServer(),
		          'outgoing_transport_type' => $mailAccount->getOutgoingTrasnportType(),
				  'workspace' => $mailAccount->getColumnValue('workspace',0),			
			); // array
			if(config_option('sent_mails_sync')){								
				$sync_details = array('sync_server' => $mailAccount->getSyncServer(),
				  'sync_addr' => $mailAccount->getSyncAddr(),
				  'sync_pass' => MailUtilities::ENCRYPT_DECRYPT($mailAccount->getSyncPass()),
				  'sync_ssl' => $mailAccount->getSyncSsl(),
				  'sync_sslport' => $mailAccount->getSyncSslPort());
				$mailAccount_data = array_merge ($mailAccount_data, $sync_details);
			}
		} else {
			if (!isset($mailAccount_data['sync_ssl']))
				$mailAccount_data['sync_ssl'] = false;
			if (!isset($mailAccount_data['incoming_ssl']))
				$mailAccount_data['incoming_ssl'] = false;
			if (!isset($mailAccount_data['is_default']))
				$mailAccount_data['is_default'] = false;
		}
		
		if ($mailAccount->getIsImap()) {
			try {
				$real_folders = MailUtilities::getImapFolders($mailAccount);
				DB::beginWork();
				foreach ($real_folders as $folder_name) {
					if (!MailAccountImapFolders::findById(array('account_id' => $mailAccount->getId(), 'folder_name' => $folder_name))) {
						$acc_folder = new MailAccountImapFolder();
						$acc_folder->setAccountId($mailAccount->getId());
						$acc_folder->setFolderName($folder_name);
						$acc_folder->setCheckFolder($folder_name == 'INBOX');// By default only INBOX is checked
					 
						$acc_folder->save();
					}
				}
				DB::commit();
			} catch (Exception $e) {
				DB::rollback();
				flash_error($e->getMessage());
			}
			 
			$imap_folders = MailAccountImapFolders::getMailAccountImapFolders($mailAccount->getId());
			tpl_assign('imap_folders', $imap_folders);
		}

		tpl_assign('mailAccount', $mailAccount);
		tpl_assign('mailAccount_data', $mailAccount_data);

		if(array_var($_POST, 'submitted')) {
			try {
				$user_changed = false;
				$selected_user = array_var($_POST, 'users_select_box');					
				if (!$is_admin){
					$mail_account_user = Contacts::findById($mailAccount->getContactId());
				}
				else{
					$mail_account_user = Contacts::findById($selected_user);
					$old_user_id = $mailAccount->getContactId();					
					if ($old_user_id != $mail_account_user->getId())
						$user_changed = true;
				}
				$mailAccount_data['user_id'] = $mail_account_user->getId();
				$mailAccount_data['sync_ssl'] = array_var($mailAccount_data, 'sync_ssl') == "checked";
				
				DB::beginWork();
				$logged_user_settings = MailAccountContacts::getByAccountAndContact($mailAccount, logged_user());
				$logged_user_can_edit = $logged_user_settings instanceof MailAccountContact && $logged_user_settings->getCanEdit() || $mailAccount->getContactId() == logged_user()->getId();
				if ($logged_user_can_edit) {
					if (!array_var($mailAccount_data, 'del_mails_from_server', false)) $mailAccount_data['del_from_server'] = 0;
					$mailAccount->setFromAttributes($mailAccount_data);
					$mailAccount->setPassword(MailUtilities::ENCRYPT_DECRYPT($mailAccount->getPassword()));
					$mailAccount->setSmtpPassword(MailUtilities::ENCRYPT_DECRYPT($mailAccount->getSmtpPassword()));
					$outbox_folder = array_var($_POST, 'outbox_select_box');
					if (config_option("sent_mails_sync") && isset($outbox_folder)){		
						$mailAccount->setSyncPass(MailUtilities::ENCRYPT_DECRYPT($mailAccount_data['sync_pass']));						
						$mailAccount->setSyncFolder($outbox_folder);					
					}
					
					
					//in case there is a new owner of the email account
					if ($user_changed){						
						$conditions = array("conditions" => "`created_by_id` = '$old_user_id' AND `account_id` = ".$mailAccount->getId()."");
						$all_emails = MailContents::findAll($conditions);					
						foreach ($all_emails as $e){							
							$e->setCreatedById($mail_account_user->getId());
							$e->save();
						}						
					}
					
					//If imap, save folders to check
					if($mailAccount->getIsImap() && is_array(array_var($_POST, 'check'))) {
					  	$checks = array_var($_POST, 'check');
					  	if (is_array($imap_folders) && count($imap_folders)) {
						  	foreach ($imap_folders as $folder) {
						  		$folder->setCheckFolder(false);
						  		foreach ($checks as $name => $cf) {
						  			$name = str_replace(array('¡','!'), array('[',']'), $name);//to avoid a mistaken array if name contains [ 
						  			if (strcasecmp($name, $folder->getFolderName()) == 0) {
						  				$folder->setCheckFolder($cf == 'checked');
						  				break;
						  			}
						  		}
						  		$folder->save();
						  	}
					  	}
					}
					
					$member_ids = json_decode(array_var($_POST, 'members'));
					if ( count($member_ids) > 0  ){
						$member = $member_ids[0];
					}else{
						$member = 0;
					}
					$mailAccount->setMemberId($member);
					
					$mailAccount->save();
					
					// process users
					
					$account_users = Contacts::findAll();
					$user_access = array_var($_POST, 'user_access');
					foreach ($account_users as $account_user) {
						$user_id = $account_user->getId();
						$access = array_var($user_access, $user_id, 'none');
						$account_user = MailAccountContacts::getByAccountAndContact($mailAccount, $account_user);
						if ($access != 'none' || $user_id == $mail_account_user->getId()) {
							if (!$account_user instanceof MailAccountContact) {
								$account_user = new MailAccountContact();
								$account_user->setAccountId($mailAccount->getId());
								$account_user->setContactId($user_id);
							}
							$account_user->setCanEdit($access == 'write');
							$account_user->save();
						} else if ($account_user instanceof MailAccountContact) {
							$account_user->delete();
						}
					}
					/*// delete any remaining ones
					$account_users = MailAccountContacts::getByAccount($mailAccount);
					foreach ($account_users as $account_user) {
						if ($access = array_var($user_access, $account_user->getId(), 'none') == 'none') {
							$account_user->delete();
						}
					}*/
					
					evt_add("mail account edited", array(
							"id" => $mailAccount->getId(),
							"name" => $mailAccount->getName(),
							"email" => $mailAccount->getEmail()
					));
				}
				
				// personal settings
				if (array_var($_POST, 'is_default')) {
					$user_accounts = MailAccountContacts::getByContact(logged_user());
					foreach ($user_accounts as $acc) {
						if ($acc->getAccountId() != $mailAccount->getId()) {
							$acc->setIsDefault(false);
							$acc->save();				
						} else {
							$acc->setIsDefault(true);
							$acc->save();
						}
					}
				}
				$logged_user_settings = MailAccountContacts::getByAccountAndContact($mailAccount, logged_user());
				if ($logged_user_settings instanceof MailAccountContact) { 
					$logged_user_settings->setSignature(array_var($_POST, 'signature'));
					$logged_user_settings->setSenderName(array_var($_POST, 'sender_name'));
					$logged_user_settings->save();
				}
				DB::commit();

				flash_success(lang('success edit mail account', $mailAccount->getName()));
				ajx_current("back");

		  // Error...
			} catch(Exception $e) {
				DB::rollback();
				ajx_current("empty");
				flash_error($e->getMessage());
			} // try
		} // if
	} // edit