<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$subop = (isset($_GET['subop'])) ? inCode($_GET['subop']) : "" ;
$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0 ;
$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : false ;

function articles($id,$subop,$ok=false) {
	global $dblink,$siteConfig;
	
	switch ($subop) {
		default:
			$n = 0;
			echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
			echo "<thead>\n";
				echo "<tr><td>"._TITLE_."</td><td width='20%' nowrap>"._AUTHOR_."</td><td width='20%' nowrap>"._ARGUMENT_."</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
				$result = $dblink->get_list("SELECT *,DATE_FORMAT(data, '".$siteConfig['timestamp']."') as data FROM memht_articoli WHERE waiting=1 ORDER BY id");
				foreach ($result as $row) {
					$id = intval($row['id']);
					$argomento = intval($row['argomento']);
					$nome = outCode($row['nome']);
					$autore = outCode($row['autore']);
					
					$row_nome_arg = $dblink->get_row("SELECT nome FROM memht_argomenti WHERE id=$argomento");
					$nome_arg = outCode($row_nome_arg['nome']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
					echo "<tr><td class='$class'><a href='admin.php?page=submission&op=articles&subop=open&id=$id' title='$nome'><b>$nome</b></a></td><td class='$class'>$autore</td><td class='$class'>$nome_arg</td></tr>\n";
				}
				if ($n<=0) {
					echo "<tr><td colspan='2' align='center' id='errorText' class='clean'><b>"._EMPTY_."</b></td></tr>\n";
				}
			echo "</tbody>\n";
			echo "</table>";
		break;
		case "open":
			if ($row = $dblink->get_row("SELECT * FROM memht_articoli WHERE id=$id AND waiting=1")) {
				$argument = intval($row['argomento']);
				$name = outCode($row['nome']);
				$description = outCode($row['descrizione']);
				$text = outCode($row['testo']);
				$author = outCode($row['autore']);
				$email = outCode($row['email']);
				$language = intval($row['language']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
				echo "<form name='formsarticles' method='post' action='admin.php?page=submission&op=articles&subop=approve&id=$id'>";
				
					echo "<tr><td width='20%'><b>"._TITLE_."</b></td><td><input type='text' name='name' value=\"$name\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._ARGUMENT_."</b></td><td>\n";
						echo "<select name='argument'>\n";
							$result = $dblink->get_list("SELECT * FROM memht_argomenti ORDER BY nome");
							foreach ($result as $row) {
								$aid = intval($row['id']);
								$nome = outCode($row['nome']);
								
								if ($argument==$aid) {
									echo "<option value='$aid' selected>$nome</option>\n";
								} else {
									echo "<option value='$aid'>$nome</option>\n";
								}
							}
						echo "</select></td></tr>\n";
					echo "<tr><td><b>"._LANGUAGE_."</b></td><td>\n";
						echo "<select name='language'>\n";
							$selected = ($language==0) ? " selected" : "" ;
							echo "<option value='0'{$selected}>"._GLOBAL_."</option>\n";
							$result = $dblink->get_list("SELECT * FROM memht_content_languages ORDER BY language");
							foreach ($result as $row) {
								$lid = intval($row['id']);
								$llanguage = outCode($row['language']);
								
								$selected = ($language==$lid) ? " selected" : "" ;
								echo "<option value='$lid'{$selected}>$llanguage</option>\n";
							}
						echo "</select></td></tr>\n";
					echo "<tr><td><b>"._AUTHOR_."</b></td><td><input type='text' name='author' value=\"$author\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._EMAIL_."</b></td><td><input type='text' name='email' value=\"$email\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._DESCRIPTION_."</b><br>";
						textarea("description","100%","200px",1,"fulladmin",$description);
					echo "</td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._TEXT_."</b><br>";
						textarea("text","100%","400px",1,"fulladmin",$text);
					echo "</td></tr>\n";
					
					echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
					echo "<tr><td><b>"._AUTHOR_."</b></td><td><input type='text' name='autore' value=\"$author\" size='20' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
					echo "<select name='usecomments'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select></td></tr>\n";
					echo "<tr><td><b>"._ENABLED_."</b></td><td>";
					echo "<select name='enabled'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select>\n";
					
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._APPROVE_."' style='border:1px solid #7BC241; background-color:#AAD885;'> <input type='button' value='"._DELETE_."' style='border:1px solid #E4A79C; background-color:#EEC6BF;' onclick=\"location = 'admin.php?page=submission&op=articles&subop=delete&id=$id'\"></td></tr>\n";
					
				echo "</form>\n";
				echo "</table>\n";
			} else {
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			}
		break;
		case "approve":			
				$name = (isset($_POST['name'])) ? inCode($_POST['name']) : "" ;
				$argument = (isset($_POST['argument'])) ? intval($_POST['argument']) : 0 ;
				$description = (isset($_POST['description'])) ? inCode($_POST['description']) : "" ;
				$text = (isset($_POST['text'])) ? inCode($_POST['text']) : "" ;
				$author = (isset($_POST['author'])) ? inCode($_POST['author']) : "" ;
				$email = (isset($_POST['email'])) ? inCode($_POST['email']) : "" ;
				$usecomments = (isset($_POST['usecomments'])) ? intval($_POST['usecomments']) : 0 ;
				$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;
				$tags = (isset($_POST['tags'])) ? inCode($_POST['tags']) : "" ;
				$language = (isset($_POST['language'])) ? intval($_POST['language']) : 0 ;
				
				$save = true;
				if ($name=="" OR !validate($name)) { $save = false; $msg = _TITLE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($description=="") { $save = false; $msg = _DESCRIPTION_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($author=="" OR !validate($author)) { $save = false; $msg = _AUTHOR_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if (!validEmail($email)) { $save = false; $msg = _MAILNOTVALID_; }
				
				if ($save) {
					if (memRunHooks('ApproveArticle',array($name,$argument,$description,$text,$author,$email,$language))) {
						$dblink->query("UPDATE memht_articoli SET argomento='$argument',nome='$name',descrizione='$description',testo='$text',autore='$author',email='$email',usecomments='$usecomments',language='$language',waiting='0',enabled='$enabled' WHERE id=$id");
						if ($tags!="") {
							$tags = explode(",",$tags);
							foreach ($tags as $tag) {
								$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$id','1')");
							}
						}
				
						echo "<div align='center' class='box'><b>"._APPROVED_."</b></div>";
						
						memRunHooks('ApproveArticleEnd',array($name,$argument,$description,$text,$author,$email,$language));
					}
					echo "<meta http-equiv='refresh' content='3;URL=admin.php?page=submission'>";
				} else {
					echo "<div align='center' id='errorText'><b>$msg</b></div>";
				}
		break;
		case "delete":
			if ($ok) {
				$dblink->query("DELETE FROM memht_articoli WHERE id=$id");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=submission&op=articles&subop=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=submission' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
	}
}

function blog($id,$subop,$ok=false) {
	global $dblink,$siteConfig;
	
	switch ($subop) {
		default:
			$n = 0;
			echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
			echo "<thead>\n";
				echo "<tr><td>"._TITLE_."</td><td width='20%' nowrap>"._AUTHOR_."</td><td width='20%' nowrap>"._CATEGORY_."</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
				if ($result = $dblink->get_list("SELECT * FROM memht_blog_posts WHERE waiting=1 ORDER BY id")) {
					foreach ($result as $row) {
						$id = intval($row['id']);
						$category = intval($row['category']);
						$title = outCode($row['title']);
						$author = outCode($row['author']);
						
						$crow = $dblink->get_row("SELECT name FROM memht_blog_categories WHERE id=$category");
						$category = outCode($crow['name']);

						$class = (($n++%2)!=0) ? "hlight" : "clean" ;
						
						echo "<tr>\n";
						echo "<td class='$class'><a href='admin.php?page=submission&op=blog&subop=open&id=$id' title='$title'><b>$title</b></a></td>\n";
						echo "<td class='$class'>$author</td>\n";
						echo "<td class='$class'>$category</td>\n";
						echo "</tr>\n";
					}
				} else {
					echo "<tr><td align='center' id='errorText' class='clean' colspan='3'><b>"._EMPTY_."</b></td></tr>";
				}
			echo "</tbody>\n";
			echo "</table>";
		break;
		case "open":
			if ($row = $dblink->get_row("SELECT * FROM memht_blog_posts WHERE id=$id AND waiting=1")) {
				$category = intval($row['category']);
				$title = outCode($row['title']);
				$home_text = outCode($row['home_text']);
				$full_text = outCode($row['full_text']);
				$author = outCode($row['author']);
				$language = intval($row['language']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
				echo "<form name='formsarticles' method='post' action='admin.php?page=submission&op=blog&subop=approve&id=$id'>";
				
					echo "<tr><td width='20%'><b>"._TITLE_."</b></td><td><input type='text' name='title' value=\"$title\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._CATEGORY_."</b></td><td>\n";
						echo "<select name='category'>\n";
							$result = $dblink->get_list("SELECT id,name FROM memht_blog_categories ORDER BY name");
							foreach ($result as $row) {
								$cid = intval($row['id']);
								$name = outCode($row['name']);
								
								$selected = ($category==$cid) ? " selected" : "" ;
								echo "<option value='$cid'{$selected}>$name</option>\n";
							}
						echo "</select></td></tr>\n";
					echo "<tr><td><b>"._AUTHOR_."</b></td><td><input type='text' name='author' value=\"$author\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._LANGUAGE_."</b></td><td>\n";
						echo "<select name='language'>\n";
							$selected = ($language==0) ? " selected" : "" ;
							echo "<option value='0'{$selected}>"._GLOBAL_."</option>\n";
							$result = $dblink->get_list("SELECT * FROM memht_content_languages ORDER BY language");
							foreach ($result as $row) {
								$lid = intval($row['id']);
								$llanguage = outCode($row['language']);
								
								$selected = ($language==$lid) ? " selected" : "" ;
								echo "<option value='$lid'{$selected}>$llanguage</option>\n";
							}
						echo "</select></td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._HOMETEXT_."</b><br>";
						textarea("home","100%","200px",1,"fulladmin",$home_text);
					echo "</td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._TEXT_."</b><br>";
						textarea("text","100%","400px",1,"fulladmin",$full_text);
					echo "</td></tr>\n";
					
					echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
					echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
					echo "<select name='usecomments'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select></td></tr>\n";
					echo "<tr><td><b>"._ENABLED_."</b></td><td>";
					echo "<select name='enabled'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select>\n";
					
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._APPROVE_."' style='border:1px solid #7BC241; background-color:#AAD885;'> <input type='button' value='"._DELETE_."' style='border:1px solid #E4A79C; background-color:#EEC6BF;' onclick=\"location = 'admin.php?page=submission&op=blog&subop=delete&id=$id'\"></td></tr>\n";
					
				echo "</form>\n";
				echo "</table>\n";
			} else {
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			}
		break;
		case "approve":			
				$title = (isset($_POST['title'])) ? inCode($_POST['title']) : "" ;
				$category = (isset($_POST['category'])) ? intval($_POST['category']) : 0 ;
				$home = (isset($_POST['home'])) ? inCode($_POST['home']) : "" ;
				$text = (isset($_POST['text'])) ? inCode($_POST['text']) : "" ;
				$author = (isset($_POST['author'])) ? inCode($_POST['author']) : "" ;
				$email = (isset($_POST['email'])) ? inCode($_POST['email']) : "" ;
				$usecomments = (isset($_POST['usecomments'])) ? intval($_POST['usecomments']) : 0 ;
				$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;
				$tags = (isset($_POST['tags'])) ? inCode($_POST['tags']) : "" ;
				$language = (isset($_POST['language'])) ? intval($_POST['language']) : 0 ;
				
				$save = true;
				if ($title=="" OR !validate($title)) { $save = false; $msg = _TITLE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($home=="") { $save = false; $msg = _HOMETEXT_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($author=="" OR !validate($author)) { $save = false; $msg = _AUTHOR_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				
				if ($save) {
					if (memRunHooks('ApproveBlogPost',array($title,$category,$home,$text,$author,$language))) {
						$dblink->query("UPDATE memht_blog_posts SET category='$category',title='$title',home_text='$home',full_text='$text',author='$author',usecomments='$usecomments',language='$language',waiting='0',enabled='$enabled' WHERE id=$id");
						
						if ($tags!="") {
							$tags = explode(",",$tags);
							foreach ($tags as $tag) {
								$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".trim($tag)."','$id','5')");
							}
						}
				
						echo "<div align='center' class='box'><b>"._APPROVED_."</b></div>";
						
						memRunHooks('ApproveBlogPostEnd',array($title,$category,$home,$text,$author,$language));
					}
					echo "<meta http-equiv='refresh' content='3;URL=admin.php?page=submission'>";
				} else {
					echo "<div align='center' id='errorText'><b>$msg</b></div>";
				}
		break;
		case "delete":
			if ($ok) {
				$dblink->query("DELETE FROM memht_blog_posts WHERE id=$id");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=submission&op=blog&subop=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=submission' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
	}
}

function download($id,$subop,$ok=false) {
	global $dblink,$siteConfig,$level,$spcs;
	
	switch ($subop) {
		default:
			$n = 0;
			echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
			echo "<thead>\n";
			echo "<tr><td>"._FILENAME_."</td><td width='20%'>"._AUTHOR_."</td><td width='20%'>"._CATEGORY_."</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
			if ($result_sf = $dblink->get_list("SELECT *,DATE_FORMAT(data, '".$siteConfig['timestamp']."') as data2 FROM memht_download WHERE waiting=1 ORDER BY id")) {
				foreach ($result_sf as $row_sf) {
					$id = intval($row_sf['id']);
					$nome = outCode($row_sf['nome']);
					$autore = outCode($row_sf['autore']);
					$cat = intval($row_sf['cat']);
					
					$row = $dblink->get_row("SELECT nome FROM memht_download_categorie WHERE id=$cat");
					$catname = outCode($row['nome']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class'><a href='admin.php?page=submission&op=download&subop=open&id=$id' title='$nome'><b>$nome</b></a></td><td class='$class'>$autore</td><td class='$class'>$catname</td></tr>\n";
				}
			} else {
				echo "<tr><td align='center' id='errorText' colspan='4' class='clean'><b>"._EMPTY_."</b></td></tr>";
			}
			echo "</tbody>\n";
			echo "</table>";
		break;
		case "open":
			if ($row_sfe = $dblink->get_row("SELECT * FROM memht_download WHERE id=$id AND waiting=1")) {
				$cat = intval($row_sfe['cat']);
				$nome = outCode($row_sfe['nome']);
				$url = outCode($row_sfe['url']);
				$demolink = outCode($row_sfe['demolink']);
				$version = outCode($row_sfe['version']);
				$descrizione = outCode($row_sfe['descrizione']);
				$size = intval($row_sfe['dimensione']);
				$author = outCode($row_sfe['autore']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
				echo "<form name='formsarticles' method='post' action='admin.php?page=submission&op=download&subop=approve&id=$id'>";
				
					echo "<tr><td width='20%'><b>"._FILENAME_."</b></td><td><input type='text' name='name' value=\"$nome\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._URL_."</b></td><td><input type='text' name='url' value=\"$url\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._CATEGORY_."</b></td><td>\n";
					echo "<select name='category'>\n";
						if ($result = $dblink->get_list("SELECT id,nome FROM memht_download_categorie WHERE parent=0 ORDER BY nome")) {
							foreach ($result as $row) {
								$cid = intval($row['id']);
								$cnome = outCode($row['nome']);
								
								$selected = ($cat==$cid) ? " selected" : "" ;
								echo "<option value='$cid'{$selected}>$cnome</div>";
								godeepFiles($cid,$level,$spcs,$cat);
							}
						}
					echo "</select>\n";
	
					echo "</select></td></tr>\n";
					echo "<tr><td><b>"._DIMENSION_."</b></td><td><input type='text' name='size' value=\"$size\" size='20' maxlength='255'> bytes</td></tr>\n";
					echo "<tr><td><b>"._DEMO_."</b></td><td><input type='text' name='demolink' value=\"$demolink\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._VERSION_."</b></td><td><input type='text' name='version' value=\"$version\" size='20' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._AUTHOR_."</b></td><td><input type='text' name='author' value=\"$author\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._DESCRIPTION_."</b><br>\n";
						textarea("description","100%","200px",1,"simple",$descrizione);
					echo "</td></tr>\n";
					
					echo "<tr><td><b>"._PERMISSION_."</b></td><td>\n";
					echo "<select name='permission'>\n";
						echo "<option value='0' selected>"._PUBLIC_."</option>\n";
						echo "<option value='1'>"._REGISTERED_." ("._USER_.")</option>\n";
						echo "<option value='2'>"._GROUP_."</option>\n";
					echo "</select></td></tr>\n";
					
					echo "<tr><td><b>"._GROUP_."</b></td><td>\n";
					echo "<select name='group'>\n";
						echo "<option value='0' selected>-</option>\n";
						$result = $dblink->get_list("SELECT * FROM memht_groups ORDER BY name");
						foreach ($result as $row) {
							$gid = intval($row['id']);
							$gname = outCode($row['name']);
								
							echo "<option value='$gid'>$gname</option>\n";
						}
					echo "</select></td></tr>\n";
					
					echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
					echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
					echo "<select name='usecomments'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select></td></tr>\n";
					echo "<tr><td><b>"._ENABLED_."</b></td><td>";
					echo "<select name='enabled'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select>\n";
					
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._APPROVE_."' style='border:1px solid #7BC241; background-color:#AAD885;'> <input type='button' value='"._DELETE_."' style='border:1px solid #E4A79C; background-color:#EEC6BF;' onclick=\"location = 'admin.php?page=submission&op=download&subop=delete&id=$id'\"></td></tr>\n";
					
				echo "</form>\n";
				echo "</table>\n";
			} else {
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			}
		break;
		case "approve":			
				$name = (isset($_POST['name'])) ? inCode($_POST['name']) : "" ;
				$url = (isset($_POST['url'])) ? inCode($_POST['url']) : "" ;
				$category = (isset($_POST['category'])) ? intval($_POST['category']) : 0 ;
				$size = (isset($_POST['size'])) ? intval($_POST['size']) : 0 ;
				$demolink = (isset($_POST['demolink'])) ? inCode($_POST['demolink']) : "" ;
				$version = (isset($_POST['version'])) ? inCode($_POST['version']) : "" ;
				$author = (isset($_POST['author'])) ? inCode($_POST['author']) : "" ;
				$description = (isset($_POST['description'])) ? inCode($_POST['description']) : "" ;
				$permission = (isset($_POST['permission'])) ? intval($_POST['permission']) : 0 ;
				$group = (isset($_POST['group'])) ? intval($_POST['group']) : 0 ;				
				$usecomments = (isset($_POST['usecomments'])) ? intval($_POST['usecomments']) : 0 ;
				$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;
				$tags = (isset($_POST['tags'])) ? inCode($_POST['tags']) : "" ;
				
				$save = true;
				if ($name=="") { $save = false; $msg = _FILENAME_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($permission<2) { $group = 0; }
				if ($permission==2 AND $group==0) { $save = false; $msg = _GROUP_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				
				if ($save) {
					if (memRunHooks('ApproveFile',array($category,$name,$url,$description,$author,$demolink,$version,$size,$permission,$group))) {
						$dblink->query("UPDATE memht_download SET cat='$category',nome='$name',url='$url',descrizione='$description',dimensione='$size',demolink='$demolink',autore='$author',version='$version',flag='$permission',pgroup='$group',usecomments='$usecomments',enabled='$enabled',waiting=0 WHERE id=$id");
						if ($tags!="") {
							$tags = explode(",",$tags);
							foreach ($tags as $tag) {
								$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$id','4')");
							}
						}
						
						echo "<div align='center' class='box'><b>"._APPROVED_."</b></div>";
						
						memRunHooks('ApproveFileEnd',array($category,$name,$url,$description,$author,$demolink,$version,$size,$permission,$group));
					}
					echo "<meta http-equiv='refresh' content='3;URL=admin.php?page=submission'>";
				} else {
					echo "<div align='center' id='errorText'><b>$msg</b></div>";
				}
		break;
		case "delete":
			if ($ok) {
				$dblink->query("DELETE FROM memht_download WHERE id=$id");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=submission&op=download&subop=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=submission' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
	}
}

function gallery($id,$subop,$ok=false) {
	global $dblink,$siteConfig;
	
	switch ($subop) {
		default:
			$n = 0;
			echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
			echo "<thead>\n";
				echo "<tr><td>"._TITLE_."</td><td width='20%' nowrap>"._CATEGORY_."</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
				if ($result = $dblink->get_list("SELECT * FROM memht_gallery_images WHERE waiting=1 ORDER BY id")) {
					foreach ($result as $row) {
						$id = intval($row['id']);
						$category = intval($row['category']);
						$title = outCode($row['title']);
						
						$crow = $dblink->get_row("SELECT name FROM memht_gallery_categories WHERE id=$category");
						$category = outCode($crow['name']);

						$class = (($n++%2)!=0) ? "hlight" : "clean" ;
						
						echo "<tr>\n";
						echo "<td class='$class'><a href='admin.php?page=submission&op=gallery&subop=open&id=$id' title='$title'><b>$title</b></a></td>\n";
						echo "<td class='$class'>$category</td>\n";
						echo "</tr>\n";
					}
				} else {
					echo "<tr><td align='center' id='errorText' class='clean' colspan='2'><b>"._EMPTY_."</b></td></tr>";
				}
			echo "</tbody>\n";
			echo "</table>";
		break;
		case "open":
			if ($row = $dblink->get_row("SELECT * FROM memht_gallery_images WHERE id=$id AND waiting=1")) {
				$id = intval($row['id']);
				$category = intval($row['category']);
				$title = outCode($row['title']);
				$imgfile = outCode($row['imgfile']);
				$thumb = outCode($row['thumb']);
				$attachfile = outCode($row['attachfile']);
				$description = outCode($row['description']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
				echo "<form name='formsarticles' method='post' action='admin.php?page=submission&op=gallery&subop=approve&id=$id'>";
				
					echo "<tr><td width='20%'><b>"._CATEGORY_."</b></td><td>";
					echo "<select name='category'>\n";
						$cres = $dblink->get_list("SELECT * FROM memht_gallery_categories WHERE enabled=1 ORDER BY name");
						foreach ($cres as $crow) {
							$cid = intval($crow['id']);
							$cname = outCode($crow['name']);
							
							echo ($category==$cid) ? "<option value='$cid' selected>$cname</option>\n" : "<option value='$cid'>$cname</option>\n" ;
						}
					echo "</select></td></tr>\n";
					echo "<tr><td><b>"._NAME_."</b></td><td><input type='text' name='name' value=\"$title\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td valign='top'><b>"._IMAGE_."</b></td><td><a href='pages/gallery/gallery/$imgfile' title=\"$title\" target='_blank'><img src='pages/gallery/gallery/thumb/$thumb' border='0'></a></td></tr>\n";
					echo "<tr><td><b>"._ATTACHMENT_."</b></td><td><input type='text' name='attachment' value=\"$attachfile\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._DESCRIPTION_."</b><br>";
						textarea("description","100%","200px",1,"fulladmin",$description);
					echo "</td></tr>\n";
					
					echo "<tr><td><b>"._ENABLED_."</b></td><td>";
					echo "<select name='enabled'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select>\n";
					
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._APPROVE_."' style='border:1px solid #7BC241; background-color:#AAD885;'> <input type='button' value='"._DELETE_."' style='border:1px solid #E4A79C; background-color:#EEC6BF;' onclick=\"location = 'admin.php?page=submission&op=gallery&subop=delete&id=$id'\"></td></tr>\n";
					
				echo "</form>\n";
				echo "</table>\n";
			} else {
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			}
		break;
		case "approve":			
				$name = (isset($_POST['name'])) ? inCode($_POST['name']) : "" ;
				$category = (isset($_POST['category'])) ? intval($_POST['category']) : 0 ;
				$description = (isset($_POST['description'])) ? inCode($_POST['description']) : "" ;
				$attachment = (isset($_POST['attachment'])) ? inCode($_POST['attachment']) : "" ;
				$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;
				
				$save = true;
				if ($name=="" OR !validate($name)) { $save = false; $msg = _TITLE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				
				if ($save) {
					$dblink->query("UPDATE memht_gallery_images SET category='$category',title='$name',attachfile='$attachment',description='$description',waiting=0,enabled='$enabled' WHERE id=$id");
					
					echo "<div align='center' class='box'><b>"._APPROVED_."</b></div>";
					echo "<meta http-equiv='refresh' content='3;URL=admin.php?page=submission'>";
				} else {
					echo "<div align='center' id='errorText'><b>$msg</b></div>";
				}
		break;
		case "delete":
			if ($ok) {
				$row = $dblink->get_row("SELECT * FROM memht_gallery_images WHERE id=$id");
				$imgfile = outCode($row['imgfile']);
				$fname = file_name($imgfile);
				$fext = file_ext($imgfile);
				$imgfile_th = $fname."_thumb.".$fext;
				if ($imgfile!="") { @unlink("pages/gallery/gallery/".$imgfile); }
				if ($imgfile_th!="") { @unlink("pages/gallery/gallery/thumb/".$imgfile_th); }
				
				$dblink->query("DELETE FROM memht_gallery_images WHERE id=$id");
				
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=submission&op=gallery&subop=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=submission' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
	}
}

function guide($id,$subop,$ok=false) {
	global $dblink,$siteConfig;
	
	switch ($subop) {
		default:
			$n = 0;
			echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
			echo "<thead>\n";
				echo "<tr><td>"._TITLE_."</td><td width='20%' nowrap>"._AUTHOR_."</td><td width='20%' nowrap>"._ARGUMENT_."</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
				$result = $dblink->get_list("SELECT *,DATE_FORMAT(data, '".$siteConfig['timestamp']."') as data FROM memht_guide WHERE waiting=1 ORDER BY id");
				foreach ($result as $row) {
					$id = intval($row['id']);
					$argomento = intval($row['argomento']);
					$nome = outCode($row['nome']);
					$autore = outCode($row['autore']);
					
					$row_nome_arg = $dblink->get_row("SELECT nome FROM memht_argomenti WHERE id=$argomento");
					$nome_arg = outCode($row_nome_arg['nome']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
					echo "<tr><td class='$class'><a href='admin.php?page=submission&op=guide&subop=open&id=$id' title='$nome'><b>$nome</b></a></td><td class='$class'>$autore</td><td class='$class'>$nome_arg</td></tr>\n";
				}
				if ($n<=0) {
					echo "<tr><td colspan='2' align='center' id='errorText' class='clean'><b>"._EMPTY_."</b></td></tr>\n";
				}
			echo "</tbody>\n";
			echo "</table>";
		break;
		case "open":
			if ($row = $dblink->get_row("SELECT * FROM memht_guide WHERE id=$id AND waiting=1")) {
				$argument = intval($row['argomento']);
				$name = outCode($row['nome']);
				$description = outCode($row['descrizione']);
				$text = outCode($row['testo']);
				$author = outCode($row['autore']);
				$email = outCode($row['email']);
				$language = intval($row['language']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
				echo "<form name='formsarticles' method='post' action='admin.php?page=submission&op=guide&subop=approve&id=$id'>";
				
					echo "<tr><td width='20%'><b>"._TITLE_."</b></td><td><input type='text' name='name' value=\"$name\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._ARGUMENT_."</b></td><td>\n";
						echo "<select name='argument'>\n";
							$result = $dblink->get_list("SELECT * FROM memht_argomenti ORDER BY nome");
							foreach ($result as $row) {
								$aid = intval($row['id']);
								$nome = outCode($row['nome']);
								
								if ($argument==$aid) {
									echo "<option value='$aid' selected>$nome</option>\n";
								} else {
									echo "<option value='$aid'>$nome</option>\n";
								}
							}
						echo "</select></td></tr>\n";
					echo "<tr><td><b>"._LANGUAGE_."</b></td><td>\n";
						echo "<select name='language'>\n";
							$selected = ($language==0) ? " selected" : "" ;
							echo "<option value='0'{$selected}>"._GLOBAL_."</option>\n";
							$result = $dblink->get_list("SELECT * FROM memht_content_languages ORDER BY language");
							foreach ($result as $row) {
								$lid = intval($row['id']);
								$llanguage = outCode($row['language']);
								
								$selected = ($language==$lid) ? " selected" : "" ;
								echo "<option value='$lid'{$selected}>$llanguage</option>\n";
							}
						echo "</select></td></tr>\n";
					echo "<tr><td><b>"._AUTHOR_."</b></td><td><input type='text' name='author' value=\"$author\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._EMAIL_."</b></td><td><input type='text' name='email' value=\"$email\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._DESCRIPTION_."</b><br>";
						textarea("description","100%","200px",1,"fulladmin",$description);
					echo "</td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._TEXT_."</b><br>";
						textarea("text","100%","400px",1,"fulladmin",$text);
					echo "</td></tr>\n";
					
					echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
					echo "<tr><td><b>"._AUTHOR_."</b></td><td><input type='text' name='autore' value=\"$author\" size='20' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
					echo "<select name='usecomments'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select></td></tr>\n";
					echo "<tr><td><b>"._ENABLED_."</b></td><td>";
					echo "<select name='enabled'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select>\n";
					
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._APPROVE_."' style='border:1px solid #7BC241; background-color:#AAD885;'> <input type='button' value='"._DELETE_."' style='border:1px solid #E4A79C; background-color:#EEC6BF;' onclick=\"location = 'admin.php?page=submission&op=guide&subop=delete&id=$id'\"></td></tr>\n";
					
				echo "</form>\n";
				echo "</table>\n";
			} else {
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			}
		break;
		case "approve":			
				$name = (isset($_POST['name'])) ? inCode($_POST['name']) : "" ;
				$argument = (isset($_POST['argument'])) ? intval($_POST['argument']) : 0 ;
				$description = (isset($_POST['description'])) ? inCode($_POST['description']) : "" ;
				$text = (isset($_POST['text'])) ? inCode($_POST['text']) : "" ;
				$author = (isset($_POST['author'])) ? inCode($_POST['author']) : "" ;
				$email = (isset($_POST['email'])) ? inCode($_POST['email']) : "" ;
				$usecomments = (isset($_POST['usecomments'])) ? intval($_POST['usecomments']) : 0 ;
				$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;
				$tags = (isset($_POST['tags'])) ? inCode($_POST['tags']) : "" ;
				$language = (isset($_POST['language'])) ? intval($_POST['language']) : 0 ;
				
				$save = true;
				if ($name=="" OR !validate($name)) { $save = false; $msg = _TITLE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($description=="") { $save = false; $msg = _DESCRIPTION_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($author=="" OR !validate($author)) { $save = false; $msg = _AUTHOR_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if (!validEmail($email)) { $save = false; $msg = _MAILNOTVALID_; }
				
				if ($save) {
					if (memRunHooks('ApproveGuide',array($name,$argument,$description,$text,$author,$email,$language))) {
						$dblink->query("UPDATE memht_guide SET argomento='$argument',nome='$name',descrizione='$description',testo='$text',autore='$author',email='$email',usecomments='$usecomments',language='$language',waiting='0',enabled='$enabled' WHERE id=$id");
						if ($tags!="") {
							$tags = explode(",",$tags);
							foreach ($tags as $tag) {
								$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$id','2')");
							}
						}
						
						memRunHooks('ApproveGuideEnd',array($name,$argument,$description,$text,$author,$email,$language));
					}
			
					echo "<div align='center' class='box'><b>"._APPROVED_."</b></div>";
					echo "<meta http-equiv='refresh' content='3;URL=admin.php?page=submission'>";
				} else {
					echo "<div align='center' id='errorText'><b>$msg</b></div>";
				}
		break;
		case "delete":
			if ($ok) {
				$dblink->query("DELETE FROM memht_guide WHERE id=$id");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=submission&op=guide&subop=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=submission' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
	}
}

function mylinks($id,$subop,$ok=false) {
	global $dblink,$level,$spcs;
	
	switch ($subop) {
		default:
			$n = 0;
			echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
			echo "<thead>\n";
			echo "<tr><td>"._SITENAME_."</td><td width='50%'>"._URL_."</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
			if ($result_sf = $dblink->get_list("SELECT * FROM memht_mylinks WHERE flag=0 ORDER BY id")) {
				foreach ($result_sf as $row_sf) {
					$id = intval($row_sf['id']);
					$name = outCode($row_sf['name']);
					$url = outCode($row_sf['url']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class'><a href='admin.php?page=submission&op=mylinks&subop=open&id=$id' title='$name'><b>$name</b></a></td><td class='$class'><a href='$url' target='_blank' title='$url'>$url</a></td></tr>\n";
				}
			} else {
				echo "<tr><td align='center' id='errorText' colspan='2' class='clean'><b>"._EMPTY_."</b></td></tr>";
			}
			echo "</tbody>\n";
			echo "</table>";
		break;
		case "open":
			if ($row = $dblink->get_row("SELECT * FROM memht_mylinks WHERE id=$id AND flag=0")) {
				$cat = intval($row['cat']);
				$name = outCode($row['name']);
				$url = outCode($row['url']);
				$email = outCode($row['email']);
				$description = outCode($row['description']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
				echo "<form name='formsarticles' method='post' action='admin.php?page=submission&op=mylinks&subop=approve&id=$id'>";
				
					echo "<tr><td width='25%'><b>"._CATEGORY_."</b></td><td>\n";
			
					echo "<select name='category'>\n";
						if ($result = $dblink->get_list("SELECT id,name FROM memht_mylinks_cat WHERE parent=0 ORDER BY name")) {
							foreach ($result as $row) {
								$cid = intval($row['id']);
								$cnome = outCode($row['name']);
								
								$selected = ($cat==$cid) ? " selected" : "" ;
								echo "<option value='$cid'{$selected}>$cnome</div>";
								godeep($cid,$level,$spcs,$cat);
							}
						}
					echo "</select>\n";
		
					echo "</td>\n";
					echo "<tr><td><b>"._SITENAME_."</b></td><td><input type='text' name='name' value=\"$name\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._SITEURL_."</b></td><td><input type='text' name='url' value=\"$url\" size='40' maxlength='255' value='http://'></td></tr>\n";
					echo "<tr><td><b>"._EMAIL_."</b></td><td><input type='text' name='email' value=\"$email\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._DESCRIPTION_."</b><br>";
						textarea("description","100%","200px",1,"fulladmin",$description);
					echo "</td></tr>\n";
					
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._APPROVE_."' style='border:1px solid #7BC241; background-color:#AAD885;'> <input type='button' value='"._DELETE_."' style='border:1px solid #E4A79C; background-color:#EEC6BF;' onclick=\"location = 'admin.php?page=submission&op=mylinks&subop=delete&id=$id'\"></td></tr>\n";
					
				echo "</form>\n";
				echo "</table>\n";
			} else {
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			}
		break;
		case "approve":			
				$name = (isset($_POST['name'])) ? inCode($_POST['name']) : "" ;
				$category = (isset($_POST['category'])) ? inCode($_POST['category']) : "" ;
				$url = (isset($_POST['url'])) ? inCode($_POST['url']) : "" ;
				$description = (isset($_POST['description'])) ? inCode($_POST['description']) : "" ;
				$email = (isset($_POST['email'])) ? inCode($_POST['email']) : "" ;
				
				$save = true;
				if ($name=="") { $save = false; $msg = _SITENAME_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if (!validate($url,"url")) { $save = false; $msg = _SITEURL_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if (!validEmail($email)) { $save = false; $msg = _MAILNOTVALID_; }
				if ($description=="") { $save = false; $msg = _DESCRIPTION_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				
				if ($save) {
					if (memRunHooks('ApproveLink',array($category,$name,$url,$email,$description))) {
						$dblink->query("UPDATE memht_mylinks SET cat='$category',name='$name',url='$url',email='$email',description='$description',flag='1' WHERE id=$id");
						
						memRunHooks('ApproveLinkEnd',array($category,$name,$url,$email,$description));
					}
					echo "<div align='center' class='box'><b>"._APPROVED_."</b></div>";
					echo "<meta http-equiv='refresh' content='3;URL=admin.php?page=submission'>";
				} else {
					echo "<div align='center' id='errorText'><b>$msg</b></div>";
				}
		break;
		case "delete":
			if ($ok) {
				$dblink->query("DELETE FROM memht_mylinks WHERE id=$id");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=submission&op=mylinks&subop=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=submission' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
	}
}

function news($id,$subop,$ok=false) {
	global $dblink,$siteConfig;
	
	switch ($subop) {
		default:
			$n = 0;
			echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
			echo "<thead>\n";
			echo "<tr><td>"._TITLE_."<td width='20%'>"._AUTHOR_."</td></td><td width='20%'>"._ARGUMENT_."</td></tr>\n";
			echo "</thead>\n";
			echo "<tbody>\n";
			if ($result = $dblink->get_list("SELECT *,DATE_FORMAT(data, '".$siteConfig['timestamp']."') as data2 FROM memht_news WHERE waiting=1 ORDER BY id")) {
				foreach ($result as $row) {
					$id = intval($row['id']);
					$argomento = intval($row['argomento']);
					$nome = outCode($row['nome']);
					$autore = outCode($row['autore']);
					
					$row_nome_arg = $dblink->get_row("SELECT nome FROM memht_argomenti WHERE id=$argomento");
					$nome_arg = outCode($row_nome_arg['nome']);
					
					$class = (($n++%2)!=0) ? "hlight" : "clean" ;
					
					echo "<tr><td class='$class'><a href='admin.php?page=submission&op=news&subop=open&id=$id' title='$nome'><b>$nome</b></a><td class='$class'>$autore</td><td class='$class'>$nome_arg</td></tr>\n";
				}
			} else {
				echo "<tr><td align='center' id='errorText' colspan='4' class='clean'><b>"._EMPTY_."</b></td></tr>";
			}
			echo "</tbody>\n";
			echo "</table>";
		break;
		case "open":
			if ($row = $dblink->get_row("SELECT * FROM memht_news WHERE id=$id AND waiting=1")) {
				$argument = intval($row['argomento']);
				$name = outCode($row['nome']);
				$home = outCode($row['testo_home']);
				$text = outCode($row['testo']);
				$author = outCode($row['autore']);
				$email = outCode($row['email']);
				$language = intval($row['language']);
			
				echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
				echo "<form name='formsarticles' method='post' action='admin.php?page=submission&op=news&subop=approve&id=$id'>";
				
					echo "<tr><td width='20%'><b>"._TITLE_."</b></td><td><input type='text' name='name' value=\"$name\" size='40' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._ARGUMENT_."</b></td><td>\n";
						echo "<select name='argument'>\n";
							$result = $dblink->get_list("SELECT * FROM memht_argomenti ORDER BY nome");
							foreach ($result as $row) {
								$aid = intval($row['id']);
								$nome = outCode($row['nome']);
								
								if ($argument==$aid) {
									echo "<option value='$aid' selected>$nome</option>\n";
								} else {
									echo "<option value='$aid'>$nome</option>\n";
								}
							}
						echo "</select></td></tr>\n";
					echo "<tr><td><b>"._LANGUAGE_."</b></td><td>\n";
						echo "<select name='language'>\n";
							$selected = ($language==0) ? " selected" : "" ;
							echo "<option value='0'{$selected}>"._GLOBAL_."</option>\n";
							$result = $dblink->get_list("SELECT * FROM memht_content_languages ORDER BY language");
							foreach ($result as $row) {
								$lid = intval($row['id']);
								$llanguage = outCode($row['language']);
								
								$selected = ($language==$lid) ? " selected" : "" ;
								echo "<option value='$lid'{$selected}>$llanguage</option>\n";
							}
						echo "</select></td></tr>\n";
					echo "<tr><td><b>"._AUTHOR_."</b></td><td><input type='text' name='author' value=\"$author\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td><b>"._EMAIL_."</b></td><td><input type='text' name='email' value=\"$email\" size='30' maxlength='255'></td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._HOMETEXT_."</b><br>";
						textarea("home","100%","200px",1,"fulladmin",$home);
					echo "</td></tr>\n";
					echo "<tr><td colspan='2'>&nbsp;</td></tr>\n";
					echo "<tr><td valign='top' colspan='2'><b>"._TEXT_."</b><br>";
						textarea("text","100%","400px",1,"fulladmin",$text);
					echo "</td></tr>\n";
					
					echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
					echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
					echo "<select name='usecomments'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select></td></tr>\n";
					echo "<tr><td><b>"._ENABLED_."</b></td><td>";
					echo "<select name='enabled'>\n";
						echo "<option value='1' selected>"._YES_."</option>\n";
						echo "<option value='0'>"._NO_."</option>\n";
					echo "</select>\n";
					
					echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._APPROVE_."' style='border:1px solid #7BC241; background-color:#AAD885;'> <input type='button' value='"._DELETE_."' style='border:1px solid #E4A79C; background-color:#EEC6BF;' onclick=\"location = 'admin.php?page=submission&op=news&subop=delete&id=$id'\"></td></tr>\n";
					
				echo "</form>\n";
				echo "</table>\n";
			} else {
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			}
		break;
		case "approve":			
				$name = (isset($_POST['name'])) ? inCode($_POST['name']) : "" ;
				$argument = (isset($_POST['argument'])) ? intval($_POST['argument']) : 0 ;
				$home = (isset($_POST['home'])) ? inCode($_POST['home']) : "" ;
				$text = (isset($_POST['text'])) ? inCode($_POST['text']) : "" ;
				$author = (isset($_POST['author'])) ? inCode($_POST['author']) : "" ;
				$email = (isset($_POST['email'])) ? inCode($_POST['email']) : "" ;
				$usecomments = (isset($_POST['usecomments'])) ? intval($_POST['usecomments']) : 0 ;
				$enabled = (isset($_POST['enabled'])) ? intval($_POST['enabled']) : 0 ;
				$tags = (isset($_POST['tags'])) ? inCode($_POST['tags']) : "" ;
				$language = (isset($_POST['language'])) ? intval($_POST['language']) : 0 ;
				
				$save = true;
				if ($name=="" OR !validate($name)) { $save = false; $msg = _TITLE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($home=="") { $save = false; $msg = _HOMETEXT_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if ($author=="" OR !validate($author)) { $save = false; $msg = _AUTHOR_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
				if (!validEmail($email)) { $save = false; $msg = _MAILNOTVALID_; }
				
				if ($save) {
					if (memRunHooks('ApproveNews',array($name,$argument,$home,$text,$author,$email,$language))) {
						$dblink->query("UPDATE memht_news SET argomento='$argument',nome='$name',testo_home='$home',testo='$text',autore='$author',email='$email',usecomments='$usecomments',language='$language',waiting='0',enabled='$enabled' WHERE id=$id");
						if ($tags!="") {
							$tags = explode(",",$tags);
							foreach ($tags as $tag) {
								$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$id','3')");
							}
						}
						
						memRunHooks('ApproveNewsEnd',array($name,$argument,$home,$text,$author,$email,$language));
					}
			
					echo "<div align='center' class='box'><b>"._APPROVED_."</b></div>";
					echo "<meta http-equiv='refresh' content='3;URL=admin.php?page=submission'>";
				} else {
					echo "<div align='center' id='errorText'><b>$msg</b></div>";
				}
		break;
		case "delete":
			if ($ok) {
				$dblink->query("DELETE FROM memht_news WHERE id=$id");
				echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=submission'>";
			} else {
				echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=submission&op=news&subop=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=submission' title='"._NO_."'>"._NO_."</a></div>";
			}
		break;
	}
}

function submain() {
	global $dblink;
	
	//Waiting articles
	if ($dblink->get_num("SELECT id FROM memht_articoli WHERE waiting=1 LIMIT 1")>0) {
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'>&nbsp;<a href='admin.php?page=submission&op=articles' title='"._WAITINGARTICLES_."'><b>"._WAITINGARTICLES_."</b></a></div>";
	}
	
	//Waiting blog posts
	if ($dblink->get_num("SELECT id FROM memht_blog_posts WHERE waiting=1 LIMIT 1")>0) {
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'>&nbsp;<a href='admin.php?page=submission&op=blog' title='"._WAITINGBLOGPOSTS_."'><b>"._WAITINGBLOGPOSTS_."</b></a></div>";
	}
	
	//Waiting downloads
	if ($dblink->get_num("SELECT id FROM memht_download WHERE waiting=1 LIMIT 1")>0) {
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'>&nbsp;<a href='admin.php?page=submission&op=download' title='"._WAITINGDOWNLOADS_."'><b>"._WAITINGDOWNLOADS_."</b></a></div>";
	}
	
	//Waiting gallery
	if ($dblink->get_num("SELECT id FROM memht_gallery_images WHERE waiting=1 LIMIT 1")>0) {
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'>&nbsp;<a href='admin.php?page=submission&op=gallery' title='"._WAITINGIMAGES_."'><b>"._WAITINGIMAGES_."</b></a></div>";
	}
	
	//Waiting guides
	if ($dblink->get_num("SELECT id FROM memht_guide WHERE waiting=1 LIMIT 1")>0) {
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'>&nbsp;<a href='admin.php?page=submission&op=guide' title='"._WAITINGGUIDES_."'><b>"._WAITINGGUIDES_."</b></a></div>";
	}
	
	//Waiting links
	if ($dblink->get_num("SELECT id FROM memht_mylinks WHERE flag=0 LIMIT 1")>0) {
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'>&nbsp;<a href='admin.php?page=submission&op=mylinks' title='"._WAITINGLINKS_."'><b>"._WAITINGLINKS_."</b></a></div>";
	}
	
	//Waiting news
	if ($dblink->get_num("SELECT id FROM memht_news WHERE waiting=1 LIMIT 1")>0) {
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet'>&nbsp;<a href='admin.php?page=submission&op=news' title='"._WAITINGNEWS_."'><b>"._WAITINGNEWS_."</b></a></div>";
	}
}

$level = 100;
$spcs = "";
function godeep($id,$level,$spcs,$now=0) {
	global $dblink;
		
	$parent = intval($id);
	if ($result = $dblink->get_list("SELECT id,name FROM memht_mylinks_cat WHERE parent=$parent ORDER BY name") AND $level>0) {
		$spcs .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$name = outCode($row['name']);
			
			$selected = ($now==$id) ? " selected" : "" ;
			echo "<option value='$id'{$selected}>{$spcs}$name</div>";
			$level--;
			godeep($id,$level,$spcs,$now);
		}
	}
}

function godeepFiles($id,$level,$spcs,$now=0) {
	global $dblink;
		
	$parent = intval($id);
	if ($result = $dblink->get_list("SELECT id,nome FROM memht_download_categorie WHERE parent=$parent ORDER BY nome") AND $level>0) {
		$spcs .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$nome = outCode($row['nome']);
			
			$selected = ($now==$id) ? " selected" : "" ;
			echo "<option value='$id'{$selected}>{$spcs}$nome</div>";
			$level--;
			godeepFiles($id,$level,$spcs,$now);
		}
	}
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);

	openTable();
		switch($op) {			
			default: submain(); break;
			case "articles": articles($id,$subop,$ok); break;
			case "blog": blog($id,$subop,$ok); break;
			case "download": download($id,$subop,$ok); break;
			case "gallery": gallery($id,$subop,$ok); break;
			case "guide": guide($id,$subop,$ok); break;
			case "mylinks": mylinks($id,$subop,$ok); break;
			case "news": news($id,$subop,$ok); break;
		}
	closeTable();
require_once("admin/inc/inc_footer.php");

?>