diff --git a/.gitignore b/.gitignore index 29f582b27..5abfb5e0c 100755 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,8 @@ vagrant/.vagrant/ vagrant/*.log /app/Lib/Dashboard/Custom/* !/app/Lib/Dashboard/Custom/empty +/app/View/Emails/html/Custom/* +!/app/View/Emails/html/Custom/empty +/app/View/Emails/text/Custom/* +!/app/View/Emails/text/Custom/empty + diff --git a/app/Console/Command/SupportShell.php b/app/Console/Command/SupportShell.php index 46693db05..30eee1738 100644 --- a/app/Console/Command/SupportShell.php +++ b/app/Console/Command/SupportShell.php @@ -1,5 +1,3 @@ -http://download.geonames.org/export/dump/countryInfo.txt - viewPath = $View->layoutPath = 'Emails' . DS . 'html'; try { - $html = $View->render($this->viewName); + $View->viewPath = $View->layoutPath = 'Emails' . DS . 'html' . DS . 'Custom'; + $html = $View->render($this->viewName); // Attempt to load a custom template if it exists } catch (MissingViewException $e) { - $html = null; // HTMl template is optional + $View->viewPath = $View->layoutPath = 'Emails' . DS . 'html'; + try { + $html = $View->render($this->viewName); + } catch (MissingViewException $e) { + $html = null; // HTMl template is optional + } } - $View->viewPath = $View->layoutPath = 'Emails' . DS . 'text'; + $View->hasRendered = false; - $text = $View->render($this->viewName); + try { + $View->viewPath = $View->layoutPath = 'Emails' . DS . 'text' . DS . 'Custom'; + $text = $View->render($this->viewName); // Attempt to load a custom template if it exists + } catch (MissingViewException $e) { + $View->viewPath = $View->layoutPath = 'Emails' . DS . 'text'; + $text = $View->render($this->viewName); + } // Template can change default subject. if ($View->get('subject')) { diff --git a/app/Model/User.php b/app/Model/User.php index 36098453d..0bef95def 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -4,6 +4,7 @@ App::uses('AuthComponent', 'Controller/Component'); App::uses('RandomTool', 'Tools'); App::uses('GpgTool', 'Tools'); App::uses('SendEmail', 'Tools'); +App::uses('SendEmailTemplate', 'Tools'); App::uses('BlowfishConstantPasswordHasher', 'Controller/Component/Auth'); /** @@ -832,6 +833,7 @@ class User extends AppModel $gpg = $this->initializeGpg(); $sendEmail = new SendEmail($gpg); + $result = $sendEmail->sendToUser($user, $subject, $body, $bodyNoEnc,$replyToUser ?: []); try { $result = $sendEmail->sendToUser($user, $subject, $body, $bodyNoEnc,$replyToUser ?: []); @@ -867,16 +869,6 @@ class User extends AppModel return true; } - public function adminMessageResolve($message) - { - $resolveVars = array('$contact' => 'MISP.contact', '$org' => 'MISP.org', '$misp' => 'MISP.baseurl'); - foreach ($resolveVars as $k => $v) { - $v = Configure::read($v); - $message = str_replace($k, $v, $message); - } - return $message; - } - /** * @param string $email * @return array @@ -955,24 +947,15 @@ class User extends AppModel public function initiatePasswordReset($user, $firstTime = false, $simpleReturn = false, $fixedPassword = false) { $org = Configure::read('MISP.org'); - $options = array('newUserText', 'passwordResetText'); $subjects = array('[' . $org . ' MISP] New user registration', '[' . $org . ' MISP] Password reset'); - $textToFetch = $options[($firstTime ? 0 : 1)]; $subject = $subjects[($firstTime ? 0 : 1)]; $this->Server = ClassRegistry::init('Server'); - $body = Configure::read('MISP.' . $textToFetch); - if (!$body) { - $body = $this->Server->serverSettings['MISP'][$textToFetch]['value']; - } - $body = $this->adminMessageResolve($body); if ($fixedPassword) { $password = $fixedPassword; } else { $password = $this->generateRandomPassword(); } - $body = str_replace('$password', $password, $body); - $body = str_replace('$username', $user['User']['email'], $body); - $body = str_replace('\n', PHP_EOL, $body); + $body = $this->preparePasswordResetEmail($user, $password, $firstTime, $subject); $result = $this->sendEmail($user, $body, false, $subject); if ($result) { $this->id = $user['User']['id']; @@ -991,6 +974,22 @@ class User extends AppModel } } + private function preparePasswordResetEmail($user, $password, $firstTime, $subject) + { + $textToFetch = $firstTime ? 'newUserText': 'passwordResetText'; + $this->Server = ClassRegistry::init('Server'); + $bodyTemplate = Configure::read('MISP.' . $textToFetch); + if (!$bodyTemplate) { + $bodyTemplate = $this->Server->serverSettings['MISP'][$textToFetch]['value']; + } + $template = new SendEmailTemplate('password_reset'); + $template->set('body', $bodyTemplate); + $template->set('user', $user); + $template->set('password', $password); + $template->subject($subject); + return $template; + } + public function getOrgAdminsForOrg($org_id, $excludeUserId = false) { $adminRoles = $this->Role->find('column', array( diff --git a/app/View/Emails/html/Custom/empty b/app/View/Emails/html/Custom/empty new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/app/View/Emails/html/Custom/empty @@ -0,0 +1 @@ + diff --git a/app/View/Emails/text/Custom/empty b/app/View/Emails/text/Custom/empty new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/app/View/Emails/text/Custom/empty @@ -0,0 +1 @@ + diff --git a/app/View/Emails/text/password_reset.ctp b/app/View/Emails/text/password_reset.ctp new file mode 100644 index 000000000..8dd972789 --- /dev/null +++ b/app/View/Emails/text/password_reset.ctp @@ -0,0 +1,14 @@ + 'MISP.contact', + '$org' => 'MISP.org', + '$misp' => 'MISP.baseurl' + ]; + foreach ($resolveVars as $k => $v) { + $v = Configure::read($v); + $body= str_replace($k, $v, $body); + } + echo $body;