<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/********************************************************/
/* NSN GR Downloads                                     */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright  2000-2005 by NukeScripts Network         */
/********************************************************/
defined('MAXIMUS') or Header("Location: index.php");

$lid = $GLOBALS['db']->XMFnum($lid);
$lidinfo = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT * FROM `".TABLE_NSNGD_DOWNLOADS."` WHERE `lid`='$lid' "));
$priv = $lidinfo['sid'] - 2;
if (($lidinfo['sid'] == 0) || ($lidinfo['sid'] == 1 AND defined('USER_ALLOW')) || ($lidinfo['sid'] == 2 AND defined('ADMIN_ALLOW')) || ($lidinfo['sid'] > 2 AND of_group($priv))) {
  if ($fetchid != "") {
    $datekey = date("F j");
    $rcode = hexdec(md5($_SERVER[HTTP_USER_AGENT] . $sitekey . $checkpass . $datekey));
    $code = substr($rcode, 2, 6);
    if ((extension_loaded("gd") AND $code != $passcode) AND $dl_config['usegfxcheck'] == 1) {
      incoh();
      title(_DL_PASSERR);
      Max_OpenTable();
      echo "<center>"._DL_INVALIDPASS."</center><br />\n";
      echo "<center>"._GOBACK."</center>\n";
      Max_CloseTable();
      incof();
      die();
    } elseif ((!extension_loaded("gd") AND $checkpass != $passcode) AND $dl_config['usegfxcheck'] == 1) {
      incoh();
      title(_DL_PASSERR);
      Max_OpenTable();
      echo "<center>"._DL_INVALIDPASS."</center><br />\n";
      echo "<center>"._GOBACK."</center>\n";
      Max_CloseTable();
      incof();
    } else {
      $url = base64_decode($fetchid);
      if (stristr($lidinfo['url'], "http://") || stristr($lidinfo['url'], "ftp://") || @file_exists($lidinfo['url'])) {
      //if (@file($lidinfo['url'])) {
        include_once('includes/counter.php');
        $GLOBALS['db']->sql_query("UPDATE `".TABLE_NSNGD_DOWNLOADS."` SET `hits`=hits+1 WHERE `lid`='$lid' ");
        if (!defined('ADMIN_ALLOW')) {
          $uinfo = getusrinfo($user);
          $username = stripslashes($uinfo['username']);
          if ($username == "") { $username = $_SERVER['REMOTE_ADDR']; }
          $result = $GLOBALS['db']->sql_numrows($GLOBALS['db']->sql_query("SELECT * FROM `".TABLE_NSNGD_ACCESSES."` WHERE `username`='$username'"));
          if ($result < 1) {
            $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_NSNGD_ACCESSES."` VALUES ('$username', 1, 0)");
          } else {
            $GLOBALS['db']->sql_query("UPDATE `".TABLE_NSNGD_ACCESSES."` SET `downloads`=downloads+1 WHERE `username`='$username'");
          }
        }
	$url_dl = stripslashes($lidinfo['url']);
        redirect_url("$url_dl");
        exit;
      } else {
        cookiedecode($user);
        $username = $cookie[1];
        if ($username == "") { $username = "Guest"; }
        $date = date("M d, Y g:i:a");
        $sub_ip = $_SERVER['REMOTE_ADDR'];
        $GLOBALS['db']->sql_query("INSERT INTO `".TABLE_NSNGD_MODS."` VALUES (NULL, $lid, 0, 0, '', '', '', '"._DSCRIPT."<br />$date', '$sub_ip', 1, '$auth_name', '$email', '$filesize', '$version', '$homepage')");
        incoh();
        title(_DL_FNF." ".$lidinfo['title']);
        Max_OpenTable();
        echo "<center>"._DL_SORRY." $username, ".$lidinfo['title']." "._DL_NOTFOUND."<br /><br />"._DL_FNFREASON."<br /><br />";
        echo _DL_FLAGGED."</center><br />";
        echo "<center>[ <a href='modules.php?name=$module_name'>"._DL_BACKTO." $module_name</a> ]</center>";
        Max_CloseTable();
        incof();
      }
    }
  } else {
    incoh();
    title(_DL_URLERR);
    Max_OpenTable();
    echo "<center>"._DL_INVALIDURL."</center><br />";
    echo "<center>"._GOBACK."</center>";
    Max_CloseTable();
    incof();
  }
} else {
  incoh();
  title(_DL_RESTRICTED);
  Max_OpenTable();
  restricted($lidinfo['sid']);
  Max_CloseTable();
  incof();
}