File: /home/centralexf/www/plugins/user/emailactivation.php
<?php
/**
* @package Email Activation
* @copyright Copyright (c)2011 Piotr Gasiorowski
* @license GNU General Public License version 2, or later
*/
// Protect from unauthorized access
defined('_JEXEC') or die('Restricted access');
class plgUserEmailactivation extends JPlugin
{
/**
* Constructor
*
* @access protected
* @param object $subject The object to observe
* @param array $config An array that holds the plugin configuration
* @since 1.5
*/
public function __construct(& $subject, $config)
{
parent::__construct($subject, $config);
$app =& JFactory::getApplication();
$lang =& JFactory::getLanguage();
// TODO: apparently J1.5 has problem with loading default language
if( !empty($lang->_lang) )
{
$path = JPATH_ADMINISTRATOR.DS.'language'.DS.$lang->_lang;
$file = $path.DS.$lang->_lang.'.plg_user_emailactivation.ini';
if(file_exists($file)) {
$this->loadLanguage('',JPATH_ADMINISTRATOR, $lang->_lang);
} else {
$this->loadLanguage('',JPATH_ADMINISTRATOR, 'en-GB');
}
} else {
$this->loadLanguage();
}
if(JRequest::getInt('emailactivation'))
{
$userId = JRequest::getInt('u');
$user =& JFactory::getUser($userId);
if($user->guest) {
// Undelegate wrong users and guests
$app->redirect(JRoute::_(''));
} else {
// get user token from db
$token = md5($user->activation);
// check that the token is in a valid format.
if ( !is_null($token) && strlen($token) === 32 && (int)JRequest::getVar($token, 0, 'get', 'integer')===1)
{
// get user's new email from parameters
$email = $user->getParam('emailactivation');
if(!is_null($email))
{
// remove old email from params
$user->setParam('emailactivation', NULL);
$params =& $user->getParameters();
// load & store user table
$table =& JTable::getInstance('user', 'JTable');
$table->load($userId);
$table->params = $params->toString();
$table->email = $email;
$table->activation = '';
if($this->params->get('block', NULL)) {
// block user account
$table->block = 0;
}
// return; // to prevent save
// save user data
if(!$table->store()) {
JError::raiseError('500', $table->getError());
} else {
$app->enqueueMessage(JText::_('PLG_EMAILACTIVATION_ACTIVATED'));
// Reirect afterwords
$app->redirect(JRoute::_($this->params->get('redirect_url', '')), false);
}
}
} else jexit('Invalid Token');
}
}
}
/**
* @since 1.5
*/
public function onUserBeforeSave($user, $isnew, $new = array())
{
$this->onBeforeStoreUser($user, $isnew, $new);
}
/**
* @since 1.5
*/
public function onUserAfterSave($user, $isnew, $result, $error)
{
$this->onAfterStoreUser($user, $isnew, $result, $error);
}
/**
* @since 1.6
*/
public function onBeforeStoreUser($user, $isnew, $new = array())
{
if(empty($new)) $new = $_POST; // J1.5
// check whether we are going to update user's email
if($isnew || !isset($user['email']) || $user['email'] == $new['email']) return;
$me =& JFactory::getUser();
// super admins can do everything
if(isset($me->gid)) {
if (intval($me->gid) === 25) return; // J1.5
} else {
if($me->authorise('core.admin')) return; // J1.6+
}
// exclude from activating selected groups
if( intval($user['id']) === intval($me->id))
{
// get user groups
$userGroups = (isset($me->gid))? array($me->gid) : $me->groups;
$allowedGroups = $this->params->get('groups', array());
if( count(array_intersect($userGroups, $allowedGroups)) > 0) return;
}
// save old email in session
$session =& JFactory::getSession();
$session->set('emailactivation.old', $user['email']);
}
/**
* @since 1.6
*/
public function onAfterStoreUser($new, $isnew, $result, $error)
{
$userId = intval($new['id']);
$user =& JFactory::getUser($userId);
if(isset($user->gid)) { // J1.5
$new = $_POST;
}
if(!isset($new['email'])) return;
// get old email from session
$session =& JFactory::getSession();
$old = $session->get('emailactivation.old', NULL);
if(is_null($old)) return;
$sending = false;
$app =& JFactory::getApplication();
// if saving user's data was successful
if($result && !$error)
{
// JomSocial Fix
$jsocial = false;
$queue = $app -> getMessageQueue();
if( !empty($queue) )
{
foreach($queue as $msg)
{
if( isset($msg['message']) && strpos($msg['message'], $new['email']) > 1 ) {
$jsocial = true;
break;
}
}
}
if($jsocial) {
$activation = $user->activation;
} else {
$activation = md5(mt_rand());
}
// get the user and store new email in User's Parameters
$user->setParam('emailactivation', $new['email']);
$user->email = $old;
// get the raw User Parameters
$params =& $user->getParameters();
// force old email until new one is activated
$table =& JTable::getInstance('user', 'JTable');
$table->load($userId);
$table->params = $params->toString();
$table->email = $old;
$table->activation = $activation;
if($this->params->get('block', NULL)) {
// block user account
$table->block = 1;
}
// return; // to prevent save
// save user data
if(!$table->store()) {
JError::raiseError('500', $table->getError());
}
else
{
if($jsocial) return;
// store activation in session
$user->activation = $activation;
// Send activation email
$sending = $this->sendActivationEmail($user->getProperties(), $activation, $new['email']);
// Check for an error.
if ($sending === true)
{
JError::raiseNotice('200', JText::sprintf('PLG_EMAILACTIVATION_SENT', htmlspecialchars($new['email'])));
// force user logout
if($this->params->get('logout', NULL) && $userId === intval(JFactory::getUser()->id) )
{
$app->logout();
$app->redirect(JRoute::_(''), false);
}
} else {
JError::raiseWarning('500', JText::_('PLG_EMAILACTIVATION_FAILED'));
}
}
}
// clear session and return
$session->set('emailactivation.old', NULL);
return;
}
/**
* Send activation email to user in order to proof it
*
* @access protected
* @param array $data JUser Properties ($user->getProperties)
* @param string $token Activation token
* @param string $email New Email address
* @since 1.0.2
*/
protected function sendActivationEmail($data, $token, $email)
{
//joomla version
$onepointsix = (file_exists(JPATH_SITE . DS .'components' . DS . 'com_users'))? 's' : '' ;
// prepare activation link separately for J1.5 and J1.6
if($onepointsix) {
$data['siteurl'] = rtrim(JURI::root(), '/').'/index.php?option=com_users&task=edit&emailactivation=1&u='.intval($data['id']).'&'.md5($token).'=1';
} else {
$data['siteurl'] = JURI::root().'index.php?option=com_user&task=logout&emailactivation=1&u='.intval($data['id']).'&'.md5($token).'=1';
}
// Compile the user activated notification mail values.
$config = JFactory::getConfig(); // do not try to get reference
if(!empty($config->_registry['config']['data']))
{
$config_copy = $config->_registry['config']['data'];
$data['fromname'] = $config_copy->fromname;
$data['mailfrom'] = $config_copy->mailfrom;
$data['sitename'] = $config_copy->sitename;
} else {
$data['fromname'] = $config->get('fromname');
$data['mailfrom'] = $config->get('mailfrom');
$data['sitename'] = $config->get('sitename');
}
$emailSubject = JText::sprintf(
'PLG_EMAILACTIVATION_SUBJECT',
$data['name'],
$data['sitename']
);
$emailBody = JText::sprintf(
'PLG_EMAILACTIVATION_BODY',
$data['name'],
$data['sitename'],
$data['siteurl']
);
/*
echo 'Debug: <br>';
echo 'mailfrom:' . $data['mailfrom'].'<br>';
echo 'fromname:' . $data['fromname'].'<br>';
echo '$email:' . $email.'<br>';
echo '$emailSubject:' . $emailSubject.'<br>';
echo '$emailBody:' . $emailBody.'<br>';
echo 'sending: '.(bool)JUtility::sendMail($data['mailfrom'], $data['fromname'], $email, $emailSubject, $emailBody);
exit;
*/
return JUtility::sendMail($data['mailfrom'], $data['fromname'], $email, $emailSubject, $emailBody);
}
}
?>