<?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: main.php,v 1.11.2.6 2004/04/18 12:46:51 ajvwaay Exp $


( (bool)FALSE === strpos( $_SERVER['REQUEST_URI'], __FILE__ ) )
  OR exit( '!! ACCESS DENIED !!' );

checkAccess( 'modules', array( 'user' ) );

include ("admin/gallery/galladmin.$phpEx");

if (!isset($op))
{
    $op = null;
}


switch($op) {
    case "Configure":
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        Configure();
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "ConfigSave":
         ConfigSave($GLOBALS['HTTP_POST_VARS']);
#       ConfigSave($vars);
        break;
    case "editallcategories":
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include ("$adminpath/editallcategories.php");
        editallcategories();
    echo "<br>";
        include("footer.$phpEx");
        break;
    case "editcategory": //OK!
        if (!isset($parent))
        {
            $parent = null;
        }

        doeditcategory($type, $category, $parent);
        break;
    case "showeditcategory": //OK!
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include ("$adminpath/editcategory.php");
        if (!isset($category))
        {
            $category = null;
        }

        if (!isset($parent))
        {
            $parent = null;
        }

        editcategory($category, $parent);
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "movecategory": //OK!
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include ("$adminpath/movecategory.php");
        movecategory($category);
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "doedittemplate":
        if (!isset($delete))
        {
            $delete = null;
        }

        if (!isset($template))
        {
            $template = null;
        }

        doedittemplate($type, $template, $delete);
        break;
    case "edittemplate":
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include("$adminpath/edittemplate.php");
        if (!isset($template))
        {
            $template = null;
        }

        edittemplate($template);
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "editalltemplate":
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include("$adminpath/editalltemplates.php");
        editalltemplates();
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "maineditmedia": //OK!
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include ("$adminpath/maineditmedia.php");
        maineditmedia();
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "editmedia": //OK!
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include ("$adminpath/editmedia.php");
        editmedia($category);
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "doeditmedia": //OK!
#        doeditmedia ($type, $category);
        $limit=5;
        if (isset($type) && $type!="") {
          switch ($type) {
            case "edit": //OK!
                include ("$adminpath/editmedia.php");
                editmedia($category);
                break;

            case "new": //OK!
                include ("$adminpath/fileFunctions.php");
                //include ("$adminpath/quickaddmedia.php");
                if (isset($add) && $add=="Insert") {
                    list($galloc) = mysql_fetch_row(mysql_query("SELECT galloc FROM ".$mpnTables['gallery_categories']." WHERE gallid=$Category"));
                    //$basedir = $gallerypath;
                    $wdir = "/";
                    $upload_return = UploadFile($gallerypath.$wdir.$galloc, $userfile, $userfile_name, $userfile_size);
                    if ($upload_return==0) {
                        $userfile_name=  traite_nom_fichier($userfile_name);  ##new
                        $ext = substr($userfile_name, (strrpos($userfile_name,'.') +  1));
                        list($type) = mysql_fetch_row(mysql_query("select filetype from ".$mpnTables['gallery_media_types']." where extension='$ext'"));
                        if($type == 1) {
                            $size = getimagesize($gallerypath.$wdir.$galloc.$wdir.$userfile_name);
                        }
                        else {
                            switch($type) {
                                case 3 :
                                    $size[0] = 320;
                                    $size[1] = 240;
                                    break;
                                default:
                                    $size[0] = 0;
                                    $size[1] = 0;
                                    break;
                            }
                        }
                        $sql = "insert into ".$mpnTables['gallery_pictures']." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) values (NULL, $Category, '$userfile_name', 0, '$Submitter', now(), '$MediaName', '$Description', 0, 0, '$ext', ".$size[0].", ".$size[1].")";
                        //echo $sql;
                        mysql_query($sql);
                    }
                    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>'.$upload_return.'<br><br>'._GOBACK.'</center>';
                        CloseTable();
#                       include('footer.php');
                    }

                    $_SERVER['F_LIB'] -> redirectRequest( $adminurl );

                }
                else {
                    include("header.$phpEx");
                    admin_menu();
                    echo "<br>";
                    include ("$adminpath/createmedia.php");
                    createmedia();
                }
               echo "<br>";
               include("footer.$phpEx");

                rebuild_totals();
                rebuild_lastadd();
                break;

            case "existing": //OK!
                for ($i=0; $i<$limit; $i++) {
                    $task = "Task$i";

                    if (isset($$task)) {
                        switch ($$task) {
                            case "Save" : // OK!
                                $file = "FileName$i";
                                $fileid = "FileId$i";
                                $desc = "Description$i";
                                $name = "FileName$i";
                                //$oldname = "OldFileName$i";
                                $mname = "MediaName$i";
                                $date = "Date$i";
                                $cat = "Category$i";
                                $oldcat = "OldCategory$i";
                                $submitter = "Submitter$i";
                                $width = "Width$i";
                                $height = "Height$i";
                                //list($galloc) = mysql_fetch_row(mysql_query("SELECT galloc FROM ".$mpnTables['gallery_categories']." WHERE gallid=".$$cat));

                                $sql  = "UPDATE ".$mpnTables['gallery_pictures']." ";
                                $sql .= "SET gid=".$$cat.", ";
                                $sql .= "submitter='".$$submitter."', date='".$$date."', img='".$$name."', ";
                                $sql .= "name='".$$mname."', description = '".$$desc."', ";
                                $sql .= "width='".$$width."', height='".$$height."' ";
                                $sql .= "WHERE pid=".$$fileid;
                                //echo $sql;
                                $okcat =1;
                                $basedir = $basepath;
                                $wdir = "/";
                                if ($$cat != $$oldcat) {
                                    // We move the picture
                                    list($oldloc) = mysql_fetch_row(mysql_query("SELECT galloc from ".$mpnTables['gallery_categories']." WHERE gallid=".$$oldcat));
                                    list($newloc) =  mysql_fetch_row(mysql_query("SELECT galloc from ".$mpnTables['gallery_categories']." WHERE gallid=".$$cat));
                                    if (!is_writeable($gallerypath.$wdir.$oldloc.$wdir.$$name) || !is_writable($gallerypath.$wdir.$newloc.$wdir))
                                    {
                                        goBack( translate('Gallery Folders misconfigured for renaming files') );
                                    }

                                    if (rename($gallerypath.$wdir.$oldloc.$wdir.$$name,$gallerypath.$wdir.$newloc.$wdir.$$name)) {
                                        @chmod($gallerypath.$wdir.$newloc.$wdir.$$name, 0666);
                                        $okcat = 1;
                                        $thumb = getThumbnail($$name, $oldloc);
                                        if (!strstr($thumb,$imagepath)) {
                                            $thumb = substr (strrchr ($thumb, "/"), 1);
                                            //echo "move : ".$gallerypath.$wdir.$oldloc.$wdir."thumb/".$thumb." -> ".$gallerypath.$wdir.$newloc.$wdir."thumb/".$thumb."<br>";
                                            if (!is_writeable($gallerypath.$wdir.$oldloc.$wdir.'thumb/'.$thumb) || !is_writeable($gallerypath.$wdir.$newloc.$wdir.'thumb/'))
                                            {
                                                goBack( translate('Gallery Folders misconfigured for renaming files') );
                                            }

                                            if (rename($gallerypath.$wdir.$oldloc.$wdir."thumb/".$thumb,$gallerypath.$wdir.$newloc.$wdir."thumb/".$thumb)) {
                                                @chmod($gallerypath.$wdir.$newloc.$wdir.'thumb/'.$thumb, 0666);
                                                $okcat = 1;
                                            } else {
                                                $okcat = 0;
                                            }
                                        }
                                    } else {
                                        $okcat = 0;
                                    }
                                }

                                if ($okcat)
                                {
                                    mysql_query($sql);
                                }

                                rebuild_totals();
                                rebuild_lastadd();
                                break;

                            case "Delete" : // OK!
                                $file = "FileName$i";
                                $cat = "Category$i";
                                $fileid = "FileId$i";
                                list($galloc) = mysql_fetch_row(mysql_query("SELECT galloc FROM ".$mpnTables['gallery_categories']." WHERE gallid=".$$cat));
                                $basedir = "$gallerypath";
                                $wdir = "/";
                                //echo "delete : ".$basedir.$wdir.$galloc.$wdir.$$file."<br>";
                                if (file_exists($basedir.$wdir.$galloc.$wdir.$$file))
                                    @unlink($basedir.$wdir.$galloc.$wdir.$$file);
                                $thumb = getThumbnail($$file, $galloc);
                                //echo "delete thumb: $thumb<br>";
                                if (!strstr($thumb, $imagepath))
                                    @unlink($thumb);
                                $sql  = "DELETE FROM ".$mpnTables['gallery_pictures']." WHERE pid=".$$fileid;
                                //echo $sql;
                                mysql_query($sql);
                                rebuild_totals();
                                rebuild_lastadd();
                                break;
                        }
                    }
                } // for
                rebuild_totals();
                rebuild_lastadd();

                $_SERVER['F_LIB'] -> redirectRequest( $adminurl . '&op=maineditmedia' );

                break;
          } // Switch
        } // If
        else {
            include ("$adminpath/maineditmedia.php");
            maineditmedia();
        }
        break;


    case "quickaddpics": //OK!
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        include ("$adminpath/quickaddmedia.php");
        if (!isset($gallid))
        {
            $gallid = null;
        }

        if (!isset($recursive))
        {
            $recursive = null;
        }

        quickaddpics($gallid, $recursive);
        rebuild_totals();
        rebuild_lastadd();
        echo "<br>";
        include("footer.$phpEx");
        break;
    case "validnew":
