. * --------------------------------------------------------------------- */ /** * Class used to manage Auth mail config */ class AuthMail extends CommonDBTM { // From CommonDBTM public $dohistory = true; static $rightname = 'config'; static function getTypeName($nb = 0) { return _n('Mail server', 'Mail servers', $nb); } function prepareInputForUpdate($input) { if (isset($input['mail_server']) && !empty($input['mail_server'])) { $input["connect_string"] = Toolbox::constructMailServerConfig($input); } return $input; } static function canCreate() { return static::canUpdate(); } static function canPurge() { return static::canUpdate(); } function prepareInputForAdd($input) { if (isset($input['mail_server']) && !empty($input['mail_server'])) { $input["connect_string"] = Toolbox::constructMailServerConfig($input); } return $input; } function defineTabs($options = []) { $ong = []; $this->addDefaultFormTab($ong); $this->addStandardTab(__CLASS__, $ong, $options); $this->addStandardTab('Log', $ong, $options); return $ong; } function rawSearchOptions() { $tab = []; $tab[] = [ 'id' => 'common', 'name' => __('Email server') ]; $tab[] = [ 'id' => '1', 'table' => $this->getTable(), 'field' => 'name', 'name' => __('Name'), 'datatype' => 'itemlink', 'massiveaction' => false ]; $tab[] = [ 'id' => '2', 'table' => $this->getTable(), 'field' => 'id', 'name' => __('ID'), 'datatype' => 'number', 'massiveaction' => false ]; $tab[] = [ 'id' => '3', 'table' => $this->getTable(), 'field' => 'host', 'name' => __('Server'), 'datatype' => 'string' ]; $tab[] = [ 'id' => '4', 'table' => $this->getTable(), 'field' => 'connect_string', 'name' => __('Connection string'), 'massiveaction' => false, 'datatype' => 'string' ]; $tab[] = [ 'id' => '6', 'table' => $this->getTable(), 'field' => 'is_active', 'name' => __('Active'), 'datatype' => 'bool' ]; $tab[] = [ 'id' => '19', 'table' => $this->getTable(), 'field' => 'date_mod', 'name' => __('Last update'), 'datatype' => 'datetime', 'massiveaction' => false ]; $tab[] = [ 'id' => '16', 'table' => $this->getTable(), 'field' => 'comment', 'name' => __('Comments'), 'datatype' => 'text' ]; return $tab; } /** * Print the auth mail form * * @param integer $ID ID of the item * @param array $options Options * * @return void|boolean (display) Returns false if there is a rights error. */ function showForm($ID, $options = []) { if (!Config::canUpdate()) { return false; } if (empty($ID)) { $this->getEmpty(); } else { $this->getFromDB($ID); } $options['colspan'] = 1; $this->showFormHeader($options); echo "" . __('Name') . ""; echo ""; echo ""; echo ""; echo "" . __('Active') . ""; echo ""; Dropdown::showYesNo('is_active', $this->fields['is_active']); echo ""; echo ""; echo "". __('Email domain Name (users email will be login@domain)') .""; echo ""; echo ""; Toolbox::showMailServerConfig($this->fields["connect_string"]); echo "" . __('Comments') . ""; echo ""; echo ""; if ($ID>0) { echo "
"; //TRANS: %s is the datetime of update printf(__('Last update on %s'), Html::convDateTime($this->fields["date_mod"])); } echo ""; $this->showFormButtons($options); } /** * Show test mail form * * @return void */ function showFormTestMail() { $ID = $this->getField('id'); if ($this->getFromDB($ID)) { echo "
"; echo "fields['connect_string']."\">"; echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
" . __('Test connection to email server') . "
" . __('Login') . "
" . __('Password') . "
"; echo "". "
"; Html::closeForm(); } } /** * Is the Mail authentication used? * * @return boolean */ static function useAuthMail() { return (countElementsInTable('glpi_authmails', ['is_active' => 1]) > 0); } /** * Test a connexion to the IMAP/POP server * * @param string $connect_string mail server * @param string $login user login * @param string $password user password * * @return boolean Authentication succeeded? */ static function testAuth($connect_string, $login, $password) { $auth = new Auth(); return $auth->connection_imap($connect_string, Toolbox::decodeFromUtf8($login), Toolbox::decodeFromUtf8($password)); } /** * Authenticate a user by checking a specific mail server * * @param object $auth identification object * @param string $login user login * @param string $password user password * @param string $mail_method mail_method array to use * * @return object identification object */ static function mailAuth($auth, $login, $password, $mail_method) { if (isset($mail_method["connect_string"]) && !empty($mail_method["connect_string"])) { $auth->auth_succeded = $auth->connection_imap( $mail_method["connect_string"], $login, $password ); if ($auth->auth_succeded) { $auth->extauth = 1; $auth->user_present = $auth->user->getFromDBbyName(addslashes($login)); $auth->user->getFromIMAP($mail_method, Toolbox::decodeFromUtf8($login)); //Update the authentication method for the current user $auth->user->fields["authtype"] = Auth::MAIL; $auth->user->fields["auths_id"] = $mail_method["id"]; } } return $auth; } /** * Try to authenticate a user by checking all the mail server * * @param object $auth identification object * @param string $login user login * @param string $password user password * @param integer $auths_id auths_id already used for the user (default 0) * @param boolean $break if user is not found in the first directory, * stop searching or try the following ones (true by default) * * @return object identification object */ static function tryMailAuth($auth, $login, $password, $auths_id = 0, $break = true) { if ($auths_id <= 0) { foreach ($auth->authtypes["mail"] as $mail_method) { if (!$auth->auth_succeded && $mail_method['is_active']) { $auth = self::mailAuth($auth, $login, $password, $mail_method); } else { if ($break) { break; } } } } else if (array_key_exists($auths_id, $auth->authtypes["mail"])) { //Check if the mail server indicated as the last good one still exists ! $auth = self::mailAuth($auth, $login, $password, $auth->authtypes["mail"][$auths_id]); } return $auth; } function cleanDBonPurge() { Rule::cleanForItemCriteria($this, 'MAIL_SERVER'); } function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { if (!$withtemplate && $item->can($item->getField('id'), READ)) { $ong = []; $ong[1] = _sx('button', 'Test'); // test connexion return $ong; } return ''; } static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { switch ($tabnum) { case 1 : $item->showFormTestMail(); break; } return true; } }