<?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: myegallery.php,v 1.9.2.5 2004/04/18 20:15:20 ajvwaay Exp $




######################################################################
# For PHP-NUKE: Web Portal System
# ===========================
# My_eGallery 2.5.6 for myphpNuke
# Copyright (c) 2001 by ~ MarsIsHere ~ (marsishere@yahoo.fr)
# for mpn Implementation Copyright (c) 2001 by ~ xweber
# http://kodred.fr.st
#
# This modules is for image galleries
#
# 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.
######################################################################

//include("functions.$phpEx");

if (!isset($mainfile))
{
    include ("mainfile.$phpEx");
}

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

$langType = 'public';

if (file_exists("$languagepath/lang-$language.php"))
{
    include("$languagepath/lang-$language.php");
}
else
{
    include("$languagepath/lang-english.$phpEx");
}


function underscoreTospace($name)
{
    $name = str_replace("_"," ",$name);
    return $name;
}


function getUserRank()
{
    global $user,$admin;

    if (is_admin($admin))
    {
        return 0;
    }

    if (is_user($user))
    {
        return 1;
    }

    return 2;
}


function galleryHeader()
{
    global $modulepath, $phpEx, $mpnTables;
    include("header.$phpEx");
    print "<LINK REL=\"StyleSheet\" HREF=\"$modulepath/style/style.css\" TYPE=\"text/css\">\n\n\n";
}


function navigationGall()
{
    global $adminurl, $module_name, $imagepath, $gallerypath, $modulepath, $adminpath, $languagepath;
    global $baseurl, $allowpostpics, $displaytop, $do, $user, $admin, $mpnTables, $phpEx;

    if (is_admin($admin))
    {
        $row = mysql_fetch_array(mysql_query("SELECT COUNT(p.pid) AS total FROM ".$mpnTables['gallery_pictures']." AS p LEFT JOIN ".$mpnTables['gallery_categories']." AS c ON c.gallid=p.gid WHERE c.visible>=0 AND (TO_DAYS(NOW()) - TO_DAYS(p.date))<7"));
    }
    else
    {
        $row = mysql_fetch_array(mysql_query("SELECT COUNT(p.pid) AS total FROM ".$mpnTables['gallery_pictures']." AS p LEFT JOIN ".$mpnTables['gallery_categories']." AS c ON c.gallid=p.gid WHERE c.visible>0 AND (TO_DAYS(NOW()) - TO_DAYS(p.date))<7"));
    }

    $row2 = mysql_fetch_array(mysql_query("SELECT SUM(total) AS total FROM ".$mpnTables['gallery_categories']." WHERE visible>0 AND parent=-1"));

    $out = '<table width="100%" align="center" border="0">'
          .'<tr><td align="center">'
          .'<font class="meg-title">[&lt;</font> '
          .'<a class="meg-title" href="'.$baseurl.'">'._GALHOME.'</a>';

    if ($displaytop)
    {
        $out .= " <font class=\"meg-title\">|</font> <a class=\"meg-title\" href=\"$baseurl&amp;do=top\">"._GALTOP10.'</a>';
    }

    if ($allowpostpics)
    {
        $out .= " <font class=\"meg-title\">|</font> <a class=\"meg-title\" href=\"$baseurl&amp;do=upload\">"._GALPOSTMEDIA.'</a>';
    }

    $out .= ' <font class="meg-title">&gt;]</font>';
    $out .= "<br><font class=\"meg-sub\">$row2[total] "._GALIMAGES."</font>";
    if ($row['total'] && !$do)
    {
        $out .= " <font class=\"meg-sub\">($row[total] "._GALNEWIMAGES.")</font>";
    }

    $out .= '</td></tr></table>';
    return $out;
}


