<?php                                                 //
/**
 * pragmaMx  Content Management System
 * Copyright (c) 2005 pragmaMx Dev Team - http://pragmaMx.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, or
 * (at your option) any later version.
 * 
 * Source: /home/www/dps3311/home/cvsroot/pragmamx/html/modules/Upload/index.php,v $
 * $Revision: ????? $
 * $Author: algebre $
 * $Date: 2007/1106/03 16:48:10 $
 * Based on Upload Module
 * Nuked-KlaN - PHP Portal
 * http://www.nuked-klan.org     
 */
if (!defined("mxMainFileLoaded")) die ("You can't access this file directly...");
$module_name = basename(dirname(__FILE__));
mxGetLangfile($module_name);
$pagetitle = _UPLOAD; 
$upload_url = 'modules.php?name='.$module_name;

function index()
    {
	global $dir, $module_name,$currentlang, $bgcolor1, $bgcolor2, $bgcolor3, $upload_url;

	include("modules/$module_name/config.php");
	include ('header.php');
	openTable();
    
	if ($up_title != "") $title = $up_title;
	else $title = _UPLOAD;

	if ($up_desc != "") $desc = $up_title;
	else $desc = _FILELIST;

	if ($dir != "") $url_dir = "&amp;dir=" . $dir;
	else $url_dir = "";

	$max_filesize = @ini_get('upload_max_filesize');
	$maxsize = $max_size/1024;

	if ($max_filesize != "" && $maxsize > $max_filesize) $max_size_file = "(" . _MAX . " : " . str_replace("M", "", $max_filesize ) . " Mo)";
	else if ($max_size >= 1024) $max_size_file = "(" . _MAX . " : " . $maxsize . " Mo)";
	else  $max_size_file = "(" . _MAX . " : " . $max_size . " Ko)";

	$i = 0;
	$l = 0;

	if (MX_IS_ADMIN)
	{
	    echo "<script type=\"text/javascript\">\n"
	    . "<!--\n"
	    . "\n"
	    . "function del(nom)\n"
	    . "{\n"
	    . "if (confirm('" . _DELETEUPLOADEDFILE . " : '+nom+' ! " . _CONFIRM . "'))\n"
	    . "{document.location.href = '{$upload_url}&op=del_file" . $url_dir . "&fichier='+nom;}\n"
	    . "}\n"
	    . "\n"
	    . "// -->\n"
	    . "</script>\n";
	}

	echo "<form action=\"{$upload_url}&op=send_file" . $url_dir . "\" method=\"post\" enctype=\"multipart/form-data\">\n"
	. "<div style=\"text-align: center;\"><br /><big><b>" . $title . "</b></big><br /></div>\n"
	. "<div style=\"text-align: center;\">" . $desc . "<br /><br />\n"
	. "<input type=\"file\" name=\"fichiernom\" size=\"40\" />&nbsp;<input type=\"submit\" value=\"" . _SEND . "\" /> " . $max_size_file . "</div></form>\n"
	. "<table style=\"margin-left: auto;margin-right: auto;text-align: left;background: " . $bgcolor2 . ";border: 1px solid " . $bgcolor3 . ";\" width=\"90%\"  border=\"0\" cellspacing=\"1\" cellpadding=\"2\">\n"
	. "<tr style=\"background: " . $bgcolor3 . ";\">\n"
	. "<td style=\"width: 10%;\" align=\"center\"><b>#</b></td>\n"
	. "<td style=\"width: 10%;\" align=\"center\"><b>" . _TYPE . "</b></td>\n"
	. "<td style=\"width: 25%;\" align=\"center\"><b>" . _FILENAME . "</b></td>\n"
	. "<td style=\"width: 25%;\" align=\"center\"><b>" . _DATEUPLOAD . "</b></td>\n"
	. "<td style=\"width: 15%;\" align=\"center\"><b>" . _SIZE . "</b></td>\n";

	if (MX_IS_ADMIN) echo "<td style=\"width: 15%;\" align=\"center\"><b>" . _DELUPLOADEDFILE . "</b></td>";
	
	echo "</tr>\n";

	if ($dir != "")
	{
		$i++;
            echo "<tr><td colspan=\"5\">&nbsp;<a href=\"{$upload_url}\"><img style=\"border: 0;\" src=\"modules/$module_name/images/back.gif\" alt=\"\" title=\"" . _BACKDIR . "\" /></a></td></tr>\n";
	}
	else
	{	
            $handle = @opendir($rep_files);
            while (false !== ($rep = readdir($handle)))
            {
	        if ($rep != ".." && $rep != "." && is_dir($rep_files . $rep))
	        {
		    $l++;

		    if ($i == 0)
                    {
                    	$bg = $bgcolor2;
                        $i++;
                    } 
                    else
                    {
                        $bg = $bgcolor1;
                        $i = 0;
                    } 

                    $rep_path = $rep_files . $rep;
                    if ($currentlang == "french") $rep_date = date("d/m/Y " . _AT . " H:i ", filectime($rep_path));
                    else $rep_date = date("m/d/Y " . _AT . " H:i ", filectime($rep_path));

		    echo "<tr style=\"background: " . $bg . ";\">\n"
		    . "<td style=\"width: 10%;\" align=\"center\">" . $l . "</td>\n"			
		    . "<td style=\"width: 10%;\" align=\"center\"><img src=\"modules/$module_name/images/rep.gif\" alt=\"\" title=\"" . _FOLDER . "\" /></td>\n"			
		    . "<td style=\"width: 25%;\" align=\"center\"><a href=\"{$upload_url}&dir=" . $rep . "\">" . $rep . "</a></td>\n"
		    . "<td style=\"width: 25%;\" align=\"center\">" . $rep_date . "</td>\n"	
		    . "<td style=\"width: 15%;\" align=\"center\">...</td>\n";

                    if (MX_IS_ADMIN) echo "<td style=\"width: 15%;\" align=\"center\">...</td>";
                    echo "</tr>\n";			
                } 
            } 
            closedir($handle);
    	}

	if ($dir != "") $rep_fichiers = $rep_files . $dir . "/";
	else $rep_fichiers = $rep_files;

	$handle2 = @opendir($rep_fichiers);
	while (false !== ($f = readdir($handle2)))
	{
	    if ($f != ".." && $f != "." && $f != "index.html" && $f != "Thumbs.db" && is_file($rep_fichiers . $f))
	    {
		$l++;

		if ($i == 0)
		{
		    $bg = $bgcolor2;
		    $i++;
		} 
		else
		{
		    $bg = $bgcolor1;
		    $i = 0;
		} 
		
		$path =  $rep_fichiers . $f;
		$filesize = ceil(filesize($path)/1024);
		if ($filesize > 1024) $size = round($filesize/1024, 2) . "&nbsp;Mo";
		else $size =  $filesize . "&nbsp;Ko";

		if ($currentlang == "french") $filedate = date("d/m/Y " . _AT . " H:i ", filectime($path));
		else  $filedate = date("m/d/Y " . _AT . " H:i ", filectime($path));

		$fichier = explode(".", $f);
		$end = count($fichier) - 1;
		$ext = $fichier[$end];
		$fichier_nom = eregi_replace("." . $ext, "", $f);

		if (is_file("modules/$module_name/images/" . $ext . ".gif")) $type = "<img src=\"modules/$module_name/images/" . $ext . ".gif\" alt=\"\" title=\"" . _FILE . " " . $ext . "\" />";
		else $type = "<img src=\"modules/$module_name/images/generic.gif\" alt=\"\" title=\"" . _FILE . " " . $ext . "\" />";

		echo "<tr style=\"background: " . $bg . ";\">\n"
		. "<td style=\"width: 10%;\" align=\"center\">" . $l . "</td>\n"
		. "<td style=\"width: 10%;\" align=\"center\">" . $type . "</td>\n"
		. "<td style=\"width: 25%;\" align=\"center\"><a href=\"{$upload_url}&op=down_file&amp;dir=" . $dir . "&amp;fichier=" . $f . "\" title=\"" . _DOWNFILE . $f . "\">" . $fichier_nom . "</a></td>\n"
		. "<td style=\"width: 25%;\" align=\"center\">" . $filedate . "</td>\n"
		. "<td style=\"width: 15%;\" align=\"center\">" . $size . "</td>\n";

		if (MX_IS_ADMIN) echo "<td style=\"width: 15%;\" align=\"center\"><a href=\"javascript:del('" . $f . "');\"><img style=\"border:0;\" src=\"modules/$module_name/images/del.gif\" alt=\"\" title=\"" . _DELTHISUPLOADEDFILE . "\" /></a></td>";

		echo "</tr>\n";
	    } 
	} 
	closedir($handle2);

	if ($l == 0) echo "<tr><td colspan=\"6\" align=\"center\">" . _EMPTYDIRECTORY . "</td></tr>";

	echo "</table><br />\n";
	closeTable();
	include ('footer.php');
}

