HestiaChangePasswordDriver.php 3.0 KB

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