HestiaChangePasswordDriver.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. class HestiaChangePasswordDriver implements \RainLoop\Providers\ChangePassword\ChangePasswordInterface
  3. {
  4. /**
  5. * @var string
  6. */
  7. private $sHost = '';
  8. /**
  9. * @var string
  10. */
  11. private $iPort = 8083;
  12. /**
  13. * @var string
  14. */
  15. private $sAllowedEmails = '';
  16. /**
  17. * @var \MailSo\Log\Logger
  18. */
  19. private $oLogger = null;
  20. /**
  21. * @param string $sHost
  22. * @param int $iPort
  23. *
  24. * @return \HestiaChangePasswordDriver
  25. */
  26. public function SetConfig($sHost, $iPort)
  27. {
  28. $this->sHost = $sHost;
  29. $this->iPort = $iPort;
  30. return $this;
  31. }
  32. /**
  33. * @param string $sAllowedEmails
  34. *
  35. * @return \HestiaChangePasswordDriver
  36. */
  37. public function SetAllowedEmails($sAllowedEmails)
  38. {
  39. $this->sAllowedEmails = $sAllowedEmails;
  40. return $this;
  41. }
  42. /**
  43. * @param \MailSo\Log\Logger $oLogger
  44. *
  45. * @return \HestiaChangePasswordDriver
  46. */
  47. public function SetLogger($oLogger)
  48. {
  49. if ($oLogger instanceof \MailSo\Log\Logger)
  50. {
  51. $this->oLogger = $oLogger;
  52. }
  53. return $this;
  54. }
  55. /**
  56. * @param \RainLoop\Account $oAccount
  57. *
  58. * @return bool
  59. */
  60. public function PasswordChangePossibility($oAccount)
  61. {
  62. return $oAccount && $oAccount->Email() &&
  63. \RainLoop\Plugins\Helper::ValidateWildcardValues($oAccount->Email(), $this->sAllowedEmails);
  64. }
  65. /**
  66. * @param \RainLoop\Account $oAccount
  67. * @param string $sPrevPassword
  68. * @param string $sNewPassword
  69. *
  70. * @return bool
  71. */
  72. public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword)
  73. {
  74. if ($this->oLogger)
  75. {
  76. $this->oLogger->Write('Hestia: Try to change password for '.$oAccount->Email());
  77. }
  78. $bResult = false;
  79. if (!empty($this->sHost) && 0 < $this->iPort && $oAccount)
  80. {
  81. $sEmail = \trim(\strtolower($oAccount->Email()));
  82. $sHost = \trim($this->sHost);
  83. $sHost = \str_replace('{user:host-imap}', $oAccount->Domain()->IncHost(), $sHost);
  84. $sHost = \str_replace('{user:host-smtp}', $oAccount->Domain()->OutHost(), $sHost);
  85. $sHost = \str_replace('{user:domain}', \MailSo\Base\Utils::GetDomainFromEmail($sEmail), $sHost);
  86. $sHost = \rtrim($this->sHost, '/\\');
  87. $sHost = 'https://'.$sHost;
  88. $sUrl = $sHost.':'.$this->iPort.'/reset/mail/';
  89. $iCode = 0;
  90. $oHttp = \MailSo\Base\Http::SingletonInstance();
  91. if ($this->oLogger)
  92. {
  93. $this->oLogger->Write('Hestia[Api Request]:'.$sUrl);
  94. }
  95. $mResult = $oHttp->SendPostRequest($sUrl,
  96. array(
  97. 'email' => $sEmail,
  98. 'password' => $sPrevPassword,
  99. 'new' => $sNewPassword,
  100. ), 'MailSo Http User Agent (v1)', $iCode, $this->oLogger);
  101. if (false !== $mResult && 200 === $iCode)
  102. {
  103. $aRes = null;
  104. @\parse_str($mResult, $aRes);
  105. if (is_array($aRes) && (!isset($aRes['error']) || (int) $aRes['error'] !== 1))
  106. {
  107. $bResult = true;
  108. }
  109. else
  110. {
  111. if ($this->oLogger)
  112. {
  113. $this->oLogger->Write('Hestia[Error]: Response: '.$mResult);
  114. }
  115. }
  116. }
  117. else
  118. {
  119. if ($this->oLogger)
  120. {
  121. $this->oLogger->Write('Hestia[Error]: Empty Response: Code:'.$iCode);
  122. }
  123. }
  124. }
  125. return $bResult;
  126. }
  127. }