Пример #1
0
 /**
  * @param \RainLoop\Model\Account $oAccount
  * @param string $sQuery
  * @param int $iLimit = 20
  *
  * @return array
  */
 public function Process($oAccount, $sQuery, $iLimit = 20)
 {
     $aResult = array();
     try {
         if (!$oAccount || !\RainLoop\Utils::IsOwnCloud() || !\class_exists('\\OCP\\Contacts') || !\OCP\Contacts::isEnabled() || !\class_exists('\\OCP\\User') || !\OCP\User::isLoggedIn()) {
             return $aResult;
         }
         $aSearchResult = \OCP\Contacts::search($sQuery, array('FN', 'EMAIL'));
         //$this->oLogger->WriteDump($aSearchResult);
         $aPreResult = array();
         if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) {
             foreach ($aSearchResult as $aContact) {
                 if (0 >= $iLimit) {
                     break;
                 }
                 $sUid = empty($aContact['UID']) ? '' : $aContact['UID'];
                 if (!empty($sUid)) {
                     $sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : '';
                     $mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : '';
                     if (!\is_array($mEmails)) {
                         $mEmails = array($mEmails);
                     }
                     if (!isset($aPreResult[$sUid])) {
                         $aPreResult[$sUid] = array();
                     }
                     foreach ($mEmails as $sEmail) {
                         $sEmail = \trim($sEmail);
                         if (!empty($sEmail)) {
                             $iLimit--;
                             $aPreResult[$sUid][] = array($sEmail, $sFullName);
                         }
                     }
                 }
             }
             $aPreResult = \array_values($aPreResult);
             //				$this->oLogger->WriteDump($aPreResult);
             foreach ($aPreResult as $aData) {
                 foreach ($aData as $aSubData) {
                     $aResult[] = $aSubData;
                 }
             }
         }
         unset($aSearchResult, $aPreResult);
     } catch (\Exception $oException) {
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $aResult;
 }
 /**
  * @param \RainLoop\Model\Account $oAccount
  * @param string $sPrevPassword
  * @param string $sNewPassword
  *
  * @return bool
  */
 public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
 {
     $bResult = false;
     try {
         $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email());
         $sUserDn = \strtr($this->sUserDnFormat, array('{domain}' => $sDomain, '{domain:dc}' => 'dc=' . \strtr($sDomain, array('.' => ',dc=')), '{email}' => $oAccount->Email(), '{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), '{email:domain}' => $sDomain, '{login}' => $oAccount->Login(), '{imap:login}' => $oAccount->Login(), '{imap:host}' => $oAccount->DomainIncHost(), '{imap:port}' => $oAccount->DomainIncPort()));
         $oCon = @\ldap_connect($this->sHostName);
         if ($oCon) {
             @\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3);
             if (!@\ldap_bind($oCon, $sUserDn, $sPrevPassword)) {
                 if ($this->oLogger) {
                     $sError = $oCon ? @\ldap_error($oCon) : '';
                     $iErrno = $oCon ? @\ldap_errno($oCon) : 0;
                     $this->oLogger->Write('ldap_bind error: ' . $sError . ' (' . $iErrno . ')', \MailSo\Log\Enumerations\Type::WARNING, 'LDAP');
                 }
                 return false;
             }
         }
         $sEncodedNewPassword = $sNewPassword;
         switch (\strtolower($this->sPasswordEncType)) {
             case 'sha':
                 switch (true) {
                     default:
                     case \function_exists('sha1'):
                         $sEncodedNewPassword = '******' . \base64_encode(\pack('H*', \sha1($sNewPassword)));
                         break;
                     case \function_exists('hash'):
                         $sEncodedNewPassword = '******' . \base64_encode(\hash('sha1', $sNewPassword, true));
                         break;
                     case \function_exists('mhash') && defined('MHASH_SHA1'):
                         $sEncodedNewPassword = '******' . \base64_encode(\mhash(MHASH_SHA1, $sNewPassword));
                         break;
                 }
                 break;
             case 'md5':
                 $sEncodedNewPassword = '******' . \base64_encode(\pack('H*', \md5($sNewPassword)));
                 break;
             case 'crypt':
                 $sEncodedNewPassword = '******' . \crypt($sNewPassword, $this->getSalt(2));
                 break;
         }
         $aEntry = array();
         $aEntry[$this->sPasswordField] = (string) $sEncodedNewPassword;
         if (!!@\ldap_modify($oCon, $sUserDn, $aEntry)) {
             $bResult = true;
         } else {
             if ($this->oLogger) {
                 $sError = $oCon ? @\ldap_error($oCon) : '';
                 $iErrno = $oCon ? @\ldap_errno($oCon) : 0;
                 $this->oLogger->Write('ldap_modify error: ' . $sError . ' (' . $iErrno . ')', \MailSo\Log\Enumerations\Type::WARNING, 'LDAP');
             }
         }
     } catch (\Exception $oException) {
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException, \MailSo\Log\Enumerations\Type::WARNING, 'LDAP');
         }
         $bResult = false;
     }
     return $bResult;
 }
 /**
  * @param \RainLoop\Model\Account $oHmailAccount
  * @param string $sPrevPassword
  * @param string $sNewPassword
  *
  * @return bool
  */
 public function ChangePassword(\RainLoop\Account $oHmailAccount, $sPrevPassword, $sNewPassword)
 {
     if ($this->oLogger) {
         $this->oLogger->Write('Try to change password for ' . $oHmailAccount->Email());
     }
     $bResult = false;
     try {
         $oHmailApp = new COM("hMailServer.Application");
         $oHmailApp->Connect();
         if ($oHmailApp->Authenticate($this->sLogin, $this->sPassword)) {
             $sEmail = $oHmailAccount->Email();
             $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($sEmail);
             $oHmailDomain = $oHmailApp->Domains->ItemByName($sDomain);
             if ($oHmailDomain) {
                 $oHmailAccount = $oHmailDomain->Accounts->ItemByAddress($sEmail);
                 if ($oHmailAccount) {
                     $oHmailAccount->Password = $sNewPassword;
                     $oHmailAccount->Save();
                     $bResult = true;
                 } else {
                     $this->oLogger->Write('HMAILSERVER: Unknown account (' . $sEmail . ')', \MailSo\Log\Enumerations\Type::ERROR);
                 }
             } else {
                 $this->oLogger->Write('HMAILSERVER: Unknown domain (' . $sDomain . ')', \MailSo\Log\Enumerations\Type::ERROR);
             }
         } else {
             $this->oLogger->Write('HMAILSERVER: Auth error', \MailSo\Log\Enumerations\Type::ERROR);
         }
     } catch (\Exception $oException) {
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $bResult;
 }
 /**
  * @param \RainLoop\Account $oAccount
  * @param string $sPrevPassword
  * @param string $sNewPassword
  *
  * @return bool
  */
 public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
 {
     if ($this->oLogger) {
         $this->oLogger->Write('Try to change password for ' . $oAccount->Email());
     }
     $bResult = false;
     $dsn = 'mysql:host=' . $this->mHost . ';dbname=' . $this->mDatabase . ';charset=utf8';
     $options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
     try {
         $conn = new PDO($dsn, $this->mUser, $this->mPass, $options);
         $select = $conn->prepare("SELECT {$this->mColumn} FROM {$this->mTable} WHERE id = :id LIMIT 1");
         $select->execute(array(':id' => $oAccount->Email()));
         $colCrypt = $select->fetchAll(PDO::FETCH_ASSOC);
         $sCryptPass = $colCrypt[0][$this->mColumn];
         if (0 < strlen($sCryptPass) && crypt($sPrevPassword, $sCryptPass) === $sCryptPass && 7 < mb_strlen($sNewPassword) && 20 > mb_strlen($sNewPassword) && !preg_match('/[^A-Za-z0-9]+/', $sNewPassword)) {
             $update = $conn->prepare("UPDATE {$this->mTable} SET {$this->mColumn} = :crypt WHERE id = :id");
             $update->execute(array(':id' => $oAccount->Email(), ':crypt' => crypt($sNewPassword, '$' . md5(rand()))));
             $bResult = true;
             if ($this->oLogger) {
                 $this->oLogger->Write('Success! Password changed.');
             }
         } else {
             $bResult = false;
             if ($this->oLogger) {
                 $this->oLogger->Write('Something went wrong. Either current password is incorrect, or new password does not match criteria.');
             }
         }
     } catch (\Exception $oException) {
         $bResult = false;
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $bResult;
 }
 /**
  * @param \RainLoop\Model\Account $oAccount
  * @param string $sPrevPassword
  * @param string $sNewPassword
  *
  * @return bool
  */
 public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
 {
     if ($this->oLogger) {
         $this->oLogger->Write('Postfix: Try to change password for ' . $oAccount->Email());
     }
     unset($sPrevPassword);
     $bResult = false;
     if (0 < \strlen($sNewPassword)) {
         try {
             $sDsn = 'mysql:host=' . $this->sHost . ';port=' . $this->iPort . ';dbname=' . $this->sDatabase;
             $oPdo = new \PDO($sDsn, $this->sUser, $this->sPassword);
             $oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
             $sUpdatePassword = $this->cryptPassword($sNewPassword, $oPdo);
             if (0 < \strlen($sUpdatePassword)) {
                 $oStmt = $oPdo->prepare("UPDATE {$this->sTable} SET {$this->sPasscol} = ? WHERE {$this->sUsercol} = ?");
                 $bResult = (bool) $oStmt->execute(array($sUpdatePassword, $oAccount->Email()));
             } else {
                 if ($this->oLogger) {
                     $this->oLogger->Write('Postfix: Encrypted password is empty', \MailSo\Log\Enumerations\Type::ERROR);
                 }
             }
             $oPdo = null;
         } catch (\Exception $oException) {
             if ($this->oLogger) {
                 $this->oLogger->WriteException($oException);
             }
         }
     }
     return $bResult;
 }
 /**
  * @param \RainLoop\Account $oAccount
  * @param string $sPrevPassword
  * @param string $sNewPassword
  *
  * @return bool
  */
 public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
 {
     if ($this->oLogger) {
         $this->oLogger->Write('Try to change password for ' . $oAccount->Email());
     }
     include_once __DIR__ . '/xmlapi.php';
     $bResult = false;
     if (!empty($this->sHost) && 0 < $this->iPost && 0 < \strlen($this->sUser) && 0 < \strlen($this->sPassword) && $oAccount && \class_exists('xmlapi')) {
         try {
             $oXmlApi = new \xmlapi($this->sHost);
             $oXmlApi->set_port($this->iPost);
             $oXmlApi->set_protocol($this->sSsl ? 'https' : 'http');
             $oXmlApi->set_debug(false);
             $oXmlApi->set_output('json');
             $oXmlApi->set_http_client('curl');
             $oXmlApi->password_auth($this->sUser, $this->sPassword);
             $sEmail = $oAccount->Email();
             $aArgs = array('email' => \MailSo\Base\Utils::GetAccountNameFromEmail($sEmail), 'domain' => \MailSo\Base\Utils::GetDomainFromEmail($sEmail), 'password' => $sNewPassword);
             $sResult = $oXmlApi->api2_query($this->sUser, 'Email', 'passwdpop', $aArgs);
             if ($sResult) {
                 $aResult = @\json_decode($sResult, true);
                 $bResult = isset($aResult['cpanelresult']['data'][0]['result']) && !!$aResult['cpanelresult']['data'][0]['result'];
             }
             if (!$bResult && $this->oLogger) {
                 $this->oLogger->Write('CPANEL: ' . $sResult, \MailSo\Log\Enumerations\Type::ERROR);
             }
         } catch (\Exception $oException) {
             if ($this->oLogger) {
                 $this->oLogger->WriteException($oException);
             }
         }
     }
     return $bResult;
 }
 /**
  * @param \RainLoop\Account $oAccount
  * @param string $sPrevPassword
  * @param string $sNewPassword
  *
  * @return bool
  */
 public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
 {
     if ($this->oLogger) {
         $this->oLogger->Write('ISP: Try to change password for ' . $oAccount->Email());
     }
     $bResult = false;
     if (!empty($this->sDsn) && 0 < \strlen($this->sUser) && 0 < \strlen($this->sPassword) && $oAccount) {
         try {
             $oPdo = new \PDO($this->sDsn, $this->sUser, $this->sPassword);
             $oPdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
             $oStmt = $oPdo->prepare('SELECT password, mailuser_id FROM mail_user WHERE login = ? LIMIT 1');
             if ($oStmt->execute(array($oAccount->IncLogin()))) {
                 $aFetchResult = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
                 if (\is_array($aFetchResult) && isset($aFetchResult[0]['password'], $aFetchResult[0]['mailuser_id'])) {
                     $sDbPassword = \stripslashes($aFetchResult[0]['password']);
                     $sDbSalt = '$1$' . \substr($sDbPassword, 3, 8) . '$';
                     if (\crypt(\stripslashes($sPrevPassword), $sDbSalt) === $sDbPassword) {
                         $oStmt = $oPdo->prepare('UPDATE mail_user SET password = ? WHERE mailuser_id = ?');
                         $bResult = (bool) $oStmt->execute(array($this->cryptPassword($sNewPassword), $aFetchResult[0]['mailuser_id']));
                     }
                 }
             }
         } catch (\Exception $oException) {
             if ($this->oLogger) {
                 $this->oLogger->WriteException($oException);
             }
         }
     }
     return $bResult;
 }
Пример #8
0
 /**
  * @param \Exception $oException
  * @param int $iDescType = \MailSo\Log\Enumerations\Type::NOTICE
  * @param bool $bThrowException = false
  *
  * @return void
  */
 protected function writeLogException($oException, $iDescType = \MailSo\Log\Enumerations\Type::NOTICE, $bThrowException = false)
 {
     if ($this->oLogger) {
         $this->oLogger->WriteException($oException, $iDescType, $this->getLogName());
     }
     if ($bThrowException) {
         throw $oException;
     }
 }
 /**
  * @param \RainLoop\Model\Account $oAccount
  * @param string $sQuery
  * @param int $iLimit = 20
  *
  * @return array
  */
 public function Process($oAccount, $sQuery, $iLimit = 20)
 {
     $iInputLimit = $iLimit;
     $aResult = array();
     $sQuery = \trim($sQuery);
     try {
         if ('' === $sQuery || !$oAccount || !\RainLoop\Utils::IsOwnCloudLoggedIn() || !\class_exists('OCP\\Contacts') || !\OCP\Contacts::isEnabled()) {
             return $aResult;
         }
         $aSearchResult = \OCP\Contacts::search($sQuery, array('FN', 'EMAIL'));
         //$this->oLogger->WriteDump($aSearchResult);
         $aHashes = array();
         if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) {
             foreach ($aSearchResult as $aContact) {
                 if (0 >= $iLimit) {
                     break;
                 }
                 $sUid = empty($aContact['UID']) ? '' : $aContact['UID'];
                 if (!empty($sUid)) {
                     $sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : '';
                     $mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : '';
                     if (!\is_array($mEmails)) {
                         $mEmails = array($mEmails);
                     }
                     foreach ($mEmails as $sEmail) {
                         $sHash = '"' . $sFullName . '" <' . $sEmail . '>';
                         if (!isset($aHashes[$sHash])) {
                             $aHashes[$sHash] = true;
                             $aResult[] = array($sEmail, $sFullName);
                             $iLimit--;
                         }
                     }
                 }
             }
             $aResult = \array_slice($aResult, 0, $iInputLimit);
         }
         unset($aSearchResult, $aHashes);
     } catch (\Exception $oException) {
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $aResult;
 }
Пример #10
0
 /**
  * @param \Exception $oException
  * @param int $iDescType = \MailSo\Log\Enumerations\Type::NOTICE
  * @param bool $bThrowException = false
  *
  * @return void
  */
 protected function writeLogException($oException, $iDescType = \MailSo\Log\Enumerations\Type::NOTICE, $bThrowException = false)
 {
     if ($this->oLogger) {
         if ($oException instanceof Exceptions\SocketCanNotConnectToHostException) {
             $this->oLogger->Write('Socket: [' . $oException->getSocketCode() . '] ' . $oException->getSocketMessage(), $iDescType, $this->getLogName());
         }
         $this->oLogger->WriteException($oException, $iDescType, $this->getLogName());
     }
     if ($bThrowException) {
         throw $oException;
     }
 }
 /**
  * @param \RainLoop\Account $oAccount
  * @param string $sPrevPassword
  * @param string $sNewPassword
  *
  * @return bool
  */
 public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
 {
     if ($this->oLogger) {
         $this->oLogger->Write('Try to change password for ' . $oAccount->Email());
     }
     if (empty($this->mHost) || empty($this->mDatabase) || empty($this->mColumn) || empty($this->mTable)) {
         return false;
     }
     $bResult = false;
     $sDsn = 'mysql:host=' . $this->mHost . ';dbname=' . $this->mDatabase . ';charset=utf8';
     $aOptions = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
     $sLoginPart = \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email());
     $sDomainPart = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email());
     try {
         $oConn = new PDO($sDsn, $this->mUser, $this->mPass, $aOptions);
         $oSelect = $oConn->prepare('SELECT ' . $this->mColumn . ' FROM ' . $this->mTable . ' WHERE pw_name=? AND pw_domain=? LIMIT 1');
         $oSelect->execute(array($sLoginPart, $sDomainPart));
         $aColCrypt = $oSelect->fetchAll(PDO::FETCH_ASSOC);
         $sCryptPass = isset($aColCrypt[0][$this->mColumn]) ? $aColCrypt[0][$this->mColumn] : '';
         if (0 < \strlen($sCryptPass) && \crypt($sPrevPassword, $sCryptPass) === $sCryptPass) {
             $oUpdate = $oConn->prepare('UPDATE ' . $this->mTable . ' SET ' . $this->mColumn . '=ENCRYPT(?,concat("$1$",right(md5(rand()), 8 ),"$")), pw_clear_passwd=\'\' WHERE pw_name=? AND pw_domain=?');
             $oUpdate->execute(array($sNewPassword, $sLoginPart, $sDomainPart));
             $bResult = true;
             if ($this->oLogger) {
                 $this->oLogger->Write('Success! Password changed.');
             }
         } else {
             $bResult = false;
             if ($this->oLogger) {
                 $this->oLogger->Write('Something went wrong. Either current password is incorrect, or new password does not match criteria.');
             }
         }
     } catch (\Exception $oException) {
         $bResult = false;
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $bResult;
 }
 /**
  * @param string $sDesc
  * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
  *
  * @return void
  */
 public function WriteException($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO)
 {
     if ($this->oLogger) {
         $this->oLogger->WriteException($sDesc, $iType, 'PLUGIN');
     }
 }