Просмотр файла includes/functions.php

Размер файла: 8.24Kb
<?php
function gen_uri($m, $id = '', $q = '', $v = '', $with_v = true, $with_sid = true)
{
	global $config;
	
	$result = '';

	if( $config['catalogue_uri_type'] == 'pages' )
	{
		if( $id )
		{
			$result = $id . '.' . $m;
		}
		else if( $with_v == false && $m == 'index' )
		{
			$result = '';
		}
		else
		{
			$result = $m;
		}

		if( $with_v )
		{
			if( $v )
			{
				$result .= '.' . $v;
			}
			else
			{
				$result .= '.' . VERSION;
			}
		}

		if( $q )
		{
			$result .= '?' . $q;
		}
	}
	else if( $config['catalogue_uri_type'] == 'dirs' )
	{
		if( $id )
		{
			$result = $m . '/' . $id . '/';
		}
		else
		{
			$result = $m == 'index' ? '' : $m . '/';
		}

		if( $with_v && $v )
		{
			if( $q != '' )
			{
				$q = 'version=' . $v . '&amp;' . $q;
			}
			else
			{
				$q = 'version=' . $v;
			}
		}

		if( $q )
		{
			$result .= '?' . $q;
		}
	}
	else
	{
		$qs = array();

		if( $m != 'index' ) $qs[] = 'm=' . $m;
		if( $id ) $qs[] = 'id=' . $id;
		if( $with_v && $v ) $qs[] = 'version=' . $v;
		if( $q ) $qs[] = $q;

		$qs = implode('&amp;', $qs);

		if( $qs )
		{
			$result = '?' . $qs;
		}
	}

	return $with_sid ? append_sid( PATH . '/' . $result ) : PATH . '/' . $result;
}

function switch_version($m, $id, $q = '')
{
	if( VERSION == 'wml' )
	{
		return "WML | <a href='" . gen_uri($m, $id, $q, 'xhtml') . "'>xHTML</a>";
	}
	else
	{
		return "<a href='" . gen_uri($m, $id, $q, 'wml') . "'>WML</a> | xHTML";
	}
}

function switch_language($m, $id, $q = '')
{
	global $lang_list;

	$q = ( $q == '' ? '' : $q . '&amp;' );

	if( $_SESSION['language'] == 'russian' )
	{
		return "<a href='" . gen_uri($m, $id, $q . 'lang=english') . "'>English</a>";
	}
	else
	{
		return "<a href='" . gen_uri($m, $id, $q . 'lang=russian') . "'>Russian</a>";
	}
}

function put_error($err_type, $message, $line = 0, $file = '')
{
	switch( $err_type )
	{
		case MESSAGE: $title = ''; break;
		case FATAL_ERROR: $title = 'Fatal error'; break;
		case DBMS_ERROR: $title = 'DBMS error'; break;
		case ERROR: $title = 'Error'; break;
		default: $title = 'Unknown error';
	}

	if( defined('VERSION') )
	{
		global $config, $lang, $m, $css_vars;
	}
	else
	{
		define('VERSION', 'wml');
		define('MIME', 'text/vnd.wap.wml');
	}

	$m = isset( $m ) ? $m : 'index';
	$css_vars = isset( $css_vars ) ? $css_vars : array();

	$template = new template( ROOTPATH );
	$template->load_template( 'templates/' . VERSION . '/header.tpl', 'header' );
	$template->load_template( 'templates/' . VERSION . '/message.tpl', 'body' );
	$template->load_template( 'templates/' . VERSION . '/footer_empty.tpl', 'footer' );

	if( VERSION == 'xhtml' )
	{
		if( $css_vars == array() )
		{
			$css_vars = array(
			'FW' => DEFAULT_FW,
			'FC' => DEFAULT_FC,
			'BGC' => DEFAULT_BGC,
			'AC' => DEFAULT_AC,
			'DIV_FC' => DEFAULT_DIV_FC,
			'DIV_BGC' => DEFAULT_DIV_BGC
			);
		}

		$template->load_template( 'templates/default.css', 'css' );
		$template->set_vars('css', $css_vars);
		$css = $template->evaluate_tpl('css');
		$template->cancel('css');
	}
	else
	{
		$css = '';
	}

	if( $title && $err_type !== MESSAGE )
	{
		$message = '<b>' . $title . '</b>: ' . $message;
	}

	if( $line && $file )
	{
		$message .=
		"<br />\r\n" .
		"<b>Line</b>: " . $line . "<br />\r\n" .
		"<b>File</b>: " . $file . "<br />";
	}

	$template->set_vars('header', array('TITLE' => $title, 'CSS' => $css));
	$template->set_vars('body', array('MESSAGE' => $message));	

	header('Content-type: ' . MIME . '; charset=utf-8');
	header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
	header('Cache-Control: no-cache, must-relative');

	$template->evaluate();

	exit;
}

function redirect($url)
{
	$url = str_replace('&amp;', '&', $url);

	header( 'Location: ' . $url );
	exit;
}

function cleanup($string)
{
	$bb = array(
	"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06",
	"\x07", "\x08", "\x0B", "\x0C", "\x0E", "\x0F", "\x10",
	"\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17",
	"\x18", "\x19", "\x1A", "\x1B", "\x1C", "\x1D", "\x1E",
	"\x1F"
	);

	return str_replace($bb, '', $string);
}

