modfilemediasource.class.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. require_once MODX_CORE_PATH . 'model/modx/sources/modmediasource.class.php';
  11. /**
  12. * Implements a file-system-based media source, allowing manipulation and management of files on the server's
  13. * location. Supports basePath and baseUrl parameters, similar to Revolution 2.1 and prior's filemanager_* settings.
  14. *
  15. * @package modx
  16. * @subpackage sources
  17. */
  18. class modFileMediaSource extends modMediaSource implements modMediaSourceInterface {
  19. /** @var modFileHandler */
  20. public $fileHandler;
  21. /**
  22. * {@inheritDoc}
  23. * @return boolean
  24. */
  25. public function initialize() {
  26. parent::initialize();
  27. $options = array();
  28. if (!$this->ctx) {
  29. $this->ctx =& $this->xpdo->context;
  30. }
  31. $options['context'] = $this->ctx->get('key');
  32. $this->fileHandler = $this->xpdo->getService('fileHandler','modFileHandler', '',$options);
  33. return true;
  34. }
  35. /**
  36. * Get base paths/urls and sanitize incoming paths
  37. *
  38. * @param string $path A path to the active directory
  39. * @return array
  40. */
  41. public function getBases($path = '') {
  42. $properties = $this->getProperties();
  43. $bases = array();
  44. $path = $this->fileHandler->sanitizePath($path);
  45. $bases['path'] = $properties['basePath']['value'];
  46. $bases['pathIsRelative'] = false;
  47. if (!empty($properties['basePathRelative']['value'])) {
  48. $realpath = realpath($this->ctx->getOption('base_path', MODX_BASE_PATH) . $bases['path']);
  49. $bases['pathAbsolute'] = ($realpath !== false) ? $realpath. '/' : '';
  50. $bases['pathIsRelative'] = true;
  51. } else {
  52. $bases['pathAbsolute'] = $bases['path'];
  53. }
  54. $bases['pathAbsoluteWithPath'] = $bases['pathAbsolute'].ltrim($path,'/');
  55. if (is_dir($bases['pathAbsoluteWithPath'])) {
  56. $bases['pathAbsoluteWithPath'] = $this->fileHandler->postfixSlash($bases['pathAbsoluteWithPath']);
  57. }
  58. $bases['pathRelative'] = ltrim($path,'/');
  59. /* get relative url */
  60. $bases['urlIsRelative'] = false;
  61. $bases['url'] = $properties['baseUrl']['value'];;
  62. if (!empty($properties['baseUrlRelative']['value'])) {
  63. $bases['urlAbsolute'] = $this->ctx->getOption('base_url',MODX_BASE_URL).$bases['url'];
  64. $bases['urlIsRelative'] = true;
  65. } else {
  66. $bases['urlAbsolute'] = $bases['url'];
  67. }
  68. $bases['urlAbsoluteWithPath'] = $bases['urlAbsolute'].ltrim($path,'/');
  69. $bases['urlRelative'] = ltrim($path,'/');
  70. return $bases;
  71. }
  72. /**
  73. * Get the ID of the edit file action
  74. *
  75. * @return boolean|int
  76. */
  77. public function getEditActionId() {
  78. return 'system/file/edit';
  79. }
  80. /**
  81. * Return an array of files and folders at this current level in the directory structure
  82. *
  83. * @param string $path
  84. * @return array
  85. */
  86. public function getContainerList($path) {
  87. $properties = $this->getPropertyList();
  88. $path = $this->fileHandler->postfixSlash($path);
  89. $bases = $this->getBases($path);
  90. if (empty($bases['pathAbsolute'])) return array();
  91. $fullPath = $bases['pathAbsolute'].ltrim($path,'/');
  92. $useMultibyte = $this->getOption('use_multibyte',$properties,false);
  93. $encoding = $this->getOption('modx_charset',$properties,'UTF-8');
  94. $hideFiles = !empty($properties['hideFiles']) && $properties['hideFiles'] != 'false' ? true : false;
  95. $hideTooltips = !empty($properties['hideTooltips']) && $properties['hideTooltips'] != 'false' ? true : false;
  96. $editAction = $this->getEditActionId();
  97. $imagesExts = $this->getOption('imageExtensions',$properties,'jpg,jpeg,png,gif,svg');
  98. $imagesExts = explode(',',$imagesExts);
  99. $skipFiles = $this->getOption('skipFiles',$properties,'.svn,.git,_notes,nbproject,.idea,.DS_Store');
  100. $skipFiles = explode(',',$skipFiles);
  101. if ($this->xpdo->getParser()) {
  102. $this->xpdo->parser->processElementTags('',$skipFiles,true,true);
  103. }
  104. $skipFiles[] = '.';
  105. $skipFiles[] = '..';
  106. $allowedExtensions = $this->getOption('allowedFileTypes', $properties, '');
  107. if (is_string($allowedExtensions)) {
  108. if (empty($allowedExtensions)) {
  109. $allowedExtensions = array();
  110. } else {
  111. $allowedExtensions = array_map("trim",explode(',', $allowedExtensions));
  112. }
  113. }
  114. $canSave = $this->checkPolicy('save');
  115. $canRemove = $this->checkPolicy('remove');
  116. $canCreate = $this->checkPolicy('create');
  117. $directories = array();
  118. $dirnames = array();
  119. $files = array();
  120. $filenames = array();
  121. if (!is_dir($fullPath)) return array();
  122. /* iterate through directories */
  123. /** @var DirectoryIterator $file */
  124. foreach (new DirectoryIterator($fullPath) as $file) {
  125. if (in_array($file,$skipFiles)) continue;
  126. if (!$file->isReadable()) continue;
  127. $fileName = $file->getFilename();
  128. if (in_array(trim($fileName,'/'),$skipFiles)) continue;
  129. if (in_array($fullPath.$fileName,$skipFiles)) continue;
  130. $filePathName = $file->getPathname();
  131. $octalPerms = substr(sprintf('%o', $file->getPerms()), -4);
  132. /* handle dirs */
  133. $cls = array();
  134. if ($file->isDir() && $this->hasPermission('directory_list')) {
  135. $cls[] = 'folder';
  136. if ($this->hasPermission('directory_chmod') && $canSave) $cls[] = 'pchmod';
  137. if ($this->hasPermission('directory_create') && $canCreate) $cls[] = 'pcreate';
  138. if ($this->hasPermission('directory_remove') && $canRemove) $cls[] = 'premove';
  139. if ($this->hasPermission('directory_update') && $canSave) $cls[] = 'pupdate';
  140. if ($this->hasPermission('file_upload') && $canCreate) $cls[] = 'pupload';
  141. if ($this->hasPermission('file_create') && $canCreate) $cls[] = 'pcreate';
  142. $dirnames[] = strtoupper($fileName);
  143. $directories[$fileName] = array(
  144. 'id' => $bases['urlRelative'].rtrim($fileName,'/').'/',
  145. 'text' => $fileName,
  146. 'cls' => implode(' ',$cls),
  147. 'iconCls' => 'icon icon-folder',
  148. 'type' => 'dir',
  149. 'leaf' => false,
  150. 'path' => $bases['pathAbsoluteWithPath'].$fileName,
  151. 'pathRelative' => $bases['pathRelative'].$fileName,
  152. 'perms' => $octalPerms,
  153. 'menu' => array(),
  154. );
  155. $directories[$fileName]['menu'] = array('items' => $this->getListContextMenu($file,$directories[$fileName]));
  156. }
  157. /* get files in current dir */
  158. if ($file->isFile() && !$hideFiles && $this->hasPermission('file_list')) {
  159. $ext = pathinfo($filePathName, PATHINFO_EXTENSION);
  160. $ext = $useMultibyte ? mb_strtolower($ext, $encoding) : strtolower($ext);
  161. if (!empty($allowedExtensions) && !in_array($ext, $allowedExtensions)) {
  162. continue;
  163. }
  164. $cls = array();
  165. if (!empty($properties['currentFile']) && rawurldecode($properties['currentFile']) == $fullPath.$fileName && $properties['currentAction'] == $editAction) {
  166. $cls[] = 'active-node';
  167. }
  168. if ($this->hasPermission('file_remove') && $canRemove) $cls[] = 'premove';
  169. if ($this->hasPermission('file_update') && $canSave) $cls[] = 'pupdate';
  170. $encFile = rawurlencode($fullPath.$fileName);
  171. $page = !empty($editAction) ? '?a='.$editAction.'&file='.$bases['urlRelative'].$fileName.'&wctx='.$this->ctx->get('key').'&source='.$this->get('id') : null;
  172. $url = $bases['urlRelative'] . $fileName;
  173. /* get relative url from manager/ */
  174. $fromManagerUrl = $bases['url'].trim(str_replace('//','/',$path.$fileName),'/');
  175. $fromManagerUrl = ($bases['urlIsRelative'] ? '../' : '').$fromManagerUrl;
  176. $filenames[] = strtoupper($fileName);
  177. $files[$fileName] = array(
  178. 'id' => $bases['urlRelative'].$fileName,
  179. 'text' => $fileName,
  180. 'cls' => implode(' ',$cls),
  181. 'iconCls' => 'icon icon-file icon-'.$ext . ($file->isWritable() ? '' : ' icon-lock'),
  182. 'type' => 'file',
  183. 'leaf' => true,
  184. // 'qtip' => in_array($ext,$imagesExts) ? '<img src="'.$fromManagerUrl.'" alt="'.$fileName.'" />' : '',
  185. 'page' => $this->fileHandler->isBinary($filePathName) ? null : $page,
  186. 'perms' => $octalPerms,
  187. 'path' => $bases['pathAbsoluteWithPath'].$fileName,
  188. 'pathRelative' => $bases['pathRelative'].$fileName,
  189. 'directory' => $bases['path'],
  190. 'url' => $bases['url'].$url,
  191. 'urlAbsolute' => $bases['urlAbsoluteWithPath'].ltrim($fileName,'/'),
  192. 'file' => $encFile,
  193. 'menu' => array(),
  194. );
  195. $files[$fileName]['menu'] = array('items' => $this->getListContextMenu($file,$files[$fileName]));
  196. // trough tree config we can request a tree without image-preview tooltips, don't do any work if not necessary
  197. if (!$hideTooltips) {
  198. $files[$fileName]['qtip'] = '';
  199. if (in_array($ext, $imagesExts)) {
  200. $modAuth = $this->xpdo->user->getUserToken($this->xpdo->context->get('key'));
  201. $preview = true;
  202. $imageWidth = $this->ctx->getOption('filemanager_image_width', 400);
  203. $imageHeight = $this->ctx->getOption('filemanager_image_height', 300);
  204. $thumbnailType = $this->getOption('thumbnailType', $properties, 'png');
  205. $thumbnailQuality = $this->getOption('thumbnailQuality', $properties, 90);
  206. if ($ext == 'svg') {
  207. $svgString = @file_get_contents($bases['pathAbsoluteWithPath'].$fileName);
  208. preg_match('/(<svg[^>]*\swidth=")([\d\.]+)([a-z]*)"/si', $svgString, $svgWidth);
  209. preg_match('/(<svg[^>]*\sheight=")([\d\.]+)([a-z]*)"/si', $svgString, $svgHeight);
  210. preg_match('/(<svg[^>]*\sviewBox=")([\d\.]+(?:,|\s)[\d\.]+(?:,|\s)([\d\.]+)(?:,|\s)([\d\.]+))"/si', $svgString, $svgViewbox);
  211. if (!empty($svgViewbox)) {
  212. // get width and height from viewbox attribute
  213. $imageWidth = round($svgViewbox[3]);
  214. $imageHeight = round($svgViewbox[4]);
  215. } elseif (!empty($svgWidth) && !empty($svgHeight)) {
  216. // get width and height from width and height attributes
  217. $imageWidth = round($svgWidth[2]);
  218. $imageHeight = round($svgHeight[2]);
  219. }
  220. $image = $bases['urlAbsolute'] . urldecode($url);
  221. } else {
  222. $size = @getimagesize($bases['pathAbsoluteWithPath'].$fileName);
  223. if (is_array($size) && $size[0] > 0 && $size[1] > 0) {
  224. // get original image size for proportional scaling
  225. if ($size[0] > $size[1]) {
  226. // landscape
  227. $imageQueryWidth = $size[0] >= $imageWidth ? $imageWidth : $size[0];
  228. $imageQueryHeight = 0;
  229. $imageWidth = $imageQueryWidth;
  230. $imageHeight = round($size[1] * ($imageQueryWidth / $size[0]));
  231. } else {
  232. // portrait or square
  233. $imageQueryWidth = 0;
  234. $imageQueryHeight = $size[1] >= $imageHeight ? $imageHeight : $size[1];
  235. $imageWidth = round($size[0] * ($imageQueryHeight / $size[1]));
  236. $imageHeight = $imageQueryHeight;
  237. }
  238. $imageQuery = http_build_query(array(
  239. 'src' => $bases['urlRelative'].$fileName,
  240. 'w' => $imageQueryWidth,
  241. 'h' => $imageQueryHeight,
  242. 'HTTP_MODAUTH' => $modAuth,
  243. 'f' => $thumbnailType,
  244. 'q' => $thumbnailQuality,
  245. 'wctx' => $this->ctx->get('key'),
  246. 'source' => $this->get('id'),
  247. 't' => $file->getMTime(),
  248. ));
  249. $image = $this->ctx->getOption('connectors_url', MODX_CONNECTORS_URL).'system/phpthumb.php?'.urldecode($imageQuery);
  250. } else {
  251. $preview = false;
  252. $this->xpdo->log(modX::LOG_LEVEL_ERROR,'Thumbnail could not be created for file: '.$bases['pathAbsoluteWithPath'].$fileName);
  253. }
  254. }
  255. if ($preview) {
  256. $files[$fileName]['qtip'] = '<img src="'.$image.'" width="'.$imageWidth.'" height="'.$imageHeight.'" alt="'.$fileName.'" />';
  257. }
  258. }
  259. }
  260. }
  261. }
  262. $ls = array();
  263. /* now sort files/directories */
  264. array_multisort($dirnames, SORT_ASC, SORT_STRING, $directories);
  265. // uksort($directories, 'strnatcasecmp');
  266. foreach ($directories as $dir) {
  267. $ls[] = $dir;
  268. }
  269. array_multisort($filenames, SORT_ASC, SORT_STRING, $files);
  270. // uksort($files, 'strnatcasecmp');
  271. foreach ($files as $file) {
  272. $ls[] = $file;
  273. }
  274. return $ls;
  275. }
  276. /**
  277. * Get the context menu items for a specific object in the list view
  278. *
  279. * @param DirectoryIterator $file
  280. * @param array $fileArray
  281. * @return array
  282. */
  283. public function getListContextMenu(DirectoryIterator $file,array $fileArray) {
  284. $canSave = $this->checkPolicy('save');
  285. $canRemove = $this->checkPolicy('remove');
  286. $canCreate = $this->checkPolicy('create');
  287. $canView = $this->checkPolicy('view');
  288. $menu = array();
  289. if (!$file->isDir()) { /* files */
  290. if ($this->hasPermission('file_update') && $canSave) {
  291. if (!empty($fileArray['page'])) {
  292. $menu[] = array(
  293. 'text' => $this->xpdo->lexicon('file_edit'),
  294. 'handler' => 'this.editFile',
  295. );
  296. $menu[] = array(
  297. 'text' => $this->xpdo->lexicon('quick_update_file'),
  298. 'handler' => 'this.quickUpdateFile',
  299. );
  300. }
  301. $menu[] = array(
  302. 'text' => $this->xpdo->lexicon('rename'),
  303. 'handler' => 'this.renameFile',
  304. );
  305. }
  306. if ($this->hasPermission('file_view') && $canView) {
  307. $menu[] = array(
  308. 'text' => $this->xpdo->lexicon('file_download'),
  309. 'handler' => 'this.downloadFile',
  310. );
  311. $menu[] = array(
  312. 'text' => $this->xpdo->lexicon('file_copy_path'),
  313. 'handler' => 'this.copyRelativePath',
  314. );
  315. }
  316. if ($this->hasPermission('file_unpack') && $canView && pathinfo($file->getFilename(), PATHINFO_EXTENSION) === 'zip') {
  317. $menu[] = array(
  318. 'text' => $this->xpdo->lexicon('file_download_unzip'),
  319. 'handler' => 'this.unpackFile',
  320. );
  321. }
  322. if ($this->hasPermission('file_remove') && $canRemove) {
  323. if (!empty($menu)) $menu[] = '-';
  324. $menu[] = array(
  325. 'text' => $this->xpdo->lexicon('file_remove'),
  326. 'handler' => 'this.removeFile',
  327. );
  328. }
  329. } else { /* directories */
  330. if ($this->hasPermission('directory_create') && $canCreate) {
  331. $menu[] = array(
  332. 'text' => $this->xpdo->lexicon('file_folder_create_here'),
  333. 'handler' => 'this.createDirectory',
  334. );
  335. }
  336. if ($this->hasPermission('directory_chmod') && $canSave) {
  337. $menu[] = array(
  338. 'text' => $this->xpdo->lexicon('file_folder_chmod'),
  339. 'handler' => 'this.chmodDirectory',
  340. );
  341. }
  342. if ($this->hasPermission('directory_update') && $canSave) {
  343. $menu[] = array(
  344. 'text' => $this->xpdo->lexicon('rename'),
  345. 'handler' => 'this.renameDirectory',
  346. );
  347. }
  348. $menu[] = array(
  349. 'text' => $this->xpdo->lexicon('directory_refresh'),
  350. 'handler' => 'this.refreshActiveNode',
  351. );
  352. $menu[] = array(
  353. 'text' => $this->xpdo->lexicon('file_folder_copy_path'),
  354. 'handler' => 'this.copyRelativePath',
  355. );
  356. if ($this->hasPermission('file_upload') && $canCreate) {
  357. $menu[] = '-';
  358. $menu[] = array(
  359. 'text' => $this->xpdo->lexicon('upload_files'),
  360. 'handler' => 'this.uploadFiles',
  361. );
  362. }
  363. if ($this->hasPermission('file_create') && $canCreate) {
  364. $menu[] = array(
  365. 'text' => $this->xpdo->lexicon('file_create'),
  366. 'handler' => 'this.createFile',
  367. );
  368. $menu[] = array(
  369. 'text' => $this->xpdo->lexicon('quick_create_file'),
  370. 'handler' => 'this.quickCreateFile',
  371. );
  372. }
  373. if ($this->hasPermission('directory_remove') && $canRemove) {
  374. $menu[] = '-';
  375. $menu[] = array(
  376. 'text' => $this->xpdo->lexicon('file_folder_remove'),
  377. 'handler' => 'this.removeDirectory',
  378. );
  379. }
  380. }
  381. return $menu;
  382. }
  383. /**
  384. * Create a filesystem folder
  385. *
  386. * @param string $name
  387. * @param string $parentContainer
  388. * @return boolean
  389. */
  390. public function createContainer($name,$parentContainer) {
  391. $bases = $this->getBases($parentContainer.'/'.$name);
  392. if ($parentContainer == '/') {
  393. $parentContainer = $bases['pathAbsolute'];
  394. } else {
  395. $parentContainer = $bases['pathAbsolute'].$parentContainer;
  396. }
  397. /* create modDirectory instance for containing directory and validate */
  398. /** @var modDirectory $parentDirectory */
  399. $parentDirectory = $this->fileHandler->make($parentContainer);
  400. if (!($parentDirectory instanceof modDirectory)) {
  401. $this->addError('parent',$this->xpdo->lexicon('file_folder_err_parent_invalid'));
  402. return false;
  403. }
  404. if (!$parentDirectory->isReadable() || !$parentDirectory->isWritable()) {
  405. $this->addError('parent',$this->xpdo->lexicon('file_folder_err_perms_parent'));
  406. return false;
  407. }
  408. /* create modDirectory instance for new path, validate doesnt already exist */
  409. $newDirectoryPath = $parentDirectory->getPath().$name;
  410. /** @var modDirectory $newDirectory */
  411. $newDirectory = $this->fileHandler->make($newDirectoryPath,array(),'modDirectory');
  412. if ($newDirectory->exists()) {
  413. $this->addError('name',$this->xpdo->lexicon('file_folder_err_ae'));
  414. return false;
  415. }
  416. /* actually create the directory */
  417. $result = $newDirectory->create();
  418. if ($result !== true) {
  419. $this->addError('name',$this->xpdo->lexicon('file_folder_err_create').$result);
  420. return false;
  421. }
  422. /* invoke event */
  423. $this->xpdo->invokeEvent('OnFileManagerDirCreate',array(
  424. 'directory' => $newDirectoryPath,
  425. 'source' => &$this,
  426. ));
  427. $this->xpdo->logManagerAction('directory_create','',$newDirectory->getPath());
  428. return true;
  429. }
  430. /**
  431. * Remove a folder at the specified location
  432. *
  433. * @param string $path
  434. * @return boolean
  435. */
  436. public function removeContainer($path) {
  437. /* instantiate modDirectory object */
  438. /** @var modDirectory $directory */
  439. $path = $this->fileHandler->postfixSlash($path);
  440. $directory = $this->fileHandler->make($path);
  441. /* validate and check permissions on directory */
  442. if (!($directory instanceof modDirectory)) {
  443. $this->addError('path',$this->xpdo->lexicon('file_folder_err_invalid'));
  444. return false;
  445. }
  446. if (!$directory->isReadable() || !$directory->isWritable()) {
  447. $this->addError('path',$this->xpdo->lexicon('file_folder_err_perms_remove'));
  448. return false;
  449. }
  450. /* remove the directory */
  451. $result = $directory->remove();
  452. if ($result == false) {
  453. $this->addError('path',$this->xpdo->lexicon('file_folder_err_remove'));
  454. }
  455. /* invoke event */
  456. $this->xpdo->invokeEvent('OnFileManagerDirRemove',array(
  457. 'directory' => $path,
  458. 'source' => &$this,
  459. ));
  460. $this->xpdo->logManagerAction('directory_remove','',$directory->getPath());
  461. return true;
  462. }
  463. /**
  464. * @param string $oldPath
  465. * @param string $newName
  466. * @return bool
  467. */
  468. public function renameContainer($oldPath,$newName) {
  469. $bases = $this->getBases($oldPath);
  470. $oldPath = $bases['pathAbsolute'].$oldPath;
  471. /** @var modDirectory $oldDirectory */
  472. $oldDirectory = $this->fileHandler->make($oldPath);
  473. /* make sure is a directory and writable */
  474. if (!($oldDirectory instanceof modDirectory)) {
  475. $this->addError('name',$this->xpdo->lexicon('file_folder_err_invalid'));
  476. return false;
  477. }
  478. if (!$oldDirectory->isReadable() || !$oldDirectory->isWritable()) {
  479. $this->addError('name',$this->xpdo->lexicon('file_folder_err_perms'));
  480. return false;
  481. }
  482. /* sanitize new path */
  483. $newPath = $this->fileHandler->sanitizePath($newName);
  484. $newPath = $this->fileHandler->postfixSlash($newPath);
  485. $newPath = dirname($oldPath).'/'.$newPath;
  486. /* check to see if the new resource already exists */
  487. if (file_exists($newPath)) {
  488. $this->addError('name',$this->xpdo->lexicon('file_folder_err_ae'));
  489. return false;
  490. }
  491. /* rename the dir */
  492. if (!$oldDirectory->rename($newPath)) {
  493. $this->addError('name',$this->xpdo->lexicon('file_folder_err_rename'));
  494. return false;
  495. }
  496. /* invoke event */
  497. $this->xpdo->invokeEvent('OnFileManagerDirRename',array(
  498. 'directory' => $newPath,
  499. 'source' => &$this,
  500. ));
  501. $this->xpdo->logManagerAction('directory_rename','',$oldDirectory->getPath());
  502. return true;
  503. }
  504. /**
  505. * Check that the filename has a file type extension that is allowed
  506. *
  507. * @param $filename
  508. * @return bool
  509. */
  510. public function checkFiletype($filename) {
  511. if ($this->getOption('allowedFileTypes')) {
  512. $allowedFileTypes = $this->getOption('allowedFileTypes');
  513. $allowedFileTypes = (!is_array($allowedFileTypes)) ? array_map("trim",explode(',', $allowedFileTypes)) : $allowedFileTypes;
  514. } else {
  515. $allowedFiles = $this->xpdo->getOption('upload_files') ? array_map("trim",explode(',', $this->xpdo->getOption('upload_files'))) : array();
  516. $allowedImages = $this->xpdo->getOption('upload_images') ? array_map("trim",explode(',', $this->xpdo->getOption('upload_images'))) : array();
  517. $allowedMedia = $this->xpdo->getOption('upload_media') ? array_map("trim",explode(',', $this->xpdo->getOption('upload_media'))) : array();
  518. $allowedFlash = $this->xpdo->getOption('upload_flash') ? array_map("trim",explode(',', $this->xpdo->getOption('upload_flash'))): array();
  519. $allowedFileTypes = array_unique(array_merge($allowedFiles, $allowedImages, $allowedMedia, $allowedFlash));
  520. $this->setOption('allowedFileTypes', $allowedFileTypes);
  521. }
  522. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  523. $ext = strtolower($ext);
  524. if (!empty($allowedFileTypes) && !in_array($ext, $allowedFileTypes)) {
  525. $this->addError('path', $this->xpdo->lexicon('file_err_ext_not_allowed', array(
  526. 'ext' => $ext,
  527. )));
  528. return false;
  529. }
  530. return true;
  531. }
  532. /**
  533. * @param string $oldPath
  534. * @param string $newName
  535. * @return bool
  536. */
  537. public function renameObject($oldPath,$newName) {
  538. $bases = $this->getBases($oldPath);
  539. $oldPath = $bases['pathAbsolute'].$oldPath;
  540. /** @var modFile $oldFile */
  541. $oldFile = $this->fileHandler->make($oldPath);
  542. /* make sure is a directory and writable */
  543. if (!($oldFile instanceof modFile)) {
  544. $this->addError('name',$this->xpdo->lexicon('file_err_invalid'));
  545. return false;
  546. }
  547. if (!$oldFile->isReadable() || !$oldFile->isWritable()) {
  548. $this->addError('name',$this->xpdo->lexicon('file_folder_err_perms'));
  549. return false;
  550. }
  551. if (!$this->checkFiletype($newName)) {
  552. return false;
  553. }
  554. /* sanitize new path */
  555. $newPath = $this->fileHandler->sanitizePath($newName);
  556. $newPath = dirname($oldPath).'/'.$newPath;
  557. /* check to see if the new resource already exists */
  558. if (file_exists($newPath)) {
  559. if (is_dir($newPath)) {
  560. $this->addError('name',$this->xpdo->lexicon('file_folder_err_ae'));
  561. return false;
  562. }
  563. $this->addError('name',sprintf($this->xpdo->lexicon('file_err_ae'),$newName));
  564. return false;
  565. }
  566. /* rename the file */
  567. if (!$oldFile->rename($newPath)) {
  568. $this->addError('name',$this->xpdo->lexicon('file_folder_err_rename'));
  569. return false;
  570. }
  571. /* invoke event */
  572. $this->xpdo->invokeEvent('OnFileManagerFileRename',array(
  573. 'path' => $newPath,
  574. 'source' => &$this,
  575. ));
  576. $this->xpdo->logManagerAction('file_rename','',$oldFile->getPath());
  577. return true;
  578. }
  579. /**
  580. * Get the contents of a specified file
  581. *
  582. * @param string $objectPath
  583. * @return array
  584. */
  585. public function getObjectContents($objectPath) {
  586. $properties = $this->getPropertyList();
  587. $bases = $this->getBases($objectPath);
  588. /** @var modFile $file */
  589. $file = $this->fileHandler->make($bases['pathAbsoluteWithPath']);
  590. if (!$file->exists()) {
  591. $this->addError('file',$this->xpdo->lexicon('file_err_nf'));
  592. }
  593. if (!$file->isReadable()) {
  594. $this->addError('file',$this->xpdo->lexicon('file_err_perms'));
  595. }
  596. $imageExtensions = $this->getOption('imageExtensions',$properties,'jpg,jpeg,png,gif,svg');
  597. $imageExtensions = explode(',',$imageExtensions);
  598. $fileExtension = pathinfo($objectPath,PATHINFO_EXTENSION);
  599. $fa = array(
  600. 'name' => $objectPath,
  601. 'basename' => basename($file->getPath()),
  602. 'path' => $file->getPath(),
  603. 'size' => @$file->getSize(),
  604. 'last_accessed' => @$file->getLastAccessed(),
  605. 'last_modified' => @$file->getLastModified(),
  606. 'content' => $file->getContents(),
  607. 'image' => in_array($fileExtension,$imageExtensions) ? true : false,
  608. 'is_writable' => $file->isWritable(),
  609. 'is_readable' => $file->isReadable(),
  610. );
  611. return $fa;
  612. }
  613. /**
  614. * Remove a file
  615. *
  616. * @param string $objectPath
  617. * @return boolean
  618. */
  619. public function removeObject($objectPath) {
  620. $bases = $this->getBases($objectPath);
  621. $fullPath = $bases['pathAbsolute'].$objectPath;
  622. if (!file_exists($fullPath)) {
  623. $this->addError('file',$this->xpdo->lexicon('file_folder_err_ns').': '.$fullPath);
  624. return false;
  625. }
  626. /** @var modFile $file */
  627. $file = $this->fileHandler->make($fullPath);
  628. /* verify file exists and is writable */
  629. if (!$file->exists()) {
  630. $this->addError('file',$this->xpdo->lexicon('file_err_nf').': '.$file->getPath());
  631. return false;
  632. } else if (!$file->isReadable() || !$file->isWritable()) {
  633. $this->addError('file',$this->xpdo->lexicon('file_err_perms_remove'));
  634. return false;
  635. } else if (!($file instanceof modFile)) {
  636. $this->addError('file',$this->xpdo->lexicon('file_err_invalid'));
  637. return false;
  638. }
  639. /* remove file */
  640. if (!$file->remove()) {
  641. $this->addError('file',$this->xpdo->lexicon('file_err_remove'));
  642. return false;
  643. }
  644. /* invoke event */
  645. $this->xpdo->invokeEvent('OnFileManagerFileRemove',array(
  646. 'path' => $fullPath,
  647. 'source' => &$this,
  648. ));
  649. /* log manager action */
  650. $this->xpdo->logManagerAction('file_remove','',$file->getPath());
  651. return true;
  652. }
  653. /**
  654. * Update the contents of a file
  655. *
  656. * @param string $objectPath
  657. * @param string $content
  658. * @return boolean|string
  659. */
  660. public function updateObject($objectPath,$content) {
  661. $bases = $this->getBases($objectPath);
  662. $fullPath = $bases['pathAbsolute'].ltrim($objectPath,'/');
  663. /** @var modFile $file */
  664. $file = $this->fileHandler->make($fullPath);
  665. /* verify file exists */
  666. if (!$file->exists()) {
  667. $this->addError('file',$this->xpdo->lexicon('file_err_nf').': '.$objectPath);
  668. return false;
  669. }
  670. /* write file */
  671. $file->setContent($content);
  672. $file->save();
  673. /* invoke event */
  674. $this->xpdo->invokeEvent('OnFileManagerFileUpdate',array(
  675. 'path' => $fullPath,
  676. 'source' => &$this,
  677. ));
  678. $this->xpdo->logManagerAction('file_update','',$file->getPath());
  679. return rawurlencode($file->getPath());
  680. }
  681. /**
  682. * Create a file
  683. *
  684. * @param string $objectPath
  685. * @param string $name
  686. * @param string $content
  687. * @return boolean|string
  688. */
  689. public function createObject($objectPath,$name,$content) {
  690. $bases = $this->getBases($objectPath);
  691. $fullPath = $bases['pathAbsolute'].ltrim($objectPath,'/').ltrim($name,'/');
  692. if (!$this->checkFiletype($fullPath)) {
  693. return false;
  694. }
  695. /** @var modFile $file */
  696. $file = $this->fileHandler->make($fullPath,array(),'modFile');
  697. /* write file */
  698. $file->setContent($content);
  699. $file->create($content);
  700. /* verify file exists */
  701. if (!$file->exists()) {
  702. $this->addError('file',$this->xpdo->lexicon('file_err_nf').': '.$fullPath);
  703. return false;
  704. }
  705. /* invoke event */
  706. $this->xpdo->invokeEvent('OnFileManagerFileCreate',array(
  707. 'path' => $fullPath,
  708. 'source' => &$this,
  709. ));
  710. $this->xpdo->logManagerAction('file_create','',$file->getPath());
  711. return rawurlencode($file->getPath());
  712. }
  713. /**
  714. * Upload files to a specific folder on the file system
  715. *
  716. * @param string $container
  717. * @param array $objects
  718. * @return boolean
  719. */
  720. public function uploadObjectsToContainer($container,array $objects = array()) {
  721. $bases = $this->getBases($container);
  722. $fullPath = $bases['pathAbsolute'].ltrim($container,'/');
  723. /** @var modDirectory $directory */
  724. $directory = $this->fileHandler->make($fullPath);
  725. /* verify target path is a directory and writable */
  726. if (!($directory instanceof modDirectory)) {
  727. $this->addError('path',$this->xpdo->lexicon('file_folder_err_invalid').': '.$fullPath);
  728. return false;
  729. }
  730. if (!($directory->isReadable()) || !$directory->isWritable()) {
  731. $this->addError('path',$this->xpdo->lexicon('file_folder_err_perms_upload').': '.$fullPath);
  732. return false;
  733. }
  734. $this->xpdo->context->prepare();
  735. $maxFileSize = $this->xpdo->getOption('upload_maxsize',null,1048576);
  736. $mode = $this->fileHandler->modx->getOption('new_file_permissions');
  737. if ($mode) {
  738. $mode = octdec($mode);
  739. }
  740. /* loop through each file and upload */
  741. foreach ($objects as $file) {
  742. /* invoke event */
  743. $this->xpdo->invokeEvent('OnFileManagerBeforeUpload', array(
  744. 'files' => &$objects,
  745. 'file' => &$file,
  746. 'directory' => $container,
  747. 'source' => &$this,
  748. ));
  749. if ($file['error'] != 0) continue;
  750. if (empty($file['name'])) continue;
  751. if (!$this->checkFiletype($file['name'])) {
  752. continue;
  753. }
  754. $size = filesize($file['tmp_name']);
  755. if ($size > $maxFileSize) {
  756. $this->addError('path',$this->xpdo->lexicon('file_err_too_large',array(
  757. 'size' => $size,
  758. 'allowed' => $maxFileSize,
  759. )));
  760. continue;
  761. }
  762. $newPath = $this->fileHandler->sanitizePath($file['name']);
  763. $newPath = $directory->getPath().$newPath;
  764. if (!move_uploaded_file($file['tmp_name'],$newPath)) {
  765. $this->addError('path',$this->xpdo->lexicon('file_err_upload'));
  766. continue;
  767. }
  768. if ($mode) {
  769. @chmod($newPath, $mode);
  770. }
  771. }
  772. /* invoke event */
  773. $this->xpdo->invokeEvent('OnFileManagerUpload',array(
  774. 'files' => &$objects,
  775. 'directory' => $container,
  776. 'source' => &$this,
  777. ));
  778. $this->xpdo->logManagerAction('file_upload','',$directory->getPath());
  779. return !$this->hasErrors();
  780. }
  781. /**
  782. * Chmod a specific folder
  783. *
  784. * @param string $directoryPath
  785. * @param string $mode
  786. * @return boolean
  787. */
  788. public function chmodContainer($directoryPath,$mode) {
  789. /** @var modDirectory $directory */
  790. $directory = $this->fileHandler->make($directoryPath);
  791. /* verify target path is a directory and writable */
  792. if (!($directory instanceof modDirectory)) {
  793. $this->addError('mode',$this->xpdo->lexicon('file_folder_err_invalid').': '.$directoryPath);
  794. return false;
  795. }
  796. if (!$directory->isReadable() || !$directory->isWritable()) {
  797. $this->addError('mode',$this->xpdo->lexicon('file_folder_err_perms_upload').': '.$directoryPath);
  798. return false;
  799. }
  800. if (!$directory->isValidMode($mode)) {
  801. $this->addError('mode',$this->xpdo->lexicon('file_err_chmod_invalid'));
  802. return false;
  803. }
  804. if (!$directory->chmod($mode)) {
  805. $this->addError('mode',$this->xpdo->lexicon('file_err_chmod'));
  806. return false;
  807. }
  808. $this->xpdo->logManagerAction('directory_chmod','',$directoryPath);
  809. return true;
  810. }
  811. /**
  812. * Move a file or folder to a specific location
  813. *
  814. * @param string $from The location to move from
  815. * @param string $to The location to move to
  816. * @param string $point
  817. * @return boolean
  818. */
  819. public function moveObject($from,$to,$point = 'append') {
  820. $success = false;
  821. $fromBases = $this->getBases($from);
  822. $toBases = $this->getBases($to);
  823. $fromPath = $fromBases['pathAbsolute'].$from;
  824. $toPath = $toBases['pathAbsolute'].$to;
  825. /* verify source path */
  826. if (!file_exists($fromPath)) {
  827. $this->addError('from',$this->xpdo->lexicon('file_err_nf').': '.$fromPath);
  828. }
  829. /** @var modFileSystemResource $fromObject */
  830. $fromObject = $this->fileHandler->make($fromPath);
  831. if (!$fromObject->isReadable() || !$fromObject->isWritable()) {
  832. $this->addError('from',$this->xpdo->lexicon('file_err_nf').': '.$fromPath);
  833. return $success;
  834. }
  835. /* verify target path */
  836. if (!file_exists($toPath)) {
  837. $this->addError('to',$this->xpdo->lexicon('file_folder_err_invalid').': '.$toPath);
  838. }
  839. /** @var modDirectory $toObject */
  840. $toObject = $this->fileHandler->make($toPath);
  841. if (!($toObject instanceof modDirectory)) {
  842. $this->addError('mode',$this->xpdo->lexicon('file_folder_err_invalid').': '.$toPath);
  843. return $success;
  844. }
  845. if (!$toObject->isReadable() || !$toObject->isWritable()) {
  846. $this->addError('to',$this->xpdo->lexicon('file_folder_err_invalid').': '.$toPath);
  847. return $success;
  848. }
  849. /* now move object */
  850. $newPath = rtrim($toPath,'/').'/'.basename($fromPath);
  851. $success = $fromObject->rename($newPath);
  852. if (!$success) {
  853. $this->addError('from',$this->xpdo->lexicon('file_err_chmod'));
  854. }
  855. /* invoke event */
  856. $this->xpdo->invokeEvent('OnFileManagerMoveObject',array(
  857. 'from' => $fromObject->getPath(),
  858. 'to' => $toObject->getPath(),
  859. 'source' => &$this,
  860. ));
  861. return $success;
  862. }
  863. /**
  864. * Get a list of files in a specific directory.
  865. *
  866. * @param string $path
  867. * @return array
  868. */
  869. public function getObjectsInContainer($path) {
  870. $properties = $this->getPropertyList();
  871. $dir = $this->fileHandler->postfixSlash($path);
  872. $bases = $this->getBases($dir);
  873. if (empty($bases['pathAbsolute'])) return array();
  874. $fullPath = $bases['pathAbsolute'].$dir;
  875. $modAuth = $this->xpdo->user->getUserToken($this->xpdo->context->get('key'));
  876. /* get default settings */
  877. $imageExtensions = $this->getOption('imageExtensions',$properties,'jpg,jpeg,png,gif,svg');
  878. $imageExtensions = explode(',',$imageExtensions);
  879. $use_multibyte = $this->ctx->getOption('use_multibyte', false);
  880. $encoding = $this->ctx->getOption('modx_charset', 'UTF-8');
  881. $allowedFileTypes = $this->getOption('allowedFileTypes', $properties, '');
  882. $editAction = $this->getEditActionId();
  883. if (is_string($allowedFileTypes)) {
  884. if (empty($allowedFileTypes)) {
  885. $allowedFileTypes = array();
  886. } else {
  887. $allowedFileTypes = explode(',', $allowedFileTypes);
  888. }
  889. }
  890. $thumbnailType = $this->getOption('thumbnailType',$properties,'png');
  891. $thumbnailQuality = $this->getOption('thumbnailQuality',$properties,90);
  892. $skipFiles = $this->getOption('skipFiles',$properties,'.svn,.git,_notes,nbproject,.idea,.DS_Store');
  893. $skipFiles = explode(',',$skipFiles);
  894. $skipFiles[] = '.';
  895. $skipFiles[] = '..';
  896. /* iterate */
  897. $files = array();
  898. $filenames = array();
  899. if (!is_dir($fullPath)) {
  900. $this->addError('dir',$this->xpdo->lexicon('file_folder_err_ns').$fullPath);
  901. return array();
  902. }
  903. /** @var DirectoryIterator $file */
  904. foreach (new DirectoryIterator($fullPath) as $file) {
  905. if (in_array($file,$skipFiles)) continue;
  906. if (!$file->isReadable()) continue;
  907. $fileName = $file->getFilename();
  908. $filePathName = $file->getPathname();
  909. if (!$file->isDir()) {
  910. $fileExtension = pathinfo($filePathName,PATHINFO_EXTENSION);
  911. $fileExtension = $use_multibyte ? mb_strtolower($fileExtension,$encoding) : strtolower($fileExtension);
  912. if (!empty($allowedFileTypes) && !in_array($fileExtension, $allowedFileTypes)) {
  913. continue;
  914. }
  915. $filesize = @filesize($filePathName);
  916. $url = urlencode(ltrim($dir.$fileName,'/'));
  917. $page = !empty($editAction) ? '?a='.$editAction.'&file='.$bases['urlRelative'].$fileName.'&wctx='.$this->ctx->get('key').'&source='.$this->get('id') : null;
  918. /* get thumbnail */
  919. $preview = 0;
  920. if (in_array($fileExtension,$imageExtensions)) {
  921. $preview = 1;
  922. $imageWidth = $this->ctx->getOption('filemanager_image_width', 800);
  923. $imageHeight = $this->ctx->getOption('filemanager_image_height', 600);
  924. $thumbWidth = $this->ctx->getOption('filemanager_thumb_width', 100);
  925. $thumbHeight = $this->ctx->getOption('filemanager_thumb_height', 80);
  926. $size = array($imageWidth, $imageHeight);
  927. if ($fileExtension == 'svg') {
  928. $svgString = @file_get_contents($filePathName);
  929. preg_match('/(<svg[^>]*\swidth=")([\d\.]+)([a-z]*)"/si', $svgString, $svgWidth);
  930. preg_match('/(<svg[^>]*\sheight=")([\d\.]+)([a-z]*)"/si', $svgString, $svgHeight);
  931. preg_match('/(<svg[^>]*\sviewBox=")([\d\.]+(?:,|\s)[\d\.]+(?:,|\s)([\d\.]+)(?:,|\s)([\d\.]+))"/si', $svgString, $svgViewbox);
  932. if (!empty($svgViewbox)) {
  933. // get width and height from viewbox attribute
  934. $size[0] = round($svgViewbox[3]);
  935. $size[1] = round($svgViewbox[4]);
  936. } elseif (!empty($svgWidth) && !empty($svgHeight)) {
  937. // get width and height from width and height attributes
  938. $size[0] = round($svgWidth[2]);
  939. $size[1] = round($svgHeight[2]);
  940. }
  941. // proportional scaling of image and thumb
  942. if ($size[0] > $size[1]) {
  943. // landscape
  944. $imageWidth = $size[0] >= $imageWidth ? $imageWidth : $size[0];
  945. $imageHeight = round($size[1] * ($imageWidth / $size[0]));
  946. $thumbWidth = $size[0] >= $thumbWidth ? $thumbWidth : $size[0];
  947. $thumbHeight = round($size[1] * ($thumbWidth / $size[0]));
  948. } else {
  949. // portrait or square
  950. $imageHeight = $size[1] >= $imageHeight ? $imageHeight : $size[1];
  951. $imageWidth = round($size[0] * ($imageHeight / $size[1]));
  952. $thumbHeight = $size[1] >= $thumbHeight ? $thumbHeight : $size[1];
  953. $thumbWidth = round($size[0] * ($thumbHeight / $size[1]));
  954. }
  955. $image = $thumb = $bases['urlAbsolute'].urldecode($url);
  956. } else {
  957. $size = @getimagesize($filePathName);
  958. if (is_array($size) && $size[0] > 0 && $size[1] > 0) {
  959. // proportional scaling of image and thumb
  960. if ($size[0] > $size[1]) {
  961. // landscape
  962. $imageQueryWidth = $size[0] >= $imageWidth ? $imageWidth : $size[0];
  963. $imageQueryHeight = 0;
  964. $imageWidth = $imageQueryWidth;
  965. $imageHeight = round($size[1] * ($imageQueryWidth / $size[0]));
  966. $thumbQueryWidth = $size[0] >= $thumbWidth ? $thumbWidth : $size[0];
  967. $thumbQueryHeight = 0;
  968. $thumbWidth = $thumbQueryWidth;
  969. $thumbHeight = round($size[1] * ($thumbQueryWidth / $size[0]));
  970. } else {
  971. // portrait or square
  972. $imageQueryWidth = 0;
  973. $imageQueryHeight = $size[1] >= $imageHeight ? $imageHeight : $size[1];
  974. $imageWidth = round($size[0] * ($imageQueryHeight / $size[1]));
  975. $imageHeight = $imageQueryHeight;
  976. $thumbQueryWidth = 0;
  977. $thumbQueryHeight = $size[1] >= $thumbHeight ? $thumbHeight : $size[1];
  978. $thumbWidth = round($size[0] * ($thumbQueryHeight / $size[1]));
  979. $thumbHeight = $thumbQueryHeight;
  980. }
  981. $imageQuery = http_build_query(array(
  982. 'src' => $url,
  983. 'w' => $imageQueryWidth,
  984. 'h' => $imageQueryHeight,
  985. 'HTTP_MODAUTH' => $modAuth,
  986. 'f' => $thumbnailType,
  987. 'q' => $thumbnailQuality,
  988. 'wctx' => $this->ctx->get('key'),
  989. 'source' => $this->get('id'),
  990. 't' => $file->getMTime(),
  991. ));
  992. $image = $this->ctx->getOption('connectors_url', MODX_CONNECTORS_URL).'system/phpthumb.php?'.urldecode($imageQuery);
  993. $thumbQuery = http_build_query(array(
  994. 'src' => $url,
  995. 'w' => $thumbQueryWidth,
  996. 'h' => $thumbQueryHeight,
  997. 'HTTP_MODAUTH' => $modAuth,
  998. 'f' => $thumbnailType,
  999. 'q' => $thumbnailQuality,
  1000. 'wctx' => $this->ctx->get('key'),
  1001. 'source' => $this->get('id'),
  1002. 't' => $file->getMTime(),
  1003. ));
  1004. $thumb = $this->ctx->getOption('connectors_url', MODX_CONNECTORS_URL).'system/phpthumb.php?'.urldecode($thumbQuery);
  1005. } else {
  1006. $this->xpdo->log(modX::LOG_LEVEL_ERROR,'Thumbnail could not be created for file: '.$filePathName);
  1007. $preview = 0;
  1008. }
  1009. }
  1010. }
  1011. if ($preview == 0) {
  1012. $size = null;
  1013. $thumb = $image = $this->ctx->getOption('manager_url', MODX_MANAGER_URL).'templates/default/images/restyle/nopreview.jpg';
  1014. $thumbWidth = $imageWidth = $this->ctx->getOption('filemanager_thumb_width', 100);
  1015. $thumbHeight = $imageHeight = $this->ctx->getOption('filemanager_thumb_height', 80);
  1016. }
  1017. $octalPerms = substr(sprintf('%o', $file->getPerms()), -4);
  1018. $filenames[] = strtoupper($fileName);
  1019. $files[$fileName] = array(
  1020. 'id' => $bases['urlAbsoluteWithPath'].$fileName,
  1021. 'name' => $fileName,
  1022. 'cls' => 'icon-'.$fileExtension,
  1023. 'image' => $image,
  1024. 'image_width' => is_array($size) && $size[0] > 0 ? $size[0] : $imageWidth,
  1025. 'image_height' => is_array($size) && $size[1] > 0 ? $size[1] : $imageHeight,
  1026. 'thumb' => $thumb,
  1027. 'thumb_width' => $thumbWidth,
  1028. 'thumb_height' => $thumbHeight,
  1029. 'url' => ltrim($dir.$fileName,'/'),
  1030. 'relativeUrl' => ltrim($dir.$fileName,'/'),
  1031. 'fullRelativeUrl' => rtrim($bases['url']).ltrim($dir.$fileName,'/'),
  1032. 'ext' => $fileExtension,
  1033. 'pathname' => str_replace('//','/',$filePathName),
  1034. 'pathRelative' => $bases['pathRelative'].$fileName,
  1035. 'lastmod' => $file->getMTime(),
  1036. 'preview' => $preview,
  1037. 'disabled' => false,
  1038. 'perms' => $octalPerms,
  1039. 'leaf' => true,
  1040. 'page' => $this->fileHandler->isBinary($filePathName) ? null : $page,
  1041. 'size' => $filesize,
  1042. 'menu' => array(),
  1043. );
  1044. $files[$fileName]['menu'] = $this->getListContextMenu($file, $files[$fileName]);
  1045. }
  1046. }
  1047. $ls = array();
  1048. array_multisort($filenames, SORT_ASC, SORT_STRING, $files);
  1049. foreach ($files as $file) {
  1050. $ls[] = $file;
  1051. }
  1052. return $ls;
  1053. }
  1054. /**
  1055. * Get the name of this source type
  1056. * @return string
  1057. */
  1058. public function getTypeName() {
  1059. $this->xpdo->lexicon->load('source');
  1060. return $this->xpdo->lexicon('source_type.file');
  1061. }
  1062. /**
  1063. * Get the description of this source type
  1064. * @return string
  1065. */
  1066. public function getTypeDescription() {
  1067. $this->xpdo->lexicon->load('source');
  1068. return $this->xpdo->lexicon('source_type.file_desc');
  1069. }
  1070. /**
  1071. * Get the default properties for the filesystem media source type.
  1072. *
  1073. * @return array
  1074. */
  1075. public function getDefaultProperties() {
  1076. return array(
  1077. 'basePath' => array(
  1078. 'name' => 'basePath',
  1079. 'desc' => 'prop_file.basePath_desc',
  1080. 'type' => 'textfield',
  1081. 'options' => '',
  1082. 'value' => '',
  1083. 'lexicon' => 'core:source',
  1084. ),
  1085. 'basePathRelative' => array(
  1086. 'name' => 'basePathRelative',
  1087. 'desc' => 'prop_file.basePathRelative_desc',
  1088. 'type' => 'combo-boolean',
  1089. 'options' => '',
  1090. 'value' => true,
  1091. 'lexicon' => 'core:source',
  1092. ),
  1093. 'baseUrl' => array(
  1094. 'name' => 'baseUrl',
  1095. 'desc' => 'prop_file.baseUrl_desc',
  1096. 'type' => 'textfield',
  1097. 'options' => '',
  1098. 'value' => '',
  1099. 'lexicon' => 'core:source',
  1100. ),
  1101. 'baseUrlRelative' => array(
  1102. 'name' => 'baseUrlRelative',
  1103. 'desc' => 'prop_file.baseUrlRelative_desc',
  1104. 'type' => 'combo-boolean',
  1105. 'options' => '',
  1106. 'value' => true,
  1107. 'lexicon' => 'core:source',
  1108. ),
  1109. 'allowedFileTypes' => array(
  1110. 'name' => 'allowedFileTypes',
  1111. 'desc' => 'prop_file.allowedFileTypes_desc',
  1112. 'type' => 'textfield',
  1113. 'options' => '',
  1114. 'value' => '',
  1115. 'lexicon' => 'core:source',
  1116. ),
  1117. 'imageExtensions' => array(
  1118. 'name' => 'imageExtensions',
  1119. 'desc' => 'prop_file.imageExtensions_desc',
  1120. 'type' => 'textfield',
  1121. 'value' => 'jpg,jpeg,png,gif,svg',
  1122. 'lexicon' => 'core:source',
  1123. ),
  1124. 'thumbnailType' => array(
  1125. 'name' => 'thumbnailType',
  1126. 'desc' => 'prop_file.thumbnailType_desc',
  1127. 'type' => 'list',
  1128. 'options' => array(
  1129. array('name' => 'PNG','value' => 'png'),
  1130. array('name' => 'JPG','value' => 'jpg'),
  1131. array('name' => 'GIF','value' => 'gif'),
  1132. ),
  1133. 'value' => 'png',
  1134. 'lexicon' => 'core:source',
  1135. ),
  1136. 'thumbnailQuality' => array(
  1137. 'name' => 'thumbnailQuality',
  1138. 'desc' => 'prop_s3.thumbnailQuality_desc',
  1139. 'type' => 'textfield',
  1140. 'options' => '',
  1141. 'value' => 90,
  1142. 'lexicon' => 'core:source',
  1143. ),
  1144. 'skipFiles' => array(
  1145. 'name' => 'skipFiles',
  1146. 'desc' => 'prop_file.skipFiles_desc',
  1147. 'type' => 'textfield',
  1148. 'options' => '',
  1149. 'value' => '.svn,.git,_notes,nbproject,.idea,.DS_Store',
  1150. 'lexicon' => 'core:source',
  1151. ),
  1152. );
  1153. }
  1154. /**
  1155. * Prepare the output values for image/file TVs by prefixing the baseUrl property to them
  1156. *
  1157. * @param string $value
  1158. * @return string
  1159. */
  1160. public function prepareOutputUrl($value) {
  1161. $properties = $this->getPropertyList();
  1162. if (!empty($properties['baseUrl'])) {
  1163. $value = $properties['baseUrl'].$value;
  1164. }
  1165. return $value;
  1166. }
  1167. /**
  1168. * Get the base path for this source. Only applicable to sources that are streams.
  1169. *
  1170. * @param string $object An optional file to find the base path of
  1171. * @return string
  1172. */
  1173. public function getBasePath($object = '') {
  1174. $bases = $this->getBases($object);
  1175. return $bases['pathAbsolute'];
  1176. }
  1177. /**
  1178. * Get the base URL for this source. Only applicable to sources that are streams.
  1179. *
  1180. * @param string $object An optional object to find the base url of
  1181. * @return string
  1182. */
  1183. public function getBaseUrl($object = '') {
  1184. $bases = $this->getBases($object);
  1185. return $bases['urlAbsolute'];
  1186. }
  1187. /**
  1188. * Get the absolute URL for a specified object. Only applicable to sources that are streams.
  1189. *
  1190. * @param string $object
  1191. * @return string
  1192. */
  1193. public function getObjectUrl($object = '') {
  1194. return $this->getBaseUrl().$object;
  1195. }
  1196. }