<?php
if (!defined("MODULE_FILE") && !defined("ADMIN_FILE")) die("Illegal File Access");

define("BLOCK_FILE", true);
define("FUNC_FILE", true);
include("config/config_global.php");
include("config/config_users.php");
include("config/config.php");
include("function/security.php");
include("function/mysql.php");

if (defined("MODULE_FILE")) {
	if (file_exists("config/config_function.php")) include("config/config_function.php");
	include("function/user.php");
} elseif (defined("ADMIN_FILE")) {
	include("function/secure.php");
	include("function/admin.php");
}

// Old filter
include("function/old_filters.php");

// Zended function
include("function/zend.php");


// Format head
function head() {
	global $prefix, $db, $home, $index, $conf, $confs, $user, $admin, $name, $bodytext, $hometext, $pagetitle, $key_words;
	if ((!defined("ADMIN_FILE") && $conf['cache'] == 1) || (!defined("ADMIN_FILE") && $conf['cache'] == 2 && $home)) {
		$url = str_replace("/", "", $_SERVER['REQUEST_URI']);
		$url = (!$url) ? "index.php" : $url;
		$match = preg_match("/index/", $url);
		if ($match && !is_user() && !is_admin()) {
			$cacheurl = "config/cache/".md5($url).".txt";
			if (file_exists($cacheurl) && filesize($cacheurl) != 0 && (time() - $conf['cache_t']) < filemtime($cacheurl)) {
				readfile($cacheurl);
				exit;
			}
		}
	}
	if (!defined("ADMIN_FILE") && $conf['close']) {
		if (!is_admin()) get_exit(""._CLOSE_TEXT."", 0);
	}
	if ($conf['session']) {
		$ip = getip();
		$url = htmlspecialchars(getenv("REQUEST_URI"));
		if ($confs['flood'] && (isset($_GET) || isset($_POST))) {
			$ftime = time() - $confs['flood_t'];
			list($flood) = $db->sql_fetchrow($db->sql_query("SELECT Count(uname) FROM ".$prefix."_session WHERE host_addr = '$ip' AND time > '$ftime'"));
			if (isset($_POST) && $flood) {
				$a = 0;
				foreach ($_POST as $var_name=>$var_value) {
					if ($a == 0) {
						$info = "".$var_name."=".$var_value."";
					} else {
						$info .= ", ".$var_name."=".$var_value."";
					}
					$a++;
				}
				if ($a) warn_report("Flood in POST - ".$info."");
			}
			if ($confs['flood'] == 2 && isset($_GET) && $flood) {
				$a = 0;
				foreach ($_GET as $var_name=>$var_value) {
					if ($a == 0) {
						$info = "".$var_name."=".$var_value."";
					} else {
						$info .= ", ".$var_name."=".$var_value."";
					}
					$a++;
				}
				if ($a) warn_report("Flood in GET - ".$info."");
			}
		}
		if (is_admin()) {
			$uname = text_filter(substr($admin[1], 0, 25), 1);
			$guest = 3;
		} elseif (!defined("ADMIN_FILE") && is_user()) {
			$uname = text_filter(substr($user[1], 0, 25), 1);
			$guest = 2;
		} elseif (!defined("ADMIN_FILE") && !is_user()) {
			$bots = explode(",", $conf['bots']);
			for ($i = 0; $i < count($bots); $i++) {
				list($uagent, $bname) = explode("=", $bots[$i]);
				if (preg_match("/$uagent/i", getagent())) {
					$uname = text_filter(substr($bname, 0, 25), 1);
					$guest = 1;
					break;
				} else {
					$uname = $ip;
					$guest = 0;
				}
			}
		}
		$past = time() - intval($conf['sess_t']);
		$db->sql_query("DELETE FROM ".$prefix."_session WHERE time < '$past'");
		$ctime = time();
		if ($uname) {
			$name = addslashes($name);
			$db->sql_query("UPDATE ".$prefix."_session SET uname='$uname', time='$ctime', host_addr='$ip', guest='$guest', module='$name', url='$url' WHERE uname='$uname'");
			$e = @mysql_info();
			preg_match("/^\D+(\d+)/", $e, $matches);
			if ($matches[1] == 0) $db->sql_query("INSERT INTO ".$prefix."_session (uname, time, host_addr, guest, module, url) VALUES ('$uname', '$ctime', '$ip', '$guest', '$name', '$url')");
		}
	}
	$ThemeSel = get_theme();
	if (file_exists("templates/$ThemeSel/index.php")) {
		include("templates/$ThemeSel/index.php");
	} else {
		include("function/template.php");
	}
	$defis_dec = urldecode($conf['defis']);
	$index = file_get_contents(get_theme_file("index"));
	$index = str_replace("{%LICENSE%}", 'Powered By <a href="http://antislaed.net.ru" target=_blank title="AntiSlaed Team">ANTISLAED 3.5</a>: <a href="http://sunvas.info">Sunvas</a>\'s release. Copyleft &copy; 2006-2008', $index);
	preg_match("#^(.*){%MODULE%}#iUs", $index, $head);
	$head = (isset($head[1])) ? $head[1] : die("Error in Head!");
	preg_match("#{%MODULE%}(.*)$#iUs", $index, $index);
	$index = (isset($index[1])) ? $index[1] : die("Error in Foot!");
	$strhead = "<meta http-equiv=\"content-type\" content=\"text/html; charset="._CHARSET."\">\n";
	if ($home) {
		$strhead .= "<title>".$conf['sitename']." $defis_dec ".$conf['slogan']."</title>\n";
	} else {
		$ptitle_dec = explode($conf['defis'], $pagetitle);
		$i = 0;
		$pagetitle_dec = "";
		foreach ($ptitle_dec as $val) $i++;
		foreach ($ptitle_dec as $val) {
			if ($val) $pagetitle_dec .= "".trim($ptitle_dec[$i])." ".$defis_dec." ";
			$i--;
		}
		$strhead .= "<title>".$pagetitle_dec."".$conf['sitename']."</title>\n";
	}
	if (!defined("ADMIN_FILE")) {
		include("function/no-cache.php");
		$strhead .= "<meta name=\"resource-type\" content=\"document\">\n"
		."<meta name=\"document-state\" content=\"dynamic\">\n"
		."<meta name=\"distribution\" content=\"global\">\n"
		."<meta name=\"author\" content=\"".$conf['sitename']."\">\n"
		."<meta name=\"copyright\" content=\"Copyright (c) ".$conf['version']."\">\n";
		$pagetitle = trim(str_replace(" ".$conf['defis']." ", ", ", $pagetitle));
		$pagetitle = trim(str_replace("".$conf['defis']." ", "", $pagetitle));
		if ((($hometext == "") && ($bodytext == "")) || ($conf['keywords_s'] == "0")) {
			$strhead .= "<meta name=\"keywords\" content=\"$pagetitle, ".$conf['keywords']."\">\n"
			."<meta name=\"description\" content=\"".$conf['slogan'].", $pagetitle.\">\n";
		} else {
			$key_words = "$pagetitle";
			$keywords_gen = "$hometext $bodytext";
			$keywords_gen = text_filter(bb_decode($keywords_gen, ""), 1);
			$keywords_gen = trim(preg_replace("/[^a-zA-Zà-ÿÀ-ß0-9]/", " ", $keywords_gen));
			$keywords_gen = preg_replace("/( |".CHR(10)."|".CHR(13).")+/", ",", $keywords_gen);
			$keywords_gen = substr($keywords_gen, 0, 1600);
			$keywords_gen = array_unique(explode(",", $keywords_gen));
			for ($a = 0, $b = 7; $a < sizeof($keywords_gen) && $b < 800; $a++) {
				if (($c = strlen($keywords_gen[$a])) > 3) {
					$key_words = "".$key_words.", ".$keywords_gen[$a]."";
					$b += $c+2;
				}
			}
			$strhead .= "<meta name=\"keywords\" content=\"$key_words\">\n"
			."<meta name=\"description\" content=\"".$conf['slogan'].", $pagetitle.\">\n";
		}
		$strhead .= "<meta name=\"robots\" content=\"index, follow\">\n"
		."<meta name=\"revisit-after\" content=\"1 days\">\n"
		."<meta name=\"rating\" content=\"general\">\n"
		."<meta name=\"generator\" content=\"".$conf['version']."\">\n"
		."<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"favicon.ico\" >\n";
		include("config/config_rss.php");
		if ($confrs['act']) {
			$fieldc = explode("||", $confrs['rss']);
			foreach ($fieldc as $val) {
				if ($val != "") {
					preg_match("#(.*)\|(.*)\|(.*)#i", $val, $out);
					if ($out[1] != "0" && $out[2] != "0" && $out[3] == "1") {
						$strhead .= "<link rel=\"alternate\" type=\"application/rss+xml\" href=\"".$out[2]."\" title=\"".$out[1]."\">\n";
					}
				}
			}
		}
		$strhead .= "<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"".$conf['homeurl']."/search.php\" title=\"".$conf['sitename']." - Search\">\n";
	}
	$strhead .= "<link rel=\"stylesheet\" href=\"templates/$ThemeSel/style.css\" type=\"text/css\">\n"
	."<script language=\"JavaScript\" type=\"text/javascript\" src=\"ajax/GlobalFunc.js\"></script>\n";
	if (!$confs['error_java']) $strhead .= "<script language=\"JavaScript\" type=\"text/javascript\" src=\"ajax/BlockError.js\"></script>\n";
	if (!defined("ADMIN_FILE")) {
		if (file_exists("config/config_header.php")) {
			ob_start();
			include("config/config_header.php");
			$strhead .= ob_get_contents();
			ob_end_clean();
		}
	} else {
		if ($conf['redaktor'] == 2 && file_exists("config/config_header.php")) {
			ob_start();
			include("config/config_redaktor.php");
			$strhead .= ob_get_contents();
			ob_end_clean();
		}
	}
	$head = addblocks($head);
	$head = str_replace('{%HEAD%}', $strhead, $head);
	themeheader($head);
	unset($head, $strhead);
	if (!defined("ADMIN_FILE")) update_points(31);
}

