<?php

if ( ! @include_once( './mainfile.php' ) ) { exit( '!! INCLUDE ERROR. ACCESS DENIED !!' ); }
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: quickaddmedia.php,v 1.5.2.2 2004/04/18 12:46:51 ajvwaay Exp $



//include "$modulepath/mediatype.php";
function is_in($f, $gid, $logo) {
    global $mpnTables;
    if($f == $logo){return true;}
    $f = addslashes($f);
    $sql = "SELECT COUNT(pid) AS total FROM ".$mpnTables['gallery_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) {
    global
        $GalleryPictureName,
        $mediatype,
        $gallerypath,
        $mpnTables
    ;
    $c = 0;

    $loc = substr($path, strlen("$gallerypath/"));
    //echo "*$loc*<br>";
    $result = mysql_query("SELECT gallid, galloc, gallimg FROM ".$mpnTables['gallery_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));
                list($type) = mysql_fetch_row(mysql_query("select filetype from ".$mpnTables['gallery_media_types']." where extension='$ext'"));
                if($type && !is_in($f, $row['gallid'], $row['gallimg'])) {
                    if($type == 1) {
                        $size = getimagesize($rf);
                    }
                    else {
                        switch($type) {
                            case 3:
                                $size[0] = 320;
                                $size[1] = 240;
                                break;
                            default:
                                $size[0] = 0;
                                $size[1] = 0;
                                break;
                        }
                    }
                    $f = addslashes($f);
                    $sql = "INSERT INTO ".$mpnTables['gallery_pictures']." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) VALUES (NULL, " . $row['gallid'] . ", '$f', 0, 'Webmaster', NOW(), '$f', '', 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++;
                }
            }
            elseif ($isdir) {
                $isindatabase = 0;
                if ($par==-1)
                    $galloc = $f;
                else
                    $galloc = "$loc/$f";
                $sql = "SELECT gallid, gallimg FROM ".$mpnTables['gallery_categories']." where galloc='$galloc'";
                //echo $sql.'<br>';
                $result = mysql_query($sql);
                if (mysql_num_rows($result)>0)
                    $isindatabase=1;
                if (!$isindatabase) {
                    $sql = "INSERT INTO ".$mpnTables['gallery_categories']." (gallid, gallname, gallimg, galloc, description, parent, visible, template, numcol, total, lastadd) VALUES (NULL, '$f', '$GalleryPictureName', '$galloc', 'Write a description Here', $par, 0, 2, 3, 0, 0)";
                    //echo $sql.'<br>';
                    $c += 1;
                    mysql_query($sql);
                    print '<font size="1"><strong>'.sprintf(_GALNEWMEDIAADDED, $f, $loc).'</strong></font><br>';
                    $row2 = mysql_fetch_array(mysql_query("SELECT gallid FROM ".$mpnTables['gallery_categories']." WHERE gallid=LAST_INSERT_ID()"));
                }
                else
                    $row2 = mysql_fetch_array(mysql_query("SELECT gallid FROM ".$mpnTables['gallery_categories']." WHERE galloc='$galloc'"));
                if ($recursive==1)
                    $c += radd($row2['gallid'], $rf, $recursive);
            }

        } // while
        closedir($d);
        return $c;
    }
}
function quickaddpics($gallid, $recursive) {
    global
        $adminurl,
        $module_name,
        $imagepath,
        $gallerypath,
        $adminpath,
        $languagepath,
        $baseurl,
        $bgcolor1,
        $bgcolor2,
        $bgcolor3,
        $bgcolor4,
        $textcolor1,
        $version,
        $hlpfile,
        $mpnTables
    ;
#   include 'header.php';
#   GraphicAdmin($hlpfile);
    OpenTable();
    print '<center><font size="4"><b>'._GALADMIN.'</b></font></center>';
    CloseTable();
    print '<br>';
    OpenTable();
    print '<table border="0" width="100%" cellspacing="0" cellpadding="0"><tr>'
        .'<td width="100%" align="left">'
        ."<font face=\"Verdana, Arial\" size=\"2\"><a href=\"$adminurl\">"._GALCONFIG."</a></font> &gt; ";

    if (isset($gallid) && $gallid!="") {
    print    '<font face="Verdana, Arial" size="2"><a href="'.$adminurl.'&amp;op=quickaddpics">'._GALBATCHBUILD.'</a></font> &gt; '
        .'<font face="Verdana, Arial" size="2"><b>'._GALBATCHBUILDRESULT.'</b></font>';
    }
    else
        print '<font face="Verdana, Arial" size="2"><b>'._GALBATCHBUILD.'</b></font>';
    print
        '</td>'
        .'</tr>'
        .'<tr>'
        .'<td align="center" colspan="2"><br>'
    ;



    if (isset($gallid) && $gallid!="") {

        if ($gallid==-1) {
            $nb = radd($gallid, $gallerypath, $recursive);
            $row['gallname'] = "/";
        }
        else {

            $row = mysql_fetch_array(mysql_query("select galloc, gallname from ".$mpnTables['gallery_categories']." where gallid=".$gallid));
            $nb = radd($gallid, $gallerypath."/".$row['galloc'], $recursive);
        }
        if($nb>0) {
            print "<hr><strong>".sprintf(_GALNBMEDIAADDED, $nb)." </strong>";
        }
        else {
            echo '<font size="-1">'.sprintf(_GALNONEWMEDIAFOUND, $row['gallname']).'</font>';
        }
    }
    else {
        print"
        <form action=\"$adminurl\" method=\"post\">
        <input type=\"hidden\" name=\"op\" value=\"quickaddpics\">
        "._GALQUICKSELECT."&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 ".$mpnTables['gallery_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>"._GALQUICKRECURSIVE."&nbsp;&nbsp;&nbsp;"
        ."<input type=\"radio\" name=\"recursive\" value=\"1\" checked>"._YES.'&nbsp;'
        ."<input type=\"radio\" name=\"recursive\" value=\"0\">"._NO
        ."<br><br>
        <input class=\"button\" type=\"submit\" name=\""._SEARCH."\" value=\""._SEARCH."\">
        </form><br>";
    }
    print '</td>'
        .'</tr>'
        .'</table>'
    ;
    CloseTable();

    print '<br>';
    // Start Of Footer
    OpenTable();
    print '
    <table><tr>
    <td>'
      ."<font face=\"Verdana,Arial\" color=\"$textcolor1\">mpn eGallery Version - $version<br>"._GALVISITUPDATE.'</font>
    </td>
    </tr></table>';
    CloseTable();
#   include 'footer.php';
}
?>
