PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_k2/lib/elfinder/elFinderConnector.class.php

http://getk2.googlecode.com/
PHP | 149 lines | 76 code | 22 blank | 51 comment | 22 complexity | 9ebc4f2b7a31d97146fcdce56001f56e MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: elFinderConnector.class.php 1492 2012-02-22 17:40:09Z joomlaworks@gmail.com $
  4. * @package K2
  5. * @author JoomlaWorks http://www.joomlaworks.net
  6. * @copyright Copyright (c) 2006 - 2012 JoomlaWorks Ltd. All rights reserved.
  7. * @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die('Restricted access');
  11. ob_start();
  12. // mb_internal_encoding("UTF-8");
  13. /**
  14. * Default elFinder connector
  15. *
  16. * @author Dmitry (dio) Levashov
  17. **/
  18. class elFinderConnector {
  19. /**
  20. * elFinder instance
  21. *
  22. * @var elFinder
  23. **/
  24. protected $elFinder;
  25. /**
  26. * Options
  27. *
  28. * @var aray
  29. **/
  30. protected $options = array();
  31. /**
  32. * undocumented class variable
  33. *
  34. * @var string
  35. **/
  36. protected $header = 'Content-Type: application/json';
  37. /**
  38. * Constructor
  39. *
  40. * @return void
  41. * @author Dmitry (dio) Levashov
  42. **/
  43. public function __construct($elFinder, $debug=false) {
  44. $this->elFinder = $elFinder;
  45. if ($debug) {
  46. $this->header = 'Content-Type: text/html; charset=utf-8';
  47. }
  48. }
  49. /**
  50. * Execute elFinder command and output result
  51. *
  52. * @return void
  53. * @author Dmitry (dio) Levashov
  54. **/
  55. public function run() {
  56. $isPost = $_SERVER["REQUEST_METHOD"] == 'POST';
  57. $src = $_SERVER["REQUEST_METHOD"] == 'POST' ? $_POST : $_GET;
  58. $cmd = isset($src['cmd']) ? $src['cmd'] : '';
  59. $args = array();
  60. if (!function_exists('json_encode')) {
  61. $error = $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_JSON);
  62. $this->output(array('error' => '{"error":["'.implode('","', $error).'"]}', 'raw' => true));
  63. }
  64. if (!$this->elFinder->loaded()) {
  65. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_CONF, elFinder::ERROR_CONF_NO_VOL), 'debug' => $this->elFinder->mountErrors));
  66. }
  67. // telepat_mode: on
  68. if (!$cmd && $isPost) {
  69. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UPLOAD, elFinder::ERROR_UPLOAD_TOTAL_SIZE), 'header' => 'Content-Type: text/html'));
  70. }
  71. // telepat_mode: off
  72. if (!$this->elFinder->commandExists($cmd)) {
  73. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_UNKNOWN_CMD)));
  74. }
  75. // collect required arguments to exec command
  76. foreach ($this->elFinder->commandArgsList($cmd) as $name => $req) {
  77. $arg = $name == 'FILES'
  78. ? $_FILES
  79. : (isset($src[$name]) ? $src[$name] : '');
  80. if (!is_array($arg)) {
  81. $arg = trim($arg);
  82. }
  83. if ($req && (!isset($arg) || $arg === '')) {
  84. $this->output(array('error' => $this->elFinder->error(elFinder::ERROR_INV_PARAMS, $cmd)));
  85. }
  86. $args[$name] = $arg;
  87. }
  88. $args['debug'] = isset($src['debug']) ? !!$src['debug'] : false;
  89. $this->output($this->elFinder->exec($cmd, $args));
  90. }
  91. /**
  92. * Output json
  93. *
  94. * @param array data to output
  95. * @return void
  96. * @author Dmitry (dio) Levashov
  97. **/
  98. protected function output(array $data) {
  99. $header = isset($data['header']) ? $data['header'] : $this->header;
  100. unset($data['header']);
  101. ob_end_clean();
  102. if ($header) {
  103. if (is_array($header)) {
  104. foreach ($header as $h) {
  105. header($h);
  106. }
  107. } else {
  108. header($header);
  109. }
  110. }
  111. if (isset($data['pointer'])) {
  112. rewind($data['pointer']);
  113. fpassthru($data['pointer']);
  114. if (!empty($data['volume'])) {
  115. $data['volume']->close($data['pointer'], $data['info']['hash']);
  116. }
  117. exit();
  118. } else {
  119. if (!empty($data['raw']) && !empty($data['error'])) {
  120. exit($data['error']);
  121. } else {
  122. exit(json_encode($data));
  123. }
  124. }
  125. }
  126. }// END class
  127. ?>