[TASK] Fix CGL violations against InlineControlStructure
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / classes / class.tx_linkvalidator_processor.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 - 2011 Michael Miousse (michael.miousse@infoglobe.ca)
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 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 $GLOBALS['LANG']->includeLLFile('EXT:linkvalidator/modfuncreport/locallang.xml');
26
27 /**
28 * This class provides Processing plugin implementation
29 *
30 * @author Michael Miousse <michael.miousse@infoglobe.ca>
31 * @author Jochen Rieger <j.rieger@connecta.ag>
32 * @package TYPO3
33 * @subpackage linkvalidator
34 */
35 class tx_linkvalidator_Processor {
36
37 /**
38 * Array of tables and fields to search for broken links
39 *
40 * @var array
41 */
42 protected $searchFields = array();
43
44 /**
45 * List of comma separated page uids (rootline downwards)
46 *
47 * @var string
48 */
49 protected $pidList = '';
50
51 /**
52 * Array of tables and the number of external links they contain
53 *
54 * @var array
55 */
56 protected $linkCounts = array();
57
58 /**
59 * Array of tables and the number of broken external links they contain
60 *
61 * @var array
62 */
63 protected $brokenLinkCounts = array();
64
65 /**
66 * Array of tables and records containing broken links
67 *
68 * @var array
69 */
70 protected $recordsWithBrokenLinks = array();
71
72 /**
73 * Array for hooks for own checks
74 *
75 * @var tx_linkvalidator_linktype_Abstract[]
76 */
77 protected $hookObjectsArr = array();
78
79 /**
80 * Array with information about the current page
81 *
82 * @var array
83 */
84 protected $extPageInTreeInfo = array();
85
86 /**
87 * Reference to the current element with table:uid, e.g. pages:85
88 *
89 * @var string
90 */
91 protected $recordReference = '';
92
93 /**
94 * Linked page together with a possible anchor, e.g. 85#c105
95 *
96 * @var string
97 */
98 protected $pageWithAnchor = '';
99
100 /**
101 * Fill hookObjectsArr with different link types and possible XClasses.
102 */
103 public function __construct() {
104 // Hook to handle own checks
105 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])) {
106 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] as $key => $classRef) {
107 $this->hookObjectsArr[$key] = t3lib_div::getUserObj($classRef);
108 }
109 }
110 }
111
112 /**
113 * Store all the needed configuration values in class variables
114 *
115 * @param array $searchField List of fields in which to search for links
116 * @param string $pid List of comma separated page uids in which to search for links
117 * @return void
118 */
119 public function init(array $searchField, $pid) {
120 $this->searchFields = $searchField;
121 $this->pidList = $pid;
122
123 foreach ($searchField as $tableName => $table) {
124 t3lib_div::loadTCA($tableName);
125 }
126 }
127
128 /**
129 * Find all supported broken links and store them in tx_linkvalidator_link
130 *
131 * @param array $checkOptions List of hook object to activate
132 * @param boolean $considerHidden Defines whether to look into hidden fields or not
133 * @return void
134 */
135 public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
136 $results = array();
137 if (count($checkOptions) > 0) {
138 $checkKeys = array_keys($checkOptions);
139 $checkLinkTypeCondition = ' and link_type in (\'' . implode('\',\'', $checkKeys) . '\')';
140
141 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_linkvalidator_link',
142 '(record_pid in (' . $this->pidList . ')'
143 . ' or ( record_uid IN (' . $this->pidList . ') and table_name like \'pages\')) '
144 . $checkLinkTypeCondition);
145
146 // Traverse all configured tables
147 foreach ($this->searchFields as $table => $fields) {
148 if ($table === 'pages') {
149 $where = 'deleted = 0 AND uid IN (' . $this->pidList . ')';
150 } else {
151 $where = 'deleted = 0 AND pid IN (' . $this->pidList . ')';
152 }
153 if (!$considerHidden) {
154 $where .= t3lib_BEfunc::BEenableFields($table);
155 }
156 // If table is not configured, assume the extension is not installed and therefore no need to check it
157 if (!is_array($GLOBALS['TCA'][$table])) {
158 continue;
159 }
160
161 // Re-init selectFields for table
162 $selectFields = 'uid, pid';
163 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
164
165 // TODO: only select rows that have content in at least one of the relevant fields (via OR)
166 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $where);
167 // Get record rows of table
168 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
169 // Analyse each record
170 $this->analyzeRecord($results, $table, $fields, $row);
171 }
172 $GLOBALS['TYPO3_DB']->sql_free_result($res);
173 }
174
175 foreach ($this->hookObjectsArr as $key => $hookObj) {
176 if ((is_array($results[$key])) && empty($checkOptions) || (is_array($results[$key]) && $checkOptions[$key])) {
177 // Check them
178 foreach ($results[$key] as $entryKey => $entryValue) {
179 $table = $entryValue['table'];
180 $record = array();
181 $record['headline'] = $entryValue['row'][$GLOBALS['TCA'][$table]['ctrl']['label']];
182 $record['record_pid'] = $entryValue['row']['pid'];
183 $record['record_uid'] = $entryValue['uid'];
184 $record['table_name'] = $table;
185 $record['link_title'] = $entryValue['link_title'];
186 $record['field'] = $entryValue['field'];
187 $record['last_check'] = time();
188
189 $this->recordReference = $entryValue['substr']['recordRef'];
190
191 $this->pageWithAnchor = $entryValue['pageAndAnchor'];
192
193 if (!empty($this->pageWithAnchor)) {
194 // Page with anchor, e.g. 18#1580
195 $url = $this->pageWithAnchor;
196 } else {
197 $url = $entryValue['substr']['tokenValue'];
198 }
199
200 $this->linkCounts[$table]++;
201 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
202 // Broken link found
203 if (!$checkURL) {
204 $response = array();
205 $response['valid'] = FALSE;
206 $response['errorParams'] = $hookObj->getErrorParams();
207 $this->brokenLinkCounts[$table]++;
208 $record['link_type'] = $key;
209 $record['url'] = $url;
210 $record['url_response'] = serialize($response);
211 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
212 } elseif (t3lib_div::_GP('showalllinks')) {
213 $response = array();
214 $response['valid'] = TRUE;
215 $this->brokenLinkCounts[$table]++;
216 $record['url'] = $url;
217 $record['link_type'] = $key;
218 $record['url_response'] = serialize($response);
219 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
220 }
221 }
222 }
223 }
224 }
225 }
226
227
228 /**
229 * Find all supported broken links for a specific record
230 *
231 * @param array $results Array of broken links
232 * @param string $table Table name of the record
233 * @param array $fields Array of fields to analyze
234 * @param array $record Record to analyse
235 * @return void
236 */
237 public function analyzeRecord(array &$results, $table, array $fields, array $record) {
238
239 // Put together content of all relevant fields
240 $haystack = '';
241 /** @var t3lib_parsehtml $htmlParser */
242 $htmlParser = t3lib_div::makeInstance('t3lib_parsehtml');
243
244 $idRecord = $record['uid'];
245
246 // Get all references
247 foreach ($fields as $field) {
248 $haystack .= $record[$field] . ' --- ';
249 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
250
251 $valueField = $record[$field];
252
253 // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
254 if ($conf['softref'] && strlen($valueField)) {
255 // Explode the list of soft references/parameters
256 $softRefs = t3lib_BEfunc::explodeSoftRefParserList($conf['softref']);
257 // Traverse soft references
258 foreach ($softRefs as $spKey => $spParams) {
259 /** @var t3lib_softrefproc $softRefObj Create or get the soft reference object */
260 $softRefObj = &t3lib_BEfunc::softRefParserObj($spKey);
261
262 // If there is an object returned...
263 if (is_object($softRefObj)) {
264
265 // Do processing
266 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
267 if (!empty($resultArray['elements'])) {
268
269 if ($spKey == 'typolink_tag') {
270 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
271 } else {
272 $this->analyseLinks($resultArray, $results, $record, $field, $table);
273 }
274 }
275 }
276 }
277 }
278 }
279 }
280
281 /**
282 * Find all supported broken links for a specific link list
283 *
284 * @param array $resultArray findRef parsed records
285 * @param array $results Array of broken links
286 * @param array $record UID of the current record
287 * @param string $field The current field
288 * @param string $table The current table
289 * @return void
290 */
291 protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
292 foreach ($resultArray['elements'] as $element) {
293 $r = $element['subst'];
294 $type = '';
295 $idRecord = $record['uid'];
296 if (!empty($r)) {
297 /** @var tx_linkvalidator_linktype_Abstract $hookObj */
298 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
299 $type = $hookObj->fetchType($r, $type, $keyArr);
300 // Store the type that was found
301 // This prevents overriding by internal validator
302 if (!empty($type)) {
303 $r['type'] = $type;
304 }
305 }
306 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['substr'] = $r;
307 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['row'] = $record;
308 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['table'] = $table;
309 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['field'] = $field;
310 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['uid'] = $idRecord;
311
312 }
313 }
314 }
315
316 /**
317 * Find all supported broken links for a specific typoLink
318 *
319 * @param array $resultArray findRef parsed records
320 * @param array $results Array of broken links
321 * @param t3lib_parsehtml $htmlParser Instance of html parser
322 * @param array $record The current record
323 * @param string $field The current field
324 * @param string $table The current table
325 * @return void
326 */
327 protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table) {
328 $currentR = array();
329 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
330 $idRecord = $record['uid'];
331 $type = '';
332 $title = '';
333 for ($i = 1; $i < count($linkTags); $i += 2) {
334 $referencedRecordType = '';
335 foreach ($resultArray['elements'] as $element) {
336 $type = '';
337 $r = $element['subst'];
338
339 if (!empty($r['tokenID'])) {
340 if (substr_count($linkTags[$i], $r['tokenID'])) {
341 // Type of referenced record
342 if (strpos($r['recordRef'], 'pages') !== FALSE) {
343 $currentR = $r;
344 // Contains number of the page
345 $referencedRecordType = $r['tokenValue'];
346 $wasPage = TRUE;
347
348 // Append number of content element to the page saved in the last loop
349 } elseif ((strpos($r['recordRef'], 'tt_content') !== FALSE) && (isset($wasPage) && $wasPage === TRUE)) {
350 $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
351 $wasPage = FALSE;
352 } else {
353 $currentR = $r;
354 }
355 $title = strip_tags($linkTags[$i]);
356 }
357 }
358 }
359 /** @var tx_linkvalidator_linktype_Abstract $hookObj */
360 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
361 $type = $hookObj->fetchType($currentR, $type, $keyArr);
362 // Store the type that was found
363 // This prevents overriding by internal validator
364 if (!empty($type)) {
365 $currentR['type'] = $type;
366 }
367 }
368
369 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['substr'] = $currentR;
370 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['row'] = $record;
371 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['table'] = $table;
372 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['field'] = $field;
373 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['uid'] = $idRecord;
374 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['link_title'] = $title;
375 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['pageAndAnchor'] = $referencedRecordType;
376
377 }
378 }
379
380 /**
381 * Fill a marker array with the number of links found in a list of pages
382 *
383 * @param string $curPage Comma separated list of page uids
384 * @return array Marker array with the number of links found
385 */
386 public function getLinkCounts($curPage) {
387 $markerArray = array();
388
389 if (empty($this->pidList)) {
390 $this->pidList = $curPage;
391 }
392
393 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
394 'count(uid) as nbBrokenLinks,link_type',
395 'tx_linkvalidator_link',
396 'record_pid in (' . $this->pidList . ')',
397 'link_type'
398 ))
399 ) {
400 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
401 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
402 $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
403 }
404 }
405 $GLOBALS['TYPO3_DB']->sql_free_result($res);
406 return $markerArray;
407 }
408
409 /**
410 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
411 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
412 * this is necessary to create the object that is used recursively by the original function.
413 *
414 * Generates a list of page uids from $id. List does not include $id itself.
415 * The only pages excluded from the list are deleted pages.
416 *
417 * @param integer $id Start page id
418 * @param integer $depth Depth to traverse down the page tree.
419 * @param integer $begin is an optional integer that determines at which
420 * @param string $permsClause Perms clause
421 * @param boolean $considerHidden Whether to consider hidden pages or not
422 * @return string Returns the list with a comma in the end (if any pages selected!)
423 */
424 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
425 $depth = intval($depth);
426 $begin = intval($begin);
427 $id = intval($id);
428 $theList = '';
429
430 if ($depth > 0) {
431 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
432 'uid,title,hidden,extendToSubpages',
433 'pages',
434 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
435 );
436
437 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
438 if ($begin <= 0 && ($row['hidden'] == 0 || $considerHidden == 1)) {
439 $theList .= $row['uid'] . ',';
440 $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
441 }
442 if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) || $considerHidden == 1)) {
443 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
444 }
445 }
446 $GLOBALS['TYPO3_DB']->sql_free_result($res);
447 }
448 return $theList;
449 }
450
451 /**
452 * @param array $pageInfo Array with uid, title, hidden, extendToSubpages from pages table
453 * @return boolean TRUE if rootline contains a hidden page, FALSE if not
454 */
455 public function getRootLineIsHidden(array $pageInfo) {
456 $hidden = FALSE;
457 if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
458 $hidden = TRUE;
459 } else {
460 if ($pageInfo['pid'] > 0) {
461 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
462 'uid,title,hidden,extendToSubpages',
463 'pages',
464 'uid=' . $pageInfo['pid']
465 );
466
467 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
468 $hidden = $this->getRootLineIsHidden($row);
469 }
470 $GLOBALS['TYPO3_DB']->sql_free_result($res);
471 } else {
472 $hidden = FALSE;
473 }
474 }
475 return $hidden;
476
477 }
478
479 }
480
481 ?>