Advertisement
Guest User

User Plugin Joomla

a guest
Jan 19th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.10 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * System Plugin for Joomla! - Title change
  5.  *
  6.  */
  7. defined('_JEXEC') or die;
  8.  
  9. jimport('joomla.plugin.plugin');
  10.  
  11. use Joomla\Registry\Registry;
  12.  
  13. /**
  14.  * Class PlgSystemTitlechange
  15.  *
  16.  * @since  1.0.0
  17.  */
  18. class PlgUserPromoterdaten extends JPlugin
  19. {
  20.     /**
  21.      * Application object
  22.      *
  23.      * @var    JApplicationCms
  24.      * @since  3.2
  25.      */
  26.     protected $app;
  27.  
  28.     /**
  29.      * Database object
  30.      *
  31.      * @var    JDatabaseDriver
  32.      * @since  3.2
  33.      */
  34.     protected $db;
  35.  
  36.     /**
  37.      * Constructor
  38.      *
  39.      * @param  object  &$subject  Instance of JEventDispatcher
  40.      * @param  array   $config    Configuration
  41.      *
  42.      * @since  1.0.0
  43.      */
  44.     public function __construct(&$subject, $config)
  45.     {
  46.         parent::__construct($subject, $config);
  47.     }
  48.  
  49.     /**
  50.      * Utility method to act on a user after it has been saved.
  51.      *
  52.      * This method sends a registration email to new users created in the backend.
  53.      *
  54.      * @param   array    $user     Holds the new user data.
  55.      * @param   boolean  $isnew    True if a new user is stored.
  56.      * @param   boolean  $success  True if user was successfully stored in the database.
  57.      * @param   string   $msg      Message.
  58.      *
  59.      * @return  void
  60.      *
  61.      * @since   1.6
  62.      */
  63.     public function onUserAfterSave($user, $isnew, $success, $msg)
  64.     {
  65.         $data = JFactory::getApplication()->input->post->get('jform', array(), 'array');
  66.         var_dump($data);
  67.         var_dump($user);
  68.         var_dump($isnew);
  69.         var_dump($success);
  70.         var_dump($msg);
  71.         die;
  72.     }
  73.  
  74.     public function onContentPrepareForm($form, $data)
  75.     {
  76.         if (!($form instanceof JForm))
  77.         {
  78.             $this->_subject->setError('JERROR_NOT_A_FORM');
  79.             return false;
  80.         }
  81.  
  82.         // Check we are manipulating a valid form,
  83.         // may also want to check whether this is frontend or admin depending on where all you want to affect
  84.         // JFactory::getApplication()->isAdmin()
  85.         $name = $form->getName();
  86.         if (!in_array($name, array('com_admin.profile', 'com_users.user', 'com_users.profile', 'com_users.registration')))
  87.         {
  88.             return true;
  89.         }
  90.  
  91.         // remove fields on frontend
  92.         if (!JFactory::getApplication()->isAdmin())
  93.         {
  94. //          $form->removeField('username');
  95.             $form->removeField('password1');
  96.             $form->removeField('password2');
  97.             $form->removeField('email2');
  98.         }
  99.  
  100.         return true;
  101.     }
  102.    
  103.     //onUserBeforeSave
  104.     public function onUserBeforeSave($user, $isnew, $new)
  105.     {
  106.         $data = JFactory::getApplication()->input->post->get('jform', array(), 'array');
  107.         $data['username'] = $data['email1'];
  108.         $data['email2'] = $data['email1'];
  109.         JFactory::getApplication()->input->post->set('jform', $data);
  110.        
  111.         return $user = &$data;
  112.     }
  113.    
  114.     /**
  115.      * This method should handle any login logic and report back to the subject
  116.      *
  117.      * @param   array  $user     Holds the user data
  118.      * @param   array  $options  Array holding options (remember, autoregister, group)
  119.      *
  120.      * @return  boolean  True on success
  121.      *
  122.      * @since   1.5
  123.      */
  124.     public function onUserLogin($user, $options = array())
  125.     {
  126.         $instance = $this->_getUser($user, $options);
  127.  
  128.         // If _getUser returned an error, then pass it back.
  129.         if ($instance instanceof Exception)
  130.         {
  131.             return false;
  132.         }
  133.  
  134.         // If the user is blocked, redirect with an error
  135.         if ($instance->block == 1)
  136.         {
  137.             $this->app->enqueueMessage(JText::_('JERROR_NOLOGIN_BLOCKED'), 'warning');
  138.  
  139.             return false;
  140.         }
  141.  
  142.         // Authorise the user based on the group information
  143.         if (!isset($options['group']))
  144.         {
  145.             $options['group'] = 'USERS';
  146.         }
  147.  
  148.         // Check the user can login.
  149.         $result = $instance->authorise($options['action']);
  150.  
  151.         if (!$result)
  152.         {
  153.             $this->app->enqueueMessage(JText::_('JERROR_LOGIN_DENIED'), 'warning');
  154.  
  155.             return false;
  156.         }
  157.  
  158.         // Mark the user as logged in
  159.         $instance->guest = 0;
  160.  
  161.         $session = JFactory::getSession();
  162.  
  163.         // Grab the current session ID
  164.         $oldSessionId = $session->getId();
  165.  
  166.         // Fork the session
  167.         $session->fork();
  168.  
  169.         $session->set('user', $instance);
  170.  
  171.         // Ensure the new session's metadata is written to the database
  172.         $this->app->checkSession();
  173.  
  174.         // Purge the old session
  175.         $query = $this->db->getQuery(true)
  176.             ->delete('#__session')
  177.             ->where($this->db->quoteName('session_id') . ' = ' . $this->db->quote($oldSessionId));
  178.  
  179.         try
  180.         {
  181.             $this->db->setQuery($query)->execute();
  182.         }
  183.         catch (RuntimeException $e)
  184.         {
  185.             // The old session is already invalidated, don't let this block logging in
  186.         }
  187.  
  188.         // Hit the user last visit field
  189.         $instance->setLastVisit();
  190.  
  191.         // Add "user state" cookie used for reverse caching proxies like Varnish, Nginx etc.
  192.         $conf          = JFactory::getConfig();
  193.         $cookie_domain = $conf->get('cookie_domain', '');
  194.         $cookie_path   = $conf->get('cookie_path', '/');
  195.  
  196.         if ($this->app->isSite())
  197.         {
  198.             $this->app->input->cookie->set("joomla_user_state", "logged_in", 0, $cookie_path, $cookie_domain, 0);
  199.         }
  200.  
  201.         return true;
  202.     }
  203.  
  204.     /**
  205.      * This method should handle any logout logic and report back to the subject
  206.      *
  207.      * @param   array  $user     Holds the user data.
  208.      * @param   array  $options  Array holding options (client, ...).
  209.      *
  210.      * @return  bool  True on success
  211.      *
  212.      * @since   1.5
  213.      */
  214.     public function onUserLogout($user, $options = array())
  215.     {
  216.         $my      = JFactory::getUser();
  217.         $session = JFactory::getSession();
  218.  
  219.         // Make sure we're a valid user first
  220.         if ($user['id'] == 0 && !$my->get('tmp_user'))
  221.         {
  222.             return true;
  223.         }
  224.  
  225.         // Check to see if we're deleting the current session
  226.         if ($my->id == $user['id'] && $options['clientid'] == $this->app->getClientId())
  227.         {
  228.             // Hit the user last visit field
  229.             $my->setLastVisit();
  230.  
  231.             // Destroy the php session for this user
  232.             $session->destroy();
  233.         }
  234.  
  235.         // Enable / Disable Forcing logout all users with same userid
  236.         $forceLogout = $this->params->get('forceLogout', 1);
  237.  
  238.         if ($forceLogout)
  239.         {
  240.             $query = $this->db->getQuery(true)
  241.                 ->delete($this->db->quoteName('#__session'))
  242.                 ->where($this->db->quoteName('userid') . ' = ' . (int) $user['id'])
  243.                 ->where($this->db->quoteName('client_id') . ' = ' . (int) $options['clientid']);
  244.  
  245.             try
  246.             {
  247.                 $this->db->setQuery($query)->execute();
  248.             }
  249.             catch (RuntimeException $e)
  250.             {
  251.                 return false;
  252.             }
  253.         }
  254.  
  255.         // Delete "user state" cookie used for reverse caching proxies like Varnish, Nginx etc.
  256.         $conf          = JFactory::getConfig();
  257.         $cookie_domain = $conf->get('cookie_domain', '');
  258.         $cookie_path   = $conf->get('cookie_path', '/');
  259.  
  260.         if ($this->app->isSite())
  261.         {
  262.             $this->app->input->cookie->set("joomla_user_state", "", time() - 86400, $cookie_path, $cookie_domain, 0);
  263.         }
  264.  
  265.         return true;
  266.     }
  267.  
  268.     /**
  269.      * This method will return a user object
  270.      *
  271.      * If options['autoregister'] is true, if the user doesn't exist yet they will be created
  272.      *
  273.      * @param   array  $user     Holds the user data.
  274.      * @param   array  $options  Array holding options (remember, autoregister, group).
  275.      *
  276.      * @return  JUser
  277.      *
  278.      * @since   1.5
  279.      */
  280.     protected function _getUser($user, $options = array())
  281.     {
  282.         $instance = JUser::getInstance();
  283.         $id = (int) JUserHelper::getUserId($user['username']);
  284.  
  285.         if ($id)
  286.         {
  287.             $instance->load($id);
  288.  
  289.             return $instance;
  290.         }
  291.  
  292.         // TODO : move this out of the plugin
  293.         $config = JComponentHelper::getParams('com_users');
  294.  
  295.         // Hard coded default to match the default value from com_users.
  296.         $defaultUserGroup = $config->get('new_usertype', 2);
  297.  
  298.         $instance->id = 0;
  299.         $instance->name = $user['fullname'];
  300.         $instance->username = $user['username'];
  301.         $instance->password_clear = $user['password_clear'];
  302.  
  303.         // Result should contain an email (check).
  304.         $instance->email = $user['email'];
  305.         $instance->groups = array($defaultUserGroup);
  306.  
  307.         // If autoregister is set let's register the user
  308.         $autoregister = isset($options['autoregister']) ? $options['autoregister'] : $this->params->get('autoregister', 1);
  309.  
  310.         if ($autoregister)
  311.         {
  312.             if (!$instance->save())
  313.             {
  314.                 JLog::add('Error in autoregistration for user ' . $user['username'] . '.', JLog::WARNING, 'error');
  315.             }
  316.         }
  317.         else
  318.         {
  319.             // No existing user and autoregister off, this is a temporary user.
  320.             $instance->set('tmp_user', true);
  321.         }
  322.  
  323.         return $instance;
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement