linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file filemanager.class.php

Documentation is available at filemanager.class.php

  1. <?php
  2. /**
  3.  *    Filemanager PHP class
  4.  *
  5.  *    filemanager.class.php
  6.  *    class for the filemanager.php connector
  7.  *
  8.  *    @license    MIT License
  9.  *    @author        Riaan Los <mail (at) riaanlos (dot) nl>
  10.  *    @author        Simon Georget <simon (at) linea21 (dot) com>
  11.  *    @copyright    Authors
  12.  *  @package     linea21.externals
  13.  *  @subpackage Filemanager
  14.  */
  15.  
  16. class Filemanager {
  17.  
  18.   protected $config = array();
  19.   protected $language = array();
  20.   protected $get = array();
  21.   protected $post = array();
  22.   protected $properties = array();
  23.   protected $item = array();
  24.   protected $languages = array();
  25.   protected $root = '';
  26.   protected $doc_root = '';
  27.  
  28.   public function __construct($config{
  29.         $this->config = $config;
  30.         $this->root = dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR;
  31.         $this->properties = array(
  32.               'Date Created'=>null,
  33.               'Date Modified'=>null,
  34.               'Height'=>null,
  35.               'Width'=>null,
  36.               'Size'=>null
  37.         );
  38.         if (isset($this->config['doc_root'])) {
  39.               $this->doc_root = $this->config['doc_root'];
  40.         else {
  41.               $this->doc_root = $_SERVER['DOCUMENT_ROOT'];
  42.         }
  43.  
  44.         $this->setParams();
  45.         $this->availableLanguages();
  46.         $this->loadLanguageFile();
  47.   }
  48.   
  49.   public function error($string,$textarea=false{
  50.     $array array(
  51.             'Error'=>$string,
  52.             'Code'=>'-1',
  53.             'Properties'=>$this->properties
  54.     );
  55.     if($textarea{
  56.       echo '<textarea>' json_encode($array'</textarea>';
  57.     else {
  58.       echo json_encode($array);
  59.     }
  60.     die();
  61.   }
  62.  
  63.   public function lang($string{
  64.     if(isset($this->language[$string]&& $this->language[$string]!=''{
  65.       return $this->language[$string];
  66.     else {
  67.       return 'Language string error on ' $string;
  68.     }
  69.   }
  70.  
  71.   public function getvar($var{
  72.     if(!isset($_GET[$var]|| $_GET[$var]==''{
  73.       $this->error(sprintf($this->lang('INVALID_VAR'),$var));
  74.     else {
  75.       $this->get[$var$this->sanitize($_GET[$var]);
  76.       return true;
  77.     }
  78.   }
  79.   public function postvar($var{
  80.     if(!isset($_POST[$var]|| $_POST[$var]==''{
  81.       $this->error(sprintf($this->lang('INVALID_VAR'),$var));
  82.     else {
  83.       $this->post[$var$_POST[$var];
  84.       return true;
  85.     }
  86.   }
  87.  
  88.   public function getinfo({
  89.     $this->item = array();
  90.     $this->item['properties'$this->properties;
  91.     $this->get_file_info();
  92.     $full_path $this->doc_root .$this->get['path'];
  93.  
  94.     $array array(
  95.             'Path'=> $this->get['path'],
  96.             'Filename'=>$this->item['filename'],
  97.             'File Type'=>$this->item['filetype'],
  98.             'Preview'=>$this->item['preview'],
  99.             'Properties'=>$this->item['properties'],
  100.             'Error'=>"",
  101.             'Code'=>0
  102.     );
  103.     return $array;
  104.   }
  105.  
  106.   public function getfolder({
  107.     $array array();
  108.     $current_path $this->doc_root . $this->get['path'];
  109.     if(!is_dir($current_path)) {
  110.       $this->error(sprintf($this->lang('DIRECTORY_NOT_EXIST'),$this->get['path']));
  111.     }
  112.     if(!$handle opendir($current_path)) {
  113.       $this->error(sprintf($this->lang('UNABLE_TO_OPEN_DIRECTORY'),$this->get['path']));
  114.     else {
  115.       while (false !== ($file readdir($handle))) {
  116.         if($file != "." && $file != ".." && is_dir($current_path $file)) {
  117.           if(!in_array($file$this->config['unallowed_dirs'])) {
  118.             $array[$this->get['path'$file .'/'array(
  119.                         'Path'=> $this->get['path'$file .'/',
  120.                         'Filename'=>$file,
  121.                         'File Type'=>'dir',
  122.                         'Preview'=> $this->config['icons']['path'$this->config['icons']['directory'],
  123.                         'Properties'=>array(
  124.                             'Date Created'=>null,
  125.                             'Date Modified'=>null,
  126.                             'Height'=>null,
  127.                             'Width'=>null,
  128.                             'Size'=>null
  129.             ),
  130.                         'Error'=>"",
  131.                         'Code'=>0
  132.             );
  133.           }
  134.         else if ($file != "." && $file != ".."  && !in_array($file$this->config['unallowed_files'])) {
  135.           $this->item = array();
  136.           $this->item['properties'$this->properties;
  137.           $this->get_file_info($this->get['path'$file);
  138.            
  139.           if(!isset($this->params['type']|| (isset($this->params['type']&& strtolower($this->params['type'])=='images' && in_array(strtolower($this->item['filetype']),$this->config['images']))) {
  140.             if($this->config['upload']['imagesonly']== false || ($this->config['upload']['imagesonly']== true && in_array(strtolower($this->item['filetype']),$this->config['images']))) {
  141.               $array[$this->get['path'$filearray(
  142.                             'Path'=>$this->get['path'$file,
  143.                             'Filename'=>$this->item['filename'],
  144.                             'File Type'=>$this->item['filetype'],
  145.                             'Preview'=>$this->item['preview'],
  146.                             'Properties'=>$this->item['properties'],
  147.                             'Error'=>"",
  148.                             'Code'=>0
  149.               );
  150.             }
  151.           }
  152.         }
  153.       }
  154.       closedir($handle);
  155.     }
  156.     return $array;
  157.   }
  158.  
  159.   public function rename({
  160.  
  161.     $suffix='';
  162.  
  163.  
  164.     if(substr($this->get['old'],-1,1)=='/'{
  165.       $this->get['old'substr($this->get['old'],0,(strlen($this->get['old'])-1));
  166.       $suffix='/';
  167.     }
  168.     $tmp explode('/',$this->get['old']);
  169.     $filename $tmp[(sizeof($tmp)-1)];
  170.     $path str_replace('/' $filename,'',$this->get['old']);
  171.  
  172.     if(file_exists ($this->doc_root . $path '/' $this->get['new'])) {
  173.       if($suffix=='/' && is_dir($this->doc_root . $path '/' $this->get['new'])) {
  174.         $this->error(sprintf($this->lang('DIRECTORY_ALREADY_EXISTS'),$this->get['new']));
  175.       }
  176.       if($suffix=='' && is_file($this->doc_root . $path '/' $this->get['new'])) {
  177.         $this->error(sprintf($this->lang('FILE_ALREADY_EXISTS'),$this->get['new']));
  178.       }
  179.     }
  180.  
  181.     if(!rename($this->doc_root . $this->get['old'],$this->doc_root . $path '/' $this->get['new'])) {
  182.       if(is_dir($this->get['old'])) {
  183.         $this->error(sprintf($this->lang('ERROR_RENAMING_DIRECTORY'),$filename,$this->get['new']));
  184.       else {
  185.         $this->error(sprintf($this->lang('ERROR_RENAMING_FILE'),$filename,$this->get['new']));
  186.       }
  187.     }
  188.     $array array(
  189.             'Error'=>"",
  190.             'Code'=>0,
  191.             'Old Path'=>$this->get['old'],
  192.             'Old Name'=>$filename,
  193.             'New Path'=>$path '/' $this->get['new'].$suffix,
  194.             'New Name'=>$this->get['new']
  195.     );
  196.     return $array;
  197.   }
  198.  
  199.   public function delete({
  200.  
  201.     if(is_dir($this->doc_root . $this->get['path'])) {
  202.       $this->unlinkRecursive($this->doc_root . $this->get['path']);
  203.       $array array(
  204.                 'Error'=>"",
  205.                 'Code'=>0,
  206.                 'Path'=>$this->get['path']
  207.       );
  208.       return $array;
  209.     else if(file_exists($this->doc_root . $this->get['path'])) {
  210.       unlink($this->doc_root . $this->get['path']);
  211.       $array array(
  212.                 'Error'=>"",
  213.                 'Code'=>0,
  214.                 'Path'=>$this->get['path']
  215.       );
  216.       return $array;
  217.     else {
  218.       $this->error(sprintf($this->lang('INVALID_DIRECTORY_OR_FILE')));
  219.     }
  220.   }
  221.  
  222.   public function add({
  223.     $this->setParams();
  224.     if(!isset($_FILES['newfile']|| !is_uploaded_file($_FILES['newfile']['tmp_name'])) {
  225.       $this->error(sprintf($this->lang('INVALID_FILE_UPLOAD')),true);
  226.     }
  227.     if(($this->config['upload']['size']!=false && is_numeric($this->config['upload']['size'])) && ($_FILES['newfile']['size'($this->config['upload']['size'1024 1024))) {
  228.       $this->error(sprintf($this->lang('UPLOAD_FILES_SMALLER_THAN'),$this->config['upload']['size''Mb'),true);
  229.     }
  230.     if($this->config['upload']['imagesonly'|| (isset($this->params['type']&& strtolower($this->params['type'])=='images')) {
  231.       if(!($size @getimagesize($_FILES['newfile']['tmp_name']))){
  232.         $this->error(sprintf($this->lang('UPLOAD_IMAGES_ONLY')),true);
  233.       }
  234.       if(!in_array($size[2]array(12378))) {
  235.         $this->error(sprintf($this->lang('UPLOAD_IMAGES_TYPE_JPEG_GIF_PNG')),true);
  236.       }
  237.     }
  238.     $_FILES['newfile']['name'$this->cleanString($_FILES['newfile']['name'],array('.','-'));
  239.     if(!$this->config['upload']['overwrite']{
  240.       $_FILES['newfile']['name'$this->checkFilename($this->doc_root . $this->post['currentpath'],$_FILES['newfile']['name']);
  241.     }
  242.     move_uploaded_file($_FILES['newfile']['tmp_name']$this->doc_root . $this->post['currentpath'$_FILES['newfile']['name']);
  243.  
  244.     $response array(
  245.             'Path'=>$this->post['currentpath'],
  246.             'Name'=>$_FILES['newfile']['name'],
  247.             'Error'=>"",
  248.             'Code'=>0
  249.     );
  250.     echo '<textarea>' json_encode($response'</textarea>';
  251.     die();
  252.   }
  253.  
  254.   public function addfolder({
  255.     if(is_dir($this->doc_root . $this->get['path'$this->get['name'])) {
  256.       $this->error(sprintf($this->lang('DIRECTORY_ALREADY_EXISTS'),$this->get['name']));
  257.        
  258.     }
  259.     $newdir $this->cleanString($this->get['name']);
  260.     if(!mkdir($this->doc_root . $this->get['path'$newdir,0755)) {
  261.       $this->error(sprintf($this->lang('UNABLE_TO_CREATE_DIRECTORY'),$newdir));
  262.     }
  263.     $array array(
  264.             'Parent'=>$this->get['path'],
  265.             'Name'=>$this->get['name'],
  266.             'Error'=>"",
  267.             'Code'=>0
  268.     );
  269.     return $array;
  270.   }
  271.  
  272.   public function download({
  273.  
  274.     if(isset($this->get['path']&& file_exists($this->doc_root .$this->get['path'])) {
  275.       header("Content-type: application/force-download");
  276.       header('Content-Disposition: inline; filename="' basename($this->get['path']'"');
  277.       header("Content-Transfer-Encoding: Binary");
  278.       header("Content-length: ".filesize($this->doc_root . $this->get['path']));
  279.       header('Content-Type: application/octet-stream');
  280.       header('Content-Disposition: attachment; filename="' basename($this->get['path']'"');
  281.       readfile($this->doc_root . $this->get['path']);
  282.     else {
  283.       $this->error(sprintf($this->lang('FILE_DOES_NOT_EXIST'),$this->get['path']));
  284.     }
  285.   }
  286.  
  287.   public function preview({
  288.  
  289.     if(isset($this->get['path']&& file_exists($this->doc_root . $this->get['path'])) {
  290.       header("Content-type: image/" .$ext pathinfo($this->get['path']PATHINFO_EXTENSION));
  291.       header("Content-Transfer-Encoding: Binary");
  292.       header("Content-length: ".filesize($this->doc_root . $this->get['path']));
  293.       header('Content-Disposition: inline; filename="' basename($this->get['path']'"');
  294.       readfile($this->doc_root . $this->get['path']);
  295.     else {
  296.       $this->error(sprintf($this->lang('FILE_DOES_NOT_EXIST'),$this->get['path']));
  297.     }
  298.   }
  299.  
  300.   private function setParams({
  301.       $tmp (isset($_SERVER['HTTP_REFERER']$_SERVER['HTTP_REFERER''/');
  302.     $tmp explode('?',$tmp);
  303.     $params array();
  304.     if(isset($tmp[1]&& $tmp[1]!=''{
  305.       $params_tmp explode('&',$tmp[1]);
  306.       if(is_array($params_tmp)) {
  307.         foreach($params_tmp as $value{
  308.           $tmp explode('=',$value);
  309.           if(isset($tmp[0]&& $tmp[0]!='' && isset($tmp[1]&& $tmp[1]!=''{
  310.             $params[$tmp[0]] $tmp[1];
  311.           }
  312.         }
  313.       }
  314.     }
  315.     $this->params $params;
  316.   }
  317.  
  318.  
  319.   private function get_file_info($path='',$return=array()) {
  320.     if($path==''{
  321.       $path $this->get['path'];
  322.     }
  323.     $tmp explode('/',$path);
  324.     $this->item['filename'$tmp[(sizeof($tmp)-1)];
  325.  
  326.     $tmp explode('.',$this->item['filename']);
  327.     $this->item['filetype'$tmp[(sizeof($tmp)-1)];
  328.     $this->item['filemtime'filemtime($this->doc_root . $path);
  329.     $this->item['filectime'filectime($this->doc_root . $path);
  330.  
  331.     $this->item['preview'$this->config['icons']['path'$this->config['icons']['default'];
  332.  
  333.     if(is_dir($this->doc_root . $path)) {
  334.        
  335.       $this->item['preview'$this->config['icons']['path'$this->config['icons']['directory'];
  336.        
  337.     else if(in_array(strtolower($this->item['filetype']),$this->config['images'])) {
  338.        
  339.       $this->item['preview''connectors/php/filemanager.php?mode=preview&path=' $path;
  340.       //if(isset($get['getsize']) && $get['getsize']=='true') {
  341.       list($width$height$type$attrgetimagesize($this->doc_root . $path);
  342.       $this->item['properties']['Height'$height;
  343.       $this->item['properties']['Width'$width;
  344.       $this->item['properties']['Size'filesize($this->doc_root . $path);
  345.       //}
  346.        
  347.     else if(file_exists($this->root . $this->config['icons']['path'strtolower($this->item['filetype']'.png')) {
  348.        
  349.       $this->item['preview'$this->config['icons']['path'strtolower($this->item['filetype']'.png';
  350.       $this->item['properties']['Size'filesize($this->doc_root . $path);
  351.        
  352.     }
  353.  
  354.     $this->item['properties']['Date Modified'date($this->config['date']$this->item['filemtime']);
  355.     //$return['properties']['Date Created'] = date($config['date'], $return['filectime']); // PHP cannot get create timestamp
  356.   }
  357.  
  358.   private function unlinkRecursive($dir,$deleteRootToo=true{
  359.     if(!$dh @opendir($dir)) {
  360.       return;
  361.     }
  362.     while (false !== ($obj readdir($dh))) {
  363.       if($obj == '.' || $obj == '..'{
  364.         continue;
  365.       }
  366.        
  367.       if (!@unlink($dir '/' $obj)) {
  368.         $this->unlinkRecursive($dir.'/'.$objtrue);
  369.       }
  370.     }
  371.  
  372.     closedir($dh);
  373.  
  374.     if ($deleteRootToo{
  375.       @rmdir($dir);
  376.     }
  377.     return;
  378.   }
  379.  
  380.   private function cleanString($string$allowed array()) {
  381.     $allow null;
  382.  
  383.     if (!empty($allowed)) {
  384.       foreach ($allowed as $value{
  385.         $allow .= "\\$value";
  386.       }
  387.     }
  388.  
  389.     $mapping array(
  390.         'Š'=>'S''š'=>'s''Đ'=>'Dj''đ'=>'dj''Ž'=>'Z''ž'=>'z''Č'=>'C''č'=>'c''Ć'=>'C''ć'=>'c',
  391.         'À'=>'A''Á'=>'A''Â'=>'A''Ã'=>'A''Ä'=>'A''Å'=>'A''Æ'=>'A''Ç'=>'C''È'=>'E''É'=>'E',
  392.         'Ê'=>'E''Ë'=>'E''Ì'=>'I''Í'=>'I''Î'=>'I''Ï'=>'I''Ñ'=>'N''Ò'=>'O''Ó'=>'O''Ô'=>'O',
  393.         'Õ'=>'O''Ö'=>'O''Ő'=>'O''Ø'=>'O''Ù'=>'U''Ú'=>'U''Û'=>'U''Ü'=>'U''Ű'=>'U''Ý'=>'Y',
  394.         'Þ'=>'B''ß'=>'Ss','à'=>'a''á'=>'a''â'=>'a''ã'=>'a''ä'=>'a''å'=>'a''æ'=>'a''ç'=>'c',
  395.         'è'=>'e''é'=>'e''ê'=>'e''ë'=>'e''ì'=>'i''í'=>'i''î'=>'i''ï'=>'i''ð'=>'o''ñ'=>'n',
  396.         'ò'=>'o''ó'=>'o''ô'=>'o''õ'=>'o''ö'=>'o''ő'=>'o''ø'=>'o''ù'=>'u''ú'=>'u''ű'=>'u',
  397.         'û'=>'u''ý'=>'y''ý'=>'y''þ'=>'b''ÿ'=>'y''Ŕ'=>'R''ŕ'=>'r'' '=>'_'"'"=>'_''/'=>''
  398.         );
  399.  
  400.         if (is_array($string)) {
  401.  
  402.           $cleaned array();
  403.  
  404.           foreach ($string as $key => $clean{
  405.             $clean strtr($clean$mapping);
  406.             $clean preg_replace("/[^{$allow}_a-zA-Z0-9]/"''$clean);
  407.             $cleaned[$keypreg_replace('/[_]+/''_'$clean)// remove double underscore
  408.           }
  409.         else {
  410.           $string strtr($string$mapping);
  411.           $string preg_replace("/[^{$allow}_a-zA-Z0-9]/"''$string);
  412.           $cleaned preg_replace('/[_]+/''_'$string)// remove double underscore
  413.         }
  414.         return $cleaned;
  415.   }
  416.  
  417.   private function sanitize($var{
  418.     $sanitized strip_tags($var);
  419.     $sanitized str_replace('http://'''$sanitized);
  420.     $sanitized str_replace('https://'''$sanitized);
  421.     $sanitized str_replace('../'''$sanitized);
  422.     return $sanitized;
  423.   }
  424.  
  425.   private function checkFilename($path,$filename,$i=''{
  426.     if(!file_exists($path $filename)) {
  427.       return $filename;
  428.     else {
  429.       $_i $i;
  430.       $tmp explode(/*$this->config['upload']['suffix'] . */$i '.',$filename);
  431.       if($i==''{
  432.         $i=1;
  433.       else {
  434.         $i++;
  435.       }
  436.       $filename str_replace($_i '.' $tmp[(sizeof($tmp)-1)],$i '.' $tmp[(sizeof($tmp)-1)],$filename);
  437.       return $this->checkFilename($path,$filename,$i);
  438.     }
  439.   }
  440.  
  441.   private function loadLanguageFile({
  442.  
  443.     // we load langCode var passed into URL if present and if exists
  444.     // else, we use default configuration var
  445.     $lang $this->config['culture'];
  446.     if(isset($this->params['langCode']&& in_array($this->params['langCode']$this->languages)) $lang $this->params['langCode'];
  447.  
  448.     if(file_exists($this->root'scripts/languages/'.$lang.'.js')) {
  449.       $stream =file_get_contents($this->root'scripts/languages/'.$lang.'.js');
  450.       $this->language = json_decode($streamtrue);
  451.     else {
  452.       $stream =file_get_contents($this->root'scripts/languages/'.$lang.'.js');
  453.       $this->language = json_decode($streamtrue);
  454.     }
  455.   }
  456.  
  457.   private function availableLanguages({
  458.  
  459.     if ($handle opendir($this->root.'/scripts/languages/')) {
  460.       while (false !== ($file readdir($handle))) {
  461.         if ($file != "." && $file != ".."{
  462.           array_push($this->languagespathinfo($filePATHINFO_FILENAME));
  463.         }
  464.       }
  465.       closedir($handle);
  466.     }
  467.   }
  468. }
  469. ?>

Documentation generated on Fri, 01 Apr 2011 09:30:52 +0200 by phpDocumentor 1.4.1