81 lines
4.9 KiB
PHP
81 lines
4.9 KiB
PHP
<?php
|
|
namespace App\Services\PaymentHealth;
|
|
|
|
class QrEvidence
|
|
{
|
|
public function valid(string $html,string $type,int $cents): bool
|
|
{
|
|
$plain=html_entity_decode(strip_tags($html),ENT_QUOTES|ENT_HTML5);
|
|
$amount=number_format($cents/100,2,'.','');
|
|
$amountSeen=preg_match('/(?<![0-9.])'.preg_quote($amount,'/').'(?![0-9.])/u',$plain)
|
|
|| preg_match('/(?:money|amount|price)["\x27\s:=]+'.preg_quote($amount,'/').'(?![0-9.])/i',$html);
|
|
if(!$amountSeen)return false;
|
|
$uri=$type==='wxpay'?'(?:wxpay:\/\/|weixin:\/\/wxpay\/)':'(?:alipays:\/\/|https:\/\/qr\.alipay\.com\/)';
|
|
$paymentUriSeen=(bool)preg_match('~'.$uri.'[^\s"\x27<>]+~i',$html);
|
|
// This provider renders WeChat QR codes as an HTTPS short link. Inspect the
|
|
// complete URL so lookalike hosts or a URL embedded in another URL fail.
|
|
if(!$paymentUriSeen&&$type==='wxpay'&&preg_match_all('~https://[^\s"\x27<>\\\\]+~i',html_entity_decode($html,ENT_QUOTES|ENT_HTML5),$links)){
|
|
foreach($links[0] as $link){
|
|
$parts=parse_url($link);
|
|
if($parts!==false&&strtolower($parts['scheme']??'')==='https'&&strtolower($parts['host']??'')==='wxmpurl.cn'
|
|
&&!isset($parts['user'])&&!isset($parts['pass'])&&!isset($parts['port'])&&!isset($parts['fragment'])
|
|
&&($parts['path']??'/')!=='/'){$paymentUriSeen=true;break;}
|
|
}
|
|
}
|
|
if(!$paymentUriSeen)return false;
|
|
// Structured rendering of a real payment URI, or a QR bitmap with three finder patterns.
|
|
if(preg_match('~(?:new\s+QRCode|\.qrcode\s*\(|QRCode\.to(?:Canvas|DataURL|String))~i',$html))return true;
|
|
if(preg_match_all('~data:image/png;base64,([A-Za-z0-9+/=]+)~',$html,$matches))foreach($matches[1]as$data){
|
|
$bytes=base64_decode($data,true);if($bytes!==false&&strlen($bytes)<=524288&&$this->hasFinders($bytes))return true;
|
|
}
|
|
return false;
|
|
}
|
|
public function hasFinders(string $png): bool
|
|
{
|
|
if(!function_exists('imagecreatefromstring'))return false;
|
|
if(strlen($png)>524288)return false;
|
|
$size=@getimagesizefromstring($png);if(!$size)return false;
|
|
[$w,$h]=$size;
|
|
if(($size[2]??null)!==IMAGETYPE_PNG||$w<21||$h<21||$w>2048||$h>2048||abs($w-$h)>2)return false;
|
|
$im=@imagecreatefromstring($png);if(!$im)return false;
|
|
try{
|
|
if($w>600){
|
|
$factor=(int)ceil($w/600);$nw=(int)floor($w/$factor);$nh=(int)floor($h/$factor);
|
|
$normalized=imagecreatetruecolor($nw,$nh);if(!$normalized)return false;
|
|
imagecopyresized($normalized,$im,0,0,0,0,$nw,$nh,$w,$h);
|
|
imagedestroy($im);$im=$normalized;$w=$nw;$h=$nh;
|
|
}
|
|
$black=static function(int $x,int $y)use($im):bool{$c=imagecolorat($im,$x,$y);$rgb=imagecolorsforindex($im,$c);return ($rgb['red']+$rgb['green']+$rgb['blue'])<384;};
|
|
$points=[];
|
|
for($y=0;$y<$h;$y+=2){$runs=[];$last=false;$len=0;
|
|
for($x=0;$x<=$w;$x++){$current=$x<$w?$black($x,$y):!$last;
|
|
if($x===0){$last=$current;$len=1;continue;}if($current===$last){$len++;continue;}
|
|
$runs[]=['black'=>$last,'len'=>$len,'end'=>$x];if(count($runs)>5)array_shift($runs);
|
|
if(count($runs)===5&&$runs[0]['black']&&!$runs[1]['black']&&$runs[2]['black']&&!$runs[3]['black']&&$runs[4]['black']){
|
|
$r=array_column($runs,'len');$module=array_sum($r)/7;
|
|
if($this->ratio($r,$module)){$cx=(int)round($x-$r[4]-$r[3]-$r[2]/2);if($this->vertical($black,$cx,$y,$h,$module)){
|
|
$duplicate=false;foreach($points as$p)if(hypot($cx-$p[0],$y-$p[1])<8*$module){$duplicate=true;break;}
|
|
if(!$duplicate)$points[]=[$cx,$y];
|
|
}}
|
|
}$last=$current;$len=1;
|
|
}
|
|
}
|
|
foreach($points as$a)foreach($points as$b)foreach($points as$c){$ab=[$b[0]-$a[0],$b[1]-$a[1]];$ac=[$c[0]-$a[0],$c[1]-$a[1]];$u=hypot(...$ab);$v=hypot(...$ac);
|
|
if($u>12&&$v>12&&min($u,$v)/max($u,$v)>.65&&abs($ab[0]*$ac[0]+$ab[1]*$ac[1])/($u*$v)<.25)return true;}
|
|
return false;
|
|
}finally{imagedestroy($im);}
|
|
}
|
|
private function ratio(array $r,float $m):bool
|
|
{if($m<1)return false;foreach([0,1,3,4]as$i)if(abs($r[$i]-$m)>$m*.75)return false;return abs($r[2]-3*$m)<$m*1.25;}
|
|
private function vertical(callable $black,int $x,int $y,int $h,float $m):bool
|
|
{
|
|
$r=[0,0,0,0,0];for($i=$y;$i>=0&&$black($x,$i);$i--)$r[2]++;
|
|
for(;$i>=0&&!$black($x,$i);$i--)$r[1]++;
|
|
for(;$i>=0&&$black($x,$i);$i--)$r[0]++;
|
|
for($i=$y+1;$i<$h&&$black($x,$i);$i++)$r[2]++;
|
|
for(;$i<$h&&!$black($x,$i);$i++)$r[3]++;
|
|
for(;$i<$h&&$black($x,$i);$i++)$r[4]++;
|
|
return $this->ratio($r,array_sum($r)/7)&&abs(array_sum($r)/7-$m)<$m;
|
|
}
|
|
}
|