<?php

/************************************************************************/
/* XAlbum Version 0.9.8                                                 */
/* ==================================================================== */
/* 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.       */
/************************************************************************/

/*----------- ENDE DER EINSTELLUNGEN - END OF SETTINGS -----------*/

// START - Added by WarpSpeed (Marco Wiesler) @ 31.12.2003
// This will avoid that someone can inject php code from outside your site
if( isset($_GET['basepath']) || isset($_GET['adminpath']) ) 
{ 
Header("Location: index.php"); 
die(); 
}
// END - Added by WarpSpeed (Marco Wiesler) @ 31.12.2003

function displayIconCat($row) {
        global
                $baseurl,
                $gallerypath,
                $font,
                $admin,
                $user
        ;
        $imgsize = @getimagesize("$gallerypath/$row[galloc]/$row[gallimg]");
        if ($row[visible]==1) {
                if (is_admin($admin) || is_user($user))
                        return '<a class="'.$font['normal'].'" href="'.$baseurl.'&amp;do=showgall&amp;gid='.$row[gallid].'">'
                                .'<img src="'.$gallerypath.'/'.$row[galloc].'/'.$row[gallimg].'" alt="'.$row[gallname].'" '.$imgsize[3].' border="0">'
                                .'</a>';
                else
                        return '<img src="'.$gallerypath.'/'.$row[galloc].'/'.$row[gallimg].'" alt="'.$row[gallname].'" '.$imgsize[3].' border="0">';
        }
        else
                return '<a class='.$font['normal'].' href="'.$baseurl.'&amp;do=showgall&amp;gid='.$row[gallid].'">'
                        .'<img src="'.$gallerypath.'/'.$row[galloc].'/'.$row[gallimg].'" alt="'.$row[gallname].'" '.$imgsize[3].' border="0">'
                        .'</a>';

}

function displayDescriptionCat($row) {
        global $font;
        return '<font class='.$font['normal'].'>'.nl2br($row[description]).'</font>';
}

function displayCatName($row) {
        global
                $imagepath,
                $font,
                $baseurl,
                $admin,
                $user,
                $adminurl
        ;
        if ($row[visible]==1 && !is_user($user))
                $vis = '<font class="'.$font['tiny'].'">(*)</font>';

        if (is_admin($admin)) {
                switch ($row[visible])
                {
                case 0:
                        $vis .= '&nbsp;<a class="'.$font['title'].'" href="'.$adminurl.'&amp;do=editcategory&amp;type=status&amp;visible=1&amp;category='.$row[gallid].'" title="'._GALVISIBLEADMIN.'"><img src="'.$imagepath.'/red_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEADMIN.'"></a>&nbsp;';
                        break;
                case 1:
                        $vis .= '&nbsp;<a class="'.$font['title'].'" href="'.$adminurl.'&amp;do=editcategory&amp;type=status&amp;visible=2&amp;category='.$row[gallid].'" title="'._GALVISIBLEMEMBER.'"><img src="'.$imagepath.'/yellow_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEMEMBER.'"></a>&nbsp;';
                        break;
                default:
                        $vis .= '&nbsp;<a class="'.$font['title'].'" href="'.$adminurl.'&amp;do=editcategory&amp;type=status&amp;visible=0&amp;category='.$row[gallid].'" title="'._GALVISIBLEPUBLIC.'"><img src="'.$imagepath.'/green_dot.gif" border="0" width="10" height="10" alt="'._GALVISIBLEPUBLIC.'"></a>&nbsp;';
                        break;
                }
        }

        if ($row[visible]==1 && !is_user($user) && !is_admin($admin))
                $out = '<font class="'.$font['title'].'">'.underscoreTospace($row[gallname]).'</font> <font class="content">('.$row[total].') '.$vis.'</font>';
        else
                $out = '<a class="'.$font['title'].'" href="'.$baseurl.'&amp;do=showgall&amp;gid='.$row[gallid].'">'.underscoreTospace($row[gallname]).'</a> <font class="content">('.$row[total].') '.$vis.'</font>';


        if($row[new_day] == 0) {
                $out .= ' <img src="'.$imagepath.'/newred.gif" border="0" alt="'._GALNEWMEDIA1.'">';
        }
        elseif($row[new_day] < 3) {
                $out .= ' <img src="'.$imagepath.'/newgreen.gif" border="0" width="34" height="14" alt="'.sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
        }
        elseif($row[new_day] < 7) {
                $out .= ' <img src="'.$imagepath.'/newblue.gif" border="0" width="34" height="14" alt="'.sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
        }
        return $out;
}

function navigationSubCat($row) {
        global
                $baseurl,
                $bgcolor1,
                $bgcolor2,
                $admin,
                $prefix
        ;
        //$userRank = getUserRank();
        //$result = mysql_query("SELECT gallid, gallname FROM 4nalbum_categories WHERE parent=$row[gallid] AND visible>=$userRank");
        if (is_admin($admin))
                $result = mysql_query("SELECT gallid, gallname FROM $prefix"._4nalbum_categories." WHERE parent=$row[gallid] AND visible>=0 ORDER by gallname");
        else
                $result = mysql_query("SELECT gallid, gallname FROM $prefix"._4nalbum_categories." WHERE parent=$row[gallid] AND visible>0 ORDER by gallname");

        if(!mysql_num_rows($result)) {
                return;
        }
        $c = false;
        $out = '<table border="0" cellpadding="0" cellspacing="0">'
                .'<tr><td>'
        ;
        while($row = mysql_fetch_array($result)) {
                if($c) {
                        $out.= ', ';
                }
                $out .= '<a class="'.$font['title'].'" href="'.$baseurl.'&amp;do=showgall&amp;gid='.$row[gallid].'">'.$row[gallname].'</a>';
                $c = true;;
        }
        $out .= '</td></tr></table>';
        return $out;
}

function viewcats() {
        global
                $galleryvar,
                $basepath,
                $adminpath,
                $admin,
                $user,
                $prefix
        ;
        include "$adminpath/config.php";
        $sql = "SELECT templateCategory, templateCSS FROM $prefix"._4nalbum_template_types." WHERE id=".$galleryvar['maintemplate']."";
        $trow = mysql_fetch_array(mysql_query($sql));

        if (is_admin($admin))
                $result = mysql_query("SELECT *, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._4nalbum_categories." WHERE parent=-1 AND visible>=0 ORDER BY gallname");
        else
                $result = mysql_query("SELECT *, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._4nalbum_categories." WHERE parent=-1 AND visible>0 ORDER BY gallname");
        $res = mysql_fetch_array(mysql_query("SELECT count(*) as total FROM $prefix"._4nalbum_categories." WHERE parent=-1 AND visible=1"));

        $nb = mysql_numrows($result);
        if($nb >= $galleryvar['numColMain']) {
                $pc = 100/$galleryvar['numColMain'];
        }
        else {
                if($nb > 0) {
                        $pc = 100/$nb;
                        $galleryvar['numColMain'] = $nb;
                }
                else {
                        $pc = 100;
                        $galleryvar['numColMain'] = 1;
                }
        }
        // Start Of Output
        galleryHeader();
        OpenTable();
        print '<br>'.navigationGall().'<br><table border="0" width="100%" align="center" cellpadding="2"><tr>';
        $c = 0;
        while($row = mysql_fetch_array($result)) {
                $template = $trow[templateCategory];
                $template = str_replace('<:IMAGE:>', displayIconCat($row), $template);
                $template = str_replace('<:GALLNAME:>', displayCatName($row), $template);
                $template = str_replace('<:DESCRIPTION:>', displayDescriptionCat($row), $template);
                $template = str_replace('<:SUBCATEGORIES:>', navigationSubCat($row), $template);
                print '<td align="center" valign="top" width="'.$pc.'%">'.$template.'</td>';
                $c++;
                if($c == $galleryvar['numColMain'] & $nb != $galleryvar['numColMain']) {
                        print '</tr><tr>';
                        $c = 0;
                }
        }
        print '</tr>'
                .'<tr><td colspan="'.$galleryvar['numColMain'].'" align="center"><br>';

        if ($galleryvar['displaySearchForm']) {
                include("$basepath/public/search.php");
                echo search_form($HTTP_POST_VARS);
        }

        if ($res[total]>0 && !is_user($user))
                print '<font class="'.$font['tiny'].'"><i>'._GALCATMEMBERS.'</i></font>';
        print '</td></tr>'
                .'</table>'
        ;
        CloseTable();
        galleryFooter();
}
?>