edit.class.php 4.5 KB

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