<?php
include("postget.php");
include_once("common.php");
$pagetitle = _DOWNLOADS;

checklevel(4,$_REQUEST['clientid']);
include("postget.php");


if ($_REQUEST[click]==""._ADD_FILE."") {
    $errors = '';
    if ($_POST['name'] == '') {
        $errors.= "<br>" . ucfirst(_NAME) . " " . _NOT_SPECIFIED . "";
    }
    if ($_POST['upload4ftp'] == 'on') {
        if ($_POST['filename'] == '') {
            $errors.= "<br>" . ucfirst(_FILE) . " " . _NOT_SPECIFIED . "";
        }
    } else {
        if ($_FILES['file']['name'] == '') {
            $errors.= "<br>" . ucfirst(_FILE) . " " . _NOT_SPECIFIED . "";
        }
        $extension = explode('.', $_FILES['file']['name']);
        $extension = array_pop($extension);
        if (substr_count($config[125][1], $extension) == 0) {
            $errors.= "<br>" . ucfirst(_FILE) . " "._NOT_ALLOWED;
        }
    }
}

if($errors=='' AND $_REQUEST[click]==""._ADD_FILE."") {

	if ($_POST['upload4ftp']=='') 	{
		$newfilename = $_FILES['file']['name'];
		$attachdir = $config[75][1].'/';
		if ($upload4ftp=='') {
			if(!@copy ($_FILES['file']['tmp_name'], $attachdir . $newfilename)) {
				$errors= error_get_last();
				echo "COPY ERROR: ".$errors['type'];
				echo "<br />\n".$errors['message']; exit;
			}
		}
		$file = $newfilename;
	}else{
		$file = $_POST['filename'];
	}


	$now=time()+$config[31][1]*3600;

	if($onlyclients=="on"){$onlyclients=1;}else{$onlyclients=0;}

	$query="INSERT INTO hd_downloads (name,description,file,date,onlyclients,position)
	VALUES ('$name','$description','$file','$now','$onlyclients','$neworder')";


	if(!mysql_query($query,$db_link)){
		messagetop("ERROR","DEBUG: ".$query."<br>MYSQL ERROR: ".mysql_error(),'downloads_admin.php',0);
	}else{
		messagetop(_THE_CHANGES_WHERE_MADE_SUCCESSFULLY,'','downloads_admin.php',1);
	}
}

include("head_template.php");

