PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/OA/Admin/PasswordRecovery.php

https://bitbucket.org/blackriver/openx
PHP | 262 lines | 145 code | 34 blank | 83 comment | 17 complexity | 426b702fd7d59e0afcb2b53cb849b524 MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id: PasswordRecovery.php 81772 2012-09-11 00:07:29Z chris.nutting $
  25. */
  26. /**
  27. * Password recovery for Openads
  28. *
  29. */
  30. require_once MAX_PATH . '/lib/OA.php';
  31. require_once MAX_PATH . '/lib/OA/Dal/PasswordRecovery.php';
  32. require_once MAX_PATH . '/lib/OA/Auth.php';
  33. require_once MAX_PATH . '/lib/OA/Email.php';
  34. require_once MAX_PATH . '/lib/OA/ServiceLocator.php';
  35. require_once LIB_PATH . '/Admin/Redirect.php';
  36. class OA_Admin_PasswordRecovery
  37. {
  38. /**
  39. * @var OA_Dal_PasswordRecovery
  40. */
  41. var $_dal;
  42. /**
  43. * PHP4-style constructor
  44. */
  45. function OA_Admin_PasswordRecovery()
  46. {
  47. $this->_useDefaultDal();
  48. }
  49. function _useDefaultDal()
  50. {
  51. $oServiceLocator =& OA_ServiceLocator::instance();
  52. $dal =& $oServiceLocator->get('OA_Dal_PasswordRecovery');
  53. if (!$dal) {
  54. $dal = new OA_Dal_PasswordRecovery();
  55. }
  56. $this->_dal =& $dal;
  57. }
  58. /**
  59. * Display page header
  60. *
  61. */
  62. function pageHeader()
  63. {
  64. phpAds_PageHeader(phpAds_PasswordRecovery);
  65. echo "<br><br>";
  66. }
  67. /**
  68. * Display page footer and make sure that the session gets destroyed
  69. *
  70. */
  71. function pageFooter()
  72. {
  73. // Remove session
  74. unset($GLOBALS['session']);
  75. phpAds_PageFooter();
  76. }
  77. /**
  78. * Display an entire page with the password recovery form.
  79. *
  80. * This method, combined with handlePost allows semantic, REST-style
  81. * actions.
  82. */
  83. function handleGet($vars)
  84. {
  85. $this->pageHeader();
  86. if (empty($vars['id'])) {
  87. $this->displayRecoveryRequestForm();
  88. } elseif ($this->_dal->checkRecoveryId($vars['id'])) {
  89. $this->displayRecoveryResetForm($vars['id']);
  90. } else {
  91. OX_Admin_Redirect::redirect();
  92. }
  93. $this->pageFooter();
  94. }
  95. /**
  96. * Display an entire page with the password recovery form.
  97. *
  98. * This method, combined with handleGet allows semantic, REST-style
  99. * actions.
  100. */
  101. function handlePost($vars)
  102. {
  103. $this->pageHeader();
  104. if (empty($vars['id'])) {
  105. if (empty($vars['email'])) {
  106. $this->displayRecoveryRequestForm($GLOBALS['strEmailRequired']);
  107. } else {
  108. $sent = $this->sendRecoveryEmail(stripslashes($vars['email']));
  109. if ($sent) {
  110. $this->displayMessage($GLOBALS['strNotifyPageMessage']);
  111. } else {
  112. $this->displayRecoveryRequestForm($GLOBALS['strPwdRecEmailNotFound']);
  113. }
  114. }
  115. } else {
  116. if (empty($vars['newpassword']) || empty($vars['newpassword2']) || $vars['newpassword'] != $vars['newpassword2']) {
  117. $this->displayRecoveryResetForm($vars['id'], $GLOBALS['strNotSamePasswords']);
  118. } elseif ($this->_dal->checkRecoveryId($vars['id'])) {
  119. $userId = $this->_dal->saveNewPasswordAndLogin($vars['id'], stripslashes($vars['newpassword']));
  120. OX_Admin_Redirect::redirect();
  121. } else {
  122. $this->displayRecoveryRequestForm($GLOBALS['strPwdRecWrongId']);
  123. }
  124. }
  125. $this->pageFooter();
  126. }
  127. /**
  128. * Display a message
  129. *
  130. * @param string message to be displayed
  131. */
  132. function displayMessage($message)
  133. {
  134. phpAds_showBreak();
  135. echo "<br /><span class='install'>{$message}</span><br /><br />";
  136. phpAds_showBreak();
  137. }
  138. /**
  139. * Display recovery request form
  140. *
  141. * @param string error message text
  142. */
  143. function displayRecoveryRequestForm($errormessage = '')
  144. {
  145. if (!empty($errormessage)) {
  146. echo "<div class='errormessage' style='width: 400px;'><img class='errormessage' src='" . OX::assetPath() . "/images/errormessage.gif' align='absmiddle'>";
  147. echo "<span class='tab-r'>{$errormessage}</span></div>";
  148. }
  149. echo "<form method='post' action='password-recovery.php'>\n";
  150. echo "<div class='install'>".$GLOBALS['strPwdRecEnterEmail']."</div>";
  151. echo "<table cellpadding='0' cellspacing='0' border='0'>";
  152. echo "<tr><td colspan='2'><img src='" . OX::assetPath() . "/images/break-el.gif' width='400' height='1' vspace='8'></td></tr>";
  153. echo "<tr height='24'><td>".$GLOBALS['strEMail'].":&nbsp;</td><td><input type='text' name='email' /></td></tr>";
  154. echo "<tr height='24'><td>&nbsp;</td><td><input type='submit' value='".$GLOBALS['strProceed']."' /></td></tr>";
  155. echo "<tr><td colspan='2'><img src='" . OX::assetPath() . "/images/break-el.gif' width='400' height='1' vspace='8'></td></tr>";
  156. echo "</table>";
  157. echo "</form>\n";
  158. }
  159. /**
  160. * Display new password form
  161. *
  162. * @param string error message text
  163. */
  164. function displayRecoveryResetForm($id, $errormessage = '')
  165. {
  166. if (!empty($errormessage)) {
  167. echo "<div class='errormessage' style='width: 400px;'><img class='errormessage' src='" . OX::assetPath() . "/images/errormessage.gif' align='absmiddle'>";
  168. echo "<span class='tab-r'>{$errormessage}</span></div>";
  169. }
  170. echo "<form method='post' action='password-recovery.php'>\n";
  171. echo "<input type='hidden' name='id' value=\"".htmlspecialchars($id)."\" />";
  172. echo "<div class='install'>".$GLOBALS['strPwdRecEnterPassword']."</div>";
  173. echo "<table cellpadding='0' cellspacing='0' border='0'>";
  174. echo "<tr><td colspan='2'><img src='" . OX::assetPath() . "/images/break-el.gif' width='400' height='1' vspace='8'></td></tr>";
  175. echo "<tr height='24'><td>".$GLOBALS['strPassword'].":&nbsp;</td><td><input type='password' name='newpassword' /></td></tr>";
  176. echo "<tr height='24'><td>".$GLOBALS['strRepeatPassword'].":&nbsp;</td><td><input type='password' name='newpassword2' /></td></tr>";
  177. echo "<tr height='24'><td>&nbsp;</td><td><input type='submit' value='".$GLOBALS['strProceed']."' /></td></tr>";
  178. echo "<tr><td colspan='2'><img src='" . OX::assetPath() . "/images/break-el.gif' width='400' height='1' vspace='8'></td></tr>";
  179. echo "</table>";
  180. echo "</form>\n";
  181. }
  182. /**
  183. * Check if the user is allowed to see the password recovery tools
  184. *
  185. */
  186. function checkAccess()
  187. {
  188. return !OA_Auth::isLoggedIn() && !OA_Auth::suppliedCredentials();
  189. }
  190. /**
  191. * Send the password recovery email
  192. *
  193. * @todo Set email language according to the account preferences
  194. *
  195. * @param string email address
  196. * @return int Number of emails sent
  197. */
  198. function sendRecoveryEmail($email)
  199. {
  200. $aConf = &$GLOBALS['_MAX']['CONF'];
  201. $aPref = $GLOBALS['_MAX']['PREF'];
  202. $aUsers = $this->_dal->searchMatchingUsers($email);
  203. $aEmails = array();
  204. foreach ($aUsers as $u) {
  205. $aEmails[$u['email_address']][] = $u;
  206. }
  207. $sent = 0;
  208. foreach ($aEmails as $email => $aUsers) {
  209. $text = '';
  210. foreach ($aUsers as $u) {
  211. $recoveryId = $this->_dal->generateRecoveryId($u['user_id']);
  212. $header = $GLOBALS['strUser']." {$u['contact_name']}";
  213. $text .= $header."\n".str_repeat('-', strlen($header))."\n";
  214. $text .= $GLOBALS['strUsername'].": {$u['username']}\n";
  215. $text .= $GLOBALS['strPwdRecResetLink'].": ";
  216. $text .= Max::constructURL(MAX_URL_ADMIN, "password-recovery.php?id={$recoveryId}")."\n\n";
  217. }
  218. // Hack
  219. $aConf['email']['admin_name'] = $aPref['admin_fullname'];
  220. $aConf['email']['admin'] = $aPref['admin_email'];
  221. $oEmail = new OA_Email();
  222. $oEmail->sendMail(sprintf($GLOBALS['strPwdRecEmailPwdRecovery'], $aPref['name']), $text, $email, $u['username']);
  223. $sent++;
  224. }
  225. return $sent;
  226. }
  227. }
  228. ?>