<?php
if (!defined('ADMIN_FILE')) die("Illegal File Access");
function movecategory($gid) {
	global $prefix, $gallerypath, $db, $admin_file;
	head();
	navigationAdmin("<b><a href=\"".$admin_file.".php?op=album\">"._GALCONFIG."</a></b> &gt;&gt; <b><a href=\"".$admin_file.".php?op=album&do=editallcategories\">"._GALEDITALLCATEGORIES."</a></b> &gt;&gt; <b>"._GALCATMOVE.'</b>');
	OpenTable();
	echo "<center><font class=\"option\"><b>"._GALCATMOVE."</b></font><br>";
	echo "<form action=\"".$admin_file.".php?op=album\" method=\"post\"><input type=\"hidden\" name=\"do\" value=\"editcategory\"><input type=\"hidden\" name=\"type\" value=\"move\">";
	list($cat) = $db->sql_fetchrow($db->sql_query("SELECT gallname FROM $prefix"._album_categories." WHERE gallid=$gid"));
	echo ""._GALCATAEGOYNAME." \"$cat\"<br>"._GALMOVEWARNING."<br><br>"._GALMOVESELECT." <input type=\"hidden\" name=\"oldlocation\" size=\"25\" value=\"$gid\"><select size=\"1\" name=\"newparent\">";
	$categories = listcategories("$gallerypath");
	$categories = explode(" ", trim($categories));
	echo "<option selected value=\"-1\">/</option> ";
	while(list($key, $val) = each($categories)) {
		$result = $db->sql_query("SELECT gallid, gallname, parent FROM $prefix"._album_categories." WHERE gallid=".$val);
		list($gallid, $gallname, $parent) = $db->sql_fetchrow($result);
		$nbtabs = indent($gallid);
		$tab = "";
		for ($i=0; $i<$nbtabs; $i++) $tab .= "&nbsp;&nbsp;&nbsp;";
		if ($gallid!=$gid) echo "<option value=\"$gallid\">$tab$gallname</option> ";
	}
	echo "</select><br><br><input type=\"submit\" name=\"confirmed\" value=\""._GALMOVE."\"></form></center>";
	CloseTable();
	foot();
}


function copydir($source , $destination) {
	global $prefix, $gallerypath, $db;
	$dossier=opendir($source);
	if (!file_exists($destination)) mkdir($destination, fileperms($source));
	$total = 0;
	while ($file = readdir($dossier)) {
		if ($file != "." && $file != "..") {
			if (is_dir($source."/".$file)) {
				$total += copydir("$source/$file", "$destination/$file");
				$newgalloc = substr($destination, strlen("$gallerypath/"));
				$oldgalloc = substr($source, strlen("$gallerypath/"));
				$db->sql_query("UPDATE $prefix"._album_categories." SET galloc='$newgalloc/$file' WHERE galloc='$oldgalloc/$file'");
			} else {
				copy("$source/$file", "$destination/$file");
				$total++;
			}
		}
	}
	return $total;
}

function rmove($source, $destination) {
	if (copydir($source, $destination)) rdel($source);
}

function rdel($path) {
	global $prefix, $gallerypath, $db;
	if ($path[strlen($path)-1] != "/") $path .= "/";
	if (is_dir($path)) {
		$d = opendir($path);
		while ($f = readdir($d)) {
			if ($f != "." && $f != "..") {
				$rf = $path . $f;
				if (is_dir($rf)) {
					$galloc = substr($rf, strlen("$gallerypath/"));
                    list($gallid) = $db->sql_fetchrow($db->sql_query("SELECT gallid FROM $prefix"._album_categories." where galloc='$galloc'"));
                    $db->sql_query("DELETE FROM $prefix"._album_categories." WHERE gallid=$gallid");
                    $db->sql_query("DELETE FROM $prefix"._album_pictures." WHERE gid=$gallid");
                    $db->sql_query("DELETE FROM album_template WHERE gid=$gallid");
                    rdel($rf);
				} else {
					unlink($rf);
				}
			}
		}
		closedir($d);
		return rmdir($path);
	}
}
?>