Skip to content

Commit

Permalink
fix: php 8.1 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkez committed Jan 30, 2022
1 parent 7825371 commit aa07384
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 37 deletions.
62 changes: 43 additions & 19 deletions base.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ function parse($str) {
* @return mixed
*/
function cast($val) {
if (preg_match('/^(?:0x[0-9a-f]+|0[0-7]+|0b[01]+)$/i',$val))
if ($val && preg_match('/^(?:0x[0-9a-f]+|0[0-7]+|0b[01]+)$/i',$val))
return intval($val,0);
if (is_numeric($val))
return $val+0;
$val=trim($val);
$val=trim($val?:'');
if (preg_match('/^\w+$/i',$val) && defined($val))
return constant($val);
return $val;
Expand Down Expand Up @@ -829,7 +829,7 @@ function constants($class,$prefix='') {
**/
function hash($str) {
return str_pad(base_convert(
substr(sha1($str),-16),16,36),11,'0',STR_PAD_LEFT);
substr(sha1($str?:''),-16),16,36),11,'0',STR_PAD_LEFT);
}

/**
Expand Down Expand Up @@ -968,6 +968,7 @@ function($expr) use($args,$conv) {
isset($prop)?$prop:null
]
);
$php81=version_compare(PHP_VERSION, '8.1.0')>=0;
switch ($type) {
case 'plural':
preg_match_all('/(?<tag>\w+)'.
Expand Down Expand Up @@ -1051,19 +1052,42 @@ function_exists('money_format') &&
($frac?strlen($frac)-2:0),
$decimal_point,$thousands_sep);
case 'date':
if (empty($mod) || $mod=='short')
$prop='%x';
elseif ($mod=='full')
$prop='%A, %d %B %Y';
elseif ($mod!='custom')
$prop='%d %B %Y';
return strftime($prop,$args[$pos]);
if ($php81) {
$lang = $this->split($this->LANGUAGE);
// requires intl extension
$formatter = new IntlDateFormatter($lang[0],
(empty($mod) || $mod=='short')
? IntlDateFormatter::SHORT :
($mod=='full' ? IntlDateFormatter::LONG : IntlDateFormatter::MEDIUM),
IntlDateFormatter::NONE);
return $formatter->format($args[$pos]);
} else {
if (empty($mod) || $mod=='short')
$prop='%x';
elseif ($mod=='full')
$prop='%A, %d %B %Y';
elseif ($mod!='custom')
$prop='%d %B %Y';
return strftime($prop,$args[$pos]);
}
case 'time':
if (empty($mod) || $mod=='short')
$prop='%X';
elseif ($mod!='custom')
$prop='%r';
return strftime($prop,$args[$pos]);
if ($php81) {
$lang = $this->split($this->LANGUAGE);
// requires intl extension
$formatter = new IntlDateFormatter($lang[0],
IntlDateFormatter::NONE,
(empty($mod) || $mod=='short')
? IntlDateFormatter::SHORT :
($mod=='full' ? IntlDateFormatter::LONG : IntlDateFormatter::MEDIUM),
IntlTimeZone::createTimeZone($this->hive['TZ']));
return $formatter->format($args[$pos]);
} else {
if (empty($mod) || $mod=='short')
$prop='%X';
elseif ($mod!='custom')
$prop='%r';
return strftime($prop,$args[$pos]);
}
default:
return $expr[0];
}
Expand Down Expand Up @@ -1424,7 +1448,7 @@ function mock($pattern,
if (empty($parts[4]))
user_error(sprintf(self::E_Pattern,$pattern),E_USER_ERROR);
$url=parse_url($parts[4]);
parse_str(@$url['query'],$GLOBALS['_GET']);
parse_str(isset($url['query'])?$url['query']:'',$GLOBALS['_GET']);
if (preg_match('/GET|HEAD/',$verb))
$GLOBALS['_GET']=array_merge($GLOBALS['_GET'],$args);
$GLOBALS['_POST']=$verb=='POST'?$args:[];
Expand Down Expand Up @@ -1775,7 +1799,7 @@ function($id) use($args) {
++$ctr;
if ($ctr/$kbps>($elapsed=microtime(TRUE)-$now) &&
!connection_aborted())
usleep(1e6*($ctr/$kbps-$elapsed));
usleep(round(1e6*($ctr/$kbps-$elapsed)));
echo $part;
}
}
Expand Down Expand Up @@ -2382,7 +2406,7 @@ function($level,$text,$file,$line) {
$req.='?'.$query;
}
$_SERVER['REQUEST_URI']=$req;
parse_str($query,$GLOBALS['_GET']);
parse_str($query?:'',$GLOBALS['_GET']);
}
elseif (function_exists('getallheaders')) {
foreach (getallheaders() as $key=>$val) {
Expand Down Expand Up @@ -2705,7 +2729,7 @@ function reset($suffix=NULL) {
if (!$this->dsn)
return TRUE;
$regex='/'.preg_quote($this->prefix.'.','/').'.*'.
preg_quote($suffix,'/').'/';
preg_quote($suffix?:'','/').'/';
$parts=explode('=',$this->dsn,2);
switch ($parts[0]) {
case 'apc':
Expand Down
2 changes: 1 addition & 1 deletion basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function count() {
**/
function save() {
if (!$this->id)
$this->id=uniqid(NULL,TRUE);
$this->id=uniqid('',TRUE);
$_SESSION[$this->key][$this->id]=$this->item;
return $this->item;
}
Expand Down
1 change: 1 addition & 0 deletions db/cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ abstract function copyto($key);
* Causes a fatal error in PHP 5.3.5 if uncommented
* return ArrayIterator
**/
#[\ReturnTypeWillChange]
abstract function getiterator();


Expand Down
2 changes: 1 addition & 1 deletion db/jig/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function insert() {
return $this->update();
$db=$this->db;
$now=microtime(TRUE);
while (($id=uniqid(NULL,TRUE)) &&
while (($id=uniqid('',TRUE)) &&
($data=&$db->read($this->file)) && isset($data[$id]) &&
!connection_aborted())
usleep(mt_rand(0,100));
Expand Down
2 changes: 1 addition & 1 deletion db/mongo/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) {
);
$tmp=$this->db->selectcollection(
$fw->HOST.'.'.$fw->BASE.'.'.
uniqid(NULL,TRUE).'.tmp'
uniqid('',TRUE).'.tmp'
);
$tmp->batchinsert($grp['retval'],['w'=>1]);
$filter=[];
Expand Down
26 changes: 15 additions & 11 deletions image.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ function resize($width=NULL,$height=NULL,$crop=TRUE,$enlarge=TRUE) {
if ($width/$ratio<=$height) {
$cropw=round($origh*$width/$height);
imagecopyresampled($tmp,$this->data,
0,0,($origw-$cropw)/2,0,$width,$height,$cropw,$origh);
0,0,round(($origw-$cropw)/2),0,$width,$height,$cropw,$origh);
}
else {
$croph=round($origw*$height/$width);
imagecopyresampled($tmp,$this->data,
0,0,0,($origh-$croph)/2,$width,$height,$origw,$croph);
0,0,0,round(($origh-$croph)/2),$width,$height,$origw,$croph);
}
}
else
Expand Down Expand Up @@ -309,13 +309,13 @@ function overlay(Image $img,$align=NULL,$alpha=100) {
if ($align & self::POS_Left)
$posx=0;
if ($align & self::POS_Center)
$posx=($imgw-$ovrw)/2;
$posx=round(($imgw-$ovrw)/2);
if ($align & self::POS_Right)
$posx=$imgw-$ovrw;
if ($align & self::POS_Top)
$posy=0;
if ($align & self::POS_Middle)
$posy=($imgh-$ovrh)/2;
$posy=round(($imgh-$ovrh)/2);
if ($align & self::POS_Bottom)
$posy=$imgh-$ovrh;
if (empty($posx))
Expand Down Expand Up @@ -374,10 +374,14 @@ function identicon($str,$size=64,$blocks=4) {
$block=$sprites[hexdec($hash[($j*$blocks+$i)*2])%$ctr];
for ($k=0,$pts=count($block);$k<$pts;++$k)
$block[$k]*=$dim;
imagefilledpolygon($sprite,$block,$pts/2,$fg);
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
imagefilledpolygon($sprite,$block,$fg);
} else {
imagefilledpolygon($sprite,$block,$pts/2,$fg);
}
for ($k=0;$k<4;++$k) {
imagecopyresampled($this->data,$sprite,
$i*$dim/2,$j*$dim/2,0,0,$dim/2,$dim/2,$dim,$dim);
round($i*$dim/2),round($j*$dim/2),0,0,round($dim/2),round($dim/2),$dim,$dim);
$this->data=imagerotate($this->data,90,
imagecolorallocatealpha($this->data,0,0,0,127));
}
Expand Down Expand Up @@ -424,25 +428,25 @@ function captcha($font,$size=24,$len=5,
$char=imagecreatetruecolor($block,$block);
imagefill($char,0,0,$bg);
imagettftext($char,$size*2,0,
($block-$w)/2,$block-($block-$h)/2,
round(($block-$w)/2),round($block-($block-$h)/2),
$fg,$path,$seed[$i]);
$char=imagerotate($char,mt_rand(-30,30),
imagecolorallocatealpha($char,0,0,0,127));
// Reduce to normal size
$tmp[$i]=imagecreatetruecolor(
($w=imagesx($char))/2,($h=imagesy($char))/2);
round(($w=imagesx($char))/2),round(($h=imagesy($char))/2));
imagefill($tmp[$i],0,0,IMG_COLOR_TRANSPARENT);
imagecopyresampled($tmp[$i],
$char,0,0,0,0,$w/2,$h/2,$w,$h);
$char,0,0,0,0,round($w/2),round($h/2),$w,$h);
imagedestroy($char);
$width+=$i+1<$len?$block/2:$w/2;
$height=max($height,$h/2);
}
$this->data=imagecreatetruecolor($width,$height);
$this->data=imagecreatetruecolor(round($width),round($height));
imagefill($this->data,0,0,IMG_COLOR_TRANSPARENT);
for ($i=0;$i<$len;++$i) {
imagecopy($this->data,$tmp[$i],
$i*$block/2,($height-imagesy($tmp[$i]))/2,0,0,
round($i*$block/2),round(($height-imagesy($tmp[$i]))/2),0,0,
imagesx($tmp[$i]),imagesy($tmp[$i]));
imagedestroy($tmp[$i]);
}
Expand Down
4 changes: 4 additions & 0 deletions magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ abstract function clear($key);
* @return mixed
* @param $key string
**/
#[\ReturnTypeWillChange]
function offsetexists($key) {
return Base::instance()->visible($this,$key)?
isset($this->$key):
Expand All @@ -69,6 +70,7 @@ function offsetexists($key) {
* @param $key string
* @param $val mixed
**/
#[\ReturnTypeWillChange]
function offsetset($key,$val) {
return Base::instance()->visible($this,$key)?
($this->$key=$val):$this->set($key,$val);
Expand All @@ -79,6 +81,7 @@ function offsetset($key,$val) {
* @return mixed
* @param $key string
**/
#[\ReturnTypeWillChange]
function &offsetget($key) {
if (Base::instance()->visible($this,$key))
$val=&$this->$key;
Expand All @@ -92,6 +95,7 @@ function &offsetget($key) {
* @return NULL
* @param $key string
**/
#[\ReturnTypeWillChange]
function offsetunset($key) {
if (Base::instance()->visible($this,$key))
unset($this->$key);
Expand Down
2 changes: 1 addition & 1 deletion smtp.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function send($message,$log=TRUE,$mock=FALSE) {
unset($headers['Content-Type']);
$enc=$headers['Content-Transfer-Encoding'];
unset($headers['Content-Transfer-Encoding']);
$hash=uniqid(NULL,TRUE);
$hash=uniqid('',TRUE);
// Send mail headers
$out='Content-Type: multipart/mixed; boundary="'.$hash.'"'.$eol;
foreach ($headers as $key=>$val)
Expand Down
2 changes: 1 addition & 1 deletion template.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function _set(array $node) {
$out='';
foreach ($node['@attrib'] as $key=>$val)
$out.='$'.$key.'='.
(preg_match('/\{\{(.+?)\}\}/',$val)?
(preg_match('/\{\{(.+?)\}\}/',$val?:'')?
$this->token($val):
Base::instance()->stringify($val)).'; ';
return '<?php '.$out.'?>';
Expand Down
4 changes: 2 additions & 2 deletions web.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function send($file,$mime=NULL,$kbps=0,$force=TRUE,$name=NULL,$flush=TRUE) {
// Throttle output
++$ctr;
if ($ctr/$kbps>$elapsed=microtime(TRUE)-$start)
usleep(1e6*($ctr/$kbps-$elapsed));
usleep(round(1e6*($ctr/$kbps-$elapsed)));
}
// Send 1KiB and reset timer
echo fread($handle,1024);
Expand Down Expand Up @@ -1006,7 +1006,7 @@ function gzdecode($str) {
if (!is_dir($tmp=$fw->TEMP))
mkdir($tmp,Base::MODE,TRUE);
file_put_contents($file=$tmp.'/'.$fw->SEED.'.'.
$fw->hash(uniqid(NULL,TRUE)).'.gz',$str,LOCK_EX);
$fw->hash(uniqid('',TRUE)).'.gz',$str,LOCK_EX);
ob_start();
readgzfile($file);
$out=ob_get_clean();
Expand Down

0 comments on commit aa07384

Please sign in to comment.