PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Lampcms/Controllers/Requestactivation.php

https://github.com/snytkine/LampCMS
PHP | 185 lines | 64 code | 32 blank | 89 comment | 6 complexity | 0e518f53df1acde0bcb92d11c3188c2e MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. *
  4. * License, TERMS and CONDITIONS
  5. *
  6. * This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
  7. * Please read the license here : http://www.gnu.org/licenses/lgpl-3.0.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * ATTRIBUTION REQUIRED
  20. * 4. All web pages generated by the use of this software, or at least
  21. * the page that lists the recent questions (usually home page) must include
  22. * a link to the http://www.lampcms.com and text of the link must indicate that
  23. * the website's Questions/Answers functionality is powered by lampcms.com
  24. * An example of acceptable link would be "Powered by <a href="http://www.lampcms.com">LampCMS</a>"
  25. * The location of the link is not important, it can be in the footer of the page
  26. * but it must not be hidden by style attributes
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31. * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  37. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * This product includes GeoLite data created by MaxMind,
  40. * available from http://www.maxmind.com/
  41. *
  42. *
  43. * @author Dmitri Snytkine <cms@lampcms.com>
  44. * @copyright 2005-2012 (or current year) Dmitri Snytkine
  45. * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
  46. * @link http://www.lampcms.com Lampcms.com project
  47. * @version Release: @package_version@
  48. *
  49. *
  50. */
  51. namespace Lampcms\Controllers;
  52. use Lampcms\WebPage;
  53. use \Lampcms\Mongo\Schema\User as Schema;
  54. /**
  55. * This controller is used when user clicks on "Request activation email" link
  56. * The new activation link is generated and emailed to user's email address
  57. *
  58. */
  59. class Requestactivation extends WebPage
  60. {
  61. protected $membersOnly = true;
  62. protected $layoutID = 1;
  63. protected $oEmail;
  64. protected $email;
  65. protected function main()
  66. {
  67. $this->aPageVars['title'] = '@@Request email confirmation@@';
  68. $this->getEmailObject()
  69. ->makeActivationCode()
  70. ->sendActivationEmail();
  71. $this->aPageVars['body'] = '<div id="tools">@@Activation instructions have just been emailed to@@ ' . $this->email . '</div>';
  72. }
  73. /**
  74. *
  75. * Create object representing email address
  76. * of current Viewer
  77. *
  78. * @throws \Lampcms\NoemailException if unable to find email address
  79. * of current Viewer
  80. *
  81. * @return object $this
  82. */
  83. protected function getEmailObject()
  84. {
  85. $this->email = \mb_strtolower($this->Registry->Viewer->email);
  86. if (empty($this->email)) {
  87. /**
  88. * @todo
  89. * Translate String
  90. */
  91. throw new \Lampcms\NoemailException('@@You have not selected any email address for your account yet@@');
  92. }
  93. try {
  94. $this->oEmail = \Lampcms\Mongo\Doc::factory($this->Registry, 'EMAILS')->byEmail($this->email);
  95. if ('' == $this->oEmail['email']) {
  96. /**
  97. * @todo
  98. * Translate String
  99. */
  100. throw new \Lampcms\NoemailException('@@You have not selected any email address for your account yet@@');
  101. }
  102. } catch ( \MongoException $e ) {
  103. /**
  104. * @todo
  105. * Translate String
  106. */
  107. throw new \Lampcms\NoemailException('@@You have not selected any email address for your account yet@@');
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Generate an activation string, make a link
  113. *
  114. * @return Requestactivation
  115. * @throws \Lampcms\NoticeException
  116. */
  117. protected function makeActivationCode()
  118. {
  119. if ((!empty($this->oEmail['i_vts'])) && $this->oEmail['i_vts'] > 0) {
  120. throw new \Lampcms\NoticeException('@@This account has already been activated@@');
  121. }
  122. $code = $this->oEmail['code'];
  123. if (empty($code)) {
  124. $this->oEmail['code'] = \substr(hash('md5', \uniqid(\mt_rand())), 0, 12);
  125. }
  126. $this->oEmail['i_code_ts'] = time();
  127. $this->oEmail['i_vts'] = null;
  128. $this->oEmail->save();
  129. return $this;
  130. }
  131. /**
  132. * Email activation link to user
  133. *
  134. * @throws \Lampcms\Exception if unable to email
  135. *
  136. * @return object $this
  137. */
  138. protected function sendActivationEmail()
  139. {
  140. $Tr = $this->Registry->Tr;
  141. $tpl = $this->Registry->Ini->SITE_URL . '{_WEB_ROOT_}/{_activate_}/%d/%s';
  142. $link = \sprintf($tpl, $this->oEmail['_id'], $this->oEmail['code']);
  143. d('$link: ' . $link);
  144. $callback = $this->Router->getCallback();
  145. $link = $callback($link);
  146. d('$link after callback: ' . $link);
  147. $siteName = $this->Registry->Ini->SITE_NAME;
  148. $body = $Tr->get('email.body.request_activation', array('{site_title}' => $siteName, '{link}' => $link));
  149. $subject = $Tr->get('email.subject.request_activation', array('{site_title}' => $siteName));
  150. $this->Registry->Mailer->mail($this->email, $subject, $body);
  151. return $this;
  152. }
  153. }