Minor changes

This commit is contained in:
pezcurrel 2024-08-07 17:30:42 +02:00
parent 3f08fbbca2
commit ccd4a22688

View file

@ -692,8 +692,6 @@ if ($instanswered && isset($idata['languages']) && is_array($idata['languages'])
// is equal to the default "en", otherwise we assume it's been set to the actual mostly used language on the instance
if ($idata['languages'][0]=='en') {
$idata['ourlanguages']=get_instance_langs($opts['hostname']);
while (count($idata['ourlanguages'])>5)
array_pop($idata['ourlanguages']);
if (count($idata['ourlanguages'])>0) {
eecho(1,'«'.$opts['hostname'].'»: detected languages: '.implode(', ',$idata['ourlanguages']).N);
} else {
@ -789,25 +787,30 @@ if (isset($idata['blocks']) && is_array($idata['blocks'])) {
if ($opts['sendtoot'] && $instanswered && $idata['IsMastodon'] && $idata['FirstSeen']==$now) {
gettlds();
eecho(0,'«'.$opts['hostname'].'»: this instance is new, trying to send a toot about it...'.N);
eecho(0,'«'.$opts['hostname'].'»: this instance is new, trying to send an announcement toot about it...'.N);
$endpoint='https://'.$iniarr['bothost'].'/api/v1/statuses';
$toot='A new Mastodon instance, https://'.$opts['hostname'].', has been found by the crawler at https://mastodon.help'.N.N.'Name: ';
(is_null($idata['Title'])) ? $toot.='unspecified' : $toot.=$idata['Title'];
$toot.=N.N.'Languages: ';
(array_key_exists('languages',$idata) && is_array($idata['languages']) && count($idata['languages'])>0) ? $toot.=implode($idata['languages']) : $toot.='unspecified/undetectable';
if (array_key_exists('languages',$idata) && is_array($idata['languages']) && count($idata['languages'])>0 && $idata['languages'][0]!='en')
$toot.=implode($idata['languages']);
elseif (array_key_exists('ourlanguages',$idata) && is_array($idata['ourlanguages']) && count($idata['ourlanguages'])>0)
$toot.=implode($idata['ourlanguages']);
else
$toot.='unspecified/undetectable';
$toot.=N.N.'Users: ';
(is_null($idata['UserCount'])) ? $toot.='unspecified' : $toot.=$idata['UserCount'];
$toot.=N.N.'Max. characters per post: ';
(is_null($idata['MaxTootChars']) || $idata['MaxTootChars']==500) ? $toot.='500' : $toot.=$idata['MaxTootChars'];
$toot.=N.N.'Registration: ';
$toot.=N.N.'Registrations: ';
if (is_null($idata['RegOpen'])) {
$toot.='unspecified';
} elseif ($idata['RegOpen']==1) {
$toot.='open';
if ($idata['RegReqApproval']==1)
$toot.=' (requires admins approval)';
$toot.=' (require admins approval)';
elseif ($idata['RegReqApproval']==0)
$toot.=' (does not require admins approval)';
$toot.=' (dont require admins approval)';
} elseif ($idata['RegOpen']==0) {
$toot.='closed';
}
@ -837,10 +840,13 @@ if ($opts['sendtoot'] && $instanswered && $idata['IsMastodon'] && $idata['FirstS
$context=stream_context_create($context);
$res=@file_get_contents($endpoint,false,$context);
if ($res===false) {
eecho(2,'«'.$opts['hostname'].'»: could not connect to «'.$iniarr['bothost'].'».'.N);
eecho(2,'«'.$opts['hostname'].'»: trying to post a toot about this new instance, could not connect to «'.$iniarr['bothost'].'».'.N);
} else {
$httprsc=gethttpcode($http_response_header);
$res=@json_decode($res,true);
if ($res===false)
if ($httprsc!=200)
eecho(2,'«'.$opts['hostname'].'»: trying to post a toot about this new instance, «'.$iniarr['bothost'].'» has returned HTTP code «'.$httprsc.'».'.N);
elseif ($res===false)
eecho(2,'«'.$opts['hostname'].'»: trying to post a toot about this new instance, «'.$iniarr['bothost'].'» has not returned valid JSON data.'.N);
else
eecho(1,'«'.$opts['hostname'].'»: correctly posted announcement about this new instance on «'.$iniarr['bothost'].'» :-)'.N);
@ -1154,6 +1160,13 @@ function datetots($date) {
return(mktime(0,0,0,$date[1],$date[2],$date[0]));
}
function gethttpcode($httprh) {
if (is_array($httprh) && preg_match('#^\S+ (\d+) \S+.*$#',$httprh[0],$matches)===1)
return $matches[1]+0;
else
return 0;
}
function ckratelimit($httpresphead) {
$headers=explode("\r\n",$httpresphead);
$buff=[];
@ -1315,6 +1328,8 @@ function get_instance_langs($host) {
$detected_langs = array_map('get_toot_languages', $data);
$summary = summary($detected_langs);
$languages = get_languages($summary);
while (count($languages)>5)
array_pop($languages);
return $languages;
}
@ -1386,26 +1401,28 @@ function waituntilonline() {
}
function headcb($arr) {
$lpad='';
/* $lpad='';
$rpad='';
for ($i=0; $i<7-$arr[2]; $i++) {
$lpad.='[';
$rpad.=']';
}
return "{$lpad} ".mb_strtoupper($arr[4])." {$rpad}\n\n";
return "{$lpad} ".mb_strtoupper($arr[4])." {$rpad}\n\n";*/
return '§ '.mb_strtoupper($arr[4])."\n\n";
}
function html2text($html,$keeplinks=false) {
$html=preg_replace('#\r#','',$html);
//$html=preg_replace('#\n|\r#','',$html);
if ($keeplinks) $html=preg_replace('#<a\s.*href="(.*)".*>#iU',' [link: $1] ',$html);
$html=preg_replace('#<li>#i',' * ',$html);
$html=preg_replace('#<li>#i',' ',$html);
$html=preg_replace_callback('#(<h)(\d)(>)(.*)(</h\d>)#iU','headcb',$html);
$html=preg_replace('#<p>|<div>|<ul>|<ol>#i',"\n\n",$html);
$html=preg_replace('#</p>|</div>|</ul>|</ol>|</li>#i',"\n\n",$html);
$html=preg_replace('#<br>|<br />#i',"\n",$html);
$html=strip_tags($html);
$html=preg_replace('#\s\n#',"\n",$html);
$html=html_entity_decode($html,ENT_QUOTES,'UTF-8');
$html=preg_replace('#\s+\n#',"\n",$html);
$html=preg_replace('# +#',' ',$html);
$html=preg_replace('#\n+#',"\n\n",$html);
$html=trim($html)."\n";