PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/admin/scripts/ckeditor/filemanager/connectors/php/plugins/rsc/filemanager.rsc.class.php

https://github.com/khazra/terra
PHP | 384 lines | 345 code | 18 blank | 21 comment | 65 complexity | a83e436c4e9dea035f655775ae724eaf MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * Filemanager PHP RSC plugin class
  4. *
  5. * filemanager.rsc.class.php
  6. * class for the filemanager.php connector which utilizes the Rackspace Cloud Files API
  7. * instead of the local filesystem
  8. *
  9. * @license MIT License
  10. * @author Alan Blount <alan (at) zeroasterisk (dot) com>
  11. * @author Riaan Los <mail (at) riaanlos (dot) nl>
  12. * @author Simon Georget <simon (at) linea21 (dot) com>
  13. * @copyright Authors
  14. */
  15. class FilemanagerRSC extends Filemanager {
  16. public function __construct($config) {
  17. $return = parent::__construct($config);
  18. require_once('cloudfiles.php');
  19. $auth = new CF_Authentication($this->config['rsc-username'], $this->config['rsc-apikey']);
  20. $auth->authenticate();
  21. $this->conn = new CF_Connection($auth);
  22. if ($this->config['rsc-ssl_use_cabundle']) {
  23. $this->conn->ssl_use_cabundle();
  24. }
  25. return $return;
  26. }
  27. public function getinfo() {
  28. $object = $this->get_object();
  29. if (isset($object->name)) {
  30. $object = $this->get_file_info(&$object);
  31. return array(
  32. 'Path' => $object->path,
  33. 'Filename' => $object->name,
  34. 'File Type' => $object->filetype,
  35. 'Preview' => $object->preview,
  36. 'Properties'=> $object->properties,
  37. 'Error' => "",
  38. 'Code' => 0
  39. );
  40. }
  41. $container = $this->get_container();
  42. if (isset($container->name)) {
  43. return array(
  44. 'Path' => $container->path,
  45. 'Filename' => $container->name,
  46. 'File Type' => 'dir',
  47. 'Preview' => $this->config['icons']['path'] . $this->config['icons']['directory'],
  48. 'Properties'=>array(
  49. 'Date Created'=>null,
  50. 'Date Modified'=>null,
  51. 'Height'=>null,
  52. 'Width'=>null,
  53. 'Size'=>null
  54. ),
  55. 'Error' => "",
  56. 'Code' => 0
  57. );
  58. }
  59. return array();
  60. }
  61. public function getfolder() {
  62. $container = trim($this->get['path'], '/ ');
  63. $containerParts = explode('/', $container);
  64. if ($containerParts[0]=='containers') {
  65. array_shift($containerParts);
  66. }
  67. $array = array();
  68. if (empty($containerParts) || trim($this->get['path'], '/ ')=='containers') {
  69. $containers = $this->conn->list_containers();
  70. $containers = array_diff($containers, $this->config['unallowed_dirs']);
  71. foreach ( $containers as $container ) {
  72. $array['/containers/' . $container . '/'] = array(
  73. 'Path'=> '/containers/' . $container .'/',
  74. 'Filename'=> $container,
  75. 'File Type'=>'dir',
  76. 'Preview'=> $this->config['icons']['path'] . $this->config['icons']['directory'],
  77. 'Properties'=>array(
  78. 'Date Created'=>null,
  79. 'Date Modified'=>null,
  80. 'Height'=>null,
  81. 'Width'=>null,
  82. 'Size'=>null
  83. ),
  84. 'Error'=>"",
  85. 'Code'=>0
  86. );
  87. }
  88. } else {
  89. $container = array_shift($containerParts);
  90. $limit = 0;
  91. $marker = null; // last record returned from a dataset
  92. $prefix = null; // search term (starts with)
  93. $path = null; // pseudo-hierarchical containers
  94. if (!empty($containerParts)) {
  95. $path = implode('/', $containerParts);
  96. }
  97. $container = $this->conn->get_container($container);
  98. //$list = $container->list_objects($limit, $marker, $prefix, $path);
  99. $objects = $container->get_objects($limit, $marker, $prefix, $path);
  100. foreach ( $objects as $object ) {
  101. if(!isset($this->params['type']) || (isset($this->params['type']) && strtolower($this->params['type'])=='images' && in_array(strtolower($object->content_type),$this->config['images']))) {
  102. if($this->config['upload']['imagesonly']== false || ($this->config['upload']['imagesonly']== true && in_array(strtolower($object->content_type),$this->config['images']))) {
  103. $object = $this->get_file_info(&$object);
  104. $array[$object->url] = array(
  105. 'Path'=> $object->path,
  106. 'Filename' => $object->name,
  107. 'File Type' => $object->filetype,
  108. 'Mime Type' => $object->content_type,
  109. 'Preview' => $object->preview,
  110. 'Properties' => $object->properties,
  111. 'Error' => "",
  112. 'Code' => 0
  113. );
  114. }
  115. }
  116. }
  117. }
  118. return $array;
  119. }
  120. public function rename() {
  121. // keep old filename, if missing from new
  122. $newNameParts = explode('.', $this->get['new']);
  123. $newNameExt = $newNameParts[(count($newNameParts)-1)];
  124. if (strlen($newNameExt) > 5 || count($newNameParts)==1) {
  125. $this->get['new'].='.'.array_pop(explode('.', $this->get['old']));
  126. }
  127. // get old
  128. $object = $this->get_object($this->get['old']);
  129. if (!isset($object->container)) {
  130. $this->error(sprintf($this->lang('FILE_DOES_NOT_EXIST'),$path));
  131. }
  132. if (in_array($this->get['new'], $object->container->list_objects())) {
  133. $this->error(sprintf($this->lang('FILE_ALREADY_EXISTS'),$this->get['new']));
  134. return false;
  135. }
  136. // create to new
  137. $new = $object->container->create_object($this->get['new']);
  138. $new->content_type = $object->content_type;
  139. $data = $object->read();
  140. $new->write($data);
  141. if (!empty($object->metadata)) {
  142. $new->metadata = $object->metadata;
  143. $object->sync_metadata(); // save back to RSC
  144. }
  145. $object->container->delete_object($object->name);
  146. $array = array(
  147. 'Error'=>"",
  148. 'Code'=>0,
  149. 'Old Path'=>$object->path,
  150. 'Old Name'=>$object->name,
  151. 'New Path'=>$new->path,
  152. 'New Name'=>$new->name
  153. );
  154. return $array;
  155. }
  156. public function delete() {
  157. $object = $this->get_object();
  158. if (isset($object->name)) {
  159. $object->container->delete_object($object->name);
  160. return array(
  161. 'Error'=>"",
  162. 'Code'=>0,
  163. 'Path'=>$this->get['path']
  164. );
  165. }
  166. $container = $this->get_container();
  167. if (isset($container->name)) {
  168. $list = $container->list_objects(5);
  169. if (!empty($list)) {
  170. $this->error("Unable to Delete Container, it is not empty.");
  171. return false;
  172. }
  173. $this->conn->delete_container($container->name);
  174. return array(
  175. 'Error'=>"",
  176. 'Code'=>0,
  177. 'Path'=>$this->get['path']
  178. );
  179. }
  180. $this->error(sprintf($this->lang('INVALID_DIRECTORY_OR_FILE')));
  181. }
  182. public function add() {
  183. $this->setParams();
  184. if(!isset($_FILES['newfile']) || !is_uploaded_file($_FILES['newfile']['tmp_name'])) {
  185. $this->error(sprintf($this->lang('INVALID_FILE_UPLOAD')),true);
  186. }
  187. if(($this->config['upload']['size']!=false && is_numeric($this->config['upload']['size'])) && ($_FILES['newfile']['size'] > ($this->config['upload']['size'] * 1024 * 1024))) {
  188. $this->error(sprintf($this->lang('UPLOAD_FILES_SMALLER_THAN'),$this->config['upload']['size'] . 'Mb'),true);
  189. }
  190. $size = @getimagesize($_FILES['newfile']['tmp_name']);
  191. if($this->config['upload']['imagesonly'] || (isset($this->params['type']) && strtolower($this->params['type'])=='images')) {
  192. if(empty($size) || !is_array($size)) {
  193. $this->error(sprintf($this->lang('UPLOAD_IMAGES_ONLY')),true);
  194. }
  195. if(!in_array($size[2], array(1, 2, 3, 7, 8))) {
  196. $this->error(sprintf($this->lang('UPLOAD_IMAGES_TYPE_JPEG_GIF_PNG')),true);
  197. }
  198. }
  199. $_FILES['newfile']['name'] = $this->cleanString($_FILES['newfile']['name'],array('.','-'));
  200. $container = $this->get_container($this->post['currentpath']);
  201. if(!$this->config['upload']['overwrite']) {
  202. $list = $container->list_objects();
  203. $i = 0;
  204. while (in_array($_FILES['newfile']['name'], $list)) {
  205. $i++;
  206. $parts = explode('.', $_FILES['newfile']['name']);
  207. $ext = array_pop($parts);
  208. $parts = array_diff($parts, array("copy{$i}", "copy".($i-1)));
  209. $parts[] = "copy{$i}";
  210. $parts[] = $ext;
  211. $_FILES['newfile']['name'] = implode('.', $parts);
  212. }
  213. }
  214. $object = $container->create_object($_FILES['newfile']['name']);
  215. $object->load_from_filename($_FILES['newfile']['tmp_name']);
  216. // set image details
  217. if (is_array($size) && count($size) > 1) {
  218. $object->metadata->height = $object->height = $size[1];
  219. $object->metadata->width = $object->width = $size[0];
  220. $object->sync_metadata(); // save back to RSC
  221. }
  222. unlink($_FILES['newfile']['tmp_name']);
  223. $response = array(
  224. 'Path'=>$this->post['currentpath'],
  225. 'Name'=>$_FILES['newfile']['name'],
  226. 'Error'=>"",
  227. 'Code'=>0
  228. );
  229. echo '<textarea>' . json_encode($response) . '</textarea>';
  230. die();
  231. }
  232. public function addfolder() {
  233. $container = trim($this->get['path'], '/ ');
  234. $containerParts = explode('/', $container);
  235. if ($containerParts[0]=='containers') {
  236. array_shift($containerParts);
  237. }
  238. if (!empty($containerParts)) {
  239. $this->error(sprintf($this->lang('UNABLE_TO_CREATE_DIRECTORY'),$newdir));
  240. }
  241. $newdir = $this->cleanString($this->get['name']);
  242. $container = $this->conn->create_container($newdir);
  243. $container->make_public(86400/2);
  244. return array(
  245. 'Parent' => "/containers/{$container->name}",
  246. 'Name' => $container->name,
  247. 'Error'=>"",
  248. 'Code'=>0
  249. );
  250. }
  251. public function download() {
  252. $object = $this->get_object();
  253. if (isset($object->name)) {
  254. header("Content-type: application/force-download");
  255. header('Content-Disposition: inline; filename="' . $object->name . '"');
  256. header("Content-Type: " . $doc->content_type);
  257. $output = fopen("php://output", "w");
  258. $object->stream($output); # stream object content to PHP's output buffer
  259. fclose($output);
  260. return true;
  261. }
  262. $this->error(sprintf($this->lang('FILE_DOES_NOT_EXIST'),$this->get['path']));
  263. }
  264. public function preview() {
  265. if(isset($this->get['path']) && file_exists($this->doc_root . $this->get['path'])) {
  266. header("Content-type: image/" .$ext = pathinfo($this->get['path'], PATHINFO_EXTENSION));
  267. header("Content-Transfer-Encoding: Binary");
  268. header("Content-length: ".filesize($this->doc_root . $this->get['path']));
  269. header('Content-Disposition: inline; filename="' . basename($this->get['path']) . '"');
  270. readfile($this->doc_root . $this->get['path']);
  271. } else {
  272. $this->error(sprintf($this->lang('FILE_DOES_NOT_EXIST'),$this->get['path']));
  273. }
  274. }
  275. private function get_container($path=null, $showError=false) {
  276. if (empty($path)) {
  277. $path = $this->get['path'];
  278. }
  279. $container = trim($path, '/ ');
  280. $containerParts = explode('/', $container);
  281. if ($containerParts[0]=='containers') {
  282. array_shift($containerParts);
  283. }
  284. $array = array();
  285. if (count($containerParts) > 0) {
  286. $container = $this->conn->get_container(array_shift($containerParts));
  287. if (isset($container->name)) {
  288. $container->path = '/containers/'.$container->name;
  289. return $container;
  290. }
  291. }
  292. if ($showError) {
  293. $this->error(sprintf($this->lang('FILE_DOES_NOT_EXIST'),$path));
  294. }
  295. return false;
  296. }
  297. private function get_object($path=null, $showError=false) {
  298. if (empty($path)) {
  299. $path = $this->get['path'];
  300. }
  301. $container = trim($path, '/ ');
  302. $containerParts = explode('/', $container);
  303. if ($containerParts[0]=='containers') {
  304. array_shift($containerParts);
  305. }
  306. $array = array();
  307. if (count($containerParts) > 1) {
  308. $container = $this->conn->get_container(array_shift($containerParts));
  309. $object = $container->get_object(array_shift($containerParts));
  310. if (isset($object->name) && isset($object->container->name)) {
  311. $object->path = '/containers/'.$object->container->name.'/'.$object->name;
  312. return $object;
  313. }
  314. }
  315. if ($showError) {
  316. $this->error(sprintf($this->lang('FILE_DOES_NOT_EXIST'),$path));
  317. }
  318. return false;
  319. }
  320. private function get_file_info($object=null) {
  321. if (empty($object) || !is_object($object)) {
  322. return null;
  323. }
  324. // parse into file extension types
  325. //$object->filetype = array_pop(explode('/', $object->content_type));
  326. $object->filetype = array_pop(explode('.', $object->name));
  327. // setup values
  328. $object->height = null;
  329. $object->width = null;
  330. if (isset($object->metadata->height)) {
  331. $object->height = $object->metadata->height;
  332. }
  333. if (isset($object->metadata->width)) {
  334. $object->width = $object->metadata->width;
  335. }
  336. $preview = $this->config['icons']['path'] . $this->config['icons']['default'];
  337. if(file_exists($this->root . $this->config['icons']['path'] . strtolower($object->filetype) . '.png')) {
  338. $preview = $this->config['icons']['path'] . strtolower($object->filetype) . '.png';
  339. }
  340. if (in_array(strtolower($object->filetype), $this->config['images'])) {
  341. $preview = $object->container->cdn_uri . '/' . $object->name;
  342. if (empty($object->height) && empty($object->width) && isset($this->config['rsc-getsize']) && !empty($get['rsc-getsize'])) {
  343. list($width, $height, $type, $attr) = getimagesize($this->doc_root . $path);
  344. $object->metadata->height = $object->height = $height;
  345. $object->metadata->width = $object->width = $width;
  346. $object->sync_metadata(); // save back to RSC
  347. }
  348. }
  349. $object->filename = $object->name;
  350. $object->path = '/containers/'.$object->container->name.'/'.$object->name;
  351. $object->url = $object->container->cdn_uri . '/' . $object->name;
  352. $object->mimetype = $object->content_type;
  353. $object->filemtime = $object->last_modified;
  354. $object->preview = $preview;
  355. $object->size = $object->content_length;
  356. $object->date = date($this->config['date'], strtotime($object->last_modified));
  357. $object->properties = array(
  358. 'Date Modified' => $object->date,
  359. 'Size' => $object->size,
  360. 'Height' => $object->height,
  361. 'Width' => $object->width,
  362. );
  363. return $object;
  364. }
  365. }
  366. ?>