@@ -55,6 +55,7 @@ discard block |
||
| 55 | 55 | * Adds a header to the email |
| 56 | 56 | * |
| 57 | 57 | * @since 12.0.0 |
| 58 | + * @return void |
|
| 58 | 59 | */ |
| 59 | 60 | public function addHeader(); |
| 60 | 61 | |
@@ -62,10 +63,11 @@ discard block |
||
| 62 | 63 | * Adds a heading to the email |
| 63 | 64 | * |
| 64 | 65 | * @param string $title |
| 65 | - * @param string $plainTitle|bool Title that is used in the plain text email |
|
| 66 | + * @param string $plainTitle Title that is used in the plain text email |
|
| 66 | 67 | * if empty the $title is used, if false none will be used |
| 67 | 68 | * |
| 68 | 69 | * @since 12.0.0 |
| 70 | + * @return void |
|
| 69 | 71 | */ |
| 70 | 72 | public function addHeading($title, $plainTitle = ''); |
| 71 | 73 | |
@@ -77,6 +79,7 @@ discard block |
||
| 77 | 79 | * if empty the $text is used, if false none will be used |
| 78 | 80 | * |
| 79 | 81 | * @since 12.0.0 |
| 82 | + * @return void |
|
| 80 | 83 | */ |
| 81 | 84 | public function addBodyText($text, $plainText = ''); |
| 82 | 85 | |
@@ -91,6 +94,7 @@ discard block |
||
| 91 | 94 | * @param string $plainTextRight Text of right button that is used in the plain text version - if empty the $textRight is used |
| 92 | 95 | * |
| 93 | 96 | * @since 12.0.0 |
| 97 | + * @return void |
|
| 94 | 98 | */ |
| 95 | 99 | public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight, $plainTextLeft = '', $plainTextRight = ''); |
| 96 | 100 | |
@@ -103,6 +107,7 @@ discard block |
||
| 103 | 107 | * if empty the $text is used, if false none will be used |
| 104 | 108 | * |
| 105 | 109 | * @since 12.0.0 |
| 110 | + * @return void |
|
| 106 | 111 | */ |
| 107 | 112 | public function addBodyButton($text, $url, $plainText = ''); |
| 108 | 113 | |
@@ -112,6 +117,7 @@ discard block |
||
| 112 | 117 | * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically generated email" will be used |
| 113 | 118 | * |
| 114 | 119 | * @since 12.0.0 |
| 120 | + * @return void |
|
| 115 | 121 | */ |
| 116 | 122 | public function addFooter($text = ''); |
| 117 | 123 | |
@@ -51,85 +51,85 @@ |
||
| 51 | 51 | * @since 12.0.0 |
| 52 | 52 | */ |
| 53 | 53 | interface IEMailTemplate { |
| 54 | - /** |
|
| 55 | - * Adds a header to the email |
|
| 56 | - * |
|
| 57 | - * @since 12.0.0 |
|
| 58 | - */ |
|
| 59 | - public function addHeader(); |
|
| 54 | + /** |
|
| 55 | + * Adds a header to the email |
|
| 56 | + * |
|
| 57 | + * @since 12.0.0 |
|
| 58 | + */ |
|
| 59 | + public function addHeader(); |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Adds a heading to the email |
|
| 63 | - * |
|
| 64 | - * @param string $title |
|
| 65 | - * @param string $plainTitle|bool Title that is used in the plain text email |
|
| 66 | - * if empty the $title is used, if false none will be used |
|
| 67 | - * |
|
| 68 | - * @since 12.0.0 |
|
| 69 | - */ |
|
| 70 | - public function addHeading($title, $plainTitle = ''); |
|
| 61 | + /** |
|
| 62 | + * Adds a heading to the email |
|
| 63 | + * |
|
| 64 | + * @param string $title |
|
| 65 | + * @param string $plainTitle|bool Title that is used in the plain text email |
|
| 66 | + * if empty the $title is used, if false none will be used |
|
| 67 | + * |
|
| 68 | + * @since 12.0.0 |
|
| 69 | + */ |
|
| 70 | + public function addHeading($title, $plainTitle = ''); |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Adds a paragraph to the body of the email |
|
| 74 | - * |
|
| 75 | - * @param string $text |
|
| 76 | - * @param string|bool $plainText Text that is used in the plain text email |
|
| 77 | - * if empty the $text is used, if false none will be used |
|
| 78 | - * |
|
| 79 | - * @since 12.0.0 |
|
| 80 | - */ |
|
| 81 | - public function addBodyText($text, $plainText = ''); |
|
| 72 | + /** |
|
| 73 | + * Adds a paragraph to the body of the email |
|
| 74 | + * |
|
| 75 | + * @param string $text |
|
| 76 | + * @param string|bool $plainText Text that is used in the plain text email |
|
| 77 | + * if empty the $text is used, if false none will be used |
|
| 78 | + * |
|
| 79 | + * @since 12.0.0 |
|
| 80 | + */ |
|
| 81 | + public function addBodyText($text, $plainText = ''); |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * Adds a button group of two buttons to the body of the email |
|
| 85 | - * |
|
| 86 | - * @param string $textLeft Text of left button |
|
| 87 | - * @param string $urlLeft URL of left button |
|
| 88 | - * @param string $textRight Text of right button |
|
| 89 | - * @param string $urlRight URL of right button |
|
| 90 | - * @param string $plainTextLeft Text of left button that is used in the plain text version - if empty the $textLeft is used |
|
| 91 | - * @param string $plainTextRight Text of right button that is used in the plain text version - if empty the $textRight is used |
|
| 92 | - * |
|
| 93 | - * @since 12.0.0 |
|
| 94 | - */ |
|
| 95 | - public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight, $plainTextLeft = '', $plainTextRight = ''); |
|
| 83 | + /** |
|
| 84 | + * Adds a button group of two buttons to the body of the email |
|
| 85 | + * |
|
| 86 | + * @param string $textLeft Text of left button |
|
| 87 | + * @param string $urlLeft URL of left button |
|
| 88 | + * @param string $textRight Text of right button |
|
| 89 | + * @param string $urlRight URL of right button |
|
| 90 | + * @param string $plainTextLeft Text of left button that is used in the plain text version - if empty the $textLeft is used |
|
| 91 | + * @param string $plainTextRight Text of right button that is used in the plain text version - if empty the $textRight is used |
|
| 92 | + * |
|
| 93 | + * @since 12.0.0 |
|
| 94 | + */ |
|
| 95 | + public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight, $plainTextLeft = '', $plainTextRight = ''); |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Adds a button to the body of the email |
|
| 99 | - * |
|
| 100 | - * @param string $text Text of button |
|
| 101 | - * @param string $url URL of button |
|
| 102 | - * @param string $plainText Text of button in plain text version |
|
| 103 | - * if empty the $text is used, if false none will be used |
|
| 104 | - * |
|
| 105 | - * @since 12.0.0 |
|
| 106 | - */ |
|
| 107 | - public function addBodyButton($text, $url, $plainText = ''); |
|
| 97 | + /** |
|
| 98 | + * Adds a button to the body of the email |
|
| 99 | + * |
|
| 100 | + * @param string $text Text of button |
|
| 101 | + * @param string $url URL of button |
|
| 102 | + * @param string $plainText Text of button in plain text version |
|
| 103 | + * if empty the $text is used, if false none will be used |
|
| 104 | + * |
|
| 105 | + * @since 12.0.0 |
|
| 106 | + */ |
|
| 107 | + public function addBodyButton($text, $url, $plainText = ''); |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email |
|
| 111 | - * |
|
| 112 | - * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically generated email" will be used |
|
| 113 | - * |
|
| 114 | - * @since 12.0.0 |
|
| 115 | - */ |
|
| 116 | - public function addFooter($text = ''); |
|
| 109 | + /** |
|
| 110 | + * Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email |
|
| 111 | + * |
|
| 112 | + * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically generated email" will be used |
|
| 113 | + * |
|
| 114 | + * @since 12.0.0 |
|
| 115 | + */ |
|
| 116 | + public function addFooter($text = ''); |
|
| 117 | 117 | |
| 118 | - /** |
|
| 119 | - * Returns the rendered HTML email as string |
|
| 120 | - * |
|
| 121 | - * @return string |
|
| 122 | - * |
|
| 123 | - * @since 12.0.0 |
|
| 124 | - */ |
|
| 125 | - public function renderHTML(); |
|
| 118 | + /** |
|
| 119 | + * Returns the rendered HTML email as string |
|
| 120 | + * |
|
| 121 | + * @return string |
|
| 122 | + * |
|
| 123 | + * @since 12.0.0 |
|
| 124 | + */ |
|
| 125 | + public function renderHTML(); |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Returns the rendered plain text email as string |
|
| 129 | - * |
|
| 130 | - * @return string |
|
| 131 | - * |
|
| 132 | - * @since 12.0.0 |
|
| 133 | - */ |
|
| 134 | - public function renderText(); |
|
| 127 | + /** |
|
| 128 | + * Returns the rendered plain text email as string |
|
| 129 | + * |
|
| 130 | + * @return string |
|
| 131 | + * |
|
| 132 | + * @since 12.0.0 |
|
| 133 | + */ |
|
| 134 | + public function renderText(); |
|
| 135 | 135 | } |
@@ -55,282 +55,282 @@ |
||
| 55 | 55 | */ |
| 56 | 56 | class LostController extends Controller { |
| 57 | 57 | |
| 58 | - /** @var IURLGenerator */ |
|
| 59 | - protected $urlGenerator; |
|
| 60 | - /** @var IUserManager */ |
|
| 61 | - protected $userManager; |
|
| 62 | - /** @var Defaults */ |
|
| 63 | - protected $defaults; |
|
| 64 | - /** @var IL10N */ |
|
| 65 | - protected $l10n; |
|
| 66 | - /** @var string */ |
|
| 67 | - protected $from; |
|
| 68 | - /** @var IManager */ |
|
| 69 | - protected $encryptionManager; |
|
| 70 | - /** @var IConfig */ |
|
| 71 | - protected $config; |
|
| 72 | - /** @var ISecureRandom */ |
|
| 73 | - protected $secureRandom; |
|
| 74 | - /** @var IMailer */ |
|
| 75 | - protected $mailer; |
|
| 76 | - /** @var ITimeFactory */ |
|
| 77 | - protected $timeFactory; |
|
| 78 | - /** @var ICrypto */ |
|
| 79 | - protected $crypto; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param string $appName |
|
| 83 | - * @param IRequest $request |
|
| 84 | - * @param IURLGenerator $urlGenerator |
|
| 85 | - * @param IUserManager $userManager |
|
| 86 | - * @param Defaults $defaults |
|
| 87 | - * @param IL10N $l10n |
|
| 88 | - * @param IConfig $config |
|
| 89 | - * @param ISecureRandom $secureRandom |
|
| 90 | - * @param string $defaultMailAddress |
|
| 91 | - * @param IManager $encryptionManager |
|
| 92 | - * @param IMailer $mailer |
|
| 93 | - * @param ITimeFactory $timeFactory |
|
| 94 | - * @param ICrypto $crypto |
|
| 95 | - */ |
|
| 96 | - public function __construct($appName, |
|
| 97 | - IRequest $request, |
|
| 98 | - IURLGenerator $urlGenerator, |
|
| 99 | - IUserManager $userManager, |
|
| 100 | - Defaults $defaults, |
|
| 101 | - IL10N $l10n, |
|
| 102 | - IConfig $config, |
|
| 103 | - ISecureRandom $secureRandom, |
|
| 104 | - $defaultMailAddress, |
|
| 105 | - IManager $encryptionManager, |
|
| 106 | - IMailer $mailer, |
|
| 107 | - ITimeFactory $timeFactory, |
|
| 108 | - ICrypto $crypto) { |
|
| 109 | - parent::__construct($appName, $request); |
|
| 110 | - $this->urlGenerator = $urlGenerator; |
|
| 111 | - $this->userManager = $userManager; |
|
| 112 | - $this->defaults = $defaults; |
|
| 113 | - $this->l10n = $l10n; |
|
| 114 | - $this->secureRandom = $secureRandom; |
|
| 115 | - $this->from = $defaultMailAddress; |
|
| 116 | - $this->encryptionManager = $encryptionManager; |
|
| 117 | - $this->config = $config; |
|
| 118 | - $this->mailer = $mailer; |
|
| 119 | - $this->timeFactory = $timeFactory; |
|
| 120 | - $this->crypto = $crypto; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Someone wants to reset their password: |
|
| 125 | - * |
|
| 126 | - * @PublicPage |
|
| 127 | - * @NoCSRFRequired |
|
| 128 | - * |
|
| 129 | - * @param string $token |
|
| 130 | - * @param string $userId |
|
| 131 | - * @return TemplateResponse |
|
| 132 | - */ |
|
| 133 | - public function resetform($token, $userId) { |
|
| 134 | - try { |
|
| 135 | - $this->checkPasswordResetToken($token, $userId); |
|
| 136 | - } catch (\Exception $e) { |
|
| 137 | - return new TemplateResponse( |
|
| 138 | - 'core', 'error', [ |
|
| 139 | - "errors" => array(array("error" => $e->getMessage())) |
|
| 140 | - ], |
|
| 141 | - 'guest' |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - return new TemplateResponse( |
|
| 146 | - 'core', |
|
| 147 | - 'lostpassword/resetpassword', |
|
| 148 | - array( |
|
| 149 | - 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)), |
|
| 150 | - ), |
|
| 151 | - 'guest' |
|
| 152 | - ); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @param string $token |
|
| 157 | - * @param string $userId |
|
| 158 | - * @throws \Exception |
|
| 159 | - */ |
|
| 160 | - protected function checkPasswordResetToken($token, $userId) { |
|
| 161 | - $user = $this->userManager->get($userId); |
|
| 162 | - if($user === null) { |
|
| 163 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - try { |
|
| 167 | - $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null); |
|
| 168 | - $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 169 | - $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret')); |
|
| 170 | - } catch (\Exception $e) { |
|
| 171 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $splittedToken = explode(':', $decryptedToken); |
|
| 175 | - if(count($splittedToken) !== 2) { |
|
| 176 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*12) || |
|
| 180 | - $user->getLastLogin() > $splittedToken[0]) { |
|
| 181 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - if (!hash_equals($splittedToken[1], $token)) { |
|
| 185 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @param $message |
|
| 191 | - * @param array $additional |
|
| 192 | - * @return array |
|
| 193 | - */ |
|
| 194 | - private function error($message, array $additional=array()) { |
|
| 195 | - return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * @return array |
|
| 200 | - */ |
|
| 201 | - private function success() { |
|
| 202 | - return array('status'=>'success'); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @PublicPage |
|
| 207 | - * @BruteForceProtection(action=passwordResetEmail) |
|
| 208 | - * |
|
| 209 | - * @param string $user |
|
| 210 | - * @return array |
|
| 211 | - */ |
|
| 212 | - public function email($user){ |
|
| 213 | - // FIXME: use HTTP error codes |
|
| 214 | - try { |
|
| 215 | - $this->sendEmail($user); |
|
| 216 | - } catch (\Exception $e){ |
|
| 217 | - return $this->error($e->getMessage()); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - return $this->success(); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * @PublicPage |
|
| 225 | - * @param string $token |
|
| 226 | - * @param string $userId |
|
| 227 | - * @param string $password |
|
| 228 | - * @param boolean $proceed |
|
| 229 | - * @return array |
|
| 230 | - */ |
|
| 231 | - public function setPassword($token, $userId, $password, $proceed) { |
|
| 232 | - if ($this->encryptionManager->isEnabled() && !$proceed) { |
|
| 233 | - return $this->error('', array('encryption' => true)); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - try { |
|
| 237 | - $this->checkPasswordResetToken($token, $userId); |
|
| 238 | - $user = $this->userManager->get($userId); |
|
| 239 | - |
|
| 240 | - \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'pre_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 241 | - |
|
| 242 | - if (!$user->setPassword($password)) { |
|
| 243 | - throw new \Exception(); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 247 | - |
|
| 248 | - $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
|
| 249 | - @\OC_User::unsetMagicInCookie(); |
|
| 250 | - } catch (\Exception $e){ |
|
| 251 | - return $this->error($e->getMessage()); |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $this->success(); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @param string $input |
|
| 259 | - * @throws \Exception |
|
| 260 | - */ |
|
| 261 | - protected function sendEmail($input) { |
|
| 262 | - $user = $this->findUserByIdOrMail($input); |
|
| 263 | - $email = $user->getEMailAddress(); |
|
| 264 | - |
|
| 265 | - if (empty($email)) { |
|
| 266 | - throw new \Exception( |
|
| 267 | - $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.') |
|
| 268 | - ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // Generate the token. It is stored encrypted in the database with the |
|
| 272 | - // secret being the users' email address appended with the system secret. |
|
| 273 | - // This makes the token automatically invalidate once the user changes |
|
| 274 | - // their email address. |
|
| 275 | - $token = $this->secureRandom->generate( |
|
| 276 | - 21, |
|
| 277 | - ISecureRandom::CHAR_DIGITS. |
|
| 278 | - ISecureRandom::CHAR_LOWER. |
|
| 279 | - ISecureRandom::CHAR_UPPER |
|
| 280 | - ); |
|
| 281 | - $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 282 | - $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 283 | - $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 284 | - |
|
| 285 | - $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |
|
| 286 | - |
|
| 287 | - $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 288 | - |
|
| 289 | - $emailTemplate->addHeader(); |
|
| 290 | - $emailTemplate->addHeading($this->l10n->t('Password reset')); |
|
| 291 | - |
|
| 292 | - $emailTemplate->addBodyText( |
|
| 293 | - $this->l10n->t('Click the following button to reset your password. If you have not requested the password reset, then ignore this email.'), |
|
| 294 | - $this->l10n->t('Click the following link to reset your password. If you have not requested the password reset, then ignore this email.') |
|
| 295 | - ); |
|
| 296 | - |
|
| 297 | - $emailTemplate->addBodyButton( |
|
| 298 | - $this->l10n->t('Reset your password'), |
|
| 299 | - $link, |
|
| 300 | - false |
|
| 301 | - ); |
|
| 302 | - $emailTemplate->addFooter(); |
|
| 303 | - |
|
| 304 | - try { |
|
| 305 | - $message = $this->mailer->createMessage(); |
|
| 306 | - $message->setTo([$email => $user->getUID()]); |
|
| 307 | - $message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); |
|
| 308 | - $message->setPlainBody($emailTemplate->renderText()); |
|
| 309 | - $message->setHtmlBody($emailTemplate->renderHTML()); |
|
| 310 | - $message->setFrom([$this->from => $this->defaults->getName()]); |
|
| 311 | - $this->mailer->send($message); |
|
| 312 | - } catch (\Exception $e) { |
|
| 313 | - throw new \Exception($this->l10n->t( |
|
| 314 | - 'Couldn\'t send reset email. Please contact your administrator.' |
|
| 315 | - )); |
|
| 316 | - } |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - /** |
|
| 320 | - * @param string $input |
|
| 321 | - * @return IUser |
|
| 322 | - * @throws \Exception |
|
| 323 | - */ |
|
| 324 | - protected function findUserByIdOrMail($input) { |
|
| 325 | - $user = $this->userManager->get($input); |
|
| 326 | - if ($user instanceof IUser) { |
|
| 327 | - return $user; |
|
| 328 | - } |
|
| 329 | - $users = $this->userManager->getByEmail($input); |
|
| 330 | - if (count($users) === 1) { |
|
| 331 | - return $users[0]; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - throw new \InvalidArgumentException($this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.')); |
|
| 335 | - } |
|
| 58 | + /** @var IURLGenerator */ |
|
| 59 | + protected $urlGenerator; |
|
| 60 | + /** @var IUserManager */ |
|
| 61 | + protected $userManager; |
|
| 62 | + /** @var Defaults */ |
|
| 63 | + protected $defaults; |
|
| 64 | + /** @var IL10N */ |
|
| 65 | + protected $l10n; |
|
| 66 | + /** @var string */ |
|
| 67 | + protected $from; |
|
| 68 | + /** @var IManager */ |
|
| 69 | + protected $encryptionManager; |
|
| 70 | + /** @var IConfig */ |
|
| 71 | + protected $config; |
|
| 72 | + /** @var ISecureRandom */ |
|
| 73 | + protected $secureRandom; |
|
| 74 | + /** @var IMailer */ |
|
| 75 | + protected $mailer; |
|
| 76 | + /** @var ITimeFactory */ |
|
| 77 | + protected $timeFactory; |
|
| 78 | + /** @var ICrypto */ |
|
| 79 | + protected $crypto; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param string $appName |
|
| 83 | + * @param IRequest $request |
|
| 84 | + * @param IURLGenerator $urlGenerator |
|
| 85 | + * @param IUserManager $userManager |
|
| 86 | + * @param Defaults $defaults |
|
| 87 | + * @param IL10N $l10n |
|
| 88 | + * @param IConfig $config |
|
| 89 | + * @param ISecureRandom $secureRandom |
|
| 90 | + * @param string $defaultMailAddress |
|
| 91 | + * @param IManager $encryptionManager |
|
| 92 | + * @param IMailer $mailer |
|
| 93 | + * @param ITimeFactory $timeFactory |
|
| 94 | + * @param ICrypto $crypto |
|
| 95 | + */ |
|
| 96 | + public function __construct($appName, |
|
| 97 | + IRequest $request, |
|
| 98 | + IURLGenerator $urlGenerator, |
|
| 99 | + IUserManager $userManager, |
|
| 100 | + Defaults $defaults, |
|
| 101 | + IL10N $l10n, |
|
| 102 | + IConfig $config, |
|
| 103 | + ISecureRandom $secureRandom, |
|
| 104 | + $defaultMailAddress, |
|
| 105 | + IManager $encryptionManager, |
|
| 106 | + IMailer $mailer, |
|
| 107 | + ITimeFactory $timeFactory, |
|
| 108 | + ICrypto $crypto) { |
|
| 109 | + parent::__construct($appName, $request); |
|
| 110 | + $this->urlGenerator = $urlGenerator; |
|
| 111 | + $this->userManager = $userManager; |
|
| 112 | + $this->defaults = $defaults; |
|
| 113 | + $this->l10n = $l10n; |
|
| 114 | + $this->secureRandom = $secureRandom; |
|
| 115 | + $this->from = $defaultMailAddress; |
|
| 116 | + $this->encryptionManager = $encryptionManager; |
|
| 117 | + $this->config = $config; |
|
| 118 | + $this->mailer = $mailer; |
|
| 119 | + $this->timeFactory = $timeFactory; |
|
| 120 | + $this->crypto = $crypto; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Someone wants to reset their password: |
|
| 125 | + * |
|
| 126 | + * @PublicPage |
|
| 127 | + * @NoCSRFRequired |
|
| 128 | + * |
|
| 129 | + * @param string $token |
|
| 130 | + * @param string $userId |
|
| 131 | + * @return TemplateResponse |
|
| 132 | + */ |
|
| 133 | + public function resetform($token, $userId) { |
|
| 134 | + try { |
|
| 135 | + $this->checkPasswordResetToken($token, $userId); |
|
| 136 | + } catch (\Exception $e) { |
|
| 137 | + return new TemplateResponse( |
|
| 138 | + 'core', 'error', [ |
|
| 139 | + "errors" => array(array("error" => $e->getMessage())) |
|
| 140 | + ], |
|
| 141 | + 'guest' |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + return new TemplateResponse( |
|
| 146 | + 'core', |
|
| 147 | + 'lostpassword/resetpassword', |
|
| 148 | + array( |
|
| 149 | + 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)), |
|
| 150 | + ), |
|
| 151 | + 'guest' |
|
| 152 | + ); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @param string $token |
|
| 157 | + * @param string $userId |
|
| 158 | + * @throws \Exception |
|
| 159 | + */ |
|
| 160 | + protected function checkPasswordResetToken($token, $userId) { |
|
| 161 | + $user = $this->userManager->get($userId); |
|
| 162 | + if($user === null) { |
|
| 163 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + try { |
|
| 167 | + $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null); |
|
| 168 | + $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 169 | + $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret')); |
|
| 170 | + } catch (\Exception $e) { |
|
| 171 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $splittedToken = explode(':', $decryptedToken); |
|
| 175 | + if(count($splittedToken) !== 2) { |
|
| 176 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*12) || |
|
| 180 | + $user->getLastLogin() > $splittedToken[0]) { |
|
| 181 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + if (!hash_equals($splittedToken[1], $token)) { |
|
| 185 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @param $message |
|
| 191 | + * @param array $additional |
|
| 192 | + * @return array |
|
| 193 | + */ |
|
| 194 | + private function error($message, array $additional=array()) { |
|
| 195 | + return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * @return array |
|
| 200 | + */ |
|
| 201 | + private function success() { |
|
| 202 | + return array('status'=>'success'); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @PublicPage |
|
| 207 | + * @BruteForceProtection(action=passwordResetEmail) |
|
| 208 | + * |
|
| 209 | + * @param string $user |
|
| 210 | + * @return array |
|
| 211 | + */ |
|
| 212 | + public function email($user){ |
|
| 213 | + // FIXME: use HTTP error codes |
|
| 214 | + try { |
|
| 215 | + $this->sendEmail($user); |
|
| 216 | + } catch (\Exception $e){ |
|
| 217 | + return $this->error($e->getMessage()); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + return $this->success(); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * @PublicPage |
|
| 225 | + * @param string $token |
|
| 226 | + * @param string $userId |
|
| 227 | + * @param string $password |
|
| 228 | + * @param boolean $proceed |
|
| 229 | + * @return array |
|
| 230 | + */ |
|
| 231 | + public function setPassword($token, $userId, $password, $proceed) { |
|
| 232 | + if ($this->encryptionManager->isEnabled() && !$proceed) { |
|
| 233 | + return $this->error('', array('encryption' => true)); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + try { |
|
| 237 | + $this->checkPasswordResetToken($token, $userId); |
|
| 238 | + $user = $this->userManager->get($userId); |
|
| 239 | + |
|
| 240 | + \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'pre_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 241 | + |
|
| 242 | + if (!$user->setPassword($password)) { |
|
| 243 | + throw new \Exception(); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 247 | + |
|
| 248 | + $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
|
| 249 | + @\OC_User::unsetMagicInCookie(); |
|
| 250 | + } catch (\Exception $e){ |
|
| 251 | + return $this->error($e->getMessage()); |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $this->success(); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @param string $input |
|
| 259 | + * @throws \Exception |
|
| 260 | + */ |
|
| 261 | + protected function sendEmail($input) { |
|
| 262 | + $user = $this->findUserByIdOrMail($input); |
|
| 263 | + $email = $user->getEMailAddress(); |
|
| 264 | + |
|
| 265 | + if (empty($email)) { |
|
| 266 | + throw new \Exception( |
|
| 267 | + $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.') |
|
| 268 | + ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // Generate the token. It is stored encrypted in the database with the |
|
| 272 | + // secret being the users' email address appended with the system secret. |
|
| 273 | + // This makes the token automatically invalidate once the user changes |
|
| 274 | + // their email address. |
|
| 275 | + $token = $this->secureRandom->generate( |
|
| 276 | + 21, |
|
| 277 | + ISecureRandom::CHAR_DIGITS. |
|
| 278 | + ISecureRandom::CHAR_LOWER. |
|
| 279 | + ISecureRandom::CHAR_UPPER |
|
| 280 | + ); |
|
| 281 | + $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 282 | + $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 283 | + $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 284 | + |
|
| 285 | + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |
|
| 286 | + |
|
| 287 | + $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 288 | + |
|
| 289 | + $emailTemplate->addHeader(); |
|
| 290 | + $emailTemplate->addHeading($this->l10n->t('Password reset')); |
|
| 291 | + |
|
| 292 | + $emailTemplate->addBodyText( |
|
| 293 | + $this->l10n->t('Click the following button to reset your password. If you have not requested the password reset, then ignore this email.'), |
|
| 294 | + $this->l10n->t('Click the following link to reset your password. If you have not requested the password reset, then ignore this email.') |
|
| 295 | + ); |
|
| 296 | + |
|
| 297 | + $emailTemplate->addBodyButton( |
|
| 298 | + $this->l10n->t('Reset your password'), |
|
| 299 | + $link, |
|
| 300 | + false |
|
| 301 | + ); |
|
| 302 | + $emailTemplate->addFooter(); |
|
| 303 | + |
|
| 304 | + try { |
|
| 305 | + $message = $this->mailer->createMessage(); |
|
| 306 | + $message->setTo([$email => $user->getUID()]); |
|
| 307 | + $message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); |
|
| 308 | + $message->setPlainBody($emailTemplate->renderText()); |
|
| 309 | + $message->setHtmlBody($emailTemplate->renderHTML()); |
|
| 310 | + $message->setFrom([$this->from => $this->defaults->getName()]); |
|
| 311 | + $this->mailer->send($message); |
|
| 312 | + } catch (\Exception $e) { |
|
| 313 | + throw new \Exception($this->l10n->t( |
|
| 314 | + 'Couldn\'t send reset email. Please contact your administrator.' |
|
| 315 | + )); |
|
| 316 | + } |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + /** |
|
| 320 | + * @param string $input |
|
| 321 | + * @return IUser |
|
| 322 | + * @throws \Exception |
|
| 323 | + */ |
|
| 324 | + protected function findUserByIdOrMail($input) { |
|
| 325 | + $user = $this->userManager->get($input); |
|
| 326 | + if ($user instanceof IUser) { |
|
| 327 | + return $user; |
|
| 328 | + } |
|
| 329 | + $users = $this->userManager->getByEmail($input); |
|
| 330 | + if (count($users) === 1) { |
|
| 331 | + return $users[0]; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + throw new \InvalidArgumentException($this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.')); |
|
| 335 | + } |
|
| 336 | 336 | } |
@@ -39,25 +39,25 @@ discard block |
||
| 39 | 39 | * @package OC\Mail |
| 40 | 40 | */ |
| 41 | 41 | class EMailTemplate implements IEMailTemplate { |
| 42 | - /** @var Defaults */ |
|
| 43 | - protected $themingDefaults; |
|
| 44 | - /** @var IURLGenerator */ |
|
| 45 | - protected $urlGenerator; |
|
| 46 | - /** @var IL10N */ |
|
| 47 | - protected $l10n; |
|
| 48 | - |
|
| 49 | - /** @var string */ |
|
| 50 | - protected $htmlBody = ''; |
|
| 51 | - /** @var string */ |
|
| 52 | - protected $plainBody = ''; |
|
| 53 | - /** @var bool indicated if the footer is added */ |
|
| 54 | - protected $headerAdded = false; |
|
| 55 | - /** @var bool indicated if the body is already opened */ |
|
| 56 | - protected $bodyOpened = false; |
|
| 57 | - /** @var bool indicated if the footer is added */ |
|
| 58 | - protected $footerAdded = false; |
|
| 59 | - |
|
| 60 | - protected $head = <<<EOF |
|
| 42 | + /** @var Defaults */ |
|
| 43 | + protected $themingDefaults; |
|
| 44 | + /** @var IURLGenerator */ |
|
| 45 | + protected $urlGenerator; |
|
| 46 | + /** @var IL10N */ |
|
| 47 | + protected $l10n; |
|
| 48 | + |
|
| 49 | + /** @var string */ |
|
| 50 | + protected $htmlBody = ''; |
|
| 51 | + /** @var string */ |
|
| 52 | + protected $plainBody = ''; |
|
| 53 | + /** @var bool indicated if the footer is added */ |
|
| 54 | + protected $headerAdded = false; |
|
| 55 | + /** @var bool indicated if the body is already opened */ |
|
| 56 | + protected $bodyOpened = false; |
|
| 57 | + /** @var bool indicated if the footer is added */ |
|
| 58 | + protected $footerAdded = false; |
|
| 59 | + |
|
| 60 | + protected $head = <<<EOF |
|
| 61 | 61 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 62 | 62 | <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" style="-webkit-font-smoothing:antialiased;background:#f3f3f3!important"> |
| 63 | 63 | <head> |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | <center data-parsed="" style="min-width:580px;width:100%"> |
| 76 | 76 | EOF; |
| 77 | 77 | |
| 78 | - protected $tail = <<<EOF |
|
| 78 | + protected $tail = <<<EOF |
|
| 79 | 79 | </center> |
| 80 | 80 | </td> |
| 81 | 81 | </tr> |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | </html> |
| 87 | 87 | EOF; |
| 88 | 88 | |
| 89 | - protected $header = <<<EOF |
|
| 89 | + protected $header = <<<EOF |
|
| 90 | 90 | <table align="center" class="wrapper header float-center" style="Margin:0 auto;background:#8a8a8a;background-color:%s;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%%"> |
| 91 | 91 | <tr style="padding:0;text-align:left;vertical-align:top"> |
| 92 | 92 | <td class="wrapper-inner" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:Lucida Grande,Geneva,Verdana,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:20px;text-align:left;vertical-align:top;word-wrap:break-word"> |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | </table> |
| 120 | 120 | EOF; |
| 121 | 121 | |
| 122 | - protected $heading = <<<EOF |
|
| 122 | + protected $heading = <<<EOF |
|
| 123 | 123 | <table align="center" class="container main-heading float-center" style="Margin:0 auto;background:0 0!important;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:580px"> |
| 124 | 124 | <tbody> |
| 125 | 125 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | </table> |
| 139 | 139 | EOF; |
| 140 | 140 | |
| 141 | - protected $bodyBegin = <<<EOF |
|
| 141 | + protected $bodyBegin = <<<EOF |
|
| 142 | 142 | <table align="center" class="wrapper content float-center" style="Margin:0 auto;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%"> |
| 143 | 143 | <tr style="padding:0;text-align:left;vertical-align:top"> |
| 144 | 144 | <td class="wrapper-inner" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:Lucida Grande,Geneva,Verdana,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:0;text-align:left;vertical-align:top;word-wrap:break-word"> |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | </table> |
| 156 | 156 | EOF; |
| 157 | 157 | |
| 158 | - protected $bodyText = <<<EOF |
|
| 158 | + protected $bodyText = <<<EOF |
|
| 159 | 159 | <table class="row description" style="border-collapse:collapse;border-spacing:0;display:table;padding:0;position:relative;text-align:left;vertical-align:top;width:100%%"> |
| 160 | 160 | <tbody> |
| 161 | 161 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | </table> |
| 175 | 175 | EOF; |
| 176 | 176 | |
| 177 | - protected $buttonGroup = <<<EOF |
|
| 177 | + protected $buttonGroup = <<<EOF |
|
| 178 | 178 | <table class="spacer" style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%%"> |
| 179 | 179 | <tbody> |
| 180 | 180 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | </table> |
| 228 | 228 | EOF; |
| 229 | 229 | |
| 230 | - protected $button = <<<EOF |
|
| 230 | + protected $button = <<<EOF |
|
| 231 | 231 | <table class="spacer" style="border-collapse:collapse;border-spacing:0;padding:0;text-align:left;vertical-align:top;width:100%%"> |
| 232 | 232 | <tbody> |
| 233 | 233 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | </table> |
| 268 | 268 | EOF; |
| 269 | 269 | |
| 270 | - protected $bodyEnd = <<<EOF |
|
| 270 | + protected $bodyEnd = <<<EOF |
|
| 271 | 271 | |
| 272 | 272 | </td> |
| 273 | 273 | </tr> |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | </table> |
| 279 | 279 | EOF; |
| 280 | 280 | |
| 281 | - protected $footer = <<<EOF |
|
| 281 | + protected $footer = <<<EOF |
|
| 282 | 282 | <table class="spacer float-center" style="Margin:0 auto;border-collapse:collapse;border-spacing:0;float:none;margin:0 auto;padding:0;text-align:center;vertical-align:top;width:100%%"> |
| 283 | 283 | <tbody> |
| 284 | 284 | <tr style="padding:0;text-align:left;vertical-align:top"> |
@@ -304,210 +304,210 @@ discard block |
||
| 304 | 304 | </table> |
| 305 | 305 | EOF; |
| 306 | 306 | |
| 307 | - /** |
|
| 308 | - * @param Defaults $themingDefaults |
|
| 309 | - * @param IURLGenerator $urlGenerator |
|
| 310 | - * @param IL10N $l10n |
|
| 311 | - */ |
|
| 312 | - public function __construct(Defaults $themingDefaults, |
|
| 313 | - IURLGenerator $urlGenerator, |
|
| 314 | - IL10N $l10n) { |
|
| 315 | - $this->themingDefaults = $themingDefaults; |
|
| 316 | - $this->urlGenerator = $urlGenerator; |
|
| 317 | - $this->l10n = $l10n; |
|
| 318 | - $this->htmlBody .= $this->head; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Adds a header to the email |
|
| 323 | - */ |
|
| 324 | - public function addHeader() { |
|
| 325 | - if ($this->headerAdded) { |
|
| 326 | - return; |
|
| 327 | - } |
|
| 328 | - $this->headerAdded = true; |
|
| 329 | - |
|
| 330 | - $logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo()); |
|
| 331 | - $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getColorPrimary(), $logoUrl, $this->themingDefaults->getName()]); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Adds a heading to the email |
|
| 336 | - * |
|
| 337 | - * @param string $title |
|
| 338 | - * @param string $plainTitle|bool Title that is used in the plain text email |
|
| 339 | - * if empty the $title is used, if false none will be used |
|
| 340 | - */ |
|
| 341 | - public function addHeading($title, $plainTitle = '') { |
|
| 342 | - if ($this->footerAdded) { |
|
| 343 | - return; |
|
| 344 | - } |
|
| 345 | - if ($plainTitle === '') { |
|
| 346 | - $plainTitle = $title; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - $this->htmlBody .= vsprintf($this->heading, [htmlspecialchars($title)]); |
|
| 350 | - if ($plainTitle !== false) { |
|
| 351 | - $this->plainBody .= $plainTitle . PHP_EOL . PHP_EOL; |
|
| 352 | - } |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Adds a paragraph to the body of the email |
|
| 357 | - * |
|
| 358 | - * @param string $text |
|
| 359 | - * @param string|bool $plainText Text that is used in the plain text email |
|
| 360 | - * if empty the $text is used, if false none will be used |
|
| 361 | - */ |
|
| 362 | - public function addBodyText($text, $plainText = '') { |
|
| 363 | - if ($this->footerAdded) { |
|
| 364 | - return; |
|
| 365 | - } |
|
| 366 | - if ($plainText === '') { |
|
| 367 | - $plainText = $text; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - if (!$this->bodyOpened) { |
|
| 371 | - $this->htmlBody .= $this->bodyBegin; |
|
| 372 | - $this->bodyOpened = true; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - $this->htmlBody .= vsprintf($this->bodyText, [htmlspecialchars($text)]); |
|
| 376 | - if ($plainText !== false) { |
|
| 377 | - $this->plainBody .= $plainText . PHP_EOL . PHP_EOL; |
|
| 378 | - } |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Adds a button group of two buttons to the body of the email |
|
| 383 | - * |
|
| 384 | - * @param string $textLeft Text of left button |
|
| 385 | - * @param string $urlLeft URL of left button |
|
| 386 | - * @param string $textRight Text of right button |
|
| 387 | - * @param string $urlRight URL of right button |
|
| 388 | - * @param string $plainTextLeft Text of left button that is used in the plain text version - if unset the $textLeft is used |
|
| 389 | - * @param string $plainTextRight Text of right button that is used in the plain text version - if unset the $textRight is used |
|
| 390 | - */ |
|
| 391 | - public function addBodyButtonGroup($textLeft, |
|
| 392 | - $urlLeft, |
|
| 393 | - $textRight, |
|
| 394 | - $urlRight, |
|
| 395 | - $plainTextLeft = '', |
|
| 396 | - $plainTextRight = '') { |
|
| 397 | - if ($this->footerAdded) { |
|
| 398 | - return; |
|
| 399 | - } |
|
| 400 | - if ($plainTextLeft === '') { |
|
| 401 | - $plainTextLeft = $textLeft; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - if ($plainTextRight === '') { |
|
| 405 | - $plainTextRight = $textRight; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - if (!$this->bodyOpened) { |
|
| 409 | - $this->htmlBody .= $this->bodyBegin; |
|
| 410 | - $this->bodyOpened = true; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - $color = $this->themingDefaults->getColorPrimary(); |
|
| 414 | - |
|
| 415 | - $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, htmlspecialchars($textLeft), $urlRight, htmlspecialchars($textRight)]); |
|
| 416 | - $this->plainBody .= $plainTextLeft . ': ' . $urlLeft . PHP_EOL; |
|
| 417 | - $this->plainBody .= $plainTextRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; |
|
| 418 | - |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * Adds a button to the body of the email |
|
| 423 | - * |
|
| 424 | - * @param string $text Text of button |
|
| 425 | - * @param string $url URL of button |
|
| 426 | - * @param string $plainText Text of button in plain text version |
|
| 427 | - * if empty the $text is used, if false none will be used |
|
| 428 | - * |
|
| 429 | - * @since 12.0.0 |
|
| 430 | - */ |
|
| 431 | - public function addBodyButton($text, $url, $plainText = '') { |
|
| 432 | - if ($this->footerAdded) { |
|
| 433 | - return; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - if (!$this->bodyOpened) { |
|
| 437 | - $this->htmlBody .= $this->bodyBegin; |
|
| 438 | - $this->bodyOpened = true; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - if ($plainText === '') { |
|
| 442 | - $plainText = $text; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - $color = $this->themingDefaults->getColorPrimary(); |
|
| 446 | - $this->htmlBody .= vsprintf($this->button, [$color, $color, $url, $color, htmlspecialchars($text)]); |
|
| 447 | - |
|
| 448 | - if ($plainText !== false) { |
|
| 449 | - $this->plainBody .= $plainText . ': '; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - $this->plainBody .= $url . PHP_EOL; |
|
| 453 | - |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email |
|
| 458 | - * |
|
| 459 | - * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically generated email" will be used |
|
| 460 | - */ |
|
| 461 | - public function addFooter($text = '') { |
|
| 462 | - if($text === '') { |
|
| 463 | - $text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan() . '<br>' . $this->l10n->t('This is an automatically generated email, please do not reply.'); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - if ($this->footerAdded) { |
|
| 467 | - return; |
|
| 468 | - } |
|
| 469 | - $this->footerAdded = true; |
|
| 470 | - |
|
| 471 | - if ($this->bodyOpened) { |
|
| 472 | - $this->htmlBody .= $this->bodyEnd; |
|
| 473 | - $this->bodyOpened = false; |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - $this->htmlBody .= vsprintf($this->footer, [$text]); |
|
| 477 | - $this->htmlBody .= $this->tail; |
|
| 478 | - $this->plainBody .= PHP_EOL . '-- ' . PHP_EOL; |
|
| 479 | - $this->plainBody .= str_replace('<br>', PHP_EOL, $text); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Returns the rendered HTML email as string |
|
| 484 | - * |
|
| 485 | - * @return string |
|
| 486 | - */ |
|
| 487 | - public function renderHTML() { |
|
| 488 | - if (!$this->footerAdded) { |
|
| 489 | - $this->footerAdded = true; |
|
| 490 | - if ($this->bodyOpened) { |
|
| 491 | - $this->htmlBody .= $this->bodyEnd; |
|
| 492 | - } |
|
| 493 | - $this->htmlBody .= $this->tail; |
|
| 494 | - } |
|
| 495 | - return $this->htmlBody; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Returns the rendered plain text email as string |
|
| 500 | - * |
|
| 501 | - * @return string |
|
| 502 | - */ |
|
| 503 | - public function renderText() { |
|
| 504 | - if (!$this->footerAdded) { |
|
| 505 | - $this->footerAdded = true; |
|
| 506 | - if ($this->bodyOpened) { |
|
| 507 | - $this->htmlBody .= $this->bodyEnd; |
|
| 508 | - } |
|
| 509 | - $this->htmlBody .= $this->tail; |
|
| 510 | - } |
|
| 511 | - return $this->plainBody; |
|
| 512 | - } |
|
| 307 | + /** |
|
| 308 | + * @param Defaults $themingDefaults |
|
| 309 | + * @param IURLGenerator $urlGenerator |
|
| 310 | + * @param IL10N $l10n |
|
| 311 | + */ |
|
| 312 | + public function __construct(Defaults $themingDefaults, |
|
| 313 | + IURLGenerator $urlGenerator, |
|
| 314 | + IL10N $l10n) { |
|
| 315 | + $this->themingDefaults = $themingDefaults; |
|
| 316 | + $this->urlGenerator = $urlGenerator; |
|
| 317 | + $this->l10n = $l10n; |
|
| 318 | + $this->htmlBody .= $this->head; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Adds a header to the email |
|
| 323 | + */ |
|
| 324 | + public function addHeader() { |
|
| 325 | + if ($this->headerAdded) { |
|
| 326 | + return; |
|
| 327 | + } |
|
| 328 | + $this->headerAdded = true; |
|
| 329 | + |
|
| 330 | + $logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo()); |
|
| 331 | + $this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getColorPrimary(), $logoUrl, $this->themingDefaults->getName()]); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Adds a heading to the email |
|
| 336 | + * |
|
| 337 | + * @param string $title |
|
| 338 | + * @param string $plainTitle|bool Title that is used in the plain text email |
|
| 339 | + * if empty the $title is used, if false none will be used |
|
| 340 | + */ |
|
| 341 | + public function addHeading($title, $plainTitle = '') { |
|
| 342 | + if ($this->footerAdded) { |
|
| 343 | + return; |
|
| 344 | + } |
|
| 345 | + if ($plainTitle === '') { |
|
| 346 | + $plainTitle = $title; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + $this->htmlBody .= vsprintf($this->heading, [htmlspecialchars($title)]); |
|
| 350 | + if ($plainTitle !== false) { |
|
| 351 | + $this->plainBody .= $plainTitle . PHP_EOL . PHP_EOL; |
|
| 352 | + } |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Adds a paragraph to the body of the email |
|
| 357 | + * |
|
| 358 | + * @param string $text |
|
| 359 | + * @param string|bool $plainText Text that is used in the plain text email |
|
| 360 | + * if empty the $text is used, if false none will be used |
|
| 361 | + */ |
|
| 362 | + public function addBodyText($text, $plainText = '') { |
|
| 363 | + if ($this->footerAdded) { |
|
| 364 | + return; |
|
| 365 | + } |
|
| 366 | + if ($plainText === '') { |
|
| 367 | + $plainText = $text; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + if (!$this->bodyOpened) { |
|
| 371 | + $this->htmlBody .= $this->bodyBegin; |
|
| 372 | + $this->bodyOpened = true; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + $this->htmlBody .= vsprintf($this->bodyText, [htmlspecialchars($text)]); |
|
| 376 | + if ($plainText !== false) { |
|
| 377 | + $this->plainBody .= $plainText . PHP_EOL . PHP_EOL; |
|
| 378 | + } |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Adds a button group of two buttons to the body of the email |
|
| 383 | + * |
|
| 384 | + * @param string $textLeft Text of left button |
|
| 385 | + * @param string $urlLeft URL of left button |
|
| 386 | + * @param string $textRight Text of right button |
|
| 387 | + * @param string $urlRight URL of right button |
|
| 388 | + * @param string $plainTextLeft Text of left button that is used in the plain text version - if unset the $textLeft is used |
|
| 389 | + * @param string $plainTextRight Text of right button that is used in the plain text version - if unset the $textRight is used |
|
| 390 | + */ |
|
| 391 | + public function addBodyButtonGroup($textLeft, |
|
| 392 | + $urlLeft, |
|
| 393 | + $textRight, |
|
| 394 | + $urlRight, |
|
| 395 | + $plainTextLeft = '', |
|
| 396 | + $plainTextRight = '') { |
|
| 397 | + if ($this->footerAdded) { |
|
| 398 | + return; |
|
| 399 | + } |
|
| 400 | + if ($plainTextLeft === '') { |
|
| 401 | + $plainTextLeft = $textLeft; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + if ($plainTextRight === '') { |
|
| 405 | + $plainTextRight = $textRight; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + if (!$this->bodyOpened) { |
|
| 409 | + $this->htmlBody .= $this->bodyBegin; |
|
| 410 | + $this->bodyOpened = true; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + $color = $this->themingDefaults->getColorPrimary(); |
|
| 414 | + |
|
| 415 | + $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, htmlspecialchars($textLeft), $urlRight, htmlspecialchars($textRight)]); |
|
| 416 | + $this->plainBody .= $plainTextLeft . ': ' . $urlLeft . PHP_EOL; |
|
| 417 | + $this->plainBody .= $plainTextRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; |
|
| 418 | + |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * Adds a button to the body of the email |
|
| 423 | + * |
|
| 424 | + * @param string $text Text of button |
|
| 425 | + * @param string $url URL of button |
|
| 426 | + * @param string $plainText Text of button in plain text version |
|
| 427 | + * if empty the $text is used, if false none will be used |
|
| 428 | + * |
|
| 429 | + * @since 12.0.0 |
|
| 430 | + */ |
|
| 431 | + public function addBodyButton($text, $url, $plainText = '') { |
|
| 432 | + if ($this->footerAdded) { |
|
| 433 | + return; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + if (!$this->bodyOpened) { |
|
| 437 | + $this->htmlBody .= $this->bodyBegin; |
|
| 438 | + $this->bodyOpened = true; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + if ($plainText === '') { |
|
| 442 | + $plainText = $text; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + $color = $this->themingDefaults->getColorPrimary(); |
|
| 446 | + $this->htmlBody .= vsprintf($this->button, [$color, $color, $url, $color, htmlspecialchars($text)]); |
|
| 447 | + |
|
| 448 | + if ($plainText !== false) { |
|
| 449 | + $this->plainBody .= $plainText . ': '; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + $this->plainBody .= $url . PHP_EOL; |
|
| 453 | + |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email |
|
| 458 | + * |
|
| 459 | + * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically generated email" will be used |
|
| 460 | + */ |
|
| 461 | + public function addFooter($text = '') { |
|
| 462 | + if($text === '') { |
|
| 463 | + $text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan() . '<br>' . $this->l10n->t('This is an automatically generated email, please do not reply.'); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + if ($this->footerAdded) { |
|
| 467 | + return; |
|
| 468 | + } |
|
| 469 | + $this->footerAdded = true; |
|
| 470 | + |
|
| 471 | + if ($this->bodyOpened) { |
|
| 472 | + $this->htmlBody .= $this->bodyEnd; |
|
| 473 | + $this->bodyOpened = false; |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + $this->htmlBody .= vsprintf($this->footer, [$text]); |
|
| 477 | + $this->htmlBody .= $this->tail; |
|
| 478 | + $this->plainBody .= PHP_EOL . '-- ' . PHP_EOL; |
|
| 479 | + $this->plainBody .= str_replace('<br>', PHP_EOL, $text); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Returns the rendered HTML email as string |
|
| 484 | + * |
|
| 485 | + * @return string |
|
| 486 | + */ |
|
| 487 | + public function renderHTML() { |
|
| 488 | + if (!$this->footerAdded) { |
|
| 489 | + $this->footerAdded = true; |
|
| 490 | + if ($this->bodyOpened) { |
|
| 491 | + $this->htmlBody .= $this->bodyEnd; |
|
| 492 | + } |
|
| 493 | + $this->htmlBody .= $this->tail; |
|
| 494 | + } |
|
| 495 | + return $this->htmlBody; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Returns the rendered plain text email as string |
|
| 500 | + * |
|
| 501 | + * @return string |
|
| 502 | + */ |
|
| 503 | + public function renderText() { |
|
| 504 | + if (!$this->footerAdded) { |
|
| 505 | + $this->footerAdded = true; |
|
| 506 | + if ($this->bodyOpened) { |
|
| 507 | + $this->htmlBody .= $this->bodyEnd; |
|
| 508 | + } |
|
| 509 | + $this->htmlBody .= $this->tail; |
|
| 510 | + } |
|
| 511 | + return $this->plainBody; |
|
| 512 | + } |
|
| 513 | 513 | } |
@@ -348,7 +348,7 @@ discard block |
||
| 348 | 348 | |
| 349 | 349 | $this->htmlBody .= vsprintf($this->heading, [htmlspecialchars($title)]); |
| 350 | 350 | if ($plainTitle !== false) { |
| 351 | - $this->plainBody .= $plainTitle . PHP_EOL . PHP_EOL; |
|
| 351 | + $this->plainBody .= $plainTitle.PHP_EOL.PHP_EOL; |
|
| 352 | 352 | } |
| 353 | 353 | } |
| 354 | 354 | |
@@ -374,7 +374,7 @@ discard block |
||
| 374 | 374 | |
| 375 | 375 | $this->htmlBody .= vsprintf($this->bodyText, [htmlspecialchars($text)]); |
| 376 | 376 | if ($plainText !== false) { |
| 377 | - $this->plainBody .= $plainText . PHP_EOL . PHP_EOL; |
|
| 377 | + $this->plainBody .= $plainText.PHP_EOL.PHP_EOL; |
|
| 378 | 378 | } |
| 379 | 379 | } |
| 380 | 380 | |
@@ -413,8 +413,8 @@ discard block |
||
| 413 | 413 | $color = $this->themingDefaults->getColorPrimary(); |
| 414 | 414 | |
| 415 | 415 | $this->htmlBody .= vsprintf($this->buttonGroup, [$color, $color, $urlLeft, $color, htmlspecialchars($textLeft), $urlRight, htmlspecialchars($textRight)]); |
| 416 | - $this->plainBody .= $plainTextLeft . ': ' . $urlLeft . PHP_EOL; |
|
| 417 | - $this->plainBody .= $plainTextRight . ': ' . $urlRight . PHP_EOL . PHP_EOL; |
|
| 416 | + $this->plainBody .= $plainTextLeft.': '.$urlLeft.PHP_EOL; |
|
| 417 | + $this->plainBody .= $plainTextRight.': '.$urlRight.PHP_EOL.PHP_EOL; |
|
| 418 | 418 | |
| 419 | 419 | } |
| 420 | 420 | |
@@ -446,10 +446,10 @@ discard block |
||
| 446 | 446 | $this->htmlBody .= vsprintf($this->button, [$color, $color, $url, $color, htmlspecialchars($text)]); |
| 447 | 447 | |
| 448 | 448 | if ($plainText !== false) { |
| 449 | - $this->plainBody .= $plainText . ': '; |
|
| 449 | + $this->plainBody .= $plainText.': '; |
|
| 450 | 450 | } |
| 451 | 451 | |
| 452 | - $this->plainBody .= $url . PHP_EOL; |
|
| 452 | + $this->plainBody .= $url.PHP_EOL; |
|
| 453 | 453 | |
| 454 | 454 | } |
| 455 | 455 | |
@@ -459,8 +459,8 @@ discard block |
||
| 459 | 459 | * @param string $text If the text is empty the default "Name - Slogan<br>This is an automatically generated email" will be used |
| 460 | 460 | */ |
| 461 | 461 | public function addFooter($text = '') { |
| 462 | - if($text === '') { |
|
| 463 | - $text = $this->themingDefaults->getName() . ' - ' . $this->themingDefaults->getSlogan() . '<br>' . $this->l10n->t('This is an automatically generated email, please do not reply.'); |
|
| 462 | + if ($text === '') { |
|
| 463 | + $text = $this->themingDefaults->getName().' - '.$this->themingDefaults->getSlogan().'<br>'.$this->l10n->t('This is an automatically generated email, please do not reply.'); |
|
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | if ($this->footerAdded) { |
@@ -475,7 +475,7 @@ discard block |
||
| 475 | 475 | |
| 476 | 476 | $this->htmlBody .= vsprintf($this->footer, [$text]); |
| 477 | 477 | $this->htmlBody .= $this->tail; |
| 478 | - $this->plainBody .= PHP_EOL . '-- ' . PHP_EOL; |
|
| 478 | + $this->plainBody .= PHP_EOL.'-- '.PHP_EOL; |
|
| 479 | 479 | $this->plainBody .= str_replace('<br>', PHP_EOL, $text); |
| 480 | 480 | } |
| 481 | 481 | |