PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/include/change_password.php

https://github.com/gobsInternetTechnologyGmbH/private_sales
PHP | 412 lines | 237 code | 129 blank | 46 comment | 46 complexity | 84b664231582ae89dc48ccd9dfcff874 MD5 | raw file
  1. <?php
  2. /* vim: set ts=4 sw=4 sts=4 et: */
  3. /*****************************************************************************\
  4. +-----------------------------------------------------------------------------+
  5. | X-Cart |
  6. | Copyright (c) 2001-2011 Ruslan R. Fazlyev <rrf@x-cart.com> |
  7. | All rights reserved. |
  8. +-----------------------------------------------------------------------------+
  9. | PLEASE READ THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT" |
  10. | FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE |
  11. | AT THE FOLLOWING URL: http://www.x-cart.com/license.php |
  12. | |
  13. | THIS AGREEMENT EXPRESSES THE TERMS AND CONDITIONS ON WHICH YOU MAY USE |
  14. | THIS SOFTWARE PROGRAM AND ASSOCIATED DOCUMENTATION THAT RUSLAN R. |
  15. | FAZLYEV (hereinafter referred to as "THE AUTHOR") IS FURNISHING OR MAKING |
  16. | AVAILABLE TO YOU WITH THIS AGREEMENT (COLLECTIVELY, THE "SOFTWARE"). |
  17. | PLEASE REVIEW THE TERMS AND CONDITIONS OF THIS LICENSE AGREEMENT |
  18. | CAREFULLY BEFORE INSTALLING OR USING THE SOFTWARE. BY INSTALLING, |
  19. | COPYING OR OTHERWISE USING THE SOFTWARE, YOU AND YOUR COMPANY |
  20. | (COLLECTIVELY, "YOU") ARE ACCEPTING AND AGREEING TO THE TERMS OF THIS |
  21. | LICENSE AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY THIS |
  22. | AGREEMENT, DO NOT INSTALL OR USE THE SOFTWARE. VARIOUS COPYRIGHTS AND |
  23. | OTHER INTELLECTUAL PROPERTY RIGHTS PROTECT THE SOFTWARE. THIS |
  24. | AGREEMENT IS A LICENSE AGREEMENT THAT GIVES YOU LIMITED RIGHTS TO USE |
  25. | THE SOFTWARE AND NOT AN AGREEMENT FOR SALE OR FOR TRANSFER OF TITLE.|
  26. | THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT. |
  27. | |
  28. | The Initial Developer of the Original Code is Ruslan R. Fazlyev |
  29. | Portions created by Ruslan R. Fazlyev are Copyright (C) 2001-2011 |
  30. | Ruslan R. Fazlyev. All Rights Reserved. |
  31. +-----------------------------------------------------------------------------+
  32. \*****************************************************************************/
  33. /**
  34. * Change password processor
  35. *
  36. * @category X-Cart
  37. * @package X-Cart
  38. * @subpackage Lib
  39. * @author Ruslan R. Fazlyev <rrf@x-cart.com>
  40. * @copyright Copyright (c) 2001-2011 Ruslan R. Fazlyev <rrf@x-cart.com>
  41. * @license http://www.x-cart.com/license.php X-Cart license agreement
  42. * @version $Id: change_password.php,v 1.60.2.2 2011/01/10 13:11:48 ferz Exp $
  43. * @link http://www.x-cart.com/
  44. * @see ____file_see____
  45. */
  46. if ( !defined('XCART_SESSION_START') ) { header("Location: ../"); die("Access denied"); }
  47. x_load(
  48. 'crypt',
  49. 'user'
  50. );
  51. x_session_register('login');
  52. x_session_register('logged_userid');
  53. x_session_register('login_change');
  54. x_session_register('chpass_referer', array());
  55. if (!isset($chpass_referer[AREA_TYPE])) {
  56. if (
  57. !empty($HTTP_REFERER)
  58. && !preg_match('/change_password.php|help.php\?section=Password_Recovery|error_message.php\?/', $HTTP_REFERER)
  59. && func_is_internal_url($HTTP_REFERER)
  60. ) {
  61. $chpass_referer[AREA_TYPE] = $HTTP_REFERER;
  62. } else {
  63. $chpass_referer[AREA_TYPE] = 'home.php';
  64. }
  65. }
  66. if (!empty($logged_userid)) {
  67. $status = func_query_first_cell("SELECT status FROM $sql_tbl[customers] WHERE id = '$logged_userid'");
  68. if (trim($status) == 'A') {
  69. $url = $chpass_referer[AREA_TYPE];
  70. func_unset($chpass_referer, AREA_TYPE);
  71. func_header_location($url);
  72. }
  73. }
  74. $reset_password = false;
  75. unset($account);
  76. if (
  77. !empty($password_reset_key)
  78. && !empty($user)
  79. ) {
  80. $user = intval($user);
  81. $account = func_query_first("SELECT userid, password_reset_key, password_reset_key_date FROM $sql_tbl[change_password] WHERE userid='$user' AND password_reset_key='".addslashes($password_reset_key)."'");
  82. $is_account_valid = is_array($account) && !empty($account);
  83. $is_url_expired = XC_TIME > ($account['password_reset_key_date'] + 3600);
  84. if (
  85. $is_account_valid
  86. && $is_url_expired
  87. ) {
  88. // Password recovery key is expired
  89. db_query("DELETE FROM $sql_tbl[change_password] WHERE userid='$user'");
  90. $top_message = array(
  91. 'type' => 'E',
  92. 'content' => func_get_langvar_by_name('password_reset_url_expired')
  93. );
  94. func_header_location('home.php');
  95. } elseif (!$is_account_valid) {
  96. $top_message['type'] = 'E';
  97. $top_message['content'] = func_get_langvar_by_name('password_reset_url_invalid');
  98. func_header_location('home.php');
  99. } elseif (!$is_url_expired) {
  100. $tmp = func_query_first("SELECT usertype, login FROM $sql_tbl[customers] WHERE id='$user'");
  101. $account = func_array_merge($account, $tmp);
  102. $smarty->assign('mode', 'recover_password');
  103. $smarty->assign('password_reset_key', $account['password_reset_key']);
  104. $reset_password = true;
  105. }
  106. }
  107. if ($REQUEST_METHOD == 'GET') {
  108. if ($reset_password === true) {
  109. $xlogin = $account['login'];
  110. $xlogin_type = $account['usertype'];
  111. $xuserid = $account['userid'];
  112. } elseif ($mode == 'updated') {
  113. $smarty->assign('mode', $mode);
  114. } elseif (
  115. empty($login)
  116. && !isset($login_change[AREA_TYPE])
  117. ) {
  118. $top_message['content'] = func_get_langvar_by_name('txt_chpass_login');
  119. func_header_location('home.php');
  120. } elseif (isset($login_change[AREA_TYPE])) {
  121. $xuserid = $login_change[AREA_TYPE];
  122. $xlogin_type = AREA_TYPE;
  123. $xlogin = func_get_login_by_userid($xuserid);
  124. } else {
  125. $xlogin = $login;
  126. $xlogin_type = $login_type;
  127. $xuserid = $logged_userid;
  128. }
  129. $smarty->assign('username', $xlogin);
  130. $smarty->assign('usertype', $xlogin_type);
  131. $smarty->assign('userid', $xuserid);
  132. } elseif ($REQUEST_METHOD == 'POST') {
  133. if ($reset_password === true) {
  134. $xlogin = $account['login'];
  135. $xlogin_type = $account['usertype'];
  136. $xuserid = $account['userid'];
  137. } elseif (isset($login_change[AREA_TYPE])) {
  138. $xuserid = $login_change[AREA_TYPE];
  139. $xlogin_type = AREA_TYPE;
  140. $xlogin = func_get_login_by_userid($xuserid);
  141. } else {
  142. $xlogin = $login;
  143. $xlogin_type = $login_type;
  144. $xuserid = $logged_userid;
  145. if (
  146. $xlogin_type == 'A'
  147. && !empty($active_modules['Simple_Mode'])
  148. ) {
  149. $xlogin_type = 'P';
  150. }
  151. }
  152. $smarty->assign('username', $xlogin);
  153. $smarty->assign('usertype', $xlogin_type);
  154. $smarty->assign('userid', $xuserid);
  155. $userinfo = func_userinfo($xuserid, $xlogin_type, true);
  156. $smarty->assign('old_password', $old_password);
  157. $smarty->assign('new_password', $new_password);
  158. $smarty->assign('confirm_password', $confirm_password);
  159. if ($reset_password === true)
  160. $old_password = $userinfo['password'];
  161. if ($userinfo['password'] == '') {
  162. func_header_location('error_message.php');
  163. } elseif ($userinfo['password'] != $old_password) {
  164. $top_message['content'] = func_get_langvar_by_name('txt_chpass_wrong');
  165. $top_message['type'] = 'E';
  166. } elseif ($new_password != $confirm_password) {
  167. $top_message['content'] = func_get_langvar_by_name('txt_chpass_match');
  168. $top_message['type'] = 'E';
  169. } elseif ($new_password == $userinfo['password']) {
  170. $top_message['content'] = func_get_langvar_by_name('txt_chpass_another');
  171. $top_message['type'] = 'E';
  172. } elseif (empty($new_password)) {
  173. $top_message['content'] = func_get_langvar_by_name('txt_chpass_empty');
  174. $top_message['type'] = 'E';
  175. } elseif (strlen($new_password) > 64) {
  176. $top_message['content'] = func_get_langvar_by_name('txt_wrong_password_len');
  177. $top_message['type'] = 'E';
  178. } elseif (func_is_password_weak($new_password)) {
  179. $top_message['content'] = func_get_langvar_by_name('txt_simple_password');
  180. $top_message['type'] = 'E';
  181. } elseif (
  182. $new_password == $xlogin
  183. && $config['Security']['use_complex_pwd'] == 'Y'
  184. ) {
  185. $top_message['content'] = func_get_langvar_by_name('txt_simple_password');
  186. $top_message['type'] = 'E';
  187. } elseif ($config['Security']['check_old_passwords'] == 'Y') {
  188. // Checking whether the password entered by the user is the same as any of the four previously used passwords
  189. $count = func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[old_passwords] WHERE userid='$xuserid' AND password='" . addslashes(md5($new_password)) . "'");
  190. if ($count == '0') {
  191. $old_passwords_ids = func_query_column("SELECT id FROM $sql_tbl[old_passwords] WHERE userid='$xuserid' ORDER BY id DESC LIMIT 2");
  192. if (
  193. is_array($old_passwords_ids)
  194. && !empty($old_passwords_ids)
  195. ) {
  196. $old_passwords_ids = implode("', '", $old_passwords_ids);
  197. db_query("DELETE FROM $sql_tbl[old_passwords] WHERE id NOT IN ('$old_passwords_ids') AND userid='$xuserid'");
  198. }
  199. func_array2insert(
  200. 'old_passwords',
  201. array(
  202. 'userid' => $xuserid,
  203. 'password' => addslashes(md5($userinfo["password"])),
  204. ),
  205. true
  206. );
  207. db_query("DELETE FROM $sql_tbl[change_password] WHERE userid='$xuserid'");
  208. func_array2update(
  209. 'customers',
  210. array(
  211. 'password' => addslashes(text_crypt($new_password)),
  212. 'change_password' => 'N',
  213. 'change_password_date' => XC_TIME,
  214. ),
  215. "id='$xuserid'"
  216. );
  217. x_log_flag(
  218. 'log_activity',
  219. 'ACTIVITY',
  220. "'$xlogin' user has changed password using 'Change password' page"
  221. );
  222. func_unset($login_change, AREA_TYPE);
  223. $top_message['content'] = $reset_password
  224. ? func_get_langvar_by_name('txt_chpass_reset')
  225. : func_get_langvar_by_name('txt_chpass_changed');
  226. func_unset($require_change_password, $xlogin_type);
  227. $url = $chpass_referer[AREA_TYPE];
  228. func_unset($chpass_referer, AREA_TYPE);
  229. func_header_location($url);
  230. } else {
  231. $top_message['content'] = func_get_langvar_by_name('txt_chpass_another');
  232. $top_message['type'] = 'E';
  233. }
  234. } else {
  235. $old_passwords_ids = func_query_column("SELECT id FROM $sql_tbl[old_passwords] WHERE userid='$xuserid' ORDER BY id DESC LIMIT 2");
  236. if (
  237. is_array($old_passwords_ids)
  238. && !empty($old_passwords_ids)
  239. ) {
  240. $old_passwords_ids = implode("', '", $old_passwords_ids);
  241. db_query("DELETE FROM $sql_tbl[old_passwords] WHERE id NOT IN ('$old_passwords_ids') AND userid='$xuserid'");
  242. }
  243. func_array2insert(
  244. 'old_passwords',
  245. array(
  246. 'userid' => $xuserid,
  247. 'password' => addslashes(md5($userinfo["password"])),
  248. ),
  249. true
  250. );
  251. db_query("DELETE FROM $sql_tbl[change_password] WHERE userid='$xuserid'");
  252. func_array2update(
  253. 'customers',
  254. array(
  255. 'password' => addslashes(text_crypt($new_password)),
  256. 'change_password' => 'N',
  257. 'change_password_date' => XC_TIME,
  258. ),
  259. "id='$xuserid'"
  260. );
  261. x_log_flag(
  262. 'log_activity',
  263. 'ACTIVITY',
  264. "'$xlogin' user has changed password using 'Change password' page"
  265. );
  266. func_unset($login_change, AREA_TYPE);
  267. $top_message['content'] = $reset_password
  268. ? func_get_langvar_by_name('txt_chpass_reset')
  269. : func_get_langvar_by_name('txt_chpass_changed');
  270. func_unset($require_change_password, $xlogin_type);
  271. $url = $chpass_referer[AREA_TYPE];
  272. func_unset($chpass_referer, AREA_TYPE);
  273. func_authenticate_user($xuserid);
  274. func_header_location($url);
  275. }
  276. if ($reset_password === true) {
  277. func_header_location("change_password.php?password_reset_key=$password_reset_key&user=$xuserid");
  278. } else {
  279. func_header_location('change_password.php');
  280. }
  281. }
  282. $location[] = array(func_get_langvar_by_name('lbl_chpass'), '');
  283. ?>