Your IP : 216.73.216.158


Current Path : /home/megadansyp/www/plugins/system/wbreactiv/
Upload File :
Current File : /home/megadansyp/www/plugins/system/wbreactiv/helper.php

<?php
/**
 * wbReactiv - resend confirmation email
 *
 * @author       Yannick Gaultier - Weeblr.com
 * @copyright    (c) Yannick Gaultier - Weeblr llc - 2015
 * @package      wbreactiv
 * @license      http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @version      1.0.7.100
 * @date        2016-07-18
 *
 */

defined('_JEXEC') or die;

/**
 * Helper for plg_System_Wbreactiv
 *
 */
class PlgSystemWbreactivHelper
{
	private static $_message = '';
	private static $_success = true;

	/**
	 * Decide if a user has already activated her account
	 *
	 * @return array response array, ready to be jsonified
	 */
	public static function reactiv($userId, $type)
	{
		if (empty($userId))
		{
			return array('success' => false, 'message' => JText::_('PLG_SYSTEM_WBREACTIV_INVALID_USER_ID'), 'messages' => null, 'data' => null);
		}

		// load user
		$user = self::getUser($userId);

		// check if not already active
		if (self::alreadyActive($user))
		{
			self::$_success = true;
			self::$_message = JText::sprintf('PLG_SYSTEM_WBREACTIV_USER_ALREADY_ACTIVE', $user->username);
		}
		else
		{
			$sent = self::sendEmail($user);

			// setup response
			self::$_success = $sent === true;
			self::$_message = self::$_success ? JText::sprintf('PLG_SYSTEM_WBREACTIV_ACTIVATION_EMAIL_RESENT_TO'
				. ($type == 'list' ? '_SHORT' : ''),
				$user->username) : ($sent instanceof Exception ? $sent->getMessage() : JText::_('COM_USERS_REGISTRATION_SEND_MAIL_FAILED'));
		}

		// build a response to be handled by our js
		$data = array('reqTime' => time());
		$response = array('success' => self::$_success, 'message' => self::$_message, 'messages' => null, 'data' => $data);

		return $response;
	}

	/**
	 * Gets a JUser object from an integer id or a JUser object
	 * Returns unmodified if already an object
	 *
	 * @param int | object $user
	 * @return JUser
	 */
	private static function getUser($user)
	{
		if (empty($user))
		{
			throw new RuntimeException(JText::_('PLG_SYSTEM_WBREACTIV_INVALID_USER_ID'), 500);
		}

		if (is_numeric($user))
		{
			$user = JFactory::getUser($user);
		}

		if (empty($user))
		{
			throw new RuntimeException(JText::_('PLG_SYSTEM_WBREACTIV_INVALID_USER_ID'), 500);
		}

		return $user;
	}

	/**
	 * Decides if user has already activated account
	 *
	 * @param int | object $user
	 * @return bool
	 */
	public static function alreadyActive($user)
	{
		$user = self::getUser($user);

		// user is active if activation field is empty
		// there will be false positive as the activation field is also
		// used for password resets. It means we'll be displaying the icon to
		// resend email, while the user is already activated.
		// but this allow sending activation email to users who have not yet activated
		// and attempted to reset their password - which is very common
		$alreadyActive = empty($user->activation);

		return $alreadyActive;
	}

	/**
	 * Send activation email to a provided user.
	 *
	 * @param JUser $user
	 */
	private static function sendEmail($user)
	{
		$config = JFactory::getConfig();

		// load com_users language strings
		$jlang = JFactory::getLanguage();
		$jlang->load('com_users', JPATH_ROOT, 'en-GB', true); // Load English (British)
		$jlang->load('com_users', JPATH_ROOT, $jlang->getDefault(), true); // Load the site's default language
		$jlang->load('com_users', JPATH_ROOT, null, true); // Load the currently selected language

		// build message
		$activationLink = str_replace('/administrator', '', JUri::root()) . 'index.php?option=com_users&task=registration.activate&token=' . $user->activation;

		$emailSubject = JText::sprintf(
			'COM_USERS_EMAIL_ACCOUNT_DETAILS',
			$user->name,
			$config->get('sitename')
		);

		$emailBody = JText::sprintf(
			'COM_USERS_EMAIL_REGISTERED_WITH_ACTIVATION_BODY_NOPW',
			$user->name,
			$config->get('sitename'),
			$activationLink,
			JUri::root(),
			$user->username
		);

		// send it
		$sent = JFactory::getMailer()->sendMail($config->get('mailfrom'), $config->get('fromname'), $user->email, $emailSubject, $emailBody);

		return $sent;
	}

	/**
	 * Decides if we should send an activation email for this users.
	 * Conditions are that Joomla is set to activate accounts
	 * and user is not activated yet
	 *
	 * @param int | object $user
	 * @return bool
	 */
	public static function shouldSendActivationEmail($user)
	{
		return self::isSetToActivate() && !self::alreadyActive($user);
	}

	/**
	 * Is joomla set to activate user accounts?
	 *
	 * @return bool
	 */
	private static function isSetToActivate()
	{
		$userParams = JComponentHelper::getParams('com_users');
		$userActivation = $userParams->get('useractivation');
		return $userActivation == 1;
	}
}