19 $query =&
$app->getQuery();
21 $jt->import(
'forgot_password.js');
24 if ( isset($query[
'--uuid']) ){
28 df_display(array(),
'xataface/forgot_password/password_has_been_reset.html');
31 throw new Exception(
"Failed to reset password for uuid ".$query[
'--uuid']);
34 }
else if ( isset($query[
'--email']) ){
37 if ( @$query[
'--format'] ==
'json' ){
40 'message'=>
'An email has been sent to the provided email address with instructions for resetting your password.'
44 df_display(array(),
'xataface/forgot_password/sent_email.html');
49 }
else if ( isset($query[
'--username']) ){
52 if ( @$query[
'--format'] ==
'json' ){
55 'message'=>
'An email has been sent to the email on file for this user account with instructions for resetting the password.'
59 df_display(array(),
'xataface/forgot_password/sent_email.html');
67 df_display(array(),
'xataface/forgot_password/form.html');
70 }
catch ( Exception $ex ){
72 if ( @$query[
'--format'] ==
'json' ){
74 'code'=>$ex->getCode(),
75 'message'=>$ex->getMessage()
79 df_display(array(
'error'=>$ex->getMessage()),
'xataface/forgot_password/form.html');
100 $res = mysql_query(
"create table if not exists `{$table}` (
101 request_id int(11) auto_increment primary key,
102 request_uuid binary(32),
103 username varchar(255),
105 date_created datetime,
107 key (request_uuid) )", df_db());
108 if ( !$res )
throw new Exception(mysql_error(df_db()));
118 $res = mysql_query(
"delete from `{$table}` where expires < ".time(), df_db());
119 if ( !$res )
throw new Exception(mysql_error(df_db()));
128 $usernameCol = $auth->usernameColumn;
129 if ( !$usernameCol )
throw new Exception(
"No username Column found in the users table. Please specify one using the username_column directive in the [_auth] section of the conf.ini file.", self::$EX_NO_EMAIL_COLUMN_FOUND);
131 $people = df_get_records_array($auth->usersTable, array($usernameCol =>
'='.$username));
132 if ( !$people )
throw new Exception(
"No account found with that username", self::$EX_NO_USERS_FOUND_WITH_USERNAME);
133 if ( count($people) > 1 ){
134 throw new Exception(
"Multiple users found with same username", self::$EX_MULTIPLE_USERS_WITH_SAME_USERNAME);
160 $emailCol = $auth->getEmailColumn();
161 if ( !$emailCol )
throw new Exception(
"No Email Column found in the users table. Please specify one using the email_column directive in the [_auth] section of the conf.ini file.", self::$EX_NO_EMAIL_COLUMN_FOUND);
163 $people = df_get_records_array($auth->usersTable, array($emailCol =>
'='.$email));
164 if ( !$people )
throw new Exception(
"No account found with that email address", self::$EX_NO_USERS_WITH_EMAIL);
165 if ( count($people) > 1 ){
166 throw new Exception(
"Multiple users found with same email address", self::$EX_MULTIPLE_USERS_WITH_SAME_EMAIL);
189 $emailCol = $auth->getEmailColumn();
190 $usernameCol = $auth->usernameColumn;
192 if ( !$emailCol )
throw new Exception(
"No Email Column found in the users table. Please specify one using the email_column directive in the [_auth] section of the conf.ini file.", self::$EX_NO_EMAIL_COLUMN_FOUND);
193 if ( !$usernameCol )
throw new Exception(
"No username column found in the users table. Please specify one using the username_column directive in the [_auth] section of the conf.ini file.", self::$EX_NO_USERNAME_COLUMN_FOUND);
194 if ( !$user )
throw new Exception(
"Cannot send email for null user", self::$EX_NO_USERS_FOUND_WITH_EMAIL);
197 $username = $user->
val($usernameCol);
199 throw new Exception(
"Cannot reset password for user without a username", self::$EX_NO_USERNAME_FOR_USER);
202 $email = $user->
val($emailCol);
207 $val = ip2long(
$_SERVER[
'REMOTE_ADDR']);
208 if ( $val !==
false ){
209 $ip = sprintf(
'%u', $val );
215 $sql =
"insert into `{$table}`
216 (`request_uuid`, `username`, `request_ip`, `date_created`, `expires`)
218 (UUID(),'".addslashes($username).
"','".addslashes($ip).
"', NOW(), ".(time()+600).
")";
219 $res = mysql_query($sql, df_db());
220 if ( !$res )
throw new Exception(mysql_error(df_db()));
221 $id = mysql_insert_id(df_db());
223 $res = mysql_query(
"select * from `{$table}` where request_id='".addslashes($id).
"'", df_db());
224 if ( !$res )
throw new Exception(mysql_error(df_db()));
226 $row = mysql_fetch_assoc($res);
227 if ( !$row )
throw new Exception(
"Failed to fetch reset password request row from database after it has been inserted. This should never happen ... must be a bug");
229 $uuid = $row[
'request_uuid'];
230 if ( !$uuid )
throw new Exception(
"Blank uuid for the reset request. This should never happen. Must be a bug.");
232 $url = df_absolute_url(DATAFACE_SITE_HREF.
'?-action=forgot_password&--uuid='.$uuid);
233 $site_url = df_absolute_url(DATAFACE_SITE_URL);
236 You have requested to reset the password
for the user
'$username'.
237 Please go to the URL below in order to proceed with resetting your password:
240 If you did not make
this request, please disregard
this email.
243 $subject =
'Password Reset';
246 $del =
$app->getDelegate();
248 if ( isset($del) and method_exists($del,
'getResetPasswordEmailInfo') ){
249 $info = $del->getResetPasswordEmailInfo($user, $url);
252 if ( isset($info[
'subject']) ) $subject = $info[
'subject'];
253 if ( isset($info[
'message']) ) $msg = $info[
'message'];
255 if ( isset($info[
'parameters']) ) $parameters = $info[
'parameters'];
259 $site_title =
$app->getSiteTitle();
260 $support_email =
$_SERVER[
'SERVER_ADMIN'];
261 if ( isset(
$app->_conf[
'admin_email']) ) $support_email =
$app->_conf[
'admin_email'];
262 if ( isset(
$app->_conf[
'support_email']) ) $support_email =
$app->_conf[
'support_email'];
265 $headers =
'From: '.$site_title.
' <'.$support_email.
'>'.
"\r\nReply-to: ".$site_title.
" <".$support_email.
">";
266 if ( isset($info[
'headers']) ) $headers = $info[
'headers'];
268 if ( @
$app->_conf[
'_mail'][
'func'] ) $func =
$app->_conf[
'_mail'][
'func'];
276 throw new Exception(
'Failed to send activation email. Please try again later.',
DATAFACE_E_ERROR);
295 $res = mysql_query(
"select * from `{$table}` where request_uuid='".addslashes($uuid).
"' limit 1", df_db());
296 if ( !$res )
throw new Exception(mysql_error(df_db()));
297 $row = mysql_fetch_assoc($res);
298 if ( !$row )
throw new Exception(
"No such reset request could be found", self::$EX_NO_SUCH_UUID);
300 if ( !$row[
'username'] ){
301 throw new Exception(
"Attempt to reset password for user with null username", self::$EX_NO_USERNAME_FOR_USER);
304 $username = $row[
'username'];
309 @mysql_free_result($res);
316 error_log(
"Rand is ".$rand);
318 for ( $i=0; $i<=16; $i+=2 ){
323 $user = df_get_record($auth->usersTable, array($auth->usernameColumn =>
'='.$username));
325 throw new Exception(
"No user account found with that username", self::$EX_USER_NOT_FOUND);
328 $emailColumn = $auth->getEmailColumn();
329 if ( !$emailColumn )
throw new Exception(
"No email column found in the users table", self::$EX_NO_EMAIL_COLUMN_FOUND);
330 $email = $user->val($emailColumn);
333 throw new Exception(
"User has account has no email address on record. Please contact support to reset the password", self::$EX_NO_EMAIL_FOR_USER);
338 $user->setValue($auth->passwordColumn, $password);
339 $res = $user->save();
341 throw new Exception($res->getMessage());
349 $del =
$app->getDelegate();
351 if ( isset($del) and method_exists($del,
'getPasswordChangedEmailInfo') ){
352 $info = $del->getPasswordChangedEmailInfo($user, $password);
355 $subject =
'Password Changed';
356 if ( isset($info[
'subject']) ) $subject = $info[
'subject'];
359 $site_url = df_absolute_url(DATAFACE_SITE_HREF);
362 Your
new temporary password is
365 You can change your password as follows:
367 1. Log in with your temporary password at <$site_url?-action=login>
368 2. Click on the
"My Profile" link in the upper right of the page
369 3. Click on the
"Edit" tab.
370 4. Change your password in the edit form and click
"Save" when done.
373 if ( isset($info[
'message']) ) $msg = $info[
'message'];
376 if ( isset($info[
'parameters']) ) $parameters = $info[
'parameters'];
380 $site_title =
$app->getSiteTitle();
381 $support_email =
$_SERVER[
'SERVER_ADMIN'];
382 if ( isset(
$app->_conf[
'admin_email']) ) $support_email =
$app->_conf[
'admin_email'];
383 if ( isset(
$app->_conf[
'support_email']) ) $support_email =
$app->_conf[
'support_email'];
385 $headers =
'From: '.$site_title.
' <'.$support_email.
'>'.
"\r\nReply-to: ".$site_title.
" <".$support_email.
">";
386 if ( isset($info[
'headers']) ) $headers = $info[
'headers'];
389 if ( @
$app->_conf[
'_mail'][
'func'] ) $func =
$app->_conf[
'_mail'][
'func'];
409 $res = mysql_query(
"delete from `{$table}` where request_uuid='".addslashes($uuid).
"' limit 1", df_db());
410 if ( !$res )
throw new Exception(mysql_error(df_db()));