TYPO3 CMS  TYPO3_6-2
ValidatorTask.php
Go to the documentation of this file.
1 <?php
3 
19 
26 
30  protected $sleepTime;
31 
35  protected $sleepAfterFinish;
36 
40  protected $countInARun;
41 
47  protected $totalBrokenLink = 0;
48 
54  protected $oldTotalBrokenLink = 0;
55 
61  protected $templateMail;
62 
68  protected $configuration = array();
69 
76 
82  protected $emailTemplateFile;
83 
89  protected $depth;
90 
96  protected $page;
97 
103  protected $email;
104 
111 
117  public function getEmail() {
118  return $this->email;
119  }
120 
127  public function setEmail($email) {
128  $this->email = $email;
129  }
130 
136  public function getEmailOnBrokenLinkOnly() {
138  }
139 
147  $this->emailOnBrokenLinkOnly = $emailOnBrokenLinkOnly;
148  }
149 
155  public function getPage() {
156  return $this->page;
157  }
158 
165  public function setPage($page) {
166  $this->page = $page;
167  }
168 
174  public function getDepth() {
175  return $this->depth;
176  }
177 
184  public function setDepth($depth) {
185  $this->depth = $depth;
186  }
187 
193  public function getEmailTemplateFile() {
195  }
196 
204  $this->emailTemplateFile = $emailTemplateFile;
205  }
206 
212  public function getConfiguration() {
213  return $this->configuration;
214  }
215 
222  public function setConfiguration($configuration) {
223  $this->configuration = $configuration;
224  }
225 
232  public function execute() {
233  $this->setCliArguments();
234  $successfullyExecuted = TRUE;
235  if (!file_exists(($file = GeneralUtility::getFileAbsFileName($this->emailTemplateFile)))
236  && !empty($this->email)
237  ) {
238  if ($this->emailTemplateFile === 'EXT:linkvalidator/res/mailtemplate.html') {
239  // Update the default email template file path
240  $this->emailTemplateFile = 'EXT:linkvalidator/Resources/Private/Templates/mailtemplate.html';
241  $this->save();
242  } else {
243  throw new \InvalidArgumentException(
244  $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.error.invalidEmailTemplateFile'),
245  '1295476972'
246  );
247  }
248  }
249  $htmlFile = GeneralUtility::getURL($file);
250  $this->templateMail = \TYPO3\CMS\Core\Html\HtmlParser::getSubpart($htmlFile, '###REPORT_TEMPLATE###');
251  // The array to put the content into
252  $html = array();
253  $pageSections = '';
254  $this->isDifferentToLastRun = FALSE;
255  $pageList = GeneralUtility::trimExplode(',', $this->page, TRUE);
256  $modTs = $this->loadModTsConfig($this->page);
257  if (is_array($pageList)) {
258  foreach ($pageList as $page) {
259  $pageSections .= $this->checkPageLinks($page);
260  }
261  }
262  if ($this->totalBrokenLink != $this->oldTotalBrokenLink) {
263  $this->isDifferentToLastRun = TRUE;
264  }
265  if ($this->totalBrokenLink > 0 && (!$this->emailOnBrokenLinkOnly || $this->isDifferentToLastRun) && !empty($this->email)) {
266  $successfullyExecuted = $this->reportEmail($pageSections, $modTs);
267  }
268  return $successfullyExecuted;
269  }
270 
277  protected function checkPageLinks($page) {
278  $page = (int)$page;
279  $pageSections = '';
280  $pageIds = '';
281  $oldLinkCounts = array();
282  $modTs = $this->loadModTsConfig($page);
283  $searchFields = $this->getSearchField($modTs);
284  $linkTypes = $this->getLinkTypes($modTs);
286  $processor = GeneralUtility::makeInstance('TYPO3\\CMS\\Linkvalidator\\LinkAnalyzer');
287  if ($page === 0) {
288  $rootLineHidden = FALSE;
289  } else {
290  $pageRow = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', 'pages', 'uid=' . $page);
291  $rootLineHidden = $processor->getRootLineIsHidden($pageRow);
292  }
293  if (!$rootLineHidden || $modTs['checkhidden'] == 1) {
294  $pageIds = $processor->extGetTreeList($page, $this->depth, 0, '1=1', $modTs['checkhidden']);
295  if (isset($pageRow) && $pageRow['hidden'] == 0 || $modTs['checkhidden'] == 1) {
296  // \TYPO3\CMS\Linkvalidator\LinkAnalyzer->extGetTreeList() always adds trailing comma
297  $pageIds .= $page;
298  }
299  }
300  if (!empty($pageIds)) {
301  $processor->init($searchFields, $pageIds);
302  if (!empty($this->email)) {
303  $oldLinkCounts = $processor->getLinkCounts($page);
304  $this->oldTotalBrokenLink += $oldLinkCounts['brokenlinkCount'];
305  }
306  $processor->getLinkStatistics($linkTypes, $modTs['checkhidden']);
307  if (!empty($this->email)) {
308  $linkCounts = $processor->getLinkCounts($page);
309  $this->totalBrokenLink += $linkCounts['brokenlinkCount'];
310  $pageSections = $this->buildMail($page, $pageIds, $linkCounts, $oldLinkCounts);
311  }
312  }
313  return $pageSections;
314  }
315 
323  protected function loadModTsConfig($page) {
324  $modTs = BackendUtility::getModTSconfig($page, 'mod.linkvalidator');
325  $parseObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\Parser\\TypoScriptParser');
326  $parseObj->parse($this->configuration);
327  if (count($parseObj->errors) > 0) {
328  $parseErrorMessage = $GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.error.invalidTSconfig') . '<br />';
329  foreach ($parseObj->errors as $errorInfo) {
330  $parseErrorMessage .= $errorInfo[0] . '<br />';
331  }
332  throw new \Exception($parseErrorMessage, '1295476989');
333  }
334  $tsConfig = $parseObj->setup;
335  $modTs = $modTs['properties'];
336  $overrideTs = $tsConfig['mod.']['tx_linkvalidator.'];
337  if (is_array($overrideTs)) {
339  }
340  return $modTs;
341  }
342 
349  protected function getSearchField(array $modTS) {
350  // Get the searchFields from TypoScript
351  foreach ($modTS['searchFields.'] as $table => $fieldList) {
352  $fields = GeneralUtility::trimExplode(',', $fieldList);
353  foreach ($fields as $field) {
354  $searchFields[$table][] = $field;
355  }
356  }
357  return isset($searchFields) ? $searchFields : array();
358  }
359 
366  protected function getLinkTypes(array $modTS) {
367  $linkTypes = array();
368  $typesTmp = GeneralUtility::trimExplode(',', $modTS['linktypes'], TRUE);
369  if (is_array($typesTmp)) {
370  if (!empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks']) && is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])) {
371  foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] as $type => $value) {
372  if (in_array($type, $typesTmp)) {
373  $linkTypes[$type] = 1;
374  }
375  }
376  }
377  }
378  return $linkTypes;
379  }
380 
389  protected function reportEmail($pageSections, array $modTsConfig) {
390  $content = \TYPO3\CMS\Core\Html\HtmlParser::substituteSubpart($this->templateMail, '###PAGE_SECTION###', $pageSections);
392  $markerArray = array();
394  $validEmailList = array();
396  $sendEmail = TRUE;
397  $markerArray['totalBrokenLink'] = $this->totalBrokenLink;
398  $markerArray['totalBrokenLink_old'] = $this->oldTotalBrokenLink;
399  // Hook
400  if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['reportEmailMarkers'])) {
401  foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['reportEmailMarkers'] as $userFunc) {
402  $params = array(
403  'pObj' => &$this,
404  'markerArray' => $markerArray
405  );
406  $newMarkers = GeneralUtility::callUserFunction($userFunc, $params, $this);
407  if (is_array($newMarkers)) {
408  $markerArray = GeneralUtility::array_merge($markerArray, $newMarkers);
409  }
410  unset($params);
411  }
412  }
413  $content = \TYPO3\CMS\Core\Html\HtmlParser::substituteMarkerArray($content, $markerArray, '###|###', TRUE, TRUE);
415  $mail = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
416  if (empty($modTsConfig['mail.']['fromemail'])) {
417  $modTsConfig['mail.']['fromemail'] = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFromAddress();
418  }
419  if (empty($modTsConfig['mail.']['fromname'])) {
420  $modTsConfig['mail.']['fromname'] = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFromName();
421  }
422  if (GeneralUtility::validEmail($modTsConfig['mail.']['fromemail'])) {
423  $mail->setFrom(array($modTsConfig['mail.']['fromemail'] => $modTsConfig['mail.']['fromname']));
424  } else {
425  throw new \Exception($GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.error.invalidFromEmail'), '1295476760');
426  }
427  if (GeneralUtility::validEmail($modTsConfig['mail.']['replytoemail'])) {
428  $mail->setReplyTo(array($modTsConfig['mail.']['replytoemail'] => $modTsConfig['mail.']['replytoname']));
429  }
430  if (!empty($modTsConfig['mail.']['subject'])) {
431  $mail->setSubject($modTsConfig['mail.']['subject']);
432  } else {
433  throw new \Exception($GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.error.noSubject'), '1295476808');
434  }
435  if (!empty($this->email)) {
436  $emailList = GeneralUtility::trimExplode(',', $this->email);
437  foreach ($emailList as $emailAdd) {
438  if (!GeneralUtility::validEmail($emailAdd)) {
439  throw new \Exception($GLOBALS['LANG']->sL('LLL:EXT:linkvalidator/Resources/Private/Language/locallang.xlf:tasks.error.invalidToEmail'), '1295476821');
440  } else {
441  $validEmailList[] = $emailAdd;
442  }
443  }
444  }
445  if (is_array($validEmailList) && !empty($validEmailList)) {
446  $mail->setTo($validEmailList);
447  } else {
448  $sendEmail = FALSE;
449  }
450  if ($sendEmail) {
451  $mail->setBody($content, 'text/html');
452  $mail->send();
453  }
454  return $sendEmail;
455  }
456 
466  protected function buildMail($curPage, $pageList, array $markerArray, array $oldBrokenLink) {
467  $pageSectionHtml = \TYPO3\CMS\Core\Html\HtmlParser::getSubpart($this->templateMail, '###PAGE_SECTION###');
468  // Hook
469  if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['buildMailMarkers'])) {
470  foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['buildMailMarkers'] as $userFunc) {
471  $params = array(
472  'curPage' => $curPage,
473  'pageList' => $pageList,
474  'markerArray' => $markerArray,
475  'oldBrokenLink' => $oldBrokenLink,
476  'pObj' => &$this
477  );
478  $newMarkers = GeneralUtility::callUserFunction($userFunc, $params, $this);
479  if (is_array($newMarkers)) {
480  $markerArray = GeneralUtility::array_merge($markerArray, $newMarkers);
481  }
482  unset($params);
483  }
484  }
485  if (is_array($markerArray)) {
486  foreach ($markerArray as $markerKey => $markerValue) {
487  if (empty($oldBrokenLink[$markerKey])) {
488  $oldBrokenLink[$markerKey] = 0;
489  }
490  if ($markerValue != $oldBrokenLink[$markerKey]) {
491  $this->isDifferentToLastRun = TRUE;
492  }
493  $markerArray[$markerKey . '_old'] = $oldBrokenLink[$markerKey];
494  }
495  }
496  $markerArray['title'] = BackendUtility::getRecordTitle(
497  'pages',
498  BackendUtility::getRecord('pages', $curPage)
499  );
500  $content = '';
501  if ($markerArray['brokenlinkCount'] > 0) {
502  $content = \TYPO3\CMS\Core\Html\HtmlParser::substituteMarkerArray($pageSectionHtml, $markerArray, '###|###', TRUE, TRUE);
503  }
504  return $content;
505  }
506 
512  protected function setCliArguments() {
513  $_SERVER['argv'] = array(
514  $_SERVER['argv'][0],
515  'tx_link_scheduler_link',
516  '0',
517  '-ss',
518  '--sleepTime',
519  $this->sleepTime,
520  '--sleepAfterFinish',
521  $this->sleepAfterFinish,
522  '--countInARun',
523  $this->countInARun
524  );
525  }
526 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
setEmailOnBrokenLinkOnly($emailOnBrokenLinkOnly)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static callUserFunction($funcName, &$params, &$ref, $checkPrefix='', $errorMode=0)
static getRecordTitle($table, $row, $prep=FALSE, $forceResult=TRUE)
static getSubpart($content, $marker)
Definition: HtmlParser.php:39
static substituteMarkerArray($content, $markContentArray, $wrap='', $uppercase=FALSE, $deleteUnused=FALSE)
Definition: HtmlParser.php:189
static substituteSubpart($content, $marker, $subpartContent, $recursive=TRUE, $keepMarker=FALSE)
Definition: HtmlParser.php:79
static array_merge(array $arr1, array $arr2)
buildMail($curPage, $pageList, array $markerArray, array $oldBrokenLink)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)