<?php

/**
 * @copyright (C) 2013 iJoomla, Inc. - All rights reserved.
 * @license GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html)
 * @author iJoomla.com <webmaster@ijoomla.com>
 * @url https://www.jomsocial.com/license-agreement
 * The PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript *are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0
 * More info at https://www.jomsocial.com/license-agreement
 */

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;

// no direct access
defined('_JEXEC') or die('Restricted access');

/**
 * Class exists checking
 */
if (!class_exists('plgSystemjomsocialredirect')) {

    /**
     * Plugin entrypoint
     * @link http://docs.joomla.org/Plugin/Events/System
     */
    class plgSystemjomsocialredirect extends CMSPlugin {

        /**
         * Construct method
         * @param type $subject
         * @param type $config
         */
        function __construct($subject, $config) {
            parent::__construct($subject, $config);
            $this->loadLanguage();
            if (Factory::getApplication()->isClient('site')) {
                include_once( JPATH_ROOT . '/components/com_community/libraries/core.php' );
            }
        }

        /**
         * This event is triggered after the framework has loaded and the application initialise method has been called.
         */
        public function onAfterRoute() {
            $jinput = Factory::getApplication()->input;
            if (Factory::getApplication()->isClient('site')) {
                $option = $jinput->get('option');
                if ($option == 'com_user' || $option == 'com_users') {
                    $this->overrideComUserRegistration();
                    $this->overrideRedirectLoginLogout();
                }
            }
        }

        /**
         * New event to be triged when FB user login
         * @return string
         */
        public function onAfterFBUserLogin() {
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            /* override redirect after login / logout */
            $task = $jinput->get('task');

            $link = $this->getMenuLink($this->params->get('redirect_login', 1));
            
            if ($this->params->get('redirect_previous') == 1) {
                $session = Factory::getSession();
                $session->set('redirect_previous_url', $_SERVER['HTTP_REFERER']);
                $link = $session->get('redirect_previous_url', $link);
            }

            return $link;
        }

        /**
         * Method to override Login / Logout redirect
         */
        private function overrideRedirectLoginLogout() {
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            /* override redirect after login / logout */
            $task = $jinput->get('task');
            $return = $jinput->get('return', '');

            if ($this->params->get('redirect_previous') == 1) {
                /* in com_user we need to save reference url for these tasks to know previous page */
                if ($task === 'user.login' || $task === 'login' || $task === 'user.logout' || $task === 'logout') {
                    $session = Factory::getSession();
                    $session->set('redirect_previous_url', $_SERVER['HTTP_REFERER']);
                }
            }

            switch ($task) {
                case 'user.login' : //Joomla 1.6 and later
                case 'login' : /* on logging */
                    if ($this->login()) { /* we do login by self */
                        /* redirect if login success */

                        $link = $this->getMenuLink($this->params->get('redirect_login', 1));
                        
                        if (preg_match("/index.php\?Itemid=1/", $link)) {
                            $link = CRoute::_('index.php?option=com_community&view=frontpage', false);
                        }
                        
                        $app = Factory::getApplication(); 
                        $returnUrl = base64_decode($app->input->getBase64('return'));

                        if(strpos($returnUrl, 'ajaxlogin') !== false) {
                           $link = $returnUrl;
                        } else {
                            if ($this->params->get('redirect_previous') == 1) {
                                $session = Factory::getSession();
                                $link = $session->get('redirect_previous_url', $link);
                            }
                        }

                        if(count($mainframe->getMessageQueue()) == 0){
                            if ($this->params->get('redirect_login_message_show', 1)) {
                                $mainframe->enqueueMessage(Text::_($this->params->get('redirect_login_msg', 'LOGIN_SUCCESSFUL')), 'message');
                                $mainframe->redirect($link);
                            } else {
                                $mainframe->redirect($link);
                            }
                        }
                    } else {
                        /* redirect if login failed */
                        $link = $this->getMenuLink($this->params->get('redirect_login_failed', 1));

                        if (preg_match("/index.php\?Itemid=1/", $link)) {
                            $link = CRoute::_('index.php?option=com_community&view=frontpage', false);
                        }

                        if ($this->params->get('redirect_previous') == 1) {
                            $session = Factory::getSession();
                            $link = $session->get('redirect_previous_url', $link);
                        }

                        if ($this->params->get('redirect_login_failed_message_show', 1)) {
                            $mainframe->enqueueMessage(Text::_($this->params->get('redirect_login_failed_msg', 'LOGIN_FAILED')), 'notice');
                            $mainframe->redirect($link);
                        } else {
                            $mainframe->redirect($link);
                        }
                    }
                    break;
                case 'user.logout' : //Joomla 1.6 and later
                case 'logout' :
                    $link = $this->getMenuLink($this->params->get('redirect_logout', 1));

                    if (preg_match("/index.php\?Itemid=1/", $link)) {
                        $link = CRoute::_('index.php?option=com_community&view=frontpage', false);
                    }
                        
                    if ($this->params->get('redirect_previous') == 1) {
                        $session = Factory::getSession();
                        $link = $session->get('redirect_previous_url', $link);
                    }
                    
                    Factory::getApplication()->logout();
                    
                    // deprecated 
                    // if ($this->params->get('redirect_logout_message_show', 1)) {
                    //     $mainframe->redirect($link, JText::_($this->params->get('redirect_logout_msg', 'YOU_HAVE_LOGGED_OUT')), 'message');
                    // } else {
                    //     $mainframe->redirect($link);
                    // }

                    $mainframe->redirect($link);

                    break;

                default :

                    break;
            }
        }

        /**
         * Method to redirect com_user registration to JomSocial registration
         */
        private function overrideComUserRegistration() {
            $mainframe = Factory::getApplication();
            $jinput = $mainframe->input;
            $view = $jinput->get('view');
            /* override Joomla com_user registration by JomSocial registration */
            if ($view == 'register' || $view == 'registration') {
                /* index.php?option=com_user&view=register */
                if ($this->params->get('override_com_user_registration', 1) == 1) {
                    $jsRegistrationLink = 'index.php?option=com_community&view=register';
                    $jsRegistrationLink = CRoute::_($jsRegistrationLink);
                    //Factory::getApplication ()->enqueueMessage ( $jsRegistrationLink );

                    if ($this->params->get('redirect_register_message_show', 1)) {
                        $mainframe->enqueueMessage(Text::_($this->params->get('redirect_registration_msg', 'REDIRECTED_TO_COMMUNITY_REGISTRATION')), 'message');
                        $mainframe->redirect($jsRegistrationLink);
                    } else {
                        $mainframe->redirect($jsRegistrationLink);
                    }
                }
            }
        }

        /**
         * Method to login
         * This method will call Application login and force to return true / false
         * We will show message override authentication event
         * @return boolean
         */
        private function login() {
            /**
             * @param   array  $credentials  Array('username' => string, 'password' => string)
             * @param   array  $options      Array('remember' => boolean)
             */
            $jinput = Factory::getApplication()->input;
            $credentials ['username'] = $jinput->get('username',null,'username');
            $credentials ['password'] = $jinput->get('passwd',null,'string');
            $credentials ['secretkey'] = $jinput->get('secretkey',null,'string');
            if ($credentials ['password'] == '') {
                //try to detect for joomla 1.6, 17
                $credentials ['password'] = $jinput->get('password',null,'string');
            }
            $options ['silent'] = true; /* force turn true / false of login function instead error msg */
            $options ['remember'] = $jinput->get('remember');
            return Factory::getApplication()->login($credentials, $options);
        }

        /**
         * Private method to get JomSocial itemId
         * If have no itemId will return itemId of current page
         * @return int
         */
        private function getItemId() {
            $jsFrontpageLink = 'index.php?option=com_community&view=frontpage';
            $jinput = Factory::getApplication()->input;

            $db = Factory::getDBO();
            $query = ' SELECT id FROM #__menu WHERE link = ' . $db->quote($jsFrontpageLink) . ' AND published = 1 ';
            $db->setQuery($query);
            $itemId = $db->loadResult();
            if (!$itemId) {
                $itemId = $jinput->get('Itemid');
            }
            return $itemId;
        }

        /**
         * 
         * @param int $Itemid
         * @return string
         */
        private function getMenuLink($Itemid) {
            if ($Itemid == 1) {
                return Uri::root();
            }

            $db = Factory::getDBO();
            $query = ' SELECT menutype,link FROM #__menu WHERE id = ' . $db->quote($Itemid) . ' AND published = 1 ';
            $db->setQuery($query);
            $menu = $db->loadObject();
            if (!empty($menu)) {
                if ($menu->menutype == 'jomsocial') {
                    //for toolbar menu item
                    $url = CRoute::_($menu->link, false);
                } else {
                    $url = Route::_($menu->link . "&Itemid=" . $Itemid, false); // use JRoute to make link from object			
                }
            } else {
                $url = Route::_(Uri::root(), false);
            }
            return $url;
        }
    }
}