<?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\Cache\Cache;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\Registry\Registry;

defined( '_JEXEC' ) or die( 'Restricted access' );


/** Define some constants that can be used by the system **/
if( !defined( 'AZRUL_SYSTEM_PATH' ) )
{
	// Get the real system path.
	$system	= rtrim(  dirname( __FILE__ ) , '/' );

	define( 'AZRUL_SYSTEM_PATH' , $system );
}


$helper	= AZRUL_SYSTEM_PATH . '/pc_includes/helper.php';

// Test if file exists before trying to include and generate errors on the entire site.
if( file_exists( $helper ) )
{
	include_once( $helper );
}
else
{
	// If file doesn't exists, just quit it now.
	return;
}

if( !defined( 'AZRUL_SYSTEM_LIVE' ) )
{
	if(basename(dirname(dirname(__FILE__))) == 'plugins'){
	//Joomla 1.5 and above
        define( 'AZRUL_SYSTEM_LIVE' , Uri::root(true)  . '/plugins/system' );
    } else {
        define( 'AZRUL_SYSTEM_LIVE' , Uri::root(true)  . '/plugins/system/jomsocial.system' );
	}
}

if( !defined( 'AZRUL_BASE_LIVE' ) )
{
	define( 'AZRUL_BASE_LIVE' , rtrim( Uri::base() , '/' ) );
}

/**
 * Register the respective events
 **/

$mainframe = Factory::getApplication();
$mainframe->registerEvent( 'onAfterRoute' , 'azrulSysBot' );
$mainframe->registerEvent( 'onAfterRender', 'azrulOnAfterRender' );
$mainframe->registerEvent( 'onAfterInitialise', 'azrulOnAfterInitialise' );

// Include the template file as Jom Comment and My Blog needs this.
include_once( AZRUL_SYSTEM_PATH .'/pc_includes/template.php');

/**
 * Display required javascript codes for AJAX function calls
 **/
function azrulSysBot()
{
	static	$added	= false;
    $jinput = Factory::getApplication()->input;
	if( !$added )
	{
		$format		= $jinput->getWord( 'format' , 'html' );

		if( $format == 'pdf' )
		{
			return;
		}

		// Include ajax file
		include_once( AZRUL_SYSTEM_PATH . '/pc_includes/ajax.php' );

		$jax	= new JAX( AZRUL_SYSTEM_LIVE . '/pc_includes' );
		$jax->setReqURI( AZRUL_BASE_LIVE . '/index.php' );
		$jax->process();

		$noHTML	= $jinput->getInt( 'no_html' , 0 );

		if( !$noHTML && $format == 'html' )
		{
			$document = Factory::getDocument();
			if ($document->getType() == 'html') {
				$document->addCustomTag( $jax->getScript() );
			}
		}
		$added	= true;
	}
}

/**
 * We need to refresh the jax token.
 */
function azrulOnAfterRender()
{
	// We only need to do this replacement if cache is enabled
	// otherwise, just return. Faster, saves memory
	$app = Factory::getApplication();
	//$config = Factory::getConfig();
	//if( !$config->get('caching') )

	if( !Factory::getConfig()->get('caching') )
	{
		return;
	}

	if ($app->getName() != 'site') {
		return true;
	}

	$session	= Factory::getSession();
	$token		= $session->getFormToken();

	$buffer = $app->getBody();
	$tokenStr = 'var jax_token_var=\'' . $token . '\';';

	$regex =	'#var jax_token_var=\'([A-Za-z0-9-_]+)\';#m';
	$buffer	= preg_replace($regex, $tokenStr, $buffer);
	azrulCheckBuffer($buffer);

	$app->setBody($buffer);
	return true;
}

/**
 * We need to refresh the jax token in case System Cache plugin is enable
 */

function azrulOnAfterInitialise(){
	$db = Factory::getDbo();
	$db->setQuery("SET @@SESSION.sql_mode =''")->execute();

    $jinput = Factory::getApplication()->input;

	// If this is ajax call, no need to process this
	$task = $jinput->getWord( 'task' , '' );
	if($task == 'azrul_ajax'){
		return true;
	}

	//Fix the registration issue when the System Cache plugin is enable
	$app = Factory::getApplication();
	if ($app->getName() != 'site') {
		return true;
	}


	//We only do the replacement for guest users
	$user	= Factory::getUser();
	if ($user->get('guest') && $_SERVER['REQUEST_METHOD'] == 'GET') {
		//$config = Factory::getConfig();
		$cache_plg = PluginHelper::getPlugin('system','cache');
		if(!is_object($cache_plg)){
			//cache plugin is disable, no need futher processing
			return;
		}
		jimport('joomla.html.parameter');
		$params = new Registry($cache_plg->params);
		$options = array(
			'cachebase' 	=> JPATH_BASE.'/cache',
			'defaultgroup' 	=> 'page',
			'lifetime' 		=> $params->get('cachetime', 15) * 60,
			'browsercache'	=> false,
			'caching'		=> true,
			'language'		=> Factory::getConfig()->get('config.language', 'en-GB') //$config->getValue('config.language', 'en-GB')
		);
		jimport('joomla.cache.cache');
		$cache = Cache::getInstance('page', $options);

		$data  = $cache->get();
		//Only replace if the page is cached before
		if ($data !== false)
		{
			// the following code searches for a token in the cached page and replaces it with the
			// proper token.

			$session	= Factory::getSession();
			$token		= $session->getFormToken(false);

			$search = '#<input type="hidden" name="[0-9a-f]{32}" value="1" />#';
			$replacement = '<input type="hidden" name="'.$token.'" value="1" />';
			$data = preg_replace( $search, $replacement, $data );

			$tokenStr = 'var jax_token_var=\'' . $token . '\';';

			$regex =	'#var jax_token_var=\'([A-Za-z0-9-_]+)\';#m';
			$data	= preg_replace($regex, $tokenStr, $data);

			JResponse::setBody($data);

			echo JResponse::toString(Factory::getConfig()->get('gzip'));

			$app->close();
		}
	}
	return true;
}


function azrulCheckBuffer($buffer) {
    if ($buffer === null) {
        switch (preg_last_error()) {
        case PREG_BACKTRACK_LIMIT_ERROR:
            $message = "PHP regular expression limit reached (pcre.backtrack_limit)";
            break;
        case PREG_RECURSION_LIMIT_ERROR:
            $message = "PHP regular expression limit reached (pcre.recursion_limit)";
            break;
        case PREG_BAD_UTF8_ERROR:
            $message = "Bad UTF8 passed to PCRE function";
            break;
        default:
            $message = "Unknown PCRE error calling PCRE function";
        }

		throw new Exception($message);
    }
}