@@ -45,7 +45,7 @@ discard block  | 
                                                    ||
| 45 | 45 | }  | 
                                                        
| 46 | 46 | |
| 47 | 47 | // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes  | 
                                                        
| 48 | -        $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] | 
                                                        |
| 48 | +        $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function(array $match): string { // ^[/] | 
                                                        |
| 49 | 49 |              if (!isset($this->svgFileArr[$match['src']])) { // check usage | 
                                                        
| 50 | 50 | return $match[0];  | 
                                                        
| 51 | 51 | }  | 
                                                        
@@ -55,7 +55,7 @@ discard block  | 
                                                    ||
| 55 | 55 | }, $html['body']);  | 
                                                        
| 56 | 56 | |
| 57 | 57 | // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes  | 
                                                        
| 58 | -        $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] | 
                                                        |
| 58 | +        $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function(array $match): string { // ^[/] | 
                                                        |
| 59 | 59 |              if (!isset($this->svgFileArr[$match['data']])) { // check usage | 
                                                        
| 60 | 60 | return $match[0];  | 
                                                        
| 61 | 61 | }  | 
                                                        
@@ -102,7 +102,7 @@ discard block  | 
                                                    ||
| 102 | 102 |          $svg = preg_replace('/.*<svg|<\/svg>.*|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup | 
                                                        
| 103 | 103 | |
| 104 | 104 | // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes  | 
                                                        
| 105 | -        $svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function (array $match) use (&$attr): string { | 
                                                        |
| 105 | +        $svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function(array $match) use (&$attr): string { | 
                                                        |
| 106 | 106 |              if (false === preg_match_all('/\s(?<attr>[\w\-]+)="\s*(?<value>[^"]+)\s*"/', $match[1], $matches)) { | 
                                                        
| 107 | 107 | return $match[0];  | 
                                                        
| 108 | 108 | }  | 
                                                        
@@ -146,7 +146,7 @@ discard block  | 
                                                    ||
| 146 | 146 | |
| 147 | 147 | $svg = preg_replace_callback(  | 
                                                        
| 148 | 148 | '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s',  | 
                                                        
| 149 | -            function (array $match): string { | 
                                                        |
| 149 | +            function(array $match): string { | 
                                                        |
| 150 | 150 |                  if (!isset($this->svgFileArr[$match['href']])) { // check usage | 
                                                        
| 151 | 151 | return $match[0];  | 
                                                        
| 152 | 152 | }  | 
                                                        
@@ -14,215 +14,215 @@  | 
                                                    ||
| 14 | 14 | */  | 
                                                        
| 15 | 15 | class SvgStoreService implements SingletonInterface  | 
                                                        
| 16 | 16 |  { | 
                                                        
| 17 | - public function __construct()  | 
                                                        |
| 18 | -    { | 
                                                        |
| 19 | - //$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css  | 
                                                        |
| 20 | - //$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14  | 
                                                        |
| 21 | - $this->svgs = [];  | 
                                                        |
| 22 | -  | 
                                                        |
| 23 | - $this->outputDir = '/typo3temp/assets/svg/';  | 
                                                        |
| 24 | - $this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$  | 
                                                        |
| 25 | -  | 
                                                        |
| 26 | - $this->connPool = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class);  | 
                                                        |
| 27 | -        $this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore'); | 
                                                        |
| 28 | - }  | 
                                                        |
| 29 | -  | 
                                                        |
| 30 | - public function process(string $html): string  | 
                                                        |
| 31 | -    { | 
                                                        |
| 32 | -        $this->spritePath = $this->svgCache->get('spritePath'); | 
                                                        |
| 33 | -        $this->svgFileArr = $this->svgCache->get('svgFileArr'); | 
                                                        |
| 34 | -  | 
                                                        |
| 35 | -        if (empty($this->spritePath) && !$this->populateCache()) { | 
                                                        |
| 36 | -            throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath); | 
                                                        |
| 37 | - }  | 
                                                        |
| 38 | -  | 
                                                        |
| 39 | -        if (!file_exists($this->sitePath.$this->spritePath)) { | 
                                                        |
| 40 | -            throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath); | 
                                                        |
| 41 | - }  | 
                                                        |
| 42 | -  | 
                                                        |
| 43 | -        if (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $html) && 5 == \count($html)) { | 
                                                        |
| 44 | -            throw new \Exception('fix HTML!'); | 
                                                        |
| 45 | - }  | 
                                                        |
| 46 | -  | 
                                                        |
| 47 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes  | 
                                                        |
| 48 | -        $html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] | 
                                                        |
| 49 | -            if (!isset($this->svgFileArr[$match['src']])) { // check usage | 
                                                        |
| 50 | - return $match[0];  | 
                                                        |
| 51 | - }  | 
                                                        |
| 52 | -            $attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|referrerpolicy)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup | 
                                                        |
| 53 | -  | 
                                                        |
| 54 | -            return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src'])); | 
                                                        |
| 55 | - }, $html['body']);  | 
                                                        |
| 56 | -  | 
                                                        |
| 57 | - // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes  | 
                                                        |
| 58 | -        $html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] | 
                                                        |