function navigationTree($gid, $pid)
{
    global $adminurl, $module_name, $imagepath, $gallerypath, $adminpath, $languagepath, $baseurl, $adminurl;
    global $bgcolor1, $bgcolor2, $textcolor1, $textcolor2, $mpnTables, $phpEx;

    list($gallname, $parent) = mysql_fetch_row(mysql_query("select gallname, parent from ".$mpnTables['gallery_categories']." WHERE gallid=$gid"));
    if (isset($pid) && $pid!="")
    {
        list($name) = mysql_fetch_row(mysql_query("select name from ".$mpnTables['gallery_pictures']." WHERE pid=$pid"));
    }

    global $bgcolor1, $bgcolor2;

    $out  = "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#000000\"><tr><td>\n";
    $out .= "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"8\" bgcolor=\"$bgcolor2\"><tr><td>\n";

    $out .= '<a class="meg-title" href="'.$baseurl.'">'._GALHOME.'</a>';
    $par = $parent;
    while ($par!=-1)
    {
        list ($gallid, $gname, $par) = mysql_fetch_row(mysql_query("select gallid, gallname, parent from ".$mpnTables['gallery_categories']." WHERE gallid=$par"));
        $out .= " &gt;&gt; <a class=\"meg-title\" href=\"$baseurl&amp;do=showgall&amp;gid=$gallid\">".underscoreTospace($gname)."</a>";
    }

    if (isset($pid) && $pid!="")
    {
        $out .= " <font class=\"meg-title\">&gt;&gt;</font> <a class=\"meg-title\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid\">".underscoreTospace($gallname)."</a>";
    }
    else
    {
        $out .= " <font class=\"meg-title\">&gt;&gt; ".underscoreTospace($gallname).'</font';
    }

    if (isset($name) && $name!="")
    {
        $out .= " <font class=\"meg-title\">&gt;&gt; ".underscoreTospace($name).'</font';
    }

    $out .= "</td></tr></table></td></tr></table>";
    return $out;
}


function postcomment($pid, $comment, $gname, $member)
{
    global $module_name, $sitename, $mpnTables;
    mysql_query("insert into ".$mpnTables['gallery_comments']." values (NULL, '$pid', '$comment', now(), '$gname', $member)");
}


function indent($gid)
{
    global $mpnTables;
    $tab = 0;
    list($parent) = mysql_fetch_row(mysql_query("SELECT parent FROM ".$mpnTables['gallery_categories']." WHERE gallid=$gid"));
    while ($parent != -1)
    {
        list($parent) = mysql_fetch_row(mysql_query("SELECT parent FROM ".$mpnTables['gallery_categories']." WHERE gallid=$parent"));
        $tab ++;
    }
    return $tab;
}


function convertorderbyin($orderby)
{
    if ($orderby == "titleA") $orderby = "name ASC";

    if ($orderby == "dateA")  $orderby = "date ASC";

    if ($orderby == "hitsA")  $orderby = "counter ASC";

    if ($orderby == "ratingA")  $orderby = "rate ASC";

    if ($orderby == "titleD")  $orderby = "name DESC";

    if ($orderby == "dateD")  $orderby = "date DESC";

    if ($orderby == "hitsD")  $orderby = "counter DESC";

    if ($orderby == "ratingD")  $orderby = "rate DESC";

    return $orderby;
}

function convertorderbyout($orderby)
{
    if ($orderby == "name ASC")  $orderby = "titleA";

    if ($orderby == "date ASC")  $orderby = "dateA";

    if ($orderby == "counter ASC")  $orderby = "hitsA";

    if ($orderby == "rate ASC")  $orderby = "ratingA";

    if ($orderby == "name DESC")  $orderby = "titleD";

    if ($orderby == "date DESC")  $orderby = "dateD";

    if ($orderby == "counter DESC")  $orderby = "hitsD";

    if ($orderby == "rate DESC")  $orderby = "ratingD";
    return $orderby;
}


function search_form($vars)
{
    global $bgcolor1, $bgcolor2, $bgcolor3, $textcolor1, $textcolor2, $module_name, $phpEx;

    if (!isset($vars['bool']))
    {
        $vars['bool'] = 'AND';
    }

    if (isset( $vars['q'] ))
    {
        $q = htmlentities($vars['q']);
    }
    else
    {
        $q = '';
    }

    $bool_select[$vars['bool']] = ' selected';

    if (!isset($bool_select['OR']))
    {
        $bool_select['OR'] = null;
    }

    if (!isset($bool_select['AND']))
    {
        $bool_select['AND'] = null;
    }

    return "<p align=\"center\">"
          .'<form action="myegallery.php" method="post">'
          .'<input type="hidden" name="do" value="search">'
          .'<input type="hidden" name="search_picname" value="1">'
          .'<input type="hidden" name="search_picdesc" value="1">'
          .'<input type="hidden" name="search_catname" value="0">'
          .'<input type="hidden" name="search_catdesc" value="0">'
          .'<table border="0" cellpadding="2" cellspacing="0" summary="Search form to help in locating the items you are looking for.">'
          // query
          .'<tr>'
          .'<td nowrap><font size="2"><b><label for="q">'._SEARCH.'</label>:</b></font></td>'
          .'<td colspan="2" align="center"><font size="2">'
          ."<input type=\"text\" name=\"q\" id=\"q\" size=\"20\" maxlength=\"255\" value=\"$q\"> "
          .'<input class="button" type="submit" value="'._SEARCH.'">'
          .'</font></td>'
          .'</tr>'
          // bool
          .'<tr>'
          .'<td>&nbsp;</td>'
          .'<td colspan="2" align="center"><font size="2">'
          .'<select name="bool" size="1">'
          ."<option value=\"AND\"" . $bool_select['AND'] . ">"._GALALLWORDSAND."</option>"
          ."<option value=\"OR\"" . $bool_select['OR'] . ">"._GALALLWORDSOR."</option>"
          .'</select>'
          .'</font></td>'
          .'</tr>'
          .'</table>'
          .'</form>';
}

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

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

