Source for file helpdocs.php
Documentation is available at helpdocs.php
* Script che permette di vedere, carica e scaricare documenti.
* I documenti sono poi visibili dal link "Documenti utili" presenti in home page
* @author Mario Marcello Verona <marcelloverona@gmail.com>
* @copyright 2007 Mario Marcello Verona
* @license http://www.gnu.org/licenses/gpl.html GNU Public License
include_once("./inc/conn.php");
include_once("./inc/layouts.php");
include_once("./inc/func.comuni.php");
// Dimensione del max_upload preso dal php.ini (è indicato in M)
$max_kb=(int) ini_get('post_max_size') * 1024 * 1024;
* Dato un nome di file restituisce il mime/type dall'estensione utilizzata
* @param string $filename
* @return string Il content-type per l'header
'rtf' => 'application/rtf',
'doc' => 'application/doc',
'pdf' => 'application/pdf',
'xls' => 'application/xls',
'odt' => 'application/odt',
'sxw' => 'application/sxw',
'zip' => 'application/x-zip-compressed',
'rar' => 'application/rar',
return $mime[$info['extension']];
* Dato un nome di file restituisce un frammento HTML per richiamare l'immagine associata.
* Le immagini sono presenti nella directory img/mime/
* @param string $filename
* @return string HTML image
return "<img src=\"img/mime/". $mime[strrchr($filename,'.')]. "\" alt=\"tipo: ". substr(strrchr($filename, '.'),1). "\" />";
// Se c'è inserisce il file
if(isset ($_GET['add']) && count($_FILES)> 0 && $_SESSION['user']['livello']> 1){
// il formato � corretto?
header("Location: ". $_SERVER['PHP_SELF']. "?ko=tipo_non_valido");
// Se non � troppo grande
if($_FILES['documento']['size']> $max_kb){
header("Location: ". $_SERVER['PHP_SELF']. "?ko=max_kb");
$NOME_FILE= preg_replace("'[^a-z0-9._-]+'i","_",$_FILES['documento']['name']);
echo $_FILES['documento']['tmp_name'] . " ---- ". _PATH_HELPDOCS. "/". $NOME_FILE;
header("Location: ". $_SERVER['PHP_SELF']. "?ok");
header("Location: ". $_SERVER['PHP_SELF']. "?ko=operazione_non_riuscita");
// CANCELLAZIONE DEL FILE:
if(isset ($_GET['del']) && $_SESSION['user']['livello']> 1){
// Open a known directory, and proceed to read its contents
while (($file = readdir($dh)) !== false) {
$docs[]= array('nome'=> $file,
if(!is_file($dir. "/". $docs[$id_file]['nome'])){
header("Location: ". $_SERVER['PHP_SELF']. "?ko=file_inesistente");
$op= unlink($dir. "/". $docs[$id_file]['nome']);
header("Location: ". $_SERVER['PHP_SELF']. "?eliminato");
header("Location: ". $_SERVER['PHP_SELF']. "?ko=operazione_non_riuscita");
else if(isset ($_GET['doc'])){
// Open a known directory, and proceed to read its contents
while (($file = readdir($dh)) !== false) {
$docs[]= array('nome'=> $file,
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename="'. $docs[$kk]['nome']. '"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. $docs[$kk]['size']);
@readfile(_PATH_HELPDOCS. "/". $docs[$kk]['nome']) OR die("<html><body OnLoad=\"javascript: alert('Nessun file trovato');history.back();\" bgcolor=\"#F0F0F0\"></body></html>");
$files= array("sty/tabelle.css","sty/questionari.css","sty/admin.css","js/confirm_delete.js");
echo "<div id=\"briciole\"><a href=\"index.php\">home</a> » documenti utili</div>\n";
echo "<h1>Documenti utili</h1>\n";
case "max_kb": echo "<p><strong class=\"var\">Attenzione!</strong> file troppo grande. E' possibile inviare file fino a ". ($max_kb/ 1024). " kb</p>\n";
case "tipo_non_valido": echo "<p><strong class=\"var\">Attenzione!</strong> formato non consentito. E' possibile inviare file dei seguenti formati: doc, xls, pdf, rtf, zip, txt, odt, sxw</p>\n";
case "operazione_non_riuscita": echo "<p><strong class=\"var\">Attenzione!</strong> operazione non riuscita. Qualora il tipo e le dimensioni del file (massimo ". ($max_kb/ 1024). " kb) fossero corrette ed il problema continuasse a verificarsi, contattare l'<a href=\"mailto:". _SYS_ADMIN_MAIL. "\">amministratore di sistema</a>.</p>\n";
elseif (isset ($_GET['ok'])){
echo "<p><strong class=\"verde\">File caricato correttamente.</strong></p>\n";
elseif(isset ($_GET['eliminato'])){
echo "<p><strong class=\"arancio\">File eliminato correttamente.</strong></p>\n";
// Open a known directory, and proceed to read its contents
while (($file = readdir($dh)) !== false) {
$docs[]= array('nome'=> $file,
echo "<div class=\"info\">
<p>In questa sezione sono presenti documenti con informazioni sul funzionamento dell'applicazione e sul database. <br />
Cliccare sul nome del documento per aprirlo,
oppure cliccare con il tasto destro e scegliere <em>Salva oggetto con nome</em>
per scaricare il documento sul proprio computer.<br />
if($_SESSION['user']['livello']> 1){
echo "<p><a href=\"javascript:;\" onclick=\"document.getElementById('add_record').style.display='';\">Aggiungi file</a></p>\n";
echo "<table summary=\"Tabella file help\" class=\"tab-report-dip\">\n";
<th class=\"lilla\" colspan=\"2\">Documento</th>
<th class=\"lilla\">Ultima modifica</th>
<th class=\"lilla\">Dimensione</th>",
($_SESSION['user']['livello']> 1) ?
"<th class=\"lilla\">Elimina</th>" : "",
// $dir_down=str_replace(FRONT_DOCROOT,'',_PATH_HELPDOCS);
for($i= 0;$i< count($docs);$i++ ){
<td><a href=\"?doc=$i\">". $docs[$i]['nome']. "</a></td>
<td>". date("d/m/Y H:i",$docs[$i]['data']). "</td>
<td>". round($docs[$i]['size']/ 1024,0). " Kb</td>",
($_SESSION['user']['livello']> 1) ?
"<td><a href=\"javascript:;\" onclick=\"confirm_delete_f(this,$i);\" >elimina</a></td>" : "",
<div id="add_record" style="display: none; top: 20%; left: 20%; width:500px; height: 230px; ">
<div style="text-align: right;">
<a href="javascript:;" onclick="document.getElementById(\'add_record\').style.display=\'none\';"> Chiudi X </a>
<div style="margin: 20px;">
<p>Sono consentiti i file dei seguenti formati: <strong>doc, xls, pdf, rtf, zip, txt, odt, sxw</strong></p>
<form method="post" enctype="multipart/form-data" action="'. $_SERVER['PHP_SELF']. '?add">
<label for="immagine"><strong>File da inserire</strong></label><br/>
<input type="file" name="documento" id="documento" size="60" />
<input name="invia" value=" Invia " type="submit" />
if($_SESSION['user']['livello']> 1){
|