| 59 | -            if (!isset($this->svgFileArr[$match['data']])) { // check usage | 
                                                        |
| 60 | - return $match[0];  | 
                                                        |
| 61 | - }  | 
                                                        |
| 62 | -            $attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup | 
                                                        |
| 63 | -  | 
                                                        |
| 64 | -            return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data'])); | 
                                                        |
| 65 | - }, $html['body']);  | 
                                                        |
| 66 | -  | 
                                                        |
| 67 | - return $html['head'].$html['body'];  | 
                                                        |
| 68 | - }  | 
                                                        |
| 69 | -  | 
                                                        |
| 70 | - private function convertFilePath(string $path): string  | 
                                                        |
| 71 | -    { | 
                                                        |
| 72 | -        return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] | 
                                                        |
| 73 | - }  | 
                                                        |
| 74 | -  | 
                                                        |
| 75 | - private function addFileToSpriteArr(string $hash, string $path): ?array  | 
                                                        |
| 76 | -    { | 
                                                        |
| 77 | -        if (1 === preg_match('/(?:;base64|i:a?i?pgf)/', $svg = file_get_contents($this->sitePath.$path))) { // noop! | 
                                                        |
| 78 | - return null;  | 
                                                        |
| 79 | - }  | 
                                                        |
| 80 | -  | 
                                                        |
| 81 | -        if (1 === preg_match('/<(?:style|defs)|url\(/', $svg)) { | 
                                                        |
| 82 | - return null; // check links @ __construct  | 
                                                        |
| 83 | - }  | 
                                                        |
| 84 | -  | 
                                                        |
| 85 | -        //$svg = preg_replace('/((?:id|class)=")/', '$1'.$hash.'__', $svg); // extend  IDs | 
                                                        |
| 86 | -        //$svg = preg_replace('/(href="|url\()#/', '$1#'.$hash.'__', $svg); // recover IDs | 
                                                        |
| 87 | -  | 
                                                        |
| 88 | -        //$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { | 
                                                        |
| 89 | - //  | 
                                                        |
| 90 | - // if(isset($match['styl']))  | 
                                                        |
| 91 | -        //    { | 
                                                        |
| 92 | -        //        $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes | 
                                                        |
| 93 | - // }  | 
                                                        |
| 94 | - // if(isset($match['defs']))  | 
                                                        |
| 95 | -        //    { | 
                                                        |
| 96 | - // $this->defs[] = trim($match['defs']);  | 
                                                        |
| 97 | - // }  | 
                                                        |
| 98 | - // return '';  | 
                                                        |
| 99 | - //}, $svg);  | 
                                                        |
| 100 | -  | 
                                                        |
| 101 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href  | 
                                                        |
| 102 | -        $svg = preg_replace('/.*<svg|<\/svg>.*|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup | 
                                                        |
| 103 | -  | 
                                                        |
| 104 | - // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes  | 
                                                        |
| 105 | -        $svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function (array $match) use (&$attr): string { | 
                                                        |
| 106 | -            if (false === preg_match_all('/\s(?<attr>[\w\-]+)="\s*(?<value>[^"]+)\s*"/', $match[1], $matches)) { | 
                                                        |
| 107 | - return $match[0];  | 
                                                        |
| 108 | - }  | 
                                                        |
| 109 | -            foreach ($matches['attr'] as $index => $attribute) { | 
                                                        |
| 110 | -                switch ($attribute) { | 
                                                        |
| 111 | - case 'id':  | 
                                                        |
| 112 | - case 'width':  | 
                                                        |
| 113 | - case 'height':  | 
                                                        |
| 114 | - unset($matches[0][$index]);  | 
                                                        |
| 115 | - break;  | 
                                                        |
| 116 | -  | 
                                                        |
| 117 | - case 'viewBox':  | 
                                                        |
| 118 | -                      $attr[] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // save! | 
                                                        |
| 119 | - // no break  | 
                                                        |
| 120 | - default:  | 
                                                        |
| 121 | -                      $matches[0][$index] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // cleanup | 
                                                        |
| 122 | - }  | 
                                                        |
| 123 | - }  | 
                                                        |
| 124 | -  | 
                                                        |
| 125 | -            return implode(' ', $matches[0]); | 
                                                        |
| 126 | - }, $svg, 1);  | 
                                                        |
| 127 | -  | 
                                                        |
| 128 | -        if ($attr) { // TODO; beautify | 
                                                        |
| 129 | -            $this->svgs[] = sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // append ID | 
                                                        |
| 130 | - }  | 
                                                        |
| 131 | -  | 
                                                        |
| 132 | -        return !$attr ?: ['attr' => implode(' ', $attr), 'hash' => $hash]; | 
                                                        |
| 133 | - }  | 
                                                        |
