<?php

/**
 * @package eggblog
 * @author eggblog.net, tenfourzero@users.sourceforge.net
 * @version $Id: global.php,v 4.0.0rc2 2007/12/20 10:24:36 tenfourzero Exp $
 * @copyright Copyright (c) 2005, http://www.eggblog.net
 * @license http://www.gnu.org/licenses/gpl.html GNU Lesser General Public License
 */


/**
 * update mysql tables from v4.0.0b to v4.0.0
 * @return string
 */
function eb_upgrade4bto4() {
	global $config,$lang;
	@mysql_query("DROP TABLE eb_tags;");
	@mysql_query("DROP TABLE eb_tags_links;");
	$sql.="CREATE TABLE eb_tags (".
		"tag_id int NOT NULL AUTO_INCREMENT,".
		"tag_name varchar(150) NOT NULL,".
		"PRIMARY KEY (tag_id),".
		"UNIQUE KEY tag_name (tag_name)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_tags_links (".
		"tag_id int NOT NULL,".
		"article_id int NOT NULL,".
		"PRIMARY KEY (tag_id,article_id)".
		") ENGINE=MyISAM;";
	$sql=explode(";",$sql);
	unset($sql[count($sql)-1]);
	$i=0;
	foreach($sql as $query) {
		$i++;
		if(!mysql_query($query)) {
			return "\t\t<p>".$lang['error_event']."</p>\r\n\t\t<p>".$lang['error_refresh']."</p>\r\n".mysql_error().$query;
			exit();
		}
		if($i==count($sql)) return "\t\t<p>".$lang['upgrade_ok'].".</p>\r\n";
	}
}


/**
 * update mysql tables from v3 to v4
 * @return string
 */
function eb_upgrade3to4() {
	global $config,$lang;
	@mysql_query("DROP TABLE eb_articles;");
	@mysql_query("DROP TABLE eb_users;");
	@mysql_query("DROP TABLE eb_comments;");
	@mysql_query("DROP TABLE eb_posts;");
	@mysql_query("DROP TABLE eb_topics;");
	@mysql_query("DROP TABLE eb_photos;");
	@mysql_query("DROP TABLE eb_albums;");
	@mysql_query("DROP TABLE eb_tags;");
	@mysql_query("DROP TABLE eb_tags_links;");
	$sql ="ALTER TABLE `eggblog_forum_posts` ".
		"CHANGE `flag` `flag` ENUM( '0', '1' ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '1', ".
		"CHANGE `author` `author` VARCHAR( 250 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, ".
		"CHANGE `ip` `ip` VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, ".
		"CHANGE `text` `text` MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ;";
	$sql.="ALTER TABLE `eggblog_comments` CHANGE `author` `author` VARCHAR( 250 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,".
		"CHANGE `ip` `ip` VARCHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,".
		"CHANGE `comments` `comments` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,".
		"CHANGE `url` `url` MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;";
	$sql.="CREATE TABLE eb_articles (".
		"article_id int NOT NULL AUTO_INCREMENT,".
		"article_flag int NOT NULL DEFAULT '0',".
		"author_id int NOT NULL,".
		"article_date int NOT NULL DEFAULT '0',".
		"article_title varchar(250) NOT NULL DEFAULT '',".
		"article_body longtext NOT NULL,".
		"article_comments int NOT NULL DEFAULT '0',".
		"tag_id int NOT NULL,".
		"PRIMARY KEY (article_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_users (".
		"user_id int NOT NULL AUTO_INCREMENT,".
		"user_flag int NOT NULL DEFAULT '0',".
		"user_name varchar(250) NOT NULL DEFAULT '',".
		"user_email varchar(250) NOT NULL DEFAULT '',".
		"user_password varchar(250) NOT NULL DEFAULT '',".
		"PRIMARY KEY (user_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_comments (".
		"comment_id int NOT NULL AUTO_INCREMENT,".
		"author_id int NOT NULL,".
		"article_id int NOT NULL,".
		"comment_ip varchar(20) NOT NULL DEFAULT '',".
		"comment_date int NOT NULL,".
		"comment_body longtext NOT NULL,".
		"PRIMARY KEY (comment_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_topics (".
		"topic_id int NOT NULL AUTO_INCREMENT,".
		"topic_title varchar(250) NOT NULL DEFAULT '',".
		"PRIMARY KEY (topic_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_posts (".
		"post_id int NOT NULL AUTO_INCREMENT,".
		"topic_id int NOT NULL,".
		"post_date int NOT NULL,".
		"post_ip varchar(20) NOT NULL DEFAULT '',".
		"author_id int NOT NULL,".
		"post_body longtext NOT NULL,".
		"PRIMARY KEY (post_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_photos (".
		"photo_id int NOT NULL AUTO_INCREMENT,".
		"album_id int NOT NULL,".
		"photo_description longtext NOT NULL,".
		"PRIMARY KEY (photo_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_albums (".
		"album_id int NOT NULL AUTO_INCREMENT,".
		"album_title varchar(250) NOT NULL,".
		"PRIMARY KEY (album_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_tags (".
		"tag_id int NOT NULL AUTO_INCREMENT,".
		"tag_name varchar(150) NOT NULL,".
		"PRIMARY KEY (tag_id),".
		"UNIQUE KEY tag_name (tag_name)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_tags_links (".
		"tag_id int NOT NULL,".
		"article_id int NOT NULL,".
		"PRIMARY KEY (tag_id,article_id)".
		") ENGINE=MyISAM;";
	$sql.="INSERT INTO eb_articles ".
		"SELECT a.id,a.flag,m.id,a.date,a.title,a.details,a.comments ".
		"FROM eggblog_articles AS a ".
		"LEFT JOIN eggblog_members AS m ON a.author=m.name ".
		"ORDER BY a.id;";
	$sql.="UPDATE eb_articles ".
		"SET article_body=REPLACE(eb_articles.article_body,'</p>','\n\n');";
	$sql.="UPDATE eb_articles ".
		"SET article_body=REPLACE(eb_articles.article_body,'<p>','');";
	$sql.="INSERT INTO eb_users ".
		"SELECT id,flag-1,name,email,email FROM eggblog_members;";
	$sql.="INSERT INTO eb_topics ".
		"(topic_id,topic_title) ".
		"SELECT id,name FROM eggblog_forum_topics;";
	$sql.="INSERT INTO eb_posts ".
		"(post_id,topic_id,post_date,post_ip,author_id,post_body) ".
		"SELECT id,topicid,date,ip,eb_users.user_id,text FROM ".
		"eggblog_forum_posts LEFT JOIN eb_users ON eb_users.user_name=".
		"eggblog_forum_posts.author;";
	$sql.="INSERT INTO eb_comments ".
		"(comment_id,author_id,article_id,comment_ip,comment_date,comment_body) ".
		"SELECT c.id,u.user_id,c.article_id,c.ip,c.date,c.comments ".
		"FROM eggblog_comments AS c ".
		"LEFT JOIN eb_users AS u ON u.user_name=c.author;";
	$sql.="INSERT INTO eb_albums ".
		"(album_id,album_title) ".
		"SELECT a.id,a.title ".
		"FROM eggblog_photos_albums AS a;";
	$sql.="INSERT INTO eb_photos ".
		"(photo_id,album_id,photo_description) ".
		"SELECT p.id,p.album_id,CONCAT(p.title,'\r\n\r\n',p.description) ".
		"FROM eggblog_photos AS p;";
	$sql=explode(";",$sql);
	unset($sql[count($sql)-1]);
	$i=0;
	foreach($sql as $query) {
		$i++;
		if(!mysql_query($query)) {
			return "\t\t<p>".$lang['error_event']."</p>\r\n\t\t<p>".$lang['error_refresh']."</p>\r\n".mysql_error().$query;
			exit();
		}
		if($i==count($sql)) return "\t\t<p>".$lang['upgrade_ok'].".</p>\r\n\t\t<p>".ucwords($lang['email']).": ".htmlentities($config['email'],ENT_QUOTES)."<br />".ucwords($lang['password']).": ".htmlentities($config['email'],ENT_QUOTES)."</p>\r\n";
	}
}


