PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/site/www/_js/libs/tiny_mce/plugins/imagemanager/classes/CorePlugin.php

https://bitbucket.org/thiscode/thiscode-shop
PHP | 942 lines | 614 code | 197 blank | 131 comment | 209 complexity | 358db68fe752bd9132c759df7198382a MD5 | raw file
  1. <?php
  2. /**
  3. * $Id: CorePlugin.php 874 2012-06-12 11:49:34Z spocke $
  4. *
  5. * @package MCManagerCore
  6. * @author Moxiecode
  7. * @copyright Copyright Š 2007, Moxiecode Systems AB, All rights reserved.
  8. */
  9. // Load local file system
  10. require_once(MCMANAGER_ABSPATH . "FileSystems/LocalFileImpl.php");
  11. require_once(MCMANAGER_ABSPATH . "FileSystems/RootFileImpl.php");
  12. /**
  13. * This plugin contains the Core logic shared between manager products.
  14. *
  15. * @package MCManagerCore
  16. */
  17. class Moxiecode_CorePlugin extends Moxiecode_ManagerPlugin {
  18. /**#@+
  19. * @access public
  20. */
  21. /**
  22. * Constructs a new MCManagerCore instance.
  23. */
  24. function Moxiecode_CorePlugin() {
  25. }
  26. /**
  27. * Register file system.
  28. */
  29. function onInit(&$man) {
  30. $config = $man->getConfig();
  31. // Register local and root file system
  32. $man->registerFileSystem('file', isset($config['filesystem']) ? $config['filesystem'] : 'Moxiecode_LocalFileImpl');
  33. $man->registerFileSystem('root', 'Moxiecode_RootFileImpl');
  34. return true;
  35. }
  36. /**
  37. * Gets executed when a RPC command is to be executed.
  38. *
  39. * @param MCManager $man MCManager reference that the plugin is assigned to.
  40. * @param string $cmd RPC Command to be executed.
  41. * @param object $input RPC input object data.
  42. * @return object Result data from RPC call or null if it should be passed to the next handler in chain.
  43. */
  44. function onRPC(&$man, $cmd, $input) {
  45. switch ($cmd) {
  46. case "deleteFiles":
  47. return $this->_deleteFile($man, $input);
  48. case "listFiles":
  49. return $this->_listFiles($man, $input);
  50. case "createDirs":
  51. return $this->_createDirs($man, $input);
  52. case "getConfig":
  53. return $this->_getConfig($man, $input);
  54. case "insertFiles":
  55. return $this->_insertFiles($man, $input);
  56. case "loopBack":
  57. return $this->_loopBack($input);
  58. case "keepAlive":
  59. return $this->_keepAlive($man, $input);
  60. }
  61. return null;
  62. }
  63. /**
  64. * Gets called when data is streamed to client. This method should setup
  65. * HTTP headers, content type etc and simply send out the binary data to the client and the return false
  66. * ones that is done.
  67. *
  68. * @param MCManager $man MCManager reference that the plugin is assigned to.
  69. * @param string $cmd Stream command that is to be performed.
  70. * @param string $input Array of input arguments.
  71. * @return bool true/false if the execution of the event chain should continue.
  72. */
  73. function onStream(&$man, $cmd, $input) {
  74. $config = $man->getConfig();
  75. // Download stream
  76. if ($cmd == "download") {
  77. if ($man->verifyPath($input["path"])) {
  78. $file =& $man->getFile($input["path"]);
  79. $config = $file->getConfig();
  80. if ($man->verifyFile($file, "download") > 0 && $file->exists()) {
  81. // Get the mimetype, need to go to ../ parent folder cause... well we have to.
  82. //$mimeType = mapMimeTypeFromUrl($file->getAbsolutePath(), "../". $config['stream.mimefile']);
  83. // These seems to be needed for some IE proxies
  84. header("Expires: 0");
  85. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  86. header("Cache-Control: private", false);
  87. header("Content-type: application/octet-stream");
  88. header("Content-Disposition: attachment; filename=\"" . $file->getName() . "\"");
  89. // Stream data
  90. $stream =& $file->open('rb');
  91. if ($stream) {
  92. while (($buff = $stream->read()) != null)
  93. echo $buff;
  94. $stream->close();
  95. }
  96. return false;
  97. }
  98. } else {
  99. header('HTTP/1.0 404 Not found');
  100. header('status: 404 Not found');
  101. echo "Requested resource could not be found. Or access was denied.";
  102. die();
  103. }
  104. // Do not pass to next
  105. return false;
  106. }
  107. // Normal stream
  108. if ($cmd == "streamFile") {
  109. if (!$man->verifyPath($input["path"]) < 0) {
  110. trigger_error("Path verification failed.", FATAL);
  111. die();
  112. }
  113. $file = $man->getFile($input["path"]);
  114. $config = $file->getConfig();
  115. if (!$file->exists()) {
  116. trigger_error("File not found.", FATAL);
  117. die();
  118. } else {
  119. if (getClassName($file) == 'moxiecode_localfileimpl') {
  120. // Redirect to data
  121. $url = $man->removeTrailingSlash($config['preview.urlprefix']) . $man->convertPathToURI($file->getParent() . "/" . str_replace("+", "%20", urlencode($file->getName())), $config["preview.wwwroot"]) . $config['preview.urlsuffix'];
  122. // Passthrough rnd
  123. if (isset($input["rnd"]))
  124. $url .= (strpos($url, "?") === false ? "?" : "&") . "rnd=" . $input["rnd"];
  125. header('location: ' . $url);
  126. die();
  127. } else {
  128. // Verify that we can stream this one
  129. if ($man->verifyFile($file, "stream") < 0) {
  130. header('HTTP/1.0 404 Not found');
  131. header('status: 404 Not found');
  132. echo "Requested resource could not be found. Or access was denied.";
  133. die();
  134. }
  135. // Get the mimetype, need to go to ../ parent folder cause... well we have to.
  136. $mimeType = mapMimeTypeFromUrl($file->getAbsolutePath(), "../". $config['stream.mimefile']);
  137. header("Content-type: " . $mimeType);
  138. // Stream data
  139. $stream =& $file->open('rb');
  140. if ($stream) {
  141. while (($buff = $stream->read()) != null)
  142. echo $buff;
  143. $stream->close();
  144. }
  145. }
  146. return false;
  147. }
  148. }
  149. // Devkit commands
  150. switch ($cmd) {
  151. case "viewServerInfo":
  152. if (!checkBool($config['general.debug']))
  153. die("You have to enable debugging in config by setting general.debug to true.");
  154. phpinfo();
  155. break;
  156. case "downloadServerInfo":
  157. if (!checkBool($config['general.debug']))
  158. die("You have to enable debugging in config by setting general.debug to true.");
  159. // Get all ini settings
  160. $data = ini_get_all();
  161. // Setup all headers
  162. header("Content-type: text/plain; charset=UTF-8");
  163. header("Content-Disposition: attachment; filename=dump.txt");
  164. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  165. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  166. header("Cache-Control: no-store, no-cache, must-revalidate");
  167. header("Cache-Control: post-check=0, pre-check=0", false);
  168. header("Pragma: no-cache");
  169. echo "# Config from config.php" . "\r\n\r\n";
  170. foreach ($config as $key => $value) {
  171. if (is_bool($value))
  172. echo $key . "=" . ($value ? "true" : "false") . "\r\n";
  173. else
  174. echo $key . "=" . $value . "\r\n";
  175. }
  176. // Dump INI settings
  177. echo "\r\n# PHP INI settings file\r\n\r\n";
  178. foreach ($data as $key => $value)
  179. echo $key . "=" . $value['local_value'] . "\r\n";
  180. // Dump function support
  181. echo "\r\n# Function check" . "\r\n\r\n";
  182. $functions = array(
  183. "ImagecreateFromJpeg",
  184. "ImageJpeg",
  185. "ImagecreateFromGif",
  186. "ImageGif",
  187. "ImagecreateFromPng",
  188. "ImagePng",
  189. "gzdeflate",
  190. "gzinflate"
  191. );
  192. foreach ($functions as $function)
  193. echo $function . "=" . (function_exists($function) ? "ok" : "missing") . "\r\n";
  194. // Dump rootpath access
  195. echo "\r\n# Rootpath access" . "\r\n\r\n";
  196. foreach ($man->getRootPaths() as $rootpath) {
  197. $stat = stat($rootpath);
  198. echo $rootpath . "\r\n";
  199. echo " is_readable=" . (is_readable($rootpath) ? "readable" : "not readable") . "\r\n";
  200. echo " is_writable=" . (is_writable($rootpath) ? "writable" : "not writable") . "\r\n";
  201. foreach ($stat as $key => $value)
  202. echo " " . $key . "=" . $value . "\r\n";
  203. }
  204. break;
  205. case "viewLog":
  206. if (!checkBool($config['general.debug']))
  207. die("You have to enable debugging in config by setting general.debug to true.");
  208. header('Content-type: text/plain');
  209. if ($input['level'] == "debug")
  210. echo @file_get_contents("../logs/debug.log");
  211. else
  212. echo @file_get_contents("../logs/error.log");
  213. break;
  214. case "clearLog":
  215. header('Content-type: text/plain');
  216. if (!checkBool($config['general.debug']))
  217. die("You have to enable debugging in config by setting general.debug to true.");
  218. if ($input['level'] == "debug")
  219. $log = "../logs/debug.log";
  220. else
  221. $log = "../logs/error.log";
  222. @unlink($log);
  223. for ($i=0; $i<10; $i++)
  224. @unlink($log . "." . $i);
  225. echo "Logs cleared.";
  226. break;
  227. }
  228. // Pass to next
  229. return true;
  230. }
  231. /**
  232. * Gets called when data is streamed/uploaded from client. This method should take care of
  233. * any uploaded files and move them to the correct location.
  234. *
  235. * @param MCManager $man MCManager reference that the plugin is assigned to.
  236. * @param string $cmd Upload command that is to be performed.
  237. * @param string $input Array of input arguments.
  238. * @return object Result object data or null if the event wasn't handled.
  239. */
  240. function onUpload(&$man, $cmd, $input) {
  241. if ($cmd == "upload") {
  242. // Setup response
  243. $result = new Moxiecode_ResultSet("status,file,message");
  244. $path = $man->decryptPath($input["path"]);
  245. $config = $man->getConfig();
  246. if ($man->verifyPath($path)) {
  247. $file =& $man->getFile($path);
  248. $config = $file->getConfig();
  249. $maxSizeBytes = preg_replace("/[^0-9]/", "", $config["upload.maxsize"]);
  250. if (strpos((strtolower($config["upload.maxsize"])), "k") > 0)
  251. $maxSizeBytes *= 1024;
  252. if (strpos((strtolower($config["upload.maxsize"])), "m") > 0)
  253. $maxSizeBytes *= (1024 * 1024);
  254. // Is chunked upload
  255. if (!isset($input["html4"])) {
  256. $filename = $input["name"];
  257. $chunk = isset($input["chunk"]) ? intval($input["chunk"]) : 0;
  258. $chunks = isset($input["chunks"]) ? intval($input["chunks"]) : 1;
  259. // No access, tool disabled
  260. if (in_array("upload", explode(',', $config['general.disabled_tools'])) || !$file->canWrite() || !checkBool($config["filesystem.writable"])) {
  261. $result->add("ACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.no_access}");
  262. return $result;
  263. }
  264. $file =& $man->getFile($path, $filename, MC_IS_FILE);
  265. if ($man->verifyFile($file, "upload") < 0) {
  266. $result->add("ACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), $man->getInvalidFileMsg());
  267. return $result;
  268. }
  269. // Hack attempt
  270. if ($filename == $config['filesystem.local.access_file_name']) {
  271. $result->add("MCACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.no_access}");
  272. return $result;
  273. }
  274. // Only peform IO when not in demo mode
  275. if (!checkBool($config['general.demo'])) {
  276. if ($chunk == 0 && $file->exists() && (!isset($config["upload.overwrite"]) || $config["upload.overwrite"] == false)) {
  277. $result->add("OVERWRITE_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.file_exists}");
  278. return $result;
  279. }
  280. if ($chunk == 0 && $file->exists() && $config["upload.overwrite"] == true)
  281. $file->delete();
  282. // Handle multipart upload
  283. if (isset($_FILES['file']['tmp_name'])) {
  284. // Write file
  285. $stream =& $file->open($chunk == 0 ? 'wb' : 'ab');
  286. if ($stream) {
  287. $in = fopen($_FILES['file']['tmp_name'], "rb");
  288. if ($in) {
  289. while ($buff = fread($in, 4096))
  290. $stream->write($buff);
  291. }
  292. $stream->close();
  293. }
  294. @unlink($_FILES['file']['tmp_name']);
  295. } else {
  296. // Write file
  297. $stream =& $file->open($chunk == 0 ? 'wb' : 'ab');
  298. if ($stream) {
  299. $in = fopen("php://input", "rb");
  300. if ($in) {
  301. while ($buff = fread($in, 4096))
  302. $stream->write($buff);
  303. }
  304. $stream->close();
  305. }
  306. }
  307. // Check file size
  308. if ($file->getLength() > $maxSizeBytes) {
  309. $file->delete();
  310. $result->add("SIZE_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.error_to_large}");
  311. return $result;
  312. }
  313. // Verify uploaded file, if it fails delete it
  314. $status = $man->verifyFile($file, "upload");
  315. if ($status < 0) {
  316. $file->delete();
  317. $result->add("FILTER_ERROR", $man->encryptPath($file->getAbsolutePath()), $man->getInvalidFileMsg());
  318. return $result;
  319. }
  320. // Import file when all chunks are complete
  321. if ($chunk == $chunks - 1) {
  322. clearstatcache();
  323. //mcdebug($chunk, $chunks, filesize($file->getAbsolutePath()), $chunk == 0 ? 'wb' : 'ab');
  324. $file->importFile();
  325. }
  326. }
  327. $result->add("OK", $man->encryptPath($file->getAbsolutePath()), "{#message.upload_ok}");
  328. return $result;
  329. } else {
  330. // Ok lets check the files array out.
  331. for ($i=0; isset($_FILES['file' . $i]['tmp_name']); $i++) {
  332. $filename = utf8_encode($input["name" . $i]);
  333. // Do nothing in demo mode
  334. if (checkBool($config['general.demo'])) {
  335. $result->add("DEMO_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.demo}");
  336. continue;
  337. }
  338. // No access, tool disabled
  339. if (in_array("upload", explode(',', $config['general.disabled_tools'])) || !$file->canWrite() || !checkBool($config["filesystem.writable"])) {
  340. $result->add("ACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.no_access}");
  341. continue;
  342. }
  343. // Get ext to glue back on
  344. $ext = "";
  345. if (strpos(basename($_FILES['file' . $i]['name']), ".") > 0) {
  346. $ar = explode('.', basename($_FILES['file' . $i]['name']));
  347. $ext = array_pop($ar);
  348. }
  349. $file =& $man->getFile($path, $filename . "." . $ext, "", MC_IS_FILE);
  350. if ($man->verifyFile($file, "upload") < 0) {
  351. $result->add("ACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), $man->getInvalidFileMsg());
  352. continue;
  353. }
  354. $config = $file->getConfig();
  355. if (is_uploaded_file($_FILES['file' . $i]['tmp_name'])) {
  356. // Hack attempt
  357. if ($filename == $config['filesystem.local.access_file_name']) {
  358. @unlink($_FILES['file' . $i]['tmp_name']);
  359. $result->add("MCACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.no_access}");
  360. continue;
  361. }
  362. if ($file->exists() && (!isset($config["upload.overwrite"]) || $config["upload.overwrite"] == false)) {
  363. @unlink($_FILES['file' . $i]['tmp_name']);
  364. $result->add("OVERWRITE_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.file_exists}");
  365. continue;
  366. }
  367. if ($file->exists() && $config["upload.overwrite"] == true)
  368. $file->delete();
  369. if (getClassName($file) == 'moxiecode_localfileimpl') {
  370. if (!move_uploaded_file($_FILES['file' . $i]['tmp_name'], $file->getAbsolutePath())) {
  371. $result->add("RW_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.upload_failed}");
  372. continue;
  373. }
  374. // Dispatch add event
  375. $file->importFile();
  376. } else
  377. $file->importFile($_FILES['file' . $i]['tmp_name']);
  378. if ($file->getLength() > $maxSizeBytes) {
  379. $file->delete();
  380. $result->add("SIZE_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.error_to_large}");
  381. continue;
  382. }
  383. // Verify uploaded file, if it fails delete it
  384. $status = $man->verifyFile($file, "upload");
  385. if ($status < 0) {
  386. $file->delete();
  387. $result->add("FILTER_ERROR", $man->encryptPath($file->getAbsolutePath()), $man->getInvalidFileMsg());
  388. continue;
  389. }
  390. $result->add("OK", $man->encryptPath($file->getAbsolutePath()), "{#message.upload_ok}");
  391. } else
  392. $result->add("GENERAL_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.upload_failed}");
  393. }
  394. }
  395. } else
  396. $result->add("PATH_ERROR", $man->encryptPath($path), "{#error.upload_failed}");
  397. return $result;
  398. }
  399. }
  400. // * * * * * * * * Private methods
  401. function _deleteFile(&$man, &$input) {
  402. $result = new Moxiecode_ResultSet("status,file,message");
  403. for ($i=0; isset($input["path" . $i]); $i++) {
  404. $file =& $man->getFile($input["path" . $i]);
  405. $config = $file->getConfig();
  406. if (checkBool($config['general.demo'])) {
  407. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), "{#error.demo}");
  408. continue;
  409. }
  410. if (!$man->isToolEnabled("delete", $config)) {
  411. trigger_error("{#error.no_access}", FATAL);
  412. die();
  413. }
  414. if (!$file->exists()) {
  415. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), "{#error.file_not_exists}");
  416. continue;
  417. }
  418. if ($man->verifyFile($file, "delete") < 0) {
  419. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), $man->getInvalidFileMsg());
  420. continue;
  421. }
  422. if (!$file->canWrite()) {
  423. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), "{#error.no_write_access}");
  424. continue;
  425. }
  426. if (!checkBool($config["filesystem.writable"])) {
  427. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), "{#error.no_write_access}");
  428. continue;
  429. }
  430. if ($file->delete($config['filesystem.delete_recursive']))
  431. $result->add("OK", $man->encryptPath($file->getAbsolutePath()), "{#message.delete_success}");
  432. else
  433. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), "{#error.delete_failed}");
  434. }
  435. return $result->toArray();
  436. }
  437. function _insertFiles(&$man, $input) {
  438. $result = new Moxiecode_ResultSet("name,path,url,size,type,created,modified,attribs,custom");
  439. $indata = array();
  440. for ($i=0; isset($input['path' . $i]); $i++) {
  441. $custom = array();
  442. $file = $man->getFile($input["path". $i]);
  443. if (!$file->exists()) {
  444. trigger_error("{#error.file_not_exists}", FATAL);
  445. die;
  446. }
  447. $ar = explode('.', $file->getName());
  448. $ext = array_pop($ar);
  449. $man->dispatchEvent("onCustomInfo", array(&$file, "insert", &$custom));
  450. $status = $man->dispatchEvent("onInsertFile", array(&$file));
  451. $config = $file->getConfig();
  452. $attribs = ($file->canRead() && checkBool($config["filesystem.readable"]) ? "R" : "-") . ($file->canWrite() && checkBool($config["filesystem.writable"]) ? "W" : "-");
  453. $url = $man->removeTrailingSlash($config['preview.urlprefix']) . $man->convertPathToURI($file->getAbsolutePath(), $config["preview.wwwroot"]);
  454. $result->add($file->getName(), $man->encryptPath($file->getAbsolutePath()), utf8_encode($url), $file->getLength(), $ext, $file->getCreationDate(), $file->getLastModified(), $attribs, $custom);
  455. }
  456. return $result->toArray();
  457. }
  458. function _listDefault(&$man, $file, $input, &$result, $filter_root_path) {
  459. $config = $man->getConfig();
  460. // Setup input file filter
  461. $inputFileFilter = new Moxiecode_BasicFileFilter();
  462. if (isset($input['include_directory_pattern']) && $input['include_directory_pattern'])
  463. $inputFileFilter->setIncludeDirectoryPattern($input['include_directory_pattern']);
  464. if (isset($input['exclude_directory_pattern']) && $input['exclude_directory_pattern'])
  465. $inputFileFilter->setExcludeDirectoryPattern($input['exclude_directory_pattern']);
  466. if (isset($input['include_file_pattern']) && $input['include_file_pattern'])
  467. $inputFileFilter->setIncludeFilePattern($input['include_file_pattern']);
  468. if (isset($input['exclude_file_pattern']) && $input['exclude_file_pattern'])
  469. $inputFileFilter->setExcludeFilePattern($input['exclude_file_pattern']);
  470. if (isset($input['extensions']) && $input['extensions'])
  471. $inputFileFilter->setIncludeExtensions($input['extensions']);
  472. // If file doesn't exists use default path
  473. if (!$file->exists()) {
  474. $file = $man->getFile($config['filesystem.path']);
  475. $result->setHeader("path", $man->encryptPath($file->getAbsolutePath()));
  476. $result->setHeader("visual_path", checkBool($config['general.user_friendly_paths']) ? $man->toVisualPath($file->getAbsolutePath()) : $man->encryptPath($file->getAbsolutePath()));
  477. }
  478. // List files
  479. $config = $file->getConfig();
  480. if ($file->isDirectory()) {
  481. // Setup file filter
  482. $fileFilter = new Moxiecode_BasicFileFilter();
  483. //$fileFilter->setDebugMode(true);
  484. $fileFilter->setIncludeDirectoryPattern($config['filesystem.include_directory_pattern']);
  485. $fileFilter->setExcludeDirectoryPattern($config['filesystem.exclude_directory_pattern']);
  486. $fileFilter->setIncludeFilePattern($config['filesystem.include_file_pattern']);
  487. $fileFilter->setExcludeFilePattern($config['filesystem.exclude_file_pattern']);
  488. $fileFilter->setIncludeExtensions($config['filesystem.extensions']);
  489. // If file is hidden then try the parent
  490. if ($fileFilter->accept($file) <= 0) {
  491. $file = $file->getParentFile();
  492. $result->setHeader("path", $man->encryptPath($file->getAbsolutePath()));
  493. $result->setHeader("visual_path", checkBool($config['general.user_friendly_paths']) ? $man->toVisualPath($file->getAbsolutePath()) : $man->encryptPath($file->getAbsolutePath()));
  494. }
  495. if (isset($input["filter"]) && $input["filter"] != null)
  496. $fileFilter->setIncludeWildcardPattern($input["filter"]);
  497. if (isset($input["only_dirs"]) && checkBool($input["only_dirs"]))
  498. $fileFilter->setOnlyDirs(true);
  499. else if (!checkBool($config["filesystem.list_directories"], true) || (isset($input["only_files"]) && checkBool($input["only_files"])))
  500. $fileFilter->setOnlyFiles(true);
  501. // List files
  502. $combinedFilter = new Moxiecode_CombinedFileFilter();
  503. $combinedFilter->addFilter($fileFilter);
  504. $combinedFilter->addFilter($inputFileFilter);
  505. $files =& $file->listFilesFiltered($combinedFilter);
  506. $showparent = isset($input["no_parent"]) ? checkBool($input["no_parent"]) : true;
  507. $showparent = $showparent && $man->verifyPath($file->getParent());
  508. if (!isset($input["only_dirs"]))
  509. $showparent = $showparent && checkBool($config["filesystem.list_directories"], true);
  510. // Add parent
  511. if ($showparent && !isset($input["only_files"])) {
  512. // Remove files below root
  513. if ($filter_root_path && getClassName($file) == 'moxiecode_localfileimpl') {
  514. if (!$man->isChildPath($filter_root_path, $file->getParent()))
  515. return $files;
  516. }
  517. if ($file->getAbsolutePath() != $filter_root_path)
  518. $result->add("..", $man->encryptPath($file->getParent()), -1, "parent", "", "", "", array());
  519. }
  520. } else
  521. trigger_error("The specified path is not a directory. Probably an incorrect setting for the filesystem.rootpath option.", FATAL);
  522. return $files;
  523. }
  524. /**
  525. * Lists files.
  526. */
  527. function _listFiles(&$man, $input) {
  528. $result = new Moxiecode_ResultSet("name,path,size,type,created,modified,attribs,custom");
  529. $config = $man->getConfig();
  530. $files = array();
  531. $rootNames = $man->_getRootNames();
  532. $filterRootPath = isset($input["root_path"]) && $input["root_path"] != null ? $man->toAbsPath($man->decryptPath($input["root_path"])) : null;
  533. if (isset($input["path"]) && $input["path"]) {
  534. // URL specified
  535. if (isset($input["url"]) && $input["path"] == '{default}')
  536. $input["path"] = $man->convertURIToPath($input["url"]);
  537. if (isset($input['remember_last_path'])) {
  538. if ($input['remember_last_path'] !== 'auto')
  539. $remember = checkBool($input['remember_last_path']);
  540. else
  541. $remember = checkBool($config['general.remember_last_path']);
  542. if ($remember) {
  543. if (isset($_COOKIE["MCManager_". $man->getType() . "_lastPath"]) && $input["path"] == '{default}') {
  544. $tmpPath = $_COOKIE["MCManager_". $man->getType() . "_lastPath"];
  545. if ($man->getFSFromPath($tmpPath) == "file" && $tmpPath)
  546. $input["path"] = $tmpPath;
  547. } else {
  548. if ($man->getFSFromPath($input["path"]) == "file")
  549. setcookie("MCManager_". $man->getType() . "_lastPath", $input["path"], time() + (3600*24*30)); // 30 days
  550. }
  551. }
  552. }
  553. $input["path"] = $man->toAbsPath($man->decryptPath($input["path"]));
  554. $result->setHeader("path", $man->encryptPath($input["path"]));
  555. $result->setHeader("visual_path", checkBool($config['general.user_friendly_paths']) ? $man->toVisualPath($input["path"]) : $man->encryptPath($input["path"]));
  556. // Move path inside rootpath if it's localfs
  557. if ($filterRootPath && $man->getFSFromPath($input["path"]) == 'file') {
  558. if (!$man->isChildPath($filterRootPath, $input["path"]))
  559. $input["path"] = $filterRootPath;
  560. $result->setHeader("path", $man->encryptPath($input["path"]));
  561. $result->setHeader("visual_path", checkBool($config['general.user_friendly_paths']) ? $man->toVisualPath($input["path"], $filterRootPath) : $man->encryptPath($input["path"]));
  562. }
  563. // Not valid path use default path
  564. if ($man->getFSFromPath($input["path"]) == 'file' && !$man->verifyPath($input["path"])) {
  565. $input["path"] = $config['filesystem.path'];
  566. $result->setHeader("path", $man->encryptPath($input["path"]));
  567. $result->setHeader("visual_path", checkBool($config['general.user_friendly_paths']) ? $man->toVisualPath($input["path"]) : $man->encryptPath($input["path"]));
  568. }
  569. $file =& $man->getFile($input["path"]);
  570. $config = $file->getConfig();
  571. $attribs = ($file->canRead() && checkBool($config["filesystem.readable"]) ? "R" : "-") . ($file->canWrite() && checkBool($config["filesystem.writable"]) ? "W" : "-");
  572. $result->setHeader("attribs", $attribs);
  573. $files = $this->_listDefault($man, $file, $input, $result, $filterRootPath);
  574. } else {
  575. trigger_error("ListFiles input not valid.", FATAL);
  576. die();
  577. }
  578. if (isset($input["page_size"])) {
  579. if ($file->getAbsolutePath() != $filterRootPath && $man->verifyPath($file->getParent()))
  580. $pageSize = $input["page_size"] - 1;
  581. else
  582. $pageSize = $input["page_size"];
  583. $pages = ceil(count($files) / $pageSize);
  584. // Setup response
  585. $result->setHeader("pages", $pages > 1 ? $pages : 1);
  586. $result->setHeader("count", count($files));
  587. if (!isset($input["page"]))
  588. $input["page"] = 0;
  589. // Remove non visible files
  590. $files = array_slice($files, ($input["page"] * $pageSize), $pageSize);
  591. }
  592. // Add directories
  593. $listFS = $man->getFSFromPath($input["path"]);
  594. foreach ($files as $file) {
  595. // Remove files below root
  596. if ($filterRootPath && $listFS == 'file') {
  597. if (!$man->isChildPath($filterRootPath, $file->getAbsolutePath()))
  598. continue;
  599. }
  600. // Setup fields
  601. $custom = array();
  602. // Attribs: R = Read, W = Write (cut/delete/rename), D = Download, S = Stream/Preview, I = Insert
  603. $attribs = ($file->canRead() && checkBool($config["filesystem.readable"]) ? "R" : "-") . ($file->canWrite() && checkBool($config["filesystem.writable"]) ? "W" : "-");
  604. $cdate = date($config['filesystem.datefmt'], $file->getCreationDate());
  605. $mdate = date($config['filesystem.datefmt'], $file->getLastModified());
  606. $filePath = $man->encryptPath($file->getAbsolutePath());
  607. if ($file->isFile())
  608. $type = getFileExt($file->getName());
  609. else
  610. $type = "folder";
  611. $man->dispatchEvent("onCustomInfo", array(&$file, "list", &$custom));
  612. // Special treatment of roots
  613. $name = $file->getName();
  614. if ($input["path"] == "root:///") {
  615. foreach ($rootNames as $rootPath => $rootName) {
  616. if ($file->getAbsolutePath() == $rootPath) {
  617. $name = $rootName;
  618. break;
  619. }
  620. }
  621. }
  622. $result->add(utf8_encode($name), $filePath, $file->isFile() ? $file->getLength() : -1, $type, $cdate, $mdate, $attribs, $custom);
  623. }
  624. if (isset($input["config"]))
  625. $result->setConfig($man->getJSConfig($config, $input["config"]));
  626. return $result->toArray();
  627. }
  628. function _filterFile(&$file, $input) {
  629. $config = $file->getConfig();
  630. // Setup file filter
  631. $fileFilter = new Moxiecode_BasicFileFilter();
  632. //$fileFilter->setDebugMode(true);
  633. $fileFilter->setIncludeDirectoryPattern($config['filesystem.include_directory_pattern']);
  634. $fileFilter->setExcludeDirectoryPattern($config['filesystem.exclude_directory_pattern']);
  635. $fileFilter->setIncludeFilePattern($config['filesystem.include_file_pattern']);
  636. $fileFilter->setExcludeFilePattern($config['filesystem.exclude_file_pattern']);
  637. $fileFilter->setIncludeExtensions($config['filesystem.extensions']);
  638. if (isset($input["only_dirs"]) && checkBool($input["only_dirs"]))
  639. $fileFilter->setOnlyDirs(true);
  640. return ($fileFilter->accept($file) > 0);
  641. }
  642. function _createDirs(&$man, $input) {
  643. $result = new Moxiecode_ResultSet("status,file,message");
  644. // Get input data
  645. $path = $man->decryptPath($input['path']);
  646. $dir =& $man->getFile($path);
  647. $config = $dir->getConfig();
  648. if (checkBool($config["general.demo"])) {
  649. $result->add("DEMO_ERROR", $man->encryptPath($dir->getAbsolutePath()), "{#error.demo}");
  650. return $result->toArray();
  651. }
  652. if (!$man->isToolEnabled("createdir", $config)) {
  653. trigger_error("{#error.no_access}", FATAL);
  654. die();
  655. }
  656. for ($i=0; isset($input['name' . $i]); $i++) {
  657. // Get dir info
  658. $name = $input['name' . $i];
  659. $template = false;
  660. if (isset($input['template' . $i]))
  661. $template = $man->decryptPath($input['template' . $i]);
  662. // Setup target file
  663. $file =& $man->getFile($path, $name, MC_IS_DIRECTORY);
  664. if ($man->verifyFile($file, "createdir") < 0) {
  665. $result->add("ACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), $man->getInvalidFileMsg());
  666. continue;
  667. }
  668. // Check write access
  669. if (!checkBool($config["filesystem.writable"])) {
  670. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), "{#error.no_write_access}");
  671. continue;
  672. }
  673. // Setup template dir
  674. if ($template) {
  675. $templateFile =& $man->getFile($template, "", MC_IS_DIRECTORY);
  676. if (!$templateFile->exists()) {
  677. $result->add("TEMPLATE_ERROR", $man->encryptPath($templateFile->getAbsolutePath()), "{#error.template_missing}");
  678. continue;
  679. }
  680. if ($man->verifyFile($templateFile, "createdir") < 0) {
  681. $result->add("ACCESS_ERROR", $man->encryptPath($file->getAbsolutePath()), $man->getInvalidFileMsg());
  682. continue;
  683. }
  684. } else
  685. $templateFile = null;
  686. // Check if target exists
  687. if ($file->exists()) {
  688. $result->add("EXISTS_ERROR", $man->encryptPath($file->getAbsolutePath()), "{#error.folder_exists}");
  689. continue;
  690. }
  691. // Create directory
  692. if ($templateFile)
  693. $status = $templateFile->copyTo($file);
  694. else
  695. $status = $file->mkdir();
  696. if ($status)
  697. $result->add("OK", $man->encryptPath($file->getAbsolutePath()), "{#message.directory_ok}");
  698. else
  699. $result->add("FAILED", $man->encryptPath($file->getAbsolutePath()), "{#error.no_write_access}");
  700. }
  701. return $result->toArray();
  702. }
  703. function _getConfig(&$man, $input) {
  704. $globalConfig = $man->getConfig();
  705. if (!isset($input['prefixes']))
  706. $input["prefixes"] = "*";
  707. // If debug mode return all
  708. if (!isset($input['path']) && isset($input['debug']) && checkBool($globalConfig['general.debug']))
  709. return $man->getConfig();
  710. if (!isset($input['path'])) {
  711. trigger_error("{#error.file_not_exists}", FATAL);
  712. die;
  713. }
  714. $file =& $man->getFile($input['path']);
  715. if (!$file->exists()) {
  716. trigger_error("{#error.file_not_exists}", FATAL);
  717. die;
  718. }
  719. $config = $file->getConfig();
  720. // If debug mode return all
  721. if (isset($input['debug']) && checkBool($globalConfig['general.debug']))
  722. return $config;
  723. return $man->getJSConfig($config, $input["prefixes"]);
  724. }
  725. /**
  726. * Simple keepalive function.
  727. */
  728. function _keepAlive(&$man, $input) {
  729. $result = new Moxiecode_ResultSet("status,time,message");
  730. $man->dispatchEvent("onKeepAlive");
  731. // Return status KEEPALIVE, current time on server and message.
  732. $result->add("KEEPALIVE", time(), "{#message.keepalive}");
  733. return $result->toArray();
  734. }
  735. /**
  736. * Simple loopback function. Used for debugging purposes of the RPC functionality.
  737. */
  738. function _loopBack($input) {
  739. return $input;
  740. }
  741. /**#@-*/
  742. }
  743. // Add plugin to MCManager
  744. $man->registerPlugin("core", new Moxiecode_CorePlugin());
  745. ?>