[BUGFIX] Fix usage of fileadminDir
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Database / SoftReferenceIndex.php
1 <?php
2 namespace TYPO3\CMS\Core\Database;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2003-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
8 * All rights reserved
9 *
10 * This script is part of the Typo3 project. The Typo3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Soft Reference processing class
28 * "Soft References" are references to database elements, files, email addresses, URls etc.
29 * which are found in-text in content. The <link [page_id]> tag from typical bodytext fields
30 * are an example of this.
31 * This class contains generic parsers for the most well-known types
32 * which are default for most TYPO3 installations. Soft References can also be userdefined.
33 * The Soft Reference parsers are used by the system to find these references and process them accordingly in import/export actions and copy operations.
34 *
35 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
36 */
37 /**
38 * Example of usage
39 * Soft References:
40 * if ($conf['softref'] && strlen($value)) { // Check if a TCA configured field has softreferences defined (see TYPO3 Core API document)
41 * $softRefs = t3lib_BEfunc::explodeSoftRefParserList($conf['softref']); // Explode the list of softreferences/parameters
42 * foreach($softRefs as $spKey => $spParams) { // Traverse soft references
43 * $softRefObj = &t3lib_BEfunc::softRefParserObj($spKey); // create / get object
44 * if (is_object($softRefObj)) { // If there was an object returned...:
45 * $resultArray = $softRefObj->findRef($table, $field, $uid, $softRefValue, $spKey, $spParams); // Do processing
46 *
47 * Result Array:
48 * The Result array should contain two keys: "content" and "elements".
49 * "content" is a string containing the input content but possibly with tokens inside.
50 * Tokens are strings like {softref:[tokenID]} which is a placeholder for a value extracted by a softref parser
51 * For each token there MUST be an entry in the "elements" key which has a "subst" key defining the tokenID and the tokenValue. See below.
52 * "elements" is an array where the keys are insignificant, but the values are arrays with these keys:
53 * "matchString" => The value of the match. This is only for informational purposes to show what was found.
54 * "error" => An error message can be set here, like "file not found" etc.
55 * "subst" => array( // If this array is found there MUST be a token in the output content as well!
56 * "tokenID" => The tokenID string corresponding to the token in output content, {softref:[tokenID]}. This is typically an md5 hash of a string defining uniquely the position of the element.
57 * "tokenValue" => The value that the token substitutes in the text. Basically, if this value is inserted instead of the token the content should match what was inputted originally.
58 * "type" => file / db / string = the type of substitution. "file" means it is a relative file [automatically mapped], "db" means a database record reference [automatically mapped], "string" means it is manually modified string content (eg. an email address)
59 * "relFileName" => (for "file" type): Relative filename. May not necessarily exist. This could be noticed in the error key.
60 * "recordRef" => (for "db" type) : Reference to DB record on the form [table]:[uid]. May not necessarily exist.
61 * "title" => Title of element (for backend information)
62 * "description" => Description of element (for backend information)
63 * )
64 */
65 /**
66 * Class for processing of the default soft reference types for CMS:
67 *
68 * - 'substitute' : A full field value targeted for manual substitution (for import /export features)
69 * - 'notify' : Just report if a value is found, nothing more.
70 * - 'images' : HTML <img> tags for RTE images / images from fileadmin/
71 * - 'typolink' : references to page id or file, possibly with anchor/target, possibly commaseparated list.
72 * - 'typolink_tag' : As typolink, but searching for <link> tag to encapsulate it.
73 * - 'TSconfig' processing (filerefs? Domains? what do we know...)
74 * - 'TStemplate' : freetext references to "fileadmin/" files.
75 * - 'email' : Email highlight
76 * - 'url' : URL highlights (with a scheme)
77 *
78 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
79 * @package TYPO3
80 * @subpackage t3lib
81 */
82 class SoftReferenceIndex {
83
84 // External configuration
85 /**
86 * @todo Define visibility
87 */
88 public $fileAdminDir = '';
89
90 // Internal:
91 /**
92 * @todo Define visibility
93 */
94 public $tokenID_basePrefix = '';
95
96 /**
97 * Class construct to set global variable
98 *
99 */
100 public function __construct() {
101 $this->fileAdminDir = !empty($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir']) ? rtrim($GLOBALS['TYPO3_CONF_VARS']['BE']['fileadminDir'], '/') : 'fileadmin';
102 }
103
104 /**
105 * Main function through which all processing happens
106 *
107 * @param string $table Database table name
108 * @param string $field Field name for which processing occurs
109 * @param integer $uid UID of the record
110 * @param string $content The content/value of the field
111 * @param string $spKey The softlink parser key. This is only interesting if more than one parser is grouped in the same class. That is the case with this parser.
112 * @param array $spParams Parameters of the softlink parser. Basically this is the content inside optional []-brackets after the softref keys. Parameters are exploded by ";
113 * @param string $structurePath If running from inside a FlexForm structure, this is the path of the tag.
114 * @return array Result array on positive matches, see description above. Otherwise FALSE
115 * @todo Define visibility
116 */
117 public function findRef($table, $field, $uid, $content, $spKey, $spParams, $structurePath = '') {
118 $retVal = FALSE;
119 $this->tokenID_basePrefix = $table . ':' . $uid . ':' . $field . ':' . $structurePath . ':' . $spKey;
120 switch ($spKey) {
121 case 'notify':
122 // Simple notification
123 $resultArray = array(
124 'elements' => array(
125 array(
126 'matchString' => $content
127 )
128 )
129 );
130 $retVal = $resultArray;
131 break;
132 case 'substitute':
133 $tokenID = $this->makeTokenID();
134 $resultArray = array(
135 'content' => '{softref:' . $tokenID . '}',
136 'elements' => array(
137 array(
138 'matchString' => $content,
139 'subst' => array(
140 'type' => 'string',
141 'tokenID' => $tokenID,
142 'tokenValue' => $content
143 )
144 )
145 )
146 );
147 $retVal = $resultArray;
148 break;
149 case 'images':
150 $retVal = $this->findRef_images($content, $spParams);
151 break;
152 case 'typolink':
153 $retVal = $this->findRef_typolink($content, $spParams);
154 break;
155 case 'typolink_tag':
156 $retVal = $this->findRef_typolink_tag($content, $spParams);
157 break;
158 case 'ext_fileref':
159 $retVal = $this->findRef_extension_fileref($content, $spParams);
160 break;
161 case 'TStemplate':
162 $retVal = $this->findRef_TStemplate($content, $spParams);
163 break;
164 case 'TSconfig':
165 $retVal = $this->findRef_TSconfig($content, $spParams);
166 break;
167 case 'email':
168 $retVal = $this->findRef_email($content, $spParams);
169 break;
170 case 'url':
171 $retVal = $this->findRef_url($content, $spParams);
172 break;
173 default:
174 $retVal = FALSE;
175 break;
176 }
177 return $retVal;
178 }
179
180 /**
181 * Finding image tags in the content.
182 * All images that are not from external URLs will be returned with an info text
183 * Will only return files in fileadmin/ and files in uploads/ folders which are prefixed with "RTEmagic[C|P]_" for substitution
184 * Any "clear.gif" images are ignored.
185 *
186 * @param string $content The input content to analyse
187 * @param array $spParams Parameters set for the softref parser key in TCA/columns
188 * @return array Result array on positive matches, see description above. Otherwise FALSE
189 * @todo Define visibility
190 */
191 public function findRef_images($content, $spParams) {
192 // Start HTML parser and split content by image tag:
193 $htmlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Html\\HtmlParser');
194 $splitContent = $htmlParser->splitTags('img', $content);
195 $elements = array();
196 // Traverse splitted parts:
197 foreach ($splitContent as $k => $v) {
198 if ($k % 2) {
199 // Get file reference:
200 $attribs = $htmlParser->get_tag_attributes($v);
201 $srcRef = \TYPO3\CMS\Core\Utility\GeneralUtility::htmlspecialchars_decode($attribs[0]['src']);
202 $pI = pathinfo($srcRef);
203 // If it looks like a local image, continue. Otherwise ignore it.
204 $absPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_site . $srcRef);
205 if (!$pI['scheme'] && !$pI['query'] && $absPath && $srcRef !== 'clear.gif') {
206 // Initialize the element entry with info text here:
207 $tokenID = $this->makeTokenID($k);
208 $elements[$k] = array();
209 $elements[$k]['matchString'] = $v;
210 // If the image seems to be from fileadmin/ folder or an RTE image, then proceed to set up substitution token:
211 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($srcRef, $this->fileAdminDir . '/') || \TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($srcRef, 'uploads/') && preg_match('/^RTEmagicC_/', basename($srcRef))) {
212 // Token and substitute value:
213 // Make sure the value we work on is found and will get substituted in the content (Very important that the src-value is not DeHSC'ed)
214 if (strstr($splitContent[$k], $attribs[0]['src'])) {
215 // Substitute value with token (this is not be an exact method if the value is in there twice, but we assume it will not)
216 $splitContent[$k] = str_replace($attribs[0]['src'], '{softref:' . $tokenID . '}', $splitContent[$k]);
217 $elements[$k]['subst'] = array(
218 'type' => 'file',
219 'relFileName' => $srcRef,
220 'tokenID' => $tokenID,
221 'tokenValue' => $attribs[0]['src']
222 );
223 // Finally, notice if the file does not exist.
224 if (!@is_file($absPath)) {
225 $elements[$k]['error'] = 'File does not exist!';
226 }
227 } else {
228 $elements[$k]['error'] = 'Could not substitute image source with token!';
229 }
230 }
231 }
232 }
233 }
234 // Return result:
235 if (count($elements)) {
236 $resultArray = array(
237 'content' => implode('', $splitContent),
238 'elements' => $elements
239 );
240 return $resultArray;
241 }
242 }
243
244 /**
245 * TypoLink value processing.
246 * Will process input value as a TypoLink value.
247 *
248 * @param string $content The input content to analyse
249 * @param array $spParams Parameters set for the softref parser key in TCA/columns. value "linkList" will split the string by comma before processing.
250 * @return array Result array on positive matches, see description above. Otherwise FALSE
251 * @see tslib_content::typolink(), getTypoLinkParts()
252 * @todo Define visibility
253 */
254 public function findRef_typolink($content, $spParams) {
255 // First, split the input string by a comma if the "linkList" parameter is set.
256 // An example: the link field for images in content elements of type "textpic" or "image". This field CAN be configured to define a link per image, separated by comma.
257 if (is_array($spParams) && in_array('linkList', $spParams)) {
258 // Preserving whitespace on purpose.
259 $linkElement = explode(',', $content);
260 } else {
261 // If only one element, just set in this array to make it easy below.
262 $linkElement = array($content);
263 }
264 // Traverse the links now:
265 $elements = array();
266 foreach ($linkElement as $k => $typolinkValue) {
267 $tLP = $this->getTypoLinkParts($typolinkValue);
268 $linkElement[$k] = $this->setTypoLinkPartsElement($tLP, $elements, $typolinkValue, $k);
269 }
270 // Return output:
271 if (count($elements)) {
272 $resultArray = array(
273 'content' => implode(',', $linkElement),
274 'elements' => $elements
275 );
276 return $resultArray;
277 }
278 }
279
280 /**
281 * TypoLink tag processing.
282 * Will search for <link ...> tags in the content string and process any found.
283 *
284 * @param string $content The input content to analyse
285 * @param array $spParams Parameters set for the softref parser key in TCA/columns
286 * @return array Result array on positive matches, see description above. Otherwise FALSE
287 * @see tslib_content::typolink(), getTypoLinkParts()
288 * @todo Define visibility
289 */
290 public function findRef_typolink_tag($content, $spParams) {
291 // Parse string for special TYPO3 <link> tag:
292 $htmlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Html\\HtmlParser');
293 $linkTags = $htmlParser->splitTags('link', $content);
294 // Traverse result:
295 $elements = array();
296 foreach ($linkTags as $k => $foundValue) {
297 if ($k % 2) {
298 $typolinkValue = preg_replace('/<LINK[[:space:]]+/i', '', substr($foundValue, 0, -1));
299 $tLP = $this->getTypoLinkParts($typolinkValue);
300 $linkTags[$k] = '<LINK ' . $this->setTypoLinkPartsElement($tLP, $elements, $typolinkValue, $k) . '>';
301 }
302 }
303 // Return output:
304 if (count($elements)) {
305 $resultArray = array(
306 'content' => implode('', $linkTags),
307 'elements' => $elements
308 );
309 return $resultArray;
310 }
311 }
312
313 /**
314 * Processing the content expected from a TypoScript template
315 * This content includes references to files in fileadmin/ folders and file references in HTML tags like <img src="">, <a href=""> and <form action="">
316 *
317 * @param string $content The input content to analyse
318 * @param array $spParams Parameters set for the softref parser key in TCA/columns
319 * @return array Result array on positive matches, see description above. Otherwise FALSE
320 * @todo Define visibility
321 */
322 public function findRef_TStemplate($content, $spParams) {
323 $elements = array();
324 // First, try to find images and links:
325 $htmlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Html\\HtmlParser');
326 $splitContent = $htmlParser->splitTags('img,a,form', $content);
327 // Traverse splitted parts:
328 foreach ($splitContent as $k => $v) {
329 if ($k % 2) {
330 $attribs = $htmlParser->get_tag_attributes($v);
331 $attributeName = '';
332 switch ($htmlParser->getFirstTagName($v)) {
333 case 'img':
334 $attributeName = 'src';
335 break;
336 case 'a':
337 $attributeName = 'href';
338 break;
339 case 'form':
340 $attributeName = 'action';
341 break;
342 }
343 // Get file reference:
344 if (isset($attribs[0][$attributeName])) {
345 $srcRef = \TYPO3\CMS\Core\Utility\GeneralUtility::htmlspecialchars_decode($attribs[0][$attributeName]);
346 // Set entry:
347 $tokenID = $this->makeTokenID($k);
348 $elements[$k] = array();
349 $elements[$k]['matchString'] = $v;
350 // OK, if it looks like a local file from fileadmin/, include it:
351 $pI = pathinfo($srcRef);
352 $absPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_site . $srcRef);
353 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($srcRef, $this->fileAdminDir . '/') && !$pI['query'] && $absPath) {
354 // Token and substitute value:
355 // Very important that the src-value is not DeHSC'ed
356 if (strstr($splitContent[$k], $attribs[0][$attributeName])) {
357 $splitContent[$k] = str_replace($attribs[0][$attributeName], '{softref:' . $tokenID . '}', $splitContent[$k]);
358 $elements[$k]['subst'] = array(
359 'type' => 'file',
360 'relFileName' => $srcRef,
361 'tokenID' => $tokenID,
362 'tokenValue' => $attribs[0][$attributeName]
363 );
364 if (!@is_file($absPath)) {
365 $elements[$k]['error'] = 'File does not exist!';
366 }
367 } else {
368 $elements[$k]['error'] = 'Could not substitute attribute (' . $attributeName . ') value with token!';
369 }
370 }
371 }
372 }
373 }
374 $content = implode('', $splitContent);
375 // Process free fileadmin/ references as well:
376 $content = $this->fileadminReferences($content, $elements);
377 // Return output:
378 if (count($elements)) {
379 $resultArray = array(
380 'content' => $content,
381 'elements' => $elements
382 );
383 return $resultArray;
384 }
385 }
386
387 /**
388 * Processes possible references inside of Page and User TSconfig fields.
389 * Currently this only includes file references to fileadmin/ but in fact there are currently no properties that supports such references.
390 *
391 * @param string $content The input content to analyse
392 * @param array $spParams Parameters set for the softref parser key in TCA/columns
393 * @return array Result array on positive matches, see description above. Otherwise FALSE
394 * @todo Define visibility
395 */
396 public function findRef_TSconfig($content, $spParams) {
397 $elements = array();
398 // Process free fileadmin/ references from TSconfig
399 $content = $this->fileadminReferences($content, $elements);
400 // Return output:
401 if (count($elements)) {
402 $resultArray = array(
403 'content' => $content,
404 'elements' => $elements
405 );
406 return $resultArray;
407 }
408 }
409
410 /**
411 * Finding email addresses in content and making them substitutable.
412 *
413 * @param string $content The input content to analyse
414 * @param array $spParams Parameters set for the softref parser key in TCA/columns
415 * @return array Result array on positive matches, see description above. Otherwise FALSE
416 * @todo Define visibility
417 */
418 public function findRef_email($content, $spParams) {
419 $resultArray = array();
420 // Email:
421 $parts = preg_split('/([^[:alnum:]]+)([A-Za-z0-9\\._-]+[@][A-Za-z0-9\\._-]+[\\.].[A-Za-z0-9]+)/', ' ' . $content . ' ', 10000, PREG_SPLIT_DELIM_CAPTURE);
422 foreach ($parts as $idx => $value) {
423 if ($idx % 3 == 2) {
424 $tokenID = $this->makeTokenID($idx);
425 $elements[$idx] = array();
426 $elements[$idx]['matchString'] = $value;
427 if (is_array($spParams) && in_array('subst', $spParams)) {
428 $parts[$idx] = '{softref:' . $tokenID . '}';
429 $elements[$idx]['subst'] = array(
430 'type' => 'string',
431 'tokenID' => $tokenID,
432 'tokenValue' => $value
433 );
434 }
435 }
436 }
437 // Return output:
438 if (count($elements)) {
439 $resultArray = array(
440 'content' => substr(implode('', $parts), 1, -1),
441 'elements' => $elements
442 );
443 return $resultArray;
444 }
445 }
446
447 /**
448 * Finding URLs in content
449 *
450 * @param string $content The input content to analyse
451 * @param array $spParams Parameters set for the softref parser key in TCA/columns
452 * @return array Result array on positive matches, see description above. Otherwise FALSE
453 * @todo Define visibility
454 */
455 public function findRef_url($content, $spParams) {
456 $resultArray = array();
457 // Fileadmin files:
458 $parts = preg_split('/([^[:alnum:]"\']+)((http|ftp):\\/\\/[^[:space:]"\'<>]*)([[:space:]])/', ' ' . $content . ' ', 10000, PREG_SPLIT_DELIM_CAPTURE);
459 foreach ($parts as $idx => $value) {
460 if ($idx % 5 == 3) {
461 unset($parts[$idx]);
462 }
463 if ($idx % 5 == 2) {
464 $tokenID = $this->makeTokenID($idx);
465 $elements[$idx] = array();
466 $elements[$idx]['matchString'] = $value;
467 if (is_array($spParams) && in_array('subst', $spParams)) {
468 $parts[$idx] = '{softref:' . $tokenID . '}';
469 $elements[$idx]['subst'] = array(
470 'type' => 'string',
471 'tokenID' => $tokenID,
472 'tokenValue' => $value
473 );
474 }
475 }
476 }
477 // Return output:
478 if (count($elements)) {
479 $resultArray = array(
480 'content' => substr(implode('', $parts), 1, -1),
481 'elements' => $elements
482 );
483 return $resultArray;
484 }
485 }
486
487 /**
488 * Finding reference to files from extensions in content, but only to notify about their existence. No substitution
489 *
490 * @param string $content The input content to analyse
491 * @param array $spParams Parameters set for the softref parser key in TCA/columns
492 * @return array Result array on positive matches, see description above. Otherwise FALSE
493 * @todo Define visibility
494 */
495 public function findRef_extension_fileref($content, $spParams) {
496 $resultArray = array();
497 // Fileadmin files:
498 $parts = preg_split('/([^[:alnum:]"\']+)(EXT:[[:alnum:]_]+\\/[^[:space:]"\',]*)/', ' ' . $content . ' ', 10000, PREG_SPLIT_DELIM_CAPTURE);
499 foreach ($parts as $idx => $value) {
500 if ($idx % 3 == 2) {
501 $tokenID = $this->makeTokenID($idx);
502 $elements[$idx] = array();
503 $elements[$idx]['matchString'] = $value;
504 }
505 }
506 // Return output:
507 if (count($elements)) {
508 $resultArray = array(
509 'content' => substr(implode('', $parts), 1, -1),
510 'elements' => $elements
511 );
512 return $resultArray;
513 }
514 }
515
516 /*************************
517 *
518 * Helper functions
519 *
520 *************************/
521 /**
522 * Searches the content for a reference to a file in "fileadmin/".
523 * When a match is found it will get substituted with a token.
524 *
525 * @param string $content Input content to analyse
526 * @param array $elements Element array to be modified with new entries. Passed by reference.
527 * @return string Output content, possibly with tokens inserted.
528 * @todo Define visibility
529 */
530 public function fileadminReferences($content, &$elements) {
531 // Fileadmin files are found
532 $parts = preg_split('/([^[:alnum:]]+)(' . preg_quote($this->fileAdminDir, '/') . '\\/[^[:space:]"\'<>]*)/', ' ' . $content . ' ', 10000, PREG_SPLIT_DELIM_CAPTURE);
533 // Traverse files:
534 foreach ($parts as $idx => $value) {
535 if ($idx % 3 == 2) {
536 // when file is found, set up an entry for the element:
537 $tokenID = $this->makeTokenID('fileadminReferences:' . $idx);
538 $elements['fileadminReferences.' . $idx] = array();
539 $elements['fileadminReferences.' . $idx]['matchString'] = $value;
540 $elements['fileadminReferences.' . $idx]['subst'] = array(
541 'type' => 'file',
542 'relFileName' => $value,
543 'tokenID' => $tokenID,
544 'tokenValue' => $value
545 );
546 $parts[$idx] = '{softref:' . $tokenID . '}';
547 // Check if the file actually exists:
548 $absPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_site . $value);
549 if (!@is_file($absPath)) {
550 $elements['fileadminReferences.' . $idx]['error'] = 'File does not exist!';
551 }
552 }
553 }
554 // Implode the content again, removing prefixed and trailing white space:
555 return substr(implode('', $parts), 1, -1);
556 }
557
558 /**
559 * Analyse content as a TypoLink value and return an array with properties.
560 * TypoLinks format is: <link [typolink] [browser target] [css class]>. See tslib_content::typolink()
561 * The syntax of the [typolink] part is: [typolink] = [page id or alias][,[type value]][#[anchor, if integer = tt_content uid]]
562 * The extraction is based on how tslib_content::typolink() behaves.
563 *
564 * @param string $typolinkValue TypoLink value.
565 * @return array Array with the properties of the input link specified. The key "LINK_TYPE" will reveal the type. If that is blank it could not be determined.
566 * @see tslib_content::typolink(), setTypoLinkPartsElement()
567 * @todo Define visibility
568 */
569 public function getTypoLinkParts($typolinkValue) {
570 $finalTagParts = array();
571 // Split by space into link / target / class
572 list($link_param, $browserTarget, $cssClass) = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(' ', $typolinkValue, 1);
573 if (strlen($browserTarget)) {
574 $finalTagParts['target'] = $browserTarget;
575 }
576 if (strlen($cssClass)) {
577 $finalTagParts['class'] = $cssClass;
578 }
579 // Parse URL:
580 $pU = @parse_url($link_param);
581 // Detecting the kind of reference:
582 if (strstr($link_param, '@') && !$pU['scheme']) {
583 // If it's a mail address:
584 $link_param = preg_replace('/^mailto:/i', '', $link_param);
585 $finalTagParts['LINK_TYPE'] = 'mailto';
586 $finalTagParts['url'] = trim($link_param);
587 } else {
588 $isLocalFile = 0;
589 $fileChar = intval(strpos($link_param, '/'));
590 $urlChar = intval(strpos($link_param, '.'));
591 // Detects if a file is found in site-root and if so it will be treated like a normal file.
592 list($rootFileDat) = explode('?', rawurldecode($link_param));
593 $containsSlash = strstr($rootFileDat, '/');
594 $rFD_fI = pathinfo($rootFileDat);
595 if (trim($rootFileDat) && !$containsSlash && (@is_file((PATH_site . $rootFileDat)) || \TYPO3\CMS\Core\Utility\GeneralUtility::inList('php,html,htm', strtolower($rFD_fI['extension'])))) {
596 $isLocalFile = 1;
597 } elseif ($containsSlash) {
598 // Adding this so realurl directories are linked right (non-existing).
599 $isLocalFile = 2;
600 }
601 // Url (external): If doubleSlash or if a '.' comes before a '/'.
602 if ($pU['scheme'] || $isLocalFile != 1 && $urlChar && (!$containsSlash || $urlChar < $fileChar)) {
603 $finalTagParts['LINK_TYPE'] = 'url';
604 $finalTagParts['url'] = $link_param;
605 } elseif ($containsSlash || $isLocalFile) {
606 // file (internal)
607 $splitLinkParam = explode('?', $link_param);
608 if (file_exists(rawurldecode($splitLinkParam[0])) || $isLocalFile) {
609 $finalTagParts['LINK_TYPE'] = 'file';
610 $finalTagParts['filepath'] = rawurldecode($splitLinkParam[0]);
611 $finalTagParts['query'] = $splitLinkParam[1];
612 }
613 } else {
614 // integer or alias (alias is without slashes or periods or commas, that is
615 // 'nospace,alphanum_x,lower,unique' according to definition in $GLOBALS['TCA']!)
616 $finalTagParts['LINK_TYPE'] = 'page';
617 $link_params_parts = explode('#', $link_param);
618 $link_param = trim($link_params_parts[0]);
619 // Link-data del
620 if (strlen($link_params_parts[1])) {
621 $finalTagParts['anchor'] = trim($link_params_parts[1]);
622 }
623 // Splitting the parameter by ',' and if the array counts more than 1 element it's a id/type/? pair
624 $pairParts = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $link_param);
625 if (count($pairParts) > 1) {
626 $link_param = $pairParts[0];
627 $finalTagParts['type'] = $pairParts[1];
628 }
629 // Checking if the id-parameter is an alias.
630 if (strlen($link_param)) {
631 if (!\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($link_param)) {
632 $finalTagParts['alias'] = $link_param;
633 $link_param = $this->getPageIdFromAlias($link_param);
634 }
635 $finalTagParts['page_id'] = intval($link_param);
636 }
637 }
638 }
639 return $finalTagParts;
640 }
641
642 /**
643 * Recompile a TypoLink value from the array of properties made with getTypoLinkParts() into an elements array
644 *
645 * @param array $tLP TypoLink properties
646 * @param array $elements Array of elements to be modified with substitution / information entries.
647 * @param string $content The content to process.
648 * @param integer $idx Index value of the found element - user to make unique but stable tokenID
649 * @return string The input content, possibly containing tokens now according to the added substitution entries in $elements
650 * @see getTypoLinkParts()
651 * @todo Define visibility
652 */
653 public function setTypoLinkPartsElement($tLP, &$elements, $content, $idx) {
654 // Initialize, set basic values. In any case a link will be shown
655 $tokenID = $this->makeTokenID('setTypoLinkPartsElement:' . $idx);
656 $elements[$tokenID . ':' . $idx] = array();
657 $elements[$tokenID . ':' . $idx]['matchString'] = $content;
658 // Based on link type, maybe do more:
659 switch ((string) $tLP['LINK_TYPE']) {
660 case 'mailto':
661
662 case 'url':
663 // Mail addresses and URLs can be substituted manually:
664 $elements[$tokenID . ':' . $idx]['subst'] = array(
665 'type' => 'string',
666 'tokenID' => $tokenID,
667 'tokenValue' => $tLP['url']
668 );
669 // Output content will be the token instead:
670 $content = '{softref:' . $tokenID . '}';
671 break;
672 case 'file':
673 // Process files found in fileadmin directory:
674 if (!$tLP['query']) {
675 // We will not process files which has a query added to it. That will look like a script we don't want to move.
676 // File must be inside fileadmin/
677 if (\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($tLP['filepath'], $this->fileAdminDir . '/')) {
678 // Set up the basic token and token value for the relative file:
679 $elements[$tokenID . ':' . $idx]['subst'] = array(
680 'type' => 'file',
681 'relFileName' => $tLP['filepath'],
682 'tokenID' => $tokenID,
683 'tokenValue' => $tLP['filepath']
684 );
685 // Depending on whether the file exists or not we will set the
686 $absPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(PATH_site . $tLP['filepath']);
687 if (!@is_file($absPath)) {
688 $elements[$tokenID . ':' . $idx]['error'] = 'File does not exist!';
689 }
690 // Output content will be the token instead
691 $content = '{softref:' . $tokenID . '}';
692 } else {
693 return $content;
694 }
695 } else {
696 return $content;
697 }
698 break;
699 case 'page':
700 // Rebuild page reference typolink part:
701 $content = '';
702 // Set page id:
703 if ($tLP['page_id']) {
704 $content .= '{softref:' . $tokenID . '}';
705 $elements[$tokenID . ':' . $idx]['subst'] = array(
706 'type' => 'db',
707 'recordRef' => 'pages:' . $tLP['page_id'],
708 'tokenID' => $tokenID,
709 'tokenValue' => $tLP['alias'] ? $tLP['alias'] : $tLP['page_id']
710 );
711 }
712 // Add type if applicable
713 if (strlen($tLP['type'])) {
714 $content .= ',' . $tLP['type'];
715 }
716 // Add anchor if applicable
717 if (strlen($tLP['anchor'])) {
718 // Anchor is assumed to point to a content elements:
719 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($tLP['anchor'])) {
720 // Initialize a new entry because we have a new relation:
721 $newTokenID = $this->makeTokenID('setTypoLinkPartsElement:anchor:' . $idx);
722 $elements[$newTokenID . ':' . $idx] = array();
723 $elements[$newTokenID . ':' . $idx]['matchString'] = 'Anchor Content Element: ' . $tLP['anchor'];
724 $content .= '#{softref:' . $newTokenID . '}';
725 $elements[$newTokenID . ':' . $idx]['subst'] = array(
726 'type' => 'db',
727 'recordRef' => 'tt_content:' . $tLP['anchor'],
728 'tokenID' => $newTokenID,
729 'tokenValue' => $tLP['anchor']
730 );
731 } else {
732 // Anchor is a hardcoded string
733 $content .= '#' . $tLP['type'];
734 }
735 }
736 break;
737 default:
738 $elements[$tokenID . ':' . $idx]['error'] = 'Couldn\\t decide typolink mode.';
739 return $content;
740 break;
741 }
742 // Finally, for all entries that was rebuild with tokens, add target and class in the end:
743 if (strlen($content) && strlen($tLP['target'])) {
744 $content .= ' ' . $tLP['target'];
745 if (strlen($tLP['class'])) {
746 $content .= ' ' . $tLP['class'];
747 }
748 }
749 // Return rebuilt typolink value:
750 return $content;
751 }
752
753 /**
754 * Look up and return page uid for alias
755 *
756 * @param integer $link_param Page alias string value
757 * @return integer Page uid corresponding to alias value.
758 * @todo Define visibility
759 */
760 public function getPageIdFromAlias($link_param) {
761 $pRec = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecordsByField('pages', 'alias', $link_param);
762 return $pRec[0]['uid'];
763 }
764
765 /**
766 * Make Token ID for input index.
767 *
768 * @param string $index Suffix value.
769 * @return string Token ID
770 * @todo Define visibility
771 */
772 public function makeTokenID($index = '') {
773 return md5($this->tokenID_basePrefix . ':' . $index);
774 }
775
776 }
777
778
779 ?>