TYPO3 CMS  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 
28 
33 {
39  public $doc;
40 
46  public $title;
47 
53  public $uid;
54 
61 
67  public $returnUrl;
68 
74  public $content;
75 
79  public function __construct()
80  {
81  parent::__construct();
82  $GLOBALS['SOBE'] = $this;
83  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
84  $this->init();
85  }
86 
94  protected function init()
95  {
96  // Initialize GPvars:
97  $this->uid = (int)GeneralUtility::_GP('uid');
98  $lang = $this->getLanguageService();
99 
100  $this->returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
101  // Cleaning and checking uid
102  if ($this->uid > 0) {
103  $this->fileOrFolderObject = ResourceFactory::getInstance()
104  ->retrieveFileOrFolderObject('file:' . $this->uid);
105  }
106  if (!$this->fileOrFolderObject) {
107  $title = $lang->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:paramError', true);
108  $message = $lang->sL('LLL:EXT:lang/locallang_mod_file_list.xlf:targetNoDir', true);
109  throw new \RuntimeException($title . ': ' . $message, 1436895930);
110  }
111  if ($this->fileOrFolderObject->getStorage()->getUid() === 0) {
113  'You are not allowed to access files outside your storages',
114  1436895931
115  );
116  }
117 
118  // If a folder should be renamed, AND the returnURL should go to the old directory name, the redirect is forced
119  // so the redirect will NOT end in an error message
120  // this case only happens if you select the folder itself in the foldertree and then use the clickmenu to
121  // rename the folder
122  if ($this->fileOrFolderObject instanceof Folder) {
123  $parsedUrl = parse_url($this->returnUrl);
124  $queryParts = GeneralUtility::explodeUrl2Array(urldecode($parsedUrl['query']));
125  if ($queryParts['id'] === $this->fileOrFolderObject->getCombinedIdentifier()) {
126  $this->returnUrl = str_replace(
127  urlencode($queryParts['id']),
128  urlencode($this->fileOrFolderObject->getStorage()->getRootLevelFolder()->getCombinedIdentifier()),
129  $this->returnUrl
130  );
131  }
132  }
133 
134  $pathInfo = [
135  'combined_identifier' => $this->fileOrFolderObject->getCombinedIdentifier(),
136  ];
137  $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($pathInfo);
138  $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
139  $this->moduleTemplate->addJavaScriptCode(
140  'ReplaceFileOnlineJavaScript',
141  'function backToList() {top.goToModule("file_FilelistList");}'
142  );
143  }
144 
150  public function main()
151  {
152  $lang = $this->getLanguageService();
153  $code = '<form action="'
154  . htmlspecialchars(BackendUtility::getModuleUrl('tce_file'))
155  . '" role="form" method="post" name="editform" enctype="multipart/form-data">';
156 
157  // Making the formfields for renaming:
158  $code .= '
159  <div class="form-group">
160  <input type="checkbox" value="1" id="keepFilename" name="file[replace][1][keepFilename]"> <label for="keepFilename">'
161  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.keepfiletitle')
162  . '</label>
163  </div>
164 
165  <div class="form-group">
166  <label for="file_replace">' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.selectfile') . '</label>
167  <div class="input-group col-xs-6">
168  <input type="text" name="fakefile" id="fakefile" class="form-control input-xlarge" readonly>
169  <a class="input-group-addon btn btn-primary" onclick="TYPO3.jQuery(\'#file_replace\').click();">'
170  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.browse')
171  . '</a>
172  </div>
173  <input class="form-control" type="file" id="file_replace" name="replace_1" style="visibility: hidden;" />
174  </div>
175 
176  <script>
177  TYPO3.jQuery(\'#file_replace\').change(function(){
178  TYPO3.jQuery(\'#fakefile\').val(TYPO3.jQuery(this).val());
179  });
180  </script>
181 
182  <input type="hidden" name="overwriteExistingFiles" value="replace" />
183  <input type="hidden" name="file[replace][1][data]" value="1" />
184  <input type="hidden" name="file[replace][1][uid]" value="' . $this->uid . '" />
185  ';
186  // Making submit button:
187  $code .= '
188  <div class="form-group">
189  <input class="btn btn-primary" type="submit" value="'
190  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.submit', true) . '" />
191  <input class="btn btn-danger" type="submit" value="'
192  . $lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.cancel', true)
193  . '" onclick="backToList(); return false;" />
194  <input type="hidden" name="redirect" value="' . htmlspecialchars($this->returnUrl) . '" />
195  </div>
196  ';
197  $code .= '</form>';
198 
199  $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
200  // csh button
201  $cshButton = $buttonBar->makeHelpButton()
202  ->setModuleName('xMOD_csh_corebe')
203  ->setFieldName('file_rename');
204  $buttonBar->addButton($cshButton);
205 
206  // Back button
207  if ($this->returnUrl) {
208  $returnButton = $buttonBar->makeLinkButton()
209  ->setHref(GeneralUtility::linkThisUrl($this->returnUrl))
210  ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.goBack'))
211  ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-view-go-back', Icon::SIZE_SMALL));
212  $buttonBar->addButton($returnButton);
213  }
214 
215  $this->content .= '<h1>' . $lang->sL('LLL:EXT:lang/locallang_core.xlf:file_replace.php.pagetitle') . '</h1>';
216  // Add the HTML as a section:
217  $this->content .= '<div>' . $code . '</div>';
218 
219  $this->moduleTemplate->setContent($this->content);
220  }
221 
229  public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
230  {
231  $this->main();
232 
233  $response->getBody()->write($this->moduleTemplate->renderContent());
234  return $response;
235  }
236 
240  protected function getLanguageService()
241  {
242  return $GLOBALS['LANG'];
243  }
244 }
mainAction(ServerRequestInterface $request, ResponseInterface $response)
static linkThisUrl($url, array $getParams=[])
static explodeUrl2Array($string, $multidim=false)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']