<?php
	
	function setContentType($string)
	{
		header(sprintf(
			'Content-type: %s',
			$string
		));
	}
	
	function render($file, $output, array $params = [])
	{
		
		if ( ! file_exists($file))
			return;
		
		if (sizeof ($params) != 0)
			extract($params);
		
		include $file;
		
	}
	
	function redirect($url)
	{
		header(sprintf(
			'Location: %s',
			$url
		));
		exit;
	}
	
	function input($string)
	{
		
		$string	=	trim(
			$string
		);
		$string	=	htmlentities($string, ENT_QUOTES);
		
		return $string;
		
	}
	
	function c_page	($items = 0, $items_per_page = 10)
	{	
		
		$pages	=	max(1, ceil($items / $items_per_page));
		
		return $pages;
		
	}
	
	function page	($pages = 1)
	{
		
		if (isset($_GET['page']))
		{
			
			if ($_GET['page']	==	'last')
				$page	=	$pages;
			else
				$page	=	intval($_GET['page']);
			
		}
		else
				$page	=	1;
		
		$page	=	min($pages, max(1, $page));
		
		return $page;
		
	}
	
	function pagenation
					(
		$url,	//	
		$pages	=	1,	//	
		$page	=	1	//	
	)
	{
		
		
		echo $page != 1 ? sprintf(
			'<a href="%s1">&lt;&lt;</a> ',
			$url
		) : '&lt;&lt;';
		
		for	(
				$i	=	$page	-	5;
				$i	<=	$page	+	5;
				++$i
			)
		{
			
			if ($i > 0)
			{
				
				if ($i <= $pages)
				{
					
					echo ($i	==	$page)	?	sprintf(
						' %d ',
						$i
					)	:	sprintf(
						'<a href="%s%d">%d</a> ',
						$url,
						$i,
						$i
					);
					
				}
				
			}
			
		}
		
		echo $page < $pages ? sprintf(
			' <a href="%slast">&gt;&gt;</a>',
			$url
		) : '&gt;&gt;';

		
	}
	
	function timeleft($i)
	{
		
		$d	=	60	*	60	*	24;
		$h	=	60	*	60;
		$m	=	60;
		
		
		$days	=	floor(
			$i	/ $d
		);
		$hours	=	floor(
			($i	-	($days	*	$d))	/	$h
		);
		$minutes=	floor(
			($i	-	($days	*	$d)	-	($hours	*	$h))	/	$m
		);
		$seconds=	floor(
			($i	-	($days	*	$d)	-	($hours	*	$h)	-	($minutes	*	$m))
		);
		
		if ($days != 0)
		{
			$output=	sprintf(
				'%d д',
				$days
			);
		}
		else
		{
			if ($hours != 0)
			{
				$output=	sprintf(
					'%d ч',
					$hours
				);
			}
			else
			{
				if ($minutes != 0)
				{
					$output=	sprintf(
						'%d м',
						$minutes
					);
				}
				else
				{
					
					$output=	sprintf(
						'%d сек',
						$seconds
					);
					
				}
			}
		}
		
		return @$output;
		
	}
	
	function bb($string)
	{
		
		$string	=	strtr($string, [
			"/n"	=>	'<br />'
		]);
		
		return $string;
		
	}
	
?>