// Format foot
function foot() {
	global $home, $module, $name, $index, $conf, $do_gzip_compress;
	$index = addblocks($index);
	if ($module == 1 && file_exists("modules/$name/copyright.php")) {
		$index = "<div align=\"right\"><a href=\"javascript:OpenWindow('modules/$name/copyright.php', 'Copyright', '400', '200')\">".str_replace("_", " ", $name)." &copy;</a></div>".$index."";
	}
	themefooter($index);
	unset($index);
	newsletter_send();
	if (!defined("ADMIN_FILE") && $conf['rewrite']) rewrite();
	if ((!defined("ADMIN_FILE") && $conf['cache'] == 1) || (!defined("ADMIN_FILE") && $conf['cache'] == 2 && $home)) {
		$url = str_replace("/", "", $_SERVER['REQUEST_URI']);
		$url = (!$url) ? "index.php" : $url;
		$match = preg_match("/index/", $url);
		if ($match && !is_user() && !is_admin()) {
			$fp = fopen("config/cache/".md5($url).".txt", "w");
			fwrite($fp, ob_get_contents());
			fclose($fp);
		}
	}
	if ($conf['gzip']) {
		if ($do_gzip_compress) {
			$gzip_contents = ob_get_contents();
			ob_end_clean();
			$gzip_size = strlen($gzip_contents);
			$gzip_crc = crc32($gzip_contents);
			$gzip_contents = gzcompress($gzip_contents, 9);
			$gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4);
			echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
			echo $gzip_contents;
			echo pack('V', $gzip_crc);
			echo pack('V', $gzip_size);
		}
	}
	ob_end_flush();
	exit;
}

// Format Time filter
function formatTimestamp($time) {
	global $datetime;
	setlocale(LC_TIME, ""._LOCALE."");
	preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $time, $datetime);
	$datetime = date(""._DATESTRING."", mktime($datetime[4],$datetime[5],$datetime[6],$datetime[2],$datetime[3],$datetime[1]));
	$datetime = ucfirst($datetime);
	return $datetime;
}

// Display Time filter
function displayTime($sec) {
	$minutes = floor($sec / 60);
	$seconds = $sec % 60;
	$content = ($minutes == 0) ? "".$seconds." "._SEC."." : "".$minutes." "._MIN.". ".$seconds." "._SEC.".";
	return $content;
}

// Size filter
function files_size($size) {
	$name = array("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
	$mysize = $size ? "".round($size / pow(1024, ($i = floor(log($size, 1024)))), 2)." ".$name[$i]."" : "".$size." Bytes";
	return $mysize;
}

// Format new graphic
function new_graphic($data) {
	$data = mktime() - $data;
	if ($data < 86400) $img = "<img src=\"".find_img("new_day.gif")."\" alt=\""._NEWTODAY."\" title=\""._NEWTODAY."\">";
	if (($data > 86400) && ($data < 259200)) $img = "<img src=\"".find_img("new_3day.gif")."\" alt=\""._NEWLAST3DAYS."\" title=\""._NEWLAST3DAYS."\">";
	if (($data > 259200) && ($data < 604800)) $img = "<img src=\"".find_img("new_week.gif")."\" alt=\""._NEWTHISWEEK."\" title=\""._NEWTHISWEEK."\">";
	return $img;
}

// Format categories
function categories($mod, $tab, $sub, $desc, $id="") {
	global $prefix, $db, $conf, $currentlang;
	if (!preg_match("/[^a-zA-Z0-9_]/", $mod)) {
		$id = (intval($id)) ? $id : 0;
		if ($id) {
			$where = "WHERE modul='$mod' AND parentid = '$id'";
		} elseif ($id && $conf['multilingual']) {
			$where = "WHERE modul='$mod' AND parentid = '$id' AND (language='$currentlang' OR language='')";
		} elseif ($conf['multilingual']) {
			$where = "WHERE modul='$mod' AND (language='$currentlang' OR language='')";
		} else {
			$where = "WHERE modul='$mod'";
		}
		$tdwidth = intval(100/$tab);
		$cat_num = 0;
		$result = $db->sql_query("SELECT id, title, description, img, parentid FROM ".$prefix."_categories ".$where." ORDER BY title");
		while (list($cid, $title, $description, $img, $parentid) = $db->sql_fetchrow($result)) {
			$massiv[] = array($cid, $title, $description, $img, $parentid);
			$cat_num++;
		}
		if ($massiv) {
			$a = 0;
			foreach ($massiv as $val) {
				if ($val[4] == $id) {
					if ($a == 0) {
						$catid = $val[0];
						$a++;
					} else {
						$catid .= ",".$val[0]."";
					}
					if ($val[3]) {
						$description = ($desc) ? "<br /><i>".$val[2]."</i>" : "";
						$ccontent .= "<td valign=\"top\" width=\"".$tdwidth."%\"><table width=\"100%\" border=\"0\"><tr><td><a href=\"index.php?name=$mod&cat=$val[0]\" title=\"$val[1]\"><img src=\"images/categories/".$val[3]."\" border=\"0\" title=\"".$val[1]."\"></a></td><td width=\"100%\"><a href=\"index.php?name=$mod&cat=$val[0]\" title=\"$val[1]\"><b>$val[1]</b></a>".$description."</td></tr>";
					} else {
						$description = ($desc) ? "<tr><td colspan=\"2\"><i>".$val[2]."</i></td></tr>" : "";
						$ccontent .= "<td valign=\"top\" width=\"".$tdwidth."%\"><table width=\"100%\" border=\"0\"><tr><td><a href=\"index.php?name=$mod&cat=$val[0]\" title=\"$val[1]\"><img src=\"images/blocks/".$mod.".gif\" border=\"0\" title=\"".$val[1]."\"></a></td><td width=\"100%\"><a href=\"index.php?name=$mod&cat=$val[0]\" title=\"$val[1]\"><b>$val[1]</b></a></td></tr>".$description."";
					}
					foreach ($massiv as $val2) {
						if ($val[0] == $val2[4]) {
							$catid .= ",".$val2[0]."";
							if ($sub == 1) $ccontent .= "<tr><td colspan=\"2\"><img border=\"0\" src=\"images/navi.gif\" title=\"$val2[1]\"> <a href=\"index.php?name=$mod&cat=$val2[0]\" title=\"$val2[1]\">$val2[1]</a></td></tr>";
						}
					}
					$ccontent .= "</table></td>";
					if ($cont == ($tab - 1)) {
						$ccontent .= "</tr><tr>";
						$cont = 0;
					} else {
						$cont++;
					}
				}
			}
		}
		if ($ccontent) {
			if ($mod == "FAQ") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(fid) FROM ".$prefix."_faq WHERE catid IN ($catid) AND time <= now() AND status!='0'"));
				$in = ""._INFA."";
			} elseif ($mod == "Files") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(lid) FROM ".$prefix."_files WHERE cid IN ($catid) AND date <= now() AND status!='0'"));
				$in = ""._INF."";
			} elseif ($mod == "Jokes") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(jokeid) FROM ".$prefix."_jokes WHERE cat IN ($catid) AND date <= now() AND status!='0'"));
				$in = ""._INJ."";
			} elseif ($mod == "Links") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(lid) FROM ".$prefix."_links WHERE cid IN ($catid) AND date <= now() AND status!='0'"));
				$in = ""._INL."";
			} elseif ($mod == "Media") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(id) FROM ".$prefix."_media WHERE cid IN ($catid) AND date <= now() AND status!='0'"));
				$in = ""._INM."";
			} elseif ($mod == "News") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(sid) FROM ".$prefix."_stories WHERE catid IN ($catid) AND time <= now() AND status!='0'"));
				$in = ""._INN."";
			} elseif ($mod == "Pages") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(pid) FROM ".$prefix."_page WHERE catid IN ($catid) AND time <= now() AND status!='0'"));
				$in = ""._INP."";
			} elseif ($mod == "Shop") {
				list($pages_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(product_id) FROM ".$prefix."_products WHERE product_cid IN ($catid) AND product_time <= now() AND product_active !='0'"));
				$in = ""._INS."";
			}
			OpenTable();
			echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"10\" align=\"center\"><tr>".$ccontent."</td></tr></table>";
			echo "<hr><center>"._ALLIN.": <b>".$pages_num."</b> ".$in." <b>".$cat_num."</b> "._ALLINC."</center>";
			CloseTable();
		}
	}
}

