<?php
/**
 * @package    VP Advanced User
 * @author     Abhishek Das <info@virtueplanet.com>
 * @copyright  Copyright (C) 2012-2019 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @link       https://www.virtueplanet.com
 */

defined('_JEXEC') or die;

defined('JPATH_USERS_ADMIN')        or define('JPATH_USERS_ADMIN',        JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_users');
defined('JPATH_USERS_SITE')         or define('JPATH_USERS_SITE',         JPATH_SITE . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_users');
defined('JPATH_ADVANCEDUSER_ADMIN') or define('JPATH_ADVANCEDUSER_ADMIN', JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_vpadvanceduser');
defined('JPATH_ADVANCEDUSER_SITE')  or define('JPATH_ADVANCEDUSER_SITE',  JPATH_SITE . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_vpadvanceduser');

if(!defined('JPATH_ADVANCEDUSER_VERSION'))
{
	$manifest = JPATH_ADVANCEDUSER_ADMIN . '/vpadvanceduser.xml';
	$version  = 'unknown';
	
	if(file_exists($manifest))
	{
		$xml = JFactory::getXML($manifest);
		$version = (string) $xml->version;
	}
	
	define('JPATH_ADVANCEDUSER_VERSION', $version);
}

if(!function_exists('vpdump'))
{
	/**
	* Method to print any data with debug information.
	* Used in debuggin purpose. Similar to vardump but more advanced.
	* 
	* @param mixed $data Any type of data but form xml object
	* 
	* @return void
	*/
	function vpdump($data, $debug = true)
	{
		ob_start();
		print_r($data);
		$str = ob_get_contents();
		ob_end_clean();

		echo '<pre class="vpdump" dir="ltr" style="font-size: 14px;">';
		echo '<small>' . gettype($data) . '</small> ';
		echo '<font color="#cc0000">' . $str . '</font>';
		echo '<i>{Length: ' . strlen($str) . '}</i>';
		
		if($debug)
		{
			$backTraces = debug_backtrace();
			
			echo '<ul style="margin:15px 0; padding:15px; list-style-position:inside; background: #FAFAFA; border: 1px solid #DDD; line-height: 1.4;">';
			
			foreach($backTraces as $backTrace)
			{
				echo '<li>';
				echo '<b>File:</b> ' . $backTrace['file'] . '  <b>Line:</b> ' . $backTrace['line'] . (isset($backTrace['class']) ? '  <b>Class:</b> ' . $backTrace['class'] : '') . (isset($backTrace['class']) ? '  <b>Function:</b> ' . $backTrace['function'] : '');
				echo '</li>';
			}
			
			echo '<li><b>Current Memory Usage:</b> ' . vpCalculateSize(memory_get_usage()) . '</li>';
			echo '<li><b>Peak Memory Usage:</b> ' . vpCalculateSize(memory_get_peak_usage()) . '</li>';
			
			echo '</ul>';
		}
		
		echo '<br/>';
		echo '</pre>';
	}
}

if(!function_exists('vpCalculateSize'))
{
	function vpCalculateSize($mem_usage)
	{
		if ($mem_usage < 1024) 
			return $mem_usage.' bytes'; 
		elseif ($mem_usage < 1048576) 
			return round($mem_usage/1024, 2).' KB'; 
		else 
			return round($mem_usage/1048576, 2).' MB';
	}
}