, licence Creative Commons By-Nc-Sa
// http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
// Pour les utilisations commerciales, contactez l'auteur.
//////////////////////////////////////////////////////
// Configuration de l'accès
// $access: limitation de l'accès
// * "free" pour accès libre non limité,
// * "ip" pour limitation par adresse IP
// * "password" pour limitation par mot depasse
// * "ip or password" pour limitation par IP ou mot de passe
// * "ip and password" pour limitation par IP et mot de passe
$access = "ip or password";
// $ip: Adresse IP pour accès limité
$ip = "192.168.1.3";
// $password: Mot de passe pour accès limité
$password = "abcd";
// $links: Liens de bas de page (en HTML)
$links = 'BohwaZ.KD2.org';
// WEB_PATH: Chemin racine d'accès aux fichiers
define('WEB_PATH','./');
// Fin de la configuration (se reporter à la classe template pour la gestion des Urls propres)
////////////////////////////////////////////////
if(!function_exists("file_put_contents")) die("You need PHP5 and SimpleXML Module to use this script.");
class Manager
{
var $XmlFiles = array();
function ReadFile($file)
{
return file_get_contents($file);
}
function WriteFile($file,$contents)
{
if(@!file_put_contents($file,$contents)) die("Can't write to $file, check permissions.");
return TRUE;
}
function simplexml2array($xml) {
if (get_class($xml) == 'SimpleXMLElement') {
$attributes = $xml->attributes();
foreach($attributes as $k=>$v) {
if ($v) $a[$k] = utf8_decode((string) $v);
}
$x = $xml;
$xml = get_object_vars($xml);
}
if (is_array($xml)) {
if (count($xml) == 0) return utf8_decode((string) $x); // for CDATA
foreach($xml as $key=>$value) {
if(is_int($key) && isset($value['filename'])) $key = (string) $value['filename'];
if(is_int($key) && isset($value['name'])) $key = (string) $value['name'];
if($key == substr($value,0,1)) $r[] = $this->simplexml2array($value);
else $r[$key] = $this->simplexml2array($value);
}
#if (isset($a)) $r['@'] = $a; // Attributes
return $r;
}
return utf8_decode((string) $xml);
}
function ReadXML($dir)
{
if(!file_exists($dir."/.fotoo-datas.xml"))
return FALSE;
$content = $this->ReadFile($dir."/.fotoo-datas.xml");
$xml = simplexml_load_string($content);
$filename = (string) $xml->photo['filename'];
$xml = $this->simplexml2array($xml);
if(isset($xml['photo']['title']) && !empty($filename))
$xml['photo'] = array($filename=>$xml['photo']);
if(count($xml['photo']) > 0)
{
$newPhoto = array();
foreach($xml['photo'] as $id=>$val)
{
if(!is_array($val)) $val = array();
$f = $id;
$dir2 = $xml['directory'];
$val['thumb'] = "$dir2/.thumbs/$f";
$val['small'] = "$dir2/.smalls/$f";
$val['path'] = "$dir2/$f";
if(count($val['keyword']) == 1) $val['keyword'] = array($val['keyword']);
$newPhoto[$id] = $val;
}
$xml['photo'] = $newPhoto;
}
return $xml;
}
function WriteXML($dir,$title,$desc,$licence,$photos)
{
$xmlfile = '';
$xmlfile.= "\n\n ".$dir."\n";
if(!empty($title)) $xmlfile.= " ".htmlspecialchars(utf8_encode(stripslashes($title)))."\n";
if(!empty($desc)) $xmlfile.= " \n";
if(!empty($licence)) $xmlfile.= " \n";
$xmlfile.= "\n";
foreach($photos as $el)
{
$xmlfile.= " \n";
if(!empty($el['title'])) $xmlfile.= " ".htmlspecialchars(utf8_encode(stripslashes($el['title'])))."\n";
if(!empty($el['desc'])) $xmlfile.= " \n";
if(is_array($el['keywords']) && count($el['keywords']) > 0)
{
foreach($el['keywords'] as $keyword)
$xmlfile.= " ".htmlspecialchars(utf8_encode(stripslashes(trim($keyword))))."\n";
}
$xmlfile.= " \n\n";
}
$xmlfile.= "\n";
$this->WriteFile($dir."/.fotoo-datas.xml",$xmlfile);
return TRUE;
}
function delete($file,$dir)
{
@unlink($dir."/".$file);
@unlink($dir."/.thumbs/".$file);
@unlink($dir."/.smalls/".$file);
$xml = $this->ReadXML($dir);
if(count($xml['photo']) > 0) {
unset($xml['photo'][$file]);
$this->WriteXML($dir,$xml['title'],$xml['desc'],$xml['licence'],$xml['photo']);
}
return TRUE;
}
function IsFotooDir($dir)
{
if(!file_exists("$dir/.thumbs") || !is_dir("$dir/.thumbs")) return FALSE;
if(!file_exists("$dir/.smalls") || !is_dir("$dir/.smalls")) return FALSE;
#if(!file_exists("$dir/.fotoo-datas.xml")) return FALSE;
#return access & dir thumb
return TRUE;
}
function IsFotooFile($dir,$file)
{
if(!file_exists("$dir/.thumbs/$file") || !file_exists("$dir/.smalls/$file")) return FALSE;
return TRUE;
}
function ExtractExifDate($file)
{
$exif = exif_read_data($file,'FILE');
if($exif === FALSE) return FALSE;
$date = explode(" ",$exif['DateTime']);
$time = substr($date[1],0,5);
$date = explode(":",$date[0]);
$outDate = $date[2]."-".$date[1]."-".$date[0]." ".$time;
return $outDate;
}
function GetDirectory($dir)
{
$files = scandir($dir);
if($dir != ".") $dir2 = "$dir/";
$xml = $this->ReadXML($dir);
$outFiles = array();
$outDirs = array();
foreach($files as $f)
{
if(substr($f,0,1) == "." || substr($f,-4) == ".xml") continue;
$fArray = array();
$fArray['name'] = $f;
if(is_dir("$dir2$f")) {
if($this->IsFotooDir("$dir2$f")) {
$fArray['type'] = "photodir";
if($xml2 = $this->ReadXML($dir2.$f)) $fArray = array_merge($xml2,$fArray);
}
else $fArray['type'] = "normaldir";
$outDirs[] = $fArray;
}
else {
#if($size = @filesize("$dir2$f")) $fArray['size'] = $size;
if($this->IsFotooFile($dir,$f)) {
$fArray['type'] = "photo";
if($xml && isset($xml['photo'][$f])) $fArray = array_merge($xml['photo'][$f],$fArray);
else $fArray['thumb'] = $dir2.".thumbs/".$f;
$fArray['date'] = $this->ExtractExifDate("$dir2$f");
}
else $fArray['type'] = "file";
$outFiles[] = $fArray;
}
}
sort($outDirs);
sort($outFiles);
$out = array_merge($outDirs,$outFiles);
return $out;
}
function ScanForXml($dir)
{
$files = scandir($dir);
if($dir != ".") $dir2 = $dir."/";
if(file_exists($dir2.".fotoo-datas.xml")) $this->XmlFiles[] = $dir;
foreach($files as $f)
{
if(substr($f,0,1) == ".") continue;
if(is_dir($dir2.$f)) $this->ScanForXml($dir2.$f);
}
}
function BuildKeywords()
{
$this->ScanForXml(".");
$keywords = array();
foreach($this->XmlFiles as $d)
{
$xml = $this->ReadXML($d);
foreach($xml['photo'] as $id=>$val)
{
if(is_array($val['keyword'])) {
foreach($val['keyword'] as $k) {
$keywords[$k][] = array("path"=>$xml['directory'].'/'.$id,"thumb"=>$xml['directory'].'/.thumbs/'.$id,
"title"=> empty($val['title']) ? $id : $val['title']);
}
}
}
}
$xmlfile = '';
$xmlfile.= "\n\n";
foreach($keywords as $word=>$files)
{
$xmlfile.= " \n";
foreach($files as $f)
$xmlfile.= " \n ".utf8_encode($f['title'])."\n ".$f['thumb']."\n ".$f['path']."\n \n";
$xmlfile.= " \n\n";
}
$xmlfile.= "\n";
$this->WriteFile('fotoo-keywords.xml',$xmlfile);
return TRUE;
}
function LoadKeywords()
{
if(!file_exists("fotoo-keywords.xml")) return FALSE;
$content = $this->ReadFile("fotoo-keywords.xml");
$xml = simplexml_load_string($content);
$xml = $this->simplexml2array($xml);
$keywords = array();
foreach($xml['keyword'] as $keyword=>$value)
{
if(array_key_exists('title',$value['file']))
$value['file'] = array($value['file']);
foreach($value['file'] as $f)
{
$keywords[utf8_decode($keyword)][] = array("title"=>$f['title'],"path"=>$f['path'],"thumb"=>$f['thumb']);
}
}
return $keywords;
}
function CheckAccess()
{
$mode = $GLOBALS['access'];
if($mode == "free") return TRUE;
$mode = explode(" ",$mode);
if(in_array("ip",$mode)) $ip = $_SERVER['REMOTE_ADDR'];
if(in_array("or",$mode)) $or = TRUE;
if(in_array("password",$mode)) { session_start(); $password = $_SESSION['fotooMMsession']; $passEnabled = TRUE; }
if(!$ip && !$passEnabled) return TRUE;
$truePassword = md5($GLOBALS['password']);
$trueIP = $GLOBALS['ip'];
if(($ip && $or && $passEnabled) && (($ip == $trueIP) || ($password == $truePassword))) return TRUE;
elseif(($ip && $passEnabled) && (($ip == $trueIP) && ($password == $truePassword))) return TRUE;
elseif($ip && $ip == $trueIP) return TRUE;
elseif($passEnabled && $password == $truePassword) return TRUE;
return FALSE;
}
function Login($password)
{
$truePassword = $GLOBALS['password'];
if($password != $truePassword) return FALSE;
session_set_cookie_params(0,'/');
session_start();
$_SESSION['fotooMMsession'] = md5($password);
return TRUE;
}
function Logout()
{
session_start();
$_SESSION = array();
session_unset();
session_destroy();
return TRUE;
}
}
class Template
{
// URLS Propres (commentées par défaut)
//var $url = "MODE/PATH";
//var $errUrl = "MODE/PATH?err=ERROR";
// URLs moches utilisées par défaut
var $url = "mini-manager.php?mode=MODE&dir=PATH";
var $errUrl = "mini-manager.php?mode=MODE&dir=PATH&err=ERROR";
var $adminUrl = "mini-manager.php5?mode=MODE&dir=PATH";
function URL($path,$mode,$err="")
{
if(empty($err)) $url = $this->url;
else {
$url = $this->errUrl;
$url = str_replace("ERROR",urlencode($err),$url);
}
if($mode == "admin") $url = $this->adminUrl;
$url = str_replace("MODE",$mode,$url);
$url = str_replace("PATH",str_replace("%2F","/",($mode == "admin" ? $path : urlencode($path))),$url);
return WEB_PATH.$url;
}
function Header($mode,$title,$dir="")
{
$out[] = '';
$out[] = '';
$out[] = '
';
$out[] = ' '.$title.'';
$out[] = ' ';
$out[] = ' ';
$out[] = '';
$out[] = '';
if($mode == "keyword") {
$path = '