Skip to content

Instantly share code, notes, and snippets.

@mbabker mbabker/passwordReset.php
Last active Aug 29, 2015

Embed
What would you like to do?
This CLI script enables site administrators to require their site users to reset their password if they have not done so within a specified number of days
<?php
/**
* Password Reset Script
*
* This command line script can be run as a cron job or on user request to flag user accounts on a site
* as requiring their passwords reset. This script flags all users who have not reset their passwords
* in the number of days specified by the user and compares to the lastResetTime column in the #__users
* table.
*
* The number of days can be specified in one of two manners:
*
* - An option passed when calling the script, --days=## (where ## is the number of days)
* - User prompt if the --days option is not specified
*
* @copyright Copyright (C) 2014 Michael Babker. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/
// Set flag that this is a parent file.
const _JEXEC = 1;
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
// Import namespaced Framework classes
use Joomla\Application\Cli\Output\Stdout;
use Joomla\Application\Cli\Output\Processor\ColorProcessor;
/**
* Password reset script
*
* @since 1.0
*/
class PasswordReset extends JApplicationCli
{
/**
* Class constructor
*
* @since 1.0
*/
public function __construct()
{
parent::__construct();
/*
* From here we're going to highlight a new CMS 3.3 feature - use of the Framework's CliOutput object
* to display data in different colors
*/
// We're going to use a Stdout object for output and inject the ColorProcessor object into it to use colored output
$output = new Stdout;
$output->setProcessor(new ColorProcessor);
$this->setOutput($output);
}
/**
* Method to run the application routines.
*
* @return void
*
* @since 1.0
*/
public function doExecute()
{
// Let the user know what's happening
$this->out(
'<info>This script will enable you to require users to reset their password if they haven\'t done so in over the specified number of days.</info>'
);
// Load up the database object
$db = JFactory::getDbo();
// Check if the user supplied an input and prompt the user if not
if ($this->input->getBool('days', false))
{
// Use the supplied input for the calculation
$days = $this->input->getUint('days');
}
else
{
// We need to get the user's input
$this->out(
'<question>Within how many days should the user have last reset their password?</question>'
);
$days = (int) rtrim($this->in(), "\n\r");
}
// Get the date for the query
$date = JFactory::getDate()->sub(new DateInterval('P' . $days . 'D'));
$date = JFactory::getDate((string) $date)->toSql();
// Update the records in the #__users table now
$db->setQuery(
$db->getQuery(true)
->update($db->quoteName('#__users'))
->set($db->quoteName('requireReset') . ' = 1')
->where($db->quoteName('lastResetTime') . ' <= ' . $db->quote($date))
)->execute();
// Get the number of updated records
$updated = $db->getAffectedRows();
// Let the user know we're done
$this->out(
sprintf('<info>Update complete, %s records updated.</info>', $updated)
);
}
}
// Global exception handler, this way we can get decent messages if need be
try
{
JApplicationCli::getInstance('PasswordReset')->execute();
}
catch (Exception $e)
{
fwrite(STDOUT, "\nERROR: " . $e->getMessage() . "\n");
fwrite(STDOUT, "\n" . $e->getTraceAsString() . "\n");
exit($e->getCode() ? : 255);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.