function send_file()
    {
	global $dir,$module_name, $upload_url;
	include("modules/$module_name/config.php");
	$filename = $_FILES['fichiernom']['name'];


	if ($dir != "")
	{
	    $path = $rep_files . $dir . "/";	
	    $url_redirection = "{$upload_url}&dir=" . $dir;
	}
	else
	{
	    $path = $rep_files;
	    $url_redirection = "{$upload_url}";
	}
	
	if ($filename != "")
	{
	    $a = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ";
	    $b = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";
	    $filename = str_replace(" ", "_", $filename);
	    $filename = strtr($filename, $a, $b);
	    $filename = strtolower($filename);
	    $f = explode(".", $filename);
	    $end = count($f) - 1;
	    $ext = $f[$end];
	    $file_name = eregi_replace("." . $ext, "", $filename);
	    $file_name = str_replace(".", "_", $file_name);

	    if ($file_filter == "on" && !in_array(strtolower($ext), $file_filtre))
	    {
    	        include ('header.php');
				openTable();
    	        echo "<br /><br /><div style=\"text-align: center;\"><b>" . _ERRORFILE. "</b></div><br /><br />";
    	        closeTable();
    	        include ('footer.php');
    	        echo "<META http-equiv=\"Refresh\" content=\"5;URL=$url_redirection\">";	
    	        footer();	
    	        exit();			
	     }

	    $filesize = ceil($_FILES['fichiernom']['size']/1024);
		
	    $max_filesize = str_replace("M", "", @ini_get('upload_max_filesize')) * 1024;
	    if ($max_filesize != "" && $max_size > $max_filesize) $max = $max_filesize;
	    else $max = $max_size;

	    if ($filesize > $max)
	    {
    	        include ('header.php');
				openTable();
    	        echo "<br /><br /><div style=\"text-align: center;\"><b>" . _TOOBIGFILE. " (" . $max . " - $filesize)</b></div><br /><br />";
    	        closeTable();
    	        include ('footer.php');
    	        echo "<META http-equiv=\"Refresh\" content=\"5;URL=$url_redirection\">";	
    	        footer();	
    	        exit();			
	     }
			
	     if (eregi("php", $ext) || eregi("htm", $ext)) $ext = "txt";
	     $urlfile = $path . $filename;

	     if ($file_rename == "on" && is_file($urlfile)) $url_file = $path . time() . "_" . $file_name . "." . $ext;
	     else $url_file = $path . $file_name . "." . $ext; 

	     if (is_dir($path) && !is_writable($path)) @chmod($path, 0777);
	     
	     if (is_writable($path))
	     {
    	        move_uploaded_file($_FILES['fichiernom']['tmp_name'], $url_file) or die ("<br /><br /><div style=\"text-align: center;\"><b>Upload file failed !!!</b></div><br /><br />");
    	        @chmod ($url_file, 0644);
    	        $message = _FILESENT;	
	     }
	     else
	     {
    	        $message = _CHMODDIRECTORY;
	     }		
	}
	else
	{
	     $message = _NOFILE;
	}
    include ('header.php');
	openTable();
	echo "<br /><br /><div style=\"text-align: center;\">" . $message . "</div><br /><br />";
	echo "<META http-equiv=\"Refresh\" content=\"3;URL=$url_redirection\">";
	closeTable();
	include ('footer.php');
}

