edit.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  11. * Loads the edit file page
  12. *
  13. * @package modx
  14. * @subpackage manager.controllers
  15. */
  16. class SystemFileEditManagerController extends modManagerController {
  17. /** @var string The basename of the file */
  18. public $filename = '';
  19. /** @var array An array of data about the file */
  20. public $fileRecord = array();
  21. /** @var bool A boolean stating whether or not this file can be saved */
  22. public $canSave = false;
  23. /**
  24. * Check for any permissions or requirements to load page
  25. * @return bool
  26. */
  27. public function checkPermissions() {
  28. return $this->modx->hasPermission('file_view');
  29. }
  30. /**
  31. * Register custom CSS/JS for the page
  32. * @return void
  33. */
  34. public function loadCustomCssJs() {
  35. $this->addJavascript($this->modx->getOption('manager_url').'assets/modext/sections/system/file/edit.js');
  36. $this->addHtml('<script type="text/javascript">Ext.onReady(function() {
  37. MODx.load({
  38. xtype: "modx-page-file-edit"
  39. ,file: "'.$this->filename.'"
  40. ,record: '.$this->modx->toJSON($this->fileRecord).'
  41. ,canSave: '.($this->canSave ? 1 : 0).'
  42. });
  43. });</script>');
  44. }
  45. /**
  46. * Custom logic code here for setting placeholders, etc
  47. * @param array $scriptProperties
  48. * @return mixed
  49. */
  50. public function process(array $scriptProperties = array()) {
  51. $placeholders = array();
  52. $this->modx->lexicon->load('file');
  53. if (empty($_GET['file'])) return $this->failure($this->modx->lexicon('file_err_nf'));
  54. $this->loadWorkingContext();
  55. /* format filename */
  56. $this->filename = preg_replace('#([\\\\]+|/{2,})#', '/',$scriptProperties['file']);
  57. $this->filename = htmlspecialchars(strip_tags($this->filename));
  58. $source = $this->getSource();
  59. $this->fileRecord = $source->getObjectContents($this->filename);
  60. $this->fileRecord['source'] = $source->get('id');
  61. if (empty($this->fileRecord)) {
  62. $errors = $source->getErrors();
  63. $error = '';
  64. foreach ($errors as $k => $msg) {
  65. $error .= $msg;
  66. }
  67. return $this->failure($error);
  68. }
  69. $this->canSave = $this->fileRecord['is_writable'] ? true : false;
  70. $placeholders['fa'] = $this->fileRecord;
  71. $placeholders['OnFileEditFormPrerender'] = $this->fireEvents();
  72. $this->fileRecord['basename'] = htmlspecialchars($this->fileRecord['basename']);
  73. $this->fileRecord['name'] = htmlspecialchars($this->fileRecord['name']);
  74. $this->fileRecord['path'] = htmlspecialchars($this->fileRecord['path']);
  75. return $placeholders;
  76. }
  77. /**
  78. * Get the active source
  79. * @return modMediaSource
  80. */
  81. public function getSource() {
  82. /** @var modMediaSource|modFileMediaSource $source */
  83. $this->modx->loadClass('sources.modMediaSource');
  84. $source = $this->modx->getOption('source',$this->scriptProperties,false);
  85. if (!empty($source)) {
  86. $source = $this->modx->getObject('source.modMediaSource',$source);
  87. }
  88. if (empty($source)) {
  89. $source = modMediaSource::getDefaultSource($this->modx);
  90. }
  91. if (!$source->getWorkingContext()) {
  92. return $this->failure($this->modx->lexicon('permission_denied'));
  93. }
  94. $source->setRequestProperties($this->scriptProperties);
  95. $source->initialize();
  96. return $source;
  97. }
  98. /**
  99. * Invoke OnFileEditFormPrerender event
  100. * @return string
  101. */
  102. public function fireEvents() {
  103. $onFileEditFormPrerender = $this->modx->invokeEvent('OnFileEditFormPrerender',array(
  104. 'mode' => modSystemEvent::MODE_UPD,
  105. 'file' => $this->filename,
  106. 'fa' => &$this->fileRecord,
  107. ));
  108. if (is_array($onFileEditFormPrerender)) $onFileEditFormPrerender = implode('',$onFileEditFormPrerender);
  109. return $onFileEditFormPrerender;
  110. }
  111. /**
  112. * Return the pagetitle
  113. *
  114. * @return string
  115. */
  116. public function getPageTitle() {
  117. return $this->modx->lexicon('file_edit').': '.basename($this->filename);
  118. }
  119. /**
  120. * Return the location of the template file
  121. * @return string
  122. */
  123. public function getTemplateFile() {
  124. return '';
  125. }
  126. /**
  127. * Specify the language topics to load
  128. * @return array
  129. */
  130. public function getLanguageTopics() {
  131. return array('file');
  132. }
  133. }