[BUGFIX] Split jQuery and DataTables into separate files
[Packages/TYPO3.CMS.git] / typo3 / sysext / integrity / Classes / RteImagesCommand.php
1 <?php
2 namespace TYPO3\CMS\Integrity;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Cleaner module: RTE magicc images
31 * User function called from tx_lowlevel_cleaner_core configured in ext_localconf.php
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 */
35 /**
36 * Looking for RTE images integrity
37 *
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 * @package TYPO3
40 * @subpackage tx_lowlevel
41 */
42 class RteImagesCommand extends \TYPO3\CMS\Integrity\CleanerCommand {
43
44 /**
45 * @todo Define visibility
46 */
47 public $checkRefIndex = TRUE;
48
49 /**
50 * Constructor
51 *
52 * @todo Define visibility
53 */
54 public function __construct() {
55 parent::__construct();
56 // Setting up help:
57 $this->cli_help['name'] = 'rte_images -- Looking up all occurencies of RTEmagic images in the database and check existence of parent and copy files on the file system plus report possibly lost files of this type.';
58 $this->cli_help['description'] = trim('
59 Assumptions:
60 - a perfect integrity of the reference index table (always update the reference index table before using this tool!)
61 - that all RTEmagic image files in the database are registered with the soft reference parser "images"
62 - images found in deleted records are included (means that you might find lost RTEmagic images after flushing deleted records)
63
64 The assumptions are not requirements by the TYPO3 API but reflects the de facto implementation of most TYPO3 installations.
65 However, many custom fields using an RTE will probably not have the "images" soft reference parser registered and so the index will be incomplete and not listing all RTEmagic image files.
66 The consequence of this limitation is that you should be careful if you wish to delete lost RTEmagic images - they could be referenced from a field not parsed by the "images" soft reference parser!
67
68 Automatic Repair of Errors:
69 - Will search for double-usages of RTEmagic images and make copies as required.
70 - Lost files can be deleted automatically by setting the value "lostFiles" as an optional parameter to --AUTOFIX, but otherwise delete them manually if you do not recognize them as used somewhere the system does not know about.
71
72 Manual repair suggestions:
73 - Missing files: Re-insert missing files or edit record where the reference is found.
74 ');
75 $this->cli_help['examples'] = '/.../cli_dispatch.phpsh lowlevel_cleaner rte_images -s -r
76 Reports problems with RTE images';
77 }
78
79 /**
80 * Analyse situation with RTE magic images. (still to define what the most useful output is).
81 * Fix methods: API in t3lib_refindex that allows to change the value of a reference (we could copy the files) or remove reference
82 *
83 * @return array
84 * @todo Define visibility
85 */
86 public function main() {
87 global $TYPO3_DB;
88 // Initialize result array:
89 $resultArray = array(
90 'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
91 'headers' => array(
92 'completeFileList' => array('Complete list of used RTEmagic files', 'Both parent and copy are listed here including usage count (which should in theory all be "1"). This list does not exclude files that might be missing.', 1),
93 'RTEmagicFilePairs' => array('Statistical info about RTEmagic files', '(copy used as index)', 0),
94 'doubleFiles' => array('Duplicate RTEmagic image files', 'These files are RTEmagic images found used in multiple records! RTEmagic images should be used by only one record at a time. A large amount of such images probably stems from previous versions of TYPO3 (before 4.2) which did not support making copies automatically of RTEmagic images in case of new copies / versions.', 3),
95 'missingFiles' => array('Missing RTEmagic image files', 'These files are not found in the file system! Should be corrected!', 3),
96 'lostFiles' => array('Lost RTEmagic files from uploads/', 'These files you might be able to delete but only if _all_ RTEmagic images are found by the soft reference parser. If you are using the RTE in third-party extensions it is likely that the soft reference parser is not applied correctly to their RTE and thus these "lost" files actually represent valid RTEmagic images, just not registered. Lost files can be auto-fixed but only if you specifically set "lostFiles" as parameter to the --AUTOFIX option.', 2)
97 ),
98 'RTEmagicFilePairs' => array(),
99 'doubleFiles' => array(),
100 'completeFileList' => array(),
101 'missingFiles' => array(),
102 'lostFiles' => array()
103 );
104 // Select all RTEmagic files in the reference table (only from soft references of course)
105 $recs = $TYPO3_DB->exec_SELECTgetRows('*', 'sys_refindex', 'ref_table=' . $TYPO3_DB->fullQuoteStr('_FILE', 'sys_refindex') . ' AND ref_string LIKE ' . $TYPO3_DB->fullQuoteStr('%/RTEmagic%', 'sys_refindex') . ' AND softref_key=' . $TYPO3_DB->fullQuoteStr('images', 'sys_refindex'), '', 'sorting DESC');
106 // Traverse the files and put into a large table:
107 if (is_array($recs)) {
108 foreach ($recs as $rec) {
109 $filename = basename($rec['ref_string']);
110 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($filename, 'RTEmagicC_')) {
111 $original = 'RTEmagicP_' . preg_replace('/\\.[[:alnum:]]+$/', '', substr($filename, 10));
112 $infoString = $this->infoStr($rec);
113 // Build index:
114 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['exists'] = @is_file((PATH_site . $rec['ref_string']));
115 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original'] = substr($rec['ref_string'], 0, -strlen($filename)) . $original;
116 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original_exists'] = @is_file((PATH_site . $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']));
117 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['count']++;
118 $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'][$rec['hash']] = $infoString;
119 $resultArray['completeFileList'][$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']]++;
120 $resultArray['completeFileList'][$rec['ref_string']]++;
121 // Missing files:
122 if (!$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['exists']) {
123 $resultArray['missingFiles'][$rec['ref_string']] = $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'];
124 }
125 if (!$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original_exists']) {
126 $resultArray['missingFiles'][$resultArray['RTEmagicFilePairs'][$rec['ref_string']]['original']] = $resultArray['RTEmagicFilePairs'][$rec['ref_string']]['usedIn'];
127 }
128 }
129 }
130 // Searching for duplicates:
131 foreach ($resultArray['RTEmagicFilePairs'] as $fileName => $fileInfo) {
132 if ($fileInfo['count'] > 1 && $fileInfo['exists'] && $fileInfo['original_exists']) {
133 $resultArray['doubleFiles'][$fileName] = $fileInfo['usedIn'];
134 }
135 }
136 }
137 // Now, ask for RTEmagic files inside uploads/ folder:
138 $cleanerModules = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules'];
139 $cleanerMode = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($cleanerModules['lost_files'][0]);
140 $resLostFiles = $cleanerMode->main(array(), FALSE, TRUE);
141 if (is_array($resLostFiles['RTEmagicFiles'])) {
142 foreach ($resLostFiles['RTEmagicFiles'] as $fileName) {
143 if (!isset($resultArray['completeFileList'][$fileName])) {
144 $resultArray['lostFiles'][$fileName] = $fileName;
145 }
146 }
147 }
148 ksort($resultArray['RTEmagicFilePairs']);
149 ksort($resultArray['completeFileList']);
150 ksort($resultArray['missingFiles']);
151 ksort($resultArray['doubleFiles']);
152 ksort($resultArray['lostFiles']);
153 return $resultArray;
154 }
155
156 /**
157 * Mandatory autofix function
158 * Will run auto-fix on the result array. Echos status during processing.
159 *
160 * @param array $resultArray Result array from main() function
161 * @return void
162 * @todo Define visibility
163 */
164 public function main_autoFix($resultArray) {
165 $limitTo = $this->cli_args['--AUTOFIX'][0];
166 if (is_array($resultArray['doubleFiles'])) {
167 if (!$limitTo || $limitTo === 'doubleFiles') {
168 echo 'FIXING double-usages of RTE files in uploads/: ' . LF;
169 foreach ($resultArray['RTEmagicFilePairs'] as $fileName => $fileInfo) {
170 // Only fix something if there is a usage count of more than 1 plus if both original and copy exists:
171 if ($fileInfo['count'] > 1 && $fileInfo['exists'] && $fileInfo['original_exists']) {
172 // Traverse all records using the file:
173 $c = 0;
174 foreach ($fileInfo['usedIn'] as $hash => $recordID) {
175 if ($c == 0) {
176 echo ' Keeping file ' . $fileName . ' for record ' . $recordID . LF;
177 } else {
178 // CODE below is adapted from "class.tx_impexp.php" where there is support for duplication of RTE images:
179 echo ' Copying file ' . basename($fileName) . ' for record ' . $recordID . ' ';
180 // Initialize; Get directory prefix for file and set the original name:
181 $dirPrefix = dirname($fileName) . '/';
182 $rteOrigName = basename($fileInfo['original']);
183 // If filename looks like an RTE file, and the directory is in "uploads/", then process as a RTE file!
184 if ($rteOrigName && \TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($dirPrefix, 'uploads/') && @is_dir((PATH_site . $dirPrefix))) {
185 // RTE:
186 // From the "original" RTE filename, produce a new "original" destination filename which is unused.
187 $fileProcObj = $this->getFileProcObj();
188 $origDestName = $fileProcObj->getUniqueName($rteOrigName, PATH_site . $dirPrefix);
189 // Create copy file name:
190 $pI = pathinfo($fileName);
191 $copyDestName = dirname($origDestName) . '/RTEmagicC_' . substr(basename($origDestName), 10) . '.' . $pI['extension'];
192 if (!@is_file($copyDestName) && !@is_file($origDestName) && $origDestName === \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($origDestName) && $copyDestName === \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($copyDestName)) {
193 echo ' to ' . basename($copyDestName);
194 if ($bypass = $this->cli_noExecutionCheck($fileName)) {
195 echo $bypass;
196 } else {
197 // Making copies:
198 \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move(PATH_site . $fileInfo['original'], $origDestName);
199 \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move(PATH_site . $fileName, $copyDestName);
200 clearstatcache();
201 if (@is_file($copyDestName)) {
202 $sysRefObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\ReferenceIndex');
203 $error = $sysRefObj->setReferenceValue($hash, substr($copyDestName, strlen(PATH_site)));
204 if ($error) {
205 echo ' - ERROR: TYPO3\\CMS\\Core\\Database\\ReferenceIndex::setReferenceValue(): ' . $error . LF;
206 die;
207 } else {
208 echo ' - DONE';
209 }
210 } else {
211 echo ' - ERROR: File "' . $copyDestName . '" was not created!';
212 }
213 }
214 } else {
215 echo ' - ERROR: Could not construct new unique names for file!';
216 }
217 } else {
218 echo ' - ERROR: Maybe directory of file was not within "uploads/"?';
219 }
220 echo LF;
221 }
222 $c++;
223 }
224 }
225 }
226 } else {
227 echo 'Bypassing fixing of double-usages since --AUTOFIX was not "doubleFiles"' . LF;
228 }
229 }
230 if (is_array($resultArray['lostFiles'])) {
231 if ($limitTo === 'lostFiles') {
232 echo 'Removing lost RTEmagic files from folders inside uploads/: ' . LF;
233 foreach ($resultArray['lostFiles'] as $key => $value) {
234 $absFileName = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($value);
235 echo 'Deleting file: "' . $absFileName . '": ';
236 if ($bypass = $this->cli_noExecutionCheck($absFileName)) {
237 echo $bypass;
238 } else {
239 if ($absFileName && @is_file($absFileName)) {
240 unlink($absFileName);
241 echo 'DONE';
242 } else {
243 echo ' ERROR: File "' . $absFileName . '" was not found!';
244 }
245 }
246 echo LF;
247 }
248 }
249 } else {
250 echo 'Bypassing fixing of double-usages since --AUTOFIX was not "lostFiles"' . LF;
251 }
252 }
253
254 /**
255 * Returns file processing object, initialized only once.
256 *
257 * @return object File processor object
258 * @todo Define visibility
259 */
260 public function getFileProcObj() {
261 if (!is_object($this->fileProcObj)) {
262 $this->fileProcObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Utility\\File\\ExtendedFileUtility');
263 $this->fileProcObj->init($GLOBALS['FILEMOUNTS'], $GLOBALS['TYPO3_CONF_VARS']['BE']['fileExtensions']);
264 $this->fileProcObj->init_actionPerms($GLOBALS['BE_USER']->getFileoperationPermissions());
265 }
266 return $this->fileProcObj;
267 }
268
269 }
270
271
272 ?>