Created
June 3, 2014 07:09
-
-
Save yireo/f2f58c2892f5c81bd512 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// User settings | |
define('userDataName', 'EXAMPLE'); | |
define('userDataUsername', 'example'); | |
define('userDataEmail', '[email protected]'); | |
define('userDataPassword', 'YOURPASSWORD'); | |
// Detect the Joomla! directory | |
define('DOCUMENT_ROOT', dirname(__FILE__).'/'); | |
/* | |
* Joomla! class | |
*/ | |
class UserJoomla | |
{ | |
/* | |
* ID of Super Users group | |
*/ | |
protected $gid = 25; | |
/* | |
* Main method to run this installer | |
*/ | |
public function __construct() | |
{ | |
// Fake some environment-stuff | |
$_SERVER['HTTP_HOST'] = null; | |
// PHP settings | |
ini_set('display_errors', 0); | |
// Neccessary definitions | |
define('_JEXEC', 1); | |
define('JPATH_BASE', DOCUMENT_ROOT); | |
define('DS', DIRECTORY_SEPARATOR ); | |
// Initialize Joomla! | |
$this->initJoomla(); | |
} | |
public function initJoomla() | |
{ | |
// Change the path to the JPATH_BASE | |
if(!is_file(JPATH_BASE.'/includes/framework.php')) { | |
die('Incorrect Joomla! base-path'); | |
} | |
chdir(JPATH_BASE); | |
// Include the framework | |
require_once(JPATH_BASE.'/includes/defines.php'); | |
require_once(JPATH_BASE.'/includes/framework.php'); | |
jimport('joomla.environment.request'); | |
jimport('joomla.database.database'); | |
// Start the application | |
$mainframe = JFactory::getApplication('site'); | |
$mainframe->initialise(); | |
if(method_exists('JFactory', 'getDbo')) { | |
$db = JFactory::getDbo(); | |
} else { | |
$db = JFactory::getDBO(); | |
} | |
// Add Joomla! variables to this object | |
$this->db = $db; | |
$this->app = $mainframe; | |
// Fetch the Super Users group | |
$db->setQuery('SELECT id FROM #__usergroups WHERE title = "Super Users" LIMIT 1'); | |
$this->gid = (int)$db->loadResult(); | |
if(!$this->gid > 0) { | |
die("FATAL ERROR: Unable to find Super Users group\n"); | |
} | |
// Spoof the first admin-user | |
$db->setQuery('SELECT user_id FROM #__user_usergroup_map WHERE group_id = '.$this->gid.' LIMIT 1'); | |
$id = $db->loadResult(); | |
$my = JFactory::getUser(); | |
$my->load($id); | |
} | |
/* | |
* Return the encrypted version of a password | |
*/ | |
protected function encrypt($password) | |
{ | |
// Generate a salt | |
$salt = null; | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
for($i = 0; $i < 32; $i++) { | |
$salt .= $characters[mt_rand(0, strlen($characters)-1)]; | |
} | |
// Construct the password | |
$password = md5($password.$salt).':'.$salt; | |
return $password; | |
} | |
public function addSuperUser($name, $username, $email, $password) | |
{ | |
// Check for all users | |
$this->db->setQuery('SELECT * FROM #__users WHERE email = '.$this->db->Quote($email).' OR username = '.$this->db->Quote($username)); | |
$rows = $this->db->loadObjectList(); | |
if(!empty($rows)) { | |
$this->db->setQuery('UPDATE #__users SET `password`="'.$this->encrypt($password).'" WHERE email = '.$this->db->Quote($email).' OR username = '.$this->db->Quote($username)); | |
$this->db->query(); | |
echo "ADDSUPERUSER: User '$username' already exists. Reset password though.\n"; | |
return false; | |
} | |
// Initialize the data-set | |
$userData = array( | |
'name' => $name, | |
'username' => $username, | |
'email' => $email, | |
'password' => $this->encrypt($password), | |
); | |
// Set the ACL-parts | |
$userData['usertype'] = 'deprecated'; | |
$userData['groups'] = array($this->gid); | |
// Create the new user | |
$user = new JUser(); | |
foreach($userData as $name => $value) { | |
$user->set($name, $value); | |
} | |
// Save the user | |
if($user->save() == false) { | |
die($user->getError()); | |
} | |
} | |
/* | |
* Remove a specific user | |
*/ | |
public function removeUser($username) | |
{ | |
// Check for all users | |
$this->db->setQuery('SELECT * FROM #__users WHERE username = '.$this->db->Quote($username).' LIMIT 1'); | |
$row = $this->db->loadObject(); | |
if(!empty($row)) { | |
$user = new JUser(); | |
$user->load($row->id); | |
$user->delete(); | |
} | |
echo "REMOVEUSER: User '$username' does not exist\n"; | |
return false; | |
} | |
} | |
// Create the right installer instance | |
$user = new UserJoomla(); | |
$user->addSuperUser(userDataName, userDataUsername, userDataEmail, userDataPassword); | |
$user->removeUser('admin'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment