<?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(); 
}

$warpspeed = strpos($basepath,"http");
if ($warpspeed === false) {
// END - Added by WarpSpeed (Marco Wiesler) @ 31.12.2003

include ("$basepath/public/imageFunctions.php");
include ("$adminpath/fileFunctions.php");

function getThumbnail($img, $galloc) {
        global
                $gallerypath,
                $imagepath,
                $prefix
        ;

        $ext = substr($img, (strrpos($img,'.') +  1));
        $name_gif = str_replace(".$ext", ".gif", $img);
        $name_jpg = str_replace(".$ext", ".jpg", $img);

        if (file_exists("$gallerypath/$galloc/thumb/$name_gif")) {
                $thumb = "$gallerypath/$galloc/thumb/$name_gif";
        }
        elseif (file_exists("$gallerypath/$galloc/thumb/$name_jpg")) {
                $thumb = "$gallerypath/$galloc/thumb/$name_jpg";
        }
        else {
                $row = mysql_fetch_array(mysql_query("SELECT thumbnail from $prefix"._4nalbum_media_types." where extension='$ext'"));
                $thumbnail = $row[thumbnail];
                $thumb = "$imagepath/$thumbnail";
        }

        //echo $thumb;
        return $thumb;
}

function displayPictureGall($filetype, $gid, $pid, $thumbwidth, $galloc, $name, $img, $orderby) {
        global
                $gallerypath,
                $font,
                $adminurl,
                $baseurl,
                $adminpath,
                $imagepath,
                $galleryvar,
                $admin
        ;

        $image = "$gallerypath/$galloc/$img";
        $thumb_dir = "$gallerypath/$galloc/thumb";
        $thumb = "$thumb_dir/$img";

        // create the thumbnail if missing
        if(!file_exists($thumb))
                {
                if(!file_exists($thumb_dir) && $galleryvar['imageSoftware']!="browser")
                {
                        makeDir("$gallerypath/$galloc", "thumb");
                }
                if ($filetype==1) {
                        $thumb = RatioResizeImg("$gallerypath/$galloc/$img", $thumb, $gid);
                }

                if ($thumb=="browser")
                        $thumb = $image;
                else
                        $thumb = getThumbnail($img, $galloc);
        }

        if (is_admin($admin))
                $out = "&nbsp;&nbsp;<a class='".$font['normal']."' href=\"$adminurl&amp;do=editmedia&amp;type=edit&amp;category=$gid&amp;pid=$pid&amp;gid=$gid\"><img src=\"$imagepath/edit.gif\" alt=\"Edit\" border=\"0\"></a>";
      if ($filetype == 1)
        {
                return "<a class=\"".$font['normal']."\" href=\"$baseurl&amp;do=showpic&amp;pid=$pid&amp;orderby=$orderby\">"
                ."<img src=\"$thumb\" width=\"$thumbwidth\" alt=\"".underscoreTospace($name)."\" border=\"0\" align=\"MIDDLE\">"
                .'</a>'.$out;
        }
        else
        {
                return "<a class=\"".$font['normal']."\" href=\"$baseurl&amp;do=showpic&amp;pid=$pid&amp;orderby=$orderby\">"
                ."<img src=\"$thumb\" alt=\"".underscoreTospace($name)."\" border=\"0\" align=\"MIDDLE\">"
                .'</a>'.$out;
        }
}

function displayDescriptionGall($row, $crow) {
        global $font;
        $m = 90 / $crow[numcol];
                if(strlen($row[description]) > $m) {
                        $row[description] = trim(substr($row[description],0,$m));
                        $p = strrpos($row[description], ' ');
                        $row[description] = trim(substr($row[description],0,$p)).'...';
                }
                return "<font class=\"".$font['tiny']."\">$row[description]</font>\n";
}

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

function displaySizeGall($row, $crow) {
        global $font;

        if($row[imginfo][2]) {
                $size = $row[imginfo];
                return "<font class=\"".$font['tiny']."\"><b>"._GALSIZE."</b>: $size[0]x$size[1]</font>";
        }
        else {
                return "<font class=\"".$font['tiny']."\"><b>"._GALSIZE."</b>: "._GALUNKNOWN."</font>";
        }
}

function displayFormatGall($row) {
        global $font;
        $ext = $row[imginfo][2];
        switch($ext) {
                case 1: $type = 'GIF'; break;
                case 2: $type = 'JPEG'; break;
                case 3: $type = 'PNG'; break;
                case 4: $type = 'WBMP'; break;
                default:
                        $type = $ext; break;
        }
        return "<font class=\"".$font['tiny']."\"><b>Format</b>: $type</font>";
}

function displayNumCommentsGall($row) {
        global
                $galleryvar,
                $prefix,
                $font
        ;
        $numcomments = mysql_fetch_array(mysql_query("SELECT COUNT(cid) AS total FROM $prefix"._4nalbum_comments." WHERE pid=$row[pid]"));
        if($galleryvar['allowcomments']) {
                return "<font class=\"".$font['tiny']."\"><b>"._GALNBCOMMENTS."</b>: $numcomments[total]</font>";
        }
}

function displayHitsGall($row) {
        global $font;
        return "<font class=\"".$font['normal']."\"><b>"._GALHITS."</b><br>$row[counter]</font>";
}

function displayRateGall($row) {
        global
                $galleryvar, $font;
        ;
        if($allowrate) {
                return "<font class=\"".$font['normal']."\"><b>"._GALRATING."</b><br>$row[rate]</font>";
        }
}

function displayDateAddedGall($row) {
        global $font;
        return "<font class=\"".$font['normal']."\"><b>"._GALADDED.'</b></font><br><font class="'.$font['tiny'].'">'.strftime(_GALDATEBRIEF,$row[unix_date]).'</font>';
}

function displayName($row) {
        global $font;
        return "<font class=\"".$font['normal']."\"><b>$row[name]</b></font>";
}

function navigationCat($gid, $offset, $orderby) {
        global
                $font,
                $galleryvar,
                $baseurl,
                $prefix
        ;
        $limit = $galleryvar['maxNumberMedia'];
        $orderby = convertorderbyout($orderby);
        $numresults = mysql_query("select * from $prefix"._4nalbum_pictures." where gid=$gid");
        $numrows=mysql_num_rows($numresults);
        $nav = '';
        $pages=intval($numrows/$limit);

        if ($numrows>0 & $pages>=1) {
                if ($offset!=0) {
                        $prevoffset=$offset-$limit;
                        $nav = "<a class=\"".$font['normal']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$prevoffset&amp;orderby=$orderby\">[&lt;&lt;]</a>&nbsp;.&nbsp;";
                }

                if ($numrows%$limit) {
                        $pages++;
                }

                for ($i=1;$i<=$pages; $i++) {
                        $newoffset=$limit*($i-1);
                        if ($newoffset==$offset) {
                                $nav .= "<font class=\"".$font['normal']."\">";
                                if ($i==1)
                                        if ($pages==1)
                                                $nav .= "[&lt;&lt;] . ".$i." . [&gt;&gt;]";
                                        else
                                                $nav .= "[&lt;&lt;] . ".$i."&nbsp;.&nbsp;";
                                elseif  ($i==$pages)
                                        $nav .= $i."&nbsp;.&nbsp;[&gt;&gt;]";
                                else
                                        $nav .= $i."&nbsp;.&nbsp;";
                                $nav .= "</font>";
                        }
                        else {
                                $nav .= "<a class=\"".$font['normal']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=$orderby\">$i</a>&nbsp;.&nbsp;";
                        }
                }
                if (!(($offset/$limit)==($pages-1)) && $pages!=1) {
                        $newoffset=$offset+$limit;
                                $nav .= "<a class=\"".$font['normal']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=$orderby\">[&gt;&gt;]</a>";
                }
        }
        return $nav;
}

function navigationSubCat($gid) {
        global
                $baseurl,
                $adminurl,
                $imagepath,
                $admin,
                $user,
                $prefix,
                $gallerypath,
                $galleryvar,
                $font
        ;
        //$userRank = getUserRank();
        //$result = mysql_query("SELECT gallid, gallname, description, total, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM 4nalbum_categories WHERE parent=$gid AND visible>=$userRank");
        if (is_admin($admin))
                $result = mysql_query("SELECT gallid, gallname, galloc, gallimg, description, visible, total, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._4nalbum_categories." WHERE parent=$gid AND visible>=0 ORDER by gallname");
        else
                $result = mysql_query("SELECT gallid, gallname, galloc, gallimg, description, visible, total, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._4nalbum_categories." WHERE parent=$gid AND visible>0 ORDER by gallname");
        $nb = mysql_num_rows($result);
        if(!$nb) {
                return false;
        }

        $c = 0;
        $out .= '<table border="0" width="50%" align="center" cellpadding="2"><tr>';
        $pc = ceil(100/3);

        $isuser = is_user($user);
        $isadmin = is_admin($admin);
        while($row = mysql_fetch_array($result)) {
                $vis='';
                if ($row[visible]==1 && !$isuser)
                        $vis = '(*)';
                if ($isadmin) {
                        switch ($row[visible])
                        {
                        case 0:
                                $vis .= "&nbsp;<a class=\"".$font['normal']."\" 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['normal']."\" 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\" alt=\""._GALVISIBLEMEMBER.'"></a>&nbsp;';
                                break;
                        default:
                                $vis .= "&nbsp;<a class=\"".$font['normal']."\" 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\" alt=\""._GALVISIBLEPUBLIC.'"></a>&nbsp;';
                                break;
                        }
                }


                if ($galleryvar['displaySubCatIcon']) {
                        if (isset($galleryvar['SubCatIconwidth']) && $galleryvar['SubCatIconwidth']!="")
                                $icon = "<img src=\"$gallerypath/".$row[galloc]."/".$row[gallimg]."\" width=\"".$galleryvar['SubCatIconwidth']."\" border=\"0\" alt=\"$row[description]\">";
                        else
                                $icon = "<img src=\"$gallerypath/".$row[galloc]."/".$row[gallimg]."\" border=\"0\" alt=\"$row[description]\"></a>";
                }

                $out .= "<td>";
                $out .= "<font class=\"".$font['tiny']."\"><img src=\"$imagepath/folder.gif\" width=\"15\" height=\"13\" border=\"0\" alt=\"\"> ";

                if ($row[visible]==1 && !$isuser && !$isadmin) {
                        $out .= underscoreTospace($row[gallname])." ($row[total]) $vis";
                }
                else {
                        $out .= "<a class=\"".$font['normal']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$row[gallid]\">".underscoreTospace($row[gallname])."</a> ($row[total])  $vis";
                        $icon = "<a class=\"".$font['normal']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$row[gallid]\">".$icon."</a>";
                }

                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\" alt=\"".sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
                }
                elseif($row[new_day] < 7) {
                        $out .= "<img src=\"$imagepath/newblue.gif\" border=\"0\" alt=\"".sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
                }

                $out .= "\n</font>\n";

                $out .= "<br>".$icon;
                $out .= "</td>";
                $c++;

                if($c == 3 && $nb != 3) {
                        $out .= '</tr><tr>';
                        $c = 0;
                }
        }
        $out .= '</tr></table>';
        return $out;
}



function convertorderbytrans($orderby) {
    if ($orderby == "counter ASC")        $orderbyTrans = ""._GALPOPULARITY1."";
    if ($orderby == "counter DESC")        $orderbyTrans = ""._GALPOPULARITY2."";
    if ($orderby == "name ASC")                $orderbyTrans = ""._GALNAMEAZ."";
    if ($orderby == "name DESC")        $orderbyTrans = ""._GALNAMEZA."";
    if ($orderby == "date ASC")                $orderbyTrans = ""._GALDATE1."";
    if ($orderby == "date DESC")        $orderbyTrans = ""._GALDATE2."";
    if ($orderby == "rate ASC")                $orderbyTrans = ""._GALRATING1."";
    if ($orderby == "rate DESC")        $orderbyTrans = ""._GALRATING2."";
    return $orderbyTrans;
}



function showgall($gid, $offset, $orderby) {
        global
                $galleryvar,
                $basepath,
                $font,
                $adminpath,
                $prefix,
                $user,
                $admin
        ;
        include "$adminpath/config.php";

        $res = mysql_fetch_array(mysql_query("SELECT count(*) as total FROM $prefix"._4nalbum_categories." WHERE parent=$gid AND visible=1"));
        $crow = mysql_fetch_array(mysql_query("SELECT c.galloc, c.numcol, c.description, c.visible, c.thumbwidth, t.templateCategory, t.templateCSS FROM $prefix"._4nalbum_categories." AS c LEFT JOIN $prefix"._4nalbum_template_types." AS t ON t.id=c.template WHERE gallid=$gid"));

        $ok=0;
        switch ($crow[visible]) {
                case 0:
                        if (is_admin($admin))
                                $ok=1;
                        break;
                case 1:
                        if (is_user($user) || is_admin($admin))
                                $ok=1;
                        break;
                default:
                        $ok=1;
                        break;
        }

        if ($ok) {
                galleryHeader();
                OpenTable();
                if(isset($orderby)) {
                        $orderby = convertorderbyin($orderby);
                }
                else {
                        $orderby = convertorderbyin($galleryvar['defaultsortmedia']);
                }
                if(!$gid) {
                        header("Location: $baseurl");
                        exit;
                }

                $limit = $galleryvar['maxNumberMedia'];

                if(empty($offset)) {
                        $offset = 0;
                }
                $orderbyTrans = convertorderbytrans($orderby);
                $navtree = navigationTree($gid, false);
                $nav =  navigationCat($gid, $offset, $orderby);
                $subcats = navigationSubCat($gid);
                $navgall = navigationGall();
                $result = mysql_query("SELECT m.*, UNIX_TIMESTAMP(m.date) AS unix_date, (TO_DAYS(NOW()) - TO_DAYS(m.date)) AS new_day, f.displaytag, f.thumbnail, f.filetype, f.description AS description_media FROM $prefix"._4nalbum_pictures." AS m LEFT JOIN $prefix"._4nalbum_media_types." AS f ON f.extension=m.extension WHERE gid=$gid ORDER BY $orderby LIMIT $offset,$limit");
                $nb = mysql_num_rows($result);
                $pc = ceil(100/$crow[numcol]);
                $c = 0;
                print '<p align="center">'.$navgall.'</p>';
                print '<p align="center">'.$navtree.'</p>';
                if($subcats != -1) {
                        print "<p align=\"center\">$subcats<hr></p>";
                }

                print "<p align=\"center\"><font class=\"".$font['normal']."\">$crow[description]</font></p>\n";
                if($galleryvar['displaysortbar']) {
                        print '<p align="center"><font class="tiny">'._GALSORTMEDIABY.': '
                                ._NAME." (<a class=\"".$font['tiny']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=titleA\">"._GALASC."</a>\<a href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=titleD\">"._GALDESC.'</a>) '
                                ._DATE." (<a class=\"".$font['tiny']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=dateA\">"._GALASC."</a>\<a href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=dateD\">"._GALDESC.'</a>) '
                                ._RATING." (<a class=\"".$font['tiny']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=ratingA\">"._GALASC."</a>\<a href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=ratingD\">"._GALDESC.'</a>) '
                                ._POPULARITY." (<a class=\"".$font['tiny']."\" href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=hitsA\">"._GALASC."</a>\<a href=\"$baseurl&amp;do=showgall&amp;gid=$gid&amp;offset=$newoffset&amp;orderby=hitsD\">"._GALDESC.'</a>) '
                                .'<br>'._GALMEDIASORTED.": $orderbyTrans</font></p>"
                        ;
                }
                print '<p align="center">'.$nav.'</p>';

                print '<table border="0" width="100%" align="center" cellpadding="2"><tr>';
                while($row = mysql_fetch_array($result)) {
                        $template = $crow[templateCategory];
                        $template = str_replace('<:IMAGE:>', displayPictureGall($row[filetype], $row[gid], $row[pid], $crow[thumbwidth], $crow[galloc], $row[name], $row[img], convertorderbyout($orderby)), $template);
                        // get some image info stuff out of the way here
                        if($row[filetype] == 1) {
                                $row[imginfo] = @getimagesize("$gallerypath/$crow[galloc]/$row[img]");
                        }
                        else {
                                $row[imginfo] = false;
                                $row[imginfo][0] = $row[width];
                                $row[imginfo][1] = $row[height];
                                $row[imginfo][2] = $row[description_media];
                                $row[imginfo][3] = "width=\"$row[width]\" height=\"$row[height]\"";
                        }
                        // ok, back to what we were doing
                        $val_description = displayDescriptionGall($row, $crow);
                        $val_rate = displayRateGall($row);
                        $val_format = displayFormatGall($row);
                        $val_size = displaySizeGall($row, $crow);
                        $val_nmbComments = displayNumCommentsGall($row);
                        $val_new = displayNewGall($row);
                        $val_hits = displayHitsGall($row);
                        $val_date = displayDateAddedGall($row);
                        $val_name = displayName($row);
                        $template = str_replace('<:DESCRIPTION:>', $val_description, $template);
                        $template = str_replace('<:RATE:>', $val_rate, $template);
                        $template = str_replace('<:FORMAT:>', $val_format, $template);
                        $template = str_replace('<:SIZE:>', $val_size, $template);
                        $template = str_replace('<:NBCOMMENTS:>', $val_nmbComments, $template);
                        $template = str_replace('<:NEW:>', $val_new, $template);
                        $template = str_replace('<:HITS:>', $val_hits, $template);
                        $template = str_replace('<:DATE:>', $val_date, $template);
                        $template = str_replace('<:NAME:>', $val_name, $template);

                        print '<td width="'.$pc.'%">'.$template.'</td>';
                        $c++;
                        if($c == $crow[numcol] && $nb != $crow[numcol]) {
                                print '</tr><tr>';
                                $c = 0;
                        }
                }

                print '</tr></table>'
                        ."<center><br>$nav<br>";
                if ($galleryvar['displaySearchForm']) {
                        include("$basepath/public/search.php");
                        print search_form($GLOBALS[HTTP_POST_VARS]);
                }

                print '</center>';

                if ($res[total]>0 && !is_user($user))
                        print '<br><center><font class="'.$font['tiny'].'"><i>'._GALCATMEMBERS.'</i></font></center>';

                CloseTable();
                galleryFooter();
        } // If Ok
        else {
        }
}

}
else {
echo "본 사이트를 해킹을 시도하신다면 모든 기록이 남습니다. 따라서 주소/기록으로 인한 추적을 받을 것입니다!";
}
?>