<?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: displayMedia.php,v 1.12.2.2 2004/04/18 12:47:18 ajvwaay Exp $




function displayPicturePic($row, $size)
{
    global $gallerypath;
    $template = $row['displaytag'];
    $template = str_replace(':FNAME:', "$gallerypath/" . $row['galloc'] . "/" . $row['img'], $template);
    $template = str_replace(':WIDTH:', $size[0], $template);
    $template = str_replace(':HEIGHT:', $size[1], $template);
    $template = str_replace(':DESCRIP:', $row['description'], $template);
    return $template;
}


function displayDescriptionPic($row)
{
    global $displaydescription, $bgcolor1, $bgcolor2, $textcolor1, $textcolor2;
    return '<table border="0" cellpadding="2" cellspacing="2" width="80%" align="center">'
      .'<tr>'
      .'<td align="left" bgcolor="'.$bgcolor2.'" valign="top" width="'.strlen(_GALDESCRIPTION).'"><font  class="meg-normal">'._GALDESCRIPTION.'&nbsp;</font></td>'
      .'<td align="left" bgcolor="'.$bgcolor1.'" valign="top"><font class="meg-normal">'.$row['description'].'</font></td>'
      .'</tr>'
      .'</table>';
}


function displaySizePic($row, $size)
{
    global $module_name, $gallerypath, $textcolor1, $textcolor2;
    return '<font class="meg-normal"><b>'.underscoreTospace($row['name']).'</b></font><br><font class="meg-normal">['.$size[0].' x '.$size[1].' '.substr($row['img'],strrpos($row['img'], '.')+1).']</font>';
}


function displayDateAddedPic($row)
{
    global $bgcolor1, $bgcolor2, $textcolor1, $textcolor2;
    return '<table cellpadding="0" width="160">'
      .'<tr>'
      .'<td width="90" align="left" bgcolor="'.$bgcolor2.'"><font class="meg-sub">'._GALADDED.'</font></td>'
      .'<td width="70" align="left" bgcolor="'.$bgcolor1.'"><font class="meg-sub">'.strftime(_GALDATEBRIEF, $row['unix_time']).'</font></td>'
      .'</tr>'
      .'</table>';
}


function displaySubmitterPic($row)
{
    global $bgcolor1, $bgcolor2, $textcolor1, $textcolor2, $mpnTables;
    $result = mysql_query("SELECT * FROM ".$mpnTables['users']." where uname='".$row['submitter']."'");
    if (mysql_numrows($result)>0)
    {
        $row['submitter'] = '<a href="user.php?op=userinfo&amp;uname='.$row['submitter'].'" target="_blank">'.$row['submitter'].'</a>';
    }

    return '<table width="160">'
          .'<tr>'
      .'<td width="90" align="left" bgcolor="'.$bgcolor2.'"><font class="meg-sub">'._GALSUBMITTER.'</font></td>'
      .'<td width="70" align="left" bgcolor="'.$bgcolor1.'"><font class="meg-sub">'.$row['submitter'].'</font></td>'
      .'</tr>'
      .'</table>';
}


function displayHitsPic($row)
{
    global $bgcolor1, $bgcolor2, $textcolor1, $textcolor2;
    return '<table width="160">'
      .'<tr>'
      .'<td width="90" align="left" bgcolor="'.$bgcolor2.'"><font class="meg-sub">'._GALHITS.'</font></td>'
      .'<td width="70" align="left" bgcolor="'.$bgcolor1.'"><font class="meg-sub">'.$row['counter'].'</font></td>'
      .'</tr>'
      .'</table>';
}


