<? // Copyright by Mateusz Rusin © 2005 // strict // 2.31
session_name ('session');
session_start ();
ini_set ('zlib.output_compression', 'On');
ini_set ('zlib.output_compression_level', '9');
require_once 'menu.class.php';
class Page {
	var $charset;
	var $language;
	var $next_language;
	var $working_mode;
	var $wrong_path = '';
	var $agent_id;
	var $agent;
	var $agent_type;
	var $profile_id;
	var $user_category = 'guest';
	var $skin;
	var $template;
	var $template_variant;
	var $news_set;
	var $page_id;
	var $keywords;
	var $desc_abstract;
	var $title;
	var $page_title;
	var $author;
	var $text;
	var $heads = '';
	var $tags;
	var $menu;
	var $db;
	var $ini;
	var $enter = false;
	function Page () {	// Copyright by Mateusz Rusin © 2005 // 1.58
		if (empty ($_SESSION))
			$this -> enter = true;
		$this -> set_mode ();
		if (isset ($_POST ['user'], $_POST ['password'])) {
			$user =& new User ($this -> db, $_POST ['user'], $_POST ['password']);
		}
		if (isset ($_POST ['logout'])) {
			$user =& new User ($this -> db);
			$user -> logout ();
		}
		if (!empty ($_SESSION ['user'] -> user_category))
			$this -> user_category =& $_SESSION ['user'] -> user_category;
		$this -> charset =& $this -> ini -> get ('charset');
		$this -> set_language ();
		$this -> set_agent_type ();
		$this -> set_profile ();
		$this -> set_current_page ();
		$this -> set_skin ();
 		$this -> set_menu ();
		$this -> tags = array (
			'on_load' => "javascript: defaultStatus = '" . $this -> ini -> get ('status_' . $this -> language) . "'" . ($this -> agent_type == 'MSIE' ? "; setInterval ('ZawszeMenu ()', 1)" : ''),
			'keywords' =>& $this -> keywords,
			'abstract' =>& $this -> desc_abstract,
			'title' =>& $this -> title,
			'page_title' =>& $this -> page_title,
			'author' =>& $this -> author,
			'publication_date' => !empty ($this -> publication_date) && strtotime ($this -> publication_date) !== -1 ? date ('r', strtotime ($this -> publication_date)) : '',
			'language' =>& $this -> language,
			'lang' =>& $this -> language,
			'charset' => $this -> charset,
			'http_host' => $_SERVER ['HTTP_HOST'],
			'base' => str_replace ($this -> wrong_path, '', 'http://' . $_SERVER ['HTTP_HOST'] . substr ($_SERVER ['SCRIPT_NAME'], 0, strrpos ($_SERVER ['SCRIPT_NAME'], '/')) . '/'),
			'query_string' => isset ($_SERVER ['QUERY_STRING']) ? '?' . str_replace ('&', '&amp;', $_SERVER ['QUERY_STRING']) : '?',
			'language_id_key' => $this -> ini -> get ('language_id_key'),
			'next_language_key' => $this -> next_language,
			'next_language_text' => $this -> ini -> get ('language_text_' . $this -> next_language),
			'page_id_key' => $this -> ini -> get ('page_id_key'),
			'page_id' => $this -> page_id,
			'news_set' => $this -> news_set ? $this -> news_set -> news_set : '',
			'menu' => $this -> make_menu (),
			'main_menu' => $this -> make_main_menu (),
			'sub_menu' => $this -> make_sub_menu (),
			'heads' =>& $this -> heads,
			'css' => $this -> choose_css ()
		);
		$result = $this -> db -> select ('news_sets');
		while ($news_set_data = $this -> db -> fetch_object ($result))
			$this -> tags ['news_set_' . $news_set_data -> news_set_id] = $this -> make_news (!empty ($news_set_data -> count) ? $news_set_data -> count : null, $news_set_data -> news_set_id, !empty ($news_set_data -> order) ? $news_set_data -> order : null);	
		if ($this -> news_set)
			$this -> text .= '<div class="' . $this -> news_set -> news_set . '">'. $this -> make_news () . '</div>';
		$result = $this -> db -> select ('extensions');
		if ($result)
		{
			$extensions = array ();
			while ($extension_data = $this -> db -> fetch_object ($result))
			{
				if (is_file ($extension_data -> extension . '.php'))
					$extensions [$extension_data -> tag] = $extension_data -> extension . '.php';
//					$this -> tags [$extension_data -> tag] = include $extension_data -> extension . '.php';
			}
			foreach ($extensions as $tag => $file)
				$this -> tags [$tag] = include $file;
		}
		$this -> tags ['text'] =& $this -> text;
	}
	function choose_css () {	// Copyright by Mateusz Rusin © 2005 // 0.00
		return '';
	}
	function show ($type = null) {	// Copyright by Mateusz Rusin © 2005 // 1.22
		if (empty ($type))
			$type = !empty ($_GET [$this -> ini -> get ('type_id_key')]) ? $_GET [$this -> ini -> get ('type_id_key')] : 'html';
		switch ($type) {
			case 'css':
				$this -> template = "@charset '{charset}';\r\n";
				$css = $this -> db -> fetch_object ($this -> db -> select ('css', array ('css_id' => 1)));
				$this -> template .= $css -> css;
				$css = $this -> db -> fetch_object ($this -> db -> select ('css', array ('css_id' => $this -> skin -> css_id)));
				$this -> template .= $css -> css;
				if (!empty ($this -> agent_type) && !empty ($this -> skin -> {'css_' . $this -> agent_type . '_id'})) {
					$css = $this -> db -> fetch_object ($this -> db -> select ('css', array ('css_id' => $this -> skin -> {'css_' . $this -> agent_type . '_id'})));
					$this -> template .= $css -> css;
				}
			case 'html':
				header ('Content-type: text/' . $type . '; charset=' . $this -> charset);
				$page = $this -> replace_tags ($this -> template, $this -> tags);
				/*if ($this -> charset != 'utf-8') {
					if (strpos ($this -> charset, 'ucs') === 0 || strpos ($this -> charset, 'utf') === 0)
						$page = mb_convert_encoding ($page, $this -> charset, 'utf-8');
					else
						$page = mb_convert_encoding ($page, 'HTML-ENTITIES', 'utf-8');
				}*/
				print $page;
			break;
			case 'rss':
				print $this -> make_RSS ();
			break;
			case 'cdf':
				print $this -> make_CDF ();
		}
		if ($type != 'css')
			$this -> opens_log ();
	}
	function set_mode () { // Copyright by Mateusz Rusin © 2005 // 1.01
		$ini = parse_ini_file (file_get_contents ('ini.ini'));
		$this -> working_mode = $ini ['dbType'];
		include_once (strtolower ($this -> working_mode) . '.class.php');
		$this -> db =& new db ($ini ['dbName'], $ini ['dbServer'], $ini ['dbUser'], $ini ['dbPassword']);
// 		$this -> ini =& $_SESSION ['ini'];
		if (empty ($this -> ini)) 
			$this -> ini =& new ini ('ini', 'key', 'value', $this -> db);
	}
	function set_language () { // Copyright by Mateusz Rusin © 2005 // 1.00
		$this -> language =& $_SESSION ['language'];
		if (!empty ($_GET [$this -> ini -> get ('language_id_key')]) && array_search ($_GET [$this -> ini -> get ('language_id_key')], explode (', ', $this -> ini -> get ('languages'))) !== false)
			$this -> language = $_GET [$this -> ini -> get ('language_id_key')];
		if (empty ($this -> language) && !empty ($_SERVER ['HTTP_ACCEPT_LANGUAGE'])) 
			foreach (explode (',', $_SERVER ['HTTP_ACCEPT_LANGUAGE']) as $language)
				if (array_search (substr ($language, 0, 2), explode (', ', $this -> ini -> get ('languages'))) !== false) {
					$this -> language = substr ($language, 0, 2);
					break;
				}
		if (empty ($this -> language))
			$this -> language = 'pl';
		$languages = explode (', ', $this -> ini -> get ('languages'));
		$language_index = array_search ($this -> language, $languages);
		$this -> next_language = $languages [$language_index + 1 >= count ($languages) ? 0 : $language_index + 1];
	}
	function set_agent_type () { // Copyright by Mateusz Rusin © 2005 // 1.01
		foreach (explode (',', $this -> ini -> get ('agent_types')) as $agent)
			if (stristr ($_SERVER ['HTTP_USER_AGENT'], trim ($agent)))
				$this -> agent_type = trim ($agent);
		$this -> agent_type_id =& $_SESSION ['agent_type_id'];
		if (empty ($this -> agent_type_id)) {
			if ($this -> working_mode != 'txt') {
				$result = $this -> db -> select ('agents_types', "`agent_type` = '" . $_SERVER ['HTTP_USER_AGENT'] . "'");
				if ($this -> db -> num_rows ($result)) {
					$this -> agent_type_id = $this -> db -> fetch_object ($result);
					$this -> agent_type_id = (int) $this -> agent_type_id -> agent_type_id;
				}
				else {
@					$this -> agent = get_browser ();
					$this -> db -> insert ('agents_types', array ('agent_type' => $_SERVER ['HTTP_USER_AGENT'], 'crawler' => (empty ($this -> agent -> crawler) ? 0 : 1)));
					$this -> agent_type_id = $this -> db -> insert_id ();
				}
			}
		}
	}
	function set_profile () { // Copyright by Mateusz Rusin © 2005 // 1.5
//		$this -> agent_id =& $_SESSION ['agent_id'];
//		$this -> profile_id =& $_SESSION ['profile_id'];
		if (empty ($this -> profile_id)) {
			if ($this -> working_mode != 'txt') {
				if (!empty ($_COOKIE ['ID'])) {
					$this -> agent_id = (int) $_COOKIE ['ID'];
					if (empty ($_SESSION ['user'] -> profile_id)) {
						$this -> profile_id = $this -> db -> fetch_object ($this -> db -> select ('agents', array ('agent_id' => $this -> agent_id)));
						$this -> profile_id = (int) $this -> profile_id -> profile_id;
					} else 
						$this -> profile_id = $_SESSION ['user'] -> profile_id;
				}
				if (empty ($this -> profile_id)) {
					$this -> db -> insert ('profiles', array ('profile_id' => 'null'));
					$this -> profile_id = (int) $this -> db -> insert_id ();
					$this -> db -> insert ('agents', array ('agent_id' => 'null', 'profile_id' => $this -> profile_id));
					$this -> agent_id = (int) $this -> db -> insert_id ();
					setcookie ('ID', $this -> agent_id, 2147483647, '/');
				}
			} else 
				$this -> profile_id = 0;			
		}
	}
	function set_skin () { // Copyright by Mateusz Rusin © 2005 // 1.03
		if (!empty ($_GET [$this -> ini -> get ('skin_id_key')])) 
			$this -> skin = $this -> db -> fetch_object ($this -> db -> select ('skins', array ('skin_id' => $_GET [$this -> ini -> get ('skin_id_key')])));
		if (empty ($this -> skin)) 
			$this -> skin = $this -> db -> fetch_object ($this -> db -> select ('skins', array ('skin_id' => $this -> ini -> get ('default_skin_id'))));
		$this -> skin -> legend = $this -> skin -> {'legend_' . $this -> language};
		$this -> skin -> title = $this -> skin -> {'title_' . $this -> language};
		$template = $this -> db -> fetch_object ($this -> db -> select ('templates, templates_variants', 'templates.template_id = templates_variants.template_id and skin_id = ' . $this -> skin -> skin_id . ' and template_variant = ' . $this -> template_variant));
		$this -> template = !empty ($template -> file) && is_file ($template -> file) ? file_get_contents ($template -> file) : $template -> template;
		if ($this -> text && $this -> ini -> get ('text_template'))
			$this -> template = $this -> replace_tags ($this -> template, array ('text' => $this -> ini -> get ('text_template')));
	}
	function set_current_page () { // Copyright by Mateusz Rusin © 2005 // 1.06
		$this -> keywords = $this -> ini -> get ('keywords_' . $this -> language);
		$this -> desc_abstract = $this -> ini -> get ('abstract_' . $this -> language);
		$this -> title = $this -> ini -> get ('title_' . $this -> language);
		$this -> author = $this -> ini -> get ('author');
		if (!empty ($_GET [$this -> ini -> get ('page_id_key')])) {
			$this -> page_id = $_GET [$this -> ini -> get ('page_id_key')];
			$page = $this -> db -> fetch_object ($this -> db -> select ('pages', "`page_id` = '{$this -> page_id}' and `rights` like '%{$this -> user_category}%'"));
		}
		if (empty ($page)) 
		{
			if ($this -> enter && $this -> ini -> get ('intro_page_id'))
				$this -> page_id = $this -> ini -> get ('intro_page_id');
			else
				$this -> page_id = $this -> ini -> get ('default_page_id');
		}
/*		$page = $this -> db -> fetch_object 
			(
				$this -> db -> select 
					(
						'`pages`, `news_sets`',
						"`page_id` = '" . $this -> page_id . "' and `pages`.`news_set_id` = `news_sets`.`news_set_id`"
					)
			);
*/		$page = $this -> db -> fetch_object ($this -> db -> select ('pages', array ('page_id' => $this -> page_id)));
		if (!empty ($page -> news_set_id))
			$this -> news_set = $this -> db -> fetch_object ($this -> db -> select ('news_sets', array ('news_set_id' => $page -> news_set_id)));
		$this -> template_variant = $page -> template_variant;
//var_dump ($this -> page_id);//		$page = $this -> db -> fetch_object ($this -> db -> select ('`pages`, `users`', "`pages`.`user_id` = `users`.`user_id` and `page_id` = '{$this -> page_id}'"));
		if ($page -> {'keywords_' . $this -> language})
			$this -> keywords = $page -> {'keywords_' . $this -> language} . ', ' . $this -> keywords;
		if ($page -> {'abstract_' . $this -> language})
			$this -> desc_abstract = $page -> {'abstract_' . $this -> language};
		if ($page -> {'title_' . $this -> language})
		{
			$this -> title = strip_tags ($page -> {'title_' . $this -> language}) . ' &mdash; ' . $this -> title;
			$this -> page_title = $page -> {'title_' . $this -> language};
		}
		if (!empty ($page -> signature)) 
			$this -> author = $page -> signature;
		if (!empty ($page -> publication_date))
			$this -> publication_date = date ('r', strtotime ($page -> publication_date));
		if (!empty ($page -> {'text_' . $this -> language}))
		{
			if ($this -> ini -> get ('text_template'))
				$this -> template = $this -> replace_tags ($this -> template, array ('text' => $this -> ini -> get ('text_template')));
			$this -> text = empty ($page -> nl2br) ? $page -> {'text_' . $this -> language} : nl2br ($page -> {'text_' . $this -> language});
		}
		if ($this -> page_id == $this -> ini -> get ('gall_page_id'))
			$this -> text .= $this -> make_gallery ();
		if ($this -> page_id == $this -> ini -> get ('search_page_id'))
			$this -> text .= $this -> search (isset ($_GET [$this -> ini -> get ('query_page_key')]) ? $_GET [$this -> ini -> get ('query_page_key')] : false);
	}
	function search ($query) { // Copyright by Mateusz Rusin © 2005 // 1.00
		$search_result = '<ol class="' . $this -> ini -> get ('search_ol_class') . "\">\r\n";
		$result = $this -> db -> query ("
			select 
				`page_id`,
				`title_{$this -> language}`
				`title`, 
				`abstract_{$this -> language}`
				`abstract`,
				match 
					(`title_{$this -> language}`, `keywords_{$this -> language}`, `abstract_{$this -> language}`, `text_{$this -> language}`) 
				against 
					('$query') 
				`score`
			from 
				`pages` 
			where 
				match 
					(`title_{$this -> language}`, `keywords_{$this -> language}`, `abstract_{$this -> language}`, `text_{$this -> language}`) 
				against 
					('$query') 
			and 
				`rights` like '%{$this -> user_category}%'
		");
		while ($search_result_data = $this -> db -> fetch_object ($result)) 
		{
			if (empty ($ratio))
				$ratio = 100 / $search_result_data -> score;
			$search_result .= '<li class="' . $this -> ini -> get ('search_li_class') . "\">\r\n" .
			'<a class="' . $this -> ini -> get ('search_a_class') . '" href="?' . $this -> ini -> get ('page_id_key') . '=' . $search_result_data -> page_id . '">' . 
			'<h4 class="' . $this -> ini -> get ('search_h_class') . '">' . $search_result_data -> title . "</h4>\r\n" . 
			(!empty ($search_result_data -> desc_abstract) 
				? '<p class="' . $this -> ini -> get ('search_p_class') . '">' . $search_result_data -> desc_abstract . "</p>\r\n" 
				: '' 
			) .
			'<div class="' . $this -> ini -> get ('search_rnk_class') . '">
			<span  class="' . $this -> ini -> get ('search_pos_class') . '" style="width: ' . round ($search_result_data -> score * $ratio) . '%;"></span>
			<span  class="' . $this -> ini -> get ('search_neg_class') . '" style="width: ' . (100 - round ($search_result_data -> score * $ratio)) . '%;"></span>
			</div>' . 
			"</a></li>\r\n";
		}
		$search_result .= "</ol>\r\n";
		return $search_result;
	}
	function make_news ($number = null, $news_set_id = false, $order = false) { // Copyright by Mateusz Rusin © 2005 // 1.11
		$news = '';
		if (!$news_set_id)
		{
			if (!empty ($this -> news_set -> news_set_id))
			{
				$news_set_id = $this -> news_set -> news_set_id;
			}
			if (!$order && !empty ($this -> news_set -> order))
			{
				$order = $this -> news_set -> order;
			}
		}
		if (!$order)
			$order = 'publication_date desc';
		$result = $this -> db -> select ('news', array ('news_set_id' => (int) $news_set_id), '*'/*array ('title_' . $this -> language, 'text_' . $this -> language, 'publication_date', 'link')*/, $order, '', $number);
		while ($news_data = $this -> db -> fetch_object ($result)) 
		{
			if ($this -> ini -> get ('news_template'))
				$news .= $this -> replace_tags ($this -> replace_tags 
					(
						$this -> ini -> get ('news_template'), 
							array 
								(
									'news_id' => $news_data -> news_id,
									'text' => empty ($news_data -> nl2br) ? $news_data -> {'text_' . $this -> language} : nl2br ($news_data -> {'text_' . $this -> language}),
									'title' => $news_data -> {'title_' . $this -> language},
									'publication_date' => $news_data -> publication_date,
									'link' => $news_data -> link 
										? ($this -> ini -> get ('link_template') 
											? $this -> replace_tags ($this -> ini -> get ('link_template'), array ('link' => $news_data -> link)) 
											: $news_data -> link) 
										: ''
								)
					), $this -> tags);
			else
			{
				$news .= '<hr class="' . $this -> ini -> get ('news_hr_class') . "\" />\r\n" . '<div class="' . $this -> ini -> get ('news_div_class') . '">' .
				(!empty ($news_data -> thumb) 
					? 
					(
						(!empty ($news_data -> full) 
							? '<a style="cursor: hand;" onclick="window.open (\'popup/?photo=' . urlencode ('/news_photo/?size=full&amp;id=' . $news_data -> news_id) . "', '_blank', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=no,dependent=yes')\" />\r\n"
							: '' 
						)
						 .
						'<img class="' . $this -> ini -> get ('news_photo_class') . '" src="news_photo/?id=' . $news_data -> news_id . "\" />\r\n" .
						(!empty ($news_data -> full) 
							? "</a>\r\n"
							: '' 
						)
					) 
					: '' 
				) .
				'<h4 class="' . $this -> ini -> get ('news_title_class') . '">' . $news_data -> {'title_' . $this -> language} . "</h4>\r\n" . 
				'<p class="' . $this -> ini -> get ('news_text_class') . '">' . $news_data -> {'text_' . $this -> language} . "</p>\r\n" . 
				(!empty ($news_data -> link) 
					? ($this -> ini -> get ('link_template')) 
						? $this -> replace_tags ($this -> ini -> get ('link_template'), array ('link' => $news_data -> link))
						: '<a class="' . $this -> ini -> get ('news_link_class') . '" href="http://' . $news_data -> link . '">' . $news_data -> link . "</a>\r\n"
					: '' 
				) .
				(!empty ($news_data -> publication_date) && $news_data -> publication_date != '0000-00-00 00:00:00' 
					? '<p class="' . $this -> ini -> get ('news_date_class') . '">' . $news_data -> publication_date . "</p>\r\n"
					: ''
				) . "</div>\r\n";
			}
		}
		if (!$this -> ini -> get ('news_template'))
			$news .= '<hr class="' . $this -> ini -> get ('news_hr_class') . "\" />\r\n";
		return $news;
	}
	function make_gallery ($number = null) { // Copyright by Mateusz Rusin © 2005 // 1.00
		$gallery = '';
		$result = $this -> db -> select ('gallery', '', array ('title_' . $this -> language, 'description_' . $this -> language, 'photo_id'), 'publication_date', 'desc', $number);
		while ($gallery_data = $this -> db -> fetch_object ($result)) 
		{
			$gallery .= '
				<div class="' . $this -> ini -> get ('gall_div_class') . '">
					<h4 class="' . $this -> ini -> get ('gall_title_class') . '">' . $gallery_data -> {'title_' . $this -> language} . '</h4>
					<img class="' . $this -> ini -> get ('gall_photo_class') . '" src="gallery_photo.php?id=' . $gallery_data -> photo_id . '" />
					<p class="' . $this -> ini -> get ('gall_desc_class') . '">' . $gallery_data -> {'description_' . $this -> language} . '</p>
				</div>';
		}
		return $gallery;
	}
	function set_menu () { // Copyright by Mateusz Rusin © 2005 // 2.11
//		$this -> menu =& $_SESSION ['menu'];
		if (empty ($this -> menu)) {
			$this -> menu ['sub'] = $this -> set_sub_menu ();
			$this -> menu =& new menu ($this -> menu, $this -> ini -> get ('menu_type_def'), $this -> ini -> get ('menu_short_def'));
			if ($this -> ini -> get ('menu_class_def'))
				$this -> menu -> default_class = $this -> ini -> get ('menu_class_def');
			if ($this -> ini -> get ('menu_cur_sub_id'))
				$this -> menu -> current_sub_id = $this -> ini -> get ('menu_cur_sub_id');
			if ($this -> ini -> get ('menu_cur_id_def'))
				$this -> menu -> templates [0 /*default*/] ['current_id'] = 'id="' . $this -> ini -> get ('menu_cur_id_def') . '"';
			if ($this -> ini -> get ('menu_bullet_src'))
				$this -> menu -> bullet_src = $this -> ini -> get ('menu_bullet_src');
			$this -> menu -> set_current_args (array ($this -> ini -> get ('page_id_key') => $this -> page_id, $this -> ini -> get ('language_id_key') => $this -> language));
			$this -> menu -> menu = $this -> menu -> find_current ($this -> menu -> menu, $current_sub);
//			$this -> menu -> templates ['list'] = array ('menu' => array ("\r\n\t{indent}<div>", "\r\n\t{indent}</div>"), 'entry' => array ("\r\n\t\t{indent}", ''));
		}
	}
	function set_sub_menu ($parent_id = 0) // ↑ helper ↑ //
	{
		$submenu = array ();
		$result = $this -> db -> select ('menu', "`parent_id` = $parent_id and `rights` like '%{$this -> user_category}%'", '*', 'position, menu_id');
		while ($element = $this -> db -> fetch_array ($result)) {
			$element ['legend'] = $element ['legend_' . $this -> language];
			$element ['title'] = $element ['title_' . $this -> language];
			$element ['args'] = array ($this -> ini -> get ('page_id_key') => $element ['page_id'], $this -> ini -> get ('language_id_key') => $this -> language);
			$element ['sub'] = $this -> set_sub_menu ($element ['menu_id']);
			$submenu [] = $element;
		}
		return $submenu;
	}
	function make_menu () { // Copyright by Mateusz Rusin © 2005 // 2.03
		return "\r\n\t<div id=\"" . ($this -> ini -> get ('menu_id') ? $this -> ini -> get ('menu_id') : 'menu') . "\">" . $this -> menu -> render () . "\r\n\t</div>";
	}
	function make_main_menu () { // Copyright by Mateusz Rusin © 2005 // 2.5
//echo'<pre>';var_dump($this -> menu );echo'</pre>';
		$main_menu = $this -> menu -> menu;
		array_walk ($main_menu ['sub'], array (&$this, 'remove_sub'));
//echo'<pre>';var_dump($main_menu);echo'</pre>';
//		return "\r\n\t<div id=\"main_menu\">" . $this -> menu -> render ($main_menu) . "\r\n\t</div>";
		return "\r\n\t<div id=\"" . ($this -> ini -> get ('main_menu_id') ? $this -> ini -> get ('main_menu_id') : 'main_menu') . "\">" . $this -> menu -> render ($main_menu, $this -> ini -> get ('main_menu_type')) . "\r\n\t</div>";
	}
	function remove_sub (&$element, $key) { // ↑ helper ↑ //
		unset ($element ['sub']);
	}
	function make_sub_menu () { // Copyright by Mateusz Rusin © 2005 // 2.5
//		$this -> heads = "<style type=\"text/css\">\r\n#main_frame\r\n{\r\n\twidth: 100%;\r\n}\r\n\</style>";
		foreach ($this -> menu -> menu ['sub'] as $sub_menu)
			if (!empty ($sub_menu ['current_sub']) || !empty ($sub_menu ['current'])) 
				if (!empty ($sub_menu ['sub'])) {
//					$this -> heads = '';
					return "\r\n\t<div id=\"" . ($this -> ini -> get ('sub_menu_id') ? $this -> ini -> get ('sub_menu_id') : 'sub_menu') . "\">" . $this -> menu -> render ($sub_menu) . "\r\n\t</div>";
				}			
			return '';
	}
	function replace_tags ($text, $tags, $prefix = '{', $suffix = '}') { // Copyright by Mateusz Rusin © 2005 // 1.1
		foreach ($tags as $tag => $value)
			$text_tags [$prefix . $tag . $suffix] = $value;
		return str_replace (array_keys ($text_tags), array_values ($tags), $text);
	}
/*	function replace_tags ($stóg_siana, $tags, $prefiks = '{', $sufiks = '}') { // Copyright by Mateusz Rusin © 2005 // 1.02
		foreach ($tags as $tag_do_zamiany => $na_co_zamienić)
			$stóg_siana = str_replace ($prefiks . $tag_do_zamiany . $sufiks, $na_co_zamienić, $stóg_siana);
		return $stóg_siana;
	}
*/	function add_form ($inputs, $name = null, $action = null, $method = 'post', $end_inputs_count = 0) // Copyright by Mateusz Rusin © 2005 // 1.01
	{
		$form = "\r\n<form " . (isset ($name) ? 'id="' . $name . '" name="' . $name . '"' : '') . 'method="' . $method . '"' . (isset ($action) ? ' action="' . $action . '">' : '>');
		$i = count ($inputs) - $end_inputs_count;	// liczba zwykłych inputów
		foreach ($inputs as $input_data)
		{
			if ($i--)												//	jeśli zwykły input
				$input_code = & $form;				//	to twórz formularz
			else														//	a jeśli końcowy
				$input_code = & $end_inputs;	//	to twórz końcówkę formularza
			$input_code .= $this -> make_input ($input_data);
		}
		$form .= (isset ($end_inputs) ? $end_inputs . '<hr />' : '') . "\r\n</form>";
		$this -> text .= $form;
	}
	function make_input ($input_data) // Copyright by Mateusz Rusin © 2005 // 1.01
	{
		//$name;
		//$label;
		$title = '';
		$max_length = 0;
		$value = '';
		$class= '';
		$type = 'text';
		$checked = false;
		foreach ($input_data as $data_name => $data_value)
			$$data_name = $data_value;
		$id = $name;
		switch ($type)
		{
			case 'select':
			case 'option':
				$input = '
					<label for="' . $id . '" class="' . $class . '" title="' . $title . '">' . $label . '
						<select id="' . $id . '" name="' . $name . '" class="' . $class . '" size="' . $max_length . '" accesskey="' . menu :: access_key ($label) . '">';
				foreach ($value as $v)
					$input .= '<option value="' . $v ['value'] . (($v ['selected']) ? '" selected>' : '">') . $v ['name'];
				$input .= '</select></label>';
				break;
			case 'button':
				$input = '<button name="' . $name . '" value="' . $value . '" class="' . $class . '" type="submit">' . $label . '</button>';
				break;
			case 'link':
				$input = '
					<label class="' . $class . '" title="' . $title . '">' . $label . '
						<a href="' . $name . '" class="' . ($class ? $class : 'button') . '" accesskey="' . menu :: access_key ($label) . '">' . $value . '</a>
					</label>';
				break;
			case 'hidden':
				$input = '<input name="' . $name . '" type="' . $type . '" value="' . $value . '">';
				break;
			case 'textarea':
				$input = '
					<label for="' . $id . '" title="' . $title . '" class="' . $class . '">' . $label . '
						<textarea id="' . $id . '" name="' . $name . '" class="' . $class . '" accesskey="' . menu :: access_key ($label) . '">' .
						$value . '
						</textarea>
					</label>';
				break;			
			case 'file':
				$input = '
						<input name="MAX_FILE_SIZE" type="hidden" value="' . $max_length . '">
					<label for="' . $id . '" class="' . $class . '" title="' . $title . '">' . $label . '
						<input id="' . $id . '" name="' . $name . '" type="' . $type . '" value="' . $value . '" class="' . $class . '" accesskey="' . menu :: access_key ($label) . '">
					</label>';
				break;			
			case 'checkbox':
			case 'radio':
				$id .= $value;
				if ($value)
					$value = ' checked';
				$input = '
					<label for="' . $id . '" class="' . $class . '" title="' . $title . '">
						<input id="' . $id . '" name="' . $name . '" type="' . $type . '" class="checkbox" accesskey="' . menu :: access_key ($label) . '"' . $value . (($max_length == 'disabled' || $max_length === false) ? ' disabled' : '') . '>
						' . $label . '</label>';
				break;			
			default:
				$input = '
					<label for="' . $id . '" class="' . $class . '" title="' . $title . '">' . $label . '
						<input id="' . $id . '" name="' . $name . '" type="' . $type . '" maxlength="' . $max_length . '" value="' . $value . '" class="' . $class . '" accesskey="' . menu :: access_key ($label) . '"' . (($max_length == 'disabled' || $max_length === false) ? ' disabled' : '') . '>
					</label>';
		}
		return $input;
	}
	function opens_log () // Copyright by Mateusz Rusin © 2005 // 3.06
	{
		$address = 'http://' . $_SERVER ['HTTP_HOST'] . str_replace ('index.php', '', (isset ($_SERVER ['REQUEST_URI']) ? $_SERVER ['REQUEST_URI'] : $_SERVER ["SCRIPT_NAME"] . (!empty ($_SERVER ['QUERY_STRING']) ? '?' . $_SERVER ['QUERY_STRING'] : '')));
		$this -> db -> query ("update /*! low_priority */ `pages` set `modyfication_date` = `modyfication_date`, `open_count` = `open_count` + 1" . (empty ($this -> agent -> crawler) ? '' : ', `crawler_open_count` = `crawler_open_count` + 1') . " where `page_id` = '{$this -> page_id}'");
		$this -> db -> query ("update /*! low_priority */ `profiles` set `open_count` = `open_count` + 1 where `profile_id` = {$this -> profile_id}");
		$this -> db -> query ("update /*! low_priority */ `agents` set `open_count` = `open_count` + 1 where `agent_id` = {$this -> agent_id}");
		$this -> db -> query ("update /*! low_priority */ `agents_types` set `open_count` = `open_count` + 1 where `agent_type_id` = {$this -> agent_type_id}");
		$result = $this -> db -> select ('net_addresses', "`address` = '" . $address . "'");
		if ($this -> db -> num_rows ($result)) {
			$current_address_id = $this -> db -> fetch_object ($result);
			$current_address_id = (int) $current_address_id -> address_id;
		} else {
			$this -> db -> insert ('net_addresses', array ('address' => $address));
			$current_address_id = (int) $this -> db -> insert_id ();
		}
		if (!empty ($_SERVER ['HTTP_REFERER'])) {
			$result = $this -> db -> select ('net_addresses', "`address` = '" . $_SERVER ['HTTP_REFERER'] . "'");
			if ($this -> db -> num_rows ($result)) {
				$referer_address_id = $this -> db -> fetch_object ($result);
				$referer_address_id = (int) $referer_address_id -> address_id;
			} else {
				$this -> db -> insert ('net_addresses', array ('address' => $_SERVER ['HTTP_REFERER']));
				$referer_address_id = (int) $this -> db -> insert_id ();
			}
		}
		$this -> db -> insert (
			'/*! delayed */ `opens_history`', 
			array (
				'agent_id' => $this -> agent_id, 
				'profile_id' => $this -> profile_id, 
				'page_id' => $this -> page_id, 
				'ip' => ip2long ($_SERVER ['REMOTE_ADDR']),
				'agent_type_id' => $this -> agent_type_id, 
				'address_id' => $current_address_id,
				'referer_id' => !empty ($referer_address_id) ? $referer_address_id : 0
			) 
		);
		if ($this -> page_id == $this -> ini -> get ('search_page_id') && !empty ($_GET [$this -> ini -> get ('query_page_key')]))
			$this -> db -> insert (
				'/*! delayed */ `queries_history`', 
				array (
					'agent_id' => $this -> agent_id, 
					'profile_id' => $this -> profile_id, 
					'query' => $_GET [$this -> ini -> get ('query_page_key')], 
					'ip' => ip2long ($_SERVER ['REMOTE_ADDR']),
					'agent_type_id' => $this -> agent_type_id
				) 
			);
	}
	function fucking_wizja ()
	{
		return intval (substr (str_replace (array ('.', '_'), '', phpversion ()), 0, 2)) < 41;
	}
}
class ini // Copyright by Mateusz Rusin © 2005 // 1.11
{
	var $ini;
	function ini ($name, $key = null, $value = null, $db = null)
	{echo $db -> error;
		if (isset ($db) && empty ($db -> error))
		{
			$result = $db -> select (basename ($name), '', array ($key, $value));
			while ($ini = $db -> fetch_object ($result)) 
				$this -> ini [$ini -> $key] = $ini -> $value;
		}
		else
			$this -> ini = parse_ini_file ($name);
	}
	function get ($key)
	{
		if (isset ($this -> ini [$key]))
			return $this -> ini [$key];
		else
			return false;
	}
	function set ($key, $value)
	{
		$this -> ini [$key] = $value;
	}
}
class User // Copyright by Mateusz Rusin 2004 // 1.11
{
	var $user;
	var $status;
	var $db;
	function User ($db, $user_id = null, $password = null)
	{
		$this -> db = $db;
		$this -> user =& $_SESSION ['user'];
		if (isset ($user_id, $password)) 
			$this -> LogIn ($user_id, $password);
	}
	function SetStatus ($status)
	{
		$this -> status = $status;
	}
	function GetStatus ()
	{
		return $this -> status;
	}
	function CheckStatus ($user_id = '', $password = '')
	{
		if (!empty ($_SESSION ['user']))
		{
			$this -> SetStatus ('logged');
			return ($_SESSION ['user']);
		}
		else
		{
			if ($user_id && $password)
			{
				if ($this -> LogIn ($user_id, $password))
					return ($_SESSION ['user']);
				else
					return false;
			}
			$this -> SetStatus ('not logged');
			return false;
		}
	}
	function LogOut ()
	{
		unset ($_SESSION ['user']);
	}
	function LogIn ($user, $password) 
	{
		if ($user && $password)
		{
			$result = $this -> db -> Select ('users', "`user_id` = '$user' and `password` = md5('$password')");
			if ($this -> db -> Num_Rows ($result) > 0) 
				$_SESSION ['user'] = $this -> db -> Fetch_Object ($result);
		}
		if (!empty ($_SESSION ['user']))
		{
			$this -> SetStatus ('logged');
			return $_SESSION ['user'];
		}
		else
		{
			if (!empty ($user))
			{
				$result = $this -> db -> Select ('users', "`user_id` = '$user'");
				if ($this -> db -> Num_Rows ($result) > 0)
				{
					$this -> SetStatus ('invalid password');
					return false;
				}
				else
				{
					$this -> SetStatus ('invalid user');
					return false;
				}
			}
			else
			{
				$this -> SetStatus ('no user');
				return false;
			}
		}
	}
}
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
?>