| 134 | -  | 
                                                        |
| 135 | - private function populateCache(): bool  | 
                                                        |
| 136 | -    { | 
                                                        |
| 137 | - $storageArr = $this->getStorageArrayFromDB();  | 
                                                        |
| 138 | - $svgFileArr = $this->getSvgFilesArrayFromDB(array_keys($storageArr));  | 
                                                        |
| 139 | -  | 
                                                        |
| 140 | - $this->svgFileArr = [];  | 
                                                        |
| 141 | -        foreach ($svgFileArr as $index => $row) { | 
                                                        |
| 142 | -            if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) { // ^[/] | 
                                                        |
| 143 | - unset($this->svgFileArr[$row['path']]);  | 
                                                        |
| 144 | - }  | 
                                                        |
| 145 | - }  | 
                                                        |
| 146 | -  | 
                                                        |
| 147 | - $svg = preg_replace_callback(  | 
                                                        |
| 148 | - '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s',  | 
                                                        |
| 149 | -            function (array $match): string { | 
                                                        |
| 150 | -                if (!isset($this->svgFileArr[$match['href']])) { // check usage | 
                                                        |
| 151 | - return $match[0];  | 
                                                        |
| 152 | - }  | 
                                                        |
| 153 | -                return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href'])); | 
                                                        |
| 154 | - },  | 
                                                        |
| 155 | - '<svg xmlns="http://www.w3.org/2000/svg">'  | 
                                                        |
| 156 | -            //."\n<style>\n".implode("\n", $this->styl)."\n</style>" | 
                                                        |
| 157 | -            //."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" | 
                                                        |
| 158 | -            ."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n" | 
                                                        |
| 159 | - .'</svg>'  | 
                                                        |
| 160 | - );  | 
                                                        |
| 161 | -  | 
                                                        |
| 162 | - //unset($this->styl); // save MEM  | 
                                                        |
| 163 | - //unset($this->defs); // save MEM  | 
                                                        |
| 164 | - unset($this->svgs); // save MEM  | 
                                                        |
| 165 | -  | 
                                                        |
| 166 | -        if (\is_int($var = $GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml']) && 1 == $var) { | 
                                                        |
| 167 | -            $svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg); | 
                                                        |
| 168 | - }  | 
                                                        |
| 169 | -  | 
                                                        |
| 170 | -        $svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy | 
                                                        |
| 171 | -        $svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify | 
                                                        |
| 172 | -  | 
                                                        |
| 173 | -        if (!is_dir($this->sitePath.$this->outputDir)) { | 
                                                        |
| 174 | - GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir);  | 
                                                        |
| 175 | - }  | 
                                                        |
