<?php
if (!defined('ADMIN_FILE')) die("Illegal File Access");
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"._magazin_categories." WHERE galloc='$galloc'"));
                    $db->sql_query("DELETE FROM $prefix"._magazin_categories." WHERE gallid=$gallid");
                    $db->sql_query("DELETE FROM $prefix"._magazin_pictures." WHERE gid=$gallid");
                    rdel($rf);
				} else {
					unlink($rf);
				}
            }
        }
		closedir($d);
		return rmdir($path);
	}
}

function chkul($gallid) {
	global $prefix, $temppath, $db;
	$sql = "SELECT pid, img FROM $prefix"._magazin_pictures_newpicture." WHERE gid=$gallid";
	while($row = $db->sql_fetchrow($db->sql_query($sql))) {
		$sql = "DELETE FROM $prefix"._magazin_pictures_newpicture." WHERE pid=$row[pid]";
		if (file_exists($temppath."/".$row[img])) unlink($temppath."/".$row[img]);
	}
}


function deletecategory($gallid) {
	global $prefix, $gallerypath, $db;
	list($galloc, $gallimg) = $db->sql_fetchrow($db->sql_query("SELECT galloc, gallimg FROM $prefix"._magazin_categories." WHERE gallid=$gallid"));
	$basedir = $gallerypath;
	if ($galloc != "") {
		rdel($basedir."/".$galloc);
		chkul($gallid);
	}
	$db->sql_query("DELETE FROM $prefix"._magazin_categories." WHERE gallid=$gallid");
	$db->sql_query("DELETE FROM $prefix"._magazin_pictures." WHERE gid=$gallid");
	$result = $db->sql_query("SELECT pid FROM $prefix"._magazin_pictures." WHERE gid = $gallid");
	while(list($pid) = $db->sql_fetchrow($result)) {
		$db->sql_query("DELETE FROM magazin_comments WHERE pid=$pid");
	}
}
?>