if ($_REQUEST[option]=="addfile") {
	echo '
<div class="container">
	<div class="row">
';

	print errors($errors); $errors = '';


	if ($_REQUEST[upload4ftp]=='on'){
		$stylenombre = "style='display: \"\";'";
		$stylesubir = "style='display: none;'";
		$chequeado = 'checked';
	}else{
		$stylenombre = "style='display: none;'";
		$stylesubir = "style='display: \"\";'";
		$chequeado = '';
	}
	
	

	print("
	<form method='post' enctype='multipart/form-data'>
		<div class='row'>
			<div class='span3 offset3'><b>".ucfirst(_NAME).":</b></div>
			<div class='span3'><input type='text' size='60' name='name' value=".$_POST[name]." ></div>
		</div>
		
		<div class='row'>
			<div class='span3 offset3'>".ucfirst(_DESCRIPTION).":</div>
			<div class='span3'><TEXTAREA cols=100 rows=8 name='description'>".$_POST[description]."</TEXTAREA></div>
		</div>
		
		<div class='row'>
			<div class='span3 offset3'><b>".ucfirst(_UPLOAD_THE_FILE_VIA_FTP).":</b></div>
			<div class='span3'>
				<input type='checkbox' name='upload4ftp' $chequeado onchange='
	if(this.checked){
		OcultarFilas(\"subirarchivo\");
		MostrarFilas(\"nombrearchivo\");
	}else{
		MostrarFilas(\"subirarchivo\");
		OcultarFilas(\"nombrearchivo\");
	}
	'>
			</div>
		</div>
		
		<div class='row' id='subirarchivo' name='subirarchivo' $stylesubir>
			<div class='span3 offset3'><b>".ucfirst(_FILE).":</b></div>
			<div class='span3'>
				<input type='file' name='file'>&nbsp;&nbsp;
			</div>
		</div>
		
		<div class='row' id='nombrearchivo' name='nombrearchivo' $stylenombre>
			<div class='span3 offset3'><b>".ucfirst(_FILE).":</b></div>
			<div class='span3'>
				<input type='text' name='filename' size='60' value=".$_POST[filename]."><br>".ucfirst(_UPLOAD_THIS_FILE_TO)." /".$config[75][1]."
			</div>
		</div>
		
		<div class='row'>
			<div class='span3 offset3'><b>".ucfirst(_VISIBLE_ONLY_TO_REGISTERED_CLIENTS).":</b></div>
			<div class='span3'>
			<input type='checkbox' name='onlyclients'");
	if($_REQUEST[id]){if ($row[onlyclients]==1){print(" checked");};}
	print("></div>
		</div>");


	print("
	
	
	<div class='row'>
        <div class='span3 offset4'>
	<input type='submit' name='click' value='"._ADD_FILE."'>
            </div></div>
	</form>
	");



echo '
</div>
</div>
';
}





echo '
<div class="container">
	<div class="row">
';

echo '<table id="table-1" class="table table-striped">';
print("
	<thead><tr id='0' class='nodrop nodrag'>
	<th>&nbsp;</th> 
	<th>".ucfirst(_TYPE)."</th>
	<th>".ucfirst(_ID)."</th>
	<th>".ucfirst(_NAME)."</th> 
	<th>".ucfirst(_FILE)."</th> 
	<th>".ucfirst(_DESCRIPTION)."</th> 
	<th>".ucfirst(_DATE)."</th> 
	<th>".ucfirst(_SIZE)."</th>
	<th>".ucfirst(_DOWNLOADS)."</th>
	<th>".ucfirst(_ONLY_CLIENTS)."</th>
	<th>".ucfirst(_OPTIONS)."</th> 
	</tr></thead><tbody>");

$data = query("SELECT * FROM hd_downloads ORDER by position ASC",0);

$_SESSION['ordenar'] = "[hd_downloads]{position}(id)<>";

while($row=mysql_fetch_array($data)) {

	print("<tr><td class='dragHandle'>&nbsp;</td>");

	print("<td width='50'>");
	$extension = explode('.',$row[file]) ;
	$extension = array_pop($extension) ;
	if (file_exists("images/fileformats/".$extension.".png")){
		$imagen =  "images/fileformats/".$extension.".png" ;
	}else{
		$imagen =  "images/fileformats/other.png" ;
	}
	print("<img src='".$imagen."' width='50' border='0' title=''>");
	print("</td>");

	print("<td align='center'>".$row[id]."</td>");

	$tamanioarchivo = @byteConvert(filesize($config[75][1].'/'.$row[file])) ;

	print("<td align='left'><b>".$row[name]."</b></td>");
	print("<td align='left'>");
	if ($tamanioarchivo=='0,00 B' OR $tamanioarchivo=='0 B') {
		print ("<font color='red'><b>".$row[file]."</b></font>");
	}else{
		print ($row[file]);
	}
	print("</td>");
	print("<td align='left'>".$row[description]."</td>");

	print("<td align='center'>");
	if ($row[date]>0) {
		print(date($df,$row[date]));
		print("<br>(".TimeAgo($row[date],time()).")");
	}
	print("</td>");

	print("<td align='center'>");
	print($tamanioarchivo);
	print("</td>");


	print("<td align='center'>".$row[downloads]."&nbsp;</td>");

	print("<td align='center'>"); if ($row[onlyclients]==1) {print("<img class='sprites green_status' src='images/transparent.gif' border='0'>");}
	print("&nbsp;</td>");

	print("<td align='center'>");
	print("<a href='".$serverurl."/downloads_edit.php?id=$row[id]'><img class='sprites edit' src='images/transparent.gif' border='0' title='"._EDIT."'></a>");
	print("<a href='".$serverurl."/downloads_delete.php?id=$row[id]&text=".strip_tags($row[name])."'><img class='sprites del' src='images/transparent.gif' border='0' title='"._DELETE."'></a>");
	print("</td>");
	print("</tr>");
}
print("</tbody></table>");

graybutton("downloads_admin.php?option=addfile",_ADD_FILE);

echo '
</div>
</div>
';

include("foot_template.php");
?>
	