function displayRatePic($row)
{
    global $allowrate, $bgcolor1, $bgcolor2, $textcolor1, $textcolor2;
    if (!$allowrate)
    {
        return;
    }

    return '<table width="160">'
      .'<tr>'
      .'<td width="90" align="left" bgcolor="'.$bgcolor2.'"><font class="meg-sub">'._GALVOTES.'</font></td>'
      .'<td width="70" align="left" bgcolor="'.$bgcolor1.'" ><font class="meg-sub">'.$row['votes'].'</font></td>'
      .'</tr>'
      .'<tr>'
      .'<td width="90" align="left" bgcolor="'.$bgcolor2.'"><font class="meg-sub">'._GALRATING.'</font></td>'
      .'<td width="70" align="left" bgcolor="'.$bgcolor1.'"><font class="meg-sub">'.$row['rate'].'</font></td>'
      .'</tr>'
      .'</table>';
}


function displayRatingBarPic($row, $orderby)
{
    global $module_name, $allowrate, $anonrate, $user, $baseurl, $imagepath, $bgcolor1, $bgcolor2, $textcolor1, $textcolor2;
    if (!$allowrate)
    {
        return;
    }

    if (!$anonrate || $user)
    {
        return '<table width="160" border="0" cellpadding="2" cellspacing="1" align="center">'
              .'<tr>'
          .'<form action="'.$baseurl.'&do=Vote" method="post">'
          .'<input type="hidden" name="pid" value="'.$row['pid'].'">'
          .'<input type="hidden" name="orderby" value="'.$orderby.'">'
          .'<td align="right" valign="center">'
          .'<select name="rate">'
          .'<option value="1">1</option>'
          .'<option value="2">2</option>'
          .'<option value="3">3</option>'
          .'<option value="4">4</option>'
          .'<option value="5">5</option>'
          .'<option value="6">6</option>'
          .'<option value="7">7</option>'
          .'<option value="8">8</option>'
          .'<option value="9">9</option>'
          .'<option value="10" selected>10</option>'
          .'</select>'
              .'</td>'
          .'<td align="left" valign="center">'
          .'<input border="0" src="'.$imagepath.'/rate.jpg" type="image">'
          .'</td>'
          .'</form>'
          .'</tr>'
          .'</table>';
    }
    else
    {
        return '<font class="meg-sub">'._GALMEMBERSRATE.'</font>';
    }
}


