$_SESSION['temp_cleaned']+1800){ # clean temp once in a session $zips=_glob($_SESSION['temp_folder'],'.zip'); foreach($zips as $zipfile){ if (time()>filectime($zipfile)+1800){unlink($zipfile);} } $_SESSION['temp_cleaned']=time(); } # Check R/W rights for system folders if (!is_writable($_SESSION['private_folder'])){echo '

'.e('Private folder is not writable',false).'

';} if (!is_readable($_SESSION['private_folder'])){echo '

'.e('Private folder is not readable',false).'

';} if (!is_writable($_SESSION['temp_folder'])){echo '

'.e('Temp folder is not writable',false).'

';} if (!is_readable($_SESSION['temp_folder'])){echo '

'.e('Temp folder is not readable',false).'

';} if (!is_readable($_SESSION['private_folder'].'trees')){$message.='
'.e('Problem accessing tree folder: not readable',false).'
';} if (!is_writable($_SESSION['private_folder'].'trees')){$message.='
'.e('Problem accessing tree/folder file: not writable',false).'
';} if (!empty($_SESSION['upload_user_path'])&&!is_readable($_SESSION['upload_root_path'].$_SESSION['upload_user_path'].$_SESSION['current_path'])){$message.='
'.e('Problem accessing ',false).$_SESSION['current_path'].e(': folder not readable',false).'
';} if (!empty($_SESSION['upload_user_path'])&&!is_writable($_SESSION['upload_root_path'].$_SESSION['upload_user_path'].$_SESSION['current_path'])){$message.='
'.e('Problem accessing ',false).$_SESSION['current_path'].e(': folder not writable',false).'
';} # Check necessary libs if(!$disable_non_installed_libs_warning){ if (!$_SESSION['zip']){$message.='
ZipArchive '.e('is not installed on this server',false).' '.e('More info',false).'
';} if (!$_SESSION['GD']){$message.='
GD '.e('is not installed on this server',false).' '.e('More info',false).'
';} if (!$_SESSION['curl']){$message.='
Curl '.e('is not installed on this server',false).' '.e('More info',false).'
';} } # Check files if (!is_file('thumbs/'.$_SESSION['upload_root_path'].'.htaccess')){file_put_contents('thumbs/'.$_SESSION['upload_root_path'].'.htaccess', 'deny from all');} if (!is_file('thumbs/'.$_SESSION['upload_root_path'].'index.html')){file_put_contents('thumbs/'.$_SESSION['upload_root_path'].'index.html',' ');} if (!is_file($_SESSION['temp_folder'].'.htaccess')){file_put_contents($_SESSION['temp_folder'].'htaccess', 'deny from all');} if (!is_file($_SESSION['upload_root_path'].'index.html')){ file_put_contents($_SESSION['upload_root_path'].'index.html',' '); } if (!is_file('thumbs/.htaccess')){file_put_contents('thumbs/.htaccess', 'deny from all');} if (!is_file('thumbs/index.html')){file_put_contents('thumbs/index.html',' ');} if (!empty($_SESSION['upload_user_path'])&&!is_file('thumbs/'.$_SESSION['upload_root_path'].$_SESSION['upload_user_path'].'.htaccess')){file_put_contents('thumbs/'.$_SESSION['upload_root_path'].$_SESSION['upload_user_path'].'.htaccess', 'deny from all');} if (!empty($_SESSION['upload_user_path'])&&!is_file('thumbs/'.$_SESSION['upload_root_path'].$_SESSION['upload_user_path'].'index.html')){file_put_contents('thumbs/'.$_SESSION['upload_root_path'].$_SESSION['upload_user_path'].'index.html',' ');} if (!is_file($_SESSION['private_folder'].'.htaccess')){file_put_contents($_SESSION['private_folder'].'/.htaccess', 'deny from all');} if (!is_file($_SESSION['folder_share_file'])){save_folder_share(array());} if (!is_file($_SESSION['private_folder'].'/salt.php')){ file_put_contents($_SESSION['private_folder'].'/salt.php',''); } else{include($_SESSION['private_folder'].'/salt.php');} if (!is_file($_SESSION['id_file'])){$ids=array();store($ids);} if (!is_file($_SESSION['stats_file'])){save($_SESSION['stats_file'], array());} if (!is_file($_SESSION['upload_root_path'].'.htaccess')){file_put_contents($_SESSION['upload_root_path'].'.htaccess', 'deny from all');} if (!is_file($_SESSION['users_rights_file'])){save_users_rights(array());} else{$users_rights=load_users_rights();} if (!is_file($_SESSION['profiles_rights_file'])){ save($_SESSION['profiles_rights_file'],array()); } if (is_file($_SESSION['id_file'])&&!is_readable($_SESSION['id_file'])){$message.='
'.e('Problem accessing ID file: not readable',false).'
';} if (is_file($_SESSION['id_file'])&&!is_writable($_SESSION['id_file'])){$message.='
'.e('Problem accessing ID file: not writable',false).'
';} if (is_file($_SESSION['stats_file'])&&!is_readable($_SESSION['stats_file'])){$message.='
'.e('Problem accessing stats file: not readable',false).'
';} if (is_file($_SESSION['stats_file'])&&!is_writable($_SESSION['stats_file'])){$message.='
'.e('Problem accessing stats file: not writable',false).'
';} # Libs configuration # Files to echo in browser (secured) $behaviour['FILES_TO_ECHO']=array('nfo','m3u','txt','js','html','php','SPHP','htm','shtml','shtm','css'); # Files to send to browser directly $behaviour['FILES_TO_RETURN']=array('md','jpg','jpeg','gif','png','mp3','mp4','svg'); $auto_dropzone['destination_filepath']=$_SESSION['current_path'].'/'; $auto_thumb['default_width']='64'; $auto_thumb['default_height']='64'; $auto_thumb['dont_try_to_resize_thumbs_files']=true; # CONSTANTS & GLOBALS define('THEME_PATH','templates/'.$_SESSION['theme'].'/'); $ACTIONS=array('users page','add user','delete user','change user status','change folder size','change status rights','change passes','markdown editor','regen ID base','acces logfile','upload'); $RIGHTS=load($_SESSION['profiles_rights_file']); if (empty($RIGHTS)){ #default profiles if not configured $PROFILES=array('admin','user'); $RIGHTS['admin']=array('add user'=>1,'delete user'=>1,'change folder size'=>1,'markdown editor'=>1,'regen ID base'=>1,'access logfile'=>1,'upload'=>1); $RIGHTS['user']=array('markdown editor'=>1,'upload'=>1); save($_SESSION['profiles_rights_file'],$RIGHTS); }else{$PROFILES=array_filter(array_keys($RIGHTS));} include('core/templates.php'); ################################################# # Load IDS ################################################# if (is_user_connected()&&$check_ID_base_on_page_load){ $ids=updateIDs(); }else{$ids=unstore();} ################################################# ################################################# # Load current path tree ################################################# if (is_user_connected()){ $tree=tree($_SESSION['upload_root_path'].$_SESSION['upload_user_path'].$_SESSION['current_path'],false,false,$allow_folder_size_stat); } ################################################# ################################################# # Functions ################################################# # Data save/load & files ################################################# function load($file){return (file_exists($file) ? unserialize(gzinflate(base64_decode(substr(file_get_contents($file),9,-strlen(6))))) : array() );} function save($file,$data){return file_put_contents($file, '');} function store($ids=null){if (!$ids){return false;}natcasesort($ids);return save($_SESSION['id_file'],$ids);} function unstore(){return array_filter(load($_SESSION['id_file']));} function save_folder_share($array=null){return save($_SESSION['folder_share_file'],$array);} function load_folder_share(){return load($_SESSION['folder_share_file']);} function save_users_rights($array=null){return save($_SESSION['users_rights_file'],$array);} function load_users_rights(){return load($_SESSION['users_rights_file']);} # Delete a file or a folder and apply changes in ids file function delete_file_or_folder($id=null,$ids=null,$tree=array()){ global $ids,$tree; if (empty($ids)){$ids=unstore();} if (empty($id)){return false;} if (empty($tree)){tree(null,$_SESSION['login'],false,false,$tree);} $f=id2file($id); if(is_file($f)){ # delete file & thumb unlink($f); $thumbfilename=get_thumbs_name($f); if (is_file($thumbfilename)){unlink($thumbfilename);} unset($ids[$id]); store($ids); return remove_branch($f,$id); }else if (is_dir($f)){ $fthumbs=explode('/',$f); unset($fthumbs[0]);$fthumbs=implode('/',$fthumbs); # delete dir rrmdir($f); rrmdir('thumbs/'.$fthumbs); # remove all vanished sub files & folders from id file foreach($ids as $id=>$path){ if (strpos($path, $f.'/')!==false){unset($ids[$id]);} } store($ids); return remove_branch($f,$id); } return $tree; } # New folder in the current path function new_folder($folder=null){ if (!$folder){return false;} $thumbs_path='thumbs/'.$_SESSION['upload_user_path'].addslash_if_needed($_SESSION['current_path']); $path=$_SESSION['upload_root_path'].$_SESSION['upload_user_path'].addslash_if_needed($_SESSION['current_path']); $complete_path=$path.$folder; $complete_thumbs=$thumbs_path.$folder; if (is_dir($complete_path)&&is_dir($complete_thumbs)){ # Folder already exists, rename $folder=rename_item($folder,$path); $complete_path=$path.$folder; $complete_thumbs=$thumbs_path.$folder; }else if(!is_dir($complete_path)&&is_dir($complete_thumbs)){ rrmdir($complete_thumbs); } mkdir($complete_path, 0744, true); mkdir($complete_thumbs, 0744, true); return addID($complete_path); } # checks if dir is empty function is_empty_dir($src){ # form https://openclassrooms.com/forum/sujet/savoir-si-un-dossier-est-vide-39930 if (!is_dir($src)){return 'no such dir';} $h = opendir($src); while (($o = readdir($h)) !== FALSE){ if (($o != '.') and ($o != '..')){$c++;} } closedir($h); if($c==0){return true;}else {return false; } } # store all client access to a file function store_access_stat($file=null,$id=null){ if (!$file||!$id){return false;} $host=$ref='∅'; if (isset($_SERVER['REMOTE_HOST'])){$host=$_SERVER['REMOTE_HOST'];} if (isset($_SERVER['HTTP_REFERER'])){$ref=$_SERVER['HTTP_REFERER'];} $data=array( 'ip'=>$_SERVER['REMOTE_ADDR'], 'host'=>$host, 'referrer'=>$ref, 'date'=>date('D d M, H:i:s'), 'file'=>$file, 'id'=>$id, ); //FIXME not very good when multi-call $stats=load($_SESSION['stats_file']); if (!is_array($stats)){$stats=array();} if (count($stats)>$_SESSION['stats_max_entries']){ $stats=array_values($stats); unset($stats[0]); } $stats[]=$data; save($_SESSION['stats_file'], $stats); } function addslash_if_needed($chaine){ if (substr($chaine,strlen($chaine)-1,1)!='/'&&!empty($chaine)){return $chaine.'/';}else{return $chaine;} } function rename_item($file=null,$folder=''){ if (!$file){return false;} if (strpos($file, '/')!==false){$file=_basename($file);} $folder=addslash_if_needed($folder); $destination=$folder.$file; $nb=1; $extension=pathinfo($file,PATHINFO_EXTENSION); $file2=$file; while (is_file($destination) || is_dir($destination)){ $nb++; $add='('.$nb.')'; if (is_file($destination)) {$file2=str_replace('.'.$extension,$add.'.'.$extension,$file);} else{$file2=$file.$add;} $destination=$folder.$file2; } return $file2; } function no_special_char($string){return preg_replace('/[\"*\/\:<>\?|]/','',$string);} function file_curl_contents($url,$pretend=true){ # distant version of file_get_contents $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Charset: UTF-8')); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); if (!ini_get("safe_mode") && !ini_get('open_basedir') ) {curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);} curl_setopt($ch, CURLOPT_MAXREDIRS, 10); if ($pretend){curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0');} curl_setopt($ch, CURLOPT_REFERER, 'http://noreferer.com');// notez le referer "custom" $data = curl_exec($ch); $response_headers = curl_getinfo($ch); curl_close($ch); return $data; } function getUrl() { $url = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"]; $url .= $_SERVER["SCRIPT_NAME"]; return $url; } function rrmdir($dir) { # delete a folder and its content if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); rmdir($dir); } } function _glob($path,$pattern='') { # glob function fallback by Cyril MAGUIRE (thx bro' ;-) if($path=='/'){ $path=''; } $liste = array(); $pattern=str_replace('*','',$pattern); if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if(stripos($file, $pattern)!==false || $pattern=='' && $file!='.' && $file!='..' && $file!='.htaccess') { $liste[] = $path.$file; } } closedir($handle); } natcasesort($liste); return $liste; } function _basename($file){$array=explode('/',$file);if (is_array($array)){return end($array);}else{return $file;}} function only_type($tree=null, $ext=null){ if (empty($tree)||empty($ext)){return false;} if (is_string($tree)){ $extension=pathinfo($tree,PATHINFO_EXTENSION); if (stripos($ext, $extension)===false||empty($extension)){return false;} return true; }elseif (is_array($tree)){ $tree=unset_first_element($tree); if (empty($tree)){return false;} foreach($tree as $file){ if (is_file($file)||is_dir($file)){ $extension=pathinfo($file,PATHINFO_EXTENSION); if (stripos($ext, $extension)===false||empty($extension)){return false;} } } return true; } } function unset_first_element($array=null){ if (!$array){return false;} $first=array_keys($array);$first=$first[0]; unset($array[$first]); return $array; } function unzip($file, $destination){ if (!class_exists('ZipArchive')){return false;} $zip = new ZipArchive() ; if ($zip->open($file) !== TRUE) { return false;} $zip->extractTo($destination); $zip->close(); return true; } function zip($source, $destination) { if (!extension_loaded('zip')){return false;} $zip = new ZipArchive(); if (is_file($destination)){unlink($destination);} if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {return false;} if (is_string($source)){$source=array($source);} foreach($source as $item){ if (is_dir($item) === true){ $files = array_keys(iterator_to_array(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($item), RecursiveIteratorIterator::SELF_FIRST))); $files[0]=dirname($files[1]); foreach ($files as $key=>$file){ # Ignore "." and ".." folders if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ){continue;} $file_short=utf8_decode(str_replace(addslash_if_needed($_SESSION['upload_root_path'].$_SESSION['upload_user_path'].$_SESSION['current_path']),'',$file)); if (is_dir($file) === true){ $zip->addEmptyDir($file_short); }else if (is_file($file) === true){ $zip->addFromString($file_short, file_get_contents($file)); } } } else if (is_file($item) === true){ $zip->addFromString(_basename($item), file_get_contents($item)); } } } function check_path($path){ return (strpos($path, '//')===false && strpos($path, '..')===false && ( empty($path[0]) || (!empty($path[0]) && $path[0]!='/') ) ); } function get_thumbs_name($file){ global $auto_thumb; if($file[0]=='/'){ $file=substr($file,1); } return 'thumbs/'.preg_replace('#\.(jpe?g|png|gif)#i','_THUMB_'.$auto_thumb['default_width'].'x'.$auto_thumb['default_height'].'.$1',$file); } function get_thumbs_name_gallery($file){ global $gallery_thumbs_width; if($file[0]=='/'){ $file=substr($file,1); } return 'thumbs/'.preg_replace('#\.(jpe?g|png|gif)#i','_THUMBGALLERY_'.$gallery_thumbs_width.'x'.$gallery_thumbs_width.'.$1',$file); } function recursive_glob($dir='.',$files=true){ # scann a folder and subfolders and return the tree if (!isset($dossiers[0]) || $dossiers[0]!=$dir){$dossiers[0]=$dir;} if (!is_dir($dir)&&$files){ return array($dir); } elseif (!is_dir($dir)&&!$files){return array();} $list=_glob(addslash_if_needed($dir)); foreach ($list as $dossier) { $dossiers=array_merge($dossiers,recursive_glob($dossier,$files)); } return $dossiers; } ############################################ # IDS functions ############################################ # Delete the id if it's a burn one function burned($id){ if ($id[0]=='*'&&!isset($_GET['thumbs'])){ if (!is_user_connected() || !is_owner($id)){removeID($id);} } } # Create an ID for a file/folder function addID($path,$ids=null){# $path must be complete upload path global $ids; if (!$ids){$ids=unstore();} if ($id=array_search($path, $ids)){return $id;} $item_id=uniqid(true); $ids[$item_id]=$path; store($ids); return $item_id; } # remove an id from id file function removeID($id){ $ids=unstore(); if (!empty($ids[$id])){unset ($ids[$id]);} store($ids); } # remove unused ids, add new ids for current user function updateIDs($ids=null,$folder_id=null){ if (!$ids){$ids=unstore();} $sdi=array_flip($ids);$saveid=$savetree=false; $ids=array_flip($sdi); # here, all the redundant ids have gone ^^ # scann all uploads folder (can be long but it's important) # or only the requested folder if (!empty($folder_id)){ $tree=recursive_glob(id2file($folder_id),true); }else{ $tree=recursive_glob($_SESSION['upload_root_path'],true); } unset($tree[0]); # add missing ids foreach($tree as $index=>$file){ if (!isset($sdi[$file])){ $saveid=true; $id=uniqid(true); $ids[$id]=$file; }else{unset($sdi[$file]);} } if (empty($folder_id)){ # remove ids with no file (not required for single folder update) if (!empty($sdi)){ $saveid=true; foreach ($sdi as $file=>$id){ if (!is_dir($file)&&!is_file($file)){ unset($ids[$id]); if ($remove=array_search($file, $tree)){ unset($tree[$remove]); } } } } } if ($saveid){ store($ids); regen_tree($_SESSION['login'],$ids); } return $ids; } function is_in($ext,$type){ global $behaviour; if (!empty($behaviour[$type])){return array_search($ext,$behaviour[$type]);}else{return false;} } function id2file($id){ global $ids; if (isset($ids[$id])){ return $ids[$id]; }else{ return false; } } function file2id($file){ global $ids; $sdi=array_flip($ids); if (isset($sdi[$file])){return $sdi[$file];}else{return false;} } function only_folders($array=null){ if (empty($array)){return false;} $tree=array(); foreach($array as $key=>$value){ if (is_dir($value)){$tree[]=$value;} } return $tree; } function add_branch($path=null,$id=null,$user=null,$tree=array()){ global $ids; if (empty($path)){return false;} if (empty($id)&&empty($ids)){return false;} if (empty($id)){$id=file2id($path);} if (empty($user)&&!empty($_SESSION['login'])){$user=$_SESSION['login'];} if (empty($tree)){$tree=loadtree($user);} $tree[$id]=$path; natcasesort($tree); savetree($user,$tree); return $tree; } function remove_branch($path=null,$id=null,$user=null,$tree=array()){ global $ids; if (empty($path)){return false;} if (empty($id)&&empty($ids)){return false;} if (empty($id)){$id=file2id($path);} if (empty($user)&&!empty($_SESSION['login'])){$user=$_SESSION['login'];} if (empty($tree)){$tree=loadtree($user);} if (isset($tree[$id])||$id=array_search($path, $tree)){ unset($tree[$id]); } return $tree; } function rename_branch($new=null,$old=null,$id=null,$user=null,$tree=array()){ global $ids; if (empty($new)||empty($old)||$new==$old){return false;} if (empty($id)&&empty($ids)){return false;} if (empty($id)){$id=file2id($old);} if (empty($user)&&!empty($_SESSION['login'])){$user=$_SESSION['login'];} if (empty($tree)){$tree=loadtree($user);} if (isset($tree[$id])||$id=array_search($old, $tree)){ $tree[$id]=$new; } return $tree; } function savetree($user=null,$tree=array()){ if (empty($tree)){return false;} if (empty($user)&&!empty($_SESSION['login'])){$user=$_SESSION['login'];} save($_SESSION['private_folder'].'trees/'.$user.'.php',$tree); } function loadtree($user=null){ if (empty($user)&&!empty($_SESSION['login'])){$user=$_SESSION['login'];} if (empty($_SESSION['private_folder'])){return false;} $path=$_SESSION['private_folder'].'trees/'.$user.'.php'; if (is_file($path)){return load($path);} return false; } # Creates the user's folder tree (for ids file) to reduce all actions to this folder and not to all upload dir # if path, only regen a subfolder, return it function regen_tree($user='',$ids=null,$path=null){ global $ids; if (!$ids){$ids=unstore();} if (empty($user)){$user=$_SESSION['login'];} if (empty($path)){$path=$_SESSION['upload_root_path'].$user.'/'; /*if (!empty($user)){$path=$_SESSION['upload_root_path'].$user.'/';} elseif (!empty($_SESSION['upload_user_path'])){ $path=$_SESSION['upload_root_path'].$_SESSION['upload_user_path']; $user=$_SESSION['login']; }else{$path=$_SESSION['upload_root_path'];}*/ } $tree=array(); foreach($ids as $id=>$value){ if (strpos($value,$path)!==false){$tree[$id]=$value;} } return $tree; } # regen recursive folder tree if needed function user_folder_tree($user=null){ if (!$user&&!empty($_SESSION['login'])){$user=$_SESSION['login'];} if (isset($_SESSION['regenfolder'])||!is_file($_SESSION['private_folder'].$user.'.php')){ # regen folder tree $tree=tree($_SESSION['upload_root_path'].$_SESSION['upload_user_path'],$user,true,true); savetree($user,$tree); unset($_SESSION['regenfolder']); return $tree; }else{ return loadtree($user); } } # build folder content from the user's tree file to avoid excessive _glob access function tree($folder=null,$user=false,$folders_only=false,$recursive=false,$tree=null){ global $current_tree,$ids; if (!empty($current_tree)&&!$folders_only&&!$recursive){return $current_tree;} $dir=array(); if (!$user){$user=$_SESSION['login'];} if (!$tree){$tree=regen_tree($user);} if (!empty($current_tree)&&$folders_only){$tree=only_folders($current_tree);} elseif($folders_only){$tree=only_folders($tree);} if (empty($folder)){$folder=$_SESSION['upload_root_path'].$_SESSION['upload_user_path'].$_SESSION['current_path'];} if (!empty($tree)){ foreach ($tree as $id=>$path){ if ($recursive){ $p=addslash_if_needed($path); $f=addslash_if_needed($folder); $match=(strpos($p, $f)===0); }else{ $match=(addslash_if_needed(dirname($path))===addslash_if_needed($folder)); } if (!empty($_SESSION['filter'])){ $match=$match&&(strpos(_basename($path),$_SESSION['filter'])!==false); } if ($match===true){ $dir[$id]=$path; } } }else{$dir=array();} unset($_SESSION['filter']); return $dir; } function deep_strip_tags($var){if (is_string($var)){return strip_tags($var);}if (is_array($var)){return array_map('deep_strip_tags',$var);}return $var; } function visualizeIcon($extension){ global $behaviour; $array=array_merge(array_flip($behaviour['FILES_TO_RETURN']),array_flip($behaviour['FILES_TO_ECHO'])); return isset($array[$extension]); } function generate_bozon_salt($length=512){ $salt=''; for($i=1;$i<=$length;$i++){ $salt.=chr(mt_rand(35,126)); } return $salt; } function blur_password($pw){ if (!empty($pw)){return hash('sha512', BOZON_SALT.$pw);} return false; } # to solve some problems on mime detection, fallback if (function_exists('mime_content_type')){ function _mime_content_type($filename) {return mime_content_type($filename);} }elseif (function_exists('finfo_file')){ function _mime_content_type($filename) {return finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $filename );} }else{ function _mime_content_type($filename){ #inspired by http://stackoverflow.com/questions/8225644/php-mime-type-checking-alternative-way-of-doing-it $mime_types = array( 'txt' => 'text/plain', 'md' => 'text/plain', 'nfo' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', 'php' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'json' => 'application/json', 'xml' => 'application/xml', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/x-flv', // images 'png' => 'image/png', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'ico' => 'image/vnd.microsoft.icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml', // archives 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', 'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload', 'cab' => 'application/vnd.ms-cab-compressed', // audio/video 'mp3' => 'audio/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'm3u' => 'audio/x-mpegurl', // adobe 'pdf' => 'application/pdf', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', // ms office 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'docx' => 'application/msword', 'xlsx' => 'application/vnd.ms-excel', 'pptx' => 'application/vnd.ms-powerpoint', // open office 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', ); $ext=strtolower(pathinfo($filename,PATHINFO_EXTENSION)); if (array_key_exists($ext, $mime_types)) {return $mime_types[$ext];} else {return 'application/octet-stream';} } } # counts how much levels to go back (to close ';} if ($current_level<$previous_level){ $nb=count_back($previous_branch,$current_branch); echo str_repeat('',$nb); } if (is_dir($branch)){ echo '
  • '._basename(rtrim($branch, '/\\')).'
  • '; echo ''; echo ''; }elseif($image_only){ # image gallery if (!function_exists('auto_thumb')){include('core/auto_thumb.php');} global $gallery_thumbs_width; $title=explode('/',$tree[$first]);$title=$title[count($title)-1];unset($tree[$first]); echo ''; echo '
    '; echo ' '; }elseif($sound_only){ # music player $title=explode('/',$tree[$first]);$title=$title[count($title)-1];unset($tree[$first]); echo '

    '.$title.'

    '; echo ''; echo ''; echo '
    '; $mult=3; for ($vol=1;$vol<11;$vol++){ if ($vol<10){echo ' ';} else{echo ' ';} } echo '
    '; foreach($tree as $id=>$sound){ if (is_file($image)){ $link='index.php?f='.$id; $file=_basename($sound); $ext=strtolower(pathinfo($sound,PATHINFO_EXTENSION)); $size = sizeconvert(filesize($sound)); echo ''.$file.' '.$size.''; } } echo '
    '; echo ' '; } } /*function template($key,$array){ global $templates; if (isset($templates[$key])){ $tpl= str_replace(array_keys($array),array_values($array),$templates[$key]); return $tpl; }else{return false;} }*/ function template($key,$array){ global $templates; if (isset($templates[$key])){ $tpl=$templates[$key]; foreach($array as $key=>$value){ $tpl= str_replace($key,$value,$tpl); } return $tpl; }else{return false;} } function navigatorLanguage(){ if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){ $language = $_SERVER['HTTP_ACCEPT_LANGUAGE']; return $language{0}.$language{1}; }else{return 'fr';} } # locales functions function e($txt,$echo=true){ global $lang; if (isset($lang[$txt])){$t= $lang[$txt];}else{$t= $txt;} if ($echo){echo $t;}else{return $t;} } function available_languages(){ $l=_glob('locale/','php'); foreach($l as $key=>$lang){ $l[$key]=str_replace('.php','',_basename($lang)); } return $l; } # Links functions # create language links function make_lang_link($pattern='#LANG'){ $langs=available_languages(); if (!empty($_GET['p'])){$page=$_GET['p'];}else{$page='';} if(function_exists('returntoken')){$token=returnToken();}else{$token='';} foreach($langs as $lang){ if ($_SESSION['language']==$lang){$class=' class="active'.$lang.'" ';}else{$class='class="'.$lang.'" ';} echo str_replace(array('#CLASS','#LANG','#TOKEN','#PAGE'),array($class,$lang,$token,$page),$pattern); } } # create the connection/admin button function make_connect_link($label_admin=' ',$label_logout=' ',$label_login=' '){ $status=''; if (is_user_connected()){ if (!empty($_SESSION['login'])&&$label_admin==' '){$label_admin= $_SESSION['login'];} if (!empty($_SESSION['status'])){$status= $_SESSION['status'];} if(function_exists('returntoken')){$token=returnToken();}else{$token='';} echo ' '.$label_admin.''; echo ''; } else{echo '';} } # create the menu link (to change view) function make_menu_link($pattern=''){ if(function_exists('returntoken')){$token=returnToken();}else{$token='';} if (!empty($_GET['p'])){$page=$_GET['p'];}else{$page='';} if ($_SESSION['aspect']=='icon'){$class=' active';}else{$class='';} echo str_replace(array('#MENU','#THEME','#TOKEN','#PAGE','#CLASS','#TITLE'),array('icon',THEME_PATH,$token,$page,$class,e('See as icon',false)),$pattern); if ($_SESSION['aspect']=='list'){$class=' active';}else{$class='';} echo str_replace(array('#MENU','#THEME','#TOKEN','#PAGE','#CLASS','#TITLE'),array('list',THEME_PATH,$token,$page,$class,e('See as file list',false)),$pattern); } # create the mode links (to change access mode) function make_mode_link($pattern=''){ if(function_exists('returntoken')){$token=returnToken();}else{$token='';} if ($_SESSION['mode']=='view'){$class='active';}else{$class='';} echo str_replace(array('#MODE','#TITLE','#TOKEN','#CLASS'),array('view',e('Manage files',false),$token,$class),$pattern); if ($_SESSION['mode']=='links'){$class='active';}else{$class='';} echo str_replace(array('#MODE','#TITLE','#TOKEN','#CLASS'),array('links',e('Manage links',false),$token,$class),$pattern); } # Checks auto_restrict's session vars to know if a user is connected function is_user_connected(){ if (empty($_SESSION['id_user'])||empty($_SESSION['login'])||empty($_SESSION['expire'])){ return false; } return true; } # echo some classes depending on filemode, pages etc function body_classes(){ if (isset($_GET['users_list'])){echo 'users_list ';} if (!empty($_GET['p'])){echo $_GET['p'].' ';}else{echo 'home ';} if (!empty($_SESSION['language'])){echo 'body_'.$_SESSION['language'].' ';} if (!empty($_SESSION['mode'])){echo $_SESSION['mode'].' ';} if (!empty($_SESSION['aspect'])&&empty($_GET['f'])){echo $_SESSION['aspect'].' ';} } # Users functions # return the user's name hashed or the user's name corresponding to a hash function hash_user($user_or_hash){ if (!is_file('private/auto_restrict_users.php')){return false;} include ('private/auto_restrict_users.php'); if (strlen($user_or_hash)>100){ # hash > user foreach ($auto_restrict['users'] as $user=>$data){ $hash=hash('sha512',$data['salt'].$user); if ($hash==$user_or_hash){return $user;} } return false; }else{ # user > hash if (!empty($auto_restrict['users'][$user_or_hash])){ return hash('sha512',$auto_restrict['users'][$user_or_hash]['salt'].$user_or_hash); } return false; } } # Check if current user is the id's owner function is_owner($id=null){ if (!$id || empty($_SESSION['login']) || !$f=id2file($id)){return false;} $file=explode('/',$f);$owner=$file[1]; return $_SESSION['login']==$owner; } # Return id's owner function return_owner($id=null){ if (!$id){return false;} $file=explode('/',id2file($id)); if (!empty($file[1])){$owner=$file[1];} else{$owner=e('Deleted',false);} return $owner; } function is_allowed($action,$profile=null){ global $RIGHTS; if (!is_user_connected()){return false;} if (!isset($RIGHTS)){return false;} if (!$profile&&!empty($_SESSION['status'])){$profile=$_SESSION['status'];}elseif (!$profile){return false;} if ($profile=='superadmin'){return true;} if (isset($RIGHTS[$profile][$action])){return true;}else{return false;} } /* function is_admin(){ global $auto_restrict; if (empty($_SESSION['login'])){return false;} if (!empty($_SESSION['status'])){ if ($_SESSION['status']=='admin'||$_SESSION['status']=='superadmin'){return true;} }else{ $first=first($auto_restrict['users']); if ($_SESSION['login']==$first['login']){return true;} if (isset($auto_restrict['users'][$_SESSION['login']]['status'])&&$auto_restrict['users'][$_SESSION['login']]['status']=='admin'){return true;} } return false; }*/ function is_superadmin(){if (!empty($_SESSION['status'])&&$_SESSION['status']=='superadmin'){return true;}return false;} function load_users_list(){ global $auto_restrict; if (empty($auto_restrict["users"])){ $auto_restrict_users=$_SESSION['private_folder'].'auto_restrict_users.php'; if (!is_file($auto_restrict_users)){return false;} include($auto_restrict_users); } return $auto_restrict["users"]; } # Complete users rights function complete_users_rights($users_rights=null){ global $auto_restrict,$default_profile_folder_max_size; $save=false; $users=$auto_restrict["users"]; if (empty($users)){return false;} if (!$users_rights){$users_rights=load_users_rights();} foreach ($users as $key=>$user){ # add missing if (!isset($users_rights[$user['login']])){ $users_rights[$user['login']]=$default_profile_folder_max_size; $save=true; } } foreach ($users_rights as $user=>$size){ # remove deleted profiles if (!isset($users[$user])){ unset($users_rights[$user]); $save=true; } } if ($save){save_users_rights($users_rights);} return $users_rights; } # creates a form with the users list function generate_users_folder_space_formlist($text='Check users to delete account and files'){ global $auto_restrict,$default_profile_folder_max_size; $users_rights=complete_users_rights(); echo '

    '.$text.'

    '; foreach ($users_rights as $user=>$size){ if ($auto_restrict['users'][$user]['status']!='superadmin'){ $class=' class="'.$auto_restrict['users'][$user]['status'].'" title="'.e($auto_restrict['users'][$user]['status'],false).'"'; echo ''; echo ''; echo ''; echo ''; newToken(); echo ''; } } echo '
    '; echo ''.$user.' ('.folder_size($_SESSION['upload_root_path'].$user).' '.e('used',false).')
    '; } # Folder functions # folder_size % functions function sizeconvert( $bytes ){ $label = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB' ); for( $i = 0; $bytes >= 1024 && $i < ( count( $label ) -1 ); $bytes /= 1024, $i++ ); return( round( $bytes, 2 ) . " " . $label[$i] ); } function folder_size($folder,$convert=true){ $tree=tree($folder,null,false,true);$size=0; foreach($tree as $branch){ if (is_file(($branch))){$size+=filesize($branch);} } if ($convert){return sizeconvert($size);} return $size; } function folder_free($folder,$mode=false){ if (empty($_SESSION['profile_folder_max_size'])){return false;} $max=($_SESSION['profile_folder_max_size']*1048576); $size=round($max-strval(folder_size($folder,false)),2); if ($size<=0){return false;} if (!$mode){ # converted size return sizeconvert($size); }elseif ($mode=1){ # oct return $size; }else{ # % return round((100*$size)/$max,1); } } function folder_fit($file=null,$size=null,$profile=null){ if (!$file&&!$size||!$profile){return false;} $is_admin=is_superadmin(); if (empty($_SESSION['profile_folder_max_size'])&&!$is_admin){return false;} $folder=$_SESSION['upload_root_path'].$profile; $max=$_SESSION['profile_folder_max_size']*1048576; if (!empty($file)){ if (!is_file($file)){return false;} $size=filesize($file); } if (folder_size($folder,false)+$size>$max&&!$is_admin){return false;} return true; } function folder_usage_draw($profile,$mode=1){ $folder=$_SESSION['upload_root_path'].$profile; if (!is_dir($folder)){return false;} if (empty($_SESSION['profile_folder_max_size'])){return false;} if (is_admin()){return false;} $free=folder_free($folder,1); $user_size=$_SESSION['profile_folder_max_size']*1048576; $used=round($user_size-$free,1); $usedpc=round($used*100/$user_size,1); $freepc=round($free*100/$user_size,1); if (empty($free)){$free=0;} if ($mode==1){echo '
    '.$usedpc.'%'.$freepc.'%
    ';} if ($mode==2){echo '
    ';} if ($mode==3){echo '
    '.$freepc.'% '.e('free',false).' ('.sizeconvert($free).')
    ';} } function draw_lb_link($file,$alt=null,$text_link=' ',$group='',$type='iframe'){ if(@is_array(getimagesize($file))){$type='img';} if (!empty($group)){$group='data-group="'.$group.'"';} echo ''.$text_link.''; } function start_session(){if (!session_id()){session_start();}} function extract_config_vars(){ preg_match_all("#\$([^';=]*?)=#", file_get_contents('config.php'), $vars);aff($vars); $conf=array(); foreach($vars[1] as $index=>$varname){echo $varname; if (!isset($$varname)){global $$varname;} $conf[$$varname]=$$varname; } return $conf; } ################################################# # Debug functions ################################################# function aff($var,$stop=true){ $dat=debug_backtrace();$origin=''; echo '
    Arret ligne '.$dat[0]['line'].' dans le fichier '.$dat[0]['file'].'
    '; echo '
    ';var_dump($var);echo '
    '; foreach (array_reverse($dat) as $data){ $dir=dirname($data['file']).'/'; $fil=_basename($data['file']); $origin.=''; } $origin.='
    '.$dir.''.$fil.''.$data['function'].'() '.$data['line'].'
    '; echo '
    '.$origin.'
    '; if ($stop){exit();} } function li($string){echo '
  • '.$string.'
  • ';} function debug_log($str){file_put_contents('debug.html', $str,FILE_APPEND);} function chrono($name='chrono',$delete=false){ if ($delete){@unlink('debug.html');} global $debug_mode; if (!$debug_mode){return false;} if (!isset($_SESSION[$name])){$_SESSION[$name]=microtime(true);} else{ $count=round(microtime(true)-$_SESSION[$name],2); if ($count<1){$color='lightgreen';$color2='black';} if ($count>1&&$count<=2){$color='yellow';$color2='maroon';} if ($count>2&&$count<=5){$color='orange';$color2='maroon';} if ($count>5){$color='red';$color2='pink';} $debug= '
  • '.date('h:i:s').' '.$name.':'.$count.'sec
  • '; debug_log($debug); unset($_SESSION[$name]); } } ?>