elfinder.drupal.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * elFinder conenctor class
  6. */
  7. class elFinderDrupal extends elFinder {
  8. public function __construct($opts) {
  9. if (is_callable(array($this, 'version'))) {
  10. $ver = floatval($this->version());
  11. if (isset(elFinder::$ApiVersion)) {
  12. $ver = elFinder::$ApiVersion;
  13. }
  14. if ($ver == 2.1 && is_callable(array($this, 'revision')) && $this->revision() < 37) {
  15. $this->connector_unsupported_error();
  16. }
  17. parent::__construct($opts);
  18. $this->commands['desc'] = array('target' => TRUE, 'content' => FALSE);
  19. $this->commands['owner'] = array('target' => TRUE, 'content' => FALSE);
  20. $this->commands['downloadcount'] = array('target' => TRUE);
  21. } else {
  22. $this->connector_unsupported_error();
  23. }
  24. }
  25. public function connector_unsupported_error() {
  26. $this->connector_error(t('Unsupported elFinder library version. Please upgrade.'));
  27. }
  28. public function connector_error($message) {
  29. exit(drupal_json_encode(array('error' => array(strip_tags($message)))));
  30. }
  31. /* Overriding search query argument name 'q' since it's already used in Drupal */
  32. public function commandArgsList($cmd) {
  33. $this->commands['search']['elfinder_search_q'] = TRUE;
  34. return $this->commandExists($cmd) ? $this->commands[$cmd] : array();
  35. }
  36. protected function search($args) {
  37. $q = trim($args['elfinder_search_q']);
  38. $mimes = !empty($args['mimes']) && is_array($args['mimes']) ? $args['mimes'] : array();
  39. $target = !empty($args['target']) ? $args['target'] : null;
  40. $result = array();
  41. $errors = array();
  42. if ($target) {
  43. if ($volume = $this->volume($target)) {
  44. $result = $volume->search($q, $mimes, $target);
  45. $errors = array_merge($errors, $volume->error());
  46. }
  47. } else {
  48. foreach ($this->volumes as $volume) {
  49. $result = array_merge($result, $volume->search($q, $mimes));
  50. $errors = array_merge($errors, $volume->error());
  51. }
  52. }
  53. // Exclude hidden folders from search results.
  54. $hidden_folders = variable_get('elfinder_settings_misc_hidden_folders', '');
  55. if($hidden_folders) {
  56. $hidden_folders = explode(',', $hidden_folders);
  57. foreach($result AS $key => $file) {
  58. $parts = explode('/', $file['path']);
  59. if(in_array($parts[1], $hidden_folders)) {
  60. unset($result[$key]);
  61. }
  62. }
  63. }
  64. $result = array('files' => $result);
  65. if ($errors) {
  66. $result['warning'] = $errors;
  67. }
  68. return $result;
  69. }
  70. protected function desc($args) {
  71. $target = $args['target'];
  72. $desc = $args['content'];
  73. $error = array(self::ERROR_UNKNOWN, '#' . $target);
  74. if (($volume = $this->volume($target)) == FALSE
  75. || ($file = $volume->file($target)) == FALSE) {
  76. return array('error' => $this->error($error, self::ERROR_FILE_NOT_FOUND));
  77. }
  78. $error[1] = $file['name'];
  79. if ($volume->driverId() == 'f') {
  80. return array('desc' => '');
  81. }
  82. if ($volume->commandDisabled('desc')) {
  83. return array('error' => $this->error($error, self::ERROR_ACCESS_DENIED));
  84. }
  85. if (($desc = $volume->desc($target, $desc)) == -1) {
  86. return array('error' => $this->error($error, $volume->error()));
  87. }
  88. return array('desc' => $desc);
  89. }
  90. protected function owner($args) {
  91. $target = $args['target'];
  92. $error = array(self::ERROR_UNKNOWN, '#' . $target);
  93. if (($volume = $this->volume($target)) == FALSE
  94. || ($file = $volume->file($target)) == FALSE) {
  95. return array('error' => $this->error($error, self::ERROR_FILE_NOT_FOUND));
  96. }
  97. $error[1] = $file['name'];
  98. if ($volume->driverId() == 'f') {
  99. return array('owner' => '');
  100. }
  101. if ($volume->commandDisabled('owner')) {
  102. return array('error' => $this->error($error, self::ERROR_ACCESS_DENIED));
  103. }
  104. if (($owner = $volume->owner($target)) == FALSE) {
  105. return array('error' => $this->error($error, $volume->error()));
  106. }
  107. return array('owner' => $owner);
  108. }
  109. protected function downloadcount($args) {
  110. $target = $args['target'];
  111. $error = array(self::ERROR_UNKNOWN, '#' . $target);
  112. if (($volume = $this->volume($target)) == FALSE
  113. || ($file = $volume->file($target)) == FALSE) {
  114. return array('error' => $this->error($error, self::ERROR_FILE_NOT_FOUND));
  115. }
  116. $error[1] = $file['name'];
  117. if ($volume->driverId() == 'f') {
  118. return array('downloadcount' => '');
  119. }
  120. if ($volume->commandDisabled('downloadcount')) {
  121. return array('error' => $this->error($error, self::ERROR_ACCESS_DENIED));
  122. }
  123. if (($downloadcount = $volume->downloadcount($target)) == FALSE) {
  124. return array('error' => $this->error($error, $volume->error()));
  125. }
  126. return array('downloadcount' => $downloadcount);
  127. }
  128. /**
  129. * Required to output file in browser when volume URL is not set
  130. * Return array contains opened file pointer, root itself and required headers
  131. *
  132. * @param array command arguments
  133. * @return array
  134. * @author Dmitry (dio) Levashov
  135. * */
  136. protected function file($args) {
  137. $target = $args['target'];
  138. $download = !empty($args['download']);
  139. $h403 = 'HTTP/1.x 403 Access Denied';
  140. $h404 = 'HTTP/1.x 404 Not Found';
  141. if (($volume = $this->volume($target)) == FALSE) {
  142. return array('error' => self::$errors[self::ERROR_FILE_NOT_FOUND], 'header' => $h404, 'raw' => TRUE);
  143. }
  144. if (($file = $volume->file($target)) == FALSE) {
  145. return array('error' => self::$errors[self::ERROR_FILE_NOT_FOUND], 'header' => $h404, 'raw' => TRUE);
  146. }
  147. if (!$file['read']) {
  148. return array('error' => self::$errors[self::ERROR_ACCESS_DENIED], 'header' => $h403, 'raw' => TRUE);
  149. }
  150. if ($volume->driverId() != 'f' && (($fp = $volume->open($target)) == FALSE)) {
  151. return array('error' => self::$errors[self::ERROR_FILE_NOT_FOUND], 'header' => $h404, 'raw' => TRUE);
  152. }
  153. $mime = ($download) ? 'application/octet-stream' : $file['mime'];
  154. $result = array(
  155. 'volume' => $volume,
  156. 'pointer' => $fp,
  157. 'info' => $file,
  158. 'header' => array(
  159. "Content-Type: " . $mime,
  160. "Content-Disposition: " . $this->GetContentDisposition($file['name'], $mime, $download),
  161. "Content-Location: " . $file['name'],
  162. 'Content-Transfer-Encoding: binary',
  163. "Content-Length: " . $file['size'],
  164. "Connection: close"
  165. )
  166. );
  167. $real_path = $this->realpath($target);
  168. module_invoke_all('file_download', $volume->drupalpathtouri($real_path));
  169. return $result;
  170. }
  171. /**
  172. * Generating Content-Disposition HTTP header
  173. *
  174. * @param string $file Filename
  175. * @param string $filemime MIME Type
  176. * @param bool $download Disposition type (true = download file, false = open file in browser)
  177. * @return string
  178. * @author Dmitry (dio) Levashov, Alexey Sukhotin
  179. * */
  180. public static function GetContentDisposition($file, $filemime, $download = FALSE) {
  181. $disp = '';
  182. $filename = $file;
  183. $ua = $_SERVER["HTTP_USER_AGENT"];
  184. $mime = $filemime;
  185. if ($download) {
  186. $disp = 'attachment';
  187. $mime = 'application/octet-stream';
  188. } else {
  189. $disp = preg_match('/^(image|text)/i', $mime)
  190. || $mime == 'application/x-shockwave-flash' ? 'inline' : 'attachment';
  191. }
  192. $disp .= '; ';
  193. if (preg_match("/MSIE ([0-9]{1,}[\.0-9]{0,})/", $ua)) {
  194. $filename = rawurlencode($filename);
  195. $filename = str_replace("+", "%20", $filename);
  196. //$filename = str_replace(" ", "%20", $filename);
  197. $disp .= "filename=" . $filename;
  198. } elseif (preg_match("/Firefox\/(\d+)/", $ua, $m)) {
  199. if ($m[1] >= 8) {
  200. $disp .= "filename*=?UTF-8''" . rawurlencode($filename);
  201. } else {
  202. $disp .= "filename*=\"?UTF-8''" . rawurlencode($filename) . "\";";
  203. }
  204. } else {
  205. $disp .= "filename=" . $filename;
  206. }
  207. return $disp;
  208. }
  209. }