| 176 | -  | 
                                                        |
| 177 | -        $this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg'; | 
                                                        |
| 178 | -        if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) { | 
                                                        |
| 179 | - return false;  | 
                                                        |
| 180 | - }  | 
                                                        |
| 181 | - unset($svg); // save MEM  | 
                                                        |
| 182 | -  | 
                                                        |
| 183 | -        $this->svgCache->set('svgFileArr', $this->svgFileArr); | 
                                                        |
| 184 | -        $this->svgCache->set('spritePath', $this->spritePath); | 
                                                        |
| 185 | -  | 
                                                        |
| 186 | - return true;  | 
                                                        |
| 187 | - }  | 
                                                        |
| 188 | -  | 
                                                        |
| 189 | - private function getStorageArrayFromDB(): array  | 
                                                        |
| 190 | -    { | 
                                                        |
| 191 | - $storageResources = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();  | 
                                                        |
| 192 | -        foreach ($storageResources as $storage) { | 
                                                        |
| 193 | -            if ('relative' == $storage->getConfiguration()['pathType']) { | 
                                                        |
| 194 | - $storageResources[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$  | 
                                                        |
| 195 | - }  | 
                                                        |
| 196 | - }  | 
                                                        |
| 197 | - unset($storageResources[0]); // keep!  | 
                                                        |
| 198 | -  | 
                                                        |
| 199 | - return $storageResources;  | 
                                                        |
| 200 | - }  | 
                                                        |
| 201 | -  | 
                                                        |
| 202 | - private function getSvgFilesArrayFromDB(array $storageIds): array  | 
                                                        |
| 203 | -    { | 
                                                        |
| 204 | -        return ($queryBuilder = $this->connPool->getQueryBuilderForTable('sys_file')) | 
                                                        |
| 205 | -            ->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') | 
                                                        |
| 206 | -            ->from('sys_file') | 
                                                        |
| 207 | - ->innerJoin(  | 
                                                        |
| 208 | - 'sys_file',  | 
                                                        |
| 209 | - 'sys_file_reference',  | 
                                                        |
| 210 | - 'sys_file_reference',  | 
                                                        |
| 211 | - $queryBuilder->expr()->eq(  | 
                                                        |
| 212 | - 'sys_file_reference.uid_local',  | 
                                                        |
| 213 | -                    $queryBuilder->quoteIdentifier('sys_file.uid') | 
                                                        |
| 214 | - )  | 
                                                        |
| 215 | - )  | 
                                                        |
| 216 | - ->where(  | 
                                                        |
| 217 | -                $queryBuilder->expr()->in('sys_file.storage', $queryBuilder->createNamedParameter($storageIds, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY)), | 
                                                        |
| 218 | -                $queryBuilder->expr()->eq('sys_file.mime_type', $queryBuilder->createNamedParameter('image/svg+xml')), | 
                                                        |
| 219 | -                $queryBuilder->expr()->lt('sys_file.size', $queryBuilder->createNamedParameter($GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'])), | 
                                                        |
| 220 | - )  | 
                                                        |
| 221 | -            ->groupBy('sys_file.uid') | 
                                                        |
| 222 | -            ->orderBy('sys_file.storage') | 
                                                        |
| 223 | -            ->addOrderBy('sys_file.identifier') | 
                                                        |
| 224 | - ->execute()  | 
                                                        |
| 225 | - ->fetchAll() // TODO; use stdClass  | 
                                                        |
| 226 | - ;  | 
                                                        |
| 227 | - }  | 
                                                        |
| 17 | + public function __construct()  | 
                                                        |
| 18 | +	{ | 
                                                        |
| 19 | + //$this->styl = []; # https://stackoverflow.com/questions/39583880/external-svg-fails-to-apply-internal-css  | 
                                                        |
| 20 | + //$this->defs = []; # https://bugs.chromium.org/p/chromium/issues/detail?id=751733#c14  | 
                                                        |
| 21 | + $this->svgs = [];  | 
                                                        |
| 22 | +  | 
                                                        |
| 23 | + $this->outputDir = '/typo3temp/assets/svg/';  | 
                                                        |
| 24 | + $this->sitePath = \TYPO3\CMS\Core\Core\Environment::getPublicPath(); // [^/]$  | 
                                                        |
| 25 | +  | 
                                                        |
| 26 | + $this->connPool = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class);  | 
                                                        |
| 27 | +		$this->svgCache = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Cache\CacheManager::class)->getCache('svgstore'); | 
                                                        |
| 28 | + }  | 
                                                        |
| 29 | +  | 
                                                        |
| 30 | + public function process(string $html): string  | 
                                                        |
| 31 | +	{ | 
                                                        |
| 32 | +		$this->spritePath = $this->svgCache->get('spritePath'); | 
                                                        |
| 33 | +		$this->svgFileArr = $this->svgCache->get('svgFileArr'); | 
                                                        |
| 34 | +  | 
                                                        |
| 35 | +		if (empty($this->spritePath) && !$this->populateCache()) { | 
                                                        |
| 36 | +			throw new \Exception('could not write file: '.$this->sitePath.$this->spritePath); | 
                                                        |
| 37 | + }  | 
                                                        |
| 38 | +  | 
                                                        |
| 39 | +		if (!file_exists($this->sitePath.$this->spritePath)) { | 
                                                        |
| 40 | +			throw new \Exception('file does not exists: '.$this->sitePath.$this->spritePath); | 
                                                        |
| 41 | + }  | 
                                                        |
| 42 | +  | 
                                                        |
| 43 | +		if (!preg_match('/(?<head>.+?<\/head>)(?<body>.+)/s', $html, $html) && 5 == \count($html)) { | 
                                                        |
| 44 | +			throw new \Exception('fix HTML!'); | 
                                                        |
| 45 | + }  | 
                                                        |
| 46 | +  | 
                                                        |
| 47 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes  | 
                                                        |
| 48 | +		$html['body'] = preg_replace_callback('/<img(?<pre>[^>]*)src="(?<src>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?!\s*<\/picture>)/s', function (array $match): string { // ^[/] | 
                                                        |
| 49 | +			if (!isset($this->svgFileArr[$match['src']])) { // check usage | 
                                                        |
| 50 | + return $match[0];  | 
                                                        |
| 51 | + }  | 
                                                        |
| 52 | +			$attr = preg_replace('/\s(?:alt|ismap|loading|title|sizes|srcset|usemap|crossorigin|decoding|referrerpolicy)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup | 
                                                        |
| 53 | +  | 
                                                        |
| 54 | +			return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['src']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['src'])); | 
                                                        |
| 55 | + }, $html['body']);  | 
                                                        |
| 56 | +  | 
                                                        |
| 57 | + // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/object#attributes  | 
                                                        |
| 58 | +		$html['body'] = preg_replace_callback('/<object(?<pre>[^>]*)data="(?<data>\/[^"]+\.svg)"(?<post>[^>]*?)[\s\/]*>(?:<\/object>)/s', function (array $match): string { // ^[/] | 
                                                        |
| 59 | +			if (!isset($this->svgFileArr[$match['data']])) { // check usage | 
                                                        |
| 60 | + return $match[0];  | 
                                                        |
| 61 | + }  | 
                                                        |
| 62 | +			$attr = preg_replace('/\s(?:form|name|type|usemap)="[^"]*"/', '', $match['pre'].$match['post']); // cleanup | 
                                                        |
| 63 | +  | 
                                                        |
| 64 | +			return sprintf('<svg %s %s><use href="%s#%s"/></svg>', $this->svgFileArr[$match['data']]['attr'], trim($attr), $this->spritePath, $this->convertFilePath($match['data'])); | 
                                                        |
| 65 | + }, $html['body']);  | 
                                                        |
| 66 | +  | 
                                                        |
| 67 | + return $html['head'].$html['body'];  | 
                                                        |
| 68 | + }  | 
                                                        |