switch($do)
{
    case 'ecard':
        include("$modulepath/ecard.$phpEx");
        displayPostCardForm($pid);
        break;

    case 'ecard_send':
        include("$modulepath/ecard.$phpEx");
        ecard_send();
        break;

    case "showgall":
        include("$modulepath/displayCategory.$phpEx");
        showgall($gid, $orderby);
        break;

    case "showpic":
        include("$modulepath/displayMedia.$phpEx");
    mysql_query("update ".$mpnTables['gallery_pictures']." set counter=counter+1 where pid=$pid");
    showpic($pid, $orderby);
        break;

    case "top":
        include("$modulepath/displayTop.$phpEx");
    top();
        break;

    case "New":
        newpics();
        break;

    case "Post":
        postcomment($pid, $comment, $gname, $member);

        $_SERVER['F_LIB'] -> redirectRequest( $baseurl . '&do=showpic&pid=' . $pid );

        exit;
    break;

    case "Vote":
    include("$modulepath/displayMedia.$phpEx");
    rateCollector($pid, $rate);
    showpic($pid, $orderby);
    break;

    case "upload":

    include("$modulepath/uploadFile.$phpEx");
    include("$adminpath/fileFunctions.$phpEx");
    if (isset($add) && $add=="Upload")
        {
            //REF: Add($gid, $file, $submitter, $medianame, $description, $userfile, $userfile_name, $userfile_size)

            $upload_return = Add($Category, $_FILES['userfile']['name'], $Submitter, $MediaName, $Description, $_FILES['userfile'], $_FILES['userfile']['name'], $_FILES['userfile']['size']);
            if ($upload_return > 0)
            {
                galleryHeader();
        OpenTable();
        echo navigationGall();
        echo '<br>';
        if ($upload_return==1)
                {
                    echo '<center>'._GALNOMEDIA.'<br><br>'._GOBACK.'</center>';
                }
                else
                {
                    if ($upload_return==2)
                    {
                        echo '<center>'._GALPBUPLOAD.'<br><br>'._GOBACK.'</center>';
                    }

                    CloseTable();
                    include("footer.$phpEx");
                 }
            }
            else
            {
                if ($upload_return == "0")
                {
                    include("header.$phpEx");
                    print "<LINK REL=\"StyleSheet\" HREF=\"$modulepath/style/style.css\" TYPE=\"text/css\">\n\n\n";
            echo "<br>";
            OpenTable();
            echo navigationGall();
            echo '<br>';
            echo "<center><b>"._GALMEDIARECEIVED."</b><br>";
            echo _GALCHECKFORIT;
            CloseTable();
            print '<p align="right">'
                 .'<font class="meg-sub">'
             .sprintf(_GALCOPYRIGHT, _GALVERSION, "$baseurl?op=modload&amp;name=$module_name&amp;file=credits")
             .'</font>'
             .'</p>';
                    include("footer.$phpEx");
        }
        }
    }
    else
        {
            upload_file();
        }

        break;

    case 'search':
    include "$modulepath/displayCategory.$phpEx";
    search_go($HTTP_POST_VARS);
    break;

    case 'deletecomment':
    include("$modulepath/displayMedia.$phpEx");
        $sql = "DELETE from ".$mpnTables['gallery_comments']." where cid=$cid";
    mysql_query($sql);
        showpic($pid, $orderby);
    break;

    default:
    include("$modulepath/mainGallery.$phpEx");
    viewcats();
        break;
}
?>
