<?php

/*********************************************************************************************
CartPlog Lite License
==============================================================================================
This software is as-is, no warranty of any kind, use at your own risk. You are granted free
use of this software. However, you are not permitted under any circumstances to redistribute
the software, whether in whole or in part. Also, the 'powered by' links must remain intact
and visible. Make sure to understand that this is *not GPL software. Do not redistribute it,
and do not use parts of it in other software. This software is copyrighted in its entirety
to 'calorie' a/k/a © ThinkDing LLC 2010, all rights reserved. The software author/entity are
in no way responsible for your site content. All copyright notices and 'powered by' links
must not be changed or removed. Do not use this software if obtained from a warez site!
*********************************************************************************************/

if (!defined('VB_AREA') || !defined('DIR'))
{
	exit();
}

//	$example_usage = cartplog_fetch_stylevar('imgdir_misc');
//	eval(cartplog_fetch_template('cartplog_shell_blank', 'cartplog[\'output\']', false));
//	print_output($cartplog['output']);
//	if (class_exists('vB_Template'))
//	{
//		vB_Template::preRegister('navbar', array('cartplog' => $cartplog));
//	}

// wrapper function for backwards compatibility with vB3
function cartplog_fetch_template($name, $variable = 'final_rendered', $concat = false)
{
	// check to see what vB version we are dealing with
	if (class_exists('vB_Template'))
	{
		// make sure process_templates is run
		global $bootstrap;
		if (!empty($bootstrap) AND !$bootstrap->called('template'))
		{
			$bootstrap->process_templates();
		}

		global $vbulletin, $tempusagecache, $templateassoc;
		if (isset($vbulletin->templatecache["$name"]))
		{
			$template = $vbulletin->templatecache["$name"];
		}
		else
		{
			vB_Template::$template_queries[$name] = true;
			$fetch_tid = intval($templateassoc["$name"]);
			if (!$fetch_tid)
			{
				$gettemp = array('template' => '');
			}
			else
			{
				$gettemp = $vbulletin->db->query_first_slave("
					SELECT template
					FROM " . TABLE_PREFIX . "template
					WHERE templateid = $fetch_tid
				");
			}
			$template = $gettemp['template'];
			$vbulletin->templatecache["$name"] = $template;
		}

		if (!isset(vB_Template::$template_usage[$name]))
		{
			vB_Template::$template_usage[$name] = 1;
		}
		else
		{
			vB_Template::$template_usage[$name] ++;
		}

		$find = array(
			'$vboptions', '$bbuserinfo', '$session', '$relpath'
		);
		$replace = array(
			'$vbulletin->options', '$vbulletin->userinfo', '$vbulletin->session->vars', 'htmlspecialchars($vbulletin->input->fetch_relpath())'
		);
		$template = str_replace($find, $replace, $template);

		if ($vbulletin->options['addtemplatename'] AND !$suppress_html_comments)
		{
			$template_name = preg_replace('#[^a-z0-9_]#i', '', $name);
			$template = str_replace('$final_rendered = \'', '$final_rendered = \'<!-- BEGIN TEMPLATE: '.$template_name.' -->\'; $final_rendered .= \'', $template);
			$template .= '$final_rendered .= \'<!-- END TEMPLATE: '.$template_name.' -->\';';
		}

		$find = array('$final_rendered = \'', '$final_rendered .= \'');
		($concat == false ? $replace = array('$'.$variable.' = \'', '$'.$variable.' .= \'') : $replace = '$'.$variable.' .= \'');
		$template = str_replace($find, $replace, $template);
	}
	else
	{
		// we are on vB3
		$template = ($concat == false ? '$'.$variable.' = "' : '$'.$variable.' .= "') . fetch_template($name) . '";';
	}

	return $template;
}

// wrapper function for backwards compatibility with vB3
function cartplog_fetch_stylevar($name)
{
	// check to see what vB version we are dealing with
	if (class_exists('vB_Template'))
	{
		// vB_Template class means we are on vB4
		return vB_Template_Runtime::fetchStyleVar($name);
	}
	else
	{
		// we are on vB3
		return $GLOBALS['stylevar']["$name"];
	}
}

// vB4 backwards compatibility
$final_rendered = $vbcsspath = '';
if (class_exists('vB_Template'))
{
	if ($vbulletin->options['storecssasfile'])
	{
		$vbcsspath = 'clientscript/vbulletin_css/style' . str_pad($style['styleid'], 5, '0', STR_PAD_LEFT) . $vbulletin->stylevars['textdirection']['string'][0] . '/';
	}
	else
	{
		// textdirection var added to prevent cache for bug #32640
		$vbcsspath = 'css.php?styleid=' . $style['styleid'] . '&amp;langid=' . LANGUAGEID . '&amp;d=' . $style['dateline'] . '&amp;td=' . $vbulletin->stylevars['textdirection']['string'] . '&amp;sheet=';
	}
}

?>