<?php
$max_caracteres = 20;
function traite_nom_fichier($nom) {
	global $max_caracteres;
	$nom=stripslashes($nom);
	$nom=str_replace("'","",$nom);
	$nom=str_replace("\"","",$nom);
	$nom=str_replace("\"","",$nom);
	$nom=str_replace("&","",$nom);
	$nom=str_replace(",","",$nom);
	$nom=str_replace(";","",$nom);
	$nom=str_replace("/","",$nom);
	$nom=str_replace("\\","",$nom);
	$nom=str_replace("`","",$nom);
	$nom=str_replace("<","",$nom);
	$nom=str_replace(">","",$nom);
	$nom=str_replace(" ","_",$nom);
	$nom=str_replace(":","",$nom);
	$nom=str_replace("*","",$nom);
	$nom=str_replace("|","",$nom);
	$nom=str_replace("?","",$nom);
	$nom=str_replace("é","e",$nom);
	$nom=str_replace("è","e",$nom);
	$nom=str_replace("ç","c",$nom);
	$nom=str_replace("@","",$nom);
	$nom=str_replace("â","a",$nom);
	$nom=str_replace("ê","e",$nom);
	$nom=str_replace("î","i",$nom);
	$nom=str_replace("ô","o",$nom);
	$nom=str_replace("û","u",$nom);
	$nom=str_replace("ù","u",$nom);
	$nom=str_replace("à","a",$nom);
	$nom=str_replace("!","",$nom);
	$nom=str_replace("§","",$nom);
	$nom=str_replace("+","",$nom);
	$nom=str_replace("^","",$nom);
	$nom=str_replace("(","",$nom);
	$nom=str_replace(")","",$nom);
	$nom=str_replace("#","",$nom);
	$nom=str_replace("=","",$nom);
	$nom=str_replace("$","",$nom);
	$nom=str_replace("%","",$nom);
	$nom=str_replace("ä","ae",$nom);
	$nom=str_replace("Ä","Ae",$nom);
	$nom=str_replace("ö","oe",$nom);
	$nom=str_replace("Ö","Oe",$nom);
	$nom=str_replace("ü","ue",$nom);
	$nom=str_replace("Ü","Ue",$nom);
	$nom=str_replace("ß","ss",$nom);
	if (strlen($nom)> $max_caracteres) {
		$ext = substr($nom, (strrpos($nom,'.') +  1));
		$nom = substr ($nom,0,$max_caracteres-4);
		$nom = $nom.".".$ext;
	}
	return $nom;
}

function makeDir($rep, $nomdir) {
	global $prefix, $db;
	$rep=stripslashes($rep);
	if($nomdir=="") {
		return _GALFILECREATED;
	} else {
		if(!is_dir("$rep/$nomdir")) {
			umask(000);
			$ret = @mkdir("$rep/$nomdir",0777);
			if ($ret) {
				return "OK";
			} else {
				return _GALCREATECATERROR;
			}
		} else {
			$sql = "SELECT gallid FROM $prefix"._album_categories." WHERE galloc='$nomdir'";
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			if($row[gallid]) {
				return "Category already exists.";
			} else {
				return "OK";
			}
		}
	}
}

function UploadFile($rep, $userfile, $userfile_name, $userfile_size) {
	global $galleryvar;
	$rep = stripslashes($rep);
	$destination = $rep;
	if ($userfile_size == 0 || ($userfile_size > $galleryvar['maxSize'] && $galleryvar['limitSize'] == 1)) {
		return _GALUPLOADERROR;
	}
	if ($userfile == "none") {
		return _GALUPLOADERROR2;
	}
	if (file_exists("$destination/$userfile_name")) {
		return _GALUPLOADERROR3;
	}
	if ($userfile != "none" && $userfile_size != 0) {
		$userfile_name = traite_nom_fichier($userfile_name);
		if (!@move_uploaded_file($userfile, "$destination/$userfile_name")) {
			return _GALUPLOADERROR4;
		} else {
			@chmod("$destination/$userfile_name",0664);
			return "OK";
		}
	}
}
?>