#       include("header.$phpEx");
#       admin_menu();
#       echo "<br>";
        $limit=6;
        if (isset($type) && $type!="") {
           //echo $type;
           switch($type) {
            case "validate":
                for ($i=0; $i<$limit; $i++) {
                    $task = "Task$i";

                    if (isset($$task)) {
                        //echo $$task;
                        switch ($$task) {
                            case "Validate" :
                                $fileid = "FileId$i";
                                $desc = "Description$i";
                                $name = "FileName$i";
                                $oldname = "OldFileName$i";
                                $mname = "MediaName$i";
                                $date = "Date$i";
                                $cat = "Category$i";
                                $submitter = "Submitter$i";
                                $width = "Width$i";
                                $height = "Height$i";
                                // We move the picture
                                $basedir = "$modulepath";
                                $wdir = "/";
                                list($newloc) =  mysql_fetch_row(mysql_query("SELECT galloc from ".$mpnTables['gallery_categories']." WHERE gallid=".$$cat));
                                //echo $basedir.$wdir."temp".$wdir.$$oldname." -> ".$basedir.$wdir."gallery".$wdir.$newloc.$wdir.$$name;
                                if (!is_writeable($basedir.$wdir."temp".$wdir.$$oldname) || !is_writable($basedir.$wdir."gallery".$wdir.$newloc.$wdir))
                                {
                                    goBack( translate('Gallery Folders misconfigured for renaming files') );
                                }

                                if (rename($basedir.$wdir."temp".$wdir.$$oldname,$basedir.$wdir."gallery".$wdir.$newloc.$wdir.$$name))
                                {
                                    @chmod($basedir.$wdir."gallery".$wdir.$newloc.$wdir.$$name, 0666);
                                    $ext = substr($$name, (strrpos($$name,'.') +  1));
                                    if ($$height == "") {
                                    $$height = 0;
                                    }
                                    $sql  = "INSERT INTO ".$mpnTables['gallery_pictures']." (pid, gid, img, counter, submitter, date, name, description, votes, rate, extension, width, height) VALUES(NULL, ".$$cat.", '".$$name."', 0, '".$$submitter."', '".$$date."', '".$$mname."', '".$$desc."', 0, 0, '".$ext."', ".$$width.", ".$$height.")";
                                    //echo $sql;
                                    mysql_query($sql);
                                    $sql = "DELETE FROM ".$mpnTables['gallery_pictures_newpicture']." WHERE pid=".$$fileid;
                                    mysql_query($sql);
                                    rebuild_totals();
                                    rebuild_lastadd();
                                }
                                else
                                    echo "Failed to move ".$$name."...<br>\n";
                                break;

                            case "Delete" : // OK
                                $file = "FileName$i";
                                $fileid = "FileId$i";

                                $basedir = "$modulepath/temp";
                                $wdir = "/";
                                //echo "delete : ".$basedir.$wdir.$$file."<br>";
                                @unlink($basedir.$wdir.$$file);
                                $sql  = "DELETE FROM ".$mpnTables['gallery_pictures_newpicture']." WHERE pid=".$$fileid;
                                //echo $sql;
                                mysql_query($sql);
                                rebuild_totals();
                                rebuild_lastadd();
                                break;
                        }
                    }
                } // for

                $_SERVER['F_LIB'] -> redirectRequest( $adminurl );

                //showoption();

                break;

            case "checknew":
                include("header.$phpEx");
                admin_menu();
                echo "<br>";
                include ("$adminpath/validnewmedia.php");
                validnew();
                rebuild_totals();
                rebuild_lastadd();
                include("footer.$phpEx");
                break;
            }// Switch
        } // If
        else

            $_SERVER['F_LIB'] -> redirectRequest( $adminurl );

        break;

    case "editmediatypes" :
        include ("$adminpath/editmediatypes.php");
        if (isset($type) && $type!="") {
            switch($type) {
            case "edit":
                if (isset($save)) {
                    $sql = "UPDATE ".$mpnTables['gallery_media_types']." SET extension='$extension', description='$description', filetype=$class, displaytag='$code', thumbnail='$thumbnail' WHERE extension='$oldext'";
                }
                if (isset($delete)) {
                    $sql = "DELETE FROM ".$mpnTables['gallery_media_types']." WHERE extension='$oldext'";
                }
                mysql_query($sql);

                $_SERVER['F_LIB'] -> redirectRequest( $adminurl . '&do=editmediatypes' );

                break;

            case "new":
                $sql = "INSERT INTO ".$mpnTables['gallery_media_types']." (extension, description,filetype, displaytag, thumbnail) VALUES ('$extension','$description', $class, '$code', '$thumbnail')";
                mysql_query($sql);

                $_SERVER['F_LIB'] -> redirectRequest( $adminurl . '&do=editmediatypes' );

                break;

            }
        }
        else
            include("header.$phpEx");
            admin_menu();
            echo "<br>";
            editmediatypes();
            echo "<br>";
            include("footer.$phpEx");
        break;

    default:
        include("header.$phpEx");
        admin_menu();
        echo "<br>";
        showoption();
        echo "<br>";
        include("footer.$phpEx");
        break;
}

?>
