passwordforgotten.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /* Copyright (C) 2007-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2008-2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2008-2011 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/user/passwordforgotten.php
  22. * \brief Page to ask a new password
  23. */
  24. define("NOLOGIN",1); // This means this output page does not require to be logged.
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  29. if (! empty($conf->ldap->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  30. $langs->load("errors");
  31. $langs->load("users");
  32. $langs->load("companies");
  33. $langs->load("ldap");
  34. $langs->load("other");
  35. // Security check
  36. if (! empty($conf->global->MAIN_SECURITY_DISABLEFORGETPASSLINK))
  37. {
  38. header("Location: ".DOL_URL_ROOT.'/');
  39. exit;
  40. }
  41. $action=GETPOST('action', 'alpha');
  42. $mode=$dolibarr_main_authentication;
  43. if (! $mode) $mode='http';
  44. $username = GETPOST('username');
  45. $passwordhash = GETPOST('passwordhash');
  46. $conf->entity = (GETPOST('entity') ? GETPOST('entity') : 1);
  47. // Instantiate hooks of thirdparty module only if not already define
  48. $hookmanager->initHooks(array('passwordforgottenpage'));
  49. if (GETPOST('dol_hide_leftmenu') || ! empty($_SESSION['dol_hide_leftmenu'])) $conf->dol_hide_leftmenu=1;
  50. if (GETPOST('dol_hide_topmenu') || ! empty($_SESSION['dol_hide_topmenu'])) $conf->dol_hide_topmenu=1;
  51. if (GETPOST('dol_optimize_smallscreen') || ! empty($_SESSION['dol_optimize_smallscreen'])) $conf->dol_optimize_smallscreen=1;
  52. if (GETPOST('dol_no_mouse_hover') || ! empty($_SESSION['dol_no_mouse_hover'])) $conf->dol_no_mouse_hover=1;
  53. if (GETPOST('dol_use_jmobile') || ! empty($_SESSION['dol_use_jmobile'])) $conf->dol_use_jmobile=1;
  54. /**
  55. * Actions
  56. */
  57. // Validate new password
  58. if ($action == 'validatenewpassword' && $username && $passwordhash)
  59. {
  60. $edituser = new User($db);
  61. $result=$edituser->fetch('',$_GET["username"]);
  62. if ($result < 0)
  63. {
  64. $message = '<div class="error">'.$langs->trans("ErrorLoginDoesNotExists",$username).'</div>';
  65. }
  66. else
  67. {
  68. if (dol_hash($edituser->pass_temp) == $passwordhash)
  69. {
  70. $newpassword=$edituser->setPassword($user,$edituser->pass_temp,0);
  71. dol_syslog("passwordforgotten.php new password for user->id=".$edituser->id." validated in database");
  72. header("Location: ".DOL_URL_ROOT.'/');
  73. exit;
  74. }
  75. else
  76. {
  77. $langs->load("errors");
  78. $message = '<div class="error">'.$langs->trans("ErrorFailedToValidatePasswordReset").'</div>';
  79. }
  80. }
  81. }
  82. // Action modif mot de passe
  83. if ($action == 'buildnewpassword' && $username)
  84. {
  85. $sessionkey = 'dol_antispam_value';
  86. $ok=(array_key_exists($sessionkey, $_SESSION) === TRUE && (strtolower($_SESSION[$sessionkey]) == strtolower($_POST['code'])));
  87. // Verify code
  88. if (! $ok)
  89. {
  90. $message = '<div class="error">'.$langs->trans("ErrorBadValueForCode").'</div>';
  91. }
  92. else
  93. {
  94. $edituser = new User($db);
  95. $result=$edituser->fetch('',$username,'',1);
  96. if ($result <= 0 && $edituser->error == 'USERNOTFOUND')
  97. {
  98. $message = '<div class="error">'.$langs->trans("ErrorLoginDoesNotExists",$username).'</div>';
  99. $username='';
  100. }
  101. else
  102. {
  103. if (! $edituser->email)
  104. {
  105. $message = '<div class="error">'.$langs->trans("ErrorLoginHasNoEmail").'</div>';
  106. }
  107. else
  108. {
  109. $newpassword=$edituser->setPassword($user,'',1);
  110. if ($newpassword < 0)
  111. {
  112. // Failed
  113. $message = '<div class="error">'.$langs->trans("ErrorFailedToChangePassword").'</div>';
  114. }
  115. else
  116. {
  117. // Success
  118. if ($edituser->send_password($user,$newpassword,1) > 0)
  119. {
  120. $message = '<div class="ok">'.$langs->trans("PasswordChangeRequestSent",$edituser->login,dolObfuscateEmail($edituser->email)).'</div>';
  121. //$message.=$newpassword;
  122. $username='';
  123. }
  124. else
  125. {
  126. //$message = '<div class="ok">'.$langs->trans("PasswordChangedTo",$newpassword).'</div>';
  127. $message.= '<div class="error">'.$edituser->error.'</div>';
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * View
  136. */
  137. $php_self = $_SERVER['PHP_SELF'];
  138. $php_self.= $_SERVER["QUERY_STRING"]?'?'.$_SERVER["QUERY_STRING"]:'';
  139. $dol_url_root = DOL_URL_ROOT;
  140. // Title
  141. $title='Dolibarr '.DOL_VERSION;
  142. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $title=$conf->global->MAIN_APPLICATION_TITLE;
  143. // Select templates
  144. if (file_exists(DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/passwordforgotten.tpl.php"))
  145. {
  146. $template_dir = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/tpl/";
  147. }
  148. else
  149. {
  150. $template_dir = DOL_DOCUMENT_ROOT."/core/tpl/";
  151. }
  152. // Note: $conf->css looks like '/theme/eldy/style.css.php'
  153. $conf->css = "/theme/".(GETPOST('theme','alpha')?GETPOST('theme','alpha'):$conf->theme)."/style.css.php";
  154. $themepath=dol_buildpath($conf->css,1);
  155. if (! empty($conf->modules_parts['theme'])) // This slow down
  156. {
  157. foreach($conf->modules_parts['theme'] as $reldir)
  158. {
  159. if (file_exists(dol_buildpath($reldir.$conf->css, 0)))
  160. {
  161. $themepath=dol_buildpath($reldir.$conf->css, 1);
  162. break;
  163. }
  164. }
  165. }
  166. $conf_css = $themepath."?lang=".$langs->defaultlang;
  167. $jquerytheme = 'smoothness';
  168. if (! empty($conf->global->MAIN_USE_JQUERY_THEME)) $jquerytheme = $conf->global->MAIN_USE_JQUERY_THEME;
  169. if (! $username) $focus_element = 'username';
  170. else $focus_element = 'password';
  171. // Send password button enabled ?
  172. $disabled='disabled';
  173. if (preg_match('/dolibarr/i',$mode)) $disabled='';
  174. if (! empty($conf->global->MAIN_SECURITY_ENABLE_SENDPASSWORD)) $disabled=''; // To force button enabled
  175. // Show logo (search in order: small company logo, large company logo, theme logo, common logo)
  176. $width=0;
  177. $rowspan=2;
  178. $urllogo=DOL_URL_ROOT.'/theme/login_logo.png';
  179. if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
  180. {
  181. $urllogo=DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode('thumbs/'.$mysoc->logo_small);
  182. }
  183. elseif (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
  184. {
  185. $urllogo=DOL_URL_ROOT.'/viewimage.php?cache=1&amp;modulepart=mycompany&amp;file='.urlencode($mysoc->logo);
  186. $width=128;
  187. }
  188. elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.png'))
  189. {
  190. $urllogo=DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/dolibarr_logo.png';
  191. }
  192. elseif (is_readable(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo.png'))
  193. {
  194. $urllogo=DOL_URL_ROOT.'/theme/dolibarr_logo.png';
  195. }
  196. // Security graphical code
  197. if (function_exists("imagecreatefrompng") && ! $disabled)
  198. {
  199. $captcha = 1;
  200. $captcha_refresh = img_picto($langs->trans("Refresh"),'refresh','id="captcha_refresh_img"');
  201. }
  202. // Execute hook getPasswordForgottenPageOptions
  203. // Should be an array with differents options in $hookmanager->resArray
  204. $parameters=array('entity' => GETPOST('entity','int'));
  205. $hookmanager->executeHooks('getPasswordForgottenPageOptions',$parameters); // Note that $action and $object may have been modified by some hooks
  206. include $template_dir.'passwordforgotten.tpl.php'; // To use native PHP