ACC SHELL

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

<?php
	// Author: Jakub Macek, CZ; Copyright: Poski.com s.r.o.; Code is 95% my work. Rest is significantly altered code from web. Do not copy.

	class E
	{
		public					$expression							= null;

		public function __construct($expression)
		{
			$this->expression = $expression;
		}

		public function evaluate($options = array())
		{
			return callback($this->expression, $options);
		}
	}

	function callback($_x_callback, $_x_args = array())
	{
		if ($_x_callback === null)
			return null;
		else if ($_x_callback instanceof E)
			return callback($_x_callback->expression, $_x_args);
		else if (is_string($_x_callback) && !function_exists($_x_callback))
		{
			if (strpos($_x_callback, '$this') !== false)
				error('callback uses $this: ' . $_x_callback);
			extract($_x_args, EXTR_SKIP);
			return eval($_x_callback);
		}
		else
			return call_user_func_array($_x_callback, $_x_args);
	}
	
	class ContainerIterator implements Iterator
	{
		private					$container							= null;
		private					$method								= null;
		private					$keys								= array();

		public function __construct($container, $keys = null, $method = null)
		{
			$this->container = $container;
			$this->method = $method;
			if ($keys === null)
				$this->keys = $this->container->keys();
			else
				$this->keys = $keys;
		}

		public function size()						{ return count($this->keys); }
		public function rewind()					{ reset($this->keys); }
		public function current()					{ $key = current($this->keys); if ($key === false) return false; if ($this->method) return $this->container->{$this->method}($key); else return $this->container->$key; }
		public function key()						{ return current($this->keys); }
		public function next()						{ $key = next($this->keys); if ($key === false) return false; if ($this->method) return $this->container->{$this->method}($key); else return $this->container->$key; }
		public function valid()						{ return ($this->current() !== false); }
		public function keys()						{ return $this->keys; }
	}

	class SimpleHTTP
	{
		public static function GET($url)
		{
			if (!class_exists('HttpRequest'))
				return @file_get_contents($url);
			$request = new HttpRequest($url);
			$request->setOptions(array(
				'timeout' => 10,
				'connecttimeout' => 10,
			));
			$tries = 3;
			$result = false;
			do
			{
				$response = $request->send();
				if ($response)
					$result = $response->getBody();
				$tries--;
			}
			while ($tries && ($result === false));
			return $result;
		}
	}

	class U
	{
		private	static			$lambda0							= null;

		public static function cut($text, $length)
		{
			if ($length <= 0)
				return '';
			if (mb_strlen($text) > $length)
				return mb_substr($text, 0, $length) . ' ...';
			else
				return $text;
		}
		
		public static function firstUpper($string)
		{
			return mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1);
		}

		public static function dashToUpper($string)
		{
			if (!self::$lambda0)
				self::$lambda0 = create_function('$matches', 'return strtoupper($matches[1]);');
			return preg_replace_callback('/-(\w)/', self::$lambda0, $string);
		}

		public static function indent($text, $indent = "\t")
		{
			if (strlen($indent) == 0)
				return $text;
			else
			{
				$result = $indent . str_replace("\n", "\n" . $indent, $text);
				if (substr($result, strlen($result) - strlen("\n" . $indent)) == ("\n" . $indent))
					$result = substr($result, 0, strlen($result) - strlen("\n" . $indent));
				return $result;
			}
		}

		public static function menuSelectedClass($location, $type = 'location', $class = 'selected')
		{
			$result = false;
			if ($type == 'location')
				$result = (page()->location == $location);
			if ($type == 'directory')
				$result = (page()->directory == $location);
			if ($type == 'pages_simple_id')
				$result = (page()->pages_simple_id == $location);
			return ($result ? $class : '');
		}

		public static function compareObjectPropertiesInteger($a, $b, $property, $reverse = false)
		{
			if ($a->$property == $b->$property)
				$result = 0;
			else if ($a->$property > $b->$property)
				$result = 1;
			else
				$result = -1;
			return ($reverse) ? (- $result) : $result;
		}

		public static function compareObjectPropertiesString($a, $b, $property, $reverse = false)
		{
			$result = strcmp($a->$property, $b->$property);
			return ($reverse) ? (- $result) : $result;
		}

		public static function findFiles($file_name, $simple = true, $single = false, $base = null)
		{
			if (!$base)
				$base = site()->base;
			if (!is_array($base))
				$base = explode(':', $base);
			$file_name = '/' . ltrim($file_name, '/');
			$file = substr(strrchr($file_name, '/'), 1);
			$directory = substr($file_name, 0, strlen($file_name) - strlen($file) - 1);

			$dirs = array(); $dir = '';
			foreach (explode('/', $directory) as $d)
			{
				$dir .= $d . '/';
				$dirs[] = ltrim($dir, '/');
			}

			$result = array();
			foreach ($dirs as $dir)
			{
				if ($simple)
					$f = $dir . $file;
				else
					$f = $dir . str_replace('/', '.', substr($file_name, 1 + strlen($dir)));
				if (self::firstExistingFile($base, $f))
					$result[] = $f;
			}
			return ($single ? (empty($result) ? false : $result[0]) : $result);
		}

		public static function firstExistingFile($dirs, $file = null)
		{
			if (is_string($dirs))
			{
				$temp = $dirs;
				$dirs = $file;
				$file = $temp;
			}
			if ($dirs === null)
				$dirs = site()->base;
			if (is_string($file) && $file)
				foreach ($dirs as $dir)
					if (file_exists($dir . $file))
						return $dir . $file;
			return false;
		}

		public static function fef($file) // simplified firstExistingFile
		{
			foreach (site()->base as $dir)
				if (file_exists($dir . $file))
					return $dir . $file;
			return null;
		}

		public static function request($key, $value = null)
		{
			if (isset($_REQUEST[$key]))
				return $_REQUEST[$key];
			elseif (isset($_REQUEST[$key = strtr($key, '.', '_')]))
				return $_REQUEST[$key];
			else
				return $value;
		}
	
		public static function flagSet($key, $value = null, $type = 'page')
		{
			if ($value === null)
				unset($GLOBALS[$type]['flags'][$key]);
			else
				$GLOBALS[$type]['flags'][$key] = $value;
		}

		public static function flagGet($key, $value = null, $type = 'page')
		{
			return (isset($GLOBALS[$type]['flags'][$key]) ? $GLOBALS[$type]['flags'][$key] : $value);
		}
		
		public static function cookie($key, $value = null, $expiration = null)
		{
			if ($expiration === null)
				return (isset($_COOKIE[$key]) ? $_COOKIE[$key] : $value);
			else
				setcookie($key, $value, time() + $expiration, PATH);
		}

		public static function patchQuery($query, $patch)
		{
			if (is_string($patch))
				$patch = array($patch);
			if (is_string($query))
				$query = array($query);
			return array_merge($query, $patch);
		}

		public static function removeNonLetters($string)
		{
			$string = self::removeAccents($string);
			$string = preg_replace('~[^-\.a-zA-Z0-9]~u', '-', $string);
			return $string;
		}

		public static function removeAccents($string)
		{
			$from = 'áäąćčďëéěęíłňóöŕřśšťúůüýźżžÁÄĄĆČĎËÉĚĘÍŁŃŇÓÖŔŘŚŠŤÚŮÜÝŹŽŻ';
			$to =   'aaaccdeeeeilnoorrsstuuuyzzzAAACCDEEEEILNNOORRSSTUUUYZZZ';
			for ($i = 0; $i < mb_strlen($from); $i++)
				$string = str_replace(mb_substr($from, $i, 1), $to[$i], $string);
			return $string;
		}

		public static function removeJunk($string, $skip = false)
		{
			$string = self::removeAccents($string);
			$pattern = '~[^a-zA-Z0-9\-\.\+\*/\~!@#\$%\^&\(\)\<\>\[\]\{\}\\\|:;\?_\']~u';
			if ($skip)
				$pattern = str_replace($skip, '', $pattern);
			$string = preg_replace($pattern, '-', $string);
			return $string;
		}

		public static function redirect($url, $options = array(), $timeout = 1)
		{
			if ($options === null)
				$options = array();
			if ($url instanceof Template)
				$url = $url->process($options);
			if (!page()->administration && ($timeout <= 1))
				header("Location: $url");
			page()->redirect = $url;
			if ($timeout != 1)
				page()->redirectTimeout = $timeout;
		}

		public static function urlize($string)
		{
			$string = trim(strtr(self::removeNonLetters($string), '.', '-'), '-');
			$string = strtolower($string);
			$string = preg_replace('~-+~', '-', $string);
			$string = str_replace('.', '', $string);
			return $string;
		}

		public static function select($value = null, $values = array())
		{
			if ($value === null)
				return $values;
			else if (isset($values[$value]))
				return $values[$value];
			else
				return false;
		}

		public static function applyNumericAdjustment($number, $adjustment)
		{
			$result = $number;
			if (substr($adjustment, 0, 1) == '=')
			{
				$adjustment = substr($adjustment, 1);
				if (strpos($adjustment, '%'))
					$result *= (0 + str_replace('%', '', $adjustment)) / 100;
				else
					$result = (0 + $adjustment);
			}
			else
			{
				if (strpos($adjustment, '%'))
					$result *= (100 + str_replace('%', '', $adjustment)) / 100;
				else
					$result += (0 + $adjustment);
			}
			return $result;
		}

		public static function arrayToUrlParameters($array)
		{
			$temp = array();
			foreach ($array as $x => $y)
				$temp[] = $x . '=' . $y;
			return implode('&', $temp);
		}

		public static function urlParametersToArray($string)
		{
			$array = array();
			foreach (explode('&', $string) as $v)
			{
				$temp = explode('=', $v);
				$array[$temp[0]] = $temp[1];
			}
			return $array;
		}

		public static function replaceParameter($var, $name, $value)
		{
			if (is_string($var))
			{
				return self::arrayToUrlParameters(self::replaceParameter(self::urlParametersToArray($var), $name, $value));
			}
			else
			{
				$var[$name] = $value;
				return $var;
			}
		}

		public static function gzip($data = '', $level = 6, $filename = '', $comments = '')
		{
			$flags = (empty($comment)? 0 : 16) + (empty($filename)? 0 : 8);
			$mtime = time();

			return (pack('C1C1C1C1VC1C1', 0x1f, 0x8b, 8, $flags, $mtime, 2, 0xFF) .
						(empty($filename) ? '' : $filename . "\0") .
						(empty($comment) ? '' : $comment . "\0") .
						gzdeflate($data, $level) .
						pack('VV', crc32($data), strlen($data)));
		}

		public static function randomString($length, $chars = 'abcdefghijklmnopqrstuvwxyz0123456789')
		{
			$result = '';
			$max = strlen($chars) - 1;
			for ($i = 0; $i < $length; $i++)
				$result .= substr($chars, mt_rand(0, $max), 1);
			return $result;
		}

		public static function patternMatch($patterns, $string, $process_all = true)
		{
			if (!is_array($patterns))
				$patterns = array($patterns);
			$result = false;
			foreach ($patterns as $patern)
				if ($pattern)
				{
					if ($negative = (substr($pattern, 0, 1) == '!'))
						$pattern = substr($pattern, 1);
					if (substr($pattern, 0, 1) == '~')
						$ok = preg_match($pattern, $string);
					else
						$ok = ($pattern == $string);
					if ($ok && !$negative)
						$result = true;
					if ($ok && $negative)
						$result = false;
					if ($ok && !$process_all)
						return $result;
				}
			return $result;
		}

		public static function piece($piece, $skip, $value)
		{
			$args = func_get_args();
			array_shift($args); array_shift($args);
			foreach ($args as $value)
				if ((($value === true) || ($piece == $value)) && (strpos($skip, ';'.$value.';') === false))
					return true;
		    return false;
		}

		public static function compare($value1, $value2, $operator = '==')
		{
			if (!in_array($operator, array('==', '!=', '===', '!==', '>', '<', '>=', '<=')))
				$operator = '==';
			if ($operator == '==')
				return ($value1 == $value2);
			else if ($operator == '==')
				return ($value1 == $value2);
			else if ($operator == '!=')
				return ($value1 != $value2);
			else if ($operator == '===')
				return ($value1 === $value2);
			else if ($operator == '!==')
				return ($value1 !== $value2);
			else if ($operator == '>')
				return ($value1 > $value2);
			else if ($operator == '>=')
				return ($value1 >= $value2);
			else if ($operator == '<')
				return ($value1 < $value2);
			else if ($operator == '<=')
				return ($value1 <= $value2);
		}
		
		public static function metaArrayToString($input, $keys = array(), $onlyKeys = false)
		{
			if (!$onlyKeys)
				$keys = array_unique(array_merge($keys, array_keys($input)));
			$output = '';
			foreach ($keys as $k)
				$output .= $k . ':' . (isset($input[$k]) ? $input[$k] : '') . "\n";
			return $output;
		}
		
		public static function metaStringToArray($input, $keys = array(), $onlyKeys = false)
		{
			$output = array();
			foreach ($keys as $k)
				$output[$k] = null;
			foreach (explode("\n", $input) as $line)
			{
				$line = trim($line);
				if ($temp = strpos($line, ':'))
				{
					$k = substr($line, 0, $temp);
					if (!$onlyKeys || isset($output[$k]))
						$output[$k] = substr($line, $temp + 1);
				}
			}
			
			return $output;
		}
	}

	function ar()
	{
		$result = array();
		$a = func_get_args();
		foreach ($a as $aa)
			if (!isset($key))
				$key = $aa;
			else
			{
				$result[$key] = $aa;
				unset($key);
			}
		return $result;
	}
	
	function array_insert($arr1, $key, $arr2, $before = false)
	{ // from http://drupal.org/node/66183
		$done = false;
		foreach ($arr1 as $arr1_key => $arr1_val)
		{
			if (!$before)
				$new_array[$arr1_key] = $arr1_val;
			if ($arr1_key == $key && !$done)
			{
				foreach($arr2 as $arr2_key => $arr2_val)
					$new_array[$arr2_key] = $arr2_val;
				$done = true;
			}
			if ($before)
				$new_array[$arr1_key] = $arr1_val;
		}
		if (!$done)
			$new_array = array_merge($arr1, $arr2);
		return $new_array;
	}

	function dgp() { dump($GLOBALS["page"]); }
	function dgs() { dump($GLOBALS["site"]); }

?>

ACC SHELL 2018