function displayCommentsPic($pid, $orderby)
{
    global $adminpath, $bgcolor1, $bgcolor2, $textcolor1, $textcolor2, $allowcomments, $user, $anoncomments, $baseurl, $admin, $imagepath, $mpnTables;

    include("$adminpath/config.php");

    $out = '';

    if ($allowcomments)
    {
        $out = '<TABLE>'
              .'<tr>'
              .'<td colspan="2" align="center">'
              .'<br><font class="meg-normal"><b>'._GALCOMMPOST.'</b></font>'
              .'</td>'
              .'</tr>';

        $result3 = mysql_query("select cid, pid, comment, date, name, member from ".$mpnTables['gallery_comments']." where pid=$pid order by date ASC");
        if (mysql_num_rows($result3) == 0)
        {
            $out .= '<tr>'
                   .'<td colspan="2" align="center">'
                   .'<font class="meg-normal">'._GALNOCOMM.'</font>'
                   .'</td>'
                   .'</tr>';
        }
        else
        {
            $out .= '<tr>'
                   .'  <td colspan="2" align="center">'
                   .'<TABLE border="0" bgcolor="white" cellPadding="2" cellSpacing="2" width="80%" align="center">';

            while(list($cid, $cpid, $comment, $date, $name, $member) = mysql_fetch_array($result3))
            {
                if ($member)
                {
                    $poster = '<a class="meg-normal" href="user.php?op=userinfo&amp;uname='.$name.'">'.underscoreTospace($name).'</a>';
                }
                else
                {
                    $poster =  '<font class="meg-normal">'.underscoreTospace($name).'</font>';
                }

                $out .= '<TR>';

                $out .= '  <TD align="center" bgColor="'.$bgcolor2.'" vAlign="top" width="140"><font class="meg-normal">'.$poster.'<br>('.substr($date,0,10).')</font></TD>'
                       .'  <TD align="left" bgColor="'.$bgcolor1.'" vAlign="top"><font class="meg-normal">'.$comment.'</font></TD>';

                if (is_admin($admin))
                {
                    $out .= '  <TD align="center" width="30"><a href="'.$baseurl.'&amp;do=deletecomment&amp;cid='.$cid.'&amp;pid='.$pid.'&amp;orderby='.$orderby.'" onClick="return confirm(\''._GALSURE2DELETECOMMENT.'\')">'
                           .'<img src="'.$imagepath.'/delete.gif" border="0" alt="'._DELETE.'"></a></td>';
                }

                $out .= '</TR>';
            } //while

            $out .= '</TABLE></TD></TR>';
        }

        $out .= '<TR><TD colspan="2" align="center"><br>';

        if (is_user($user))
        {
            $userdata=cookiedecode($user);
            $out .= '<form action="myegallery.php" method="post">'
                   .'<input type="hidden" name="do" value="Post">'
                   .'<font color="'.$textcolor2.'" size="2">'._GALNAME.'</font>&nbsp;<b>'.$userdata[1].'</b>'
                   .'<input type="hidden" name="gname" value="'.$userdata[1].'">'
                   .'<input type="hidden" name="member" value="1"><br>'
                   .'<font color="'.$textcolor2.'" size="2">'._GALCOMMENT.'</font><br>'
                   .'<input class="textbox" type="text" name="comment" size="70" maxlength="70"><br>'
                   .'<input class="textbox" type="hidden" name="pid" value="'.$pid.'">&nbsp;'
                   .'<input class="button" type="submit" value="' . translate("Post") . '">'
                   .'</form>'
                   ._GALNOTE.'<br><br>';
        }
        elseif (!$anoncomments)
        {
            $out .= '<form action="myegallery.php" method="post">'
                 .'<input type="hidden" name="do" value="Post">'
                 .'<font color="'.$textcolor2.'" size=2>'._GALNAME.'</font><br>'
                 .'<input type="text" SIZE="20" MAXLENGTH="20" name="gname">'
                 .'<input type="hidden" name="member" value="0">'
                 .'<br><font color="'.$textcolor2.'" size=2>'._GALCOMMENT.'</font><br>'
                 .'<input class="textbox" type="text" name="comment" size="70" maxlength="70"><br>'
                 .'<input class="textbox" type="hidden" name="pid" value="'.$pid.'">&nbsp;'
                 .'<input class="button" type="submit" value="' . translate("Post") . '">'
                 .'</form>'
                 ._GALNOTE.'<br><br>';
        }
        else
        {
            $out .= _GALREGISTER."<br><br>";
        }

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

    return $out;
}

function getPos($res, $pid)
{
    $i=0;
    while ($row=mysql_fetch_array($res))
    {
        if ($row['pid'] == $pid)
        {
            return $i;
        }

        $i++;
    }
}


function navigationPic($gid, $pid, $orderby) {
    global $textcolor1, $textcolor2, $adminurl, $module_name, $imagepath, $gallerypath, $modulepath, $adminpath, $languagepath, $baseurl, $adminurl, $mpnTables;

    $orderbyA = convertorderbyin($orderby);
    $sql = "SELECT pid, name FROM ".$mpnTables['gallery_pictures']." where gid=$gid ORDER BY $orderbyA";
    $res = mysql_query($sql);
    $max = mysql_num_rows($res);
    $ind = getPos($res, $pid);
    if ($ind > 0)
    {
    $prev = mysql_result($res, $ind-1, 0);
    $pname = mysql_result($res, $ind-1, 1);
    }

    if ($ind < $max - 1)
    {
        $next = mysql_result($res, $ind+1, 0);
        $nname = mysql_result($res, $ind+1, 1);
    }

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

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

    $out = '<table>'
          .'<tr>'
      .'<td align="right"><font class="meg-normal">&lt;&lt;</font></td>'
      .'<td align="left">';

    if (isset( $prev ))
    {
        $out .= '<a class="meg-normal" href="'.$baseurl.'&amp;do=showpic&amp;gid='.$gid.'&amp;pid='.$prev.'&amp;orderby='.$orderby.'">'._GALPREV.'</a>'
           .'</td>'
           .'<td align="center"><font color="'.$textcolor2.'" size="2">|</font></td>';
    }
    else
    {
    $out .= '<font class="meg-normal">'._GALPREV.'</font></td>'
           .'<td align="center"><font class="meg-normal">|</font></td>';
    }

    $out .= '<td align="right">';

    if (isset($next))
    {
        $out .= '<a class="meg-normal" href="'.$baseurl.'&amp;do=showpic&amp;gid='.$gid.'&amp;pid='.$next.'&amp;orderby='.$orderby.'">'._GALNEXT.'</a>'
               .'</td>';
    }
    else
    {
        $out .= '<font class="meg-normal">'._GALNEXT.'</font></td>';
    }

    $out .= '<td align="left"> <font class="meg-normal">&gt;&gt;</font></td></tr>'
           .'<tr>'
       .'<td align="right"><font class="meg-sub">'.underscoreTospace($pname).'</font></td>'
       .'<td colspan="3">&nbsp;</td>'
       .'<td align="left"><font class="meg-sub">'.underscoreTospace($nname).'</font></td>'
       .'</tr>'
       .'</table>';
    return $out;
}


function rateCollector($pid, $rate)
{
    global $cookiePrefix, $HTTP_COOKIE_VARS, $adminpath, $mpnTables;

    include ("$adminpath/config.php");

    // Fix for lamers that like to cheat on polls
    $ip = getenv("REMOTE_ADDR");
    $past = time()-1800;
    mysql_query("DELETE FROM ".$mpnTables['gallery_rate_check']." WHERE time < $past");
    $result = mysql_query("SELECT ip, pid FROM ".$mpnTables['gallery_rate_check']." WHERE pid=$pid");
    list($ips, $pids) = mysql_fetch_row($result);
    $ctime = time();
    if (($ip == $ips) && ($pid == $pids))
    {
    $voteValid = 0;
    }
    else
    {
    mysql_query("INSERT INTO ".$mpnTables['gallery_rate_check']." (ip, time, pid) VALUES ('$ip', '$ctime', '$pid')");
    $voteValid = 1;
    }
    // Fix end

    if($setRateCookies > 0)
    {
        // we have to check for cookies, so get timestamp of this rate
    list($time) = mysql_fetch_row(mysql_query("SELECT date FROM ".$mpnTables['gallery_pictures']." WHERE pid=$pid"));
    $timeStamp = formatTimestamp($time);
    $cookieName = $cookiePrefix.$timeStamp;
    // check if cookie exists
    if ($HTTP_COOKIE_VARS["$cookieName"] == "1")
        {
            // cookie exists, invalidate this vote
        echo $warn = "You already voted today!";
        $voteValid = 0;
    }
        else
        {
        // cookie does not exist yet, set one now
        $cvalue = 1;
        //setcookie("$cookieName",$cvalue,time()+86400);

        bakeCookie( $cookieName, $cvalue, '86400' );
    }
    }

    // update database if the vote is valid
    if ($voteValid > 0)
    {
        mysql_query("update ".$mpnTables['gallery_pictures']." set votes=votes+1, rate=(rate*(votes-1) + $rate)/votes where pid=$pid");
    }

    // a lot of browsers can't handle it if there's an empty page
//    print '<html><head></head><body></body></html>';

}


function showpic($pid, $orderby)
{
    global $bgcolor1, $bgcolor2, $user, $adminpath, $defaultsortmedia, $modulepath, $mpnTables;

    include("$adminpath/config.php");

    if (!isset($orderby) ||$orderby=="")
    {
        $orderby = $defaultsortmedia;
    }

    $prow = mysql_fetch_array(mysql_query("SELECT p.*, UNIX_TIMESTAMP(p.date) AS unix_time, c.galloc, t.templatePictures, t.templateCSS, f.displaytag, f.filetype, f.description AS description_media FROM ".$mpnTables['gallery_pictures']." AS p LEFT JOIN ".$mpnTables['gallery_categories']." AS c ON c.gallid=p.gid LEFT JOIN ".$mpnTables['gallery_template_types']." AS t ON t.id=c.template LEFT JOIN ".$mpnTables['gallery_media_types']." AS f ON f.extension=p.extension WHERE pid=$pid"));

    // Start Of Output
    galleryHeader();
    OpenTable();

    if ($prow['filetype'] == 1)
    {
        $size = @getimagesize("$gallerypath/" . $prow['galloc'] . "/" . $prow['img']);
    }
    else
    {
        $size[0] = $prow['width'];
    $size[1] = $prow['height'];
    $size[2] = $prow['description_media'];
    $size[3] = "width=\"size[0]\" height=\"size[1]\"";
    }

    $navtree = navigationTree($prow['gid'], $prow['pid']);
    $navpic = navigationPic($prow['gid'], $prow['pid'], $orderby);
    $navgall = navigationGall();

    $iname = displaySizePic($prow, $size);
    $img = displayPicturePic($prow, $size);
    $date = displayDateAddedPic($prow);
    $submitter = displaySubmitterPic($prow);
    $hits = displayHitsPic($prow);
    $rate = displayRatePic($prow);
    $ratingbar = displayRatingBarPic($prow, $orderby);
    $description = displayDescriptionPic($prow);
    $comments = displayCommentsPic($prow['pid'], $orderby);

    $template = $prow['templatePictures'];
    $template = str_replace(':IMG:', $img, $template);
    $template = str_replace(':DESCRIP:', $description, $template);
    $template = str_replace(':RATE:', $rate, $template);
    $template = str_replace(':RATEBAR:', $ratingbar, $template);
    $template = str_replace(':SUBMITTER:', $submitter, $template);
    $template = str_replace(':NAMESIZE:', $iname, $template);
    $template = str_replace(':COMM:', $comments, $template);
    $template = str_replace(':HITS:', $hits, $template);
    $template = str_replace(':DATE:', $date, $template);
    // raw data
    $template = str_replace(':RAWIMG:', $prow['img'], $template);
    $template = str_replace(':RAWIMGSIZE:', $size[3], $template);
    $template = str_replace(':RAWIMGWIDTH:', $size[0], $template);
    $template = str_replace(':RAWIMGHEIGHT:', $size[1], $template);
    $template = str_replace(':RAWDESCRIP:', $prow['description'], $template);
    $template = str_replace(':RAWRATE:', $prow['rate'], $template);
    $template = str_replace(':RAWVOTES:', $prow['votes'], $template);
    $template = str_replace(':RAWSUBMITTER:', $prow['submitter'], $template);
    $template = str_replace(':RAWNAME:', $prow['name'], $template);
//    $template = str_replace(':RAWHITS:', $prow['hist'], $template);
//    $template = str_replace(':RAWDATEBRIEF:', strftime(_GALBRIEFDATE, $prow['unix_time']), $template);

    print '<table border="0" width="100%" align="center" cellpadding="0" cellspacing="0"><tr><td>';
    print '<table border="0" cellpadding="0" cellspacing="1" width="100%">'
         .'<tr>'
     ."<td align=\"center\">$navgall<br>$navtree<br><br>$navpic<br><br>"
     . '<a href="./myegallery.php?do=ecard&amp;pid=' . $pid . '">' . translate('Send an E-Card') . '</a>'
     ."</td>"
     .'</tr>'
     .'<tr>'
     ."<td align=\"center\">$template</td>"
     .'</tr>'
     .'</table>';

    print '</td></tr></table>';
    CloseTable();
    print '<p align="right">'
         .'<font class="meg-sub">'
     .sprintf(_GALCOPYRIGHT, _GALVERSION, "$modulepath/file=credits.php")
     .'</font>'
     .'</p>';
    include("footer.php");
}

?>
