<?php
if (!defined('MODULE_FILE')) {
	Header("Location: ../../../index.php");
	exit;
}

$pagetitle = "$defis "._ALBUM_NAME." $defis "._GALUPLOADMEDIA."";

function listcategories($dir){
	global $gallerypath, $prefix, $db;

	$dossier = opendir($dir);
	$category = "";
	while ($file = readdir($dossier)) {
		if ($file != "." && $file != ".." && $file!="thumb"){
			if (is_dir($dir."/".$file)) {
				$galloc  = substr($dir."/".$file, strlen("$gallerypath/"));
				$result = $db->sql_query("SELECT gallid FROM ".$prefix."_album_categories WHERE galloc='$galloc'");
				if ($result) {
					$gid = intval($gid);
					list($gid) = $db->sql_fetchrow($result);
					$category .= "$gid ";
				}
				$category .= listcategories("$dir/$file");
			}
		}
	}
	return $category;
}

function upload_file() {
	global $name, $cookie, $galleryvar, $user, $gallerypath, $prefix, $db, $anonymous;
	galleryHeader();
	OpenTable();
	$size = $maxSize/1000;
	$nav = navigationGall();
	echo "<center>$nav<br><font class=\"option\">"._GALUPLOADMEDIA."</font><br>";
	if ($galleryvar['limitSize']) {
		echo ""._GALMAXSIZEPOST." ".files_size($galleryvar['maxSize'])."";
	}
	echo "</center>";

	if (is_user($user) || !$galleryvar['anonpostpics']) {
		echo "<form enctype=\"multipart/form-data\" action=\"index.php?name=Album&file=index\" method=\"post\"><input type=\"hidden\" name=\"do\" value=\"upload\"><table border=\"0\" align=\"center\">";
		echo "<tr><td>"._GALSUBMITTER.":</td><td>";
//		if (is_user($user)) {
//			cookiedecode($user);
//			echo "<input type=\"hidden\" name=\"Submitter\" value=\"".$cookie[1]."\">\n";
//			echo "".$cookie[1]."</td></tr>";
//		}


		if (is_user($user)) {
#			getusrinfo($user);
			$author = $user[1];
#			$authormail = $userinfo['user_email'];
		} else {
			$author = $anonymous;
		}

echo "<input type=\"hidden\" name=\"Submitter\" value=\"$author\">\n";
echo "$author</td></tr>";




		echo "<tr><td>"._GALSELECTCATEGORY.":</td><td><select name=\"Category\" style=\"width=300px\">";
		$categories = listcategories("$gallerypath");
		
		if (isset($categories) && $categories!="") {
			$categories = explode(" ", trim($categories));
			while(list($key, $val) = each($categories)) {
				if (is_user($user)) {
					$result = $db->sql_query("SELECT gallid, gallname, parent FROM $prefix"._album_categories." where visible>0 and gallid=".$val);
				} else {
					$result = $db->sql_query("SELECT gallid, gallname, parent FROM $prefix"._album_categories." where visible>1 and gallid=".$val);
				}
				if ($db->sql_numrows($result)>0) {
					list($gallid, $gallname, $parent) = $db->sql_fetchrow($result);
					$nbtabs = indent($gallid);
					$tab = "";
					for($k=0; $k<$nbtabs; $k++) $tab .= "&nbsp;&nbsp;";
					echo "<option value=\"$gallid\">$tab$gallname</option>\n";
				}
			}
		}
		echo "</select></tr><tr><td>"._GALMEDIANAME.":</td><td><input type=\"text\" name=\"MediaName\" size=\"50\" value=\"\" style=\"width=300px\"></td></tr>";
		if ($galleryvar['limitSize']) echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$maxSize\">";
		echo "<tr><td>"._GALFILENAME.":</td><td><input type=\"file\" name=\"userfile\" size=\"50\" style=\"width=300px\"></td></tr>";
		echo "<tr><td>"._GALDESCRIPTION.":</td><td><textarea  name=\"Description\" rows=\"10\" cols=\"50\" style=\"width=300px\"></textarea></td></tr><tr><td colspan=\"2\" align=\"center\"><input type=\"hidden\" name=\"add\" value=\"Upload\"><input type=\"submit\" value=\""._GALPOSTMEDIA."\"></td></tr></form></table>";
	} else {
		echo "<hr><center>"._GALREGISTER2POSTPICS."</center><hr>";
	}
	CloseTable();
	include("footer.php");
}

