<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* Original Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)   */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2005 by Philippe Brunier   */
/*                                                                      */
/* Based on MyPHPortal (http://sourceforge.net/projects/myphportal)     */
/*                                                                      */
/* 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.       */
/************************************************************************/
if (!function_exists("Mysql_Connexion")) {include ("mainfile.php");}
include_once("lib/file.class.php");
include('functions.php');

function callscript() {
   echo "<script language=\"javascript\" type=\"text/javascript\">
   NS4 = (document.layers) ? 1 : 0;
   IE4 = (document.all) ? 1 : 0;
   if (!IE4)
      MOZ = (document.getElementById) ? 1 : 0;
   else
      MOZ=0;

   function show (name) {
      x = currentX;
      y = currentY + 20;
      if (NS4) {
         document.layers[name].xpos = parseInt(x);
         document.layers[name].left = parseInt(x);
         document.layers[name].ypos = parseInt(y);
         document.layers[name].top = parseInt(y);
         document.layers[name].visibility = \"show\";
      }
      if (IE4) {
         document.all[name].style.left = self.document.body.scrollLeft + parseInt(x);
         document.all[name].style.top = self.document.body.scrollTop + parseInt(y);
         document.all[name].style.visibility = \"visible\";
      }
      if (MOZ) { 
         document.getElementById(name).style.left = parseInt(x);
         document.getElementById(name).style.top = parseInt(y);
         document.getElementById(name).style.visibility = \"visible\";
      }
   }

   function hide (name) {
      if (NS4) {
         document.layers[name].visibility = \"hide\";
      }
      if (IE4) {
         document.all[name].style.visibility = \"hidden\";
      }
      if (MOZ) {
         document.getElementById(name).style.visibility = \"hidden\";
      }
   }
   currentX = currentY = 0;

   function grabEl(e) {
      if (IE4) {
         currentX = event.x;
         currentY = event.y;
      } else {
         currentX = e.pageX;
         currentY = e.pageY;
      }
   }

   if ( NS4 ) {
      document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE);
   }
   document.onmousemove = grabEl;
   </script>";
}

function geninfo($did) {
  global $sitename, $PHP_SELF;
  $result = mysql_query("select dcounter, durl, dfilename, dfilesize, ddate, dweb, duser, dver, dcategory, ddescription, perms from downloads where did='$did'");
  list($dcounter, $durl, $dfilename, $dfilesize, $ddate, $dweb, $duser, $dver, $dcategory, $ddescription, $dperm) = mysql_fetch_row($result);
  $okfile=autorisation($dperm);
  if ($okfile) {
     $title=$dfilename;
     include("header.php");
     OpenTable();
     echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
     echo "$sitename ".translate("File Information");
     echo "</td></tr></table>\n";
     echo "<br />
     <b>".translate("Program Name")." :</b> $dfilename<br /><br />\n
     <b>".translate("File Size")." :</b> ";
     $Fichier = new File($durl);
     if ($dfilesize!=0) {
        echo $Fichier->Pretty_Size($dfilesize);
     } else {
        echo $Fichier->Affiche_Size();
     }
     echo "<br /><br />\n";
     echo "<b>".translate("Version")." :</b> $dver<br /><br />\n";
     echo "<b>".translate("Upload Date")." :</b> ".convertdate($ddate)."<br /><br />\n";
     echo "<b>".translate("Category")." :</b> ".aff_langue(stripslashes($dcategory))."<br /><br />\n
     <b>".translate("Description")." :</b> ".stripslashes($ddescription)."<br /><br />\n
     <b>".translate("Downloads")." :</b> ".wrh($dcounter)."<br /><br />\n
     <b>".translate("Author")." :</b> $duser<br /><br />\n
     <b>".translate("HomePage")." :</b> <a href=\"$dweb\" target=\"_blank\" class=\"NOIR\">$dweb</a><br /><br />\n";
     echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\"><p align=\"center\">\n";
     echo "[ <a href=\"$PHP_SELF?op=mydown&amp;did=$did\" class=\"BOX\">".translate("Download This File Now!")."</a> | <a href=\"javascript:history.go(-1)\" class=\"BOX\">".translate("Go Back")."</a> ]</p>";
     echo "</td></tr></table>\n";
     CloseTable();
     include("footer.php");
  } else {
     Header("Location: download.php");
  }
}