/**
 * install mysql tables
 * @return string
 */
function eb_install() {
	global $config,$lang;
	@mysql_query("DROP TABLE eb_articles;");
	@mysql_query("DROP TABLE eb_users;");
	@mysql_query("DROP TABLE eb_comments;");
	@mysql_query("DROP TABLE eb_posts;");
	@mysql_query("DROP TABLE eb_topics;");
	@mysql_query("DROP TABLE eb_photos;");
	@mysql_query("DROP TABLE eb_albums;");
	@mysql_query("DROP TABLE eb_tags;");
	@mysql_query("DROP TABLE eb_tags_links;");
	$sql ="CREATE TABLE eb_articles (".
		"article_id int NOT NULL AUTO_INCREMENT,".
		"article_flag int NOT NULL DEFAULT '0',".
		"author_id int NOT NULL,".
		"article_date int NOT NULL DEFAULT '0',".
		"article_title varchar(250) NOT NULL DEFAULT '',".
		"article_body longtext NOT NULL,".
		"article_comments int NOT NULL DEFAULT '0',".
		"tag_id int NOT NULL,".
		"PRIMARY KEY (article_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_users (".
		"user_id int NOT NULL AUTO_INCREMENT,".
		"user_flag int NOT NULL DEFAULT '0',".
		"user_name varchar(250) NOT NULL DEFAULT '',".
		"user_email varchar(250) NOT NULL DEFAULT '',".
		"user_password varchar(250) NOT NULL DEFAULT '',".
		"PRIMARY KEY (user_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_comments (".
		"comment_id int NOT NULL AUTO_INCREMENT,".
		"author_id int NOT NULL,".
		"article_id int NOT NULL,".
		"comment_ip varchar(20) NOT NULL DEFAULT '',".
		"comment_date int NOT NULL,".
		"comment_body longtext NOT NULL,".
		"PRIMARY KEY (comment_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_posts (".
		"post_id int NOT NULL AUTO_INCREMENT,".
		"topic_id int NOT NULL,".
		"post_date int NOT NULL,".
		"post_ip varchar(20) NOT NULL DEFAULT '',".
		"author_id int NOT NULL,".
		"post_body longtext NOT NULL,".
		"PRIMARY KEY (post_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_topics (".
		"topic_id int NOT NULL AUTO_INCREMENT,".
		"topic_title varchar(250) NOT NULL DEFAULT '',".
		"PRIMARY KEY (topic_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_photos (".
		"photo_id int NOT NULL AUTO_INCREMENT,".
		"album_id int NOT NULL,".
		"photo_description longtext NOT NULL,".
		"PRIMARY KEY (photo_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_albums (".
		"album_id int NOT NULL AUTO_INCREMENT,".
		"album_title varchar(250) NOT NULL,".
		"PRIMARY KEY (album_id)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_tags (".
		"tag_id int NOT NULL AUTO_INCREMENT,".
		"tag_name varchar(150) NOT NULL,".
		"PRIMARY KEY (tag_id),".
		"UNIQUE KEY tag_name (tag_name)".
		") ENGINE=MyISAM;";
	$sql.="CREATE TABLE eb_tags_links (".
		"tag_id int NOT NULL,".
		"article_id int NOT NULL,".
		"PRIMARY KEY (tag_id,article_id)".
		") ENGINE=MyISAM;";
	$sql.="INSERT INTO eb_users ".
		"(user_id,user_flag,user_name,user_email,user_password) ".
		"VALUES (NULL,'2','".str_replace("'","\'",$config["title"])."',".
		"'".str_replace("'","\'",$config["email"])."','".
		str_replace("'","\'",$config["email"])."');";
	$sql=explode(";",$sql);
	unset($sql[count($sql)-1]);
	$i=0;
	foreach($sql as $query) {
		$i++;
		if(!mysql_query($query)) {
			return "\t\t<p>".$lang['error_event'].".</p>\r\n\t\t<p>".$lang['error_refresh'].".</p>\r\n".mysql_error();
			exit();
		}
		if($i==count($sql)) return "\t\t<p>".$lang['install_ok']."</p>\r\n\t\t<p>".ucwords($lang['email']).": ".htmlentities($config['email'],ENT_QUOTES)."<br />".ucwords($lang['password']).": ".htmlentities($config['email'],ENT_QUOTES)."</p>\r\n";
	}
}


