/** * 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'); $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 = MailAccountUsers::getByAccount($mailAccount); $mau = array(); foreach ($mailAccountUsers as $au) { $mau[$au->getUserId()] = array('name' => $au->getUser()->getDisplayName(), 'can_edit' => $au->getCanEdit()); } tpl_assign('mailAccountUsers', $mau); $is_admin = false; if (logged_user()->isAdministrator()) { $is_admin = true; } 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['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 = Users::findById($mailAccount->getUserId()); } else { $mail_account_user = Users::findById($selected_user); $old_user_id = $mailAccount->getUserId(); if ($old_user_id != $mail_account_user->getId()) { $user_changed = true; } } $mailAccount_data['user_id'] = $mail_account_user->getId(); DB::beginWork(); $logged_user_settings = MailAccountUsers::getByAccountAndUser($mailAccount, logged_user()); $logged_user_can_edit = $logged_user_settings instanceof MailAccountUser && $logged_user_settings->getCanEdit() || $mailAccount->getUserId() == 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(); } } } $mailAccount->save(); // process users $account_users = $mail_account_user->getCompany()->getUsers(); $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 = MailAccountUsers::getByAccountAndUser($mailAccount, $account_user); if ($access != 'none' || $user_id == $mail_account_user->getId()) { if (!$account_user instanceof MailAccountUser) { $account_user = new MailAccountUser(); $account_user->setAccountId($mailAccount->getId()); $account_user->setUserId($user_id); } $account_user->setCanEdit($access == 'write'); $account_user->save(); } else { if ($account_user instanceof MailAccountUser) { $account_user->delete(); } } } /*// delete any remaining ones $account_users = MailAccountUsers::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 = MailAccountUsers::getByUser(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 = MailAccountUsers::getByAccountAndUser($mailAccount, logged_user()); if ($logged_user_settings instanceof MailAccountUser) { $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 }
/** * Add email account * * @access public * @param void * @return null */ function add_account() { if (logged_user()->isGuest()) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } if (!MailAccount::canAdd(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $mailAccount = new MailAccount(); tpl_assign('mailAccount', $mailAccount); $mailAccount_data = array_var($_POST, 'mailAccount'); tpl_assign('mailAccount_data', $mailAccount_data); // get mail account users $mau = array(logged_user()->getId() => array('name' => logged_user()->getObjectName(), 'can_edit' => true)); tpl_assign('mailAccountUsers', $mau); $is_admin = logged_user()->isAdministrator(); tpl_assign('is_admin', $is_admin); if (is_array(array_var($_POST, 'mailAccount'))) { $email_address = array_var(array_var($_POST, 'mailAccount'), 'email_addr'); /*if (MailAccounts::findOne(array('conditions' => "`email` = '$email_address'")) != null) { flash_error(lang('email address already exists')); ajx_current("empty"); return; }*/ try { $selected_user = array_var($_POST, 'users_select_box'); if (!$is_admin) { $mail_account_user = logged_user(); } else { $mail_account_user = Contacts::findById($selected_user); } $mailAccount_data['sync_ssl'] = array_var($mailAccount_data, 'sync_ssl') == "checked"; $mailAccount_data['contact_id'] = $mail_account_user->getId(); 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); } $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); DB::beginWork(); $mailAccount->save(); // process users $account_users = Contacts::getAllUsers(); $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); if (!is_null($access) && $access != 'none' || $user_id == $mail_account_user->getId()) { $account_user = new MailAccountContact(); $account_user->setAccountId($mailAccount->getId()); $account_user->setContactId($user_id); $account_user->setCanEdit($access == 'write'); $account_user->save(); } } if ($mailAccount->getIsImap() && is_array(array_var($_POST, 'check'))) { $real_folders = MailUtilities::getImapFolders($mailAccount); 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(); } } $imap_folders = MailAccountImapFolders::getMailAccountImapFolders($mailAccount->getId()); $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(); } } } // 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(); } if ($mailAccount->canView(logged_user())) { evt_add("mail account added", array("id" => $mailAccount->getId(), "name" => $mailAccount->getName(), "email" => $mailAccount->getEmail())); } // Restore old emails, if account was deleted and its emails weren't DB::executeAll("\r\n\t\t\t\t\t\tUPDATE " . TABLE_PREFIX . "mail_contents mc INNER JOIN " . TABLE_PREFIX . "objects o ON mc.object_id = o.id\r\n\t\t\t\t\t\t\tSET mc.account_id=" . $mailAccount->getId() . " \r\n\t\t\t\t\t\t\tWHERE o.`created_by_id` = " . $mail_account_user->getId() . " \r\n\t\t\t\t\t\t\t\t\tAND mc.`account_email` = '" . $mailAccount->getEmail() . "' \r\n\t\t\t\t\t\t\t\t\tAND mc.`account_id` NOT IN (SELECT `id` FROM `" . TABLE_PREFIX . "mail_accounts`)"); DB::commit(); flash_success(lang('success add mail account', $mailAccount->getName())); ajx_current("back"); // Error... } catch (Exception $e) { DB::rollback(); ajx_current("empty"); flash_error($e->getMessage()); } } }