ACC SHELL

Path : /srv/www/vhosts/alfa-com/engine/
File Upload :
Current File : /srv/www/vhosts/alfa-com/engine/Template.php

<?php
	// Author: Jakub Macek, CZ; Copyright: Poski.com s.r.o.; Code is 100% my work. Do not copy.
	
	class Counters
	{
		public static $counters = array();
		
		public static function reset($id)
		{
			self::$counters[$id] = 0;
		}
		
		public static function set($id, $value)
		{
			self::$counters[$id] = $value;
		}
		
		public static function increase($id, $count = 1)
		{
			if (!isset(self::$counters[$id]))
				self::reset($id);
			self::$counters[$id] += $count;
		}
		
		public static function decrease($id, $count = 1)
		{
			self::increase($id, -$count);
		}
		
		public static function toClass($id, $min_mod = 2, $max_mod = 5, $template = 'i%d_of_%d')
		{
			if (!isset(self::$counters[$id]))
				self::reset($id);

			$result = array();
			$counter = self::$counters[$id];
			for ($mod = $min_mod; $mod <= $max_mod; $mod++)
			{
				$temp0 = $counter % $mod;
				if ($temp0 == 0)
					$temp0 = $mod;
				$result[] = sprintf($template, $temp0, $mod);
			}
			return implode(' ', $result);
		}
	}

	class Template
	{
		const TYPE_DEFAULT = 65536;
		const TYPE_DEBUG = 131072;
		
		const TYPE_HTML = 1;
		const TYPE_HTML_HEAD = 2;
		const TYPE_MAIN = 4;
		const TYPE_FRAME = 8;
		const TYPE_HEADER = 16;
		const TYPE_FOOTER = 32;
		const TYPE_BREADCRUMBS = 64;
		const TYPE_NAVIGATION = 128;
		const TYPE_MENU = 128; // = TYPE_NAVIGATION
		const TYPE_SUBNAVIGATION = 256;
		const TYPE_SUBMENU = 256; // = TYPE_SUBNAVIGATION
		const TYPE_TOOLBOX = 512;
		const TYPE_INVOCATION = 1024;
		
		public static			$overrides							= array();
		public static			$stack								= array();
		
		public					$id									= null;
		public					$type								= null;
		public					$engine								= null;
		public					$source								= null;
		public					$data								= null;
		//public				$directory							= null;

		public					$group								= null;
		public					$part								= null;
		public					$options							= array();

		public static function create($id, $data, $source = null, $engine = null, $type = null)
		{
			return new Template($id, $data, $source, $engine, $type);
		}

		public function __construct($id, $data, $source = null, $engine = null, $type = null)
		{
			$this->id = $id;
			$this->data = $data;
			$this->engine = $engine;
			$this->source = $source;
			$this->type = $type;
			//$this->directory = BASE;
			if (is_array($this->data))
			{
				$this->engine = 'php';
				$this->source = 'callback';
			}
		}

		public function set($key, $value = null)
		{
			if ($value === null)
				unset($this->options[$key]);
			else
				$this->options[$key] = $value;
		}

		public function get($key, $value = null)
		{
			if (isset($this->options[$key]))
				return $this->options[$key];
			else
				return $value;
		}
		
		public function render($_x_options = array(), $_x_try = false)
		{
			array_push(self::$stack, $this);
			
			if (($this->source === null) && ($this->engine === null))
				if (isset(self::$overrides[$this->data]))
				{
					$OUTPUT = self::$overrides[$this->data]->render($_x_options, $_x_try);
					array_pop(self::$stack);
					return $OUTPUT;
				}
			
			$this->options = $_x_options;

			if (($this->source === null) && ($this->engine === null))
			{
				$this->data = str_replace('{#locale#}', L, $this->data);
				$this->data = str_replace('#locale#', L, $this->data);
				if (($temp0 = count(self::$stack)) >= 2)
				{
					$group = self::$stack[$temp0 - 2]->group;
					$this->data = str_replace('::', ':'.$group.':', $this->data);
				}
				if (substr($this->data, 0, 1) == ':')
				{
					$prefix = 'templates/';
					$files = array();
					$temp = explode(':', substr($this->data, 1));
					$this->group = $temp[0];
					if (isset($temp[1]))
						$this->part = $temp[1];
					if ($this->part)
						$files[] = $this->group . '-' . $this->part;
					$files[] = $this->group;
					if (m($this->group) && (($parent_group = get_class(m($this->group))) != $this->group))
					{
						if ($this->part)
							$files[] = $parent_group . '-' . $this->part;
						$files[] = $parent_group;
					}
				}
				else
				{
					$prefix = 'web/';
					$files = array($this->data);
				}

				foreach (site()->base as $base)
					foreach ($files as $file)
						foreach (array('.php' => 'php', '.tpl' => 'smarty', '.html' => 'html') as $extension => $engine)
							if (is_file($base . $prefix . $file . $extension))
							{
								$this->source = 'file';
								$this->engine = $engine;
								$this->data = $prefix . $file . $extension;
								break 3;
							}
				if (($this->source === null) && ($this->engine === null) && $_x_try)
				{
					array_pop(self::$stack);
					return null;
				}
			}

			if ($this->source == 'file')
			{
				$this->data = str_replace('{#locale#}', L, $this->data);
				$temp = strrpos($this->data, '.');
				if ($temp === false)
					$temp = strlen($this->data);
				$ext = substr($this->data, $temp);
				$name = substr($this->data, 0, $temp);
			}

			$templateId = $this->group . ($this->part ? (':' . $this->part) : '');
			foreach (array('skip', 'cut_length', 'date_format', 'datetime_format', 'pieces', '__class') as $temp)
				if (!isset($this->options[$temp]))
					$this->options[$temp] = @$GLOBALS['settings']['templates'][$templateId][$temp];
					
			if (($this->engine != 'smarty') || (strpos($this->part, 'mail-') !== false) || (strpos($this->group, '@') !== false))
				$this->options['skip'] .= ';container;';
			if ($this->options['cut_length'] === null)
				$this->options['cut_length'] = 400;
			if ($this->options['date_format'] === null)
				$this->options['date_format'] = __('date-format', 'core');
			if ($this->options['datetime_format'] === null)
				$this->options['datetime_format'] = __('datetime-format', 'core');
			$counter = (int) @$GLOBALS['settings']['templates'][$templateId]['counter'];
			$counter++;
			$GLOBALS['settings']['templates'][$templateId]['counter'] = $counter;
			Counters::set($templateId, $counter);

			$OUTPUT = '';

			if (strpos($this->options['skip'], ';container;') === false)
			{
				$temp1 = str_replace(array('-', ':'), array('_', ''), $this->group);
				$temp2 = str_replace(array('-', '@'), array('_', '__'), $this->part);
				$baseclass = $temp1 . '_' . $temp2 . '* ' . $temp1 . '* ' . $temp2 . '*';
				$class = '';
				if ($this->part)
					$class .= str_replace('*', '', $baseclass);
				if ($temp = @$GLOBALS['settings']['templates'][$templateId]['counter-divisor'])
					if (($counter % $temp) == 0)
						$class .= ' ' . str_replace('*', '_division', $baseclass);
				/*else
					$class = 'page' . str_replace(array('/', '-'), array('_', '_'), page()->location);*/
						
				$class .= ' ' . Counters::toClass($templateId);
				
				if ($this->options['__class'])
					$class .= ' ' . $this->options['__class'];
				$OUTPUT .= '<div class="' . $class . '">';
			}

			switch ($this->engine)
			{
				case 'html':
					switch ($this->source)
					{
						case 'string':
							$OUTPUT .= $this->data;
							break;
						case 'file':
							$f = U::fef(/*$this->directory . */$name . '.override' . $ext);
							if (!$f)
								$f = U::fef(/*$this->directory . */$name . $ext);
							if (!$f)
								error("template '{$this->data}' not found");
							else
								$OUTPUT .= file_get_contents($f);
							break;
					}
					break;
				case 'php':
					switch ($this->source)
					{
						case 'string':
							ob_start();
							extract($this->options, EXTR_SKIP);
							$OUTPUT .= eval($this->data);
							$OUTPUT .= ob_get_clean();
							break;
						case 'callback':
							ob_start();
							$OUTPUT .= callback($this->data, array('template' => $this));
							$OUTPUT .= ob_get_clean();
							break;
						case 'file':
							if (substr($name, 0, 1) == ':')
							{
								$name = 'templates/' . strtr(substr($name, 1), ':', '-');
								$ext = '.php';
							}
							$f = U::fef(/*$this->directory . */$name . '.override' . $ext);
							if (!$f)
								$f = U::fef(/*$this->directory . */$name . $ext);
							if (!$f)
								error("template '{$this->data}' / '{$name}' not found");
							else
							{
								ob_start();
								extract($this->options, EXTR_SKIP);
								include($f);
								$OUTPUT .= ob_get_clean();
							}
							break;
					}
					break;
				case 'dwoo':
					require_once('dwoo/dwooAutoload.php');
					require_once('DwooEx.php');
					$template = new DwooEx();
					$this->options['this'] = $this;

					$templateInner = null;
					switch ($this->source)
					{
						case 'string':
							$templateInner = new Dwoo_Template_String($this->data);
							break;
						case 'file':
							$cacheTime = null;
							$cacheId = null;
							/*$cache = Cache::tget($this->data);
							if ($cache)
							{
								$template->caching = 1;
								$cacheTime = $cache['ttl'];
								$cacheId = '~|' . L . '|' . $this->data . '|' . $cache['id']->evaluate(array('template' => $this, 'dwoo' => $template));
							}*/
							$includePath = array();
							foreach (site()->base as $base)
								$includePath[] = $base;
							$templateInner = new Dwoo_Template_FileEx($this->data, $cacheTime, $cacheId, null, $includePath);
							break;
					}
					if ($templateInner)
					{
						if (DEV)
							$templateInner->forceCompilation();
						$OUTPUT .= $template->get($templateInner, $this->options);
					}
					break;					
				case 'smarty':
					require_once('Smarty/Smarty.class.php');
					require_once('SmartyEx.php');
					$template = new SmartyEx();
					foreach (array_keys($this->options) as $k)
						$template->assign_by_ref($k, $this->options[$k]);
					$template->assign_by_ref('this', $this);

					switch ($this->source)
					{
						case 'string':
							$OUTPUT .= $template->fetch('string:' . $this->data);
							break;
						case 'file':
							$cache = Cache::tget($this->data);
							$cache_id = null;
							$is_cached = false;
							if ($cache)
							{
								$template->caching = 1;
								$template->cache_lifetime = $cache['ttl'];
								$cache_id = '~|' . L . '|' . $this->data . '|' . $cache['id']->evaluate(array('template' => $this, 'smarty' => $template));
								$is_cached = $template->is_cached($this->data, $cache_id);
							}
							lll('template', $this->data, ($is_cached ? 'cached' : ''), Log::EVENT_TEMPLATES);
							$OUTPUT .= $template->fetch($this->data, $cache_id);
							break;
					}
					break;
			}

			if (strpos($this->options['skip'], ';container;') === false)
					$OUTPUT .= '</div>';

			array_pop(self::$stack);
			return $OUTPUT;
		}

		public function process($_x_options = array(), $_x_try = false)
		{
			return $this->render($_x_options, $_x_try);
		}
		
		public function __toString()
		{
			return $this->render();
		}
		
		public static function prepare($file, $prefix = 'templates/', $id = null)
		{
			$file = str_replace('{#locale#}', L, $file);

			if (($temp = strpos($file, ':')) !== false)
			{
				$prefix = substr($file, 0, $temp + 1);
				$file = substr($file, $temp + 1);
			}
			if ($prefix == ':')
				$prefix = 'templates/';

			foreach (site()->base as $base)
				foreach (array('.php' => 'php', '.tpl' => 'dwoo', '.html' => 'html') as $extension => $engine)
					if (is_file($base . $prefix . $file . $extension))
						return new Template($id, $prefix . $file . $extension, 'file', $engine);
			return null;
		}
		
		public static function pseudoSmarty($template, $args = array())
		{
			foreach ($args as $k => $v)
				if (is_scalar($v))
					$template = str_replace('{$'.$k.'}', $v, $template);
				else if (is_array($v))
					foreach ($v as $kk => $vv)
						if (is_scalar($vv))
							$template = str_replace('{$'.$k.'.'.$kk.'}', $vv, $template);
				else if (is_object($v))
					foreach ($v as $kk => $vv)
						if (is_scalar($vv))
							$template = str_replace('{$'.$k.'->'.$kk.'}', $vv, $template);
			return $template;
		}

		public static function string($string, $args = array())
		{
			if (is_string($string) && !$args)
				return $string;
			if (is_array($string) && ($string[0] == '__'))
				return self::string(__($string[1], @$string[2]), $args);
			if ($string instanceof E)
				return $string->evaluate($args);
			if (is_string($string) && $args)
				return self::pseudoSmarty($string, $args);
		}
	}
?>

ACC SHELL 2018