| 69 | +  | 
                                                        |
| 70 | + private function convertFilePath(string $path): string  | 
                                                        |
| 71 | +	{ | 
                                                        |
| 72 | +		return preg_replace('/.svg$|[^\w\-]/', '', str_replace('/', '-', ltrim($path, '/'))); // ^[^/] | 
                                                        |
| 73 | + }  | 
                                                        |
| 74 | +  | 
                                                        |
| 75 | + private function addFileToSpriteArr(string $hash, string $path): ?array  | 
                                                        |
| 76 | +	{ | 
                                                        |
| 77 | +		if (1 === preg_match('/(?:;base64|i:a?i?pgf)/', $svg = file_get_contents($this->sitePath.$path))) { // noop! | 
                                                        |
| 78 | + return null;  | 
                                                        |
| 79 | + }  | 
                                                        |
| 80 | +  | 
                                                        |
| 81 | +		if (1 === preg_match('/<(?:style|defs)|url\(/', $svg)) { | 
                                                        |
| 82 | + return null; // check links @ __construct  | 
                                                        |
| 83 | + }  | 
                                                        |
| 84 | +  | 
                                                        |
| 85 | +		//$svg = preg_replace('/((?:id|class)=")/', '$1'.$hash.'__', $svg); // extend  IDs | 
                                                        |
| 86 | +		//$svg = preg_replace('/(href="|url\()#/', '$1#'.$hash.'__', $svg); // recover IDs | 
                                                        |
| 87 | +  | 
                                                        |
| 88 | +		//$svg = preg_replace_callback('/<style[^>]*>(?<styl>.+?)<\/style>|<defs[^>]*>(?<defs>.+?)<\/defs>/s', function(array $match) use($hash): string { | 
                                                        |
| 89 | + //  | 
                                                        |
| 90 | + // if(isset($match['styl']))  | 
                                                        |
| 91 | +		//    { | 
                                                        |
| 92 | +		//        $this->styl[] = preg_replace('/\s*(\.|#){1}(.+?)\s*\{/', '$1'.$hash.'__$2{', $match['styl']); // patch CSS # https://mathiasbynens.be/notes/css-escapes | 
                                                        |
| 93 | + // }  | 
                                                        |
| 94 | + // if(isset($match['defs']))  | 
                                                        |
| 95 | +		//    { | 
                                                        |
| 96 | + // $this->defs[] = trim($match['defs']);  | 
                                                        |
| 97 | + // }  | 
                                                        |
| 98 | + // return '';  | 
                                                        |
| 99 | + //}, $svg);  | 
                                                        |
| 100 | +  | 
                                                        |
| 101 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href  | 
                                                        |
| 102 | +		$svg = preg_replace('/.*<svg|<\/svg>.*|xlink:|\s(?:(?:version|xmlns)|(?:[a-z\-]+\:[a-z\-]+))="[^"]*"/s', '', $svg); // cleanup | 
                                                        |
| 103 | +  | 
                                                        |
| 104 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg#attributes  | 
                                                        |
| 105 | +		$svg = preg_replace_callback('/([^>]+)\s*(?=>)/s', function (array $match) use (&$attr): string { | 
                                                        |
| 106 | +			if (false === preg_match_all('/\s(?<attr>[\w\-]+)="\s*(?<value>[^"]+)\s*"/', $match[1], $matches)) { | 
                                                        |
| 107 | + return $match[0];  | 
                                                        |
| 108 | + }  | 
                                                        |
| 109 | +			foreach ($matches['attr'] as $index => $attribute) { | 
                                                        |
| 110 | +				switch ($attribute) { | 
                                                        |
| 111 | + case 'id':  | 
                                                        |
| 112 | + case 'width':  | 
                                                        |
| 113 | + case 'height':  | 
                                                        |
| 114 | + unset($matches[0][$index]);  | 
                                                        |
| 115 | + break;  | 
                                                        |
| 116 | +  | 
                                                        |
| 117 | + case 'viewBox':  | 
                                                        |
| 118 | +					  $attr[] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // save! | 
                                                        |
| 119 | + // no break  | 
                                                        |
| 120 | + default:  | 
                                                        |
| 121 | +					  $matches[0][$index] = sprintf('%s="%s"', $attribute, $matches['value'][$index]); // cleanup | 
                                                        |
| 122 | + }  | 
                                                        |
| 123 | + }  | 
                                                        |
| 124 | +  | 
                                                        |
| 125 | +			return implode(' ', $matches[0]); | 
                                                        |
| 126 | + }, $svg, 1);  | 
                                                        |
| 127 | +  | 
                                                        |
| 128 | +		if ($attr) { // TODO; beautify | 
                                                        |
| 129 | +			$this->svgs[] = sprintf('id="%s" %s', $this->convertFilePath($path), $svg); // append ID | 
                                                        |
| 130 | + }  | 
                                                        |
| 131 | +  | 
                                                        |
| 132 | +		return !$attr ?: ['attr' => implode(' ', $attr), 'hash' => $hash]; | 
                                                        |
| 133 | + }  | 
                                                        |
