PageRenderTime 52ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/elfinder/php/elFinderConnector.class.php

https://bitbucket.org/rohitrox/hotc
PHP | 133 lines | 72 code | 19 blank | 42 comment | 22 complexity | 95927d2aac633aee4fb7c9bc5e5712cc MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Default elFinder connector
  4. *
  5. * @author Dmitry (dio) Levashov
  6. **/
  7. class elFinderConnector {
  8. /**
  9. * elFinder instance
  10. *
  11. * @var elFinder
  12. **/
  13. protected $elFinder;
  14. /**
  15. * Options
  16. *
  17. * @var aray
  18. **/
  19. protected $options = array();
  20. /**
  21. * undocumented class variable
  22. *
  23. * @var string
  24. **/
  25. protected $header = 'Content-Type: application/json';
  26. /**
  27. * Constructor
  28. *
  29. * @return void
  30. * @author Dmitry (dio) Levashov
  31. **/
  32. public function __construct($elFinder, $debug=false) {
  33. $this->elFinder = $elFinder;
  34. if ($debug) {
  35. $this->header = 'Content-Type: text/html; charset=utf-8';
  36. }
  37. }
  38. /**
  39. * Execute elFinder command and output result
  40. *
  41. * @return void
  42. * @author Dmitry (dio) Levashov
  43. **/
  44. public function run() {
  45. $isPost = $_SERVER["REQUEST_METHOD"] == 'POST';
  46. $src = $_SERVER["REQUEST_METHOD"] == 'POST' ? $_POST : $_GET;
  47. $cmd = isset($src['cmd']) ? $src['cmd'] : '';
  48. $args = array();
  49. if (!function_exists('json_encode')) {
  50. $error = $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_JSON);
  51. $this->output(array('error' => '{"error":["'.implode('","', $error).'"]}', 'raw' => true));
  52. }
  53. if (!$this->elFinder->loaded()) {
  54. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_VOL), 'debug' => $this->elFinder->mountErrors));
  55. }
  56. // telepat_mode: on
  57. if (!$cmd && $isPost) {
  58. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UPLOAD, elFinder::ERROR_UPLOAD_TOTAL_SIZE), 'header' => 'Content-Type: text/html'));
  59. }
  60. // telepat_mode: off
  61. if (!$this->elFinder->commandExists($cmd)) {
  62. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UNKNOWN_CMD)));
  63. }
  64. // collect required arguments to exec command
  65. foreach ($this->elFinder->commandArgsList($cmd) as $name => $req) {
  66. $arg = $name == 'FILES'
  67. ? $_FILES
  68. : (isset($src[$name]) ? $src[$name] : '');
  69. if (!is_array($arg)) {
  70. $arg = trim($arg);
  71. }
  72. if ($req && (!isset($arg) || $arg === '')) {
  73. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_INV_PARAMS, $cmd)));
  74. }
  75. $args[$name] = $arg;
  76. }
  77. $args['debug'] = isset($src['debug']) ? !!$src['debug'] : false;
  78. $this->output($this->elFinder->exec($cmd, $args));
  79. }
  80. /**
  81. * Output json
  82. *
  83. * @param array data to output
  84. * @return void
  85. * @author Dmitry (dio) Levashov
  86. **/
  87. protected function output(array $data) {
  88. $header = isset($data['header']) ? $data['header'] : $this->header;
  89. unset($data['header']);
  90. if ($header) {
  91. if (is_array($header)) {
  92. foreach ($header as $h) {
  93. header($h);
  94. }
  95. } else {
  96. header($header);
  97. }
  98. }
  99. if (isset($data['pointer'])) {
  100. rewind($data['pointer']);
  101. fpassthru($data['pointer']);
  102. if (!empty($data['volume'])) {
  103. $data['volume']->close($data['pointer'], $data['info']['hash']);
  104. }
  105. exit();
  106. } else {
  107. if (!empty($data['raw']) && !empty($data['error'])) {
  108. exit($data['error']);
  109. } else {
  110. exit(json_encode($data));
  111. }
  112. }
  113. }
  114. }// END class