// Format categories select
function getcat($mod, $id="") {
	global $prefix, $db;
	if (!preg_match("/[^a-zA-Z0-9_]/", $mod)) {
		$content = ($id == "no") ? "" : "<option value=\"\">"._HOMECAT."</option>";
		$result = $db->sql_query("SELECT id, title, parentid FROM ".$prefix."_categories WHERE modul='$mod'");
		if ($db->sql_numrows($result) > 0) {
			while (list($cid, $title, $parentid) = $db->sql_fetchrow($result)) $massiv[$cid] = array($title, $parentid);
			foreach ($massiv as $key => $val) {
				$cont[$key] = $val[0];
				$flag = $val[1];
				while ($flag != "0") {
					$cont[$key] = $massiv[$flag][0]." / ".$cont[$key];
					$flag = $massiv[$flag][1];
				}
			}
			asort($cont);
			foreach ($cont as $key => $val) {
				$sel = ($id == $key) ? "selected" : "";
				$content .= "<option value=\"$key\" $sel>$val</option>";
			}
		}
		return $content;
	}
}

// Format categories parent
function getparent($id, $title) {
	global $prefix, $db;
	if (intval($id)) {
		$result = $db->sql_query("SELECT title, parentid FROM ".$prefix."_categories WHERE id='$id'");
		list($ptitle, $pid) = $db->sql_fetchrow($result);
		if ($ptitle) $title = "".$ptitle." / ".$title."";
		if ($pid) $title = getparent($pid, $title);
		return $title;
	}
}

// Lang filter
function cutstr($linkstrip, $strip) {
	$linkstrip = stripslashes($linkstrip);
	if (strlen($linkstrip) > $strip) $linkstrip = "".substr($linkstrip, 0, $strip)."...";
	return $linkstrip;
}

// Analyzer of variables
function variables() {
	if ($_GET) {
		echo "<br /><br /><font color=\"blue\"><b>GET</b></font> - ";
		$a = 0;
		foreach ($_GET as $var_name => $var_value) {
			if ($a == 0) {
				echo "<b>".$var_name."</b>=".$var_value."";
			} else {
				echo ", <b>".$var_name."</b>=".$var_value."";
			}
			$a++;
		}
	}
	if ($_POST) {
		echo "<br /><br /><font color=\"blue\"><b>POST</b></font> - ";
		$a = 0;
		foreach ($_POST as $var_name => $var_value) {
			$var_value = is_array($var_value) ? fields_save($var_value) : $var_value;
			if ($a == 0) {
				echo "<b>".$var_name."</b>=".htmlspecialchars($var_value)."";
			} else {
				echo ", <b>".$var_name."</b>=".htmlspecialchars($var_value)."";
			}
			$a++;
		}
	}
	if ($_COOKIE) {
		echo "<br /><br /><font color=\"blue\"><b>COOKIE</b></font> - ";
		$a = 0;
		foreach ($_COOKIE as $var_name => $var_value) {
			if ($a == 0) {
				echo "<b>".$var_name."</b>=".$var_value."";
			} else {
				echo ", <b>".$var_name."</b>=".$var_value."";
			}
			$a++;
		}
	}
	if ($_FILES) {
		echo "<br /><br /><font color=\"blue\"><b>FILES</b></font> - ";
		$a = 0;
		foreach ($_FILES as $var_name => $var_value) {
			if ($a == 0) {
				echo "<b>".$var_name."</b>=".$var_value."";
			} else {
				echo ", <b>".$var_name."</b>=".$var_value."";
			}
			$a++;
		}
	}
}

// Check module
function is_active($module) {
	global $prefix, $db;
	static $name;
	if (!is_array($name)) {
		$result = $db->sql_query("SELECT title FROM ".$prefix."_modules WHERE active='1'");
		while (list($title) = $db->sql_fetchrow($result)) $name[] = $title;
	}
	foreach ($name as $val) {
		if ($val == $module) {
			$a = 1;
			break;
		} else {
			$a = 0;
		}
	}
	return $a;
}

// Rewrite mod
function rewrite() {
	$contents = ob_get_contents();
	ob_end_clean();
	include("config/config_rewrite.php");
	$rewrite = preg_replace($in, $out, $contents);
	echo $rewrite;
}

// Decode BB
function bb_decode($sourse, $mod) {
	$bb[] = "#\[img\]([^?](?:[^\[]+|\[(?!url))*?)\[/img\]#i";
	$html[] = "<img src=\"\\1\" border=\"0\" alt=\"\\1\" title=\"\\1\" />";
	$bb[] = "#\[img=([a-zA-Z]+)\]([^?](?:[^\[]+|\[(?!url))*?)\[/img\]#is";
	$html[] = "<img src=\"\\2\" align=\"\\1\" border=\"0\" alt=\"\\2\" title=\"\\2\" />";
	$bb[] = "#\[img\ alt=([a-zA-Zà-ÿÀ-ß0-9\_\-\. ]+)\]([^?](?:[^\[]+|\[(?!url))*?)\[/img\]#is";
	$html[] = "<img src=\"\\2\" align=\"\\1\" border=\"0\" alt=\"\\1\" title=\"\\1\" />";
	$bb[] = "#\[img=([a-zA-Z]+) alt=([a-zA-Zà-ÿÀ-ß0-9\_\-\. ]+)\]([^?](?:[^\[]+|\[(?!url))*?)\[/img\]#is";
	$html[] = "<img src=\"\\3\" align=\"\\1\" border=\"0\" alt=\"\\2\" title=\"\\2\" />";
	$bb[] = "#\[url\]([\w]+?://([\w\#$%&~/.\-;:=,?@\]+]+|\[(?!url=))*?)\[/url\]#is";
	$html[] = "<a href=\"\\1\" target=\"_blank\" title=\"\\1\">\\1</a>";
	$bb[] = "#\[url\]((www|ftp)\.([\w\#$%&~/.\-;:=,?@\]+]+|\[(?!url=))*?)\[/url\]#is";
	$html[] = "<a href=\"http://\\1\" target=\"_blank\" title=\"\\1\">\\1</a>";
	$bb[] = "#\[url=([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is";
	$html[] = "<a href=\"\\1\" target=\"_blank\" title=\"\\1\">\\2</a>";
	$bb[] = "#\[url=((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*?)\]([^?\n\r\t].*?)\[/url\]#is";
	$html[] = "<a href=\"http://\\1\" target=\"_blank\" title=\"\\1\">\\3</a>";
	$bb[] = "#\[mail\](\S+?)\[/mail\]#i";
	$html[] = "<a href=\"mailto:\\1\">\\1</a>";
	$bb[] = "#\[mail\s*=\s*([\.\w\-]+\@[\.\w\-]+\.[\w\-]+)\s*\](.*?)\[\/mail\]#i";
	$html[] = "<a href=\"mailto:\\1\">\\2</a>";
	$bb[] = "#\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*?)\[/color\]#si";
	$html[] = "<span style=\"color: \\1\">\\2</span>";
	$bb[] = "#\[family=([A-Za-z ]+)\](.*?)\[/family\]#si";
	$html[] = "<span style=\"font-family: \\1\">\\2</span>";
	$bb[] = "#\[size=([0-9]{1,2}+)\](.*?)\[/size\]#si";
	$html[] = "<span style=\"font-size: \\1\">\\2</span>";
	$bb[] = "#\[(left|right|center|justify)\](.*?)\[/\\1\]#is";
	$html[] = "<div align=\"\\1\">\\2</div>";
	$bb[] = "#\[b\](.*?)\[/b\]#si";
	$html[] = "<b>\\1</b>";
	$bb[] = "#\[i\](.*?)\[/i\]#si";
	$html[] = "<i>\\1</i>";
	$bb[] = "#\[u\](.*?)\[/u\]#si";
	$html[] = "<u>\\1</u>";
	$bb[] = "#\[s\](.*?)\[/s\]#si";
	$html[] = "<s>\\1</s>";
	$bb[] = "#\[li\]#si";
	$html[] = "<li>";
	$bb[] = "#\[hr\]#si";
	$html[] = "<hr>";
	$bb[] = "#\*(\d{2})#";
	$html[] = "<img src=\"images/smilies/\\1.gif\" border=\"0\" />";

	$bb[] = "#javascript:#si";
	$html[] = "Java Script";
	$bb[] = "#about:#si";
	$html[] = "About";
	$bb[] = "#vbscript:#si";
	$html[] = "VB Script";

	$sourse = preg_replace($bb, $html, $sourse);
	if (preg_match("#\[quote\](.*?)\[/quote\]#si", $sourse)) $sourse = encode_quote($sourse);
	if (preg_match("#\[hide\](.*?)\[/hide\]#si", $sourse)) $sourse = encode_hide($sourse);
	if (preg_match("#\[code\](.*?)\[/code\]#si", $sourse)) $sourse = encode_code($sourse);
	if (preg_match("#\[php\](.*?)\[/php\]#si", $sourse)) $sourse = encode_php($sourse);
	if (preg_match("#\[attach=(.*?)\]#si", $sourse)) $sourse = encode_attach($sourse, strtolower($mod));
	return $sourse;
}

// Format hide
function encode_hide($text) {
	$start_html = "<fieldset style=\"width: 95%; overflow: auto;\"><legend style=\"color: red;\">"._HIDE."</legend><div style=\"margin: 3px;\">";
	$end_html = "</div></fieldset>";
	$text = (defined("ADMIN_FILE") || is_user()) ? preg_replace("#\[hide\](.*?)\[/hide\]#si", "".$start_html."\\1".$end_html."", $text) : preg_replace("#\[hide\](.*?)\[/hide\]#si", "".$start_html.""._HIDETEXT."".$end_html."", $text);
	return $text;
}

