File indexing completed on 2025-02-09 07:18:02

0001 <?php
0002 
0003 /**
0004  * Default elFinder connector
0005  *
0006  * @author Dmitry (dio) Levashov
0007  **/
0008 class elFinderConnector {
0009   /**
0010    * elFinder instance
0011    *
0012    * @var elFinder
0013    **/
0014   protected $elFinder;
0015   
0016   /**
0017    * Options
0018    *
0019    * @var aray
0020    **/
0021   protected $options = array();
0022   
0023   /**
0024    * undocumented class variable
0025    *
0026    * @var string
0027    **/
0028   protected $header = 'Content-Type: application/json';
0029   
0030   
0031   /**
0032    * Constructor
0033    *
0034    * @return void
0035    * @author Dmitry (dio) Levashov
0036    **/
0037   public function __construct($elFinder, $debug=false) {
0038     
0039     $this->elFinder = $elFinder;
0040     if ($debug) {
0041       $this->header = 'Content-Type: text/html; charset=utf-8';
0042     }
0043   }
0044   
0045   /**
0046    * Execute elFinder command and output result
0047    *
0048    * @return void
0049    * @author Dmitry (dio) Levashov
0050    **/
0051   public function run() {
0052     $isPost = $_SERVER["REQUEST_METHOD"] == 'POST';
0053     $src    = $_SERVER["REQUEST_METHOD"] == 'POST' ? $_POST : $_GET;
0054     $cmd    = isset($src['cmd']) ? $src['cmd'] : '';
0055     $args   = array();
0056     
0057     if (!function_exists('json_encode')) {
0058       $error = $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_JSON);
0059       $this->output(array('error' => '{"error":["'.implode('","', $error).'"]}', 'raw' => true));
0060     }
0061     
0062     if (!$this->elFinder->loaded()) {
0063       $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_VOL), 'debug' => $this->elFinder->mountErrors));
0064     }
0065     
0066     // telepat_mode: on
0067     if (!$cmd && $isPost) {
0068       $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UPLOAD, elFinder::ERROR_UPLOAD_TOTAL_SIZE), 'header' => 'Content-Type: text/html'));
0069     }
0070     // telepat_mode: off
0071     
0072     if (!$this->elFinder->commandExists($cmd)) {
0073       $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UNKNOWN_CMD)));
0074     }
0075     
0076     // collect required arguments to exec command
0077     foreach ($this->elFinder->commandArgsList($cmd) as $name => $req) {
0078       $arg = $name == 'FILES' 
0079         ? $_FILES 
0080         : (isset($src[$name]) ? $src[$name] : '');
0081         
0082       if (!is_array($arg)) {
0083         $arg = trim($arg);
0084       }
0085       if ($req && (!isset($arg) || $arg === '')) {
0086         $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_INV_PARAMS, $cmd)));
0087       }
0088       $args[$name] = $arg;
0089     }
0090     
0091     $args['debug'] = isset($src['debug']) ? !!$src['debug'] : false;
0092     
0093     $this->output($this->elFinder->exec($cmd, $args));
0094   }
0095   
0096   /**
0097    * Output json
0098    *
0099    * @param  array  data to output
0100    * @return void
0101    * @author Dmitry (dio) Levashov
0102    **/
0103   protected function output(array $data) {
0104     $header = isset($data['header']) ? $data['header'] : $this->header;
0105     unset($data['header']);
0106     if ($header) {
0107       if (is_array($header)) {
0108         foreach ($header as $h) {
0109           header($h);
0110         }
0111       } else {
0112         header($header);
0113       }
0114     }
0115     
0116     if (isset($data['pointer'])) {
0117       rewind($data['pointer']);
0118       fpassthru($data['pointer']);
0119       if (!empty($data['volume'])) {
0120         $data['volume']->close($data['pointer'], $data['info']['hash']);
0121       }
0122       exit();
0123     } else {
0124       if (!empty($data['raw']) && !empty($data['error'])) {
0125         exit($data['error']);
0126       } else {
0127         exit(json_encode($data));
0128       }
0129     }
0130     
0131   }
0132   
0133 }// END class