function Add($gid, $file, $submitter, $medianame, $description, $userfile, $userfile_name, $userfile_size) {
	global $user, $galleryvar, $temppath, $prefix, $db;
	
	$gid = intval($gid);
    $sql = "SELECT img FROM $prefix"._album_pictures." where gid=$gid and img='$file'";
    $result = $db->sql_query($sql);
    $numrows = $db->sql_numrows($result);
    $navgall = navigationGall();

	if ($file==$galleryvar['GalleryPictureName'] || $numrows>0) {
		galleryHeader();
		echo "<br>";
		OpenTable();
		print '<table border="0" width="100%" align="center" cellpadding="0"><tr><td>'.$navgall;
		echo "<br><center>"._GALPICALREADYEXT."<br><br>"._GOBACK."";
		print '</td></tr></table>';
		CloseTable();
		include("footer.php");
	} else {
		if(is_user($user)) {
#			$user2 = base64_decode($user);
#			$cookie = explode(":", $user2);
#			cookiedecode($user);
			$submitter = $user[1];
		}
		if (!isset($medianame) || $medianame=="") {
			galleryHeader();
			echo "<br>";
			OpenTable();
			print '<table border="0" width="100%" align="center" cellpadding="0"><tr><td>'.$navgall;
			echo "<br><center>"._GALPICNONAME."<br><br>"._GOBACK."";
			print '</td></tr></table>';
			CloseTable();
			include("footer.php");
		} else {
			if (!isset($description) || $description=="") {
                galleryHeader();
                echo "<br>";
                OpenTable();
                print '<table border="0" width="100%" align="center" cellpadding="0"><tr><td>'.$navgall;
                echo "<center>"._GALPICNODESC."<br><br>"._GOBACK."";
                print '</td></tr></table>';
				CloseTable();
				include("footer.php");
			} else {
				if (!isset($submitter) || $submitter=="") {
					galleryHeader();
					echo "<br>";
					OpenTable();
					print '<table border="0" width="100%" align="center" cellpadding="0"><tr><td>'.$navgall;
					echo "<br><center>"._GALPICNOSUBMITTER."<br><br>"._GOBACK."";
					print '</td></tr></table>';
					CloseTable();
					include("footer.php");
				} else {
					$medianame = FixQuotes($medianame);
					$description = FixQuotes($description);
					$wdir = "/";
					$upload_return = UploadFile($temppath, $userfile, $userfile_name, $userfile_size);
					if ($upload_return=="OK") {
						$ext = substr($file, (strrpos($file,'.') +  1));
						list($type) = $db->sql_fetchrow($db->sql_query("SELECT filetype FROM $prefix"._album_media_types." where extension='$ext'"));
						$wdir = "/";

						switch($type) {
							case 1 :
							$size = @getimagesize($temppath.$wdir.$file);
							break;
							case 3 :
							$size[0] = 320;
							$size[1] = 240;
							break;
							default:
							$size[0] = 0;
							$size[1] = 0;
							break;
						}
						$file = traite_nom_fichier($file);
						$gid = intval($gid);
						if ($size!=NULL) {
							$sql = "insert into $prefix"._album_pictures_newpicture." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) values(NULL, $gid, '$file', 0, '$submitter', now(), '$medianame', '$description', 0, 0, '$ext', ".$size[0].", ".$size[1].")";
						} else {
							$sql = "insert into $prefix"._album_pictures_newpicture." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) values(NULL, $gid, '$file', 0, '$submitter', now(), '$medianame', '$description', 0, 0, '$ext', 0, 0)";
						}
						$db->sql_query($sql);
					} else {
						return $upload_return;
					}
					return "OK";
				}
			}
		}
	}
}
?>