function del_file()
    {
	global $dir, $fichier, $module_name, $upload_url, $mxDemoMode;;

	include("modules/$module_name/config.php");
    if (empty($GLOBALS["mxDemoMode"])) {
	if ($dir != "") 
	{
	    $path = $rep_files . $dir . "/" . $fichier;	
	    $url_redirection = "{$upload_url}&dir=" . $dir;
	}
	else
	{
	    $path = $rep_files . $fichier;
	    $url_redirection = "{$upload_url}";
	}

	if (is_file($path))
	{
	    $filesys = str_replace("/", "\\", $path);	
	    @chmod($path, 0775);
	    @unlink($path);
	    @system("del $filesys");
		
	    if (is_file($path)) $test = 0;
	    else $test = 1;		
	}
	else
	{
	    $test = 0;
	}

	if ($test == 1) $message = _FILEUPLOADEDDEL;
	else  $message = _DELERROR . " : " . $fichier . " !";
	}
    include ('header.php');
	openTable();
	echo "<br /><br /><div style=\"text-align: center;\">" . $message . "</div><br /><br />";
	echo "<META http-equiv=\"Refresh\" content=\"2;URL=$url_redirection\">";
	closeTable();
	include ('footer.php');
}

function down_file()
	{
	global $dir, $fichier, $module_name, $upload_url, $allow_down, $mxDemoMode;
    
	include("modules/$module_name/config.php");
    if (MX_IS_ADMIN || $allow_down !=0 ){
	if ($dir != "") $path = $rep_files . $dir . "/" . $fichier;	
	else $path = $rep_files . $fichier;
	$size = filesize($path);
	if (is_file($path))
	{
	    header("Content-type: application/force-download");
	    header('Pragma: public');
	    header("Pragma: no-cache");
	    header('Cache-Control: no-store, no-cache, must-revalidate');
	    header('Cache-Control: pre-check=0, post-check=0, max-age=0');
	    header('Content-Transfer-Encoding: none');
	    header('Content-Type: application/octet-stream; name="' . $fichier . '"');
	    header('Content-Type: application/octetstream; name="' . $fichier . '"');
	    header('Content-Type: application/download; name="' . $fichier . '"');
	    header('Content-Disposition: attachment; filename="' . $fichier.'"');
	    header("Content-Description: File Transfer");
	    header("Content-length: $size".'bytes');
	    readfile($path);
	}

}else {
	include ('header.php');
	openTable();
	mxErrorScreen("<b></b><br><br>" . _DOWNNOTALLOWED . "", _ACCESSDENIED);
	closeTable();
	include ('footer.php');
	  }
}
    switch ($op)
    {
	case "send_file":
	if (defined('MX_DEMOMODE_RUN')) {
	index();
	exit;
	}else{
	send_file(); 
	}
	break;

	case "del_file":
	if (defined('MX_DEMOMODE_RUN')) {
	index();
	exit;
	}else{
	del_file(); 
	}
	break;

	case "down_file":
	if (defined('MX_DEMOMODE_RUN')) {
	index();
	exit;
	}else{
	down_file(); 
	}
	break;
                        
	default:
	index();
	break;
    }
?>