<?php

/************************************************************************/
/* 4nAlbum Version 0.9                                                  */
/* ==================================================================== */
/* 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.       */
/************************************************************************/

function rdel($path)
{
    global $prefix, $gallerypath;

    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) = mysql_fetch_row(mysql_query("SELECT gallid FROM $prefix"._4nalbum_categories." where galloc='$galloc'"));
                    mysql_query("DELETE FROM $prefix"._4nalbum_categories." WHERE gallid=$gallid");
                    mysql_query("DELETE FROM $prefix"._4nalbum_pictures." WHERE gid=$gallid");
                    rdel($rf);
                }
                else
                    unlink($rf);
            }
        }

        closedir($d);


        return rmdir($path);
    }
}

function chkul($gallid)
{
        global $prefix, $temppath;
        $sql = "SELECT pid, img FROM $prefix"._4nalbum_pictures_newpicture." WHERE gid=$gallid";
        while($row = mysql_fetch_array(mysql_query($sql)))
        {
                $sql  = "DELETE FROM $prefix"._4nalbum_pictures_newpicture." WHERE pid=$row[pid]";
                if (file_exists($temppath."/".$row[img]))
                        unlink($temppath."/".$row[img]);
        }

}


function deletecategory($gallid) {
        global $prefix, $gallerypath;

        list($galloc, $gallimg) = mysql_fetch_row(mysql_query("SELECT galloc, gallimg FROM $prefix"._4nalbum_categories." where gallid=$gallid"));
        $basedir = $gallerypath;
        if ($galloc != "")
        {
                rdel($basedir."/".$galloc);
                chkul($gallid);
        }
        else
        {
                include ("header.php");
                GraphicAdmin($hlpfile);
                OpenTable();
                echo "<center><font size=\"4\"><b>"._GALADMIN."</b></font></center>";
                CloseTable();
                echo "<br>";
                OpenTable();
                echo '<br>';
                echo '<center>Galloc is wrong.<br><br>'._GOBACK.'</center>';
                CloseTable();
                adminFooter();
        }

        mysql_query("DELETE FROM $prefix"._4nalbum_categories." WHERE gallid=$gallid");
        mysql_query("DELETE FROM $prefix"._4nalbum_pictures." WHERE gid=$gallid");
        $result = mysql_query("SELECT pid FROM $prefix"._4nalbum_pictures." WHERE gid = $gallid");
        while(list($pid) = mysql_fetch_row($result)) {
                mysql_query("DELETE FROM 4nalbum_comments WHERE pid=$pid");
        }
}



?>