| 134 | +  | 
                                                        |
| 135 | + private function populateCache(): bool  | 
                                                        |
| 136 | +	{ | 
                                                        |
| 137 | + $storageArr = $this->getStorageArrayFromDB();  | 
                                                        |
| 138 | + $svgFileArr = $this->getSvgFilesArrayFromDB(array_keys($storageArr));  | 
                                                        |
| 139 | +  | 
                                                        |
| 140 | + $this->svgFileArr = [];  | 
                                                        |
| 141 | +		foreach ($svgFileArr as $index => $row) { | 
                                                        |
| 142 | +			if (!$this->svgFileArr[($row['path'] = '/'.$storageArr[$row['storage']].$row['identifier'])] = $this->addFileToSpriteArr($row['sha1'], $row['path'])) { // ^[/] | 
                                                        |
| 143 | + unset($this->svgFileArr[$row['path']]);  | 
                                                        |
| 144 | + }  | 
                                                        |
| 145 | + }  | 
                                                        |
| 146 | +  | 
                                                        |
| 147 | + $svg = preg_replace_callback(  | 
                                                        |
| 148 | + '/<use(?<pre>.*?)(?:xlink:)?href="(?<href>\/.+?\.svg)#[^"]+"(?<post>.*?)[\s\/]*>(?:<\/use>)?/s',  | 
                                                        |
| 149 | +			function (array $match): string { | 
                                                        |
| 150 | +				if (!isset($this->svgFileArr[$match['href']])) { // check usage | 
                                                        |
| 151 | + return $match[0];  | 
                                                        |
| 152 | + }  | 
                                                        |
| 153 | +				return sprintf('<use%s href="#%s"/>', $match['pre'].$match['post'], $this->convertFilePath($match['href'])); | 
                                                        |
| 154 | + },  | 
                                                        |
| 155 | + '<svg xmlns="http://www.w3.org/2000/svg">'  | 
                                                        |
| 156 | +			//."\n<style>\n".implode("\n", $this->styl)."\n</style>" | 
                                                        |
| 157 | +			//."\n<defs>\n".implode("\n", $this->defs)."\n</defs>" | 
                                                        |
| 158 | +			."\n<symbol ".implode("</symbol>\n<symbol ", $this->svgs)."</symbol>\n" | 
                                                        |
| 159 | + .'</svg>'  | 
                                                        |
| 160 | + );  | 
                                                        |
| 161 | +  | 
                                                        |
| 162 | + //unset($this->styl); // save MEM  | 
                                                        |
| 163 | + //unset($this->defs); // save MEM  | 
                                                        |
| 164 | + unset($this->svgs); // save MEM  | 
                                                        |
| 165 | +  | 
                                                        |
| 166 | +		if (\is_int($var = $GLOBALS['TSFE']->config['config']['sourceopt.']['formatHtml']) && 1 == $var) { | 
                                                        |
| 167 | +			$svg = preg_replace('/[\n\r\t\v\0]|\s{2,}/', '', $svg); | 
                                                        |
| 168 | + }  | 
                                                        |
| 169 | +  | 
                                                        |
| 170 | +		$svg = preg_replace('/<([a-z]+)\s*(\/|>\s*<\/\1)>\s*/i', '', $svg); // remove emtpy | 
                                                        |
| 171 | +		$svg = preg_replace('/<((circle|ellipse|line|path|polygon|polyline|rect|stop|use)\s[^>]+?)\s*>\s*<\/\2>/', '<$1/>', $svg); // shorten/minify | 
                                                        |
| 172 | +  | 
                                                        |
| 173 | +		if (!is_dir($this->sitePath.$this->outputDir)) { | 
                                                        |
| 174 | + GeneralUtility::mkdir_deep($this->sitePath.$this->outputDir);  | 
                                                        |
| 175 | + }  | 
                                                        |
| 176 | +  | 
                                                        |
| 177 | +		$this->spritePath = $this->outputDir.hash('sha1', serialize($this->svgFileArr)).'.svg'; | 
                                                        |
| 178 | +		if (false === file_put_contents($this->sitePath.$this->spritePath, $svg)) { | 
                                                        |
| 179 | + return false;  | 
                                                        |
| 180 | + }  | 
                                                        |
| 181 | + unset($svg); // save MEM  | 
                                                        |
| 182 | +  | 
                                                        |
| 183 | +		$this->svgCache->set('svgFileArr', $this->svgFileArr); | 
                                                        |
| 184 | +		$this->svgCache->set('spritePath', $this->spritePath); | 
                                                        |
| 185 | +  | 
                                                        |
| 186 | + return true;  | 
                                                        |
| 187 | + }  | 
                                                        |
