// versione 20060912 - aggiunto n00
error_reporting(501);
$controllochk=true;
$notifypm=true;
$TQUALITY=80;
//info per log AG
$typeAG="- LOGAGMATRIX 1H logger name:";
$userip=$_SERVER['REMOTE_ADDR'];
$serverip=$userip;
$userts=date("Y-m-d\TH:i:sP");
$operationAG="upload";
$fileAG="";
$pathAG="";
$objidAG=uniqid();
$uniqidold="";
$noteAG="";
$serviceip="multimedia-fe";
$md5AG=md5_file($_FILES['file']['tmp_name']);
$delimAG=chr(12);
$hostnameAG=exec('hostname');
$filenameAG="MULTIMEDIA.".date("Y.m.d").".".$hostnameAG."_AVATAR"."."."0001";
$PATH_LOG_AG="/LOGAG";
// ########## Path fisici e valori di default ##########
$PATH_MEDIA="/media/fileroot"; // esercizio
$URL_PMFOTO="http://muco.pm.local:8080/pm/multimediac6/setNumeroFoto.jsp";
$URL_PMAV="http://muco.pm.local:8080/pm/multimediac6/setAttributoAudioVideo.jsp";
$community="mu";
$profilo="f00";
$mustcomp=true;
$tipofile=0; //foto
global $NOME_HOST;
$NOME_HOST="multimediatlantide.alice.it";
global $nickname;
// ######### Classificazione degli errori #########
$NOERR="200 OK";
// Errori nei parametri del form
$ERR_NONICK="401 Il campo nickname è vuoto";
$ERR_NOVALIDNICK="402 Il nickname inviato è troppo corto";
$ERR_NOVALIDTYPE="403 l'attribute_name inviato non è in un formato valido";
// Errori nel checksum
$ERR_CHK="411 Non è stato inviata la stringa per il controllo MD5";
$ERR_MD5="412 Errore nella verifica del checksum del nickname";
// Errori nell'invio del file'
$ERR_NO_FILE="421 Il file non è stato inviato correttamente";
$ERR_NO_EXT="422 Manca l'estensione per il file inviato'";
$ERR_EXT="423 L'estensione del file non è tra quelle ammesse in relazione al tipo di file'";
$ERR_DIM="424 La dimensione del file inviato eccede i limiti impostati per tale tipo di contenuto";
// Errori nella comunicazione con il Profile Manager
$ERR_PM1="501 Errore di collegamento con il Profile Manager";
$ERR_PM2="502 Errore di risposta dal Profile Manager";
$ERR_PM3="503 Errore nell'esecuzione delle query sul Profile Manager";
// Errori nel caricamento dei files
$ERR_UPLOAD="511 Errore nel caricamento del file e/o nella creazione dei thumbnails";
//Dimensioni massime dei file in bytes( 0 = immagini, 1 = audio , 2 = video )
$MAXDIM[0]=2097152;
$MAXDIM[1]=307200;
$MAXDIM[2]=2048000;
// numero di estensioni differenti ( 0,x = immagini, 1,x = audio , 2,x = video )
global $NUMEXT;
$NUMEXT[0]=4;
$NUMEXT[1]=12;
$NUMEXT[2]=10;
// Estensioni permesse per i vari files ( 0,x = immagini, 1,x = audio , 2,x = video )
global $ESTENSIONI;
$ESTENSIONI[0][0]="jpg";
$ESTENSIONI[0][1]="jpeg";
$ESTENSIONI[0][2]="gif";
$ESTENSIONI[0][3]="png";
$ESTENSIONI[1][0]="aif";
$ESTENSIONI[1][1]="aifc";
$ESTENSIONI[1][2]="aiff";
$ESTENSIONI[1][3]="au";
$ESTENSIONI[1][4]="ra";
$ESTENSIONI[1][5]="ram";
$ESTENSIONI[1][6]="snd";
$ESTENSIONI[1][7]="wav";
$ESTENSIONI[1][8]="mp3";
$ESTENSIONI[1][9]="m3u";
$ESTENSIONI[1][10]="rm";
$ESTENSIONI[1][11]="rmm";
$ESTENSIONI[2][0]="m1v";
$ESTENSIONI[2][1]="mp2";
$ESTENSIONI[2][2]="mpa";
$ESTENSIONI[2][3]="mpe";
$ESTENSIONI[2][4]="mpeg";
$ESTENSIONI[2][5]="mpg";
$ESTENSIONI[2][6]="mov";
$ESTENSIONI[2][7]="qt";
$ESTENSIONI[2][8]="avi";
$ESTENSIONI[2][9]="3gp";
$errore=$NOERR;
// Caricamento dei dati arrivati tramite il post
$nickname = $_POST['nickname'];
$chk = $_POST['chk'];
if ($_POST['community'] != "") $community = strtolower($_POST['community']);
if (strtolower($_POST['mustcomp'] == "no")) $mustcomp = false;
if (strtolower($_POST['notifypm'] == "no")) $notifypm = false;
if ($_POST['attribute_name'] != "") $profilo = strtolower($_POST['attribute_name']);
// ########## Verifica dati inseriti ##########
// Verifica Nickname non nullo e di lunghezza >= 2
If ($nickname == ""){
$errore = $ERR_NONICK;
} elseif(strlen($nickname)<2){
$errore = $ERR_NOVALIDNICK;
}
// Verifica del campo attribute name
if ($errore == $NOERR) {
if(strlen($profilo)==3 && (substr($profilo,0,1)=="f" || substr($profilo,0,1)=="a" || substr($profilo,0,1)=="v") && is_numeric(substr($profilo,-2))){
if(substr($profilo,0,1)=="a"){
$tipofile=1;
} elseif(substr($profilo,0,1)=="v"){
$tipofile=2;
}
} else $errore = $ERR_NOVALIDTYPE;
}
// Controllo del checksum MD5 del nickname
if ($errore == $NOERR && $controllochk) {
if($chk == ""){
$errore=$ERR_CHK; // decommentare in seguito
}else{
$salt=substr($chk,0,8);
$contr= (md5($nickname.$salt,false));
if ($salt.$contr != $chk) $errore=$ERR_MD5;
}
}
// Controllo il file che è stato inviato
if ($errore == $NOERR) {
if (!is_file($_FILES['file']['tmp_name'])) {
$errore = $ERR_NO_FILE;
} else {
$estensione = get_extension($_FILES['file']['name']);
if($estensione == ""){
$errore=$ERR_NO_EXT;
} else {
$trovato = false;
for ($ne = 0; $ne < $NUMEXT[$tipofile]; $ne++ ) {
if ($estensione == $ESTENSIONI[$tipofile][$ne]) {
$trovato = true;
}
}
if ($trovato == false) {
$errore = $ERR_EXT;
} else {
if ($_FILES['file']['size'] == 0 || $_FILES['file']['size'] > $MAXDIM[$tipofile]) {
$errore = $ERR_DIM;
}
}
}
}
}
// Inizio a processare i files inviati
if ($errore == $NOERR) {
// Creo la struttura di directory ed il percorso del file
$outpath = "$PATH_MEDIA/$community";
if(!is_dir($outpath)) mkdir($outpath, 0777);
$outpath = $outpath . "/" . $nickname{0};
if(!is_dir($outpath)) mkdir($outpath, 0777);
$outpath = $outpath . "/" . substr($nickname , -2);
if(!is_dir($outpath)) mkdir($outpath, 0777);
$outpath = $outpath . "/" . $nickname;
$pathAG = $outpath."/";
if(!is_dir($outpath)) mkdir($outpath, 0777);
$outpath = $outpath . "/" . $nickname . ",";
$outfile = $outpath . $profilo . "." . $estensione;
// Valorizzo uniqidold
if (!$insert) {
if (is_file("${outpath}${profilo}.html")) {
$uniqidold = getObjid("${outpath}${profilo}.html");
}
}
// Copia del file
if (copy($_FILES[file][tmp_name] , $outfile ) ) {
//scrivo il log AG
$fileAG=$nickname.",".$profilo.".".$estensione;
$noteAG="http://$NOME_HOST/contenuti/$fileAG";
$recordAG=$typeAG.$delimAG.$userip.$delimAG.$userts.$delimAG.$nickname.$delimAG.$operationAG.$delimAG.$fileAG.$delimAG.$pathAG.$delimAG.$objidAG.$delimAG.$noteAG.$delimAG.$serviceip;
//logAG per datacenter Pomezia
//error_log("$recordAG\n", 3, "$PATH_LOG_AG/$filenameAG");
//logAG per datacenter Rozzano
$ret_sys = syslog(LOG_INFO, "$recordAG\n");
//if ($ret_sys)
//{
// print "scrittura su syslog: ok
";
//}
// Creazione dei ThumbNail
if ($tipofile == 0) {
//creaThumb($profilo,$outpath,$estensione,$TQUALITY)
$resp_convert = creaThumb($profilo,$outpath,$estensione,$TQUALITY);
if($resp_convert != ""){
$errore = "499 ERROR in thumb-creation: ".$resp_convert;
}
}
// Creo il file HTML
// Inizio a processare i files inviati
if ($errore == $NOERR) {
make_html("$outpath$profilo.html","http://$NOME_HOST/contenuti/$nickname,$profilo.$estensione", $objidAG);
}else{
// da gestire la cancellazione del file
}
// Cancella eventuali versioni obsolete del file
for ($ne = 0; $ne < $NUMEXT[$tipofile]; $ne++ ) {
if ($ESTENSIONI[$tipofile][$ne] != $estensione and is_file($outpath . $profilo . "." . $ESTENSIONI[$tipofile][$ne])) {
unlink($outpath . $profilo . "." . $ESTENSIONI[$tipofile][$ne]);
}
}
} else {
$errore = $ERR_UPLOAD;
}
if($mustcomp) compatta_foto($outpath);
}
// comunicazione con il profile manager
if ($errore == $NOERR && $notifypm){
$nick=urlencode($nickname);
$community=strtoupper($community);
if($tipofile == 0){ //foto
$numfoto=conta_foto($outpath);
//encodo i parametri per il pm
$userts = urlencode($userts);
error_log("DEBUG: call url: $URL_PMFOTO?nickname=$nick&comunita=$community&numerofoto=$numfoto&userip=$userip&userts=$userts&serviceip=$serviceip&md5=$md5AG&action=$operationAG&uniqid=$objidAG&serverip=$serverip&uniqidold=$uniqidold
");
$sFile = file_get_contents("$URL_PMFOTO?nickname=$nick&comunita=$community&numerofoto=$numfoto&userip=$userip&userts=$userts&serviceip=$serviceip&md5=$md5AG&action=$operationAG&uniqid=$objidAG&serverip=$serverip&uniqidold=$uniqidold");
error_log("DEBUG: response from PM: $sFile
");
}else{ //audio o video
$filename = urlencode("/contenuti/$nickname,$profilo.$estensione");
if($tipofile == 1){
$nomeattributo="audio";
}else{
$nomeattributo="video";
}
$sFile = file_get_contents("$URL_PMAV?nickname=$nick&comunita=$community&nomeattributo=$nomeattributo&valoreattributo=$filename&cmd=set");
error_log("$URL_PMAV?nickname=$nick&comunita=$community&nomeattributo=$nomeattributo&valoreattributo=$filename&cmd=set
");
}
if($sFile){
if(strlen($sFile)>2){
if(substr($sFile,0,3)!="200") $errore = $ERR_PM3;
}else{
$errore = $ERR_PM2;
}
}else{
$errore = $ERR_PM1;
}
}
// tocca per sicurezza
$outpathd = "$PATH_MEDIA/$community" . "/" . $nickname{0} . "/" . substr($nickname , -2) . "/" . $nickname;
exec("touch $outpathd/*");
// Stampa a video del risultato dell'operazione
//if ($errore != $NOERR){
error_log("UPLOAD: ".$nickname." ".$errore);
// }
echo $errore;
exit;
//prende larghezza foto
function getLarghezzaFoto($outfile) {
$cmd = "identify $outfile";
$rr = exec($cmd);
if (preg_match("/\s(\d+)x(\d+)\s/", $rr, $matches))
{
$larghezza = $matches[1];
}
return($larghezza);
}
function getAltezzaFoto($outfile) {
#questo comando ritorna: /media/fileroot/mu/o/75/ortona75/ortona75,t04.jpg JPEG 160x141 160x141+0+0 DirectClass 6.1kb
$cmd = "identify $outfile";
$rr = exec($cmd);
if (preg_match("/\s(\d+)x(\d+)\s/", $rr, $matches))
{
$altezza = $matches[2];
}
return($altezza);
}
// creazione dei thumbNail
function creaThumb($profilo,$outpath,$estensione,$TQUALITY){
$outfile = $outpath . $profilo . "." . $estensione;
$numero=substr($profilo , -2);
#immagine n normal
$thumbfile = $outpath . "n" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " " . $outfile . "[0] " . $thumbfile ." 2>&1";
$rr= exec($esegui);
#immagine i icona 64x64
$thumbfile = $outpath . "i" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 64x64 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
#immagine c cropped 32x32
$thumbfile = $outpath . "c" . $numero . ".jpg";
$esegui="convert " . $outfile . "[0] -colorspace RGB -quality " . $TQUALITY . " -resize x64 -resize \"64x<\" -resize 50% -gravity center -crop 32x32+0+0 +repage " . $thumbfile ;
$rr= $rr . exec($esegui,$aaa,$bbb);
#immagine t thumbnail 160x160
$thumbfile = $outpath . "t" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 160x160 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
#immagine b big 600x600
$thumbfile = $outpath . "b" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 600x600 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
#immagine d 162 larghezza/altezza
$thumbfile = $outpath . "d" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 162x162 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
#immagine e 240 larghezza
$larghezza = getLarghezzaFoto($outfile);
if ($larghezza <= 240)
{
$thumbfile = $outpath . "e" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " " . $outfile . "[0] " . $thumbfile ." 2>&1";
$rr= $rr . exec($esegui);
}
else
{
$thumbfile = $outpath . "e" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 240x10000 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
}
#immagine g cropped 64x64
$thumbfile = $outpath . "g" . $numero . ".jpg";
$esegui="convert " . $outfile . "[0] -colorspace RGB -quality " . $TQUALITY . " -resize x128 -resize \"128x<\" -resize 50% -gravity center -crop 64x64+0+0 +repage " . $thumbfile ;
$rr= $rr . exec($esegui,$aaa,$bbb);
#immagine o 200 larghezza 175 altezza
$larghezza = getLarghezzaFoto($outfile);
$altezza = getAltezzaFoto($outfile);
//echo "DEBUG: larghezza: $larghezza, altezza: $altezza
";
if (($larghezza >= 200) && ($altezza <= 175))
{
$thumbfile = $outpath . "o" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 200x175 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
}
elseif ($larghezza >= 200)
{
$thumbfile = $outpath . "o" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 200x10000 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
}
elseif ($altezza <= 175)
{
$thumbfile = $outpath . "o" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " -geometry 175x10000 " . $outfile . "[0] " . $thumbfile;
$rr= $rr . exec($esegui);
}
else
{
$thumbfile = $outpath . "o" . $numero . ".jpg";
$esegui="convert -colorspace RGB -quality " . $TQUALITY . " " . $outfile . "[0] " . $thumbfile ." 2>&1";
$rr= $rr . exec($esegui);
}
return $rr;
}
// funzione per estrarre l'estensione del nome del file
function get_extension($filename){
$estensione = "";
if(strlen($filename)>4 && substr(substr($filename,-5),0,1) == ".") $estensione=substr($filename,-4);
if(strlen($filename)>3 && substr(substr($filename,-4),0,1) == ".") $estensione=substr($filename,-3);
if(strlen($filename)>2 && substr(substr($filename,-3),0,1) == ".") $estensione=substr($filename,-2);
return strtolower($estensione);
}
// funzione per la "compattazione" delle foto
function compatta_foto($uri) {
$maxfoto = 6;
$firstempty = 0;
global $NUMEXT;
global $ESTENSIONI;
global $NOME_HOST;
global $nickname;
for ($co=0; $co<($maxfoto); $co++) {
if (is_file($uri."f0".$co.".html")){
if ($firstempty < $co){ //sposta
unlink($uri."f0".$co.".html");
for ($ne = 0; $ne < $NUMEXT[0]; $ne++ ) {
if(is_file($uri."f0".$co.".".$ESTENSIONI[0][$ne])){
rename($uri."f0".$co.".".$ESTENSIONI[0][$ne],$uri."f0".$firstempty.".".$ESTENSIONI[0][$ne]);
make_html($uri."f0".$firstempty.".html","http://$NOME_HOST/contenuti/$nickname,f0$firstempty.".$ESTENSIONI[0][$ne]);
}
}
if(is_file($uri."i0".$co.".jpg")) rename($uri."i0".$co.".jpg",$uri."i0".$firstempty.".jpg");
if(is_file($uri."t0".$co.".jpg")) rename($uri."t0".$co.".jpg",$uri."t0".$firstempty.".jpg");
if(is_file($uri."c0".$co.".jpg")) rename($uri."c0".$co.".jpg",$uri."c0".$firstempty.".jpg");
if(is_file($uri."n0".$co.".jpg")) rename($uri."n0".$co.".jpg",$uri."n0".$firstempty.".jpg");
}
$firstempty++;
}else{
// da gestire la rimozione di sporcizia (non esiste l'html ma magari ci sono le imgs)
}
}
return true;
}
//funzione per creare i files html di reindirizzamento
function make_html($file,$url,$objidAG){
$fileout=fopen ($file ,"w");
if($fileout) {
fwrite($fileout,"\n");
fwrite($fileout,"