* Slightly changed a comment
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_div.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains the reknown class "t3lib_div" with general purpose functions
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
32 * XHTML compliant
33 * Usage counts are based on search 22/2 2003 through whole source including tslib/
34 *
35 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
36 */
37 /**
38 * [CLASS/FUNCTION INDEX of SCRIPT]
39 *
40 *
41 *
42 * 211: class t3lib_div
43 *
44 * SECTION: GET/POST Variables
45 * 241: function _GP($var)
46 * 259: function _GET($var=NULL)
47 * 276: function _POST($var=NULL)
48 * 292: function _GETset($inputGet,$key='')
49 * 315: function GPvar($var,$strip=0)
50 * 332: function GParrayMerged($var)
51 *
52 * SECTION: IMAGE FUNCTIONS
53 * 376: function gif_compress($theFile, $type)
54 * 402: function png_to_gif_by_imagemagick($theFile)
55 * 426: function read_png_gif($theFile,$output_png=0)
56 *
57 * SECTION: STRING FUNCTIONS
58 * 475: function fixed_lgd($string,$origChars,$preStr='...')
59 * 500: function fixed_lgd_pre($string,$chars)
60 * 514: function fixed_lgd_cs($string,$chars)
61 * 531: function breakTextForEmail($str,$implChar="\n",$charWidth=76)
62 * 550: function breakLinesForEmail($str,$implChar="\n",$charWidth=76)
63 * 585: function cmpIP($baseIP, $list)
64 * 627: function cmpFQDN($baseIP, $list)
65 * 657: function inList($in_list,$item)
66 * 669: function rmFromList($element,$list)
67 * 687: function intInRange($theInt,$min,$max=2000000000,$zeroValue=0)
68 * 703: function intval_positive($theInt)
69 * 716: function int_from_ver($verNumberStr)
70 * 728: function md5int($str)
71 * 741: function shortMD5($input, $len=10)
72 * 754: function uniqueList($in_list, $secondParameter=NULL)
73 * 768: function split_fileref($fileref)
74 * 805: function dirname($path)
75 * 821: function modifyHTMLColor($color,$R,$G,$B)
76 * 841: function modifyHTMLColorAll($color,$all)
77 * 852: function rm_endcomma($string)
78 * 865: function danish_strtoupper($string)
79 * 880: function convUmlauts($str)
80 * 893: function testInt($var)
81 * 905: function isFirstPartOfStr($str,$partStr)
82 * 921: function formatSize($sizeInBytes,$labels='')
83 * 956: function convertMicrotime($microtime)
84 * 970: function splitCalc($string,$operators)
85 * 992: function calcPriority($string)
86 * 1032: function calcParenthesis($string)
87 * 1058: function htmlspecialchars_decode($value)
88 * 1073: function deHSCentities($str)
89 * 1086: function slashJS($string,$extended=0,$char="'")
90 * 1099: function rawUrlEncodeJS($str)
91 * 1111: function rawUrlEncodeFP($str)
92 * 1122: function validEmail($email)
93 * 1137: function formatForTextarea($content)
94 *
95 * SECTION: ARRAY FUNCTIONS
96 * 1168: function inArray($in_array,$item)
97 * 1185: function intExplode($delim, $string)
98 * 1204: function revExplode($delim, $string, $count=0)
99 * 1224: function trimExplode($delim, $string, $onlyNonEmptyValues=0)
100 * 1246: function uniqueArray($valueArray)
101 * 1258: function removeArrayEntryByValue($array,$cmpValue)
102 * 1286: function implodeArrayForUrl($name,$theArray,$str='',$skipBlank=0,$rawurlencodeParamName=0)
103 * 1313: function compileSelectedGetVarsFromArray($varList,$getArray,$GPvarAlt=1)
104 * 1336: function addSlashesOnArray(&$theArray)
105 * 1360: function stripSlashesOnArray(&$theArray)
106 * 1382: function slashArray($arr,$cmd)
107 * 1399: function array_merge_recursive_overrule($arr0,$arr1,$notAddKeys=0,$includeEmtpyValues=true)
108 * 1432: function array_merge($arr1,$arr2)
109 * 1445: function csvValues($row,$delim=',',$quote='"')
110 *
111 * SECTION: HTML/XML PROCESSING
112 * 1487: function get_tag_attributes($tag)
113 * 1524: function split_tag_attributes($tag)
114 * 1558: function implodeAttributes($arr,$xhtmlSafe=FALSE,$dontOmitBlankAttribs=FALSE)
115 * 1585: function implodeParams($arr,$xhtmlSafe=FALSE,$dontOmitBlankAttribs=FALSE)
116 * 1600: function wrapJS($string, $linebreak=TRUE)
117 * 1630: function xml2tree($string,$depth=999)
118 * 1716: function array2xml($array,$NSprefix='',$level=0,$docTag='phparray',$spaceInd=0, $options=array(),$stackData=array())
119 * 1830: function xml2array($string,$NSprefix='',$reportDocTag=FALSE)
120 * 1940: function xmlRecompileFromStructValArray($vals)
121 * 1984: function xmlGetHeaderAttribs($xmlData)
122 *
123 * SECTION: FILES FUNCTIONS
124 * 2016: function getURL($url)
125 * 2058: function writeFile($file,$content)
126 * 2075: function fixPermissions($file)
127 * 2092: function writeFileToTypo3tempDir($filepath,$content)
128 * 2135: function mkdir($theNewFolder)
129 * 2155: function get_dirs($path)
130 * 2180: function getFilesInDir($path,$extensionList='',$prependPath=0,$order='')
131 * 2234: function getAllFilesAndFoldersInPath($fileArr,$path,$extList='',$regDirs=0,$recursivityLevels=99)
132 * 2257: function removePrefixPathFromList($fileArr,$prefixToRemove)
133 * 2273: function fixWindowsFilePath($theFile)
134 * 2285: function resolveBackPath($pathStr)
135 * 2312: function locationHeaderUrl($path)
136 *
137 * SECTION: DEBUG helper FUNCTIONS
138 * 2352: function debug_ordvalue($string,$characters=100)
139 * 2369: function view_array($array_in)
140 * 2397: function print_array($array_in)
141 * 2412: function debug($var="",$brOrHeader=0)
142 *
143 * SECTION: SYSTEM INFORMATION
144 * 2481: function getThisUrl()
145 * 2497: function linkThisScript($getParams=array())
146 * 2521: function linkThisUrl($url,$getParams=array())
147 * 2546: function getIndpEnv($getEnvName)
148 * 2735: function milliseconds()
149 * 2747: function clientInfo($useragent='')
150 *
151 * SECTION: TYPO3 SPECIFIC FUNCTIONS
152 * 2834: function getFileAbsFileName($filename,$onlyRelative=1,$relToTYPO3_mainDir=0)
153 * 2870: function validPathStr($theFile)
154 * 2881: function isAbsPath($path)
155 * 2892: function isAllowedAbsPath($path)
156 * 2909: function verifyFilenameAgainstDenyPattern($filename)
157 * 2927: function upload_copy_move($source,$destination)
158 * 2953: function upload_to_tempfile($uploadedFileName)
159 * 2971: function unlink_tempfile($uploadedTempFileName)
160 * 2987: function tempnam($filePrefix)
161 * 3000: function stdAuthCode($uid_or_record,$fields='')
162 * 3030: function cHashParams($addQueryParams)
163 * 3059: function loadTCA($table)
164 * 3079: function resolveSheetDefInDS($dataStructArray,$sheet='sDEF')
165 * 3109: function resolveAllSheetsInDS($dataStructArray)
166 * 3138: function callUserFunction($funcName,&$params,&$ref,$checkPrefix='user_',$silent=0)
167 * 3237: function &getUserObj($classRef,$checkPrefix='user_',$silent=0)
168 * 3295: function &makeInstance($className)
169 * 3307: function makeInstanceClassName($className)
170 * 3321: function &makeInstanceService($serviceType, $serviceSubType='', $excludeServiceKeys=array())
171 * 3382: function plainMailEncoded($email,$subject,$message,$headers='',$enc='',$charset='ISO-8859-1',$dontEncodeSubject=0)
172 * 3427: function quoted_printable($string,$maxlen=76)
173 * 3471: function substUrlsInPlainText($message,$urlmode='76',$index_script_url='')
174 * 3505: function makeRedirectUrl($inUrl,$l=0,$index_script_url='')
175 * 3532: function freetypeDpiComp($font_size)
176 * 3551: function devLog($msg, $extKey, $severity=0, $dataVar=FALSE)
177 * 3572: function arrayToLogString($arr, $valueList=array(), $valueLength=20)
178 *
179 * TOTAL FUNCTIONS: 117
180 * (This index is automatically created/updated by the extension "extdeveval")
181 *
182 */
183
184
185
186
187
188
189
190
191
192
193
194
195 /**
196 * The legendary "t3lib_div" class - Miscellaneous functions for general purpose.
197 * Most of the functions does not relate specifically to TYPO3
198 * However a section of functions requires certain TYPO3 features available
199 * See comments in the source.
200 * You are encouraged to use this library in your own scripts!
201 *
202 * USE:
203 * The class is intended to be used without creating an instance of it.
204 * So: Don't instantiate - call functions with "t3lib_div::" prefixed the function name.
205 * So use t3lib_div::[method-name] to refer to the functions, eg. 't3lib_div::milliseconds()'
206 *
207 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
208 * @package TYPO3
209 * @subpackage t3lib
210 */
211 class t3lib_div {
212
213
214
215
216
217 /*************************
218 *
219 * GET/POST Variables
220 *
221 * Background:
222 * Input GET/POST variables in PHP may have their quotes escaped with "\" or not depending on configuration.
223 * TYPO3 has always converted quotes to BE escaped if the configuration told that they would not be so.
224 * But the clean solution is that quotes are never escaped and that is what the functions below offers.
225 * Eventually TYPO3 should provide this in the global space as well.
226 * In the transitional phase (or forever..?) we need to encourage EVERY to read and write GET/POST vars through the API functions below.
227 *
228 *************************/
229
230 /**
231 * Returns the 'GLOBAL' value of incoming data from POST or GET, with priority to POST (that is equalent to 'GP' order)
232 * Strips slashes from all output, both strings and arrays.
233 * This function substitutes t3lib_div::GPvar()
234 * To enhancement security in your scripts, please consider using t3lib_div::_GET or t3lib_div::_POST if you already know by which method your data is arriving to the scripts!
235 * Usage: 537
236 *
237 * @param string GET/POST var to return
238 * @return mixed POST var named $var and if not set, the GET var of the same name.
239 * @see GPvar()
240 */
241 function _GP($var) {
242 if(empty($var)) return;
243 $value = isset($_POST[$var]) ? $_POST[$var] : $_GET[$var];
244 if (isset($value)) {
245 if (is_array($value)) { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
246 }
247 return $value;
248 }
249
250 /**
251 * Returns the global GET array (or value from) normalized to contain un-escaped values.
252 * ALWAYS use this API function to acquire the GET variables!
253 * Usage: 27
254 *
255 * @param string Optional pointer to value in GET array (basically name of GET var)
256 * @return mixed If $var is set it returns the value of $_GET[$var]. If $var is NULL (default), returns $_GET itself. In any case *slashes are stipped from the output!*
257 * @see _POST(), _GP(), _GETset()
258 */
259 function _GET($var=NULL) {
260 $value = ($var === NULL) ? $_GET : (empty($var) ? NULL : $_GET[$var]);
261 if (isset($value)) { // Removes slashes since TYPO3 has added them regardless of magic_quotes setting.
262 if (is_array($value)) { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
263 }
264 return $value;
265 }
266
267 /**
268 * Returns the global POST array (or value from) normalized to contain un-escaped values.
269 * ALWAYS use this API function to acquire the POST variables!
270 * Usage: 41
271 *
272 * @param string Optional pointer to value in POST array (basically name of POST var)
273 * @return mixed If $var is set it returns the value of $_POST[$var]. If $var is NULL (default), returns $_POST itself. In any case *slashes are stipped from the output!*
274 * @see _GET(), _GP()
275 */
276 function _POST($var=NULL) {
277 $value = ($var === NULL) ? $_POST : (empty($var) ? NULL : $_POST[$var]);
278 if (isset($value)) { // Removes slashes since TYPO3 has added them regardless of magic_quotes setting.
279 if (is_array($value)) { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
280 }
281 return $value;
282 }
283
284 /**
285 * Writes input value to $_GET
286 * Usage: 2
287 *
288 * @param array Array to write to $_GET. Values should NOT be escaped at input time (but will be escaped before writing according to TYPO3 standards).
289 * @param string Alternative key; If set, this will not set the WHOLE GET array, but only the key in it specified by this value!
290 * @return void
291 */
292 function _GETset($inputGet,$key='') {
293 // ADDS slashes since TYPO3 standard currently is that slashes MUST be applied (regardless of magic_quotes setting).
294 if (strcmp($key,'')) {
295 if (is_array($inputGet)) { t3lib_div::addSlashesOnArray($inputGet); } else { $inputGet = addslashes($inputGet); }
296 $GLOBALS['HTTP_GET_VARS'][$key] = $_GET[$key] = $inputGet;
297 } elseif (is_array($inputGet)){
298 t3lib_div::addSlashesOnArray($inputGet);
299 $GLOBALS['HTTP_GET_VARS'] = $_GET = $inputGet;
300 }
301 }
302
303 /**
304 * GET/POST variable
305 * Returns the 'GLOBAL' value of incoming data from POST or GET, with priority to POST (that is equalent to 'GP' order)
306 * Strips slashes of string-outputs, but not arrays UNLESS $strip is set. If $strip is set all output will have escaped characters unescaped.
307 * Usage: 2
308 *
309 * @param string GET/POST var to return
310 * @param boolean If set, values are stripped of return values that are *arrays!* - string/integer values returned are always strip-slashed()
311 * @return mixed POST var named $var and if not set, the GET var of the same name.
312 * @deprecated Use t3lib_div::_GP instead (ALWAYS delivers a value with un-escaped values!)
313 * @see _GP()
314 */
315 function GPvar($var,$strip=0) {
316 if(empty($var)) return;
317 $value = isset($_POST[$var]) ? $_POST[$var] : $_GET[$var];
318 if (isset($value) && is_string($value)) { $value = stripslashes($value); } // Originally check '&& get_magic_quotes_gpc() ' but the values of $_GET are always slashed regardless of get_magic_quotes_gpc() because HTTP_POST/GET_VARS are run through addSlashesOnArray in the very beginning of index_ts.php eg.
319 if ($strip && isset($value) && is_array($value)) { t3lib_div::stripSlashesOnArray($value); }
320 return $value;
321 }
322
323 /**
324 * Returns the GET/POST global arrays merged with POST taking precedence.
325 * Usage: 1
326 *
327 * @param string Key (variable name) from GET or POST vars
328 * @return array Returns the GET vars merged recursively onto the POST vars.
329 * @ignore
330 * @deprecated
331 */
332 function GParrayMerged($var) {
333 $postA = is_array($_POST[$var]) ? $_POST[$var] : array();
334 $getA = is_array($_GET[$var]) ? $_GET[$var] : array();
335 $mergedA = t3lib_div::array_merge_recursive_overrule($getA,$postA);
336 t3lib_div::stripSlashesOnArray($mergedA);
337 return $mergedA;
338 }
339
340
341
342
343
344
345
346
347
348
349 /*************************
350 *
351 * IMAGE FUNCTIONS
352 *
353 *************************/
354
355
356 /**
357 * Compressing a GIF file if not already LZW compressed
358 * This function is a workaround for the fact that ImageMagick and/or GD does not compress GIF-files to their minimun size (that is RLE or no compression used)
359 *
360 * The function takes a file-reference, $theFile, and saves it again through GD or ImageMagick in order to compress the file
361 * GIF:
362 * If $type is not set, the compression is done with ImageMagick (provided that $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'] is pointing to the path of a lzw-enabled version of 'convert') else with GD (should be RLE-enabled!)
363 * If $type is set to either 'IM' or 'GD' the compression is done with ImageMagick and GD respectively
364 * PNG:
365 * No changes.
366 *
367 * $theFile is expected to be a valid GIF-file!
368 * The function returns a code for the operation.
369 * Usage: 9
370 *
371 * @param string Filepath
372 * @param string See description of function
373 * @return string Returns "GD" if GD was used, otherwise "IM" if ImageMagick was used. If nothing done at all, it returns empty string.
374 * @internal
375 */
376 function gif_compress($theFile, $type) {
377 $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
378 $returnCode='';
379 if ($gfxConf['gif_compress'] && strtolower(substr($theFile,-4,4))=='.gif') { // GIF...
380 if (($type=='IM' || !$type) && $gfxConf['im'] && $gfxConf['im_path_lzw']) { // IM
381 $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$theFile.'"', $gfxConf['im_path_lzw']);
382 exec($cmd);
383
384 $returnCode='IM';
385 } elseif (($type=='GD' || !$type) && $gfxConf['gdlib'] && !$gfxConf['gdlib_png']) { // GD
386 $tempImage = imageCreateFromGif($theFile);
387 imageGif($tempImage, $theFile);
388 imageDestroy($tempImage);
389 $returnCode='GD';
390 }
391 }
392 return $returnCode;
393 }
394
395 /**
396 * Converts a png file to gif
397 * This converts a png file to gif IF the FLAG $GLOBALS['TYPO3_CONF_VARS']['FE']['png_to_gif'] is set true.
398 * Usage: 5
399 *
400 * @param string $theFile the filename with path
401 * @return string new filename
402 * @internal
403 */
404 function png_to_gif_by_imagemagick($theFile) {
405 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['png_to_gif']
406 && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im']
407 && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']
408 && strtolower(substr($theFile,-4,4))=='.png'
409 && @is_file($theFile)) { // IM
410 $newFile = substr($theFile,0,-4).'.gif';
411 $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$newFile.'"', $gfxConf['im_path_lzw']);
412 exec($cmd);
413 $theFile = $newFile;
414 // unlink old file?? May be bad idea bacause TYPO3 would then recreate the file every time as TYPO3 thinks the file is not generated because it's missing!! So do not unlink $theFile here!!
415 }
416 return $theFile;
417 }
418
419 /**
420 * Returns filename of the png/gif version of the input file (which can be png or gif).
421 * If input file type does not match the wanted output type a conversion is made and temp-filename returned.
422 * Usage: 2
423 *
424 * @param string Filepath of image file
425 * @param boolean If set, then input file is converted to PNG, otherwise to GIF
426 * @return string If the new image file exists, it's filepath is returned
427 * @internal
428 */
429 function read_png_gif($theFile,$output_png=0) {
430 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] && @is_file($theFile)) {
431 $ext = strtolower(substr($theFile,-4,4));
432 if (
433 ((string)$ext=='.png' && $output_png) ||
434 ((string)$ext=='.gif' && !$output_png)
435 ) {
436 return $theFile;
437 } else {
438 $newFile = PATH_site.'typo3temp/readPG_'.md5($theFile.'|'.filemtime($theFile)).($output_png?'.png':'.gif');
439 exec($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'].'convert "'.$theFile.'" "'.$newFile.'"');
440 if (@is_file($newFile)) return $newFile;
441 }
442 }
443 }
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459 /*************************
460 *
461 * STRING FUNCTIONS
462 *
463 *************************/
464
465 /**
466 * Truncate string
467 * Returns a new string of max. $chars length.
468 * If the string is longer, it will be truncated and appended with '...'.
469 * DEPRECIATED. Works ONLY for single-byte charsets! USE t3lib_div::fixed_lgd_cs() instead
470 * Usage: 39
471 *
472 * @param string $string string to truncate
473 * @param integer $chars must be an integer with an absolute value of at least 4. if negative the string is cropped from the right end.
474 * @param string String to append to the output if it is truncated, default is '...'
475 * @return string new string
476 * @see fixed_lgd_pre()
477 */
478 function fixed_lgd($string,$origChars,$preStr='...') {
479 $chars = abs($origChars);
480 if ($chars >= 4) {
481 if(strlen($string)>$chars) {
482 return $origChars < 0 ?
483 $preStr.trim(substr($string, -($chars-3))) :
484 trim(substr($string, 0, $chars-3)).$preStr;
485 }
486 }
487 return $string;
488 }
489
490 /**
491 * Truncate string
492 * Returns a new string of max. $chars length.
493 * If the string is longer, it will be truncated and prepended with '...'.
494 * This works like fixed_lgd, but is truncated in the start of the string instead of the end
495 * DEPRECIATED. Use either fixed_lgd() or fixed_lgd_cs() (with negative input value for $chars)
496 * Usage: 6
497 *
498 * @param string $string string to truncate
499 * @param integer $chars must be an integer of at least 4
500 * @return string new string
501 * @see fixed_lgd()
502 */
503 function fixed_lgd_pre($string,$chars) {
504 return strrev(t3lib_div::fixed_lgd(strrev($string),$chars));
505 }
506
507 /**
508 * Truncates a string with appended/prepended "..." and takes backend character set into consideration
509 * Use only from backend!
510 * Usage: 75
511 *
512 * @param string $string string to truncate
513 * @param integer $chars must be an integer with an absolute value of at least 4. if negative the string is cropped from the right end.
514 * @return string New string
515 * @see fixed_lgd()
516 */
517 function fixed_lgd_cs($string,$chars) {
518 if (is_object($GLOBALS['LANG'])) {
519 return $GLOBALS['LANG']->csConvObj->crop($GLOBALS['LANG']->charSet,$string,$chars,'...');
520 } else {
521 return t3lib_div::fixed_lgd($string, $chars);
522 }
523 }
524
525 /**
526 * Breaks up the text for emails
527 * Usage: 1
528 *
529 * @param string The string to break up
530 * @param string The string to implode the broken lines with (default/typically \n)
531 * @param integer The line length
532 * @return string
533 */
534 function breakTextForEmail($str,$implChar="\n",$charWidth=76) {
535 $lines = explode(chr(10),$str);
536 $outArr=array();
537 while(list(,$lStr)=each($lines)) {
538 $outArr = array_merge($outArr,t3lib_div::breakLinesForEmail($lStr,$implChar,$charWidth));
539 }
540 return implode(chr(10),$outArr);
541 }
542
543 /**
544 * Breaks up a single line of text for emails
545 * Usage: 5
546 *
547 * @param string The string to break up
548 * @param string The string to implode the broken lines with (default/typically \n)
549 * @param integer The line length
550 * @return string
551 * @see breakTextForEmail()
552 */
553 function breakLinesForEmail($str,$implChar="\n",$charWidth=76) {
554 $lines=array();
555 $l=$charWidth;
556 $p=0;
557 while(strlen($str)>$p) {
558 $substr=substr($str,$p,$l);
559 if (strlen($substr)==$l) {
560 $count = count(explode(' ',trim(strrev($substr))));
561 if ($count>1) { // OK...
562 $parts = explode(' ',strrev($substr),2);
563 $theLine = strrev($parts[1]);
564 } else {
565 $afterParts = explode(' ',substr($str,$l+$p),2);
566 $theLine = $substr.$afterParts[0];
567 }
568 if (!strlen($theLine)) {break; } // Error, because this would keep us in an endless loop.
569 } else {
570 $theLine=$substr;
571 }
572
573 $lines[]=trim($theLine);
574 $p+=strlen($theLine);
575 if (!trim(substr($str,$p,$l))) break; // added...
576 }
577 return implode($implChar,$lines);
578 }
579
580 /**
581 * Match IP number with list of numbers with wildcard
582 * Usage: 10
583 *
584 * @param string $baseIP is the current remote IP address for instance, typ. REMOTE_ADDR
585 * @param string $list is a comma-list of IP-addresses to match with. *-wildcard allowed instead of number, plus leaving out parts in the IP number is accepted as wildcard (eg. 192.168.*.* equals 192.168)
586 * @return boolean True if an IP-mask from $list matches $baseIP
587 */
588 function cmpIP($baseIP, $list) {
589 $IPpartsReq = explode('.',$baseIP);
590 if (count($IPpartsReq)==4) {
591 $values = t3lib_div::trimExplode(',',$list,1);
592
593 foreach($values as $test) {
594 list($test,$mask) = explode('/',$test);
595
596 if(intval($mask)) {
597 // "192.168.3.0/24"
598 $lnet = ip2long($test);
599 $lip = ip2long($baseIP);
600 $binnet = str_pad( decbin($lnet),32,'0','STR_PAD_LEFT');
601 $firstpart = substr($binnet,0,$mask);
602 $binip = str_pad( decbin($lip),32,'0','STR_PAD_LEFT');
603 $firstip = substr($binip,0,$mask);
604 $yes = (strcmp($firstpart,$firstip)==0);
605 } else {
606 // "192.168.*.*"
607 $IPparts = explode('.',$test);
608 $yes = 1;
609 reset($IPparts);
610 while(list($index,$val)=each($IPparts)) {
611 $val = trim($val);
612 if (strcmp($val,'*') && strcmp($IPpartsReq[$index],$val)) {
613 $yes=0;
614 }
615 }
616 }
617 if ($yes) return true;
618 }
619 }
620 return false;
621 }
622
623 /**
624 * Match fully qualified domain name with list of strings with wildcard
625 *
626 * @param string $baseIP is the current remote IP address for instance, typ. REMOTE_ADDR
627 * @param string $list is a comma-list of domain names to match with. *-wildcard allowed but cannot be part of a string, so it must match the full host name (eg. myhost.*.com => correct, myhost.*domain.com => wrong)
628 * @return boolean True if a domain name mask from $list matches $baseIP
629 */
630 function cmpFQDN($baseIP, $list) {
631 if (count(explode('.',$baseIP))==4) {
632 $resolvedHostName = explode('.', gethostbyaddr($baseIP));
633 $values = t3lib_div::trimExplode(',',$list,1);
634
635 foreach($values as $test) {
636 $hostNameParts = explode('.',$test);
637 $yes = 1;
638
639 foreach($hostNameParts as $index => $val) {
640 $val = trim($val);
641 if (strcmp($val,'*') && strcmp($resolvedHostName[$index],$val)) {
642 $yes=0;
643 }
644 }
645 if ($yes) return true;
646 }
647 }
648 return false;
649 }
650
651 /**
652 * Check for item in list
653 * Check if an item exists in a comma-separated list of items.
654 * Usage: 163
655 *
656 * @param string $in_list comma-separated list of items (string)
657 * @param string $item item to check for
658 * @return boolean true if $item is in $in_list
659 */
660 function inList($in_list,$item) {
661 return strstr(','.$in_list.',', ','.$item.',');
662 }
663
664 /**
665 * Removes an item from a comma-separated list of items.
666 * Usage: 1
667 *
668 * @param string $element element to remove
669 * @param string $list comma-separated list of items (string)
670 * @return string new comma-separated list of items
671 */
672 function rmFromList($element,$list) {
673 $items = explode(',',$list);
674 while(list($k,$v)=each($items)) {
675 if ($v==$element) {unset($items[$k]);}
676 }
677 return implode(',',$items);
678 }
679
680 /**
681 * Expand a comma-separated list of integers with ranges (eg 1,3-5,7 becomes 1,3,4,5,7).
682 * Ranges are limited to 1000 values per range.
683 *
684 * @param string $list comma-separated list of integers with ranges (string)
685 * @return string new comma-separated list of items
686 * @author Martin Kutschker <martin.kutschker@activesolution.at>
687 */
688 function expandList($list) {
689 $items = explode(',',$list);
690 $list = array();
691 while(list(,$item)=each($items)) {
692 $range = explode('-',$item);
693 if (isset($range[1])) {
694 $runAwayBrake = 1000;
695 for ($n=$range[0]; $n<=$range[1]; $n++) {
696 $list[] = $n;
697
698 $runAwayBrake--;
699 if ($runAwayBrake<=0) break;
700 }
701 } else {
702 $list[] = $item;
703 }
704 }
705
706 return implode(',',$list);
707 }
708
709 /**
710 * Forces the integer $theInt into the boundaries of $min and $max. If the $theInt is 'false' then the $zeroValue is applied.
711 * Usage: 224
712 *
713 * @param integer Input value
714 * @param integer Lower limit
715 * @param integer Higher limit
716 * @param integer Default value if input is false.
717 * @return integer The input value forced into the boundaries of $min and $max
718 */
719 function intInRange($theInt,$min,$max=2000000000,$zeroValue=0) {
720 // Returns $theInt as an integer in the integerspace from $min to $max
721 $theInt = intval($theInt);
722 if ($zeroValue && !$theInt) {$theInt=$zeroValue;} // If the input value is zero after being converted to integer, zeroValue may set another default value for it.
723 if ($theInt<$min){$theInt=$min;}
724 if ($theInt>$max){$theInt=$max;}
725 return $theInt;
726 }
727
728 /**
729 * Returns the $integer if greater than zero, otherwise returns zero.
730 * Usage: 1
731 *
732 * @param integer Integer string to process
733 * @return integer
734 */
735 function intval_positive($theInt) {
736 $theInt = intval($theInt);
737 if ($theInt<0){$theInt=0;}
738 return $theInt;
739 }
740
741 /**
742 * Returns an integer from a three part version number, eg '4.12.3' -> 4012003
743 * Usage: 2
744 *
745 * @param string Version number on format x.x.x
746 * @return integer Integer version of version number (where each part can count to 999)
747 */
748 function int_from_ver($verNumberStr) {
749 $verParts = explode('.',$verNumberStr);
750 return intval((int)$verParts[0].str_pad((int)$verParts[1],3,'0',STR_PAD_LEFT).str_pad((int)$verParts[2],3,'0',STR_PAD_LEFT));
751 }
752
753 /**
754 * Makes a positive integer hash out of the first 7 chars from the md5 hash of the input
755 * Usage: 5
756 *
757 * @param string String to md5-hash
758 * @return integer Returns 28bit integer-hash
759 */
760 function md5int($str) {
761 return hexdec(substr(md5($str),0,7));
762 }
763
764 /**
765 * Returns the first 10 positions of the MD5-hash (changed from 6 to 10 recently)
766 *
767 * Usage: 37
768 *
769 * @param string Input string to be md5-hashed
770 * @param integer The string-length of the output
771 * @return string Substring of the resulting md5-hash, being $len chars long (from beginning)
772 */
773 function shortMD5($input, $len=10) {
774 return substr(md5($input),0,$len);
775 }
776
777 /**
778 * Takes comma-separated lists and arrays and removes all duplicates
779 * If a value in the list is trim(empty), the value is ignored.
780 * Usage: 16
781 *
782 * @param string Accept multiple parameters wich can be comma-separated lists of values and arrays.
783 * @param mixed $secondParameter: Dummy field, which is set will show a warning!
784 * @return string Returns the list without any duplicates of values, space around values are trimmed
785 */
786 function uniqueList($in_list, $secondParameter=NULL) {
787 if (is_array($in_list)) die('t3lib_div::uniqueList() does NOT support array arguments anymore! Only string comma lists!');
788 if (isset($secondParameter)) die('t3lib_div::uniqueList() does NOT support more than a single argument value anymore. You have specified more than one.');
789
790 return implode(',',array_unique(t3lib_div::trimExplode(',',$in_list,1)));
791 }
792
793 /**
794 * Splits a reference to a file in 5 parts
795 * Usage: 43
796 *
797 * @param string Filename/filepath to be analysed
798 * @return array Contains keys [path], [file], [filebody], [fileext], [realFileext]
799 */
800 function split_fileref($fileref) {
801 if ( ereg('(.*/)(.*)$',$fileref,$reg) ) {
802 $info['path'] = $reg[1];
803 $info['file'] = $reg[2];
804 } else {
805 $info['path'] = '';
806 $info['file'] = $fileref;
807 }
808 $reg='';
809 if ( ereg('(.*)\.([^\.]*$)',$info['file'],$reg) ) {
810 $info['filebody'] = $reg[1];
811 $info['fileext'] = strtolower($reg[2]);
812 $info['realFileext'] = $reg[2];
813 } else {
814 $info['filebody'] = $info['file'];
815 $info['fileext'] = '';
816 }
817 reset($info);
818 return $info;
819 }
820
821 /**
822 * Returns the directory part of a path without trailing slash
823 * If there is no dir-part, then an empty string is returned.
824 * Behaviour:
825 *
826 * '/dir1/dir2/script.php' => '/dir1/dir2'
827 * '/dir1/' => '/dir1'
828 * 'dir1/script.php' => 'dir1'
829 * 'd/script.php' => 'd'
830 * '/script.php' => ''
831 * '' => ''
832 * Usage: 5
833 *
834 * @param string Directory name / path
835 * @return string Processed input value. See function description.
836 */
837 function dirname($path) {
838 $p=t3lib_div::revExplode('/',$path,2);
839 return count($p)==2?$p[0]:'';
840 }
841
842 /**
843 * Modifies a HTML Hex color by adding/subtracting $R,$G and $B integers
844 * Usage: 11
845 *
846 * @param string A hexadecimal color code, #xxxxxx
847 * @param integer Offset value 0-255
848 * @param integer Offset value 0-255
849 * @param integer Offset value 0-255
850 * @return string A hexadecimal color code, #xxxxxx, modified according to input vars
851 * @see modifyHTMLColorAll()
852 */
853 function modifyHTMLColor($color,$R,$G,$B) {
854 // This takes a hex-color (# included!) and adds $R, $G and $B to the HTML-color (format: #xxxxxx) and returns the new color
855 $nR = t3lib_div::intInRange(hexdec(substr($color,1,2))+$R,0,255);
856 $nG = t3lib_div::intInRange(hexdec(substr($color,3,2))+$G,0,255);
857 $nB = t3lib_div::intInRange(hexdec(substr($color,5,2))+$B,0,255);
858 return '#'.
859 substr('0'.dechex($nR),-2).
860 substr('0'.dechex($nG),-2).
861 substr('0'.dechex($nB),-2);
862 }
863
864 /**
865 * Modifies a HTML Hex color by adding/subtracting $all integer from all R/G/B channels
866 * Usage: 6
867 *
868 * @param string A hexadecimal color code, #xxxxxx
869 * @param integer Offset value 0-255 for all three channels.
870 * @return string A hexadecimal color code, #xxxxxx, modified according to input vars
871 * @see modifyHTMLColor()
872 */
873 function modifyHTMLColorAll($color,$all) {
874 return t3lib_div::modifyHTMLColor($color,$all,$all,$all);
875 }
876
877 /**
878 * Removes comma (if present) in the end of string
879 * Usage: 2
880 *
881 * @param string String from which the comma in the end (if any) will be removed.
882 * @return string
883 */
884 function rm_endcomma($string) {
885 return ereg_replace(',$','',$string);
886 }
887
888 /**
889 * strtoupper which converts danish (and other characters) characters as well
890 * (DEPRECIATED, use t3lib_cs::conv_case() instead or for HTML output, wrap your content in <span class="uppercase">...</span>)
891 * Usage: 0
892 *
893 * @param string String to process
894 * @return string
895 * @ignore
896 */
897 function danish_strtoupper($string) {
898 $value = strtoupper($string);
899 return strtr($value, 'áéúíâêûôîæøåäöü', 'ÁÉÚÍÄËÜÖÏÆØÅÄÖÜ');
900 }
901
902 /**
903 * Change umlaut characters to plain ASCII with normally two character target
904 * Only known characters will be converted, so don't expect a result for any character.
905 * (DEPRECIATED: Works only for western europe single-byte charsets! Use t3lib_cs::specCharsToASCII() instead!)
906 *
907 * ä => ae, Ö => Oe
908 *
909 * @param string String to convert.
910 * @return string
911 */
912 function convUmlauts($str) {
913 $pat = array ( '/ä/', '/Ä/', '/ö/', '/Ö/', '/ü/', '/Ü/', '/ß/', '/å/', '/Å/', '/ø/', '/Ø/', '/æ/', '/Æ/' );
914 $repl = array ( 'ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss', 'aa', 'AA', 'oe', 'OE', 'ae', 'AE' );
915 return preg_replace($pat,$repl,$str);
916 }
917
918 /**
919 * Tests if the input is an integer.
920 * Usage: 77
921 *
922 * @param mixed Any input variable to test.
923 * @return boolean Returns true if string is an integer
924 */
925 function testInt($var) {
926 return !strcmp($var,intval($var));
927 }
928
929 /**
930 * Returns true if the first part of $str matches the string $partStr
931 * Usage: 59
932 *
933 * @param string Full string to check
934 * @param string Reference string which must be found as the "first part" of the full string
935 * @return boolean True if $partStr was found to be equal to the first part of $str
936 */
937 function isFirstPartOfStr($str,$partStr) {
938 // Returns true, if the first part of a $str equals $partStr and $partStr is not ''
939 $psLen = strlen($partStr);
940 if ($psLen) {
941 return substr($str,0,$psLen)==(string)$partStr;
942 } else return false;
943 }
944
945 /**
946 * Formats the input integer $sizeInBytes as bytes/kilobytes/megabytes (-/K/M)
947 * Usage: 53
948 *
949 * @param integer Number of bytes to format.
950 * @param string Labels for bytes, kilo, mega and giga separated by vertical bar (|) and possibly encapsulated in "". Eg: " | K| M| G" (which is the default value)
951 * @return string Formatted representation of the byte number, for output.
952 */
953 function formatSize($sizeInBytes,$labels='') {
954
955 // Set labels:
956 if (strlen($labels) == 0) {
957 $labels = ' | K| M| G';
958 } else {
959 $labels = str_replace('"','',$labels);
960 }
961 $labelArr = explode('|',$labels);
962
963 // Find size:
964 if ($sizeInBytes>900) {
965 if ($sizeInBytes>900000000) { // GB
966 $val = $sizeInBytes/(1024*1024*1024);
967 return number_format($val, (($val<20)?1:0), '.', '').$labelArr[3];
968 }
969 elseif ($sizeInBytes>900000) { // MB
970 $val = $sizeInBytes/(1024*1024);
971 return number_format($val, (($val<20)?1:0), '.', '').$labelArr[2];
972 } else { // KB
973 $val = $sizeInBytes/(1024);
974 return number_format($val, (($val<20)?1:0), '.', '').$labelArr[1];
975 }
976 } else { // Bytes
977 return $sizeInBytes.$labelArr[0];
978 }
979 }
980
981 /**
982 * Returns microtime input to milliseconds
983 * Usage: 2
984 *
985 * @param string Microtime
986 * @return integer Microtime input string converted to an integer (milliseconds)
987 */
988 function convertMicrotime($microtime) {
989 $parts = explode(' ',$microtime);
990 return round(($parts[0]+$parts[1])*1000);
991 }
992
993 /**
994 * This splits a string by the chars in $operators (typical /+-*) and returns an array with them in
995 * Usage: 2
996 *
997 * @param string Input string, eg "123 + 456 / 789 - 4"
998 * @param string Operators to split by, typically "/+-*"
999 * @return array Array with operators and operands separated.
1000 * @see tslib_cObj::calc(), tslib_gifBuilder::calcOffset()
1001 */
1002 function splitCalc($string,$operators) {
1003 $res = Array();
1004 $sign='+';
1005 while($string) {
1006 $valueLen=strcspn($string,$operators);
1007 $value=substr($string,0,$valueLen);
1008 $res[] = Array($sign,trim($value));
1009 $sign=substr($string,$valueLen,1);
1010 $string=substr($string,$valueLen+1);
1011 }
1012 reset($res);
1013 return $res;
1014 }
1015
1016 /**
1017 * Calculates the input by +,-,*,/,%,^ with priority to + and -
1018 * Usage: 1
1019 *
1020 * @param string Input string, eg "123 + 456 / 789 - 4"
1021 * @return integer Calculated value. Or error string.
1022 * @see calcParenthesis()
1023 */
1024 function calcPriority($string) {
1025 $string=ereg_replace('[[:space:]]*','',$string); // removing all whitespace
1026 $string='+'.$string; // Ensuring an operator for the first entrance
1027 $qm='\*\/\+-^%';
1028 $regex = '(['.$qm.'])(['.$qm.']?[0-9\.]*)';
1029 // split the expression here:
1030 preg_match_all('/'.$regex.'/',$string,$reg);
1031
1032 reset($reg[2]);
1033 $number=0;
1034 $Msign='+';
1035 $err='';
1036 $buffer=doubleval(current($reg[2]));
1037 next($reg[2]); // Advance pointer
1038 while(list($k,$v)=each($reg[2])) {
1039 $v=doubleval($v);
1040 $sign = $reg[1][$k];
1041 if ($sign=='+' || $sign=='-') {
1042 $number = $Msign=='-' ? $number-=$buffer : $number+=$buffer;
1043 $Msign = $sign;
1044 $buffer=$v;
1045 } else {
1046 if ($sign=='/') {if ($v) $buffer/=$v; else $err='dividing by zero';}
1047 if ($sign=='%') {if ($v) $buffer%=$v; else $err='dividing by zero';}
1048 if ($sign=='*') {$buffer*=$v;}
1049 if ($sign=='^') {$buffer=pow($buffer,$v);}
1050 }
1051 }
1052 $number = $Msign=='-' ? $number-=$buffer : $number+=$buffer;
1053 return $err ? 'ERROR: '.$err : $number;
1054 }
1055
1056 /**
1057 * Calculates the input with parenthesis levels
1058 * Usage: 2
1059 *
1060 * @param string Input string, eg "(123 + 456) / 789 - 4"
1061 * @return integer Calculated value. Or error string.
1062 * @see calcPriority(), tslib_cObj::stdWrap()
1063 */
1064 function calcParenthesis($string) {
1065 $securC=100;
1066 do {
1067 $valueLenO=strcspn($string,'(');
1068 $valueLenC=strcspn($string,')');
1069 if ($valueLenC==strlen($string) || $valueLenC < $valueLenO) {
1070 $value = t3lib_div::calcPriority(substr($string,0,$valueLenC));
1071 $string = $value.substr($string,$valueLenC+1);
1072 return $string;
1073 } else {
1074 $string = substr($string,0,$valueLenO).t3lib_div::calcParenthesis(substr($string,$valueLenO+1));
1075 }
1076 // Security:
1077 $securC--;
1078 if ($securC<=0) break;
1079 } while($valueLenO<strlen($string));
1080 return $string;
1081 }
1082
1083 /**
1084 * Inverse version of htmlspecialchars()
1085 * Usage: 4
1086 *
1087 * @param string Value where &gt;, &lt;, &quot; and &amp; should be converted to regular chars.
1088 * @return string Converted result.
1089 */
1090 function htmlspecialchars_decode($value) {
1091 $value = str_replace('&gt;','>',$value);
1092 $value = str_replace('&lt;','<',$value);
1093 $value = str_replace('&quot;','"',$value);
1094 $value = str_replace('&amp;','&',$value);
1095 return $value;
1096 }
1097
1098 /**
1099 * Re-converts HTML entities if they have been converted by htmlspecialchars()
1100 * Usage: 10
1101 *
1102 * @param string String which contains eg. "&amp;amp;" which should stay "&amp;". Or "&amp;#1234;" to "&#1234;". Or "&amp;#x1b;" to "&#x1b;"
1103 * @return string Converted result.
1104 */
1105 function deHSCentities($str) {
1106 return ereg_replace('&amp;([#[:alnum:]]*;)','&\1',$str);
1107 }
1108
1109 /**
1110 * This function is used to escape any ' -characters when transferring text to JavaScript!
1111 * Usage: 3
1112 *
1113 * @param string String to escape
1114 * @param boolean If set, also backslashes are escaped.
1115 * @param string The character to escape, default is ' (single-quote)
1116 * @return string Processed input string
1117 */
1118 function slashJS($string,$extended=0,$char="'") {
1119 if ($extended) {$string = str_replace ("\\", "\\\\", $string);}
1120 return str_replace ($char, "\\".$char, $string);
1121 }
1122
1123 /**
1124 * Version of rawurlencode() where all spaces (%20) are re-converted to space-characters.
1125 * Usefull when passing text to JavaScript where you simply url-encode it to get around problems with syntax-errors, linebreaks etc.
1126 * Usage: 4
1127 *
1128 * @param string String to raw-url-encode with spaces preserved
1129 * @return string Rawurlencoded result of input string, but with all %20 (space chars) converted to real spaces.
1130 */
1131 function rawUrlEncodeJS($str) {
1132 return str_replace('%20',' ',rawurlencode($str));
1133 }
1134
1135 /**
1136 * rawurlencode which preserves "/" chars
1137 * Usefull when filepaths should keep the "/" chars, but have all other special chars encoded.
1138 * Usage: 5
1139 *
1140 * @param string Input string
1141 * @return string Output string
1142 */
1143 function rawUrlEncodeFP($str) {
1144 return str_replace('%2F','/',rawurlencode($str));
1145 }
1146
1147 /**
1148 * Checking syntax of input email address
1149 * Usage: 5
1150 *
1151 * @param string Input string to evaluate
1152 * @return boolean Returns true if the $email address (input string) is valid; Has a "@", domain name with at least one period and only allowed a-z characters.
1153 */
1154 function validEmail($email) {
1155 $email = trim ($email);
1156 if (strstr($email,' ')) return FALSE;
1157 return ereg('^[A-Za-z0-9\._-]+[@][A-Za-z0-9\._-]+[\.].[A-Za-z0-9]+$',$email) ? TRUE : FALSE;
1158 }
1159
1160 /**
1161 * Formats a string for output between <textarea>-tags
1162 * All content outputted in a textarea form should be passed through this function
1163 * Not only is the content htmlspecialchar'ed on output but there is also a single newline added in the top. The newline is necessary because browsers will ignore the first newline after <textarea> if that is the first character. Therefore better set it!
1164 * Usage: 23
1165 *
1166 * @param string Input string to be formatted.
1167 * @return string Formatted for <textarea>-tags
1168 */
1169 function formatForTextarea($content) {
1170 return chr(10).htmlspecialchars($content);
1171 }
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184 /*************************
1185 *
1186 * ARRAY FUNCTIONS
1187 *
1188 *************************/
1189
1190 /**
1191 * Check if an item exists in an array
1192 * Please note that the order of parameters is reverse compared to the php4-function in_array()!!!
1193 * Usage: 3
1194 *
1195 * @param array $in_array one-dimensional array of items
1196 * @param string $item item to check for
1197 * @return boolean true if $item is in the one-dimensional array $in_array
1198 * @internal
1199 */
1200 function inArray($in_array,$item) {
1201 if (is_array($in_array)) {
1202 while (list(,$val)=each($in_array)) {
1203 if (!is_array($val) && !strcmp($val,$item)) return true;
1204 }
1205 }
1206 }
1207
1208 /**
1209 * Explodes a $string delimited by $delim and passes each item in the array through intval().
1210 * Corresponds to explode(), but with conversion to integers for all values.
1211 * Usage: 76
1212 *
1213 * @param string Delimiter string to explode with
1214 * @param string The string to explode
1215 * @return array Exploded values, all converted to integers
1216 */
1217 function intExplode($delim, $string) {
1218 $temp = explode($delim,$string);
1219 while(list($key,$val)=each($temp)) {
1220 $temp[$key]=intval($val);
1221 }
1222 reset($temp);
1223 return $temp;
1224 }
1225
1226 /**
1227 * Reverse explode which explodes the string counting from behind.
1228 * Thus t3lib_div::revExplode(':','my:words:here',2) will return array('my:words','here')
1229 * Usage: 8
1230 *
1231 * @param string Delimiter string to explode with
1232 * @param string The string to explode
1233 * @param integer Number of array entries
1234 * @return array Exploded values
1235 */
1236 function revExplode($delim, $string, $count=0) {
1237 $temp = explode($delim,strrev($string),$count);
1238 while(list($key,$val)=each($temp)) {
1239 $temp[$key]=strrev($val);
1240 }
1241 $temp=array_reverse($temp);
1242 reset($temp);
1243 return $temp;
1244 }
1245
1246 /**
1247 * Explodes a string and trims all values for whitespace in the ends.
1248 * If $onlyNonEmptyValues is set, then all blank ('') values are removed.
1249 * Usage: 256
1250 *
1251 * @param string Delimiter string to explode with
1252 * @param string The string to explode
1253 * @param boolean If set, all empty values (='') will NOT be set in output
1254 * @return array Exploded values
1255 */
1256 function trimExplode($delim, $string, $onlyNonEmptyValues=0) {
1257 $temp = explode($delim,$string);
1258 $newtemp=array();
1259 while(list($key,$val)=each($temp)) {
1260 if (!$onlyNonEmptyValues || strcmp('',trim($val))) {
1261 $newtemp[]=trim($val);
1262 }
1263 }
1264 reset($newtemp);
1265 return $newtemp;
1266 }
1267
1268 /**
1269 * Remove duplicate values from an array
1270 * This function is deprecated, use the PHP function array_unique instead
1271 * Usage: 0
1272 *
1273 * @param array Array of values to make unique
1274 * @return array
1275 * @ignore
1276 * @deprecated
1277 */
1278 function uniqueArray($valueArray) {
1279 return array_unique($valueArray);
1280 }
1281
1282 /**
1283 * Removes the value $cmpValue from the $array if found there. Returns the modified array
1284 * Usage: 3
1285 *
1286 * @param array Array containing the values
1287 * @param string Value to search for and if found remove array entry where found.
1288 * @return array Output array with entries removed if search string is found
1289 */
1290 function removeArrayEntryByValue($array,$cmpValue) {
1291 if (is_array($array)) {
1292 reset($array);
1293 while(list($k,$v)=each($array)) {
1294 if (is_array($v)) {
1295 $array[$k] = t3lib_div::removeArrayEntryByValue($v,$cmpValue);
1296 } else {
1297 if (!strcmp($v,$cmpValue)) {
1298 unset($array[$k]);
1299 }
1300 }
1301 }
1302 }
1303 reset($array);
1304 return $array;
1305 }
1306
1307 /**
1308 * Implodes a multidim-array into GET-parameters (eg. &param[key][key2]=value2&param[key][key3]=value3)
1309 * Usage: 24
1310 *
1311 * @param string Name prefix for entries. Set to blank if you wish none.
1312 * @param array The (multidim) array to implode
1313 * @param string (keep blank)
1314 * @param boolean If set, parameters which were blank strings would be removed.
1315 * @param boolean If set, the param name itself (for example "param[key][key2]") would be rawurlencoded as well.
1316 * @return string Imploded result, fx. &param[key][key2]=value2&param[key][key3]=value3
1317 * @see explodeUrl2Array()
1318 */
1319 function implodeArrayForUrl($name,$theArray,$str='',$skipBlank=0,$rawurlencodeParamName=0) {
1320 if (is_array($theArray)) {
1321 foreach($theArray as $Akey => $AVal) {
1322 $thisKeyName = $name ? $name.'['.$Akey.']' : $Akey;
1323 if (is_array($AVal)) {
1324 $str = t3lib_div::implodeArrayForUrl($thisKeyName,$AVal,$str,$skipBlank,$rawurlencodeParamName);
1325 } else {
1326 if (!$skipBlank || strcmp($AVal,'')) {
1327 $str.='&'.($rawurlencodeParamName ? rawurlencode($thisKeyName) : $thisKeyName).
1328 '='.rawurlencode($AVal);
1329 }
1330 }
1331 }
1332 }
1333 return $str;
1334 }
1335
1336 /**
1337 * Explodes a string with GETvars (eg. "&id=1&type=2&ext[mykey]=3") into an array
1338 *
1339 * @param string GETvars string
1340 * @param boolean If set, the string will be parsed into a multidimensional array if square brackets are used in variable names (using PHP function parse_str())
1341 * @return array Array of values. All values AND keys are rawurldecoded() as they properly should be. But this means that any implosion of the array again must rawurlencode it!
1342 * @see implodeArrayForUrl()
1343 */
1344 function explodeUrl2Array($string,$multidim=FALSE) {
1345 if ($multidim) {
1346 parse_str($string,$tempGetVars);
1347 return $tempGetVars;
1348 } else {
1349 $output = array();
1350 $p = explode('&',$string);
1351 foreach($p as $v) {
1352 if (strlen($v)) {
1353 list($pK,$pV) = explode('=',$v,2);
1354 $output[rawurldecode($pK)] = rawurldecode($pV);
1355 }
1356 }
1357 return $output;
1358 }
1359 }
1360
1361 /**
1362 * Returns an array with selected keys from incoming data.
1363 * (Better read source code if you want to find out...)
1364 * Usage: 3
1365 *
1366 * @param string List of variable/key names
1367 * @param array Array from where to get values based on the keys in $varList
1368 * @param boolean If set, then t3lib_div::_GP() is used to fetch the value if not found (isset) in the $getArray
1369 * @return array Output array with selected variables.
1370 */
1371 function compileSelectedGetVarsFromArray($varList,$getArray,$GPvarAlt=1) {
1372 $keys = t3lib_div::trimExplode(',',$varList,1);
1373 $outArr=array();
1374 foreach($keys as $v) {
1375 if (isset($getArray[$v])) {
1376 $outArr[$v]=$getArray[$v];
1377 } elseif ($GPvarAlt) {
1378 $outArr[$v]=t3lib_div::_GP($v);
1379 }
1380 }
1381 return $outArr;
1382 }
1383
1384 /**
1385 * AddSlash array
1386 * This function traverses a multidimentional array and adds slashes to the values.
1387 * NOTE that the input array is and argument by reference.!!
1388 * Twin-function to stripSlashesOnArray
1389 * Usage: 8
1390 *
1391 * @param array Multidimensional input array, (REFERENCE!)
1392 * @return array
1393 */
1394 function addSlashesOnArray(&$theArray) {
1395 if (is_array($theArray)) {
1396 reset($theArray);
1397 while(list($Akey,$AVal)=each($theArray)) {
1398 if (is_array($AVal)) {
1399 t3lib_div::addSlashesOnArray($theArray[$Akey]);
1400 } else {
1401 $theArray[$Akey] = addslashes($AVal);
1402 }
1403 }
1404 reset($theArray);
1405 }
1406 }
1407
1408 /**
1409 * StripSlash array
1410 * This function traverses a multidimentional array and strips slashes to the values.
1411 * NOTE that the input array is and argument by reference.!!
1412 * Twin-function to addSlashesOnArray
1413 * Usage: 10
1414 *
1415 * @param array Multidimensional input array, (REFERENCE!)
1416 * @return array
1417 */
1418 function stripSlashesOnArray(&$theArray) {
1419 if (is_array($theArray)) {
1420 reset($theArray);
1421 while(list($Akey,$AVal)=each($theArray)) {
1422 if (is_array($AVal)) {
1423 t3lib_div::stripSlashesOnArray($theArray[$Akey]);
1424 } else {
1425 $theArray[$Akey] = stripslashes($AVal);
1426 }
1427 }
1428 reset($theArray);
1429 }
1430 }
1431
1432 /**
1433 * Either slashes ($cmd=add) or strips ($cmd=strip) array $arr depending on $cmd
1434 * Usage: 0
1435 *
1436 * @param array Multidimensional input array
1437 * @param string "add" or "strip", depending on usage you wish.
1438 * @return array
1439 */
1440 function slashArray($arr,$cmd) {
1441 if ($cmd=='strip') t3lib_div::stripSlashesOnArray($arr);
1442 if ($cmd=='add') t3lib_div::addSlashesOnArray($arr);
1443 return $arr;
1444 }
1445
1446 /**
1447 * Merges two arrays recursively and "binary safe" (integer keys are overridden as well), overruling similar values in the first array ($arr0) with the values of the second array ($arr1)
1448 * In case of identical keys, ie. keeping the values of the second.
1449 * Usage: 0
1450 *
1451 * @param array First array
1452 * @param array Second array, overruling the first array
1453 * @param boolean If set, keys that are NOT found in $arr0 (first array) will not be set. Thus only existing value can/will be overruled from second array.
1454 * @param boolean If set, values from $arr1 will overrule if they are empty. Default: true
1455 * @return array Resulting array where $arr1 values has overruled $arr0 values
1456 */
1457 function array_merge_recursive_overrule($arr0,$arr1,$notAddKeys=0,$includeEmtpyValues=true) {
1458 reset($arr1);
1459 while(list($key,$val) = each($arr1)) {
1460 if(is_array($arr0[$key])) {
1461 if (is_array($arr1[$key])) {
1462 $arr0[$key] = t3lib_div::array_merge_recursive_overrule($arr0[$key],$arr1[$key],$notAddKeys);
1463 }
1464 } else {
1465 if ($notAddKeys) {
1466 if (isset($arr0[$key])) {
1467 if ($includeEmtpyValues OR $val) {
1468 $arr0[$key] = $val;
1469 }
1470 }
1471 } else {
1472 if ($includeEmtpyValues OR $val) {
1473 $arr0[$key] = $val;
1474 }
1475 }
1476 }
1477 }
1478 reset($arr0);
1479 return $arr0;
1480 }
1481
1482 /**
1483 * An array_merge function where the keys are NOT renumbered as they happen to be with the real php-array_merge function. It is "binary safe" in the sense that integer keys are overridden as well.
1484 * Usage: 16
1485 *
1486 * @param array First array
1487 * @param array Second array
1488 * @return array Merged result.
1489 */
1490 function array_merge($arr1,$arr2) {
1491 return $arr2+$arr1;
1492 }
1493
1494 /**
1495 * Takes a row and returns a CSV string of the values with $delim (default is ,) and $quote (default is ") as separator chars.
1496 * Usage: 5
1497 *
1498 * @param array Input array of values
1499 * @param string Delimited, default is comman
1500 * @param string Quote-character to wrap around the values.
1501 * @return string A single line of CSV
1502 */
1503 function csvValues($row,$delim=',',$quote='"') {
1504 reset($row);
1505 $out=array();
1506 while(list(,$value)=each($row)) {
1507 list($valPart) = explode(chr(10),$value);
1508 $valPart = trim($valPart);
1509 $out[]=str_replace($quote,$quote.$quote,$valPart);
1510 }
1511 $str = $quote.implode($quote.$delim.$quote,$out).$quote;
1512 return $str;
1513 }
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530 /*************************
1531 *
1532 * HTML/XML PROCESSING
1533 *
1534 *************************/
1535
1536 /**
1537 * Returns an array with all attributes of the input HTML tag as key/value pairs. Attributes are only lowercase a-z
1538 * $tag is either a whole tag (eg '<TAG OPTION ATTRIB=VALUE>') or the parameterlist (ex ' OPTION ATTRIB=VALUE>')
1539 * If a attribute is empty (I call it 'an option'), then the value for the key is empty. You can check if it existed with isset()
1540 * Usage: 8
1541 *
1542 * @param string HTML-tag string (or attributes only)
1543 * @return array Array with the attribute values.
1544 */
1545 function get_tag_attributes($tag) {
1546 $components = t3lib_div::split_tag_attributes($tag);
1547 $name = ''; // attribute name is stored here
1548 $valuemode = '';
1549 if (is_array($components)) {
1550 while (list($key,$val) = each ($components)) {
1551 if ($val != '=') { // Only if $name is set (if there is an attribute, that waits for a value), that valuemode is enabled. This ensures that the attribute is assigned it's value
1552 if ($valuemode) {
1553 if ($name) {
1554 $attributes[$name] = $val;
1555 $name = '';
1556 }
1557 } else {
1558 if ($key = strtolower(ereg_replace('[^a-zA-Z0-9]','',$val))) {
1559 $attributes[$key] = '';
1560 $name = $key;
1561 }
1562 }
1563 $valuemode = '';
1564 } else {
1565 $valuemode = 'on';
1566 }
1567 }
1568 if (is_array($attributes)) reset($attributes);
1569 return $attributes;
1570 }
1571 }
1572
1573 /**
1574 * Returns an array with the 'components' from an attribute list from an HTML tag. The result is normally analyzed by get_tag_attributes
1575 * Removes tag-name if found
1576 * Usage: 2
1577 *
1578 * @param string HTML-tag string (or attributes only)
1579 * @return array Array with the attribute values.
1580 * @internal
1581 */
1582 function split_tag_attributes($tag) {
1583 $tag_tmp = trim(eregi_replace ('^<[^[:space:]]*','',trim($tag)));
1584 // Removes any > in the end of the string
1585 $tag_tmp = trim(eregi_replace ('>$','',$tag_tmp));
1586
1587 while (strcmp($tag_tmp,'')) { // Compared with empty string instead , 030102
1588 $firstChar=substr($tag_tmp,0,1);
1589 if (!strcmp($firstChar,'"') || !strcmp($firstChar,"'")) {
1590 $reg=explode($firstChar,$tag_tmp,3);
1591 $value[]=$reg[1];
1592 $tag_tmp=trim($reg[2]);
1593 } elseif (!strcmp($firstChar,'=')) {
1594 $value[] = '=';
1595 $tag_tmp = trim(substr($tag_tmp,1)); // Removes = chars.
1596 } else {
1597 // There are '' around the value. We look for the next ' ' or '>'
1598 $reg = split('[[:space:]=]',$tag_tmp,2);
1599 $value[] = trim($reg[0]);
1600 $tag_tmp = trim(substr($tag_tmp,strlen($reg[0]),1).$reg[1]);
1601 }
1602 }
1603 if (is_array($value)) reset($value);
1604 return $value;
1605 }
1606
1607 /**
1608 * Implodes attributes in the array $arr for an attribute list in eg. and HTML tag (with quotes)
1609 * Usage: 14
1610 *
1611 * @param array Array with attribute key/value pairs, eg. "bgcolor"=>"red", "border"=>0
1612 * @param boolean If set the resulting attribute list will have a) all attributes in lowercase (and duplicates weeded out, first entry taking precedence) and b) all values htmlspecialchar()'ed. It is recommended to use this switch!
1613 * @param boolean If true, don't check if values are blank. Default is to omit attributes with blank values.
1614 * @return string Imploded attributes, eg. 'bgcolor="red" border="0"'
1615 */
1616 function implodeAttributes($arr,$xhtmlSafe=FALSE,$dontOmitBlankAttribs=FALSE) {
1617 if (is_array($arr)) {
1618 if ($xhtmlSafe) {
1619 $newArr=array();
1620 foreach($arr as $p => $v) {
1621 if (!isset($newArr[strtolower($p)])) $newArr[strtolower($p)] = htmlspecialchars($v);
1622 }
1623 $arr = $newArr;
1624 }
1625 $list = array();
1626 foreach($arr as $p => $v) {
1627 if (strcmp($v,'') || $dontOmitBlankAttribs) {$list[]=$p.'="'.$v.'"';}
1628 }
1629 return implode(' ',$list);
1630 }
1631 }
1632
1633 /**
1634 * Implodes attributes in the array $arr for an attribute list in eg. and HTML tag (with quotes)
1635 *
1636 * @param array See implodeAttributes()
1637 * @param boolean See implodeAttributes()
1638 * @param boolean See implodeAttributes()
1639 * @return string See implodeAttributes()
1640 * @deprecated Name was changed into implodeAttributes
1641 * @see implodeAttributes()
1642 */
1643 function implodeParams($arr,$xhtmlSafe=FALSE,$dontOmitBlankAttribs=FALSE) {
1644 return t3lib_div::implodeAttributes($arr,$xhtmlSafe,$dontOmitBlankAttribs);
1645 }
1646
1647 /**
1648 * Wraps JavaScript code XHTML ready with <script>-tags
1649 * Automatic re-identing of the JS code is done by using the first line as ident reference.
1650 * This is nice for identing JS code with PHP code on the same level.
1651 *
1652 * @param string JavaScript code
1653 * @param boolean Wrap script element in linebreaks? Default is TRUE.
1654 * @return string The wrapped JS code, ready to put into a XHTML page
1655 * @author Ingmar Schlecht <ingmars@web.de>
1656 * @author René Fritz <r.fritz@colorcube.de>
1657 */
1658 function wrapJS($string, $linebreak=TRUE) {
1659 if(trim($string)) {
1660 // <script wrapped in nl?
1661 $cr = $linebreak? "\n" : '';
1662
1663 // remove nl from the beginning
1664 $string = preg_replace ('/^\n+/', '', $string);
1665 // re-ident to one tab using the first line as reference
1666 if(preg_match('/^(\t+)/',$string,$match)) {
1667 $string = str_replace($match[1],"\t", $string);
1668 }
1669 $string = $cr.'<script type="text/javascript">
1670 /*<![CDATA[*/
1671 '.$string.'
1672 /*]]>*/
1673 </script>'.$cr;
1674 }
1675 return trim($string);
1676 }
1677
1678
1679 /**
1680 * Parses XML input into a PHP array with associative keys
1681 * Usage: 0
1682 *
1683 * @param string XML data input
1684 * @param integer Number of element levels to resolve the XML into an array. Any further structure will be set as XML.
1685 * @return mixed The array with the parsed structure unless the XML parser returns with an error in which case the error message string is returned.
1686 * @author bisqwit at iki dot fi dot not dot for dot ads dot invalid / http://dk.php.net/xml_parse_into_struct + kasperYYYY@typo3.com
1687 */
1688 function xml2tree($string,$depth=999) {
1689 $parser = xml_parser_create();
1690 $vals = array();
1691 $index = array();
1692
1693 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1694 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
1695 xml_parse_into_struct($parser, $string, $vals, $index);
1696
1697 if (xml_get_error_code($parser)) return 'Line '.xml_get_current_line_number($parser).': '.xml_error_string(xml_get_error_code($parser));
1698 xml_parser_free($parser);
1699
1700 $stack = array( array() );
1701 $stacktop = 0;
1702 $startPoint=0;
1703
1704 unset($tagi);
1705 foreach($vals as $key => $val) {
1706 $type = $val['type'];
1707
1708 // open tag:
1709 if ($type=='open' || $type=='complete') {
1710 $stack[$stacktop++] = $tagi;
1711
1712 if ($depth==$stacktop) {
1713 $startPoint=$key;
1714 }
1715
1716 $tagi = array('tag' => $val['tag']);
1717
1718 if(isset($val['attributes'])) $tagi['attrs'] = $val['attributes'];
1719 if(isset($val['value'])) $tagi['values'][] = $val['value'];
1720 }
1721 // finish tag:
1722 if ($type=='complete' || $type=='close') {
1723 $oldtagi = $tagi;
1724 $tagi = $stack[--$stacktop];
1725 $oldtag = $oldtagi['tag'];
1726 unset($oldtagi['tag']);
1727
1728 if ($depth==($stacktop+1)) {
1729 if ($key-$startPoint > 0) {
1730 $partArray = array_slice(
1731 $vals,
1732 $startPoint+1,
1733 $key-$startPoint-1
1734 );
1735 #$oldtagi=array('XMLvalue'=>t3lib_div::xmlRecompileFromStructValArray($partArray));
1736 $oldtagi['XMLvalue']=t3lib_div::xmlRecompileFromStructValArray($partArray);
1737 } else {
1738 $oldtagi['XMLvalue']=$oldtagi['values'][0];
1739 }
1740 }
1741
1742 $tagi['ch'][$oldtag][] = $oldtagi;
1743 unset($oldtagi);
1744 }
1745 // cdata
1746 if($type=='cdata') {
1747 $tagi['values'][] = $val['value'];
1748 }
1749 }
1750 return $tagi['ch'];
1751 }
1752
1753 /**
1754 * Converts a PHP array into an XML string.
1755 * The XML output is optimized for readability since associative keys are used as tagnames.
1756 * This also means that only alphanumeric characters are allowed in the tag names AND only keys NOT starting with numbers (so watch your usage of keys!). However there are options you can set to avoid this problem.
1757 * Numeric keys are stored with the default tagname "numIndex" but can be overridden to other formats)
1758 * The function handles input values from the PHP array in a binary-safe way; All characters below 32 (except 9,10,13) will trigger the content to be converted to a base64-string
1759 * The PHP variable type of the data IS preserved as long as the types are strings, arrays, integers and booleans. Strings are the default type unless the "type" attribute is set.
1760 * The output XML has been tested with the PHP XML-parser and parses OK under all tested circumstances.
1761 * However using MSIE to read the XML output didn't always go well: One reason could be that the character encoding is not observed in the PHP data. The other reason may be if the tag-names are invalid in the eyes of MSIE. Also using the namespace feature will make MSIE break parsing. There might be more reasons...
1762 * Usage: 5
1763 *
1764 * @param array The input PHP array with any kind of data; text, binary, integers. Not objects though.
1765 * @param string tag-prefix, eg. a namespace prefix like "T3:"
1766 * @param integer Current recursion level. Don't change, stay at zero!
1767 * @param string Alternative document tag. Default is "phparray".
1768 * @param integer If set, the number of spaces corresponding to this number is used for indenting, otherwise a single chr(9) (TAB) is used
1769 * @param array Options for the compilation. Key "useNindex" => 0/1 (boolean: whether to use "n0, n1, n2" for num. indexes); Key "useIndexTagForNum" => "[tag for numerical indexes]"; Key "useIndexTagForAssoc" => "[tag for associative indexes"; Key "parentTagMap" => array('parentTag' => 'thisLevelTag')
1770 * @param string Stack data. Don't touch.
1771 * @return string An XML string made from the input content in the array.
1772 * @see xml2array()
1773 */
1774 function array2xml($array,$NSprefix='',$level=0,$docTag='phparray',$spaceInd=0, $options=array(),$stackData=array()) {
1775 // The list of byte values which will trigger binary-safe storage. If any value has one of these char values in it, it will be encoded in base64
1776 $binaryChars = chr(0).chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8).
1777 chr(11).chr(12).chr(14).chr(15).chr(16).chr(17).chr(18).chr(19).
1778 chr(20).chr(21).chr(22).chr(23).chr(24).chr(25).chr(26).chr(27).chr(28).chr(29).
1779 chr(30).chr(31);
1780 // Set indenting mode:
1781 $indentChar = $spaceInd ? ' ' : chr(9);
1782 $indentN = $spaceInd>0 ? $spaceInd : 1;
1783
1784 // Init output variable:
1785 $output='';
1786
1787 // Traverse the input array
1788 if (is_array($array)) {
1789 foreach($array as $k=>$v) {
1790 $attr = '';
1791 $tagName = $k;
1792
1793 // Construct the tag name.
1794 if(isset($options['grandParentTagMap'][$stackData['grandParentTagName'].'/'.$stackData['parentTagName']])) { // Use tag based on grand-parent + parent tag name
1795 $attr.=' index="'.htmlspecialchars($tagName).'"';
1796 $tagName = (string)$options['grandParentTagMap'][$stackData['grandParentTagName'].'/'.$stackData['parentTagName']];
1797 }elseif(isset($options['parentTagMap'][$stackData['parentTagName'].':'.$tagName])) { // Use tag based on parent tag name + current tag
1798 $attr.=' index="'.htmlspecialchars($tagName).'"';
1799 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName'].':'.$tagName];
1800 } elseif(isset($options['parentTagMap'][$stackData['parentTagName']])) { // Use tag based on parent tag name:
1801 $attr.=' index="'.htmlspecialchars($tagName).'"';
1802 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName']];
1803 } elseif (!strcmp(intval($tagName),$tagName)) { // If integer...;
1804 if ($options['useNindex']) { // If numeric key, prefix "n"
1805 $tagName = 'n'.$tagName;
1806 } else { // Use special tag for num. keys:
1807 $attr.=' index="'.$tagName.'"';
1808 $tagName = $options['useIndexTagForNum'] ? $options['useIndexTagForNum'] : 'numIndex';
1809 }
1810 } elseif($options['useIndexTagForAssoc']) { // Use tag for all associative keys:
1811 $attr.=' index="'.htmlspecialchars($tagName).'"';
1812 $tagName = $options['useIndexTagForAssoc'];
1813 }
1814
1815 // The tag name is cleaned up so only alphanumeric chars (plus - and _) are in there and not longer than 100 chars either.
1816 $tagName = substr(ereg_replace('[^[:alnum:]_-]','',$tagName),0,100);
1817
1818 // If the value is an array then we will call this function recursively:
1819 if (is_array($v)) {
1820
1821 // Sub elements:
1822 if ($options['alt_options'][$stackData['path'].'/'.$tagName]) {
1823 $subOptions = $options['alt_options'][$stackData['path'].'/'.$tagName];
1824 $clearStackPath = $subOptions['clearStackPath'];
1825 } else {
1826 $subOptions = $options;
1827 $clearStackPath = FALSE;
1828 }
1829
1830 $content = chr(10).
1831 t3lib_div::array2xml(
1832 $v,
1833 $NSprefix,
1834 $level+1,
1835 '',
1836 $spaceInd,
1837 $subOptions,
1838 array(
1839 'parentTagName' => $tagName,
1840 'grandParentTagName' => $stackData['parentTagName'],
1841 'path' => $clearStackPath ? '' : $stackData['path'].'/'.$tagName,
1842 )
1843 ).
1844 str_pad('',($level+1)*$indentN,$indentChar);
1845 $attr.=' type="array"';
1846 } else { // Just a value:
1847
1848 // Look for binary chars:
1849 if (strcspn($v,$binaryChars) != strlen($v)) { // Go for base64 encoding if the initial segment NOT matching any binary char has the same length as the whole string!
1850 // If the value contained binary chars then we base64-encode it an set an attribute to notify this situation:
1851 $content = chr(10).chunk_split(base64_encode($v));
1852 $attr.=' base64="1"';
1853 } else {
1854 // Otherwise, just htmlspecialchar the stuff:
1855 $content = htmlspecialchars($v);
1856 $dType = gettype($v);
1857 if ($dType!='string' && !$options['disableTypeAttrib']) { $attr.=' type="'.$dType.'"'; }
1858 }
1859 }
1860
1861 // Add the element to the output string:
1862 $output.=str_pad('',($level+1)*$indentN,$indentChar).'<'.$NSprefix.$tagName.$attr.'>'.$content.'</'.$NSprefix.$tagName.'>'.chr(10);
1863 }
1864 }
1865
1866 // If we are at the outer-most level, then we finally wrap it all in the document tags and return that as the value:
1867 if (!$level) {
1868 $output =
1869 '<'.$docTag.'>'.chr(10).
1870 $output.
1871 '</'.$docTag.'>';
1872 }
1873
1874 return $output;
1875 }
1876
1877 /**
1878 * Converts an XML string to a PHP array.
1879 * This is the reverse function of array2xml()
1880 * Usage: 17
1881 *
1882 * @param string XML content to convert into an array
1883 * @param string The tag-prefix resolve, eg. a namespace like "T3:"
1884 * @param boolean If set, the document tag will be set in the key "_DOCUMENT_TAG" of the output array
1885 * @return mixed If the parsing had errors, a string with the error message is returned. Otherwise an array with the content.
1886 * @see array2xml()
1887 */
1888 function xml2array($string,$NSprefix='',$reportDocTag=FALSE) {
1889 global $TYPO3_CONF_VARS;
1890
1891 // Create parser:
1892 $parser = xml_parser_create();
1893 $vals = array();
1894 $index = array();
1895
1896 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1897 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
1898
1899 // PHP5 fix of charset awareness:
1900 // Problem is: PHP5 apparently detects the charset of the XML file (or defaults to utf-8) and will AUTOMATICALLY convert the content to either utf-8, iso-8859-1 or us-ascii. PHP4 just passed the content through without taking action regarding the charset.
1901 // In TYPO3 we expect that the charset of XML content is NOT handled in the parser but internally in TYPO3 instead. Therefore it would be very nice if PHP5 could be configured to NOT process the charset of the files. But this is not possible for now.
1902 // What we do here fixes the problem but ONLY if the charset is utf-8, iso-8859-1 or us-ascii. That should work for most TYPO3 installations, in particular if people use utf-8 which we highly recommend.
1903 if ((double)phpversion()>=5) {
1904 unset($ereg_result);
1905 ereg('^[[:space:]]*<\?xml[^>]*encoding[[:space:]]*=[[:space:]]*"([^"]*)"',substr($string,0,200),$ereg_result);
1906 $theCharset = $ereg_result[1] ? $ereg_result[1] : ($TYPO3_CONF_VARS['BE']['forceCharset'] ? $TYPO3_CONF_VARS['BE']['forceCharset'] : 'iso-8859-1');
1907 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $theCharset); // us-ascii / utf-8 / iso-8859-1
1908 }
1909
1910 // Parse content:
1911 xml_parse_into_struct($parser, $string, $vals, $index);
1912
1913 // If error, return error message:
1914 if (xml_get_error_code($parser)) {
1915 return 'Line '.xml_get_current_line_number($parser).': '.xml_error_string(xml_get_error_code($parser));
1916 }
1917 xml_parser_free($parser);
1918
1919 // Init vars:
1920 $stack = array(array());
1921 $stacktop = 0;
1922 $current=array();
1923 $tagName = '';
1924 $documentTag = '';
1925
1926 // Traverse the parsed XML structure:
1927 foreach($vals as $key => $val) {
1928
1929 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
1930 $tagName = $val['tag'];
1931 if (!$documentTag) $documentTag = $tagName;
1932
1933 // Test for name space:
1934 $tagName = ($NSprefix && substr($tagName,0,strlen($NSprefix))==$NSprefix) ? substr($tagName,strlen($NSprefix)) : $tagName;
1935
1936 // Test for numeric tag, encoded on the form "nXXX":
1937 $testNtag = substr($tagName,1); // Closing tag.
1938 $tagName = (substr($tagName,0,1)=='n' && !strcmp(intval($testNtag),$testNtag)) ? intval($testNtag) : $tagName;
1939
1940 // Test for alternative index value:
1941 if (strlen($val['attributes']['index'])) { $tagName = $val['attributes']['index']; }
1942
1943 // Setting tag-values, manage stack:
1944 switch($val['type']) {
1945 case 'open': // If open tag it means there is an array stored in sub-elements. Therefore increase the stackpointer and reset the accumulation array:
1946 $current[$tagName] = array(); // Setting blank place holder
1947 $stack[$stacktop++] = $current;
1948 $current = array();
1949 break;
1950 case 'close': // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
1951 $oldCurrent = $current;
1952 $current = $stack[--$stacktop];
1953 end($current); // Going to the end of array to get placeholder key, key($current), and fill in array next:
1954 $current[key($current)] = $oldCurrent;
1955 unset($oldCurrent);
1956 break;
1957 case 'complete': // If "complete", then it's a value. If the attribute "base64" is set, then decode the value, otherwise just set it.
1958 if ($val['attributes']['base64']) {
1959 $current[$tagName] = base64_decode($val['value']);
1960 } else {
1961 $current[$tagName] = (string)$val['value']; // Had to cast it as a string - otherwise it would be evaluate false if tested with isset()!!
1962
1963 // Cast type:
1964 switch((string)$val['attributes']['type']) {
1965 case 'integer':
1966 $current[$tagName] = (integer)$current[$tagName];
1967 break;
1968 case 'double':
1969 $current[$tagName] = (double)$current[$tagName];
1970 break;
1971 case 'boolean':
1972 $current[$tagName] = (bool)$current[$tagName];
1973 break;
1974 case 'array':
1975 $current[$tagName] = array(); // MUST be an empty array since it is processed as a value; Empty arrays would end up here because they would have no tags inside...
1976 break;
1977 }
1978 }
1979 break;
1980 }
1981 }
1982
1983 if ($reportDocTag) {
1984 $current[$tagName]['_DOCUMENT_TAG'] = $documentTag;
1985 }
1986
1987 // Finally return the content of the document tag.
1988 return $current[$tagName];
1989 }
1990
1991 /**
1992 * This implodes an array of XML parts (made with xml_parse_into_struct()) into XML again.
1993 * Usage: 2
1994 *
1995 * @param array A array of XML parts, see xml2tree
1996 * @return string Re-compiled XML data.
1997 */
1998 function xmlRecompileFromStructValArray($vals) {
1999 $XMLcontent='';
2000
2001 foreach($vals as $val) {
2002 $type = $val['type'];
2003
2004 // open tag:
2005 if ($type=='open' || $type=='complete') {
2006 $XMLcontent.='<'.$val['tag'];
2007 if(isset($val['attributes'])) {
2008 foreach($val['attributes'] as $k => $v) {
2009 $XMLcontent.=' '.$k.'="'.htmlspecialchars($v).'"';
2010 }
2011 }
2012 if ($type=='complete') {
2013 if(isset($val['value'])) {
2014 $XMLcontent.='>'.htmlspecialchars($val['value']).'</'.$val['tag'].'>';
2015 } else $XMLcontent.='/>';
2016 } else $XMLcontent.='>';
2017
2018 if ($type=='open' && isset($val['value'])) {
2019 $XMLcontent.=htmlspecialchars($val['value']);
2020 }
2021 }
2022 // finish tag:
2023 if ($type=='close') {
2024 $XMLcontent.='</'.$val['tag'].'>';
2025 }
2026 // cdata
2027 if($type=='cdata') {
2028 $XMLcontent.=htmlspecialchars($val['value']);
2029 }
2030 }
2031
2032 return $XMLcontent;
2033 }
2034
2035 /**
2036 * Extract the encoding scheme as found in the first line of an XML document (typically)
2037 * Usage: 1
2038 *
2039 * @param string XML data
2040 * @return string Encoding scheme (lowercase), if found.
2041 */
2042 function xmlGetHeaderAttribs($xmlData) {
2043 $xmlHeader = substr(trim($xmlData),0,200);
2044 $reg=array();
2045 if (eregi('^<\?xml([^>]*)\?\>',$xmlHeader,$reg)) {
2046 return t3lib_div::get_tag_attributes($reg[1]);
2047 }
2048 }
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060 /*************************
2061 *
2062 * FILES FUNCTIONS
2063 *
2064 *************************/
2065
2066 /**
2067 * Reads the file or url $url and returns the content
2068 * If you are having trouble with proxys when reading URLs you can configure your way out of that with settings like $TYPO3_CONF_VARS['SYS']['curlUse'] etc.
2069 * Usage: 83
2070 *
2071 * @param string Filepath/URL to read
2072 * @return string The content from the resource given as input.
2073 */
2074 function getURL($url) {
2075 $content = '';
2076
2077 // (Proxy support implemented by Arco <arco@appeltaart.mine.nu>)
2078 if((substr($url,0,7)=='http://') && ($GLOBALS['TYPO3_CONF_VARS']['SYS']['curlUse']=='1')) {
2079 //external URL without error checking.
2080 $ch = curl_init();
2081 curl_setopt ($ch,CURLOPT_URL, $url);
2082 curl_setopt ($ch,CURLOPT_HEADER, 0);
2083 curl_setopt ($ch,CURLOPT_RETURNTRANSFER, 1);
2084
2085 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['curlProxyServer']) {
2086 curl_setopt ($ch, CURLOPT_PROXY, $GLOBALS['TYPO3_CONF_VARS']['SYS']['curlProxyServer']);
2087
2088 // I don't know if it will be needed
2089 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['curlProxyTunnel']) {
2090 curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, $GLOBALS['TYPO3_CONF_VARS']['SYS']['curlProxyTunnel'] );
2091 }
2092 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['curlProxyUserPass']) {
2093 curl_setopt ($ch, CURLOPT_PROXYUSERPWD, $GLOBALS['TYPO3_CONF_VARS']['SYS']['curlProxyUserPass'] );
2094 }
2095 }
2096 $content=curl_exec ($ch);
2097 curl_close ($ch);
2098 return $content;
2099 } elseif($fd = fopen($url,'rb')) {
2100 while (!feof($fd)) {
2101 $content.=fread($fd, 5000);
2102 }
2103 fclose($fd);
2104 return $content;
2105 }
2106 }
2107
2108 /**
2109 * Writes $content to the file $file
2110 * Usage: 30
2111 *
2112 * @param string Filepath to write to
2113 * @param string Content to write
2114 * @return boolean True if the file was successfully opened and written to.
2115 */
2116 function writeFile($file,$content) {
2117 if($fd = fopen($file,'wb')) {
2118 fwrite( $fd, $content);
2119 fclose( $fd );
2120
2121 t3lib_div::fixPermissions($file); // Change the permissions of the file
2122
2123 return true;
2124 }
2125 }
2126
2127 /**
2128 * Setting file system mode & group ownership of file
2129 *
2130 * @param string Filepath of newly created file
2131 * @return void
2132 */
2133 function fixPermissions($file) {
2134 if (@is_file($file) && TYPO3_OS!='WIN') {
2135 @chmod($file, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'])); // "@" is there because file is not necessarily OWNED by the user
2136 if($GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']) { // skip this if createGroup is empty
2137 @chgrp($file, $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']); // "@" is there because file is not necessarily OWNED by the user
2138 }
2139 }
2140 }
2141
2142 /**
2143 * Writes $content to a filename in the typo3temp/ folder (and possibly a subfolder...)
2144 * Accepts an additional subdirectory in the file path!
2145 *
2146 * @param string Absolute filepath to write to inside "typo3temp/". First part of this string must match PATH_site."typo3temp/"
2147 * @param string Content string to write
2148 * @return string Returns false on success, otherwise an error string telling about the problem.
2149 */
2150 function writeFileToTypo3tempDir($filepath,$content) {
2151
2152 // Parse filepath into directory and basename:
2153 $fI = pathinfo($filepath);
2154 $fI['dirname'].= '/';
2155
2156 // Check parts:
2157 if (t3lib_div::validPathStr($filepath) && $fI['basename'] && strlen($fI['basename'])<60) {
2158 if (defined('PATH_site')) {
2159 $dirName = PATH_site.'typo3temp/'; // Setting main temporary directory name (standard)
2160 if (@is_dir($dirName)) {
2161 if (t3lib_div::isFirstPartOfStr($fI['dirname'],$dirName)) {
2162
2163 // Checking if the "subdir" is found:
2164 $subdir = substr($fI['dirname'],strlen($dirName));
2165 if ($subdir) {
2166 if (ereg('^[[:alnum:]_]+\/$',$subdir)) {
2167 $dirName.= $subdir;
2168 if (!@is_dir($dirName)) {
2169 t3lib_div::mkdir($dirName);
2170 }
2171 } else return 'Subdir, "'.$subdir.'", was NOT on the form "[a-z]/"';
2172 }
2173 // Checking dir-name again (sub-dir might have been created):
2174 if (@is_dir($dirName)) {
2175 if ($filepath == $dirName.$fI['basename']) {
2176 t3lib_div::writeFile($filepath, $content);
2177 if (!@is_file($filepath)) return 'File not written to disk! Write permission error in filesystem?';
2178 } else return 'Calculated filelocation didn\'t match input $filepath!';
2179 } else return '"'.$dirName.'" is not a directory!';
2180 } else return '"'.$fI['dirname'].'" was not within directory PATH_site + "typo3temp/"';
2181 } else return 'PATH_site + "typo3temp/" was not a directory!';
2182 } else return 'PATH_site constant was NOT defined!';
2183 } else return 'Input filepath "'.$filepath.'" was generally invalid!';
2184 }
2185
2186 /**
2187 * Wrapper function for mkdir, setting folder permissions according to $GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'] and group ownership according to $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']
2188 * Usage: 6
2189 *
2190 * @param string Absolute path to folder, see PHP mkdir() function. Removes trailing slash internally.
2191 * @return boolean TRUE if @mkdir went well!
2192 */
2193 function mkdir($theNewFolder) {
2194 $theNewFolder = ereg_replace('\/$','',$theNewFolder);
2195 if (mkdir($theNewFolder, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask']))){
2196 chmod($theNewFolder, octdec($GLOBALS['TYPO3_CONF_VARS']['BE']['folderCreateMask'])); //added this line, because the mode at 'mkdir' has a strange behaviour sometimes
2197
2198 if($GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']) { // skip this if createGroup is empty
2199 chgrp($theNewFolder, $GLOBALS['TYPO3_CONF_VARS']['BE']['createGroup']);
2200 }
2201 return TRUE;
2202 }
2203 }
2204
2205 /**
2206 * Returns an array with the names of folders in a specific path
2207 * Will return 'error' (string) if there were an error with reading directory content.
2208 * Usage: 11
2209 *
2210 * @param string Path to list directories from
2211 * @return array Returns an array with the directory entries as values. If no path, the return value is nothing.
2212 */
2213 function get_dirs($path) {
2214 if ($path) {
2215 $d = @dir($path);
2216 if (is_object($d)) {
2217 while($entry=$d->read()) {
2218 if (@is_dir($path.'/'.$entry) && $entry!= '..' && $entry!= '.') {
2219 $filearray[]=$entry;
2220 }
2221 }
2222 $d->close();
2223 } else return 'error';
2224 return $filearray;
2225 }
2226 }
2227
2228 /**
2229 * Returns an array with the names of files in a specific path
2230 * Usage: 18
2231 *
2232 * @param string $path: Is the path to the file
2233 * @param string $extensionList is the comma list of extensions to read only (blank = all)
2234 * @param boolean If set, then the path is prepended the filenames. Otherwise only the filenames are returned in the array
2235 * @param string $order is sorting: 1= sort alphabetically, 'mtime' = sort by modification time.
2236 * @return array Array of the files found
2237 */
2238 function getFilesInDir($path,$extensionList='',$prependPath=0,$order='') {
2239
2240 // Initialize variabels:
2241 $filearray = array();
2242 $sortarray = array();
2243 $path = ereg_replace('\/$','',$path);
2244
2245 // Find files+directories:
2246 if (@is_dir($path)) {
2247 $extensionList = strtolower($extensionList);
2248 $d = dir($path);
2249 if (is_object($d)) {
2250 while($entry=$d->read()) {
2251 if (@is_file($path.'/'.$entry)) {
2252 $fI = pathinfo($entry);
2253 $key = md5($path.'/'.$entry); // Don't change this ever - extensions may depend on the fact that the hash is an md5 of the path! (import/export extension)
2254 if (!$extensionList || t3lib_div::inList($extensionList,strtolower($fI['extension']))) {
2255 $filearray[$key]=($prependPath?$path.'/':'').$entry;
2256 if ($order=='mtime') {$sortarray[$key]=filemtime($path.'/'.$entry);}
2257 elseif ($order) {$sortarray[$key]=$entry;}
2258 }
2259 }
2260 }
2261 $d->close();
2262 } else return 'error opening path: "'.$path.'"';
2263 }
2264
2265 // Sort them:
2266 if ($order) {
2267 asort($sortarray);
2268 reset($sortarray);
2269 $newArr=array();
2270 while(list($k,$v)=each($sortarray)) {
2271 $newArr[$k]=$filearray[$k];
2272 }
2273 $filearray=$newArr;
2274 }
2275
2276 // Return result
2277 reset($filearray);
2278 return $filearray;
2279 }
2280
2281 /**
2282 * Recursively gather all files and folders of a path.
2283 * Usage: 5
2284 *
2285 * @param array $fileArr: Empty input array (will have files added to it)
2286 * @param string $path: The path to read recursively from (absolute) (include trailing slash!)
2287 * @param string $extList: Comma list of file extensions: Only files with extensions in this list (if applicable) will be selected.
2288 * @param boolean $regDirs: If set, directories are also included in output.
2289 * @param integer $recursivityLevels: The number of levels to dig down...
2290 * @return array An array with the found files/directories.
2291 */
2292 function getAllFilesAndFoldersInPath($fileArr,$path,$extList='',$regDirs=0,$recursivityLevels=99) {
2293 if ($regDirs) $fileArr[] = $path;
2294 $fileArr = array_merge($fileArr, t3lib_div::getFilesInDir($path,$extList,1,1));
2295
2296 $dirs = t3lib_div::get_dirs($path);
2297 if (is_array($dirs) && $recursivityLevels>0) {
2298 foreach ($dirs as $subdirs) {
2299 if ((string)$subdirs!='') {
2300 $fileArr = t3lib_div::getAllFilesAndFoldersInPath($fileArr,$path.$subdirs.'/',$extList,$regDirs,$recursivityLevels-1);
2301 }
2302 }
2303 }
2304 return $fileArr;
2305 }
2306
2307 /**
2308 * Removes the absolute part of all files/folders in fileArr
2309 * Usage: 2
2310 *
2311 * @param array $fileArr: The file array to remove the prefix from
2312 * @param string $prefixToRemove: The prefix path to remove (if found as first part of string!)
2313 * @return array The input $fileArr processed.
2314 */
2315 function removePrefixPathFromList($fileArr,$prefixToRemove) {
2316 foreach($fileArr as $k => $absFileRef) {
2317 if(t3lib_div::isFirstPartOfStr($absFileRef,$prefixToRemove)) {
2318 $fileArr[$k] = substr($absFileRef,strlen($prefixToRemove));
2319 } else return 'ERROR: One or more of the files was NOT prefixed with the prefix-path!';
2320 }
2321 return $fileArr;
2322 }
2323
2324 /**
2325 * Fixes a path for windows-backslashes and reduces double-slashes to single slashes
2326 * Usage: 2
2327 *
2328 * @param string File path to process
2329 * @return string
2330 */
2331 function fixWindowsFilePath($theFile) {
2332 return str_replace('//','/', str_replace('\\','/', $theFile));
2333 }
2334
2335 /**
2336 * Resolves "../" sections in the input path string.
2337 * For example "fileadmin/directory/../other_directory/" will be resolved to "fileadmin/other_directory/"
2338 * Usage: 2
2339 *
2340 * @param string File path in which "/../" is resolved
2341 * @return string
2342 */
2343 function resolveBackPath($pathStr) {
2344 $parts = explode('/',$pathStr);
2345 $output=array();
2346 foreach($parts as $pV) {
2347 if ($pV=='..') {
2348 if ($c) {
2349 array_pop($output);
2350 $c--;
2351 } else $output[]=$pV;
2352 } else {
2353 $c++;
2354 $output[]=$pV;
2355 }
2356 }
2357 return implode('/',$output);
2358 }
2359
2360 /**
2361 * Prefixes a URL used with 'header-location' with 'http://...' depending on whether it has it already.
2362 * - If already having a scheme, nothing is prepended
2363 * - If having REQUEST_URI slash '/', then prefixing 'http://[host]' (relative to host)
2364 * - Otherwise prefixed with TYPO3_REQUEST_DIR (relative to current dir / TYPO3_REQUEST_DIR)
2365 * Usage: 30
2366 *
2367 * @param string URL / path to prepend full URL addressing to.
2368 * @return string
2369 */
2370 function locationHeaderUrl($path) {
2371 $uI = parse_url($path);
2372 if (substr($path,0,1)=='/') { // relative to HOST
2373 $path = t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').$path;
2374 } elseif (!$uI['scheme']) { // No scheme either
2375 $path = t3lib_div::getIndpEnv('TYPO3_REQUEST_DIR').$path;
2376 }
2377 return $path;
2378 }
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395 /*************************
2396 *
2397 * DEBUG helper FUNCTIONS
2398 *
2399 *************************/
2400
2401 /**
2402 * Returns a string with a list of ascii-values for the first $characters characters in $string
2403 * Usage: 0
2404 *
2405 * @param string String to show ASCII value for
2406 * @param integer Number of characters to show
2407 * @return string The string with ASCII values in separated by a space char.
2408 * @internal
2409 */
2410 function debug_ordvalue($string,$characters=100) {
2411 if(strlen($string) < $characters) $characters = strlen($string);
2412 for ($i=0; $i<$characters; $i++) {
2413 $valuestring.=' '.ord(substr($string,$i,1));
2414 }
2415 return trim($valuestring);
2416 }
2417
2418 /**
2419 * Returns HTML-code, which is a visual representation of a multidimensional array
2420 * use t3lib_div::print_array() in order to print an array
2421 * Returns false if $array_in is not an array
2422 * Usage: 31
2423 *
2424 * @param array Array to view
2425 * @return string HTML output
2426 */
2427 function view_array($array_in) {
2428 if (is_array($array_in)) {
2429 $result='<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
2430 if (!count($array_in)) {$result.= '<tr><td><font face="Verdana,Arial" size="1"><b>'.htmlspecialchars("EMPTY!").'</b></font></td></tr>';}
2431 while (list($key,$val)=each($array_in)) {
2432 $result.= '<tr><td><font face="Verdana,Arial" size="1">'.htmlspecialchars((string)$key).'</font></td><td>';
2433 if (is_array($array_in[$key])) {
2434 $result.=t3lib_div::view_array($array_in[$key]);
2435 } else
2436 $result.= '<font face="Verdana,Arial" size="1" color="red">'.nl2br(htmlspecialchars((string)$val)).'<br /></font>';
2437 $result.= '</td></tr>';
2438 }
2439 $result.= '</table>';
2440 } else {
2441 $result = false;
2442 }
2443 return $result;
2444 }
2445
2446 /**
2447 * Prints an array
2448 * Usage: 6
2449 *
2450 * @param array Array to print visually (in a table).
2451 * @return void
2452 * @internal
2453 * @see view_array()
2454 */
2455 function print_array($array_in) {
2456 echo t3lib_div::view_array($array_in);
2457 }
2458
2459 /**
2460 * Makes debug output
2461 * Prints $var in bold between two vertical lines
2462 * If not $var the word 'debug' is printed
2463 * If $var is an array, the array is printed by t3lib_div::print_array()
2464 * Usage: 8
2465 *
2466 * @param mixed Variable to print
2467 * @param mixed If the parameter is a string it will be used as header. Otherwise number of break tags to apply after (positive integer) or before (negative integer) the output.
2468 * @return void
2469 */
2470 function debug($var="",$brOrHeader=0) {
2471 if ($brOrHeader && !t3lib_div::testInt($brOrHeader)) {
2472 echo '<table border="0" cellpadding="0" cellspacing="0" bgcolor="white" style="border:0px; margin-top:3px; margin-bottom:3px;"><tr><td style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;">'.htmlspecialchars((string)$brOrHeader).'</td></tr><td>';
2473 } elseif ($brOrHeader<0) {
2474 for($a=0;$a<abs(intval($brOrHeader));$a++){echo '<br />';}
2475 }
2476
2477 if (is_array($var)) {
2478 t3lib_div::print_array($var);
2479 } elseif (is_object($var)) {
2480 echo '<b>|Object:<pre>';
2481 print_r($var);
2482 echo '</pre>|</b>';
2483 } elseif ((string)$var!='') {
2484 echo '<b>|'.htmlspecialchars((string)$var).'|</b>';
2485 } else {
2486 echo '<b>| debug |</b>';
2487 }
2488
2489 if ($brOrHeader && !t3lib_div::testInt($brOrHeader)) {
2490 echo '</td></tr></table>';
2491 } elseif ($brOrHeader>0) {
2492 for($a=0;$a<intval($brOrHeader);$a++){echo '<br />';}
2493 }
2494 }
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527 /*************************
2528 *
2529 * SYSTEM INFORMATION
2530 *
2531 *************************/
2532
2533 /**
2534 * Returns the HOST+DIR-PATH of the current script (The URL, but without 'http://' and without script-filename)
2535 * Usage: 1
2536 *
2537 * @return string
2538 */
2539 function getThisUrl() {
2540 $p=parse_url(t3lib_div::getIndpEnv('TYPO3_REQUEST_SCRIPT')); // Url of this script
2541 $dir=t3lib_div::dirname($p['path']).'/'; // Strip file
2542 $url = str_replace('//','/',$p['host'].($p['port']?':'.$p['port']:'').$dir);
2543 return $url;
2544 }
2545
2546 /**
2547 * Returns the link-url to the current script.
2548 * In $getParams you can set associative keys corresponding to the GET-vars you wish to add to the URL. If you set them empty, they will remove existing GET-vars from the current URL.
2549 * REMEMBER to always use htmlspecialchars() for content in href-properties to get ampersands converted to entities (XHTML requirement and XSS precaution)
2550 * Usage: 52
2551 *
2552 * @param array Array of GET parameters to include
2553 * @return string
2554 */
2555 function linkThisScript($getParams=array()) {
2556 $parts = t3lib_div::getIndpEnv('SCRIPT_NAME');
2557 $params = t3lib_div::_GET();
2558
2559 foreach($getParams as $k => $v) {
2560 if (strcmp($v,'')) {
2561 $params[$k]=$v;
2562 } else unset($params[$k]);
2563 }
2564
2565 $pString = t3lib_div::implodeArrayForUrl('',$params);
2566
2567 return $pString ? $parts.'?'.ereg_replace('^&','',$pString) : $parts;
2568 }
2569
2570 /**
2571 * Takes a full URL, $url, possibly with a querystring and overlays the $getParams arrays values onto the quirystring, packs it all together and returns the URL again.
2572 * So basically it adds the parameters in $getParams to an existing URL, $url
2573 * Usage: 2
2574 *
2575 * @param string URL string
2576 * @param array Array of key/value pairs for get parameters to add/overrule with. Can be multidimensional.
2577 * @return string Output URL with added getParams.
2578 */
2579 function linkThisUrl($url,$getParams=array()) {
2580 $parts = parse_url($url);
2581 if ($parts['query']) {
2582 parse_str($parts['query'],$getP);
2583 } else {
2584 $getP = array();
2585 }
2586
2587 $getP = t3lib_div::array_merge_recursive_overrule($getP,$getParams);
2588 $uP = explode('?',$url);
2589
2590 $params = t3lib_div::implodeArrayForUrl('',$getP);
2591 $outurl = $uP[0].($params ? '?'.substr($params, 1) : '');
2592
2593 return $outurl;
2594 }
2595
2596 /**
2597 * Abstraction method which returns System Environment Variables regardless of server OS, CGI/MODULE version etc. Basically this is SERVER variables for most of them.
2598 * This should be used instead of getEnv() and $_SERVER/ENV_VARS to get reliable values for all situations.
2599 * Usage: 221
2600 *
2601 * @param string Name of the "environment variable"/"server variable" you wish to use. Valid values are SCRIPT_NAME, SCRIPT_FILENAME, REQUEST_URI, PATH_INFO, REMOTE_ADDR, REMOTE_HOST, HTTP_REFERER, HTTP_HOST, HTTP_USER_AGENT, HTTP_ACCEPT_LANGUAGE, QUERY_STRING, TYPO3_DOCUMENT_ROOT, TYPO3_HOST_ONLY, TYPO3_HOST_ONLY, TYPO3_REQUEST_HOST, TYPO3_REQUEST_URL, TYPO3_REQUEST_SCRIPT, TYPO3_REQUEST_DIR, TYPO3_SITE_URL, _ARRAY
2602 * @return string Value based on the input key, independent of server/os environment.
2603 */
2604 function getIndpEnv($getEnvName) {
2605 /*
2606 Conventions:
2607 output from parse_url():
2608 URL: http://username:password@192.168.1.4:8080/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value#link1
2609 [scheme] => 'http'
2610 [user] => 'username'
2611 [pass] => 'password'
2612 [host] => '192.168.1.4'
2613 [port] => '8080'
2614 [path] => '/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/'
2615 [query] => 'arg1,arg2,arg3&p1=parameter1&p2[key]=value'
2616 [fragment] => 'link1'
2617
2618 Further definition: [path_script] = '/typo3/32/temp/phpcheck/index.php'
2619 [path_dir] = '/typo3/32/temp/phpcheck/'
2620 [path_info] = '/arg1/arg2/arg3/'
2621 [path] = [path_script/path_dir][path_info]
2622
2623
2624 Keys supported:
2625
2626 URI______:
2627 REQUEST_URI = [path]?[query] = /typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value
2628 HTTP_HOST = [host][:[port]] = 192.168.1.4:8080
2629 SCRIPT_NAME = [path_script]++ = /typo3/32/temp/phpcheck/index.php // NOTICE THAT SCRIPT_NAME will return the php-script name ALSO. [path_script] may not do that (eg. '/somedir/' may result in SCRIPT_NAME '/somedir/index.php')!
2630 PATH_INFO = [path_info] = /arg1/arg2/arg3/
2631 QUERY_STRING = [query] = arg1,arg2,arg3&p1=parameter1&p2[key]=value
2632 HTTP_REFERER = [scheme]://[host][:[port]][path] = http://192.168.1.4:8080/typo3/32/temp/phpcheck/index.php/arg1/arg2/arg3/?arg1,arg2,arg3&p1=parameter1&p2[key]=value
2633 (Notice: NO username/password + NO fragment)
2634
2635 CLIENT____:
2636 REMOTE_ADDR = (client IP)
2637 REMOTE_HOST = (client host)
2638 HTTP_USER_AGENT = (client user agent)
2639 HTTP_ACCEPT_LANGUAGE = (client accept language)
2640
2641 SERVER____:
2642 SCRIPT_FILENAME = Absolute filename of script (Differs between windows/unix). On windows 'C:\\blabla\\blabl\\' will be converted to 'C:/blabla/blabl/'
2643
2644 Special extras:
2645 TYPO3_HOST_ONLY = [host] = 192.168.1.4
2646 TYPO3_PORT = [port] = 8080 (blank if 80, taken from host value)
2647 TYPO3_REQUEST_HOST = [scheme]://[host][:[port]]
2648 TYPO3_REQUEST_URL = [scheme]://[host][:[port]][path]?[query] (sheme will by default be 'http' until we can detect if it's https -
2649 TYPO3_REQUEST_SCRIPT = [scheme]://[host][:[port]][path_script]
2650 TYPO3_REQUEST_DIR = [scheme]://[host][:[port]][path_dir]
2651 TYPO3_SITE_URL = [scheme]://[host][:[port]][path_dir] of the TYPO3 website frontend
2652 TYPO3_SITE_SCRIPT = [script / Speaking URL] of the TYPO3 website
2653 TYPO3_DOCUMENT_ROOT = Absolute path of root of documents: TYPO3_DOCUMENT_ROOT.SCRIPT_NAME = SCRIPT_FILENAME (typically)
2654
2655 Notice: [fragment] is apparently NEVER available to the script!
2656
2657
2658 Testing suggestions:
2659 - Output all the values.
2660 - In the script, make a link to the script it self, maybe add some parameters and click the link a few times so HTTP_REFERER is seen
2661 - ALSO TRY the script from the ROOT of a site (like 'http://www.mytest.com/' and not 'http://www.mytest.com/test/' !!)
2662
2663 */
2664
2665 # if ($getEnvName=='HTTP_REFERER') return '';
2666 switch((string)$getEnvName) {
2667 case 'SCRIPT_NAME':
2668 return (php_sapi_name()=='cgi'||php_sapi_name()=='cgi-fcgi')&&($_SERVER['ORIG_PATH_INFO']?$_SERVER['ORIG_PATH_INFO']:$_SERVER['PATH_INFO']) ? ($_SERVER['ORIG_PATH_INFO']?$_SERVER['ORIG_PATH_INFO']:$_SERVER['PATH_INFO']) : ($_SERVER['ORIG_SCRIPT_NAME']?$_SERVER['ORIG_SCRIPT_NAME']:$_SERVER['SCRIPT_NAME']);
2669 break;
2670 case 'SCRIPT_FILENAME':
2671 return str_replace('//','/', str_replace('\\','/', (php_sapi_name()=='cgi'||php_sapi_name()=='isapi' ||php_sapi_name()=='cgi-fcgi')&&($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED'])? ($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED']):($_SERVER['ORIG_SCRIPT_FILENAME']?$_SERVER['ORIG_SCRIPT_FILENAME']:$_SERVER['SCRIPT_FILENAME'])));
2672 break;
2673 case 'REQUEST_URI':
2674 // Typical application of REQUEST_URI is return urls, forms submitting to itself etc. Example: returnUrl='.rawurlencode(t3lib_div::getIndpEnv('REQUEST_URI'))
2675 if (!$_SERVER['REQUEST_URI']) { // This is for ISS/CGI which does not have the REQUEST_URI available.
2676 return '/'.ereg_replace('^/','',t3lib_div::getIndpEnv('SCRIPT_NAME')).
2677 ($_SERVER['QUERY_STRING']?'?'.$_SERVER['QUERY_STRING']:'');
2678 } else return $_SERVER['REQUEST_URI'];
2679 break;
2680 case 'PATH_INFO':
2681 // $_SERVER['PATH_INFO']!=$_SERVER['SCRIPT_NAME'] is necessary because some servers (Windows/CGI) are seen to set PATH_INFO equal to script_name
2682 // Further, there must be at least one '/' in the path - else the PATH_INFO value does not make sense.
2683 // IF 'PATH_INFO' never works for our purpose in TYPO3 with CGI-servers, then 'php_sapi_name()=='cgi'' might be a better check. Right now strcmp($_SERVER['PATH_INFO'],t3lib_div::getIndpEnv('SCRIPT_NAME')) will always return false for CGI-versions, but that is only as long as SCRIPT_NAME is set equal to PATH_INFO because of php_sapi_name()=='cgi' (see above)
2684 // if (strcmp($_SERVER['PATH_INFO'],t3lib_div::getIndpEnv('SCRIPT_NAME')) && count(explode('/',$_SERVER['PATH_INFO']))>1) {
2685 if (php_sapi_name()!='cgi'&&php_sapi_name()!='cgi-fcgi') {
2686 return $_SERVER['PATH_INFO'];
2687 } else return '';
2688 break;
2689 // These are let through without modification
2690 case 'REMOTE_ADDR':
2691 case 'REMOTE_HOST':
2692 case 'HTTP_REFERER':
2693 case 'HTTP_HOST':
2694 case 'HTTP_USER_AGENT':
2695 case 'HTTP_ACCEPT_LANGUAGE':
2696 case 'QUERY_STRING':
2697 return $_SERVER[$getEnvName];
2698 break;
2699 case 'TYPO3_DOCUMENT_ROOT':
2700 // Some CGI-versions (LA13CGI) and mod-rewrite rules on MODULE versions will deliver a 'wrong' DOCUMENT_ROOT (according to our description). Further various aliases/mod_rewrite rules can disturb this as well.
2701 // Therefore the DOCUMENT_ROOT is now always calculated as the SCRIPT_FILENAME minus the end part shared with SCRIPT_NAME.
2702 $SFN = t3lib_div::getIndpEnv('SCRIPT_FILENAME');
2703 $SN_A = explode('/',strrev(t3lib_div::getIndpEnv('SCRIPT_NAME')));
2704 $SFN_A = explode('/',strrev($SFN));
2705 $acc = array();
2706 while(list($kk,$vv)=each($SN_A)) {
2707 if (!strcmp($SFN_A[$kk],$vv)) {
2708 $acc[] = $vv;
2709 } else break;
2710 }
2711 $commonEnd=strrev(implode('/',$acc));
2712 if (strcmp($commonEnd,'')) $DR = substr($SFN,0,-(strlen($commonEnd)+1));
2713 return $DR;
2714 break;
2715 case 'TYPO3_HOST_ONLY':
2716 $p = explode(':',$_SERVER['HTTP_HOST']);
2717 return $p[0];
2718 break;
2719 case 'TYPO3_PORT':
2720 $p = explode(':',$_SERVER['HTTP_HOST']);
2721 return $p[1];
2722 break;
2723 case 'TYPO3_REQUEST_HOST':
2724 return (t3lib_div::getIndpEnv('TYPO3_SSL') ? 'https://' : 'http://').
2725 $_SERVER['HTTP_HOST'];
2726 break;
2727 case 'TYPO3_REQUEST_URL':
2728 return t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::getIndpEnv('REQUEST_URI');
2729 break;
2730 case 'TYPO3_REQUEST_SCRIPT':
2731 return t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::getIndpEnv('SCRIPT_NAME');
2732 break;
2733 case 'TYPO3_REQUEST_DIR':
2734 return t3lib_div::getIndpEnv('TYPO3_REQUEST_HOST').t3lib_div::dirname(t3lib_div::getIndpEnv('SCRIPT_NAME')).'/';
2735 break;
2736 case 'TYPO3_SITE_URL':
2737 if (defined('PATH_thisScript') && defined('PATH_site')) {
2738 $lPath = substr(dirname(PATH_thisScript),strlen(PATH_site)).'/';
2739 $url = t3lib_div::getIndpEnv('TYPO3_REQUEST_DIR');
2740 $siteUrl = substr($url,0,-strlen($lPath));
2741 if (substr($siteUrl,-1)!='/') $siteUrl.='/';
2742 return $siteUrl;
2743 } else return '';
2744 break;
2745 case 'TYPO3_SITE_SCRIPT':
2746 return substr(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'),strlen(t3lib_div::getIndpEnv('TYPO3_SITE_URL')));
2747 break;
2748 case 'TYPO3_SSL':
2749 return $_SERVER['SSL_SESSION_ID'] || !strcmp($_SERVER['HTTPS'],'on') ? TRUE : FALSE;
2750 break;
2751 case '_ARRAY':
2752 $out = array();
2753 // Here, list ALL possible keys to this function for debug display.
2754 $envTestVars = t3lib_div::trimExplode(',','
2755 HTTP_HOST,
2756 TYPO3_HOST_ONLY,
2757 TYPO3_PORT,
2758 PATH_INFO,
2759 QUERY_STRING,
2760 REQUEST_URI,
2761 HTTP_REFERER,
2762 TYPO3_REQUEST_HOST,
2763 TYPO3_REQUEST_URL,
2764 TYPO3_REQUEST_SCRIPT,
2765 TYPO3_REQUEST_DIR,
2766 TYPO3_SITE_URL,
2767 TYPO3_SITE_SCRIPT,
2768 TYPO3_SSL,
2769 SCRIPT_NAME,
2770 TYPO3_DOCUMENT_ROOT,
2771 SCRIPT_FILENAME,
2772 REMOTE_ADDR,
2773 REMOTE_HOST,
2774 HTTP_USER_AGENT,
2775 HTTP_ACCEPT_LANGUAGE',1);
2776 reset($envTestVars);
2777 while(list(,$v)=each($envTestVars)) {
2778 $out[$v]=t3lib_div::getIndpEnv($v);
2779 }
2780 reset($out);
2781 return $out;
2782 break;
2783 }
2784 }
2785
2786 /**
2787 * milliseconds
2788 * microtime recalculated to t3lib_div::milliseconds(1/1000 sec)
2789 * Usage: 20
2790 *
2791 * @return integer
2792 */
2793 function milliseconds() {
2794 $p=explode(' ',microtime());
2795 return round(($p[0]+$p[1])*1000);
2796 }
2797
2798 /**
2799 * Client Browser Information
2800 * Usage: 4
2801 *
2802 * @param string Alternative User Agent string (if empty, t3lib_div::getIndpEnv('HTTP_USER_AGENT') is used)
2803 * @return array Parsed information about the HTTP_USER_AGENT in categories BROWSER, VERSION, SYSTEM and FORMSTYLE
2804 */
2805 function clientInfo($useragent='') {
2806 if (!$useragent) $useragent=t3lib_div::getIndpEnv('HTTP_USER_AGENT');
2807
2808 $bInfo=array();
2809 // Which browser?
2810 if (strstr($useragent,'Konqueror')) {
2811 $bInfo['BROWSER']= 'konqu';
2812 } elseif (strstr($useragent,'Opera')) {
2813 $bInfo['BROWSER']= 'opera';
2814 } elseif (strstr($useragent,'MSIE 4') || strstr($useragent,'MSIE 5') || strstr($useragent,'MSIE 6')) {
2815 $bInfo['BROWSER']= 'msie';
2816 } elseif (strstr($useragent,'Mozilla/4') || strstr($useragent,'Mozilla/5')) {
2817 $bInfo['BROWSER']='net';
2818 }
2819 if ($bInfo['BROWSER']) {
2820 // Browser version
2821 switch($bInfo['BROWSER']) {
2822 case 'net':
2823 $bInfo['VERSION']= doubleval(substr($useragent,8));
2824 if (strstr($useragent,'Netscape6/')) {$bInfo['VERSION']=doubleval(substr(strstr($useragent,'Netscape6/'),10));}
2825 if (strstr($useragent,'Netscape/7')) {$bInfo['VERSION']=doubleval(substr(strstr($useragent,'Netscape/7'),9));}
2826 break;
2827 case 'msie':
2828 $tmp = strstr($useragent,'MSIE');
2829 $bInfo['VERSION'] = doubleval(ereg_replace('^[^0-9]*','',substr($tmp,4)));
2830 break;
2831 case 'opera':
2832 $tmp = strstr($useragent,'Opera');
2833 $bInfo['VERSION'] = doubleval(ereg_replace('^[^0-9]*','',substr($tmp,5)));
2834 break;
2835 case 'konqu':
2836 $tmp = strstr($useragent,'Konqueror/');
2837 $bInfo['VERSION'] = doubleval(substr($tmp,10));
2838 break;
2839 }
2840 // Client system
2841 if (strstr($useragent,'Win')) {
2842 $bInfo['SYSTEM'] = 'win';
2843 } elseif (strstr($useragent,'Mac')) {
2844 $bInfo['SYSTEM'] = 'mac';
2845 } elseif (strstr($useragent,'Linux') || strstr($useragent,'X11') || strstr($useragent,'SGI') || strstr($useragent,' SunOS ') || strstr($useragent,' HP-UX ')) {
2846 $bInfo['SYSTEM'] = 'unix';
2847 }
2848 }
2849 // Is true if the browser supports css to format forms, especially the width
2850 $bInfo['FORMSTYLE']=($bInfo['BROWSER']=='msie' || ($bInfo['BROWSER']=='net'&&$bInfo['VERSION']>=5) || $bInfo['BROWSER']=='opera' || $bInfo['BROWSER']=='konqu');
2851
2852 return $bInfo;
2853 }
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877 /*************************
2878 *
2879 * TYPO3 SPECIFIC FUNCTIONS
2880 *
2881 *************************/
2882
2883 /**
2884 * Returns the absolute filename of a relative reference, resolves the "EXT:" prefix (way of referring to files inside extensions) and checks that the file is inside the PATH_site of the TYPO3 installation and implies a check with t3lib_div::validPathStr(). Returns false if checks failed. Does not check if the file exists.
2885 * Usage: 24
2886 *
2887 * @param string The input filename/filepath to evaluate
2888 * @param boolean If $onlyRelative is set (which it is by default), then only return values relative to the current PATH_site is accepted.
2889 * @param boolean If $relToTYPO3_mainDir is set, then relative paths are relative to PATH_typo3 constant - otherwise (default) they are relative to PATH_site
2890 * @return string Returns the absolute filename of $filename IF valid, otherwise blank string.
2891 */
2892 function getFileAbsFileName($filename,$onlyRelative=1,$relToTYPO3_mainDir=0) {
2893 if (!strcmp($filename,'')) return '';
2894
2895 if ($relToTYPO3_mainDir) {
2896 if (!defined('PATH_typo3')) return '';
2897 $relPathPrefix = PATH_typo3;
2898 } else {
2899 $relPathPrefix = PATH_site;
2900 }
2901 if (substr($filename,0,4)=='EXT:') { // extension
2902 list($extKey,$local) = explode('/',substr($filename,4),2);
2903 $filename='';
2904 if (strcmp($extKey,'') && t3lib_extMgm::isLoaded($extKey) && strcmp($local,'')) {
2905 $filename = t3lib_extMgm::extPath($extKey).$local;
2906 }
2907 } elseif (!t3lib_div::isAbsPath($filename)) { // relative. Prepended with $relPathPrefix
2908 $filename=$relPathPrefix.$filename;
2909 } elseif ($onlyRelative && !t3lib_div::isFirstPartOfStr($filename,$relPathPrefix)) { // absolute, but set to blank if not allowed
2910 $filename='';
2911 }
2912 if (strcmp($filename,'') && t3lib_div::validPathStr($filename)) { // checks backpath.
2913 return $filename;
2914 }
2915 }
2916
2917 /**
2918 * Checks for malicious file paths.
2919 * Returns true if no '//', '..' or '\' is in the $theFile
2920 * This should make sure that the path is not pointing 'backwards' and further doesn't contain double/back slashes.
2921 * So it's compatible with the UNIX style path strings valid for TYPO3 internally.
2922 * Usage: 14
2923 *
2924 * @param string Filepath to evaluate
2925 * @return boolean True, if no '//', '..' or '\' is in the $theFile
2926 * @todo Possible improvement: Should it rawurldecode the string first to check if any of these characters is encoded ?
2927 */
2928 function validPathStr($theFile) {
2929 if (!strstr($theFile,'//') && !strstr($theFile,'..') && !strstr($theFile,'\\')) return true;
2930 }
2931
2932 /**
2933 * Checks if the $path is absolute or relative (detecting either '/' or 'x:/' as first part of string) and returns true if so.
2934 * Usage: 8
2935 *
2936 * @param string Filepath to evaluate
2937 * @return boolean
2938 */
2939 function isAbsPath($path) {
2940 return TYPO3_OS=='WIN' ? substr($path,1,2)==':/' : substr($path,0,1)=='/';
2941 }
2942
2943 /**
2944 * Returns true if the path is absolute, without backpath '..' and within the PATH_site OR within the lockRootPath
2945 * Usage: 5
2946 *
2947 * @param string Filepath to evaluate
2948 * @return boolean
2949 */
2950 function isAllowedAbsPath($path) {
2951 if (t3lib_div::isAbsPath($path) &&
2952 t3lib_div::validPathStr($path) &&
2953 ( t3lib_div::isFirstPartOfStr($path,PATH_site)
2954 ||
2955 ($GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath'] && t3lib_div::isFirstPartOfStr($path,$GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath']))
2956 )
2957 ) return true;
2958 }
2959
2960 /**
2961 * Verifies the input filename againts the 'fileDenyPattern'. Returns true if OK.
2962 * Usage: 2
2963 *
2964 * @param string Filepath to evaluate
2965 * @return boolean
2966 */
2967 function verifyFilenameAgainstDenyPattern($filename) {
2968 if (strcmp($filename,'') && strcmp($GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],'')) {
2969 $result = eregi($GLOBALS['TYPO3_CONF_VARS']['BE']['fileDenyPattern'],$filename);
2970 if ($result) return false; // so if a matching filename is found, return false;
2971 }
2972 return true;
2973 }
2974
2975 /**
2976 * Moves $source file to $destination if uploaded, otherwise try to make a copy
2977 * Usage: 4
2978 *
2979 * @param string Source file, absolute path
2980 * @param string Destination file, absolute path
2981 * @return boolean Returns true if the file was moved.
2982 * @coauthor Dennis Petersen <fessor@software.dk>
2983 * @see upload_to_tempfile()
2984 */
2985 function upload_copy_move($source,$destination) {
2986 if (is_uploaded_file($source)) {
2987 $uploaded = TRUE;
2988 // Return the value of move_uploaded_file, and if false the temporary $source is still around so the user can use unlink to delete it:
2989 $uploadedResult = move_uploaded_file($source, $destination);
2990 } else {
2991 $uploaded = FALSE;
2992 @copy($source,$destination);
2993 }
2994
2995 t3lib_div::fixPermissions($destination); // Change the permissions of the file
2996
2997 // If here the file is copied and the temporary $source is still around, so when returning false the user can try unlink to delete the $source
2998 return $uploaded ? $uploadedResult : FALSE;
2999 }
3000
3001 /**
3002 * Will move an uploaded file (normally in "/tmp/xxxxx") to a temporary filename in PATH_site."typo3temp/" from where TYPO3 can use it under safe_mode.
3003 * Use this function to move uploaded files to where you can work on them.
3004 * REMEMBER to use t3lib_div::unlink_tempfile() afterwards - otherwise temp-files will build up! They are NOT automatically deleted in PATH_site."typo3temp/"!
3005 * Usage: 6
3006 *
3007 * @param string The temporary uploaded filename, eg. $_FILES['[upload field name here]']['tmp_name']
3008 * @return string If a new file was successfully created, return its filename, otherwise blank string.
3009 * @see unlink_tempfile(), upload_copy_move()
3010 */
3011 function upload_to_tempfile($uploadedFileName) {
3012 if (is_uploaded_file($uploadedFileName)) {
3013 $tempFile = t3lib_div::tempnam('upload_temp_');
3014 move_uploaded_file($uploadedFileName, $tempFile);
3015 return @is_file($tempFile) ? $tempFile : '';
3016 }
3017 }
3018
3019 /**
3020 * Deletes (unlink) a temporary filename in 'PATH_site."typo3temp/"' given as input.
3021 * The function will check that the file exists, is in PATH_site."typo3temp/" and does not contain back-spaces ("../") so it should be pretty safe.
3022 * Use this after upload_to_tempfile() or tempnam() from this class!
3023 * Usage: 9
3024 *
3025 * @param string Filepath for a file in PATH_site."typo3temp/". Must be absolute.
3026 * @return boolean Returns true if the file was unlink()'ed
3027 * @see upload_to_tempfile(), tempnam()
3028 */
3029 function unlink_tempfile($uploadedTempFileName) {
3030 if ($uploadedTempFileName && t3lib_div::validPathStr($uploadedTempFileName) && t3lib_div::isFirstPartOfStr($uploadedTempFileName,PATH_site.'typo3temp/') && @is_file($uploadedTempFileName)) {
3031 if (unlink($uploadedTempFileName)) return TRUE;
3032 }
3033 }
3034
3035 /**
3036 * Create temporary filename (Create file with unique file name)
3037 * This function should be used for getting temporary filenames - will make your applications safe for open_basedir = on
3038 * REMEMBER to delete the temporary files after use! This is done by t3lib_div::unlink_tempfile()
3039 * Usage: 7
3040 *
3041 * @param string Prefix to temp file (which will have no extension btw)
3042 * @return string result from PHP function tempnam() with PATH_site.'typo3temp/' set for temp path.
3043 * @see unlink_tempfile(), upload_to_tempfile()
3044 */
3045 function tempnam($filePrefix) {
3046 return tempnam(PATH_site.'typo3temp/',$filePrefix);
3047 }
3048
3049 /**
3050 * standard authentication code - can't remember what it's used for.
3051 * Usage: 2
3052 *
3053 * @param mixed Uid (integer) or record (array)
3054 * @param string List of fields from the record if that is given.
3055 * @return string MD5 hash of 8 chars.
3056 * @internal
3057 */
3058 function stdAuthCode($uid_or_record,$fields='') {
3059 if (is_array($uid_or_record)) {
3060 $recCopy_temp=array();
3061 if ($fields) {
3062 $fieldArr = t3lib_div::trimExplode(',',$fields,1);
3063 reset($fieldArr);
3064 while(list($k,$v)=each($fieldArr)) {
3065 $recCopy_temp[$k]=$recCopy[$v];
3066 }
3067 } else {
3068 $recCopy_temp=$recCopy;
3069 }
3070 $preKey = implode('|',$recCopy_temp);
3071 } else {
3072 $preKey = $uid_or_record;
3073 }
3074
3075 $authCode = $preKey.'||'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
3076 $authCode = substr(md5($authCode),0,8);
3077 return $authCode;
3078 }
3079
3080 /**
3081 * Splits the input query-parameters into an array with certain parameters filtered out.
3082 * Used to create the cHash value
3083 *
3084 * @param string Query-parameters: "&xxx=yyy&zzz=uuu"
3085 * @return array Array with key/value pairs of query-parameters WITHOUT a certain list of variable names (like id, type, no_cache etc) and WITH a variable, encryptionKey, specific for this server/installation
3086 * @see tslib_fe::makeCacheHash(), tslib_cObj::typoLink()
3087 */
3088 function cHashParams($addQueryParams) {
3089 $params = explode('&',substr($addQueryParams,1)); // Splitting parameters up
3090
3091 // Make array:
3092 $pA = array();
3093 foreach($params as $theP) {
3094 $pKV = explode('=', $theP); // Splitting single param by '=' sign
3095 if (!t3lib_div::inList('id,type,no_cache,cHash,MP,ftu',$pKV[0])) {
3096 $pA[$pKV[0]] = (string)rawurldecode($pKV[1]);
3097 }
3098 }
3099 $pA['encryptionKey'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
3100 ksort($pA);
3101
3102 return $pA;
3103 }
3104
3105 /**
3106 * Responds on input localization setting value whether the page it comes from should be hidden if no translation exists or not.
3107 *
3108 * @param integer Value from "l18n_cfg" field of a page record
3109 * @return boolean True if the page should be hidden
3110 */
3111 function hideIfNotTranslated($l18n_cfg_fieldValue) {
3112 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['hidePagesIfNotTranslatedByDefault']) {
3113 return $l18n_cfg_fieldValue&2 ? FALSE : TRUE;
3114 } else {
3115 return $l18n_cfg_fieldValue&2 ? TRUE : FALSE;
3116 }
3117 }
3118
3119 /**
3120 * Includes a locallang file and returns the $LOCAL_LANG array found inside.
3121 *
3122 * @param string Input is a file-reference (see t3lib_div::getFileAbsFileName) which, if exists, is included. That file is expected to be a 'local_lang' file containing a $LOCAL_LANG array.
3123 * @param string Language key
3124 * @return array Value of $LOCAL_LANG found in the included file. If that array is found it's returned. Otherwise an empty array
3125 */
3126 function readLLfile($fileRef,$langKey) {
3127 $file = t3lib_div::getFileAbsFileName($fileRef);
3128 if ($file) {
3129 $baseFile = ereg_replace('\.(php|xml)$', '', $file);
3130
3131 if (@is_file($baseFile.'.xml')) {
3132 $LOCAL_LANG = t3lib_div::readLLXMLfile($baseFile.'.xml', $langKey);
3133 } elseif (@is_file($baseFile.'.php')) {
3134 include($baseFile.'.php');
3135 } else die('Filereference, "'.$file.'", not found!');
3136 }
3137
3138 return is_array($LOCAL_LANG)?$LOCAL_LANG:array();
3139 }
3140
3141 /**
3142 * Includes a locallang-xml file and returns the $LOCAL_LANG array
3143 * Works only when the frontend or backend has been initialized with a charset conversion object. See first code lines.
3144 *
3145 * @param string Absolute reference to locallang-XML file
3146 * @param string TYPO3 language key, eg. "dk" or "de" or "default"
3147 * @return array LOCAL_LANG array in return.
3148 */
3149 function readLLXMLfile($fileRef,$langKey) {
3150
3151 if (is_object($GLOBALS['LANG'])) {
3152 $csConvObj = &$GLOBALS['LANG']->csConvObj;
3153 } elseif (is_object($GLOBALS['TSFE'])) {
3154 $csConvObj = &$GLOBALS['TSFE']->csConvObj;
3155 } else $csConvObj = NULL;
3156
3157 if (@is_file($fileRef) && $langKey && is_object($csConvObj)) {
3158
3159 // Set charset:
3160 $origCharset = $csConvObj->parse_charset($csConvObj->charSetArray[$langKey] ? $csConvObj->charSetArray[$langKey] : 'iso-8859-1');
3161
3162 // Cache file name:
3163 $hashSource = substr($fileRef,strlen(PATH_site)).'|'.date('d-m-Y H:i:s',filemtime($fileRef));
3164 $cacheFileName = PATH_site.'typo3temp/llxml/'.
3165 #str_replace('_','',ereg_replace('^.*\/','',dirname($fileRef))).
3166 #'_'.basename($fileRef).
3167 substr(basename($fileRef),10,15).
3168 '_'.t3lib_div::shortMD5($hashSource).'.'.$langKey.'.'.$origCharset.'.cache';
3169
3170 // Check if cache file exists...
3171 if (!@is_file($cacheFileName)) { // ... if it doesn't, create content and write it:
3172
3173 // Read XML, parse it.
3174 $xmlString = t3lib_div::getUrl($fileRef);
3175 $xmlContent = t3lib_div::xml2array($xmlString);
3176
3177 // Set default LOCAL_LANG array content:
3178 $LOCAL_LANG = array();
3179 $LOCAL_LANG['default'] = $xmlContent['data']['default'];
3180
3181 // Specific language, convert from utf-8 to backend language charset:
3182 // NOTICE: Converting from utf-8 back to "native" language may be a temporary solution until we can totally discard "locallang.php" files altogether (and use utf-8 for everything). But doing this conversion is the quickest way to migrate now and the source is in utf-8 anyway which is the main point.
3183 if ($langKey!='default') {
3184 $LOCAL_LANG[$langKey] = $xmlContent['data'][$langKey];
3185
3186 // Checking if charset should be converted.
3187 if (is_array($LOCAL_LANG[$langKey]) && $origCharset!='utf-8') {
3188 foreach($LOCAL_LANG[$langKey] as $labelKey => $labelValue) {
3189 $LOCAL_LANG[$langKey][$labelKey] = $csConvObj->utf8_decode($labelValue,$origCharset);
3190 }
3191 }
3192 }
3193
3194 // Cache the content now:
3195 $serContent = array('origFile'=>$hashSource, 'LOCAL_LANG'=>$LOCAL_LANG);
3196 $res = t3lib_div::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
3197 if ($res) die('ERROR: '.$res);
3198 } else {
3199 // Get content from cache:
3200 $serContent = unserialize(t3lib_div::getUrl($cacheFileName));
3201 $LOCAL_LANG = $serContent['LOCAL_LANG'];
3202 }
3203
3204 // Checking for EXTERNAL file for non-default language:
3205 if ($langKey!='default' && is_string($LOCAL_LANG[$langKey]) && strlen($LOCAL_LANG[$langKey])) {
3206
3207 // Look for localized file:
3208 $localized_file = t3lib_div::getFileAbsFileName($LOCAL_LANG[$langKey]);
3209 if ($localized_file && @is_file($localized_file)) {
3210
3211 // Cache file name:
3212 $hashSource = substr($localized_file,strlen(PATH_site)).'|'.date('d-m-Y H:i:s',filemtime($localized_file));
3213 $cacheFileName = PATH_site.'typo3temp/llxml/ext_'.
3214 substr(basename($localized_file),10,15).
3215 '_'.t3lib_div::shortMD5($hashSource).'.'.$langKey.'.'.$origCharset.'.cache';
3216
3217 // Check if cache file exists...
3218 if (!@is_file($cacheFileName)) { // ... if it doesn't, create content and write it:
3219
3220 // Read and parse XML content:
3221 $local_xmlString = t3lib_div::getUrl($localized_file);
3222 $local_xmlContent = t3lib_div::xml2array($local_xmlString);
3223 $LOCAL_LANG[$langKey] = is_array($local_xmlContent['data'][$langKey]) ? $local_xmlContent['data'][$langKey] : array();
3224
3225 // Checking if charset should be converted.
3226 if (is_array($LOCAL_LANG[$langKey]) && $origCharset!='utf-8') {
3227 foreach($LOCAL_LANG[$langKey] as $labelKey => $labelValue) {
3228 $LOCAL_LANG[$langKey][$labelKey] = $csConvObj->utf8_decode($labelValue,$origCharset);
3229 }
3230 }
3231
3232 // Cache the content now:
3233 $serContent = array('extlang'=>$langKey, 'origFile'=>$LOCAL_LANG[$langKey], 'EXT_DATA'=>$LOCAL_LANG[$langKey]);
3234 $res = t3lib_div::writeFileToTypo3tempDir($cacheFileName, serialize($serContent));
3235 if ($res) die('ERROR: '.$res);
3236 } else {
3237 // Get content from cache:
3238 $serContent = unserialize(t3lib_div::getUrl($cacheFileName));
3239 $LOCAL_LANG[$langKey] = $serContent['EXT_DATA'];
3240 }
3241 } else {
3242 $LOCAL_LANG[$langKey] = array();
3243 }
3244 }
3245
3246 return $LOCAL_LANG;
3247 }
3248 }
3249
3250 /**
3251 * Loads the $TCA (Table Configuration Array) for the $table
3252 *
3253 * Requirements:
3254 * 1) must be configured table (the ctrl-section configured),
3255 * 2) columns must not be an array (which it is always if whole table loaded), and
3256 * 3) there is a value for dynamicConfigFile (filename in typo3conf)