00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00232 class t3lib_div {
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00262 function _GP($var) {
00263 if(empty($var)) return;
00264 $value = isset($_POST[$var]) ? $_POST[$var] : $_GET[$var];
00265 if (isset($value)) {
00266 if (is_array($value)) { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
00267 }
00268 return $value;
00269 }
00270
00280 function _GET($var=NULL) {
00281 $value = ($var === NULL) ? $_GET : (empty($var) ? NULL : $_GET[$var]);
00282 if (isset($value)) {
00283 if (is_array($value)) { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
00284 }
00285 return $value;
00286 }
00287
00297 function _POST($var=NULL) {
00298 $value = ($var === NULL) ? $_POST : (empty($var) ? NULL : $_POST[$var]);
00299 if (isset($value)) {
00300 if (is_array($value)) { t3lib_div::stripSlashesOnArray($value); } else { $value = stripslashes($value); }
00301 }
00302 return $value;
00303 }
00304
00313 function _GETset($inputGet,$key='') {
00314
00315 if (strcmp($key,'')) {
00316 if (is_array($inputGet)) { t3lib_div::addSlashesOnArray($inputGet); } else { $inputGet = addslashes($inputGet); }
00317 $GLOBALS['HTTP_GET_VARS'][$key] = $_GET[$key] = $inputGet;
00318 } elseif (is_array($inputGet)){
00319 t3lib_div::addSlashesOnArray($inputGet);
00320 $GLOBALS['HTTP_GET_VARS'] = $_GET = $inputGet;
00321 }
00322 }
00323
00336 function GPvar($var,$strip=0) {
00337 if(empty($var)) return;
00338 $value = isset($_POST[$var]) ? $_POST[$var] : $_GET[$var];
00339 if (isset($value) && is_string($value)) { $value = stripslashes($value); }
00340 if ($strip && isset($value) && is_array($value)) { t3lib_div::stripSlashesOnArray($value); }
00341 return $value;
00342 }
00343
00353 function GParrayMerged($var) {
00354 $postA = is_array($_POST[$var]) ? $_POST[$var] : array();
00355 $getA = is_array($_GET[$var]) ? $_GET[$var] : array();
00356 $mergedA = t3lib_div::array_merge_recursive_overrule($getA,$postA);
00357 t3lib_div::stripSlashesOnArray($mergedA);
00358 return $mergedA;
00359 }
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00397 function gif_compress($theFile, $type) {
00398 $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
00399 $returnCode='';
00400 if ($gfxConf['gif_compress'] && strtolower(substr($theFile,-4,4))=='.gif') {
00401 if (($type=='IM' || !$type) && $gfxConf['im'] && $gfxConf['im_path_lzw']) {
00402 $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$theFile.'"', $gfxConf['im_path_lzw']);
00403 exec($cmd);
00404
00405 $returnCode='IM';
00406 } elseif (($type=='GD' || !$type) && $gfxConf['gdlib'] && !$gfxConf['gdlib_png']) {
00407 $tempImage = imageCreateFromGif($theFile);
00408 imageGif($tempImage, $theFile);
00409 imageDestroy($tempImage);
00410 $returnCode='GD';
00411 }
00412 }
00413 return $returnCode;
00414 }
00415
00425 function png_to_gif_by_imagemagick($theFile) {
00426 if ($GLOBALS['TYPO3_CONF_VARS']['FE']['png_to_gif']
00427 && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im']
00428 && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']
00429 && strtolower(substr($theFile,-4,4))=='.png'
00430 && @is_file($theFile)) {
00431 $newFile = substr($theFile,0,-4).'.gif';
00432 $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$newFile.'"', $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw']);
00433 exec($cmd);
00434 $theFile = $newFile;
00435
00436 }
00437 return $theFile;
00438 }
00439
00450 function read_png_gif($theFile,$output_png=0) {
00451 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] && @is_file($theFile)) {
00452 $ext = strtolower(substr($theFile,-4,4));
00453 if (
00454 ((string)$ext=='.png' && $output_png) ||
00455 ((string)$ext=='.gif' && !$output_png)
00456 ) {
00457 return $theFile;
00458 } else {
00459 $newFile = PATH_site.'typo3temp/readPG_'.md5($theFile.'|'.filemtime($theFile)).($output_png?'.png':'.gif');
00460 exec($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'].'convert "'.$theFile.'" "'.$newFile.'"');
00461 if (@is_file($newFile)) return $newFile;
00462 }
00463 }
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00499 function fixed_lgd($string,$origChars,$preStr='...') {
00500 $chars = abs($origChars);
00501 if ($chars >= 4) {
00502 if(strlen($string)>$chars) {
00503 return $origChars < 0 ?
00504 $preStr.trim(substr($string, -($chars-3))) :
00505 trim(substr($string, 0, $chars-3)).$preStr;
00506 }
00507 }
00508 return $string;
00509 }
00510
00524 function fixed_lgd_pre($string,$chars) {
00525 return strrev(t3lib_div::fixed_lgd(strrev($string),$chars));
00526 }
00527
00538 function fixed_lgd_cs($string,$chars) {
00539 if (is_object($GLOBALS['LANG'])) {
00540 return $GLOBALS['LANG']->csConvObj->crop($GLOBALS['LANG']->charSet,$string,$chars,'...');
00541 } else {
00542 return t3lib_div::fixed_lgd($string, $chars);
00543 }
00544 }
00545
00555 function breakTextForEmail($str,$implChar="\n",$charWidth=76) {
00556 $lines = explode(chr(10),$str);
00557 $outArr=array();
00558 while(list(,$lStr)=each($lines)) {
00559 $outArr = array_merge($outArr,t3lib_div::breakLinesForEmail($lStr,$implChar,$charWidth));
00560 }
00561 return implode(chr(10),$outArr);
00562 }
00563
00574 function breakLinesForEmail($str,$implChar="\n",$charWidth=76) {
00575 $lines=array();
00576 $l=$charWidth;
00577 $p=0;
00578 while(strlen($str)>$p) {
00579 $substr=substr($str,$p,$l);
00580 if (strlen($substr)==$l) {
00581 $count = count(explode(' ',trim(strrev($substr))));
00582 if ($count>1) {
00583 $parts = explode(' ',strrev($substr),2);
00584 $theLine = strrev($parts[1]);
00585 } else {
00586 $afterParts = explode(' ',substr($str,$l+$p),2);
00587 $theLine = $substr.$afterParts[0];
00588 }
00589 if (!strlen($theLine)) {break; }
00590 } else {
00591 $theLine=$substr;
00592 }
00593
00594 $lines[]=trim($theLine);
00595 $p+=strlen($theLine);
00596 if (!trim(substr($str,$p,$l))) break;
00597 }
00598 return implode($implChar,$lines);
00599 }
00600
00610 function cmpIP($baseIP, $list) {
00611 if ($list==='*') return TRUE;
00612 if (strstr($baseIP, ':') && t3lib_div::validIPv6($baseIP)) {
00613 return t3lib_div::cmpIPv6($baseIP, $list);
00614 } else {
00615 return t3lib_div::cmpIPv4($baseIP, $list);
00616 }
00617 }
00618
00626 function cmpIPv4($baseIP, $list) {
00627 $IPpartsReq = explode('.',$baseIP);
00628 if (count($IPpartsReq)==4) {
00629 $values = t3lib_div::trimExplode(',',$list,1);
00630
00631 foreach($values as $test) {
00632 list($test,$mask) = explode('/',$test);
00633
00634 if(intval($mask)) {
00635
00636 $lnet = ip2long($test);
00637 $lip = ip2long($baseIP);
00638 $binnet = str_pad( decbin($lnet),32,'0','STR_PAD_LEFT');
00639 $firstpart = substr($binnet,0,$mask);
00640 $binip = str_pad( decbin($lip),32,'0','STR_PAD_LEFT');
00641 $firstip = substr($binip,0,$mask);
00642 $yes = (strcmp($firstpart,$firstip)==0);
00643 } else {
00644
00645 $IPparts = explode('.',$test);
00646 $yes = 1;
00647 reset($IPparts);
00648 while(list($index,$val)=each($IPparts)) {
00649 $val = trim($val);
00650 if (strcmp($val,'*') && strcmp($IPpartsReq[$index],$val)) {
00651 $yes=0;
00652 }
00653 }
00654 }
00655 if ($yes) return true;
00656 }
00657 }
00658 return false;
00659 }
00660
00668 function cmpIPv6($baseIP, $list) {
00669 $success = false;
00670 $baseIP = t3lib_div::normalizeIPv6($baseIP);
00671
00672 $values = t3lib_div::trimExplode(',',$list,1);
00673 foreach ($values as $test) {
00674 list($test,$mask) = explode('/',$test);
00675 if (t3lib_div::validIPv6($test)) {
00676 $test = t3lib_div::normalizeIPv6($test);
00677 if (intval($mask)) {
00678 switch ($mask) {
00679 case '48':
00680 $testBin = substr(t3lib_div::IPv6Hex2Bin($test), 0, 48);
00681 $baseIPBin = substr(t3lib_div::IPv6Hex2Bin($baseIP), 0, 48);
00682 $success = strcmp($testBin, $baseIPBin)==0 ? true : false;
00683 break;
00684 case '64':
00685 $testBin = substr(t3lib_div::IPv6Hex2Bin($test), 0, 64);
00686 $baseIPBin = substr(t3lib_div::IPv6Hex2Bin($baseIP), 0, 64);
00687 $success = strcmp($testBin, $baseIPBin)==0 ? true : false;
00688 break;
00689 default:
00690 $success = false;
00691 }
00692 } else {
00693 if (t3lib_div::validIPv6($test)) {
00694 $testBin = t3lib_div::IPv6Hex2Bin($test);
00695 $baseIPBin = t3lib_div::IPv6Hex2Bin($baseIP);
00696 $success = strcmp($testBin, $baseIPBin)==0 ? true : false;
00697 }
00698 }
00699 }
00700 if ($success) return true;
00701 }
00702 return false;
00703 }
00704
00711 function IPv6Hex2Bin ($hex) {
00712 $bin = '';
00713 $hex = str_replace(':', '', $hex);
00714 for ($i=0; $i<strlen($hex); $i=$i+2) {
00715 $bin.= chr(hexdec(substr($hex, $i, 2)));
00716 }
00717 return $bin;
00718 }
00719
00726 function normalizeIPv6($address) {
00727 $normalizedAddress = '';
00728 $stageOneAddress = '';
00729
00730 $chunks = explode('::', $address);
00731 if (count($chunks)==2) {
00732 $chunksLeft = explode(':', $chunks[0]);
00733 $chunksRight = explode(':', $chunks[1]);
00734 $left = count($chunksLeft);
00735 $right = count($chunksRight);
00736
00737
00738 if ($left==1 && strlen($chunksLeft[0])==0) $left=0;
00739
00740 $hiddenBlocks = 8 - ($left + $right);
00741 $hiddenPart = '';
00742 while ($h<$hiddenBlocks) {
00743 $hiddenPart .= '0000:';
00744 $h++;
00745 }
00746
00747 if ($left == 0) {
00748 $stageOneAddress = $hiddenPart . $chunks[1];
00749 } else {
00750 $stageOneAddress = $chunks[0] . ':' . $hiddenPart . $chunks[1];
00751 }
00752 } else $stageOneAddress = $address;
00753
00754
00755 $blocks = explode(':', $stageOneAddress);
00756 $divCounter = 0;
00757 foreach ($blocks as $block) {
00758 $tmpBlock = '';
00759 $i = 0;
00760 $hiddenZeros = 4 - strlen($block);
00761 while ($i < $hiddenZeros) {
00762 $tmpBlock .= '0';
00763 $i++;
00764 }
00765 $normalizedAddress .= $tmpBlock . $block;
00766 if ($divCounter < 7) {
00767 $normalizedAddress .= ':';
00768 $divCounter++;
00769 }
00770 }
00771 return $normalizedAddress;
00772 }
00773
00782 function validIPv6($ip) {
00783 $uppercaseIP = strtoupper($ip);
00784
00785 $regex = '/^(';
00786 $regex.= '(([\dA-F]{1,4}:){7}[\dA-F]{1,4})|';
00787 $regex.= '(([\dA-F]{1,4}){1}::([\dA-F]{1,4}:){1,5}[\dA-F]{1,4})|';
00788 $regex.= '(([\dA-F]{1,4}:){2}:([\dA-F]{1,4}:){1,4}[\dA-F]{1,4})|';
00789 $regex.= '(([\dA-F]{1,4}:){3}:([\dA-F]{1,4}:){1,3}[\dA-F]{1,4})|';
00790 $regex.= '(([\dA-F]{1,4}:){4}:([\dA-F]{1,4}:){1,2}[\dA-F]{1,4})|';
00791 $regex.= '(([\dA-F]{1,4}:){5}:([\dA-F]{1,4}:){0,1}[\dA-F]{1,4})|';
00792 $regex.= '(::([\dA-F]{1,4}:){0,6}[\dA-F]{1,4})';
00793 $regex.= ')$/';
00794
00795 return preg_match($regex, $uppercaseIP) ? true : false;
00796 }
00797
00805 function cmpFQDN($baseIP, $list) {
00806 if (count(explode('.',$baseIP))==4) {
00807 $resolvedHostName = explode('.', gethostbyaddr($baseIP));
00808 $values = t3lib_div::trimExplode(',',$list,1);
00809
00810 foreach($values as $test) {
00811 $hostNameParts = explode('.',$test);
00812 $yes = 1;
00813
00814 foreach($hostNameParts as $index => $val) {
00815 $val = trim($val);
00816 if (strcmp($val,'*') && strcmp($resolvedHostName[$index],$val)) {
00817 $yes=0;
00818 }
00819 }
00820 if ($yes) return true;
00821 }
00822 }
00823 return false;
00824 }
00825
00835 function inList($list,$item) {
00836 return strstr(','.$list.',', ','.$item.',') ? true : false;
00837 }
00838
00847 function rmFromList($element,$list) {
00848 $items = explode(',',$list);
00849 while(list($k,$v)=each($items)) {
00850 if ($v==$element) {unset($items[$k]);}
00851 }
00852 return implode(',',$items);
00853 }
00854
00863 function expandList($list) {
00864 $items = explode(',',$list);
00865 $list = array();
00866 while(list(,$item)=each($items)) {
00867 $range = explode('-',$item);
00868 if (isset($range[1])) {
00869 $runAwayBrake = 1000;
00870 for ($n=$range[0]; $n<=$range[1]; $n++) {
00871 $list[] = $n;
00872
00873 $runAwayBrake--;
00874 if ($runAwayBrake<=0) break;
00875 }
00876 } else {
00877 $list[] = $item;
00878 }
00879 }
00880
00881 return implode(',',$list);
00882 }
00883
00894 function intInRange($theInt,$min,$max=2000000000,$zeroValue=0) {
00895
00896 $theInt = intval($theInt);
00897 if ($zeroValue && !$theInt) {$theInt=$zeroValue;}
00898 if ($theInt<$min){$theInt=$min;}
00899 if ($theInt>$max){$theInt=$max;}
00900 return $theInt;
00901 }
00902
00910 function intval_positive($theInt) {
00911 $theInt = intval($theInt);
00912 if ($theInt<0){$theInt=0;}
00913 return $theInt;
00914 }
00915
00923 function int_from_ver($verNumberStr) {
00924 $verParts = explode('.',$verNumberStr);
00925 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));
00926 }
00927
00936 function compat_version($verNumberStr) {
00937 global $TYPO3_CONF_VARS;
00938 $currVersionStr = $TYPO3_CONF_VARS['SYS']['compat_version'] ? $TYPO3_CONF_VARS['SYS']['compat_version'] : TYPO3_branch;
00939
00940 if (t3lib_div::int_from_ver($currVersionStr) < t3lib_div::int_from_ver($verNumberStr)) {
00941 return FALSE;
00942 } else {
00943 return TRUE;
00944 }
00945 }
00946
00954 function md5int($str) {
00955 return hexdec(substr(md5($str),0,7));
00956 }
00957
00967 function shortMD5($input, $len=10) {
00968 return substr(md5($input),0,$len);
00969 }
00970
00980 function uniqueList($in_list, $secondParameter=NULL) {
00981 if (is_array($in_list)) die('t3lib_div::uniqueList() does NOT support array arguments anymore! Only string comma lists!');
00982 if (isset($secondParameter)) die('t3lib_div::uniqueList() does NOT support more than a single argument value anymore. You have specified more than one.');
00983
00984 return implode(',',array_unique(t3lib_div::trimExplode(',',$in_list,1)));
00985 }
00986
00994 function split_fileref($fileref) {
00995 $reg = array();
00996 if ( ereg('(.*/)(.*)$',$fileref,$reg) ) {
00997 $info['path'] = $reg[1];
00998 $info['file'] = $reg[2];
00999 } else {
01000 $info['path'] = '';
01001 $info['file'] = $fileref;
01002 }
01003 $reg='';
01004 if ( ereg('(.*)\.([^\.]*$)',$info['file'],$reg) ) {
01005 $info['filebody'] = $reg[1];
01006 $info['fileext'] = strtolower($reg[2]);
01007 $info['realFileext'] = $reg[2];
01008 } else {
01009 $info['filebody'] = $info['file'];
01010 $info['fileext'] = '';
01011 }
01012 reset($info);
01013 return $info;
01014 }
01015
01032 function dirname($path) {
01033 $p=t3lib_div::revExplode('/',$path,2);
01034 return count($p)==2?$p[0]:'';
01035 }
01036
01048 function modifyHTMLColor($color,$R,$G,$B) {
01049
01050 $nR = t3lib_div::intInRange(hexdec(substr($color,1,2))+$R,0,255);
01051 $nG = t3lib_div::intInRange(hexdec(substr($color,3,2))+$G,0,255);
01052 $nB = t3lib_div::intInRange(hexdec(substr($color,5,2))+$B,0,255);
01053 return '#'.
01054 substr('0'.dechex($nR),-2).
01055 substr('0'.dechex($nG),-2).
01056 substr('0'.dechex($nB),-2);
01057 }
01058
01068 function modifyHTMLColorAll($color,$all) {
01069 return t3lib_div::modifyHTMLColor($color,$all,$all,$all);
01070 }
01071
01079 function rm_endcomma($string) {
01080 return ereg_replace(',$','',$string);
01081 }
01082
01092 function danish_strtoupper($string) {
01093 $value = strtoupper($string);
01094 return strtr($value, 'áéúíâêûôîæøåäöü', 'ÁÉÚÍÄËÜÖÏÆØÅÄÖÜ');
01095 }
01096
01107 function convUmlauts($str) {
01108 $pat = array ( '/ä/', '/Ä/', '/ö/', '/Ö/', '/ü/', '/Ü/', '/ß/', '/å/', '/Å/', '/ø/', '/Ø/', '/æ/', '/Æ/' );
01109 $repl = array ( 'ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss', 'aa', 'AA', 'oe', 'OE', 'ae', 'AE' );
01110 return preg_replace($pat,$repl,$str);
01111 }
01112
01120 function testInt($var) {
01121 return !strcmp($var,intval($var));
01122 }
01123
01132 function isFirstPartOfStr($str,$partStr) {
01133
01134 $psLen = strlen($partStr);
01135 if ($psLen) {
01136 return substr($str,0,$psLen)==(string)$partStr;
01137 } else return false;
01138 }
01139
01148 function formatSize($sizeInBytes,$labels='') {
01149
01150
01151 if (strlen($labels) == 0) {
01152 $labels = ' | K| M| G';
01153 } else {
01154 $labels = str_replace('"','',$labels);
01155 }
01156 $labelArr = explode('|',$labels);
01157
01158
01159 if ($sizeInBytes>900) {
01160 if ($sizeInBytes>900000000) {
01161 $val = $sizeInBytes/(1024*1024*1024);
01162 return number_format($val, (($val<20)?1:0), '.', '').$labelArr[3];
01163 }
01164 elseif ($sizeInBytes>900000) {
01165 $val = $sizeInBytes/(1024*1024);
01166 return number_format($val, (($val<20)?1:0), '.', '').$labelArr[2];
01167 } else {
01168 $val = $sizeInBytes/(1024);
01169 return number_format($val, (($val<20)?1:0), '.', '').$labelArr[1];
01170 }
01171 } else {
01172 return $sizeInBytes.$labelArr[0];
01173 }
01174 }
01175
01183 function convertMicrotime($microtime) {
01184 $parts = explode(' ',$microtime);
01185 return round(($parts[0]+$parts[1])*1000);
01186 }
01187
01197 function splitCalc($string,$operators) {
01198 $res = Array();
01199 $sign='+';
01200 while($string) {
01201 $valueLen=strcspn($string,$operators);
01202 $value=substr($string,0,$valueLen);
01203 $res[] = Array($sign,trim($value));
01204 $sign=substr($string,$valueLen,1);
01205 $string=substr($string,$valueLen+1);
01206 }
01207 reset($res);
01208 return $res;
01209 }
01210
01219 function calcPriority($string) {
01220 $string=ereg_replace('[[:space:]]*','',$string);
01221 $string='+'.$string;
01222 $qm='\*\/\+-^%';
01223 $regex = '(['.$qm.'])(['.$qm.']?[0-9\.]*)';
01224
01225 $reg = array();
01226 preg_match_all('/'.$regex.'/',$string,$reg);
01227
01228 reset($reg[2]);
01229 $number=0;
01230 $Msign='+';
01231 $err='';
01232 $buffer=doubleval(current($reg[2]));
01233 next($reg[2]);
01234 while(list($k,$v)=each($reg[2])) {
01235 $v=doubleval($v);
01236 $sign = $reg[1][$k];
01237 if ($sign=='+' || $sign=='-') {
01238 $number = $Msign=='-' ? $number-=$buffer : $number+=$buffer;
01239 $Msign = $sign;
01240 $buffer=$v;
01241 } else {
01242 if ($sign=='/') {if ($v) $buffer/=$v; else $err='dividing by zero';}
01243 if ($sign=='%') {if ($v) $buffer%=$v; else $err='dividing by zero';}
01244 if ($sign=='*') {$buffer*=$v;}
01245 if ($sign=='^') {$buffer=pow($buffer,$v);}
01246 }
01247 }
01248 $number = $Msign=='-' ? $number-=$buffer : $number+=$buffer;
01249 return $err ? 'ERROR: '.$err : $number;
01250 }
01251
01260 function calcParenthesis($string) {
01261 $securC=100;
01262 do {
01263 $valueLenO=strcspn($string,'(');
01264 $valueLenC=strcspn($string,')');
01265 if ($valueLenC==strlen($string) || $valueLenC < $valueLenO) {
01266 $value = t3lib_div::calcPriority(substr($string,0,$valueLenC));
01267 $string = $value.substr($string,$valueLenC+1);
01268 return $string;
01269 } else {
01270 $string = substr($string,0,$valueLenO).t3lib_div::calcParenthesis(substr($string,$valueLenO+1));
01271 }
01272
01273 $securC--;
01274 if ($securC<=0) break;
01275 } while($valueLenO<strlen($string));
01276 return $string;
01277 }
01278
01286 function htmlspecialchars_decode($value) {
01287 $value = str_replace('>','>',$value);
01288 $value = str_replace('<','<',$value);
01289 $value = str_replace('"','"',$value);
01290 $value = str_replace('&','&',$value);
01291 return $value;
01292 }
01293
01301 function deHSCentities($str) {
01302 return ereg_replace('&([#[:alnum:]]*;)','&\1',$str);
01303 }
01304
01314 function slashJS($string,$extended=0,$char="'") {
01315 if ($extended) {$string = str_replace ("\\", "\\\\", $string);}
01316 return str_replace ($char, "\\".$char, $string);
01317 }
01318
01327 function rawUrlEncodeJS($str) {
01328 return str_replace('%20',' ',rawurlencode($str));
01329 }
01330
01339 function rawUrlEncodeFP($str) {
01340 return str_replace('%2F','/',rawurlencode($str));
01341 }
01342
01350 function validEmail($email) {
01351 $email = trim ($email);
01352 if (strstr($email,' ')) return FALSE;
01353 return ereg('^[A-Za-z0-9\._-]+[@][A-Za-z0-9\._-]+[\.].[A-Za-z0-9]+$',$email) ? TRUE : FALSE;
01354 }
01355
01365 function formatForTextarea($content) {
01366 return chr(10).htmlspecialchars($content);
01367 }
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380 /*************************
01381 *
01382 * ARRAY FUNCTIONS
01383 *
01384 *************************/
01385
01396 function inArray($in_array,$item) {
01397 if (is_array($in_array)) {
01398 while (list(,$val)=each($in_array)) {
01399 if (!is_array($val) && !strcmp($val,$item)) return true;
01400 }
01401 }
01402 }
01403
01413 function intExplode($delim, $string) {
01414 $temp = explode($delim,$string);
01415 while(list($key,$val)=each($temp)) {
01416 $temp[$key]=intval($val);
01417 }
01418 reset($temp);
01419 return $temp;
01420 }
01421
01432 function revExplode($delim, $string, $count=0) {
01433 $temp = explode($delim,strrev($string),$count);
01434 while(list($key,$val)=each($temp)) {
01435 $temp[$key]=strrev($val);
01436 }
01437 $temp=array_reverse($temp);
01438 reset($temp);
01439 return $temp;
01440 }
01441
01452 function trimExplode($delim, $string, $onlyNonEmptyValues=0) {
01453 $temp = explode($delim,$string);
01454 $newtemp=array();
01455 while(list($key,$val)=each($temp)) {
01456 if (!$onlyNonEmptyValues || strcmp('',trim($val))) {
01457 $newtemp[]=trim($val);
01458 }
01459 }
01460 reset($newtemp);
01461 return $newtemp;
01462 }
01463
01474 function uniqueArray($valueArray) {
01475 return array_unique($valueArray);
01476 }
01477
01486 function removeArrayEntryByValue($array,$cmpValue) {
01487 if (is_array($array)) {
01488 reset($array);
01489 while(list($k,$v)=each($array)) {
01490 if (is_array($v)) {
01491 $array[$k] = t3lib_div::removeArrayEntryByValue($v,$cmpValue);
01492 } else {
01493 if (!strcmp($v,$cmpValue)) {
01494 unset($array[$k]);
01495 }
01496 }
01497 }
01498 }
01499 reset($array);
01500 return $array;
01501 }
01502
01515 function implodeArrayForUrl($name,$theArray,$str='',$skipBlank=0,$rawurlencodeParamName=0) {
01516 if (is_array($theArray)) {
01517 foreach($theArray as $Akey => $AVal) {
01518 $thisKeyName = $name ? $name.'['.$Akey.']' : $Akey;
01519 if (is_array($AVal)) {
01520 $str = t3lib_div::implodeArrayForUrl($thisKeyName,$AVal,$str,$skipBlank,$rawurlencodeParamName);
01521 } else {
01522 if (!$skipBlank || strcmp($AVal,'')) {
01523 $str.='&'.($rawurlencodeParamName ? rawurlencode($thisKeyName) : $thisKeyName).
01524 '='.rawurlencode($AVal);
01525 }
01526 }
01527 }
01528 }
01529 return $str;
01530 }
01531
01540 function explodeUrl2Array($string,$multidim=FALSE) {
01541 $output = array();
01542 if ($multidim) {
01543 parse_str($string,$output);
01544 } else {
01545 $p = explode('&',$string);
01546 foreach($p as $v) {
01547 if (strlen($v)) {
01548 list($pK,$pV) = explode('=',$v,2);
01549 $output[rawurldecode($pK)] = rawurldecode($pV);
01550 }
01551 }
01552 }
01553 return $output;
01554 }
01555
01566 function compileSelectedGetVarsFromArray($varList,$getArray,$GPvarAlt=1) {
01567 $keys = t3lib_div::trimExplode(',',$varList,1);
01568 $outArr=array();
01569 foreach($keys as $v) {
01570 if (isset($getArray[$v])) {
01571 $outArr[$v]=$getArray[$v];
01572 } elseif ($GPvarAlt) {
01573 $outArr[$v]=t3lib_div::_GP($v);
01574 }
01575 }
01576 return $outArr;
01577 }
01578
01589 function addSlashesOnArray(&$theArray) {
01590 if (is_array($theArray)) {
01591 reset($theArray);
01592 while(list($Akey,$AVal)=each($theArray)) {
01593 if (is_array($AVal)) {
01594 t3lib_div::addSlashesOnArray($theArray[$Akey]);
01595 } else {
01596 $theArray[$Akey] = addslashes($AVal);
01597 }
01598 }
01599 reset($theArray);
01600 }
01601 }
01602
01613 function stripSlashesOnArray(&$theArray) {
01614 if (is_array($theArray)) {
01615 reset($theArray);
01616 while(list($Akey,$AVal)=each($theArray)) {
01617 if (is_array($AVal)) {
01618 t3lib_div::stripSlashesOnArray($theArray[$Akey]);
01619 } else {
01620 $theArray[$Akey] = stripslashes($AVal);
01621 }
01622 }
01623 reset($theArray);
01624 }
01625 }
01626
01635 function slashArray($arr,$cmd) {
01636 if ($cmd=='strip') t3lib_div::stripSlashesOnArray($arr);
01637 if ($cmd=='add') t3lib_div::addSlashesOnArray($arr);
01638 return $arr;
01639 }
01640
01652 function array_merge_recursive_overrule($arr0,$arr1,$notAddKeys=0,$includeEmtpyValues=true) {
01653 reset($arr1);
01654 while(list($key,$val) = each($arr1)) {
01655 if(is_array($arr0[$key])) {
01656 if (is_array($arr1[$key])) {
01657 $arr0[$key] = t3lib_div::array_merge_recursive_overrule($arr0[$key],$arr1[$key],$notAddKeys);
01658 }
01659 } else {
01660 if ($notAddKeys) {
01661 if (isset($arr0[$key])) {
01662 if ($includeEmtpyValues OR $val) {
01663 $arr0[$key] = $val;
01664 }
01665 }
01666 } else {
01667 if ($includeEmtpyValues OR $val) {
01668 $arr0[$key] = $val;
01669 }
01670 }
01671 }
01672 }
01673 reset($arr0);
01674 return $arr0;
01675 }
01676
01685 function array_merge($arr1,$arr2) {
01686 return $arr2+$arr1;
01687 }
01688
01698 function csvValues($row,$delim=',',$quote='"') {
01699 reset($row);
01700 $out=array();
01701 while(list(,$value)=each($row)) {
01702 list($valPart) = explode(chr(10),$value);
01703 $valPart = trim($valPart);
01704 $out[]=str_replace($quote,$quote.$quote,$valPart);
01705 }
01706 $str = $quote.implode($quote.$delim.$quote,$out).$quote;
01707 return $str;
01708 }
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01740 function get_tag_attributes($tag) {
01741 $components = t3lib_div::split_tag_attributes($tag);
01742 $name = '';
01743 $valuemode = '';
01744 if (is_array($components)) {
01745 while (list($key,$val) = each ($components)) {
01746 if ($val != '=') {
01747 if ($valuemode) {
01748 if ($name) {
01749 $attributes[$name] = $val;
01750 $name = '';
01751 }
01752 } else {
01753 if ($key = strtolower(ereg_replace('[^a-zA-Z0-9]','',$val))) {
01754 $attributes[$key] = '';
01755 $name = $key;
01756 }
01757 }
01758 $valuemode = '';
01759 } else {
01760 $valuemode = 'on';
01761 }
01762 }
01763 if (is_array($attributes)) reset($attributes);
01764 return $attributes;
01765 }
01766 }
01767
01777 function split_tag_attributes($tag) {
01778 $tag_tmp = trim(eregi_replace ('^<[^[:space:]]*','',trim($tag)));
01779
01780 $tag_tmp = trim(eregi_replace ('>$','',$tag_tmp));
01781
01782 while (strcmp($tag_tmp,'')) {
01783 $firstChar=substr($tag_tmp,0,1);
01784 if (!strcmp($firstChar,'"') || !strcmp($firstChar,"'")) {
01785 $reg=explode($firstChar,$tag_tmp,3);
01786 $value[]=$reg[1];
01787 $tag_tmp=trim($reg[2]);
01788 } elseif (!strcmp($firstChar,'=')) {
01789 $value[] = '=';
01790 $tag_tmp = trim(substr($tag_tmp,1));
01791 } else {
01792
01793 $reg = split('[[:space:]=]',$tag_tmp,2);
01794 $value[] = trim($reg[0]);
01795 $tag_tmp = trim(substr($tag_tmp,strlen($reg[0]),1).$reg[1]);
01796 }
01797 }
01798 if (is_array($value)) reset($value);
01799 return $value;
01800 }
01801
01811 function implodeAttributes($arr,$xhtmlSafe=FALSE,$dontOmitBlankAttribs=FALSE) {
01812 if (is_array($arr)) {
01813 if ($xhtmlSafe) {
01814 $newArr=array();
01815 foreach($arr as $p => $v) {
01816 if (!isset($newArr[strtolower($p)])) $newArr[strtolower($p)] = htmlspecialchars($v);
01817 }
01818 $arr = $newArr;
01819 }
01820 $list = array();
01821 foreach($arr as $p => $v) {
01822 if (strcmp($v,'') || $dontOmitBlankAttribs) {$list[]=$p.'="'.$v.'"';}
01823 }
01824 return implode(' ',$list);
01825 }
01826 }
01827
01838 function implodeParams($arr,$xhtmlSafe=FALSE,$dontOmitBlankAttribs=FALSE) {
01839 return t3lib_div::implodeAttributes($arr,$xhtmlSafe,$dontOmitBlankAttribs);
01840 }
01841
01853 function wrapJS($string, $linebreak=TRUE) {
01854 if(trim($string)) {
01855
01856 $cr = $linebreak? "\n" : '';
01857
01858
01859 $string = preg_replace ('/^\n+/', '', $string);
01860
01861 $match = array();
01862 if(preg_match('/^(\t+)/',$string,$match)) {
01863 $string = str_replace($match[1],"\t", $string);
01864 }
01865 $string = $cr.'<script type="text/javascript">
01866
01867 '.$string.'
01868
01869 </script>'.$cr;
01870 }
01871 return trim($string);
01872 }
01873
01874
01884 function xml2tree($string,$depth=999) {
01885 $parser = xml_parser_create();
01886 $vals = array();
01887 $index = array();
01888
01889 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
01890 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
01891 xml_parse_into_struct($parser, $string, $vals, $index);
01892
01893 if (xml_get_error_code($parser)) return 'Line '.xml_get_current_line_number($parser).': '.xml_error_string(xml_get_error_code($parser));
01894 xml_parser_free($parser);
01895
01896 $stack = array( array() );
01897 $stacktop = 0;
01898 $startPoint=0;
01899
01900
01901 unset($tagi);
01902 foreach($vals as $key => $val) {
01903 $type = $val['type'];
01904
01905
01906 if ($type=='open' || $type=='complete') {
01907 $stack[$stacktop++] = $tagi;
01908
01909 if ($depth==$stacktop) {
01910 $startPoint=$key;
01911 }
01912
01913 $tagi = array('tag' => $val['tag']);
01914
01915 if(isset($val['attributes'])) $tagi['attrs'] = $val['attributes'];
01916 if(isset($val['value'])) $tagi['values'][] = $val['value'];
01917 }
01918
01919 if ($type=='complete' || $type=='close') {
01920 $oldtagi = $tagi;
01921 $tagi = $stack[--$stacktop];
01922 $oldtag = $oldtagi['tag'];
01923 unset($oldtagi['tag']);
01924
01925 if ($depth==($stacktop+1)) {
01926 if ($key-$startPoint > 0) {
01927 $partArray = array_slice(
01928 $vals,
01929 $startPoint+1,
01930 $key-$startPoint-1
01931 );
01932 #$oldtagi=array('XMLvalue'=>t3lib_div::xmlRecompileFromStructValArray($partArray));
01933 $oldtagi['XMLvalue']=t3lib_div::xmlRecompileFromStructValArray($partArray);
01934 } else {
01935 $oldtagi['XMLvalue']=$oldtagi['values'][0];
01936 }
01937 }
01938
01939 $tagi['ch'][$oldtag][] = $oldtagi;
01940 unset($oldtagi);
01941 }
01942
01943 if($type=='cdata') {
01944 $tagi['values'][] = $val['value'];
01945 }
01946 }
01947 return $tagi['ch'];
01948 }
01949
01960 function array2xml_cs($array,$docTag='phparray',$options=array(),$charset='') {
01961
01962
01963 if (!$charset) {
01964 if ($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset']) {
01965 $charset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
01966 } elseif (is_object($GLOBALS['LANG'])) {
01967 $charset = $GLOBALS['LANG']->charSet;
01968 } else {
01969 $charset = 'iso-8859-1';
01970 }
01971 }
01972
01973
01974 return '<?xml version="1.0" encoding="'.htmlspecialchars($charset).'" standalone="yes" ?>'.chr(10).
01975 t3lib_div::array2xml($array,'',0,$docTag,0, $options);
01976 }
01977
02001 function array2xml($array,$NSprefix='',$level=0,$docTag='phparray',$spaceInd=0, $options=array(),$stackData=array()) {
02002
02003 $binaryChars = chr(0).chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8).
02004 chr(11).chr(12).chr(14).chr(15).chr(16).chr(17).chr(18).chr(19).
02005 chr(20).chr(21).chr(22).chr(23).chr(24).chr(25).chr(26).chr(27).chr(28).chr(29).
02006 chr(30).chr(31);
02007
02008 $indentChar = $spaceInd ? ' ' : chr(9);
02009 $indentN = $spaceInd>0 ? $spaceInd : 1;
02010
02011
02012 $output='';
02013
02014
02015 if (is_array($array)) {
02016 foreach($array as $k=>$v) {
02017 $attr = '';
02018 $tagName = $k;
02019
02020
02021 if(isset($options['grandParentTagMap'][$stackData['grandParentTagName'].'/'.$stackData['parentTagName']])) {
02022 $attr.=' index="'.htmlspecialchars($tagName).'"';
02023 $tagName = (string)$options['grandParentTagMap'][$stackData['grandParentTagName'].'/'.$stackData['parentTagName']];
02024 }elseif(isset($options['parentTagMap'][$stackData['parentTagName'].':_IS_NUM']) && t3lib_div::testInt($tagName)) {
02025 $attr.=' index="'.htmlspecialchars($tagName).'"';
02026 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName'].':_IS_NUM'];
02027 }elseif(isset($options['parentTagMap'][$stackData['parentTagName'].':'.$tagName])) {
02028 $attr.=' index="'.htmlspecialchars($tagName).'"';
02029 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName'].':'.$tagName];
02030 } elseif(isset($options['parentTagMap'][$stackData['parentTagName']])) {
02031 $attr.=' index="'.htmlspecialchars($tagName).'"';
02032 $tagName = (string)$options['parentTagMap'][$stackData['parentTagName']];
02033 } elseif (!strcmp(intval($tagName),$tagName)) {
02034 if ($options['useNindex']) {
02035 $tagName = 'n'.$tagName;
02036 } else {
02037 $attr.=' index="'.$tagName.'"';
02038 $tagName = $options['useIndexTagForNum'] ? $options['useIndexTagForNum'] : 'numIndex';
02039 }
02040 } elseif($options['useIndexTagForAssoc']) {
02041 $attr.=' index="'.htmlspecialchars($tagName).'"';
02042 $tagName = $options['useIndexTagForAssoc'];
02043 }
02044
02045
02046 $tagName = substr(ereg_replace('[^[:alnum:]_-]','',$tagName),0,100);
02047
02048
02049 if (is_array($v)) {
02050
02051
02052 if ($options['alt_options'][$stackData['path'].'/'.$tagName]) {
02053 $subOptions = $options['alt_options'][$stackData['path'].'/'.$tagName];
02054 $clearStackPath = $subOptions['clearStackPath'];
02055 } else {
02056 $subOptions = $options;
02057 $clearStackPath = FALSE;
02058 }
02059
02060 $content = chr(10).
02061 t3lib_div::array2xml(
02062 $v,
02063 $NSprefix,
02064 $level+1,
02065 '',
02066 $spaceInd,
02067 $subOptions,
02068 array(
02069 'parentTagName' => $tagName,
02070 'grandParentTagName' => $stackData['parentTagName'],
02071 'path' => $clearStackPath ? '' : $stackData['path'].'/'.$tagName,
02072 )
02073 ).
02074 str_pad('',($level+1)*$indentN,$indentChar);
02075 if ((int)$options['disableTypeAttrib']!=2) {
02076 $attr.=' type="array"';
02077 }
02078 } else {
02079
02080
02081 if (strcspn($v,$binaryChars) != strlen($v)) {
02082
02083 $content = chr(10).chunk_split(base64_encode($v));
02084 $attr.=' base64="1"';
02085 } else {
02086
02087 $content = htmlspecialchars($v);
02088 $dType = gettype($v);
02089 if ($dType!='string' && !$options['disableTypeAttrib']) { $attr.=' type="'.$dType.'"'; }
02090 }
02091 }
02092
02093
02094 $output.=str_pad('',($level+1)*$indentN,$indentChar).'<'.$NSprefix.$tagName.$attr.'>'.$content.'</'.$NSprefix.$tagName.'>'.chr(10);
02095 }
02096 }
02097
02098
02099 if (!$level) {
02100 $output =
02101 '<'.$docTag.'>'.chr(10).
02102 $output.
02103 '</'.$docTag.'>';
02104 }
02105
02106 return $output;
02107 }
02108
02120 function xml2array($string,$NSprefix='',$reportDocTag=FALSE) {
02121 global $TYPO3_CONF_VARS;
02122
02123
02124 $parser = xml_parser_create();
02125 $vals = array();
02126 $index = array();
02127
02128 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
02129 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
02130
02131
02132