OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / scripts / elfinder-2.0-rc1 / php / elFinderConnector.class.php
1 <?php
2
3 /**
4  * Default elFinder connector
5  *
6  * @author Dmitry (dio) Levashov
7  **/
8 class elFinderConnector {
9         /**
10          * elFinder instance
11          *
12          * @var elFinder
13          **/
14         protected $elFinder;
15         
16         /**
17          * Options
18          *
19          * @var aray
20          **/
21         protected $options = array();
22         
23         /**
24          * undocumented class variable
25          *
26          * @var string
27          **/
28         protected $header = 'Content-Type: application/json';
29         
30         
31         /**
32          * Constructor
33          *
34          * @return void
35          * @author Dmitry (dio) Levashov
36          **/
37         public function __construct($elFinder, $debug=false) {
38                 
39                 $this->elFinder = $elFinder;
40                 if ($debug) {
41                         $this->header = 'Content-Type: text/html; charset=utf-8';
42                 }
43         }
44         
45         /**
46          * Execute elFinder command and output result
47          *
48          * @return void
49          * @author Dmitry (dio) Levashov
50          **/
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'] : '';
55                 $args   = array();
56                 
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));
60                 }
61                 
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));
64                 }
65                 
66                 // telepat_mode: on
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'));
69                 }
70                 // telepat_mode: off
71                 
72                 if (!$this->elFinder->commandExists($cmd)) {
73                         $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UNKNOWN_CMD)));
74                 }
75                 
76                 // collect required arguments to exec command
77                 foreach ($this->elFinder->commandArgsList($cmd) as $name => $req) {
78                         $arg = $name == 'FILES' 
79                                 ? $_FILES 
80                                 : (isset($src[$name]) ? $src[$name] : '');
81                                 
82                         if (!is_array($arg)) {
83                                 $arg = trim($arg);
84                         }
85                         if ($req && (!isset($arg) || $arg === '')) {
86                                 $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_INV_PARAMS, $cmd)));
87                         }
88                         $args[$name] = $arg;
89                 }
90                 
91                 $args['debug'] = isset($src['debug']) ? !!$src['debug'] : false;
92                 
93                 $this->output($this->elFinder->exec($cmd, $args));
94         }
95         
96         /**
97          * Output json
98          *
99          * @param  array  data to output
100          * @return void
101          * @author Dmitry (dio) Levashov
102          **/
103         protected function output(array $data) {
104                 $header = isset($data['header']) ? $data['header'] : $this->header;
105                 unset($data['header']);
106                 if ($header) {
107                         if (is_array($header)) {
108                                 foreach ($header as $h) {
109                                         header($h);
110                                 }
111                         } else {
112                                 header($header);
113                         }
114                 }
115                 
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']);
121                         }
122                         exit();
123                 } else {
124                         if (!empty($data['raw']) && !empty($data['error'])) {
125                                 exit($data['error']);
126                         } else {
127                                 exit(json_encode($data));
128                         }
129                 }
130                 
131         }
132         
133 }// END class