/**
 * print admin options
 */
function eb_admin() {
	global $lang;
	echo "\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";
	echo "\t\t<ul>\r\n";
	echo "\t\t\t<li><a href=\"admin.php?id=config\">".ucwords($lang['config'])."</a></li>\r\n";
	echo "\t\t\t<li><a href=\"admin.php?id=newarticle\">".ucwords($lang['new_article'])."</a></li>\r\n";
	echo "\t\t\t<li><a href=\"admin.php?id=newalbum\">".ucwords($lang['new_album'])."</a></li>\r\n";
	echo "\t\t\t<li><a href=\"admin.php?id=newphoto\">".ucwords($lang['new_photo'])."</a></li>\r\n";
	echo "\t\t\t<li><a href=\"admin.php?id=members\">".ucwords($lang['members'])."</a></li>\r\n";
	echo "\t\t\t<li><a href=\"admin.php?id=update\">".ucwords($lang['update'])."</a></li>\r\n";
	echo "\t\t</ul>\r\n";
}


/**
 * print configuration form / save configuration details
 */
function eb_admin_config() {
	global $config,$lang;
	if(!empty($_POST["submit"])) {
		unset($_POST["submit"]);

		$file="<?php\r\n\r\n// ".date("r")."\r\n// ".$_SERVER['REMOTE_ADDR']."\r\n\r\n";
		foreach($_POST as $key => $value) {
			$file.="\$config['".$key."']=\"".$value."\";\r\n";
			if(strlen($value)==0) {
				return "\t\t<p>".$lang['error_missing']."</p>\r\n\t\t<p>".$lang['error_goback']."</p>\r\n";
				exit();
			}
		}
		$file.="\$config['uri']=\"http://".$_SERVER['SERVER_NAME'].str_replace("admin.php?id=config","",$_SERVER['REQUEST_URI'])."\";\r\n";
		$file.="\r\n?>";
		if(!$handle=fopen("config.php","w+")) header("Location: error.php?id=config-rights");
		fwrite($handle,$file);
		fclose($handle);

		$output ="\t\t<h3>".ucwords($lang['config'])."</h3>\r\n";
		$output.="\t\t<table>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['lang'])."</th><td>".$config['lang']."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['theme'])."</th><td>".$config['theme']."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['email'])."</th><td>".htmlentities($config['email'],ENT_QUOTES)."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['title'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['title'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['subtitle'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['subtitle'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['meta_desc'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['meta_desc'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['meta_keys'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['meta_keys'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['forums'])."</th><td>";
		 if($config["forums"]==1) $output.=$lang['yes'];
		 elseif($config["forums"]==0) $output.=$lang['no'];
		 $output.="</td></tr>\r\n";
		$output.="\t\t\t<tr><th>".ucwords($lang['photos'])."</th><td>";
		 if($config['photos']==1) $output.=$lang['yes'];
		 elseif($config['photos']==0) $output.=$lang['no'];
		 $output.="</td></tr>\r\n";
		$output.="\t\t\t<tr><th>MySQL ".ucwords($lang['servername'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['mysql_host'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>MySQL ".ucwords($lang['username'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['mysql_user'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>MySQL ".ucwords($lang['password'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['mysql_pass'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t\t<tr><th>MySQL ".ucwords($lang['table'])."</th><td>".str_replace("&Acirc;","",htmlentities($config['mysql_table'],ENT_QUOTES))."</td></tr>\r\n";
		$output.="\t\t</table>\r\n";
		return $output;
	}
	else {
		if(!$handle=fopen("config.php","r+")) header("Location: error.php?id=config-rights");
		fclose($handle);

		$output ="\t\t<h3>".ucwords($lang['config'])."</h3>\r\n";
		$output.="\t\t<form action=\"admin.php?id=config\" method=\"post\">\r\n";
		$output.="\t\t<table>\r\n";
		$output.="\t\t\t<tr><th><label for=\"\">".ucwords($lang['lang'])."</label></th><td><select name=\"lang\">";
		 foreach (glob("langs/*.php") as $file) {
			$file=str_replace("langs/","",str_replace(".php","",$file));
			$output.="<option value=\"".$file."\"";
			 if($file==$config['lang']) $output.=" selected=\"selected\"";
			 $output.=">".$file."</option>";
		 }
		 $output.="</select></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['theme'])."</label></th><td><select name=\"theme\">";
		 foreach (glob("themes/*") as $folder) {
		  $folder=str_replace("themes/","",$folder);
		   $output.="<option value=\"".$folder."\"";
		   if($folder==$config['theme']) $output.=" selected=\"selected\"";
		   $output.=">".$folder."</option>";
		 }
		 $output.="</select></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['email'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"email\" value=\"".htmlentities($config['email'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['title'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"title\" value=\"".htmlentities($config['title'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['subtitle'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"subtitle\" value=\"".htmlentities($config['subtitle'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['meta_desc'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"meta_desc\" value=\"".htmlentities($config['meta_desc'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['meta_keys'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"meta_keys\" value=\"".htmlentities($config['meta_keys'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['forums'])."</label></th><td><select name=\"forums\"><option value=\"1\"";
		 if($config['forums']==1 || empty($_config['forums'])) $output.=" selected=\"selected\"";
		 $output.=">".$lang['yes']."</option><option value=\"0\"";
		 if($config['forums']==0) $output.=" selected=\"selected\"";
		 $output.=">".$lang['no']."</option></select></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">".ucwords($lang['photos'])."</label></th><td><select name=\"photos\"><option value=\"1\"";
		 if($config['photos']==1 || empty($_config['photos'])) $output.=" selected=\"selected\"";
		 $output.=">".$lang['yes']."</option><option value=\"0\"";
		 if($config['photos']==0) $output.=" selected=\"selected\"";
		 $output.=">".$lang['no']."</option></select></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">MySQL ".ucwords($lang['servername'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"mysql_host\" value=\"".htmlentities($config['mysql_host'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">MySQL ".ucwords($lang['username'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"mysql_user\" value=\"".htmlentities($config['mysql_user'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">MySQL ".ucwords($lang['password'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"mysql_pass\" value=\"".htmlentities($config['mysql_pass'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th><label for=\"\">MySQL ".ucwords($lang['table'])."</label></th><td><input class=\"inputtext\" id=\"\" type=\"text\" name=\"mysql_table\" value=\"".htmlentities($config['mysql_table'],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t<tr><th></th><td><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></td></tr>\r\n";
		$output.="\t</table>\r\n";
		$output.="</form>\r\n";
		return $output;
	}
}


/**
 * check latest against current version
 * @return string
 */
function eb_admin_update() {
	global $lang,$config;
	$latest=file_get_contents("http://www.eggblog.net/version.txt");
	if($latest==$config['version']) return "\t\t<h3>".$lang['update']."</h3>\r\n\t\t<p>".$lang['update_no'].".</p>\r\n";
	else return "\t\t<h3>".$lang['update']."</h3>\r\n\t\t<p>".$lang['update_yes'].".</p>\r\n\t\t<p><a href=\"http://eggblog.net/download.php\">eggBlog v".$latest."</a></p>\r\n";
}


/**
 * edit a comment
 */
function eb_admin_editcomment($comment,$article) {
	global $lang;
	if(!empty($_POST['text'])) {
		$sql="UPDATE eb_comments SET comment_body=\"".str_replace("&Acirc;","",htmlentities($_POST['text'],ENT_QUOTES))."\" WHERE comment_id=".$comment;
		@mysql_query($sql);
		header("Location: news.php?id=".$article);
	}
	else {
		$text=mysql_result(mysql_query("SELECT comment_body FROM eb_comments WHERE comment_id=".$comment),0);
		$output ="\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";
		$output.="\t\t<form action=\"admin.php?editcomment=".$comment."&article=".$article."\" method=\"post\">\r\n";
		$output.="\t\t\t<p><textarea name=\"text\">".htmlentities($text,ENT_QUOTES)."</textarea></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}


/**
 * edit a topic
 */
function eb_admin_edittopic($topic) {
	global $lang;
	if(!empty($_POST['text'])) {
		$sql="UPDATE eb_topics SET topic_title=\"".str_replace("&Acirc;","",htmlentities($_POST['text'],ENT_QUOTES))."\" WHERE topic_id=".$topic;
		@mysql_query($sql);
		header("Location: forum.php?topic=".$topic);
	}
	else {
		$text=mysql_result(mysql_query("SELECT topic_title FROM eb_topics WHERE topic_id=".$topic),0);
		$output ="\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";
		$output.="\t\t<form action=\"admin.php?edittopic=".$topic."\" method=\"post\">\r\n";
		$output.="\t\t\t<p><input class=\"inputtext\" type=\"text\" name=\"text\" value=\"".htmlentities($text,ENT_QUOTES)."\" /></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}



/**
 * edit a topic post
 */
function eb_admin_editpost($post,$topic) {
	global $lang;
	if(!empty($_POST['text'])) {
		$sql="UPDATE eb_posts SET post_body=\"".str_replace("&Acirc;","",$_POST['text'])."\" WHERE post_id=".$post;
		@mysql_query($sql);
		header("Location: forum.php?topic=".$topic);
	}
	else {
		$text=mysql_result(mysql_query("SELECT post_body FROM eb_posts WHERE post_id=".$post),0);
		$output ="\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";
		$output.="\t\t<form name=\"post\" action=\"admin.php?editpost=".$post."&topic=".$topic."\" method=\"post\">\r\n";
		$output.="\t\t\t<script type=\"text/javascript\">function smilie(emote) { document.post.commtext.value += emote; document.post.commtext.focus(); }</script>\r\n";
		$output.="\t\t\t<ul id=\"smilies\">\r\n";
		foreach(eb_smilies() as $smilie) $output.="\t\t\t\t<li><a href=\"#post\" onclick=\"smilie(' [".$smilie."] '); return false;\"><img src=\"smilies/".$smilie.".gif\" alt=\"".$smilie."\" width=\"16\" height=\"16\" /></a></li>\n";
		$output.="\t\t\t</ul>\r\n";
		$output.="\t\t\t<p><textarea id=\"commtext\" name=\"text\">".$text."</textarea></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}


/**
 * view members
 * @param $n int show n members per page
 * @param $i int page number to show
 */
function eb_admin_members($n) {
	global $lang;
	if(!empty($_GET['i'])) settype($_GET['i'],"integer");
	else $_GET['i']=1;
	$from=($_GET['i']-1)*10;
	$sql="SELECT * FROM eb_users ORDER BY user_name LIMIT ".$from.",".$n;
	$query=mysql_query($sql);
	$output ="\t\t<h3>".ucwords($lang['members'])."</h3>\r\n";
	$output.="\t\t<table width=\"100%\">\r\n";
	$output.="\t\t\t<tr><th>".ucwords($lang['username'])."</th><th>".ucwords($lang['email'])."</th><th colspan=2></th></tr>\r\n";
	while($row=mysql_fetch_array($query)) {
		$output.="\t\t\t<tr><td>".$row['user_name']."</td><td>".$row['user_email']."</td><td><a href=\"admin.php?editmember=".$row[0]."\">".$lang['edit']."</a></td><td>";
		if($row['user_flag']==0) $output.="<a href=\"admin.php?activatemember=".$row[0]."\">".$lang['activate']."</a>";
		else $output.="<a href=\"admin.php?deletemember=".$row[0]."\">".$lang['delete']."</a>";
		$output.="</td></tr>\r\n";
	}
	$output.="\t\t</table>\r\n";

	eb_head('');
	echo $output;
	echo eb_pages($n,$_GET['i'],mysql_num_rows(mysql_query("SELECT user_id FROM eb_users ORDER BY user_name")),"admin.php?id=members&");
	eb_foot();
}


/**
 * delete a user
 * @param $id int user/member id to delete
 */
function eb_admin_deletemember($id) {
	$sql="UPDATE eb_users SET user_flag=0 WHERE user_id=".$id;
	@mysql_query($sql);
	if($_SESSION['user_id']==$id) eb_logout();
	else header('Location: admin.php?id=members');
}


/**
 * activate a user
 * @param $id int user/member id to delete
 */
function eb_admin_activatemember($id) {
	$sql="UPDATE eb_users SET user_flag=1 WHERE user_id=".$id;
	@mysql_query($sql);
	if($_SESSION['user_id']==$id) eb_logout();
	else header('Location: admin.php?editmember='.$id);
}


/**
 * edit a member
 * @param $id int user/member id
 */
function eb_admin_editmember($id) {
	global $lang;
	if(!empty($_POST['email'])) {
		foreach($_POST as $key=>$value) $_POST[$key]=str_replace("&Acirc;","",htmlentities($value,ENT_QUOTES));
		$sql="UPDATE eb_users SET user_name=\"".$_POST['username']."\",user_email=\"".$_POST['email']."\",user_password=\"".$_POST['password']."\",user_flag=".$_POST['flag']." WHERE user_id=".$id;
		@mysql_query($sql);
		header('Location: admin.php?id=members');
	}
	else {
		$sql="SELECT * FROM eb_users WHERE user_id=".$id;
		$query=mysql_query($sql);
		$row=mysql_fetch_row($query);
		$output ="\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";
		$output.="\t\t<form action=\"admin.php?editmember=".$id."\" method=\"post\">\r\n";
		$output.="\t\t\t<table width=\"100%\">\r\n";
		$output.="\t\t\t\t<tr><th>".ucwords($lang['username'])."</th><td><input type=\"text\" class=\"inputtext\" name=\"username\" value=\"".htmlentities($row[2],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t\t\t<tr><th>".ucwords($lang['email'])."</th><td><input type=\"text\" class=\"inputtext\" name=\"email\" value=\"".htmlentities($row[3],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t\t\t<tr><th>".ucwords($lang['password'])."</th><td><input type=\"text\" class=\"inputtext\" name=\"password\" value=\"".htmlentities($row[4],ENT_QUOTES)."\" /></td></tr>\r\n";
		$output.="\t\t\t\t<tr><th>".ucwords($lang['admin'])."</th><td><input type=\"radio\" name=\"flag\" value=\"1\" ";
		 if($row[1]!=2) $output.=" checked=\"checked\"";
		 $output.="/> ".ucwords($lang['no'])."<br /><input type=\"radio\" name=\"flag\" value=\"2\" ";
		 if($row[1]==2) $output.=" checked=\"checked\"";
		 $output.="/> ".ucwords($lang['yes'])."</td></tr>\r\n";
		$output.="\t\t\t\t<tr><th></th><td><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></td></tr>\r\n";
		$output.="\t\t\t</table>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}


/**
 * edit an article
 * @param $id int article id to edit
 */
function eb_admin_editarticle($id) {
	global $lang;
	$output="\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";;
	if(!empty($_POST['text'])) {
		foreach($_POST as $key=>$value) $_POST[$key]=str_replace("&Acirc;","",htmlentities($value,ENT_QUOTES));
		if(strlen($_POST['tags'])>1) {
			@mysql_query("DELETE FROM eb_tags_links WHERE article_id=".$id);
			$tags=explode(",",$_POST['tags']);
			foreach($tags as $tag) {
				$sql="INSERT INTO eb_tags SET tag_name=\"".$tag."\"";
				@mysql_query($sql);
				if(mysql_affected_rows()==1) $tag_ids[]=mysql_insert_id();
				else $tag_ids[]=mysql_result(mysql_query("SELECT tag_id FROM eb_tags WHERE tag_name=\"".$tag."\""),0);
			}
			foreach($tag_ids as $tag_id) @mysql_query("INSERT INTO eb_tags_links SET tag_id=".$tag_id.",article_id=".$id);
		}


		$sql="UPDATE eb_articles SET article_flag=".$_POST['sticky'].", article_title=\"".$_POST['title']."\",article_body=\"".$_POST['text']."\",article_comments=".$_POST['comments']." WHERE article_id=".$id;
		@mysql_query($sql);
		header('Location: news.php?id='.$id);
	}
	else {
		$sql="SELECT article_title,article_body,article_comments,article_flag FROM eb_articles WHERE article_id=".$id;
		$query=mysql_query($sql);
		$row=mysql_fetch_row($query);

		$tags="SELECT tag_name FROM eb_tags LEFT JOIN eb_tags_links ON eb_tags_links.tag_id=eb_tags.tag_id WHERE article_id=".$id;
		$tags=mysql_query($tags);
		if(mysql_num_rows($tags)>0) {
			while($tags_row=mysql_fetch_array($tags)) $tag_array[]=$tags_row[0];
			$tags=implode(",",$tag_array);
		}
		else $tags="";

		$output.="\t\t<script type=\"text/javascript\" src=\"_lib/fckeditor/fckeditor.js\"></script>\r\n";
		$output.="\t\t<script type=\"text/javascript\">\r\n\t\t\twindow.onload = function() {\r\n\t\t\t\tvar oFCKeditor=new FCKeditor('text');\r\n\t\t\t\t
		oFCKeditor.Height = '500'; 
		oFCKeditor.BasePath=\"_lib/fckeditor/\";\r\n\t\t\t\toFCKeditor.ReplaceTextarea();\r\n\t\t\t}\r\n\t\t</script>\r\n";
		$output.="\t\t<form action=\"admin.php?editarticle=".$id."\" method=\"post\">\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['title'])."</b><br /><input class=\"inputtext\" type=\"text\" name=\"title\" value=\"".$row[0]."\" /></p>\r\n";
		$output.="\t\t\t<p><textarea id=\"text\" name=\"text\">".$row[1]."</textarea></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['tags'])."</b><br />".$lang['tag_seperate']."<br /><input type=\"text\" class=\"inputtext\" name=\"tags\" value=\"".$tags."\" /></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['sticky'])."</b><br /><input type=\"radio\" name=\"sticky\" value=\"1\" ";
		  if($row[3]==1) $output.=" checked=\"checked\"";
		  $output.="/> ".ucwords($lang['no'])."<br /><input type=\"radio\" name=\"sticky\" value=\"2\" ";
		  if($row[3]==2) $output.=" checked=\"checked\"";
		  $output.="/> ".ucwords($lang['yes'])."<br /></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['comments'])."</b><br /><input type=\"radio\" name=\"comments\" value=\"0\" ";
		  if($row[2]==0) $output.=" checked=\"checked\"";
		  $output.="/> ".ucwords($lang['no'])."<br /><input type=\"radio\" name=\"comments\" value=\"1\" ";
		  if($row[2]==1) $output.=" checked=\"checked\"";
		  $output.="/> ".ucwords($lang['yes'])."<br /></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}


/**
 * create a new a article
 */
function eb_admin_newarticle() {
	global $lang;
	$output="\t\t<h3>".ucwords($lang['new_article'])."</h3>\r\n";;
	if(!empty($_POST['text'])) {
		foreach($_POST as $key=>$value) $_POST[$key]=str_replace("&Acirc;","",htmlentities($value,ENT_QUOTES));
		$sql="INSERT INTO eb_articles SET article_flag=".$_POST['sticky'].",article_date=\"".time()."\",author_id=\"".$_SESSION['user_id']."\", article_title=\"".$_POST['title']."\",article_body=\"".$_POST['text']."\",article_comments=".$_POST['comments'];
		@mysql_query($sql);
		$article_id=mysql_insert_id();

		if(strlen($_POST['tags'])>1) {
			$tags=explode(",",$_POST['tags']);
			foreach($tags as $tag) {
				$sql="INSERT INTO eb_tags SET tag_name=\"".$tag."\"";
				@mysql_query($sql);
				if(mysql_affected_rows()==1) $tag_ids[]=mysql_insert_id();
				else $tag_ids[]=mysql_result(mysql_query("SELECT tag_id FROM eb_tags WHERE tag_name=\"".$tag."\""),0);
			}
			foreach($tag_ids as $tag_id) @mysql_query("INSERT INTO eb_tags_links SET tag_id=".$tag_id.",article_id=".$article_id);
		}

		header('Location: news.php?id='.$article_id);
	}
	else {
		$output.="\t\t<script type=\"text/javascript\" src=\"_lib/fckeditor/fckeditor.js\"></script>\r\n";
		$output.="\t\t<script type=\"text/javascript\">\r\n\t\t\twindow.onload = function() {\r\n\t\t\t\tvar oFCKeditor=new FCKeditor('text');\r\n\t\t\t\toFCKeditor.Height='500';\r\n\t\t\t\toFCKeditor.BasePath=\"_lib/fckeditor/\";\r\n\t\t\t\toFCKeditor.ReplaceTextarea();\r\n\t\t\t}\r\n\t\t</script>\r\n";
		$output.="\t\t<form action=\"admin.php?id=newarticle\" method=\"post\">\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['title'])."</b><br /><input class=\"inputtext\" type=\"text\" name=\"title\" /></p>\r\n";
		$output.="\t\t\t<p><textarea id=\"text\" name=\"text\"></textarea></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['tags'])."</b><br />".$lang['tag_seperate']."<br /><input type=\"text\" class=\"inputtext\" name=\"tags\" /></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['sticky'])."</b><br /><input type=\"radio\" name=\"sticky\" value=\"1\" checked=\"checked\" /> ".ucwords($lang['no'])."<br /><input type=\"radio\" name=\"sticky\" value=\"2\" /> ".ucwords($lang['yes'])."</p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['comments'])."</b><br /><input type=\"radio\" name=\"comments\" value=\"0\" /> ".ucwords($lang['no'])."<br /><input type=\"radio\" name=\"comments\" value=\"1\" checked=\"checked\" /> ".ucwords($lang['yes'])."</p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}


/*
 * delete photo
 * @param $photo int photo id to delete
 * @param $album int album id to return to
 */
function eb_admin_deletephoto($photo,$album) {
	$sql="DELETE FROM eb_photos WHERE photo_id=".$photo;
	@mysql_query($sql);
	unlink("photos/thumbnail/".$photo.".jpg");
	unlink("photos/full/".$photo.".jpg");
	header('Location: photos.php?album='.$album);
}


/*
 * delete album
 */
function eb_admin_deletealbum($album) {
	$sql="SELECT photo_id FROM eb_photos WHERE album_id=".$album;
	$query=mysql_query($sql);
	while($photo=mysql_fetch_array($query)) {
		@mysql_query("DELETE FROM eb_photos WHERE photo_id=".$photo[0]);
		unlink("photos/thumbnail/".$photo[0].".jpg");
		unlink("photos/full/".$photo[0].".jpg");
	}
	@mysql_query("DELETE FROM eb_albums WHERE album_id=".$album);
	unlink("photos/category/".$album.".jpg");
	header('Location: photos.php');
}


/**
 * edit a photo
 * @param $photo int photo id
 * @param $album int album id
 */
function eb_admin_editphoto($photo,$album) {
	global $lang;
	if(!empty($_POST['text'])) {
		$_POST['text']=htmlentities($_POST['text'],ENT_QUOTES);
		$sql="UPDATE eb_photos SET photo_description=\"".str_replace("&Acirc;","",$_POST['text'])."\" WHERE photo_id=".$photo;
		@mysql_query($sql);
		header("Location: photos.php?photo=".$photo."&album=".$album);
	}
	else {
		$text=mysql_result(mysql_query("SELECT photo_description FROM eb_photos WHERE photo_id=".$photo),0);
		$output ="\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";
		$output.="\t\t<form name=\"post\" action=\"admin.php?editphoto=".$photo."&album=".$album."\" method=\"post\">\r\n";
		$output.="\t\t\t<p><textarea name=\"text\">".$text."</textarea></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}


/**
 * edit an album
 * @param $id int album id
 */
function eb_admin_editalbum($id) {
	global $lang;
	if(!empty($_POST['text'])) {
		$_POST['text']=htmlentities($_POST['text'],ENT_QUOTES);
		$sql="UPDATE eb_albums SET album_title=\"".str_replace("&Acirc;","",$_POST['text'])."\" WHERE album_id=".$id;
		@mysql_query($sql);
		header("Location: photos.php?album=".$id);
	}
	else {
		$text=mysql_result(mysql_query("SELECT album_title FROM eb_albums WHERE album_id=".$id),0);
		$output ="\t\t<h3>".ucwords($lang['admin'])."</h3>\r\n";
		$output.="\t\t<form name=\"post\" action=\"admin.php?editalbum=".$id."\" method=\"post\">\r\n";
		$output.="\t\t\t<p><input type=\"text\" class=\"inputtext\" name=\"text\" value=\"".$text."\" /></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		eb_head('');
		echo $output;
		eb_foot();
	}
}


/**
 * submit a new photo
 */
function eb_admin_newphoto() {
	global $lang;
	if(!empty($_FILES['file']['tmp_name'])) {
		$_POST['text']=htmlentities($_POST['text'],ENT_QUOTES);
		$sql="INSERT INTO eb_photos SET album_id=".$_POST['album'].",photo_description=\"".str_replace("&Acirc;","",$_POST['text'])."\"";
		@mysql_query($sql);
		$photo=mysql_insert_id();

		if(eb_admin_photo($_FILES['file']['tmp_name'],"photos/full/".$photo.".jpg",0,0) && eb_admin_photo("photos/full/".$photo.".jpg","photos/thumbnail/".$photo.".jpg",170,0)) header("Location: photos.php?photo=".$photo."&album=".$_POST['album']);
		else {
			@mysql_query("DELETE FROM eb_photos WHERE photo_id=".$photo);
			header('Location: admin.php?id=newphoto');
		}
	}
	else {
		$output ="\t\t<h3>".ucwords($lang['new_photo'])."</h3>\r\n";
		$output.="\t\t<form name=\"post\" action=\"admin.php?id=newphoto\" method=\"post\" enctype=\"multipart/form-data\">\r\n";
		$output.="\t\t\t<p><textarea name=\"text\"></textarea></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['album'])."</b>: <select name=\"album\">";
		 $sql="SELECT album_id,album_title FROM eb_albums ORDER BY album_title";
		 $query=mysql_query($sql);
		 while($album=mysql_fetch_array($query)) $output.="<option value=\"".$album[0]."\">".$album[1]."</option>";
		 $output.="</select></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['new_photo'])."</b>: <input type=\"file\" name=\"file\" /></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		return $output;
	}
}


/**
 * create a new photo album
 */
function eb_admin_newalbum() {
	global $lang;
	if(!empty($_FILES['file']['tmp_name'])) {
		$_POST['text']=htmlentities($_POST['text'],ENT_QUOTES);
		$sql="INSERT INTO eb_albums SET album_title=\"".str_replace("&Acirc;","",$_POST['text'])."\"";
		@mysql_query($sql);
		$album=mysql_insert_id();

		@eb_admin_photo($_FILES['file']['tmp_name'],"photos/category/".$album.".jpg",83,83);

		header("Location: admin.php?id=newphoto");
	}
	else {
		$output ="\t\t<h3>".ucwords($lang['new_album'])."</h3>\r\n";
		$output.="\t\t<form name=\"post\" action=\"admin.php?id=newalbum\" method=\"post\" enctype=\"multipart/form-data\">\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['title'])."</b>: <input type=\"text\" class=\"inputtext\" name=\"text\" /></p>\r\n";
		$output.="\t\t\t<p><b>".ucwords($lang['new_photo'])."</b>: <input type=\"file\" name=\"file\" /></p>\r\n";
		$output.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords($lang['save'])."\" /></p>\r\n";
		$output.="\t\t</form>\r\n";
		return $output;
	}
}


/*
 * upload photo
 * @param $img string path to image
 * @param $photo photo id
 * @param $w max width if images
 */
function eb_admin_photo($img,$newimg,$maxw,$maxh) {
	ini_set("memory_limit","32M");
	$type=mime_content_type($img);
	if($type=='image/png') $src=imagecreatefrompng($img);
	elseif($type=='image/gif') $src=imagecreatefromgif($img);
	elseif($type=='image/bmp') $src=imagecreatefromwbmp($img);
	else $src=imagecreatefromjpeg($img);

	$w=imagesx($src);
	$h=imagesy($src);
	if($w>$maxw && $maxw>0) $ratio=$maxw/$w;
	else $ratio=1;
	$h_new=$ratio*$h;
	$w_new=$ratio*$w;
	if($h_new>$maxh && $maxh>0) {
		$newratio=$maxh/$h;
		if($newratio<$ratio) {
			$h_new=$ratio*$h;
			$w_new=$ratio*$w;
		}
	}
	$img=imagecreatetruecolor($w_new,$h_new);
	imagecopyresampled($img,$src,0,0,0,0,$w_new,$h_new,$w,$h);
	imagejpeg($img,$newimg,90);
	imagedestroy($img);

	if(file_exists($newimg)) return true;
	else return false;
}

?>