// Format quote
function encode_quote($text) {
	$start_html = "<fieldset style=\"width: 95%; overflow: auto;\"><legend style=\"color: green;\">"._QUOTE."</legend><div style=\"margin: 3px;\">";
	$end_html = "</div></fieldset>";
	$text = preg_replace("#\[quote\](.*?)\[/quote\]#si", "".$start_html."\\1".$end_html."", $text);
	return $text;
}

// Format code
function encode_code($text) {
	$start_html = "<fieldset style=\"width: 95%; overflow: auto;\"><legend style=\"color: blue;\">"._CODE."</legend><div style=\"margin: 3px;\" class=\"code\">";
	$end_html = "</div></fieldset>";
	$text = preg_replace("#\[code\](.*?)\[/code\]#si", "".$start_html."\\1".$end_html."", $text);
	return $text;
}

// Format PHP code
function encode_php($text) {
	$start_html = "<fieldset style=\"width: 95%; overflow: auto;\"><legend style=\"color: purple;\">PHP - "._CODE."</legend><div style=\"margin: 3px;\">";
	$end_html = "</div></fieldset>";
	$match_count = preg_match_all("#\[php\](.*?)\[/php\]#si", $text, $matches);
	for ($i = 0; $i < $match_count; $i++) {
		$after_replace = trim($matches[1][$i]);
		$after_replace = str_replace("&lt;", "<", $after_replace);
		$after_replace = str_replace("&gt;", ">", $after_replace);
		$after_replace = str_replace("&quot;", '"', $after_replace);
		$after_replace = str_replace("&#039;", "'", $after_replace);
		$after_replace = str_replace("&amp;", "&", $after_replace);
		$after_replace = preg_replace("/<br \/>/i", "", $after_replace);
		$after_replace = (substr($after_replace, 0, 2) != "<?") ? "<?php".$after_replace."" : $after_replace;
		$after_replace = (substr($after_replace, -2) != "?>") ? "".$after_replace."?>" : $after_replace;
		ob_start();
		highlight_string($after_replace);
		$after_replace = ob_get_contents();
		ob_end_clean();
		$text = str_replace("[php]".$matches[1][$i]."[/php]", "".$start_html."".$after_replace."".$end_html."", $text);
	}
	$text = str_replace("&lt;?php", "", $text);
	$text = str_replace("?&gt;", "", $text);
	return $text;
}

// Mail check
function checkemail($mail) {
	global $stop;
	$mail = strtolower(text_filter($mail, 1));
	if ((!$mail) || ($mail=="") || (!preg_match("/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$/", $mail))) $stop = ""._ERROR1."<br />"._ERROR2." (<b>email@domain.com</b>)";
	if ((strlen($mail) >= 4) && (substr($mail, 0, 4) == "www.")) $stop = ""._ERROR1."<br />"._ERROR3." (<b>www.</b>)";
	if (strrpos($mail, " ") > 0) $stop = ""._ERROR1."<br />"._ERROR4.".";
	return $stop;
}

// Format add block
function addblocks($str) {
	global $blocks, $blocks_c, $home, $showbanners, $foot, $db, $start_time, $conf, $foot;
	preg_match_all('#{%BLOCKS([^%]+)%}#iUs', $str, $blk);
	$ci = sizeof($blk[1]);
	for ($i = 0; $i < $ci; $i++) {
		$blk[0][$i] = '#'.$blk[0][$i].'#';
		$telo = trim($blk[1][$i]);
		$pos = strtolower($telo[0]);
		switch($pos) {
			case 'l':
			if ($blocks == "" || $blocks == "0"|| $blocks == "1") {
				ob_start();
				blocks('l');
				$blk[1][$i] = ob_get_contents();
				ob_end_clean();
			} else {
				$blk[1][$i] = "";
			}
			break;
			case 'r':
			if ($blocks == "" || $blocks == "0"|| $blocks == "2") {
				ob_start();
				blocks('r');
				$blk[1][$i] = ob_get_contents();
				ob_end_clean();
			} else {
				$blk[1][$i] = "";
			}
			break;
			case 'c':
			if ($blocks_c == "" || $blocks_c == "0" || $blocks_c == "1") {
				ob_start();
				blocks('c');
				$blk[1][$i] = ob_get_contents();
				ob_end_clean();
			} else {
				$blk[1][$i] = "";
			}
			break;
			case 'd':
			if ($blocks_c == "" || $blocks_c == "0"|| $blocks_c == "2") {
				ob_start();
				blocks('d');
				$blk[1][$i] = ob_get_contents();
				ob_end_clean();
			} else {
				$blk[1][$i] = "";
			}
			break;
			case 'b':
			blocks('b');
			$blk[1][$i] = $showbanners;
			break;
			case 'f':
			blocks('f');
			$blk[1][$i] = $foot;
			break;
			case 'm':
			if ($home == 1) {
				ob_start();
				message_box();
				$blk[1][$i] = ob_get_contents();
				ob_end_clean();
			} else {
				$blk[1][$i] = "";
			}
			break;
			case 't':
			if ($conf['db_t'] == "1") {
				$total_time = round(array_sum(explode(" ", microtime())) - $start_time, 3);
				$sqlnums = $db->num_queries;
				$total_time_db = round($db->total_time_db, 3);
				$total_time = ""._GENERATION.": ".$total_time." "._SEC.". "._AND." ".$sqlnums." "._GENERATION_DB." ".$total_time_db." "._SEC.".";
				$footer = $total_time;
			}
			$blk[1][$i] = $footer;
			break;
			case 'v':
			if ($conf['variables'] == "1" && is_moder()) {
				ob_start();
				variables();
				$blk[1][$i] = "<div align=\"left\">".ob_get_contents()."</div>";
				ob_end_clean();
			} else {
				$blk[1][$i] = "";
			}
			break;
			case 'q':
			if ($conf['db_t_q'] == "1" && is_moder()) {
				$total_time_query = $db->time_query;
				$blk[1][$i] = "<br /><br /><div align=\"left\">".$total_time_query."</div>";
			} else {
				$blk[1][$i] = "";
			}
			break;
			default:
			$telo = explode(",", $telo);
			ob_start();
			blocks($telo[0], $telo[1]);
			$blk[1][$i] = ob_get_contents();
			ob_end_clean();
		}
	}
	return preg_replace($blk[0], $blk[1], $str);
}

// Format block
function blocks($side, $fly="") {
	global $prefix, $db, $conf, $currentlang, $name, $home, $pos, $b_id, $blockfile;
	static $barr;
	$querylang = ($conf['multilingual'] == 1) ? "AND (blanguage='$currentlang' OR blanguage='')" : "";
	$pos = strtolower($side[0]);
	$side = $pos;
	if (!isset($barr)) {
		$result = $db->sql_query("SELECT bid, bkey, title, content, url, blockfile, view, expire, action, bposition, which FROM ".$prefix."_blocks WHERE active='1' $querylang ORDER BY weight ASC");
		while(list($bid, $bkey, $title, $content, $url, $blockfile, $view, $expire, $action, $bposition, $which) = $db->sql_fetchrow($result)) {
			$bid = intval($bid);
			$view = intval($view);
			$where_mas = explode(",", $which);
			$barr[] = array($bid, $bkey, $title, $content, $url, $blockfile, $view, $expire, $action, $bposition, $where_mas);
		}
	}
	if ($fly != "") {
		$b_id = 0;
		$flag = 0;
		$blockfile = "";
		if (false === strpos($fly, "-")) {
			$b_id = intval($fly);
		} else {
			$blockfile = trim($fly);
		}
		$ci = sizeof($barr);
		for ($i = 0; $i < $ci; $i++) {
			if (($b_id != 0 && $barr[$i][0] == $b_id) || ($blockfile != "" && $barr[$i][5] == $blockfile)) {
				list($bid, $bkey, $title, $content, $url, $blockfile, $view, $expire, $action, $bposition, $where_mas) = $barr[$i];
				$b_id = $bid;
				$flag = 1;
				break;
			}
		}
		if ($flag == 1) {
			if (in_array("flyfix", $where_mas)) {
				switch ($where_mas[0]) {
					case "all":
					$flag_where = 1;
					break;
					case "":
					$flag_where = 1;
					break;
					case "infly":
					$flag_where = 0;
					break;
					case "home":
					$flag_where = ($home == 1) ? 1 : 0;
					break;
					case "ihome":
					if ($home == 1) $flag_where = 1;
					default:
					if (!$home){
						foreach ($where_mas as $val) {
							if ($val == $name) $flag_where = 1;
						}
					}
					break;
				}
				if (in_array("otricanie", $where_mas)) $flag_where = ($flag_where) ? 0 : 1;
			} else {
				$flag_where = 1;
			}
			if ($flag_where == 1) {
				if ($view == 0) {
					return render_blocks($side, $blockfile, $title, $content, $bid, $url);
				} elseif ($view == 1 && is_user() || is_moder()) {
					return render_blocks($side, $blockfile, $title, $content, $bid, $url);
				} elseif ($view == 2 && is_moder()) {
					return render_blocks($side, $blockfile, $title, $content, $bid, $url);
				} elseif ($view == 3 && !is_user() || is_moder()) {
					return render_blocks($side, $blockfile, $title, $content, $bid, $url);
				}
			}
		}
	} else {
		$ci = sizeof($barr);
		for ($i = 0; $i < $ci; $i++) {
			if ($barr[$i][9] != $side) continue;
			$flag_where = 0;
			$where_mas = $barr[$i][10];
			switch ($where_mas[0]) {
				case "all":
				$flag_where = 1;
				break;
				case "":
				$flag_where = 1;
				break;
				case "infly":
				$flag_where = 0;
				break;
				case "home":
				$flag_where = ($home == 1) ? 1 : 0;
				break;
				case "ihome":
				if ($home == 1) $flag_where = 1;
				default:
				if (!$home) {
					foreach ($where_mas as $val) {
						if ($val == $name) $flag_where = 1;
					}
				}
				break;
			}
			if (in_array("otricanie", $where_mas)) $flag_where = ($flag_where) ? 0 : 1;
			if ($flag_where == 1) {
				list($bid, $bkey, $title, $content, $url, $blockfile, $view, $expire, $action, $bposition, $where_mas) = $barr[$i];
				$b_id = $bid;
				if ($expire && $expire < time()) {
					if ($action == "d") {
						$db->sql_query("UPDATE ".$prefix."_blocks SET active='0', expire='0' WHERE bid='$bid'");
						return;
					} elseif ($action == "r") {
						$db->sql_query("DELETE FROM ".$prefix."_blocks WHERE bid='$bid'");
						return;
					}
				}
				switch ($bkey) {
					case "admin":
					adminblock();
					break;
					case "userbox":
					userblock();
					break;
					default:
					if ($view == 0) {
						render_blocks($side, $blockfile, $title, $content, $bid, $url);
					} elseif ($view == 1 && is_user() || is_moder()) {
						render_blocks($side, $blockfile, $title, $content, $bid, $url);
					} elseif ($view == 2 && is_moder()) {
						render_blocks($side, $blockfile, $title, $content, $bid, $url);
					} elseif ($view == 3 && !is_user() || is_moder()) {
						render_blocks($side, $blockfile, $title, $content, $bid, $url);
					}
					break;
				}
			}
		}
	}
}

