示例#1
0
 /**
  * Exec command and return result
  *
  * @param  string  $cmd  command name
  * @param  array   $args command arguments
  * @return array
  * @author Dmitry (dio) Levashov
  **/
 public function exec($cmd, $args)
 {
     // set error handler of WARNING, NOTICE
     set_error_handler('elFinder::phpErrorHandler', E_WARNING | E_NOTICE | E_USER_WARNING | E_USER_NOTICE);
     if (!$this->loaded) {
         return array('error' => $this->error(self::ERROR_CONF, self::ERROR_CONF_NO_VOL));
     }
     if ($this->session_expires()) {
         return array('error' => $this->error(self::ERROR_SESSION_EXPIRES));
     }
     if (!$this->commandExists($cmd)) {
         return array('error' => $this->error(self::ERROR_UNKNOWN_CMD));
     }
     if (!empty($args['mimes']) && is_array($args['mimes'])) {
         foreach ($this->volumes as $id => $v) {
             $this->volumes[$id]->setMimesFilter($args['mimes']);
         }
     }
     // detect destination dirHash and volume
     $dstVolume = false;
     $dst = !empty($args['target']) ? $args['target'] : (!empty($args['dst']) ? $args['dst'] : '');
     if ($dst) {
         $dstVolume = $this->volume($dst);
     } else {
         if (isset($args['targets']) && is_array($args['targets']) && isset($args['targets'][0])) {
             $dst = $args['targets'][0];
             $dstVolume = $this->volume($dst);
             if (($_stat = $dstVolume->file($dst)) && !empty($_stat['phash'])) {
                 $dst = $_stat['phash'];
             } else {
                 $dst = '';
             }
         }
     }
     $result = null;
     // call pre handlers for this command
     $args['sessionCloseEarlier'] = isset($this->sessionUseCmds[$cmd]) ? false : $this->sessionCloseEarlier;
     if (!empty($this->listeners[$cmd . '.pre'])) {
         $_break = false;
         foreach ($this->listeners[$cmd . '.pre'] as $handler) {
             $_res = call_user_func_array($handler, array($cmd, &$args, $this, $dstVolume));
             if (is_array($_res)) {
                 if (!empty($_res['preventexec'])) {
                     $result = array('error' => true);
                     if ($cmd === 'upload' & !empty($args['node'])) {
                         $result['callback'] = array('node' => $args['node'], 'bind' => $cmd);
                     }
                     if (!empty($_res['results']) && is_array($_res['results'])) {
                         $result = array_merge($result, $_res['results']);
                     }
                     break;
                 }
             }
         }
     }
     // unlock session data for multiple access
     if ($this->sessionCloseEarlier && $args['sessionCloseEarlier']) {
         $this->session->close();
         // deprecated property
         elFinder::$sessionClosed = true;
     }
     if (substr(PHP_OS, 0, 3) === 'WIN') {
         // set time out
         elFinder::extendTimeLimit(300);
     }
     if (!is_array($result)) {
         try {
             $result = $this->{$cmd}($args);
         } catch (Exception $e) {
             $result = array('error' => htmlspecialchars($e->getMessage()), 'sync' => true);
         }
     }
     // check change dstDir
     $changeDst = false;
     if ($dst && $dstVolume && (!empty($result['added']) || !empty($result['removed']))) {
         $changeDst = true;
     }
     foreach ($this->volumes as $volume) {
         $removed = $volume->removed();
         if (!empty($removed)) {
             if (!isset($result['removed'])) {
                 $result['removed'] = array();
             }
             $result['removed'] = array_merge($result['removed'], $removed);
             if (!$changeDst && $dst && $dstVolume && $volume === $dstVolume) {
                 $changeDst = true;
             }
         }
         $added = $volume->added();
         if (!empty($added)) {
             if (!isset($result['added'])) {
                 $result['added'] = array();
             }
             $result['added'] = array_merge($result['added'], $added);
             if (!$changeDst && $dst && $dstVolume && $volume === $dstVolume) {
                 $changeDst = true;
             }
         }
         $volume->resetResultStat();
     }
     // dstDir is changed
     if ($changeDst) {
         if ($dstDir = $dstVolume->dir($dst)) {
             if (!isset($result['changed'])) {
                 $result['changed'] = array();
             }
             $result['changed'][] = $dstDir;
         }
     }
     // call handlers for this command
     if (!empty($this->listeners[$cmd])) {
         foreach ($this->listeners[$cmd] as $handler) {
             if (call_user_func_array($handler, array($cmd, &$result, $args, $this, $dstVolume))) {
                 // handler return true to force sync client after command completed
                 $result['sync'] = true;
             }
         }
     }
     // replace removed files info with removed files hashes
     if (!empty($result['removed'])) {
         $removed = array();
         foreach ($result['removed'] as $file) {
             $removed[] = $file['hash'];
         }
         $result['removed'] = array_unique($removed);
     }
     // remove hidden files and filter files by mimetypes
     if (!empty($result['added'])) {
         $result['added'] = $this->filter($result['added']);
     }
     // remove hidden files and filter files by mimetypes
     if (!empty($result['changed'])) {
         $result['changed'] = $this->filter($result['changed']);
     }
     if ($this->debug || !empty($args['debug'])) {
         $result['debug'] = array('connector' => 'php', 'phpver' => PHP_VERSION, 'time' => $this->utime() - $this->time, 'memory' => (function_exists('memory_get_peak_usage') ? ceil(memory_get_peak_usage() / 1024) . 'Kb / ' : '') . ceil(memory_get_usage() / 1024) . 'Kb / ' . ini_get('memory_limit'), 'upload' => $this->uploadDebug, 'volumes' => array(), 'mountErrors' => $this->mountErrors, 'phpErrors' => elFinder::$phpErrors);
         elFinder::$phpErrors = array();
         foreach ($this->volumes as $id => $volume) {
             $result['debug']['volumes'][] = $volume->debug();
         }
     }
     foreach ($this->volumes as $volume) {
         $volume->umount();
         $volume->__destruct();
     }
     if (!empty($result['callback'])) {
         $result['callback']['json'] = json_encode($result);
         $this->callback($result['callback']);
     } else {
         return $result;
     }
     //TODO: Add return statement here
 }