<?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 is_in($f, $gid, $logo) {
        global $prefix;
        if($f == $logo){return true;}
        $f = addslashes($f);
        $sql = "SELECT COUNT(pid) AS total FROM $prefix"._4nalbum_pictures." WHERE gid=$gid AND img='$f'";
        //echo $sql.'<br>';
        $row = mysql_fetch_array(mysql_query($sql));
        if($row[total]) {
                return true;
        }
}

function radd($par, $path, $recursive, $submitter, $desc_pic, $desc_cat) {
        global $prefix, $font, $galleryvar, $gallerypath;
        $c = 0;

        $loc = substr($path, strlen("$gallerypath/"));
        //echo "*$loc*<br>";
        $result = mysql_query("SELECT gallid, galloc, gallimg FROM $prefix"._4nalbum_categories." where galloc='$loc'");
        if(mysql_num_rows($result)>0)
                $row = mysql_fetch_array($result);
        //echo $row[gallid].'<br>';
        if(is_dir($path)) {
                $d = opendir($path);
                while($f = readdir($d)) {
                        //echo $f."<br>";
                        if($f == '.' || $f == '..' || $f == 'thumb'){continue;}
                        $rf = "$path/$f";
                        $isdir = is_dir($rf);
                        if(!$isdir && $par !=-1) { // check if this is a sub-directory
                                $ext = substr($f, (strrpos($f,'.') +  1));
                                $sql = "select filetype from $prefix"._4nalbum_media_types." where extension='$ext'";
                                list($type) = mysql_fetch_row(mysql_query($sql));
                                //echo $type;
                                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"._4nalbum_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])";
                                        //echo $sql.'<br>';
                                        mysql_query($sql);
                                        print "<font size=\"1\">&nbsp;&nbsp;".sprintf(_GALFILEADDED, $f, $loc)."</font><br>";
                                        $c++;
                                }
                                else {
                                }
                        }
                        elseif ($isdir) {
                                $isindatabase = 0;
                                if ($par==-1)
                                        $galloc = $f;
                                else
                                        $galloc = "$loc/$f";
                                $sql = "SELECT gallid, gallimg FROM $prefix"._4nalbum_categories." where galloc='$galloc'";
                                //echo $sql.'<br>';
                                $result = mysql_query($sql);
                                if (mysql_num_rows($result)>0)
                                        $isindatabase=1;
                                if (!$isindatabase) {
                                        $sql = "INSERT INTO $prefix"._4nalbum_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)";
                                        //echo $sql.'<br>';
                                        $c += 1;
                                        mysql_query($sql);
                                        print '<font class="'.$font['normal'].'"><strong>'.sprintf(_GALNEWMEDIAADDED, $f, $loc).'</strong></font><br>';
                                        $row2 = mysql_fetch_array(mysql_query("SELECT gallid FROM $prefix"._4nalbum_categories." WHERE gallid=LAST_INSERT_ID()"));
                                }
                                else
                                        $row2 = mysql_fetch_array(mysql_query("SELECT gallid FROM $prefix"._4nalbum_categories." WHERE galloc='$galloc'"));
                                if ($recursive==1)
                                        $c += radd($row2[gallid], $rf, $recursive, $submitter, $desc_pic, $desc_cat);
                        }

                } // while
                closedir($d);
                return $c;
        }
}

function quickaddpics($gallid, $recursive, $submitter, $desc_pic, $desc_cat) {
        global
                $prefix,
                $adminurl,
                $gallerypath,
                $baseurl
        ;
        galleryHeader();
        if (isset($gallid) && $gallid!="") {
        print    $nav = '<a class="'.$font['title'].'" href="'.$adminurl.'&amp;do=quickaddpics">'._GALBATCHBUILD.'</a><font class="'.$font['title'].'"> &gt; </font>'
                .'<font class="'.$font['title'].'">'._GALBATCHBUILDRESULT.'</font>';
        }
        else
                $nav= '<font class="'.$font['title'].'">'._GALBATCHBUILD.'</font>';

          navigationAdmin("<a class=\"".$font['title']."\" href=\"$adminurl\">"._GALCONFIG."</a><font class=\"".$font['title']."\"> &gt; </font>".$nav);

          echo "<br>";
          OpenTable();
        print '<p align="center">';

        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 = mysql_fetch_array(mysql_query("select galloc, gallname from $prefix"._4nalbum_categories." where gallid=".$gallid));
                        $nb = radd($gallid, $gallerypath."/".$row[galloc], $recursive, $submitter, $desc_pic, $desc_cat);
                }
                if($nb>0) {
                        print "<hr><font class=\"".$font['title']."\">".sprintf(_GALNBMEDIAADDED, $nb)." </ont>";
                }
                else {
                        echo '<font class="'.$font['normal'].'">'.sprintf(_GALNONEWMEDIAFOUND, $row[gallname]).'</font>';
                }
        }
        else {
                print "
                <form action=\"$adminurl\" method=\"post\">
                <input type=\"hidden\" name=\"do\" value=\"quickaddpics\">
                <font class=\"".$font['title']."\">"._GALQUICKSELECT."</font>&nbsp;&nbsp;&nbsp;
                <select name=\"gallid\">";
                $categories = listcategories("$gallerypath");
                echo "<option selected value=\"-1\">/</option> ";
                if (isset($categories) && $categories!="") {
                        $categories = explode(" ", trim($categories));
                        while(list($key, $val) = each($categories)) {
                                $result = mysql_query("select gallid, gallname from $prefix"._4nalbum_categories." where gallid=".$val);
                                list($gallid, $gallname) = mysql_fetch_row($result);
                                $nbtabs = indent($gallid);
                                $tab = "";
                                for($k=0; $k<$nbtabs; $k++)
                                        $tab .= "&nbsp;&nbsp;";
                                echo "<option value=\"$gallid\">$tab$gallname</option>\n";
                        }
                }
                echo "</select><br><br><font class=\"".$font['title']."\">"._GALQUICKRECURSIVE."</font>&nbsp;&nbsp;&nbsp;"
                ."<input type=\"radio\" name=\"recursive\" value=\"1\" checked>"._YES.'&nbsp;'
                ."<input type=\"radio\" name=\"recursive\" value=\"0\">"._NO
                ."<br><br>"
                .'<font class="">'._GALDEFAULTSUBMITTER."</font><br><input type=\"text\" size=\"90\" name=\"submitter\" value=\""._GALDEFAULTSUBMITTERDESC."\"><br><br>"
                .'<font class="">'._GALDEFAULTDESCPIC."</font><br><input type=\"text\" size=\"90\" name=\"desc_pic\" value=\""._GALDEFAULTDESCPICDESC."\"><br><br>"
                .'<font class="">'._GALDEFAULTDESCCAT."</font><br><input type=\"text\" size=\"90\" name=\"desc_cat\" value=\""._GALDEFAULTDESCCATDESC."\">"
                ."<br><br>"
                ."<input type=\"submit\" name=\""._SEARCH."\" value=\""._SEARCH."\">"
                ."</form>";
        }
        print '</p>';
        CloseTable();
        print '<br>';
        adminFooter();
}
?>