4 * Default elFinder connector
6 * @author Dmitry (dio) Levashov
8 class elFinderConnector {
21 protected $options = array();
24 * undocumented class variable
28 protected $header = 'Content-Type: application/json';
35 * @author Dmitry (dio) Levashov
37 public function __construct($elFinder, $debug=false) {
39 $this->elFinder = $elFinder;
41 $this->header = 'Content-Type: text/html; charset=utf-8';
46 * Execute elFinder command and output result
49 * @author Dmitry (dio) Levashov
51 public function run() {
52 $isPost = $_SERVER["REQUEST_METHOD"] == 'POST';
53 $src = $_SERVER["REQUEST_METHOD"] == 'POST' ? $_POST : $_GET;
54 $cmd = isset($src['cmd']) ? $src['cmd'] : '';
57 if (!function_exists('json_encode')) {
58 $error = $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_JSON);
59 $this->output(array('error' => '{"error":["'.implode('","', $error).'"]}', 'raw' => true));
62 if (!$this->elFinder->loaded()) {
63 $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_VOL), 'debug' => $this->elFinder->mountErrors));
67 if (!$cmd && $isPost) {
68 $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UPLOAD, elFinder::ERROR_UPLOAD_TOTAL_SIZE), 'header' => 'Content-Type: text/html'));
72 if (!$this->elFinder->commandExists($cmd)) {
73 $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UNKNOWN_CMD)));
76 // collect required arguments to exec command
77 foreach ($this->elFinder->commandArgsList($cmd) as $name => $req) {
78 $arg = $name == 'FILES'
80 : (isset($src[$name]) ? $src[$name] : '');
82 if (!is_array($arg)) {
85 if ($req && (!isset($arg) || $arg === '')) {
86 $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_INV_PARAMS, $cmd)));
91 $args['debug'] = isset($src['debug']) ? !!$src['debug'] : false;
93 $this->output($this->elFinder->exec($cmd, $args));
99 * @param array data to output
101 * @author Dmitry (dio) Levashov
103 protected function output(array $data) {
104 $header = isset($data['header']) ? $data['header'] : $this->header;
105 unset($data['header']);
107 if (is_array($header)) {
108 foreach ($header as $h) {
116 if (isset($data['pointer'])) {
117 rewind($data['pointer']);
118 fpassthru($data['pointer']);
119 if (!empty($data['volume'])) {
120 $data['volume']->close($data['pointer'], $data['info']['hash']);
124 if (!empty($data['raw']) && !empty($data['error'])) {
125 exit($data['error']);
127 exit(json_encode($data));