TYPO3  7.6
ReplaceFileController.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 use TYPO3\CMS\Core\Resource\Exception\InsufficientFileAccessPermissionsException;
28 
33 {
34 
40  public $doc;
41 
47  public $title;
48 
54  public $uid;
55 
62 
68  public $returnUrl;
69 
75  public $content;
76 
80  public function __construct()
81  {
82  parent::__construct();
83  $GLOBALS['SOBE'] = $this;
84  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
85  $this->init();
86  }
87 
95  protected function init()
96  {
97  // Initialize GPvars:
98  $this->uid = (int)GeneralUtility::_GP('uid');
99  $lang = $this->getLanguageService();
100 
101  $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
102  // Cleaning and checking uid
103  if ($this->uid > 0) {
104  $this->fileOrFolderObject = ResourceFactory::getInstance()
105  ->retrieveFileOrFolderObject('file:' . $this->uid);
106  }
107  if (!$this->fileOrFolderObject) {
108  $title = $lang->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', true);
109  $message = $lang->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', true);
110  throw new \RuntimeException($title . ': ' . $message, 1436895930);
111  }
112  if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
114  'You are not allowed to access files outside your storages',
115  1436895931
116  );
117  }
118 
119  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
120  // so the redirect will NOT end in an error message
121  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
122  // rename the folder
123  if ($this->fileOrFolderObject instanceof Folder) {
124  $parsedUrl = parse_url($this->returnUrl);
125  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
126  if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
127  $this->returnUrl = str_replace(
128  urlencode($queryParts['id']),
129  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
130  $this->returnUrl
131  );
132  }
133  }
134 
135  $pathInfo = [
136  'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
137  ];
138  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
139  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
140  $this->moduleTemplate->addJavaScriptCode(
141  'ReplaceFileOnlineJavaScript',
142  'function backToList() {top.goToModule("file_FilelistList");}'
143  );
144  }
145 
151  public function main()
152  {
153  $lang = $this->getLanguageService();
154  $code = '<form action="'
155  . htmlspecialchars(BackendUtility::getModuleUrl('tce_file'))
156  . '" role="form" method="post" name="editform" enctype="multipart/form-data">';
157 
158  // Making the formfields for renaming:
159  $code .= '
160  <div class="form-group">
161  <input type="checkbox" value="1" id="keepFilename" name="file[replace][1][keepFilename]"> <label for="keepFilename">'
162  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.keepfiletitle')
163  . '</label>
164  </div>
165 
166  <div class="form-group">
167  <label for="file_replace">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.selectfile') . '</label>
168  <div class="input-group col-xs-6">
169  <input type="text" name="fakefile" id="fakefile" class="form-control input-xlarge" readonly>
170  <a class="input-group-addon btn btn-primary" onclick="TYPO3.jQuery(\'#file_replace\').click();">'
171  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.browse')
172  . '</a>
173  </div>
174  <input class="form-control" type="file" id="file_replace" name="replace_1" style="visibility: hidden;" />
175  </div>
176 
177  <script>
178  TYPO3.jQuery(\'#file_replace\').change(function(){
179  TYPO3.jQuery(\'#fakefile\').val(TYPO3.jQuery(this).val());
180  });
181  </script>
182 
183  <input type="hidden" name="overwriteExistingFiles" value="replace" />
184  <input type="hidden" name="file[replace][1][data]" value="1" />
185  <input type="hidden" name="file[replace][1][uid]" value="' . $this->uid . '" />
186  ';
187  // Making submit button:
188  $code .= '
189  <div class="form-group">
190  <input class="btn btn-primary" type="submit" value="'
191  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.submit', true) . '" />
192  <input class="btn btn-danger" type="submit" value="'
193  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', true)
194  . '" onclick="backToList(); return false;" />
195  <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
196  </div>
197  ';
198  $code .= '</form>';
199 
200  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
201  // csh button
202  $cshButton = $buttonBar->makeHelpButton()
203  ->setModuleName('xMOD_csh_corebe')
204  ->setFieldName('file_rename');
205  $buttonBar->addButton($cshButton);
206 
207  // Back button
208  if ($this->returnUrl) {
209  $returnButton = $buttonBar->makeLinkButton()
210  ->setHref(GeneralUtility::linkThisUrl($this->returnUrl))
211  ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack', true))
212  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
213  $buttonBar->addButton($returnButton);
214  }
215 
216  $this->content .= '<h1>' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.pagetitle') . '</h1>';
217  // Add the HTML as a section:
218  $this->content .= '<div>' . $code . '</div>';
219 
220  $this->moduleTemplate->setContent($this->content);
221  }
222 
231  {
232  $this->main();
233 
234  $response->getBody()->write($this->moduleTemplate->renderContent());
235  return $response;
236  }
237 
241  protected function getLanguageService()
242  {
243  return $GLOBALS['LANG'];
244  }
245 }