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