2006-12-02 04:36:16 +00:00
< ? php
/*
2010-08-10 19:34:17 +00:00
* e107 website system
*
2014-08-14 17:50:03 +02:00
* Copyright 2008 - 2014 e107 Inc ( e107 . org )
2010-08-10 19:34:17 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
* Interface for users who have forgotten their password
*
2006-12-02 04:36:16 +00:00
*/
2014-08-14 17:50:03 +02:00
2018-01-06 11:31:48 -08:00
$_E107 [ 'allow_guest' ] = true ;
2010-08-10 19:34:17 +00:00
require_once ( 'class2.php' );
2014-08-14 17:50:03 +02:00
e107 :: coreLan ( 'fpw' );
2011-07-22 12:33:55 +00:00
$tp = e107 :: getParser ();
2006-12-02 04:36:16 +00:00
2012-12-02 18:13:57 -08:00
if ( USER && ! getperms ( '0' ))
2008-06-13 20:20:23 +00:00
{
2016-01-13 19:17:37 -08:00
e107 :: redirect ();
2006-12-02 04:36:16 +00:00
exit ;
}
2010-08-10 19:34:17 +00:00
if ( $pref [ 'fpwcode' ] && extension_loaded ( 'gd' ))
2008-06-13 20:20:23 +00:00
{
2014-08-14 17:50:03 +02:00
define ( 'USE_IMAGECODE' , TRUE );
2014-09-11 18:30:52 +02:00
$sec_img = e107 :: getSecureImg ();
2008-06-13 20:20:23 +00:00
}
else
{
2014-08-14 17:50:03 +02:00
define ( 'USE_IMAGECODE' , FALSE );
2006-12-02 04:36:16 +00:00
}
2011-07-22 12:33:55 +00:00
if ( $pref [ 'membersonly_enabled' ])
2008-06-13 20:20:23 +00:00
{
2011-07-22 12:33:55 +00:00
$sc = array (
'FPW_LOGIN_LOGO' => file_exists ( THEME . " images/login_logo.png " ) ? " <img src=' " . THEME_ABS . " images/login_logo.png' alt='' /> \n " : " <img src=' " . e_IMAGE_ABS . " logo.png' alt='' /> \n "
);
2014-01-03 02:43:16 -08:00
if ( deftrue ( 'BOOTSTRAP' ))
{
$FPW_TABLE_HEADER = e107 :: getCoreTemplate ( 'fpw' , 'header' );
$FPW_TABLE_FOOTER = e107 :: getCoreTemplate ( 'fpw' , 'footer' );
}
else
2008-08-29 21:16:39 +00:00
{
2021-01-11 18:05:29 -08:00
$oldDefs = array ( 'LAN_112' => 'LAN_FPW22' );
e107 :: getLanguage () -> bcDefs ( $oldDefs );
2011-07-22 12:33:55 +00:00
require_once ( e107 :: coreTemplatePath ( 'fpw' )); //correct way to load a core template.
2006-12-02 04:36:16 +00:00
}
2016-05-01 10:34:34 -07:00
define ( 'e_IFRAME' , true );
$HEAD = $tp -> simpleParse ( $FPW_TABLE_HEADER , $sc );
$FOOT = $tp -> simpleParse ( $FPW_TABLE_FOOTER , $sc );
define ( 'e_IFRAME_HEADER' , $HEAD );
define ( 'e_IFRAME_FOOTER' , $FOOT );
2006-12-02 04:36:16 +00:00
}
2010-10-26 07:41:20 +00:00
$user_info = e107 :: getUserSession ();
2008-06-13 20:20:23 +00:00
2006-12-02 04:36:16 +00:00
require_once ( HEADERF );
2008-02-18 02:12:06 +00:00
function fpw_error ( $txt )
{
2014-08-16 01:04:32 +02:00
if ( deftrue ( 'BOOTSTRAP' ))
{
e107 :: getMessage () -> addError ( $txt );
e107 :: getRender () -> tablerender ( LAN_03 , e107 :: getMessage () -> render ());
require_once ( FOOTERF );
exit ;
}
e107 :: getRender () -> tablerender ( LAN_03 , " <div class='fpw-page'> " . $txt . " </div> " , 'fpw' );
2006-12-02 04:36:16 +00:00
require_once ( FOOTERF );
exit ;
}
2007-12-15 15:06:40 +00:00
//the separator character used
2010-08-10 19:34:17 +00:00
define ( 'FPW_SEPARATOR' , '#' );
//$fpw_sep = '#';
2006-12-02 04:36:16 +00:00
2007-12-15 15:06:40 +00:00
2014-08-16 01:04:32 +02:00
// User has clicked on the emailed link
if ( e_QUERY )
{
// Make sure login menu is not giving any troubles
2010-08-10 19:34:17 +00:00
define ( 'FPW_ACTIVE' , 'TRUE' );
2014-08-16 01:04:32 +02:00
// Verify the password reset code syntax
2010-08-10 19:34:17 +00:00
$tmpinfo = preg_replace ( " #[ \ W_]# " , " " , e107 :: getParser () -> toDB ( e_QUERY , true )); // query part is a 'random' number
if ( $tmpinfo != e_QUERY )
{
2014-08-16 01:04:32 +02:00
// Shouldn't be any characters that toDB() changes
//die();
e107 :: getRedirect () -> redirect ( SITEURL );
2010-08-10 19:34:17 +00:00
}
2014-08-16 01:04:32 +02:00
// Verify the password reset code
2014-08-14 17:50:03 +02:00
if ( $sql -> select ( 'tmp' , '*' , " `tmp_ip`='pwreset' AND `tmp_info` LIKE '% " . FPW_SEPARATOR . $tmpinfo . " ' " ))
2007-12-15 15:06:40 +00:00
{
2014-08-14 17:50:03 +02:00
$row = $sql -> fetch ();
2014-08-16 01:04:32 +02:00
2019-02-28 12:27:43 -08:00
// Delete the record
if ( time () > ( int ) $row [ 'tmp_time' ])
{
$sql -> delete ( 'tmp' , " `tmp_time` = " . $row [ 'tmp_time' ] . " AND `tmp_info` = ' " . $row [ 'tmp_info' ] . " ' " );
e107 :: getMessage () -> addDebug ( " Tmp Password Reset Entry Deleted " );
}
$sql -> delete ( 'tmp' , " tmp_time < " . time ()); // cleanup table.
2010-08-10 19:34:17 +00:00
2016-06-07 08:37:19 -07:00
list ( $uid , $loginName , $md5 ) = explode ( FPW_SEPARATOR , $row [ 'tmp_info' ]);
2014-08-14 17:50:03 +02:00
$loginName = $tp -> toDB ( $loginName , true );
2010-08-10 19:34:17 +00:00
2014-08-16 01:04:32 +02:00
// This should never happen!
if ( $md5 != $tmpinfo )
2008-08-29 21:16:39 +00:00
{
2014-08-16 01:04:32 +02:00
e107 :: getRedirect () -> redirect ( SITEURL );
2006-12-02 04:36:16 +00:00
}
2010-08-10 19:34:17 +00:00
2014-08-16 01:04:32 +02:00
// Generate new temporary password
2016-06-07 08:37:19 -07:00
$pwdArray = e107 :: getUserSession () -> resetPassword ( $uid , $loginName , array ( 'return' => 'array' ));
if ( $pwdArray === false )
{
fpw_error ( LAN_214 );
}
$newpw = $pwdArray [ 'password' ];
2006-12-02 04:36:16 +00:00
2007-12-15 15:06:40 +00:00
// Details for admin log
2016-06-07 08:37:19 -07:00
$do_log = array ();
$do_log [ 'password_action' ] = LAN_FPW21 ;
$do_log [ 'user_loginname' ] = $loginName ;
$do_log [ 'activation_code' ] = $tmpinfo ;
$do_log [ 'user_password' ] = $newpw ;
$do_log [ 'user_password_hash' ] = $pwdArray [ 'hash' ];
2019-02-28 12:27:43 -08:00
$do_log [ 'expires' ] = date ( DATE_W3C , $row [ 'tmp_time' ]);
2007-12-15 15:06:40 +00:00
2016-06-06 10:24:11 -07:00
2014-08-16 01:04:32 +02:00
// Prepare new information to display to user
2012-05-24 10:02:17 +00:00
if (( integer ) e107 :: getPref ( 'allowEmailLogin' ) > 0 )
{
// always show email when possible
2016-06-07 08:37:19 -07:00
$sql -> select ( 'user' , 'user_email' , " user_id= " . intval ( $uid ));
2014-08-14 17:50:03 +02:00
$tmp = $sql -> fetch ();
2012-05-24 10:02:17 +00:00
$loginName = $tmp [ 'user_email' ];
2016-06-07 08:37:19 -07:00
$do_log [ 'user_email' ] = $tmp [ 'user_email' ];
2012-05-24 10:02:17 +00:00
unset ( $tmp );
}
2008-08-29 21:16:39 +00:00
2016-06-07 08:37:19 -07:00
$admin_log -> user_audit ( USER_AUDIT_PW_RES , $do_log , 0 , $do_log [ 'user_name' ]);
if ( getperms ( '0' )) // Test Mode.
{
echo " <div class='alert alert-danger'> " . print_a ( $do_log , true ) . " </div> " ;
}
else
{
// Reset login cookie/session (?)
cookie ( $pref [ 'cookie_name' ], '' , ( time () - 2592000 ));
$_SESSION [ $pref [ 'cookie_name' ]] = '' ;
}
2006-12-02 04:36:16 +00:00
2014-08-16 01:04:32 +02:00
// Display success message containing new login information
2012-05-24 10:02:17 +00:00
$txt = " <div class='fpw-message'> " . LAN_FPW8 . " </div>
< table class = 'fpw-info' >
2008-08-29 21:16:39 +00:00
< tr >< td > " .LAN_218. " </ td >< td style = 'font-weight:bold' > { $loginName } </ td ></ tr >
2019-02-28 12:27:43 -08:00
< tr >< td > " .LAN_FPW9. " </ td >< td style = 'font-weight:bold' > { $newpw } </ td ></ tr >
2007-12-13 01:01:35 +00:00
</ table >
2016-06-07 08:37:19 -07:00
< br />< br /> " .LAN_FPW10. " < a href = '".e_LOGIN."' > " .LAN_LOGIN. " </ a >. " ; // .LAN_FPW12;
2014-08-16 01:04:32 +02:00
e107 :: getMessage () -> addSuccess ( $txt );
e107 :: getRender () -> tablerender ( LAN_03 , e107 :: getMessage () -> render ());
require_once ( FOOTERF );
exit ;
2011-07-22 12:33:55 +00:00
}
2014-08-16 01:04:32 +02:00
// The password reset code was not found
2011-07-22 12:33:55 +00:00
else
2008-06-13 20:20:23 +00:00
{
2014-08-16 01:04:32 +02:00
fpw_error ( LAN_FPW7 );
2006-12-02 04:36:16 +00:00
}
}
2007-12-15 15:06:40 +00:00
// Request to reset password
2016-12-02 12:04:08 -08:00
if ( ! empty ( $_POST [ 'pwsubmit' ]))
2014-08-14 17:50:03 +02:00
{
2010-08-10 19:34:17 +00:00
require_once ( e_HANDLER . 'mail.php' );
2014-08-14 17:50:03 +02:00
2011-07-22 12:33:55 +00:00
if ( $pref [ 'fpwcode' ] && extension_loaded ( 'gd' ))
2007-12-15 15:06:40 +00:00
{
2011-07-22 12:33:55 +00:00
if ( ! $sec_img -> verify_code ( $_POST [ 'rand_num' ], $_POST [ 'code_verify' ]))
2008-08-29 21:16:39 +00:00
{
2015-07-11 14:46:29 -07:00
fpw_error ( LAN_INVALID_CODE );
2006-12-02 04:36:16 +00:00
}
}
2014-08-14 17:50:03 +02:00
$email = $_POST [ 'email' ];
$clean_email = check_email ( $tp -> toDB ( $_POST [ 'email' ]));
$clean_username = $tp -> toDB ( varset ( $_POST [ 'username' ], '' ));
2008-08-29 21:16:39 +00:00
$query = " `user_email`=' { $clean_email } ' " ;
2006-12-02 04:36:16 +00:00
// Allow admins to remove 'username' from fpw_template.php if they wish.
2008-08-29 21:16:39 +00:00
$query .= ( isset ( $_POST [ 'username' ])) ? " AND `user_loginname`=' { $clean_username } ' " : " " ;
2006-12-02 04:36:16 +00:00
2014-08-14 17:50:03 +02:00
if ( $sql -> select ( 'user' , '*' , $query ))
2014-08-16 01:04:32 +02:00
{
// Found user in DB
2014-08-14 17:50:03 +02:00
$row = $sql -> fetch ();
2011-07-22 12:33:55 +00:00
2014-08-16 01:04:32 +02:00
// Main admin expected to be competent enough to never forget password! (And its a security check - so warn them)
2016-12-02 12:04:08 -08:00
// Sending email to admin alerting them of attempted admin password reset, and redirect user to homepage.
if ( ! getperms ( '0' )) // disabled when testing as main-admin.
{
if (( $row [ 'user_admin' ] == 1 ) && (( $row [ 'user_perms' ] == '0' ) OR ( $row [ 'user_perms' ] == '0.' )))
{
sendemail ( $pref [ 'siteadminemail' ], LAN_06 , LAN_07 . ' [' . e107 :: getIPHandler () -> getIP ( FALSE ) . '] ' . e107 :: getIPHandler () -> getIP ( TRUE ) . ' ' . LAN_08 );
e107 :: getRedirect () -> redirect ( SITEURL );
}
2006-12-02 04:36:16 +00:00
}
2014-08-16 01:04:32 +02:00
// Banned user, or not validated
switch ( $row [ 'user_ban' ])
{
case USER_BANNED :
e107 :: getRedirect () -> redirect ( SITEURL );
2015-02-15 02:37:36 -08:00
break ;
2014-08-16 01:04:32 +02:00
case USER_VALIDATED :
2010-08-10 19:34:17 +00:00
break ;
2014-08-16 01:04:32 +02:00
default :
fpw_error ( LAN_02 . ':' . $row [ 'user_ban' ]); // Intentionally rather a vague message
2010-08-10 19:34:17 +00:00
exit ;
}
2014-08-16 01:04:32 +02:00
// Check if password reset was already requested
2014-08-14 17:50:03 +02:00
if ( $result = $sql -> select ( 'tmp' , '*' , " `tmp_ip` = 'pwreset' AND `tmp_info` LIKE ' " . $row [ 'user_loginname' ] . FPW_SEPARATOR . " %' " ))
2007-12-15 15:06:40 +00:00
{
2016-06-06 10:24:11 -07:00
fpw_error ( LAN_FPW4 );
2010-08-10 19:34:17 +00:00
exit ;
2006-12-02 04:36:16 +00:00
}
2014-08-16 01:04:32 +02:00
// Set unique reset code
2016-12-02 12:04:08 -08:00
$datekey = microtime ( true );
2019-02-28 12:27:43 -08:00
$rcode = e107 :: getUserSession () -> generateRandomString ( '############' );
// $rcode = crypt(($_SERVER['HTTP_USER_AGENT'] . serialize($pref). $clean_email . $datekey), e_TOKEN);
2006-12-02 04:36:16 +00:00
2014-08-16 01:04:32 +02:00
// Prepare email
$link = SITEURL . 'fpw.php?' . $rcode ;
$message = LAN_FPW5 . ' ' . SITENAME . ' ' . LAN_FPW14 . ': ' . e107 :: getIPHandler () -> getIP ( TRUE ) . " . \n \n " . LAN_FPW15 . " \n \n " . LAN_FPW16 . " \n \n " . LAN_FPW17 . " \n \n { $link } " ;
2006-12-02 04:36:16 +00:00
2014-08-16 01:04:32 +02:00
// Set timestamp two days ahead so it doesn't get auto-deleted
2019-02-28 12:27:43 -08:00
// $deltime = time()+86400 * 2;
$deltime = strtotime ( " + 10 minutes " );
2014-08-16 01:04:32 +02:00
2016-06-07 08:37:19 -07:00
// Insert the password reset request into the database
$insertQry = array (
'tmp_ip' => 'pwreset' ,
'tmp_time' => $deltime ,
'tmp_info' => ( $row [ 'user_id' ] . FPW_SEPARATOR . $row [ 'user_loginname' ] . FPW_SEPARATOR . $rcode )
);
$sql -> insert ( 'tmp' , $insertQry );
2014-08-16 01:04:32 +02:00
// Setup the information to log
$do_log [ 'password_action' ] = LAN_FPW18 ;
$do_log [ 'user_id' ] = $row [ 'user_id' ];
$do_log [ 'user_name' ] = $row [ 'user_name' ];
$do_log [ 'user_loginname' ] = $row [ 'user_loginname' ];
$do_log [ 'activation_code' ] = $rcode ;
2015-05-18 11:49:32 -07:00
if ( getperms ( '0' ))
{
2019-11-05 09:10:27 +01:00
$message .= " \n \n <hr><strong>Caution: If you click the button below, you will follow the link the user receives in their email, and the password will actually be reset!</strong> " ; // NO LAN
$message .= " \n \n <a class='btn btn-primary' href=' " . $link . " '>Click to Continue with test</a> " ; // NO LAN
2016-06-07 08:37:19 -07:00
$ns -> tablerender ( " Testing Mode " , nl2br ( $message ));
2015-05-18 11:49:32 -07:00
require_once ( FOOTERF );
exit ;
}
2014-08-16 01:04:32 +02:00
// Try to send the email
2015-05-18 11:49:32 -07:00
if ( sendemail ( $clean_email , " " . LAN_09 . " " . SITENAME , $message ))
2007-12-15 15:06:40 +00:00
{
2014-08-16 01:04:32 +02:00
e107 :: getMessage () -> addInfo ( LAN_FPW6 );
$do_log [ 'password_result' ] = LAN_FPW20 ;
2011-07-22 12:33:55 +00:00
}
else
2007-12-15 15:06:40 +00:00
{
2014-08-16 01:04:32 +02:00
//$text = "<div style='text-align:center'>".LAN_02."</div>";
$do_log [ 'password_result' ] = LAN_FPW19 ;
fpw_error ( LAN_02 );
2006-12-02 04:36:16 +00:00
}
2014-08-16 01:04:32 +02:00
// Log to user audit log
2020-12-22 09:36:02 -08:00
e107 :: getLog () -> user_audit ( USER_AUDIT_PW_RES , $do_log , $row [ 'user_id' ], $row [ 'user_name' ]);
2014-08-16 01:04:32 +02:00
$ns -> tablerender ( LAN_03 , $text . e107 :: getMessage () -> render ());
2006-12-02 04:36:16 +00:00
require_once ( FOOTERF );
exit ;
2011-07-22 12:33:55 +00:00
}
else
2007-12-15 15:06:40 +00:00
{
2014-08-16 01:04:32 +02:00
//$text = LAN_213;
//$ns->tablerender(LAN_214, "<div style='text-align:center'>".$text."</div>");
2017-08-04 16:45:16 +07:00
//e107::getMessage()->addError(LAN_213);
//$ns->tablerender(LAN_214, e107::getMessage()->render());
fpw_error ( LAN_213 );
2006-12-02 04:36:16 +00:00
}
}
2014-08-16 01:04:32 +02:00
$sc = array (); // needed?
2013-05-31 15:54:45 -07:00
2014-01-03 02:43:16 -08:00
if ( deftrue ( 'BOOTSTRAP' ))
{
2014-08-16 01:04:32 +02:00
$FPW_TABLE = " <form method='post' action=' " . SITEURL . " fpw.php' autocomplete='off'> " ;
2016-06-06 10:24:11 -07:00
if ( getperms ( '0' ))
{
$FPW_TABLE .= " <div class='alert alert-danger'>Logged in as admin</div> " ;
}
2014-08-16 01:04:32 +02:00
$FPW_TABLE .= e107 :: getCoreTemplate ( 'fpw' , 'form' );
$FPW_TABLE .= " </form> " ;
2014-01-03 02:43:16 -08:00
$caption = deftrue ( 'LAN_FPW_100' , " Forgot your password? " );
}
2014-08-16 01:04:32 +02:00
elseif ( ! $FPW_TABLE )
2008-08-29 21:16:39 +00:00
{
2011-07-22 12:33:55 +00:00
require_once ( e107 :: coreTemplatePath ( 'fpw' )); //correct way to load a core template.
2014-01-03 02:43:16 -08:00
$caption = LAN_03 ;
2006-12-02 04:36:16 +00:00
}
2013-05-31 15:54:45 -07:00
2021-01-12 14:46:10 -08:00
$sc = e107 :: getScBatch ( 'fpw' ); // fpw_shortcodes;
2022-10-07 12:41:36 +02:00
$sc -> wrapper ( 'fpw/form' );
2013-05-31 15:54:45 -07:00
// New Shortcode names in v2. BC Fix.
$bcShortcodes = array ( '{FPW_TABLE_SECIMG_LAN}' , '{FPW_TABLE_SECIMG_HIDDEN}' , '{FPW_TABLE_SECIMG_SECIMG}' , '{FPW_TABLE_SECIMG_TEXTBOC}' );
$nwShortcodes = array ( '{FPW_CAPTCHA_LAN}' , '{FPW_CAPTCHA_HIDDEN}' , '{FPW_CAPTCHA_IMG}' , '{FPW_CAPTCHA_INPUT}' );
$FPW_TABLE = str_replace ( $bcShortcodes , $nwShortcodes , $FPW_TABLE );
$text = $tp -> parseTemplate ( $FPW_TABLE , true , $sc );
2014-01-03 02:43:16 -08:00
2019-03-24 13:08:44 +01:00
$ns -> tablerender ( $caption , $text , 'fpw' );
2006-12-02 04:36:16 +00:00
require_once ( FOOTERF );