// Format block
function render_blocks($side, $blockfile, $blocktitle, $content, $bid, $url) {
	global $showbanners, $foot;
	if ($url == "") {
		if ($blockfile != "") {
			if (file_exists("blocks/".$blockfile."")) {
				include("blocks/".$blockfile."");
			} else {
				$content = "<center>"._BLOCKPROBLEM."</center>";
			}
		}
		if (!isset($content) || empty($content)) $content = "<center>"._BLOCKPROBLEM2."</center>";
		switch($side) {
			case "b":
			$showbanners = $content;
			break;
			case "f":
			$foot = $content;
			break;
			case "n":
			echo $content;
			break;
			case "p":
			return $content;
			break;
			case "o":
			return themesidebox($blocktitle, $content);
			break;
			default:
			themesidebox($blocktitle, $content);
			break;
		}
	} else {
		rss_load($bid);
	}
}

// Format rating
function rating() {
	global $db, $prefix, $user;
	include("config/config_ratings.php");
	$id = (isset($_GET['id'])) ? intval($_GET['id']) : "";
	$mod = (preg_match("/[^a-zA-Z0-9_]/", $_GET['mod'])) ? "" : $_GET['mod'];
	$rating = (isset($_GET['rating'])) ? intval($_GET['rating']) : 0;
	$con = explode("|", $confra[strtolower($mod)]);
	$mod=ucfirst($mod);
	if ($id && $mod) {
		if ($mod == "Account") {
			$query = "user_votes, user_totalvotes FROM ".$prefix."_users WHERE user_id='$id'";
		} elseif ($mod == "FAQ") {
			$query = "ratings, score FROM ".$prefix."_faq WHERE fid='$id'";
		} elseif ($mod == "Files") {
			$query = "votes, totalvotes FROM ".$prefix."_files WHERE lid='$id'";
		} elseif ($mod == "Jokes") {
			$query = "ratingtot, rating FROM ".$prefix."_jokes WHERE jokeid='$id'";
		} elseif ($mod == "Links") {
			$query = "votes, totalvotes FROM ".$prefix."_links WHERE lid='$id'";
		} elseif ($mod == "Media") {
			$query = "votes, totalvotes FROM ".$prefix."_media WHERE id='$id'";
		} elseif ($mod == "News") {
			$query = "ratings, score FROM ".$prefix."_stories WHERE sid='$id'";
		} elseif ($mod == "Pages") {
			$query = "ratings, score FROM ".$prefix."_page WHERE pid='$id'";
		} elseif ($mod == "Shop") {
			$query = "product_votes, product_totalvotes FROM ".$prefix."_products WHERE product_id='$id'";
			} elseif ($mod == "Music") {
			$query = "votes, totalvotes FROM ".$prefix."_music WHERE id='$id'";
		}
		$ip = getip();
		$past = time() - intval($con[0]);
		$cookies = (intval($_COOKIE[substr($mod, 0, 2).'-'.$id])) ? $_COOKIE[substr($mod, 0, 2).'-'.$id] : "";
		$postname = (is_user()) ? text_filter(substr($user[1], 0, 25)) : "";
		$db->sql_query("DELETE FROM ".$prefix."_rating WHERE time<'$past' AND modul='$mod'");
		list($num) = $db->sql_fetchrow($db->sql_query("SELECT Count(id) FROM ".$prefix."_rating WHERE ((mid='$id' AND host='$ip') OR (mid='$id' AND name='$postname' AND name!='')) AND modul='".$mod."'"));
		if ($cookies == $id ||$num > 0) {
			list($votes, $totalvotes) = $db->sql_fetchrow($db->sql_query("SELECT ".$query.""));
			echo vote_graphic($votes, $totalvotes);
		} elseif (!$cookies && !$num && !$rating) {
			list($votes, $totalvotes) = $db->sql_fetchrow($db->sql_query("SELECT ".$query.""));
			$votes = (intval($votes)) ? $votes : 1;
			$width = number_format($totalvotes/$votes, 2) * 17;
			echo "<ul class=\"urating\" style=\"width: 85px;\">"
			."<li class=\"crating\" style=\"width:".$width."px;\"></li>"
			."<li><div class=\"out1\" OnMouseOver=\"this.className='over1';\" OnMouseOut=\"this.className='out1';\" OnClick=\"ajax('ajax.php?go=1&op=Rating&mod=$mod&id=$id&rating=1', 'rate".$id."');\" title=\""._RATE1."\"></div></li>"
			."<li><div class=\"out2\" OnMouseOver=\"this.className='over2';\" OnMouseOut=\"this.className='out2';\" OnClick=\"ajax('ajax.php?go=1&op=Rating&mod=$mod&id=$id&rating=2', 'rate".$id."');\" title=\""._RATE2."\"></div></li>"
			."<li><div class=\"out3\" OnMouseOver=\"this.className='over3';\" OnMouseOut=\"this.className='out3';\" OnClick=\"ajax('ajax.php?go=1&op=Rating&mod=$mod&id=$id&rating=3', 'rate".$id."');\" title=\""._RATE3."\"></div></li>"
			."<li><div class=\"out4\" OnMouseOver=\"this.className='over4';\" OnMouseOut=\"this.className='out4';\" OnClick=\"ajax('ajax.php?go=1&op=Rating&mod=$mod&id=$id&rating=4', 'rate".$id."');\" title=\""._RATE4."\"></div></li>"
			."<li><div class=\"out5\" OnMouseOver=\"this.className='over5';\" OnMouseOut=\"this.className='out5';\" OnClick=\"ajax('ajax.php?go=1&op=Rating&mod=$mod&id=$id&rating=5', 'rate".$id."');\" title=\""._RATE5."\"></div></li>"
			."</ul>";
		} elseif (!$cookies && !$num && $rating) {
			setcookie("".substr($mod, 0, 2)."-".$id."", $id, time() + intval($con[0]));
			$new = time();
			$db->sql_query("INSERT INTO ".$prefix."_rating VALUES (NULL, '$id', '$mod', '$new', '$postname', '$ip')");
			 if ($mod == "Account") {
				$db->sql_query("UPDATE ".$prefix."_users SET user_votes=user_votes+1, user_totalvotes=user_totalvotes+$rating WHERE user_id='$id'");
				update_points(1);
			} elseif ($mod == "Album") {
				#$db->sql_query("UPDATE ".$prefix."_media SET votes=votes+1, totalvotes=totalvotes+$rating WHERE id='$id'");
				update_points(5);
			} elseif ($mod == "FAQ") {
				$db->sql_query("UPDATE ".$prefix."_faq SET score=score+$rating, ratings=ratings+1 WHERE fid='$id'");
				update_points(24);
			} elseif ($mod == "Files") {
				$db->sql_query("UPDATE ".$prefix."_files SET votes=votes+1, totalvotes=totalvotes+$rating WHERE lid='$id'");
				update_points(12);
			} elseif ($mod == "Jokes") {
				$db->sql_query("UPDATE ".$prefix."_jokes SET rating=rating+$rating, ratingtot=ratingtot+1 WHERE jokeid='$id'");
				update_points(14);
			} elseif ($mod == "Links") {
				$db->sql_query("UPDATE ".$prefix."_links SET votes=votes+1, totalvotes=totalvotes+$rating WHERE lid='$id'");
				update_points(18);
			} elseif ($mod == "Media") {
				$db->sql_query("UPDATE ".$prefix."_media SET votes=votes+1, totalvotes=totalvotes+$rating WHERE id='$id'");
				update_points(21);
			} elseif ($mod == "News") {
				$db->sql_query("UPDATE ".$prefix."_stories SET score=score+$rating, ratings=ratings+1 WHERE sid='$id'");
				update_points(24);
			} elseif ($mod == "Pages") {
				$db->sql_query("UPDATE ".$prefix."_page SET score=score+$rating, ratings=ratings+1 WHERE pid='$id'");
				update_points(24);
			} elseif ($mod == "Shop") {
				$db->sql_query("UPDATE ".$prefix."_products SET product_votes=product_votes+1, product_totalvotes=product_totalvotes+$rating WHERE product_id='$id'");
				update_points(28);
				} elseif ($mod == "Music") {
				$db->sql_query("UPDATE ".$prefix."_music SET votes=votes+1, totalvotes=totalvotes+$rating WHERE id='$id'");
				update_points(12);
			}
			list($votes, $totalvotes) = $db->sql_fetchrow($db->sql_query("SELECT ".$query.""));
			echo "".vote_graphic($votes, $totalvotes)."";
		}
	}
}