function random_string($len)
{
	$chrs = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnPpQqRrSsTtUuVvWwXxYyZz0123456789';

	for($s = '', $i = 0; $i < $len; $i++)
	{
		$s .= $chrs[rand(0, strlen($chrs) - 1)];
	}

	return $s;
}

function output($string, $quotes = ENT_QUOTES)
{
	$string = htmlspecialchars($string, $quotes);

	if( VERSION == 'wml' )
	{
		$string = str_replace('$', '$$', $string);
	}

	return $string;
}

function maxpage($total, $onpage, &$page_eq = NULL)
{
	global $page;

	if( isset($page_eq) )
	{
		$page = &$page_eq;
	}

	if( $total )
	{
		$maxpage = (int) (($total % $onpage) ? ($total / $onpage) : ($total / $onpage - 1));
		if($page > $maxpage) $page = $maxpage;
		if($page < 0) $page = 0;
	}
	else
	{
		$maxpage = $page = 0;
	}

	return $maxpage;
}

function limit($page, $onpage)
{
	return ($page * $onpage) . ", " . $onpage;
}

function pages($page, $maxpage, $onpage, $nlinks = 1, $query = '', $path = '')
{
	$links = array();
	$query = $query != '' ? '&amp;' . preg_replace('/^&amp;/', '', $query) : '';
	$path = empty( $path ) ? $_SERVER['PHP_SELF'] : $path;

	if( strpos($path, '?') === false )
	{
		$path = $path . '?';
	}

	if($page - $nlinks > 1)
	{
		$links[] = "<a href='" . $path . $query . "'>1</a>";
		$links[] = "...";

		for($i = $page - $nlinks; $i < $page; $i++)
		{
			$links[] = "<a href='" . $path . "&amp;page=" . ($i + 1) . $query . "'>" . ($i + 1) . "</a>";
		}
	}
	else
	{

		for($i = 0; $i < $page; $i++)
		{
			$links[] = "<a href='" . $path . "&amp;page=" . ($i + 1) .  $query . "'>" . ($i + 1) . "</a>";
		}
	}

	if($page + $nlinks < $maxpage)
	{
		for($i = $page; $i <= $page + $nlinks; $i++)
		{
			if($page == $i)
			{
				$links[] = $i + 1;
			}
			else
			{
				$links[] = "<a href='" . $path . "&amp;page=" . ($i + 1) .  $query . "'>" . ($i + 1) . "</a>";
			}
		}

		if($maxpage - $i != 0)
		{
			$links[] = "...";
		}

		$links[] = "<a href='" . $path . "&amp;page=" . ($maxpage + 1) .  $query . "'>" . ($maxpage + 1) . "</a>";
	}
	else
	{
		for($i = $page; $i <= $maxpage; $i++)
		{
			if($page == $i)
			{
				$links[] = $i + 1;
			}
			else
			{
				$links[] = "<a href='" . $path . "&amp;page=" . ($i + 1) .  $query . "'>" . ($i + 1) . "</a>";
			}
		}
	}

	if( count($links) == 1 )
	{
		return;
	}

	return implode(" ", $links) . "<br />\r\n";
}

function tr($s, $reverse = false)
{
	$lat = array('Jo', 'jo', 'Yo', 'yo', 'Zh', 'zh', '*', 'Ch', 'ch', 'Sh', 'sh', 'Shc', 'shc', "''", "'", 'Ye', 'ye', 'Yu', 'yu', 'Ya', 'ya', 'Ja', 'ja', 'X', 'x', 'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'Y', 'y', 'Z', 'z');
	$cyr = array('Ё', 'ё', 'Ё', 'ё', 'Ж', 'ж', 'ж', 'Ч', 'ч', 'Ш', 'ш', 'Щ', 'щ', 'ъ', 'ь', 'Э', 'э', 'Ю', 'ю', 'Я', 'я', 'Я', 'я', 'Ы', 'ы', 'А', 'а', 'Б', 'б', 'Ц', 'ц', 'Д', 'д', 'Е', 'е', 'Ф', 'ф', 'Г', 'г', 'Х', 'х', 'И', 'и', 'К', 'к', 'Л', 'л', 'М', 'м', 'Н', 'н', 'О', 'о', 'П', 'п', 'Ь', 'ь', 'Р', 'р', 'С', 'с', 'Т', 'т', 'У', 'у', 'В', 'в', 'Ш', 'ш', 'Й', 'й', 'З', 'з');

	$result = array();

	if( $reverse )
	{
		$tr = array_combine($cyr, $lat);
	}
	else
	{
		$tr = array_combine($lat, $cyr);	
	}

	for($p = 0; true; $p = strlen(implode($result)))
	{
		$part = substr($s, $p);
		$pos = strpos($part, '<');

		if( $pos !== false )
		{
			$result[] = substr($part, 0, $pos); 
			$result[] = substr($part, $pos, strpos($part, '>') - $pos + 1);
		}
		else
		{
			if( $part !== '' )
			{
				$result[] = $part;
			}

			break;
		}
	}

	for($i = 0; $i < count( $result ); $i++)
	{
		if( $result[$i] === '' )
		{
			continue;
		}

		if( $result[$i][0] != '<' )
		{
			$result[$i] = strtr($result[$i], $tr);
		}
		else if( !preg_match('#^<\d+>$#', rtrim($result[$i])) )
		{
			$result[$i] = substr($result[$i], 1, strlen($result[$i]) - 2);
		}
	}

	return implode( $result );
}
?>