| 188 | +  | 
                                                        |
| 189 | + private function getStorageArrayFromDB(): array  | 
                                                        |
| 190 | +	{ | 
                                                        |
| 191 | + $storageResources = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\StorageRepository::class)->findAll();  | 
                                                        |
| 192 | +		foreach ($storageResources as $storage) { | 
                                                        |
| 193 | +			if ('relative' == $storage->getConfiguration()['pathType']) { | 
                                                        |
| 194 | + $storageResources[$storage->getUid()] = rtrim($storage->getConfiguration()['basePath'], '/'); // [^/]$  | 
                                                        |
| 195 | + }  | 
                                                        |
| 196 | + }  | 
                                                        |
| 197 | + unset($storageResources[0]); // keep!  | 
                                                        |
| 198 | +  | 
                                                        |
| 199 | + return $storageResources;  | 
                                                        |
| 200 | + }  | 
                                                        |
| 201 | +  | 
                                                        |
| 202 | + private function getSvgFilesArrayFromDB(array $storageIds): array  | 
                                                        |
| 203 | +	{ | 
                                                        |
| 204 | +		return ($queryBuilder = $this->connPool->getQueryBuilderForTable('sys_file')) | 
                                                        |
| 205 | +			->select('sys_file.storage', 'sys_file.identifier', 'sys_file.sha1') | 
                                                        |
| 206 | +			->from('sys_file') | 
                                                        |
| 207 | + ->innerJoin(  | 
                                                        |
| 208 | + 'sys_file',  | 
                                                        |
| 209 | + 'sys_file_reference',  | 
                                                        |
| 210 | + 'sys_file_reference',  | 
                                                        |
| 211 | + $queryBuilder->expr()->eq(  | 
                                                        |
| 212 | + 'sys_file_reference.uid_local',  | 
                                                        |
| 213 | +					$queryBuilder->quoteIdentifier('sys_file.uid') | 
                                                        |
| 214 | + )  | 
                                                        |
| 215 | + )  | 
                                                        |
| 216 | + ->where(  | 
                                                        |
| 217 | +				$queryBuilder->expr()->in('sys_file.storage', $queryBuilder->createNamedParameter($storageIds, \TYPO3\CMS\Core\Database\Connection::PARAM_INT_ARRAY)), | 
                                                        |
| 218 | +				$queryBuilder->expr()->eq('sys_file.mime_type', $queryBuilder->createNamedParameter('image/svg+xml')), | 
                                                        |
| 219 | +				$queryBuilder->expr()->lt('sys_file.size', $queryBuilder->createNamedParameter($GLOBALS['TSFE']->config['config']['svgstore.']['fileSize'])), | 
                                                        |
| 220 | + )  | 
                                                        |
| 221 | +			->groupBy('sys_file.uid') | 
                                                        |
| 222 | +			->orderBy('sys_file.storage') | 
                                                        |
| 223 | +			->addOrderBy('sys_file.identifier') | 
                                                        |
| 224 | + ->execute()  | 
                                                        |
| 225 | + ->fetchAll() // TODO; use stdClass  | 
                                                        |
| 226 | + ;  | 
                                                        |
| 227 | + }  | 
                                                        |
| 228 | 228 | }  |