PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/core/js/tiny_mce/plugins/filemanager/classes/FileManager/FileManagerPlugin.php

http://github.com/forkcms/forkcms
PHP | 484 lines | 336 code | 94 blank | 54 comment | 79 complexity | d0296bbc0753e9ce7c81b9e61eaf4c15 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * $Id: FileManagerPlugin.php 751 2009-10-20 12:05:36Z spocke $
  4. *
  5. * @package MCFileManager
  6. * @author Moxiecode
  7. * @copyright Copyright ďż˝ 2007, Moxiecode Systems AB, All rights reserved.
  8. */
  9. require_once(MCMANAGER_ABSPATH . "FileManager/FileSystems/ZipFileImpl.php");
  10. /**
  11. * This plugin class contans the core logic of the MCFileManager application.
  12. *
  13. * @package MCFileManager
  14. */
  15. class Moxiecode_FileManagerPlugin extends Moxiecode_ManagerPlugin {
  16. /**#@+
  17. * @access public
  18. */
  19. /**
  20. * Constructs a new MCFileManager instance.
  21. */
  22. function Moxiecode_FileManagerPlugin() {
  23. }
  24. function onPreInit(&$man, $prefix) {
  25. global $mcFileManagerConfig;
  26. if ($prefix == "fm") {
  27. $man->setConfig($mcFileManagerConfig, false);
  28. $man->setLangPackPath("fm");
  29. return false;
  30. }
  31. return true;
  32. }
  33. /**
  34. * Register file system.
  35. */
  36. function onInit(&$man) {
  37. // Register file systems
  38. $man->registerFileSystem('zip', 'Moxiecode_ZipFileImpl');
  39. return true;
  40. }
  41. /**
  42. * Gets executed when a RPC command is to be executed.
  43. *
  44. * @param MCManager $man MCManager reference that the plugin is assigned to.
  45. * @param string $cmd RPC Command to be executed.
  46. * @param object $input RPC input object data.
  47. * @return object Result data from RPC call or null if it should be passed to the next handler in chain.
  48. */
  49. function onRPC(&$man, $cmd, $input) {
  50. switch ($cmd) {
  51. case "copyFiles":
  52. return $this->_copyFiles($man, $input);
  53. case "moveFiles":
  54. return $this->_moveFiles($man, $input);
  55. case "createDocs":
  56. return $this->_createDocs($man, $input);
  57. case "createZip":
  58. return $this->_createZip($man, $input);
  59. case "loadContent":
  60. return $this->_loadContent($man, $input);
  61. case "saveContent":
  62. return $this->_saveContent($man, $input);
  63. }
  64. return null;
  65. }
  66. /**
  67. * Gets called when custom data is to be added for a file custom data can for example be
  68. * plugin specific name value items that should get added into a file listning.
  69. *
  70. * @param MCManager $man MCManager reference that the plugin is assigned to.
  71. * @param BaseFile $file File reference to add custom info/data to.
  72. * @param string $type Where is the info needed for example list or info.
  73. * @param Array $custom Name/Value array to add custom items to.
  74. * @return bool true/false if the execution of the event chain should continue.
  75. */
  76. function onCustomInfo(&$man, &$file, $type, &$input) {
  77. switch ($type) {
  78. case "list":
  79. $input['previewable'] = $file->isFile() && $man->verifyFile($file, "preview") >= 0;
  80. $input['editable'] = $file->isFile() && $man->verifyFile($file, "edit") >= 0;
  81. break;
  82. }
  83. return true; // Pass to next
  84. }
  85. // * * * * * * * * Private methods
  86. function _createZip(&$man, &$input) {
  87. $result = new Moxiecode_ResultSet("status,fromfile,tofile,message");
  88. $config = $man->getConfig();
  89. if (!$man->isToolEnabled("zip", $config)) {
  90. trigger_error("{#error.no_access}", FATAL);
  91. die();
  92. }
  93. $zipFile = $man->getFile($man->decryptPath($input["topath"]), $input["toname"] . ".zip");
  94. if ($zipFile->exists()) {
  95. trigger_error("{#error.tofile_exists}", FATAL);
  96. die();
  97. }
  98. $toZip = $man->getFile("zip://" . $man->decryptPath($input["topath"]), $input["toname"] . ".zip/");
  99. for ($i=0; isset($input["frompath" . $i]); $i++) {
  100. $fromFile =& $man->getFile($man->decryptPath($input["frompath" . $i]));
  101. $toFile = $man->getFile("zip://" . $man->decryptPath($input["topath"]) . '/' . $input["toname"] . ".zip", $fromFile->getName());
  102. $zipFile = $man->getFile($man->decryptPath($input["topath"]) . '/' . $input["toname"] . ".zip");
  103. $config = $fromFile->getConfig();
  104. if (checkBool($config['general.demo'])) {
  105. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.demo}");
  106. continue;
  107. }
  108. // Check write access
  109. if (!checkBool($config["filesystem.writable"])) {
  110. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_write_access}");
  111. continue;
  112. }
  113. if ($man->verifyFile($fromFile, "createzip") < 0) {
  114. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  115. continue;
  116. }
  117. if ($man->verifyFile($zipFile, "createzip") < 0) {
  118. $result->add("FAILED", $man->encryptPath($zipFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  119. continue;
  120. }
  121. if (!$fromFile->exists()) {
  122. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_from_file}");
  123. continue;
  124. }
  125. if ($man->verifyFile($fromFile, "zip") < 0) {
  126. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  127. continue;
  128. }
  129. if ($fromFile->copyTo($toFile))
  130. $result->add("OK", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#message.zip_success}");
  131. else
  132. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.createzip_failed}");
  133. }
  134. return $result->toArray();
  135. }
  136. function _copyFiles(&$man, &$input) {
  137. $result = new Moxiecode_ResultSet("status,fromfile,tofile,message");
  138. $config = $man->getConfig();
  139. if (!$man->isToolEnabled("copy", $config)) {
  140. trigger_error("{#error.no_access}", FATAL);
  141. die();
  142. }
  143. for ($i=0; isset($input["frompath" . $i]); $i++) {
  144. $fromFile =& $man->getFile($input["frompath" . $i]);
  145. $fromType = $fromFile->isFile() ? MC_IS_FILE : MC_IS_DIRECTORY;
  146. if (isset($input["toname" . $i])) {
  147. $toFile =& $man->getFile($fromFile->getParent(), $input["toname" . $i], $fromType);
  148. } else {
  149. if (isset($input["topath" . $i]))
  150. $toFile =& $man->getFile($input["topath" . $i], "", $fromType);
  151. else
  152. $toFile =& $man->getFile($input["topath"], $fromFile->getName(), $fromType);
  153. }
  154. if (!$fromFile->exists()) {
  155. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_from_file}");
  156. continue;
  157. }
  158. if ($toFile->exists()) {
  159. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.tofile_exists}");
  160. continue;
  161. }
  162. $toConfig = $toFile->getConfig();
  163. if (checkBool($toConfig['general.demo'])) {
  164. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.demo}");
  165. continue;
  166. }
  167. if ($man->verifyFile($toFile, "copy") < 0) {
  168. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  169. continue;
  170. }
  171. if (!$toFile->canWrite()) {
  172. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_write_access}");
  173. continue;
  174. }
  175. if (!checkBool($toConfig["filesystem.writable"])) {
  176. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_write_access}");
  177. continue;
  178. }
  179. // Check if file can be zipped
  180. if (getClassName($toFile) == 'moxiecode_zipfileimpl') {
  181. if ($man->verifyFile($fromFile, "zip") < 0) {
  182. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  183. continue;
  184. }
  185. }
  186. // Check if file can be unzipped
  187. if (getClassName($fromFile) == 'moxiecode_zipfileimpl') {
  188. if ($man->verifyFile($toFile, "unzip") < 0) {
  189. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  190. continue;
  191. }
  192. }
  193. if ($fromFile->copyTo($toFile))
  194. $result->add("OK", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#message.copy_success}");
  195. else
  196. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.copy_failed}");
  197. }
  198. return $result->toArray();
  199. }
  200. function _moveFiles(&$man, &$input) {
  201. $result = new Moxiecode_ResultSet("status,fromfile,tofile,message");
  202. $config = $man->getConfig();
  203. if (!$man->isToolEnabled("rename", $config) && !$man->isToolEnabled("cut", $config)) {
  204. trigger_error("{#error.no_access}", FATAL);
  205. die();
  206. }
  207. for ($i=0; isset($input["frompath" . $i]); $i++) {
  208. $fromFile =& $man->getFile($input["frompath" . $i]);
  209. $fromType = $fromFile->isFile() ? MC_IS_FILE : MC_IS_DIRECTORY;
  210. if (isset($input["toname" . $i])) {
  211. $toFile =& $man->getFile($fromFile->getParent(), $input["toname" . $i], $fromType);
  212. } else {
  213. if (isset($input["topath" . $i]))
  214. $toFile =& $man->getFile($input["topath" . $i], "", $fromType);
  215. else
  216. $toFile =& $man->getFile($input["topath"], $fromFile->getName(), $fromType);
  217. }
  218. // User tried to change extension
  219. if ($fromFile->isFile() && getFileExt($fromFile->getName()) != getFileExt($toFile->getName())) {
  220. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.move_failed}");
  221. continue;
  222. }
  223. if (!$fromFile->exists()) {
  224. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_from_file}");
  225. continue;
  226. }
  227. if ($toFile->exists()) {
  228. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.tofile_exists}");
  229. continue;
  230. }
  231. $toConfig = $toFile->getConfig();
  232. if (checkBool($toConfig['general.demo'])) {
  233. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.demo}");
  234. continue;
  235. }
  236. if ($man->verifyFile($toFile, "rename") < 0) {
  237. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  238. continue;
  239. }
  240. if (!$toFile->canWrite()) {
  241. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_write_access}");
  242. continue;
  243. }
  244. if (!checkBool($toConfig["filesystem.writable"])) {
  245. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_write_access}");
  246. continue;
  247. }
  248. // Check if file can be zipped
  249. if (getClassName($toFile) == 'moxiecode_zipfileimpl') {
  250. if ($man->verifyFile($fromFile, "zip") < 0) {
  251. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  252. continue;
  253. }
  254. }
  255. // Check if file can be unzipped
  256. if (getClassName($fromFile) == 'moxiecode_zipfileimpl') {
  257. if ($man->verifyFile($toFile, "unzip") < 0) {
  258. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  259. continue;
  260. }
  261. }
  262. if ($fromFile->renameTo($toFile))
  263. $result->add("OK", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#message.move_success}");
  264. else
  265. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.move_failed}");
  266. }
  267. return $result->toArray();
  268. }
  269. function _createDocs(&$man, &$input) {
  270. $result = new Moxiecode_ResultSet("status,fromfile,tofile,message");
  271. $config = $man->getConfig();
  272. if (!$man->isToolEnabled("createdoc", $config)) {
  273. trigger_error("{#error.no_access}", FATAL);
  274. die();
  275. }
  276. for ($i=0; isset($input["frompath" . $i]) && isset($input["toname" . $i]); $i++) {
  277. $fromFile =& $man->getFile($input["frompath" . $i]);
  278. $ext = getFileExt($fromFile->getName());
  279. $toFile =& $man->getFile($input["topath" . $i], $input["toname" . $i] . '.' . $ext);
  280. $toConfig = $toFile->getConfig();
  281. if (checkBool($toConfig['general.demo'])) {
  282. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.demo}");
  283. continue;
  284. }
  285. if ($man->verifyFile($toFile, "createdoc") < 0) {
  286. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), $man->getInvalidFileMsg());
  287. continue;
  288. }
  289. if (!$toFile->canWrite()) {
  290. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_write_access}");
  291. continue;
  292. }
  293. if (!checkBool($toConfig["filesystem.writable"])) {
  294. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.no_write_access}");
  295. continue;
  296. }
  297. if (!$fromFile->exists()) {
  298. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.template_missing}");
  299. continue;
  300. }
  301. if ($fromFile->copyTo($toFile)) {
  302. // Replace title
  303. $fields = $input["fields"];
  304. // Replace all fields
  305. if ($fields) {
  306. // Read all data
  307. $stream = $toFile->open('r');
  308. $fileData = $stream->readToEnd();
  309. $stream->close();
  310. // Replace fields
  311. foreach ($fields as $name => $value)
  312. $fileData = str_replace('${' . $name . '}', htmlentities($value), $fileData);
  313. // Write file data
  314. $stream = $toFile->open('w');
  315. $stream->write($fileData);
  316. $stream->close();
  317. }
  318. $result->add("OK", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#message.createdoc_success}");
  319. } else
  320. $result->add("FAILED", $man->encryptPath($fromFile->getAbsolutePath()), $man->encryptPath($toFile->getAbsolutePath()), "{#error.createdoc_failed}");
  321. }
  322. return $result->toArray();
  323. }
  324. function _loadContent(&$man, &$input) {
  325. $file = $man->getFile($input["path"]);
  326. $config = $file->getConfig();
  327. if (!$man->isToolEnabled("edit", $config)) {
  328. trigger_error("{#error.no_access}", FATAL);
  329. die();
  330. }
  331. if (!checkBool($config["filesystem.writable"])) {
  332. trigger_error("{#error.no_write_access}", FATAL);
  333. die();
  334. }
  335. if ($man->verifyFile($file, "edit") < 0) {
  336. trigger_error($man->getInvalidFileMsg(), FATAL);
  337. die();
  338. }
  339. if (!$file->canWrite()) {
  340. trigger_error("{#error.no_write_access}", FATAL);
  341. die();
  342. }
  343. $reader = $file->open('r');
  344. if ($reader) {
  345. $content = $reader->readToEnd();
  346. $reader->close();
  347. } else {
  348. trigger_error("{#error.no_read_access}", FATAL);
  349. die();
  350. }
  351. return array("content" => $content);
  352. }
  353. function _saveContent(&$man, &$input) {
  354. $file = $man->getFile($input["path"]);
  355. $config = $file->getConfig();
  356. if (!$man->isToolEnabled("edit", $config)) {
  357. trigger_error("{#error.no_access}", FATAL);
  358. die();
  359. }
  360. if (checkBool($config['general.demo'])) {
  361. trigger_error("{#error.demo}", FATAL);
  362. die();
  363. }
  364. if (!checkBool($config["filesystem.writable"])) {
  365. trigger_error("{#error.no_write_access}", FATAL);
  366. die();
  367. }
  368. if ($man->verifyFile($file, "edit") < 0) {
  369. trigger_error($man->getInvalidFileMsg(), FATAL);
  370. die();
  371. }
  372. if (!$file->canWrite()) {
  373. trigger_error("{#error.no_write_access}", FATAL);
  374. die();
  375. }
  376. $writer = $file->open('w');
  377. if ($writer) {
  378. $writer->write($input["content"]);
  379. $writer->close();
  380. } else {
  381. trigger_error("{#error.no_write_access}", FATAL);
  382. die();
  383. }
  384. return array("status" => "OK");
  385. }
  386. /**#@-*/
  387. }
  388. // Add plugin to MCManager
  389. $man->registerPlugin("filemanager", new Moxiecode_FileManagerPlugin(), "fm");
  390. ?>