[TASK] Use strpos instead of substr to check the start of a string
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Controller / AccessibilityLinkController.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Controller;
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 /**
18 * Remove accessibility icon when no link was rendered
19 */
20 class AccessibilityLinkController extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin
21 {
22 /**
23 * Same as class name
24 */
25 public $prefixId = 'AccessibilityLinkController';
26
27 /**
28 * Path to this script relative to the extension dir
29 */
30 public $scriptRelPath = 'Classes/Controller/AccessibilityLinkController.php';
31
32 /**
33 * The extension key
34 */
35 public $extKey = 'rtehtmlarea';
36
37 /**
38 * Configuration
39 */
40 public $conf = [];
41
42 /**
43 * cObj object
44 *
45 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
46 */
47 public $cObj;
48
49 /**
50 * Remove accessibility icon when no link was rendered, called from TypoScript
51 *
52 * @param string $content Content input. Not used, ignore.
53 * @param array $conf TypoScript configuration
54 * @return string HTML output.
55 * @access private
56 */
57 public function removeAccessibilityIcon($content, $conf)
58 {
59 // If the link was not rendered
60 if (substr($content, 0, 3) !== '<a ' && substr($content, 0, 5) === '<img ') {
61 // Let's remove the accessibility icon, if there is one
62 $matches = [];
63 if (preg_match('/^<img .*>/', $content, $matches) === 1) {
64 $attributes = \TYPO3\CMS\Core\Utility\GeneralUtility::get_tag_attributes($matches[0]);
65 if ($attributes['src']) {
66 // Get RTE Configconfiguration
67 $pageTSConfig = $this->frontendController->getPagesTSconfig();
68 if (is_array($pageTSConfig) && is_array($pageTSConfig['RTE.'])) {
69 $classesAnchorConfiguration = $pageTSConfig['RTE.']['classesAnchor.'];
70 if (is_array($classesAnchorConfiguration)) {
71 // Make the url of the source relative
72 $siteUrl = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
73 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($attributes['src'], $siteUrl)) {
74 $attributes['src'] = substr($attributes['src'], strlen($siteUrl));
75 }
76 // Lookup the RTE.classesAnchor array
77 foreach ($classesAnchorConfiguration as $item => $conf) {
78 if ($conf['image']) {
79 $imagePath = $this->getFullFileName(trim(str_replace('\'', '', str_replace('"', '', $conf['image']))));
80 if ($attributes['src'] === $imagePath) {
81 // If found, remove the img tag and break
82 $content = substr($content, strlen($matches[0]));
83 break;
84 }
85 }
86 }
87 }
88 }
89 }
90 }
91 }
92 return $content;
93 }
94 /*
95 * Returns the full name of a file referenced in Page TSConfig
96 */
97 protected function getFullFileName($filename)
98 {
99 if (strpos($filename, 'EXT:') === 0) {
100 list($extKey, $local) = explode('/', substr($filename, 4), 2);
101 $newFilename = '';
102 if ((string)$extKey !== '' && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded($extKey) && (string)$local !== '') {
103 $newFilename = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath($extKey) . $local;
104 }
105 } elseif ($filename[0] !== '/') {
106 $newFilename = $filename;
107 } else {
108 $newFilename = substr($filename, 1);
109 }
110 return \TYPO3\CMS\Core\Utility\GeneralUtility::resolveBackPath($newFilename);
111 }
112 }