// Format BB Code and Smilies
function textarea($id, $name, $var, $mod, $rows) {
	global $conf;
	$desc = ($var) ? $var : $_POST[$name];
	include("config/config_uploads.php");
	$con = explode("|", $confup[strtolower($mod)]);
	$style = (defined("ADMIN_FILE")) ? "admin" : strtolower($mod);
	if ((defined("ADMIN_FILE") && $conf['redaktor'] == 1) || (!defined("ADMIN_FILE"))) {
		$code = "<script type=\"text/javascript\" src=\"ajax/Upload.js\"></script><script type=\"text/javascript\" src=\"ajax/Handler.js\"></script><script language=\"JavaScript\" type=\"text/javascript\" src=\"ajax/Ajax.js\"></script><script language=\"JavaScript\" type=\"text/javascript\" src=\"ajax/SwitchMenu.js\"></script><script language=\"JavaScript\" type=\"text/javascript\" src=\"ajax/InsertCode.js\"></script>"
		."<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><div class=\"editor\">"
		."<div class=\"editorbutton\" OnClick=\"RowsTextarea('".$id."', 1)\"><img title=\""._EPLUS."\" src=\"images/editor/plus.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"RowsTextarea('".$id."', 0)\"><img title=\""._EMINUS."\" src=\"images/editor/minus.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('b', '', '', '', '".$id."')\"><img title=\""._EBOLD."\" src=\"images/editor/bold.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('i', '', '', '', '".$id."')\"><img title=\""._EITALIC."\" src=\"images/editor/italic.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('u', '', '', '', '".$id."')\"><img title=\""._EUNDERLINE."\" src=\"images/editor/underline.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('s', '', '', '', '".$id."')\"><img title=\""._ESTRIKET."\" src=\"images/editor/striket.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('li', '', '', '', '".$id."')\"><img title=\""._ELI."\" src=\"images/editor/li.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('hr', '', '', '', '".$id."')\"><img title=\""._EHR."\" src=\"images/editor/hr.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('left', '', '', '', '".$id."')\"><img title=\""._ELEFT."\" src=\"images/editor/left.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('center', '', '', '', '".$id."')\"><img title=\""._ECENTER."\" src=\"images/editor/center.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('right', '', '', '', '".$id."')\"><img title=\""._ERIGHT."\" src=\"images/editor/right.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('justify', '', '', '', '".$id."')\"><img title=\""._EYUSTIFY."\" src=\"images/editor/justify.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('code', '', '', '', '".$id."')\"><img title=\""._CODE."\" src=\"images/editor/code.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('php', '', '', '', '".$id."')\"><img title=\"PHP - "._CODE."\" src=\"images/editor/php.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('hide', '', '', '', '".$id."')\"><img title=\""._HIDE."\" src=\"images/editor/hide.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('url', '"._JINFO."', '"._JTYPE."', '"._JERROR."', '".$id."')\"><img title=\""._EURL."\" src=\"images/editor/url.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('mail', '"._JINFO."', '"._JTYPE."', '"._JERROR."', '".$id."')\"><img title=\""._EEMAIL."\" src=\"images/editor/mail.gif\"></div>"
		."<div class=\"editorbutton\" OnClick=\"InsertCode('img', '', '', '', '".$id."')\"><img title=\""._EIMG."\" src=\"images/editor/img.gif\"></div>"
		."</div>"
		."<textarea id=\"".$id."\" name=\"".$name."\" cols=\"65\" rows=\"".$rows."\" class=\"".$style."\" OnKeyPress=\"TransliteFeld(this, event)\" OnSelect=\"FieldName(this, this.name)\" OnClick=\"FieldName(this, this.name)\" OnKeyUp=\"FieldName(this, this.name)\">".$desc."</textarea>"
		."<div class=\"editor\">";
		if (defined("ADMIN_FILE") || (is_user() && $con[8] == 1) || (!is_user() && $con[9] == 1)) $code .= "<div id=\"cont\" class=\"editorbutton\" OnClick=\"SwitchMenu('af".$id."')\" ><img title=\""._EUPLOAD."\" src=\"images/editor/upload.gif\"></div>";
		if (!$conf['smilies']) $code .= "<div id=\"cont\" class=\"editorbutton\" OnClick=\"SwitchMenu('sm".$id."')\"><img title=\""._ESMILIE."\" src=\"images/editor/smilie.gif\"></div>";
		$code .= "<div class=\"editorbutton\" OnClick=\"InsertCode('quote', '', '', '', '".$id."')\"><img title=\""._EQUOTE."\" src=\"images/editor/quote.gif\"></div>";
		if (substr(""._LOCALE."", 0, 2) == "ru") {
			$code .= "<div class=\"editorbutton\" OnClick=\"translateAlltoCyrillic()\"><img title=\""._ERUS."\" src=\"images/editor/rus.gif\"></div>"
			."<div class=\"editorbutton\" OnClick=\"translateAlltoLatin()\"><img title=\""._ELAT."\" src=\"images/editor/eng.gif\"></div>"
			."<div class=\"editorbutton\" OnClick=\"changelanguage()\"><img title=\""._EAUTOTR."\" src=\"images/editor/auto.gif\"></div>";
		}
		$fonts = 0;
		$font = array("Arial", "Courier New", "Mistral", "Impact", "Sans Serif", "Tahoma", "Helvetica", "Verdana");
		foreach ($font as $val) if ($val != "") $fonts .= "<option style=\"font-family: ".$val.";\" value=\"".$val."\">".$val."</option>";
		$colors = 0;
		$color = array("black", "silver", "gray", "white", "maroon", "orange", "orangered", "red", "purple", "fuchsia", "green", "lime", "olive", "yellow", "navy", "blue", "teal", "aqua");
		foreach ($color as $val) if ($val != "") $colors .= "<option style=\"color: ".$val.";\" value=\"".$val."\">"._ECOLOR."</option>";
		$fsizes = 0;
		$fsize = array("8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "32");
		foreach ($fsize as $val) if ($val != "") $fsizes .= "<option value=\"".$val."\">"._ESIZE." ".$val."</option>";
		$code .= "<div class=\"editorbutton\"><select style=\"font-size: 10px;\" name=\"family\" onChange=\"InsertCode('family', this.options[this.selectedIndex].value, '', '', '".$id."')\">".$fonts."</select></div>"
		."<div class=\"editorbutton\"><select style=\"font-size: 10px;\" name=\"color\" onChange=\"InsertCode('color', this.options[this.selectedIndex].value, '', '', '".$id."')\">".$colors."</select></div>"
		."<div class=\"editorbutton\"><select style=\"font-size: 10px;\" name=\"size\" onChange=\"InsertCode('size', this.options[this.selectedIndex].value, '', '', '".$id."')\">".$fsizes."</select></div></div>";
		if ($conf['smilies'] == 1) {
			$code .= "<div class=\"editor\">";
			for ($i = 1; $i < 19; $i++) {
				$i = ($i < 10) ? "0".$i ."" : $i;
				$code .= "<div class=\"editorbutton\"><img src=\"images/smilies/$i.gif\" OnClick=\"AddSmile(' *$i');\" alt=\""._SMILIE." - $i\" title=\""._SMILIE." - $i\"></div>";
			}
			$code .= "</div>";
		} elseif ($conf['smilies'] == 2) {
			$code .= "<div class=\"smilies\">";
			$i = 1;
			$dir = opendir("images/smilies");
			while ($entry = readdir($dir)) {
				if (preg_match("/(\.gif|\.png|\.jpg|\.jpeg)$/is", $entry) && $entry != "." && $entry != "..") {
					$i = ($i < 10) ? "0".$i ."" : $i;
					$code .= " <img src=\"images/smilies/$i.gif\" OnClick=\"AddSmile(' *$i');\" style=\"cursor: pointer; margin: 3px 2px 0px 0px;\" alt=\""._SMILIE." - $i\" title=\""._SMILIE." - $i\">";
					$i++;
				}
			}
			closedir($dir);
			$code .= "</div>";
		} else {
			$code .= "<div id=\"sm".$id."\" style=\"display:none;\" class=\"smilies\">";
			$i = 1;
			$dir = opendir("images/smilies");
			while ($entry = readdir($dir)) {
				if (preg_match("/(\.gif|\.png|\.jpg|\.jpeg)$/is", $entry) && $entry != "." && $entry != "..") {
					$i = ($i < 10) ? "0".$i ."" : $i;
					$code .= " <img src=\"images/smilies/$i.gif\" OnClick=\"AddSmile(' *$i');\" style=\"cursor: pointer; margin: 3px 2px 0px 0px;\" alt=\""._SMILIE." - $i\" title=\""._SMILIE." - $i\">";
					$i++;
				}
			}
			closedir($dir);
			$code .= "</div>";
		}
		if (defined("ADMIN_FILE") || (is_user() && $con[8] == 1) || (!is_user() && $con[9] == 1)) {
			$code .= "<div id=\"af".$id."\" style=\"display:none;\" class=\"smilies\">
			<style type=\"text/css\">
			.progressWrapper {
				width: 357px;
				overflow: hidden;
			}
			.progressContainer {
				margin: 5px;
				padding: 4px;
				border: solid 1px #E8E8E8;
				background-color: #F7F7F7;
				overflow: hidden;
			}
			.red {
				border: solid 1px #B50000;
				background-color: #FFEBEB;
			}
			.green {
				border: solid 1px #DDF0DD;
				background-color: #EBFFEB;
			}
			.blue {
				border: solid 1px #CEE2F2;
				background-color: #F0F5FF;
			}
			.progressName {
				font-size: 8pt;
				font-weight: bold;
				color: #555555;
				width: 323px;
				height: 14px;
				text-align: left;
				white-space: nowrap;
				overflow: hidden;
			}
			.progressBarInProgress, .progressBarComplete, .progressBarError {
				font-size: 0px;
				width: 0%;
				height: 2px;
				background-color: blue;
				margin-top: 2px;
			}
			.progressBarComplete {
				width: 100%;
				background-color: green;
				visibility: hidden;
			}
			.progressBarError {
				width: 100%;
				background-color: red;
				visibility: hidden;
			}
			.progressBarStatus {
				margin-top: 2px;
				width: 337px;
				font-size: 7pt;
				font-family: Verdana;
				text-align: left;
				white-space: nowrap;
			}
			a.progressCancel, a.progressCancel:link, a.progressCancel:active, a.progressCancel:visited, a.progressCancel:hover {
				font-size: 0px;
				display: block;
				height: 14px;
				width: 14px;
				background-image: url(images/cancelbutton.gif);
				background-repeat: no-repeat;
				background-position: -14px 0px;
				float: right;
			}
			a.progressCancel:hover {
				background-position: 0px 0px;
			}
			</style>";
			if ($id == 1) {
				$code .= "<script type=\"text/javascript\">
				var upload1;
				window.onload = function() {
					upload1 = new SWFUpload({
						upload_target_url: \"../ajax.php\",
						upload_cookies: [\"PHPSESSID\"],
						upload_params: { go: \"4\", mod: \"$mod\" },

						file_size_limit : \"".$con[2]."\",
						file_types : \"*.".str_replace(",", ";*.", $con[0])."\",
						file_types_description : \"All Files\",
						file_upload_limit : \"".$con[5]."\",
						file_queue_limit : \"0\",
						begin_upload_on_queue : true,

						file_queued_handler : uploadStart,
						file_progress_handler : uploadProgress,
						file_cancelled_handler : uploadCancel,
						file_complete_handler : uploadComplete,
						queue_complete_handler : uploadQueueComplete,
						error_handler : uploadError,

						flash_url : \"ajax/Upload.swf\",
						ui_container_id : \"eflash\",

						debug: false
					});
					upload1.addSetting(\"progress_target\", \"eupload\");
				}
				</script>
				<div id=\"eflash\" style=\"display: none;\"><fieldset id=\"eupload\" style=\"margin: 5px;\">
				<legend>"._UPLOADINFO."</legend><div align=\"left\" style=\"margin: 3px;\">"
				.""._FTYPE.": ".str_replace(",", ", ", $con[0])."<br />"
				.""._FSIZEALL.": ".files_size($con[1])."<br />"
				.""._FSIZE.": ".files_size($con[2])."<br />"
				.""._AWIDTH.": ".$con[3]." px<br />"
				.""._AHEIGHT.": ".$con[4]." px<br />"
				.""._FILEUP.": ".$con[5]."<br />"
				."</div></fieldset><div>
				<input type=\"button\" value=\""._UPLOAD."\" onclick=\"upload1.browse()\">
				<input id=\"btnCancel1\" type=\"button\" value=\""._DELETE."\" OnClick=\"upload1.cancelQueue();\" disabled=\"disabled\">
				<input type=\"button\" value=\""._UPDATE."\" OnClick=\"ajax('ajax.php?go=3&op=show_files&mod=$mod&id=$id', 'f".$id."');\">
				<br /></div></div>";
			} else {
				$code .= "<input type=\"button\" value=\""._UPDATE."\" style=\"margin: 5px 0 0 0;\" OnClick=\"ajax('ajax.php?go=3&op=show_files&mod=$mod&id=$id', 'f".$id."');\">";
			}
			$code .= "<script type=\"text/javascript\">ajax('ajax.php?go=3&op=show_files&mod=$mod&id=$id', 'f".$id."');</script><div id=\"f".$id."\"></div></div>";
		}
	} else {
		$code = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td>"
		."<textarea id=\"".$id."\" name=\"".$name."\" cols=\"65\" rows=\"".$rows."\" class=\"".$style."\">".$desc."</textarea>";
	}
	$code .= "</td></tr></table>";
	return $code;
}

// Format Page
function get_page($mod) {
	OpenTable();
	echo "<h5>[ <a href=\"javascript:history.go(-1)\" title=\""._PAGEBACK."\">"._PAGEBACK."</a> | <a href=\"index.php?name=$mod\" title=\""._PAGEHOME."\">"._PAGEHOME."</a> | <a href=\"#\" title=\""._PAGETOP."\">"._PAGETOP."</a> ]</h5>";
	CloseTable();
}

// Format Nummer Page
function num_page($mod="", $numstories, $numpages, $storynum, $module_link="") {
	global $admin_file;
	$num = isset($_GET['num']) ? intval($_GET['num']) : "1";
	if ($numpages > 1) {
		if (defined("ADMIN_FILE")) {
			$index = $admin_file;
			$module = "";
		} else {
			$index = "index";
			$module = "name=".$mod."&";
		}
		OpenTable();
		echo "<center><h3 style=\"margin: 0 0 5px 0;\">"._OVERALL." $numstories "._ON." $numpages "._PAGE_S." $storynum "._PERPAGE."</h3>";
		if ($num > 1) {
			$prevpage = $num - 1;
			echo "<span class=\"pagelink\"><a href=\"".$index.".php?".$module."".$module_link."num=$prevpage\" title=\"&lt;&lt;\">&lt;&lt;</a></span> ";
		}
		for ($i = 1; $i < $numpages+1; $i++) {
			if ($i == $num) {
				echo "<span class=\"pagelink\" title=\"$i\">$i</span>";
			} else {
				if ((($i > ($num - 8)) && ($i < ($num + 8))) OR ($i == $numpages) || ($i == 1)) {
					echo "<span class=\"pagelink\"><a href=\"".$index.".php?".$module."".$module_link."num=$i\" title=\"$i\"><b>$i</b></a></span>";
				}
			}
			if ($i < $numpages) {
				if (($i > ($num - 9)) && ($i < ($num + 8))) echo " ";
				if (($num > 9) && ($i == 1)) echo " <span class=\"pagelink\">...</span>";
				if (($num < ($numpages - 8)) && ($i == ($numpages - 1))) echo "<span class=\"pagelink\">...</span> ";

			}
		}
		if ($num < $numpages) {
			$nextpage = $num + 1;
			echo " <span class=\"pagelink\"><a href=\"".$index.".php?".$module."".$module_link."num=$nextpage\" title=\"&gt;&gt;\">&gt;&gt;</a></span>";
		}
		echo "</center>" ;
		CloseTable();
	}
}

// Format Nummer Pages
function num_pages($mod="", $numstories, $numpages, $storynum, $module_link="") {
	global $admin_file;
	$pag = isset($_GET['pag']) ? intval($_GET['pag']) : "1";
	$num = isset($_GET['num']) ? intval($_GET['num']) : "1";
	if ($numpages > 1) {
		if (defined("ADMIN_FILE")) {
			$index = $admin_file;
			$module = "";
		} else {
			$index = "index";
			$module = "name=".$mod."&";
		}
		OpenTable();
		echo "<center><h3 style=\"margin: 0 0 5px 0;\">"._OVERALL." $numstories "._ON." $numpages "._PAGE_S." $storynum "._PERPAGE."</h3>";
		if ($pag > 1) {
			$prevpage = $pag - 1;
			echo "<span class=\"pagelink\"><a href=\"".$index.".php?".$module."".$module_link."pag=$prevpage&num=$num\" title=\"&lt;&lt;\">&lt;&lt;</a></span> ";
		}
		for ($i = 1; $i < $numpages+1; $i++) {
			if ($i == $pag) {
				echo "<span class=\"pagelink\" title=\"$i\">$i</span>";
			} else {
				if ((($i > ($pag - 8)) && ($i < ($pag + 8))) OR ($i == $numpages) || ($i == 1)) {
					echo "<span class=\"pagelink\"><a href=\"".$index.".php?".$module."".$module_link."pag=$i&num=$num\" title=\"$i\"><b>$i</b></a></span>";
				}
			}
			if ($i < $numpages) {
				if (($i > ($pag - 9)) && ($i < ($pag + 8))) echo " ";
				if (($pag > 9) && ($i == 1)) echo " <span class=\"pagelink\">...</span>";
				if (($pag < ($numpages - 8)) && ($i == ($numpages - 1))) echo "<span class=\"pagelink\">...</span> ";

			}
		}
		if ($pag < $numpages) {
			$nextpage = $pag + 1;
			echo " <span class=\"pagelink\"><a href=\"".$index.".php?".$module."".$module_link."pag=$nextpage&num=$num\" title=\"&gt;&gt;\">&gt;&gt;</a></span>";
		}
		echo "</center>" ;
		CloseTable();
	}
}

// Check type upload file
function check_file($type, $typefile) {
	$strtypefile = str_replace(",", "|", $typefile);
	if (!preg_match("/".$strtypefile."/i", $type) || preg_match("/php|php3|php4|php5|php6|js|htm|html|phtml|cgi|pl|perl|asp/i", $type)) return ""._ERROR_FILE."";
}

// Check size upload file
function check_size($file, $width, $height) {
	list($imgwidth, $imgheight) = getimagesize($file);
	if ($imgwidth > $width || $imgheight > $height) return ""._ERROR_SIZE."";
}

// Upload file
function upload($directory, $typefile, $maxsize, $namefile, $width, $height) {
	global $user, $stop;
	if ($_POST['sitefile'] != "") {
		$afile = str_replace(array("&", "?", "#"), "", $_POST['sitefile']);
		$type = strtolower(end(explode(".", $afile)));
		if (!check_file($type, $typefile) && !check_size($_POST['sitefile'], $width, $height)) {
			$fn = $_POST['sitefile'];
			$path_sitefile = fopen($fn, "rb");
			if (!$path_sitefile) {
				$stop = ""._ERROR_DOWN."";
				return 0;
			} else {
				$newname = ($namefile) ? "".$namefile."-".gen_pass(10).".".$type."" : "".gen_pass(15).".".$type."";
				$directoryp = "".$directory."/".basename($newname)."";
				if (file_exists($directoryp)) {
					$stop = ""._ERROR_EXIST."";
					return 0;
				} else {
					while (!feof($path_sitefile)) $data .= fread($path_sitefile, 1024);
					fclose($path_sitefile);
					$path_sitefile = fopen("".$directory."/".basename($newname)."", "wb");
					if (!$path_sitefile) {
						$stop = ""._ERROR_UP."";
						return 0;
					} else {
						fwrite($path_sitefile, $data);
						fclose($path_sitefile);
						if (file_exists($directoryp)) {
							if (filesize($directoryp) > $maxsize) {
								unlink($directoryp);
								$stop = ""._ERROR_BIG."";
								return 0;
							} else {
								return $newname;
							}
						}
					}
				}
			}
		} else {
			$stop = (!check_file($type, $typefile)) ? "".check_size($_POST['sitefile'], $width, $height)."" : "".check_file($type, $typefile)."";
			return 0;
		}
	}
	if (intval($_FILES['userfile']['size'])) {
		if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
			if ($_FILES['userfile']['size'] > $maxsize) {
				$stop = ""._ERROR_BIG."";
				return 0;
			} else {
				$type = strtolower(end(explode(".", $_FILES['userfile']['name'])));
				if (!check_file($type, $typefile) && !check_size($_FILES['userfile']['tmp_name'], $width, $height)) {
					if (file_exists("".$directory."/".$type."")) {
						$stop = ""._ERROR_EXIST."";
						return 0;
					} else {
						$newname = ($namefile) ? "".$namefile."-".gen_pass(10).".".$type."" : "".gen_pass(15).".".$type."";
						$res = copy($_FILES['userfile']['tmp_name'], "".$directory."/".$newname."");
						if (!$res) {
							$stop = ""._ERROR_UP."";
							return 0;
						} else {
							return $newname;
						}
					}
				} else {
					$stop = (!check_file($type, $typefile)) ? "".check_size($_FILES['userfile']['tmp_name'], $width, $height)."" : "".check_file($type, $typefile)."";
					return 0;
				}
			}
		} else {
			$stop = ""._ERROR_DOWN."";
			return 0;
		}
	}
	if (intval($_FILES['Filedata']['size'])) {
		if (is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
			if ($_FILES['Filedata']['size'] > $maxsize) {
				$stop = ""._ERROR_BIG."";
				return 0;
			} else {
				$type = strtolower(end(explode(".", $_FILES['Filedata']['name'])));
				if (!check_file($type, $typefile) && !check_size($_FILES['Filedata']['tmp_name'], $width, $height)) {
					if (file_exists("".$directory."/".$type."")) {
						$stop = ""._ERROR_EXIST."";
						return 0;
					} else {
						if (!$namefile) $namefile = $namefile;
						if (is_admin() && !is_user()) {
							$newname = ($namefile) ? "".$namefile."-".gen_pass(10).".".$type."" : "".gen_pass(15).".".$type."";
						} else {
							$uname = ($user[0]) ? intval($user[0]) : "0";
							$newname = ($namefile) ? "".$namefile."-".gen_pass(10)."-".$uname.".".$type."" : "".gen_pass(15).".".$type."";
						}
						$res = copy($_FILES['Filedata']['tmp_name'], "".$directory."/".$newname."");
						if (!$res) {
							$stop = ""._ERROR_UP."";
							return 0;
						} else {
							return $newname;
						}
					}
				} else {
					$stop = (!check_file($type, $typefile)) ? "".check_size($_FILES['Filedata']['tmp_name'], $width, $height)."" : "".check_file($type, $typefile)."";
					return 0;
				}
			}
		} else {
			$stop = ""._ERROR_DOWN."";
			return 0;
		}
	}
}

// Format language
function language($lang="") {
	$dir = opendir("language");
	$content = "<option value=\"\">"._ALL."</option>";
	while ($file = readdir($dir)) {
		if (preg_match("/^lang\-(.+)\.php/", $file, $matches)) {
			$langfound = $matches[1];
			$title = ucfirst($langfound);
			$selected = ($lang == $langfound) ? "selected" : "";
			$content .= "<option value=\"$langfound\" $selected>".$title."</option>";
		}
	}
	closedir($dir);
	return $content;
}

// Format module
function modul($modul="") {
	$dir = opendir("modules");
	while ($file = readdir($dir)) {
		if (!preg_match("/\./", $file)) {
			$title = str_replace("_", " ", $file);
			$selected = ($modul == $file) ? "selected" : "";
			$content .= "<option value=\"$file\" $selected>".$title."</option>";
		}
	}
	closedir($dir);
	return $content;
}

// Format image preview PHP GD
function create_img_gd($imgfile, $imgthumb, $newwidth) {
	if (function_exists("imagecreate")) {
		$imginfo = getimagesize($imgfile);
		switch($imginfo[2]) {
			case 1:
			$type = IMG_GIF;
			break;
			case 2:
			$type = IMG_JPG;
			break;
			case 3:
			$type = IMG_PNG;
			break;
			case 4:
			$type = IMG_WBMP;
			break;
			default:
			return $imgfile;
			break;
		}
		switch($type) {
			case IMG_GIF:
			if (!function_exists("imagecreatefromgif")) return $imgfile;
			$srcImage = imagecreatefromgif("$imgfile");
			break;
			case IMG_JPG:
			if (!function_exists("imagecreatefromjpeg")) return $imgfile;
			$srcImage = imagecreatefromjpeg($imgfile);
			break;
			case IMG_PNG:
			if(!function_exists("imagecreatefrompng")) return $imgfile;
			$srcImage = imagecreatefrompng("$imgfile");
			break;
			case IMG_WBMP:
			if (!function_exists("imagecreatefromwbmp")) return $imgfile;
			$srcImage = imagecreatefromwbmp("$imgfile");
			break;
			default:
			return $imgfile;
		}
		if ($srcImage){
			$srcWidth = $imginfo[0];
			$srcHeight = $imginfo[1];
			$ratioWidth = $srcWidth / $newwidth;
			$destWidth = $newwidth;
			$destHeight = $srcHeight / $ratioWidth;
			$destImage = imagecreatetruecolor($destWidth, $destHeight);
			imagealphablending($destImage, true);
			imagealphablending($srcImage, false);
			imagecopyresized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
			switch($type) {
				case IMG_GIF:
				imagegif($destImage, "$imgthumb");
				break;
				case IMG_JPG:
				imagejpeg($destImage, "$imgthumb");
				break;
				case IMG_PNG:
				imagepng($destImage, "$imgthumb");
				break;
				case IMG_WBMP:
				imagewbmp($destImage, "$imgthumb");
				break;
			}
			imagedestroy($srcImage);
			imagedestroy($destImage);
			return $imgthumb;
		} else {
			return $imgfile;
		}
	} else {
		return $imgfile;
	}
}
?>