function tlist() {
  global $sortby, $dcategory, $download_cat;
  if ($dcategory == "") { $dcategory = addslashes($download_cat); }
  $cate = stripslashes($dcategory);
  echo "<span class=\"NOIR\">".translate("Select Category Folder")."</span>";
  echo "<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"2\" class=\"TITBOXC\">";
  echo "<tr><td width=\"20%\" nowrap=\"nowrap\">";
  $acounter = mysql_query("SELECT count(*) FROM downloads");
  list($acount) = mysql_fetch_row($acounter);
  if (($cate == translate("All")) OR ($cate == "")) {
     echo "<img src=\"";
     if ($ibid=theme_image("download/folder_open.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/folder_open.gif";}
     echo "$imgtmp\" title=\"".translate("All")." ($acount)\" alt=\"".translate("All")." ($acount)\" border=\"0\" /> <b>".translate("All")." ($acount)</b>\n";
  } else {
     echo "<a href=\"download.php?dcategory=".translate("All")."&amp;sortby=$sortby\" class=\"NOIR\"><img src=\"";
     if ($ibid=theme_image("download/folder.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/folder.gif";}
     echo "$imgtmp\" title=\"".translate("All")." ($acount)\" alt=\"".translate("All")." ($acount)\" border=\"0\" /> ".translate("All")."</a> ($acount)\n";
  }
  $result = mysql_query("select distinct dcategory, count(dcategory) from downloads group by dcategory order by dcategory");
  echo "</td>";
  $rup=1;
  while (list($category, $dcount) = mysql_fetch_row($result)) {
    $rup++;
    $category=stripslashes($category);
    echo "<td width=\"20%\" nowrap=\"nowrap\">";
    if ($category == $cate) {
        echo "<img src=\"";
        if ($ibid=theme_image("download/folder_open.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/folder_open.gif";}
        echo "$imgtmp\" title=\"$category ($dcount)\" alt=\"$category ($dcount)\" border=\"0\" /><span class=\"NOIR\"> <b>".aff_langue($category)." ($dcount)</b></span> \n";
    } else {
        $category2 = urlencode($category);
        echo "<a href=\"download.php?dcategory=$category2&amp;sortby=$sortby\" class=\"NOIR\"><img src=\"";
        if ($ibid=theme_image("download/folder.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/folder.gif";}
        echo "$imgtmp\" title=\"$category ($dcount)\" alt=\"$category ($dcount)\" border=\"0\" /> ".aff_langue($category)."</a> ($dcount) \n";
    }
    echo "</td>";
    if ($rup>=5) {
       echo "</tr><tr>";
       $rup=1;
    }
  }
  echo "</tr></table>";
}

function act_dl_tableheader($dcategory, $sortby, $fieldname, $englishname) {
  echo "<a href=\"download.php?dcategory=$dcategory&amp;sortby=$fieldname\"><img src=\"";
  if ($ibid=theme_image("download/up.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/up.gif";}
  echo "$imgtmp\" alt=\"".translate("Sort Ascending")."\" title=\"".translate("Sort Ascending")."\" border=\"0\" /></a> ";
  echo translate("$englishname");
  echo "<a href=\"download.php?dcategory=$dcategory&amp;sortby=$fieldname&amp;sortorder=DESC\"><img src=\"";
  if ($ibid=theme_image("download/down.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/down.gif";}
  echo "$imgtmp\" title=\"".translate("Sort Descending")."\" alt=\"".translate("Sort Descending")."\" border=\"0\" /></a>";
}

function inact_dl_tableheader($dcategory, $sortby, $fieldname, $englishname) {
  echo "<a href=\"download.php?dcategory=$dcategory&amp;sortby=$fieldname\"><img src=\"";
  if ($ibid=theme_image("download/up.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/up.gif";}
  echo "$imgtmp\" alt=\"".translate("Sort Ascending")."\" title=\"".translate("Sort Ascending")."\" border=\"0\" /></a> <a href=\"download.php?dcategory=$dcategory&amp;sortby=$fieldname\" class=\"HEADA\"> ";
  echo translate("$englishname");
  echo "</a><a href=\"download.php?dcategory=$dcategory&amp;sortby=$fieldname&amp;sortorder=DESC\"><img src=\"";
  if ($ibid=theme_image("download/down.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/down.gif";}
  echo "$imgtmp\" title=\"".translate("Sort Descending")."\" alt=\"".translate("Sort Descending")."\" border=\"0\" class=\"HEADA\" /></a>";
}

function dl_tableheader () {
  echo "</td><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">";
}

function popuploader($did, $ddescription, $dcounter, $dfilename,$aff, $row_color) {
  global $dcategory, $sortby;
  if ($aff) {
     echo "<a name=\"$did\" href=\"download.php?op=geninfo&amp;did=$did\" onMouseOver=\"window.status='".addslashes($dfilename)."'; show('entry-$did'); return true;\" onMouseOut=\"hide('entry-$did'); return true;\"><img src=\"";
     if ($ibid=theme_image("download/info.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/info.gif";}
     echo "$imgtmp\" border=\"0\" alt=\"\" /></a>&nbsp;&nbsp;&nbsp;&nbsp;
     <a href=\"download.php?op=mydown&amp;did=$did\"><img src=\"";
     if ($ibid=theme_image("download/download.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/download.gif";}
     echo "$imgtmp\" border=\"0\" alt=\"".translate("Download Now!")."\" /></a>";
  }
  if ($ibid=theme_image("download/info.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/info.gif";}
  $res= "<div id=\"entry-$did\" style=\"position: absolute;z-index: 20; visibility: hidden; top: 0px; left: 0px; width: 300px;\">\n" .
       "<table border=\"0\" width=\"100%\">\n" .
       "<tr><td class=\"HEADER\">\n" .
       "<table border=\"0\" width=\"100%\" cellpadding=\"5\">\n" .
       "<tr $row_color>\n" .
       "<td>\n" .
       "<img src=\"$imgtmp\" title=\"$did\" alt=\"$did\" border=\"0\" align=\"left\" />\n" .
       "&nbsp;&nbsp;<b>$dfilename</b><br /><br />\n" .
       "".stripslashes($ddescription)."\n" .
       "</td>\n" .
       "</tr>\n" .
       "</table>\n".
       "</td></tr>\n" .
       "</table>\n" .
       "</div>\n";
  return $res;
}

function SortLinks($dcategory, $sortby) {
  $dcategory=stripslashes($dcategory);
  echo "<tr><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">".translate("Info")."</td><td class=\"ONGL\" align=\"center\">".translate("Type")."</td><td class=\"ONGL\" align=\"left\" nowrap=\"nowrap\">";
  if ($sortby == "dfilename" OR !$sortby) {
     act_dl_tableheader($dcategory, $sortby, dfilename, "Name");
  } else {
     inact_dl_tableheader($dcategory, $sortby, dfilename, "Name");
  }
  echo "</td><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">";
  if ($sortby == "dfilesize") {
     act_dl_tableheader($dcategory, $sortby, dfilesize, "Size");
  } else {
     inact_dl_tableheader($dcategory, $sortby, dfilesize, "Size");
  }
  echo "</td><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">";
  if ($sortby == "dcategory") {
     act_dl_tableheader($dcategory, $sortby, dcategory, "Category");
  } else {
     inact_dl_tableheader($dcategory, $sortby, dcategory, "Category");
  }
  echo "</td><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">";
  if ($sortby == "ddate") {
     act_dl_tableheader($dcategory, $sortby, ddate, "Date");
  } else {
     inact_dl_tableheader($dcategory, $sortby, ddate, "Date");
  }
  echo "</td><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">";
  if ($sortby == "dver") {
     act_dl_tableheader($dcategory, $sortby, dver, "Version");
  } else {
     inact_dl_tableheader($dcategory, $sortby, dver, "Version");
  }
  echo "</td><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">";
  if ($sortby == "dcounter") {
     act_dl_tableheader($dcategory, $sortby, dcounter, "Counter");
  } else {
     inact_dl_tableheader($dcategory, $sortby, dcounter, "Counter");
  }
  echo "</td><td class=\"ONGL\" align=\"center\" nowrap=\"nowrap\">&nbsp;</td></tr>";
}

function listdownloads ($dcategory, $sortby, $sortorder) {
  global $perpage, $page, $download_cat, $user;

  $dcategory = urldecode($dcategory);
  $alldivs = "";

  if ($dcategory == "") { $dcategory = addslashes($download_cat); }
  if (!$sortby) { $sortby = "dfilename"; }
  if (($sortorder!="ASC") && ($sortorder!="DESC")) {
     $sortorder = "ASC";
  }
  echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\" align=\"center\">\n";
  echo translate("Display filtered with")." <i>";
  if ($dcategory==translate("All"))
     echo translate("All");
  else
     echo aff_langue(stripslashes($dcategory));
  echo "</i> ".translate("sorted by")." ";

  if ($sortorder=="ASC") {
    echo "<i>".translate("Ascending")."</i>";
  } else {
    echo "<i>".translate("Descending")."</i>";
  }

  if ($sortby == "dfilename") {
    $sortby2 = "".translate("Name")."";
  }
  if ($sortby == "dfilesize") {
    $sortby2 = "".translate("File Size")."";
  }
  if ($sortby == "dcategory") {
    $sortby2 = "".translate("Category")."";
  }
  if ($sortby == "ddate") {
    $sortby2 = "".translate("Creation Date")."";
  }
  if ($sortby == "dver") {
    $sortby2 = "".translate("Version")."";
  }
  if ($sortby == "dcounter") {
    $sortby2 = "".translate("Downloads")."";
  }

  echo " ".translate("of")." <i>$sortby2</i>";
  echo "</td></tr></table>\n";
  echo"<br />";
  echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\" class=\"TITBOXC\">";
  sortlinks($dcategory, $sortby);
  if ($dcategory==translate("All")) {
    $sql="SELECT count(*) FROM downloads";
  } else {
    $sql="SELECT count(*) FROM downloads WHERE dcategory='".addslashes($dcategory)."'";
  }
  $result = mysql_query($sql);
  list($total) =  mysql_fetch_row($result);
  if ($total>$perpage) {
    $pages=ceil($total/$perpage);
    if ($page > $pages) { $page = $pages; }
    if (!$page) { $page=1; }
    $offset=($page-1)*$perpage;
  } else {
    $offset=0;
    $pages=1;
    $page=1;

  }
  settype($offset, "integer");
  settype($perpage, "integer");
  if ($dcategory==translate("All")) {
    $sql="SELECT * FROM downloads ORDER BY $sortby $sortorder limit $offset,$perpage";
  } else {
    $sql="SELECT * FROM downloads WHERE dcategory='".addslashes($dcategory)."' ORDER BY $sortby $sortorder limit $offset,$perpage";
  }

   $result = mysql_query($sql);
   while(list($did, $dcounter, $durl, $dfilename, $dfilesize, $ddate, $dweb, $duser, $dver, $dcat, $ddescription, $dperm) = mysql_fetch_row($result)) {
      $Fichier = new File($durl);
      $row_color = tablos();
      $okfile=autorisation($dperm);
      echo "<tr $row_color><td align=\"center\">";
      if ($okfile==true) {
         $tmpstr = popuploader($did, $ddescription, $dcounter, $dfilename,true, $row_color);
      } else {
         $tmpstr = popuploader($did, $ddescription, $dcounter, $dfilename,false, $row_color);
         echo "<span class=\"ROUGE\">".translate("Private")."</span>";
      }
      echo"</td><td align=\"center\"><img src=\"".$Fichier->Affiche_Extention()."\" alt=\"".$Fichier->Affiche_Extention."\" border=\"0\" align=\"absmiddle\" /></td>
           <td align=\"left\">";
      if ($okfile==true) {
         echo "<a href=\"download.php?op=mydown&amp;did=$did\" class=\"NOIR\">$dfilename</a>";
      } else {
         echo "...";
      }
      echo "</td>
            <td valign=\"bottom\" align=\"center\">";
            if ($dfilesize!=0) {
               echo $Fichier->Pretty_Size($dfilesize);
            } else {
               echo $Fichier->Affiche_Size();
            }
            echo "</td>
            <td valign=\"bottom\" align=\"center\">
            ".aff_langue(stripslashes($dcat))."
            </td>
            <td valign=\"bottom\" align=\"center\">
            &nbsp;".convertdate($ddate)."
            </td>
            <td valign=\"bottom\" align=\"center\">
            &nbsp;$dver
            </td>
            <td valign=\"bottom\" align=\"center\">
            ".wrh($dcounter)."
            </td>
            <td valign=\"bottom\" align=\"right\">";
            if ($user) {
               echo"<a href=\"download.php?op=broken&amp;did=$did\"><img src=\"";
                if ($ibid=theme_image("download/broken-download.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/broken-download.gif";}
                echo "$imgtmp\" border=\"0\" alt=\"".translate("Report Broken Link")."\" /></a>";
            }
      if ($a) {
         $a = 0;
      } else {
         $a = 1;
      }
      $alldivs .= $tmpstr;
      echo "</td></tr>";
   }
   echo "</table>";
   echo "<br /><hr noshade class=\"ONGL\">";
   $dcategory = urlencode($dcategory);
   if ($pages > 1) {
      $pcnt=1;
      echo "<table width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" border=\"0\" class=\"LIGNA\"><tr><td align=\"center\">";
      if ($page > 1) {
          echo "<a href=\"download.php?dcategory=$dcategory&amp;sortby=$sortby&amp;sortorder=$sortorder&amp;page=".($page-1)."\"><img src=\"";
          if ($ibid=theme_image("download/left.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/left.gif";}
          echo "$imgtmp\" alt=\"".translate("Previous Page")."\" border=\"0\" align=\"middle\" /></a>&nbsp;";
      }
      while($pcnt < $page) {
         echo "[ <a href=\"download.php?dcategory=$dcategory&amp;sortby=$sortby&amp;sortorder=$sortorder&amp;page=$pcnt\" class=\"NOIR\">$pcnt</a> ]&nbsp;";
         $pcnt++;
      }
      echo "[ <span class=\"ROUGE\">$page</span> ]&nbsp;";
      $pcnt++;
      while($pcnt <= $pages) {
         echo "[ <a href=\"download.php?dcategory=$dcategory&amp;sortby=$sortby&amp;sortorder=$sortorder&amp;page=$pcnt\" class=\"NOIR\">$pcnt</a> ]&nbsp;";
         $pcnt++;
      }
      if ($page < $pages) {
          echo "&nbsp;<a href=\"$PHP_SELF?dcategory=$dcategory&amp;sortby=$sortby&amp;sortorder=$sortorder&amp;page=".($page+1)."\"><img src=\"";
          if ($ibid=theme_image("download/right.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/download/right.gif";}
          echo "$imgtmp\" alt=\"".translate("Next Page")."\" border=\"0\" align=\"middle\" /></a>\n";
      }
      echo "<td></tr></table>";
   }
   echo $alldivs;
}

function main() {
  global $dcategory, $sortby, $sortorder;
  include("header.php");
  echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
  echo "$sitename ".translate("Download Section");
  echo "</td></tr></table>\n";
  echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td align=\"center\">\n";
  tlist();
  echo "</td></tr></table>\n";
  if ($dcategory!=translate("No category")) {
     callscript();
     listdownloads($dcategory, $sortby, $sortorder);
  }
  if (file_exists("static/download.ban.txt")) {include("static/download.ban.txt");}
  include("footer.php");
}

function transferfile($did) {
  $result = mysql_query("select dcounter, durl, perms from downloads where did='$did'");
  list($dcounter, $durl, $dperm) = mysql_fetch_row($result);
  $okfile=autorisation($dperm);
  if (!$durl) {
     include("header.php");
     opentable();
     echo "<p align=\"center\">$durl : ".translate("There is no such file...")."</p>\n";
     closetable();
     include("footer.php");
  } else {
     if ($okfile) {
        $dcounter++;
        mysql_query("update downloads set dcounter='$dcounter' where did='$did'");
        Header("Location: $durl");
     } else {
        Header("Location: download.php");
     }
  }
}

function broken($did) {
  if ($did) {
     global $notify_email, $notify_message, $notify_from, $cookie, $anonymous;
     settype ($did, "integer");
     if (!$cookie) {
        $submitter=$anonymous;
     } else {
        $submitter=$cookie[1];
     }
     $message=translate("downloads")." ID : $did\n\n".translate("Submitter")." $submitter / IP : ".getip();
     send_email($notify_email, translate("Report Broken Link"), $message, $notify_from , false, "text");
     include("header.php");
     opentable();
     echo "<br /><p align=\"center\">".translate("For security reasons your user name and IP address will also be temporarily recorded.");
     echo "<br /><br />".translate("Thanks for this information. We'll look into your request shortly.")."</p><br />";
     closetable();
     include("footer.php");
  } else {
    Header("Location: download.php");
  }
}

switch ($op) {
  case "main":
       main();
       break;

  case "mydown":
       transferfile($did);
       break;

  case "geninfo":
       geninfo($did);
       break;

  case "broken":
       broken($did);
       break;

  default:
       main();
       break;
}
?>