<?php
if (!defined('ADMIN_FILE')) {
	die("Illegal File Access");
}

function is_in($f, $gid, $logo) {
	global $prefix, $db;
	if($f == $logo){return true;}
	$f = addslashes($f);
	$sql = "SELECT COUNT(pid) AS total FROM $prefix"._album_pictures." WHERE gid=$gid AND img='$f'";
	$row = $db->sql_fetchrow($db->sql_query($sql));
	if($row[total]) {
		return true;
	}
}

function radd($par, $path, $recursive, $submitter, $desc_pic, $desc_cat) {
	global $prefix, $galleryvar, $gallerypath, $db;
	$c = 0;
	$loc = substr($path, strlen("$gallerypath/"));
	$result = $db->sql_query("SELECT gallid, galloc, gallimg FROM $prefix"._album_categories." WHERE galloc='$loc'");
	if($db->sql_numrows($result)>0) $row = $db->sql_fetchrow($result);
	if(is_dir($path)) {
		$d = opendir($path);
		while($f = readdir($d)) {
			if($f == '.' || $f == '..' || $f == 'thumb'){continue;}
			$rf = "$path/$f";
			$isdir = is_dir($rf);
			if(!$isdir && $par !=-1) {
				$ext = substr($f, (strrpos($f,'.') +  1));
				$sql = "SELECT filetype FROM $prefix"._album_media_types." WHERE extension='$ext'";
				list($type) = $db->sql_fetchrow($db->sql_query($sql));
				if($type && !is_in($f, $row[gallid], $row[gallimg])) {
					switch($type) {
						case 1:
						$size[0] = 0;
						$size[1] = 0;
						if ($ext!="bmp" && $ext!="BMP") $size = getimagesize($rf);
						break;
						case 3:
						$size[0] = 320;
						$size[1] = 240;
						break;
						default:
						$size[0] = 0;
						$size[1] = 0;
						break;
					}
					$f = addslashes($f);
					$sql = "INSERT INTO $prefix"._album_pictures." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) VALUES (NULL, $row[gallid], '$f', 0, '$submitter', NOW(), '$f', '$desc_pic', 0, 0, '$ext', $size[0], $size[1])";
					$db->sql_query($sql);
					print "&nbsp;&nbsp;".sprintf(_GALFILEADDED, $f, $loc)."<br>";
					$c++;
				}
			} elseif ($isdir) {
				$isindatabase = 0;
				if ($par==-1) $galloc = $f; else $galloc = "$loc/$f";
				$sql = "SELECT gallid, gallimg FROM $prefix"._album_categories." where galloc='$galloc'";
				$result = $db->sql_query($sql);
				if ($db->sql_numrows($result)>0) $isindatabase=1;
				if (!$isindatabase) {
					$sql = "INSERT INTO $prefix"._album_categories." (gallid, gallname, gallimg, galloc, description, parent, visible, template, numcol, total, lastadd) VALUES (NULL, '$f', '".$galleryvar['GalleryPictureName']."', '$galloc', '$desc_cat', $par, 0, 2, 3, 0, 0)";
					$c += 1;
					$db->sql_query($sql);
					print '<b>'.sprintf(_GALNEWMEDIAADDED, $f, $loc).'</b><br>';
					$row2 = $db->sql_fetchrow($db->sql_query("SELECT gallid FROM $prefix"._album_categories." WHERE gallid=LAST_INSERT_ID()"));
				} else {
					$row2 = $db->sql_fetchrow($db->sql_query("SELECT gallid FROM $prefix"._album_categories." WHERE galloc='$galloc'"));
				}
				if ($recursive==1) $c += radd($row2[gallid], $rf, $recursive, $submitter, $desc_pic, $desc_cat);
			}
		}
		closedir($d);
		return $c;
	}
}

function quickaddpics($gallid, $recursive, $submitter, $desc_pic, $desc_cat) {
	global $prefix, $admin_file, $gallerypath, $db;
	if (isset($gallid) && $gallid!="") {
		$nav = '<b><a href="'.$admin_file.'.php?op=album&do=quickaddpics">'._GALBATCHBUILD.'</a></b> &gt;&gt; <b>'._GALBATCHBUILDRESULT.'</b>';
	} else {
		$nav= '<b>'._GALBATCHBUILD.'</b>';
	}
	include("header.php");
	navigationAdmin("<b><a href=\"".$admin_file.".php?op=album&do=showoptions\">"._GALCONFIG."</a></b> &gt;&gt; ".$nav."");
	OpenTable();
	echo "<center><font class=\"option\"><b>"._GALBATCHBUILD."</b></font></center><br>";
	if (isset($gallid) && $gallid!="") {
		if ($submitter==_GALDEFAULTSUBMITTERDESC) $submitter="";
		if ($desc_pic==_GALDEFAULTDESCPICDESC) $desc_pic="";
		if ($desc_cat==_GALDEFAULTDESCCATDESC) $desc_cat="";
		if ($gallid==-1) {
			$nb = radd($gallid, $gallerypath, $recursive, $submitter, $desc_pic, $desc_cat);
			$row[gallname] = "/";
		} else {
			$row = $db->sql_fetchrow($db->sql_query("select galloc, gallname from $prefix"._album_categories." where gallid=".$gallid));
			$nb = radd($gallid, $gallerypath."/".$row[galloc], $recursive, $submitter, $desc_pic, $desc_cat);
		}
		if($nb>0) {
			echo "<hr><center><font class=\"option\">".sprintf(_GALNBMEDIAADDED, $nb)."</font></center>";
		} else {
			echo "<center><font class=\"option\">".sprintf(_GALNONEWMEDIAFOUND, $row[gallname])."</font></center>";
		}
	} else {
		echo "<table border=\"0\" align=\"center\"><tr><td>";
		echo "<form action=\"".$admin_file.".php?op=album\" method=\"post\"><input type=\"hidden\" name=\"do\" value=\"quickaddpics\">"._GALQUICKSELECT."</td><td><select name=\"gallid\" style=\"width=300px\">";
		$categories = listcategories("$gallerypath");
		echo "<option selected value=\"-1\">/</option>";
		if (isset($categories) && $categories!="") {
			$categories = explode(" ", trim($categories));
			while(list($key, $val) = each($categories)) {
				$result = $db->sql_query("SELECT gallid, gallname FROM $prefix"._album_categories." WHERE gallid=".$val);
				list($gallid, $gallname) = $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></td></tr><tr><td>"._GALQUICKRECURSIVE."</td>"
		."<td><input type=\"radio\" name=\"recursive\" value=\"1\" checked>"._YES.'&nbsp;'
		."<input type=\"radio\" name=\"recursive\" value=\"0\">"._NO."</td></tr>"
		."<tr><td>"._GALDEFAULTSUBMITTER."</td><td><input type=\"text\" size=\"50\" style=\"width=300px\" name=\"submitter\" value=\"\"></td></tr>"
		."<tr><td>"._GALDEFAULTDESCPIC."</td><td><input type=\"text\" size=\"50\" style=\"width=300px\" name=\"desc_pic\" value=\"\"></td></tr>"
		."<tr><td>"._GALDEFAULTDESCCAT."</td><td><input type=\"text\" size=\"50\" style=\"width=300px\" name=\"desc_cat\" value=\"\"></td></tr></table><br>"
		."<center><input type=\"submit\" name=\""._SEARCH."\" value=\""._SEARCH."\"></center></form>";
	}
	CloseTable();
	include("footer.php");;
}
?>