[BUGFIX] Catch FolderDoesNotExistsException in linkvalidator
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / Classes / Linktype / FileLinktype.php
1 <?php
2 namespace TYPO3\CMS\Linkvalidator\Linktype;
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
17 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
18 use TYPO3\CMS\Core\Resource\Exception\FolderDoesNotExistException;
19 use TYPO3\CMS\Core\Resource\ResourceFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * This class provides Check File Links plugin implementation
24 */
25 class FileLinktype extends AbstractLinktype
26 {
27 /**
28 * Type fetching method, based on the type that softRefParserObj returns
29 *
30 * @param array $value Reference properties
31 * @param string $type Current type
32 * @param string $key Validator hook name
33 * @return string fetched type
34 */
35 public function fetchType($value, $type, $key)
36 {
37 if (strpos(strtolower($value['tokenValue']), 'file:') === 0) {
38 $type = 'file';
39 }
40 return $type;
41 }
42 /**
43 * Checks a given URL + /path/filename.ext for validity
44 *
45 * @param string $url Url to check
46 * @param array $softRefEntry The soft reference entry which builds the context of the url
47 * @param \TYPO3\CMS\Linkvalidator\LinkAnalyzer $reference Parent instance
48 * @return bool TRUE on success or FALSE on error
49 */
50 public function checkLink($url, $softRefEntry, $reference)
51 {
52 $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
53 try {
54 $file = $resourceFactory->retrieveFileOrFolderObject($url);
55 } catch (FileDoesNotExistException $e) {
56 return false;
57 } catch (FolderDoesNotExistException $e) {
58 return false;
59 }
60 return !$file->isMissing();
61 }
62
63 /**
64 * Generate the localized error message from the error params saved from the parsing
65 *
66 * @param array $errorParams All parameters needed for the rendering of the error message
67 * @return string Validation error message
68 */
69 public function getErrorMessage($errorParams)
70 {
71 return $this->getLanguageService()->getLL('list.report.filenotexisting');
72 }
73
74 /**
75 * Construct a valid Url for browser output
76 *
77 * @param array $row Broken link record
78 * @return string Parsed broken url
79 */
80 public function getBrokenUrl($row)
81 {
82 return GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . $row['url'];
83 }
84 }