1
0
Fork 0
MastodonStartpage/web/admin/crawler/crawler.php
2019-12-28 22:51:41 +01:00

646 lines
24 KiB
PHP
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/php
<?php
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define('N',"\n");
$link=false;
$logf=false;
$jsonf=false;
declare(ticks=1);
pcntl_signal(SIGTERM,'signalHandler');// Termination ('kill' was called)
pcntl_signal(SIGHUP,'signalHandler');// Terminal log-out
pcntl_signal(SIGINT,'signalHandler');// Interrupted (Ctrl-C is pressed)
function signalHandler($signal) {
global $link, $logf, $jsonf;
lecho(N.'Sono stato interrotto.'.N);
if ($link) {
lecho('La connessione MySQL è aperta, la chiudo.'.N);
mysqli_close($link);
}
if ($jsonf) {
echo('Il file di dump json è aperto, lo chiudo.'.N);
fwrite($jsonf,'"Fine?": true'.N.'}'.N);
fclose($jsonf);
}
if ($logf) {
echo('Il file di log è aperto, lo chiudo.'.N);
fclose($logf);
}
exit(2);
}
$opts=array(
'timeout'=>3,
'log'=>true,
'jsonfp'=>'instances.json',
'jsonwrite'=>true,
'jsonread'=>false
);
use function mysqli_real_escape_string as myesc;
function tosec($str) {
if (preg_match('/^([0-9]+)([smogSMA]?)/',$str,$buf)===1) {
switch ($buf[2]) {
case '':
case 's':
return($buf[1]);
break;
case 'm':
return($buf[1]*60);
break;
case 'o':
return($buf[1]*60*60);
break;
case 'g':
return($buf[1]*60*60*24);
break;
case 'S':
return($buf[1]*60*60*24*7);
break;
case 'M':
return($buf[1]*60*60*24*30);
break;
case 'A':
return($buf[1]*60*60*24*365);
break;
}
} else {
return(false);
}
}
function mexit($msg,$code) {
global $link, $jsonf, $logf;
lecho($msg);
if ($link)
mysqli_close($link);
if ($jsonf)
fclose($jsonf);
if ($logf)
fclose($logf);
exit($code);
}
function lecho($msg,$logonly=false) {
global $opts, $logf;
if (!$logonly)
echo($msg);
if ($opts['log'])
fwrite($logf,$msg);
}
$logfp='crawler.log';
if ($opts['log']) {
$logf=@fopen(__DIR__.'/'.$logfp,'w')
or mexit('Non ho potuto aprire in scrittura il file di log «'.$logfp.'».',1);
}
$inifp='../sec/mastostartadmin.ini';
$iniarr=parse_ini_file($inifp)
or mexit('Impossibile aprire il file di configurazione «'.$inifp.'»'.N,1);
$link=mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket'])
or mexit(mysqli_error($link).N,1);
mysqli_set_charset($link,'utf8mb4')
or mexit(mysqli_error($link).N,1);
$tables=array();
$res=mysqli_query($link,'SHOW TABLES')
or mexit(mysqli_error($link).N,1);
while ($row=mysqli_fetch_row($res)) {
$resb=mysqli_query($link,'SHOW COLUMNS FROM '.$row[0])
or mexit(mysqli_error($link).N,1);
$fields=array();
while ($rowb=mysqli_fetch_assoc($resb)) {
preg_match('/\((.*)\)/',$rowb['Type'],$buf);
$fields[$rowb['Field']]=$buf[1];
}
$tables[$row[0]]=$fields;
}
function trunc($str,$tab,$col) {
global $tables;
$size=$tables[$tab][$col];
if (mb_strlen($str,'UTF-8')>$size) {
$str=mb_substr($str,0,$size-1,'UTF-8').'…';
notify('Ho dovuto troncare a '.$size.' caratteri il valore da inserire nella colonna «'.$col.'» della tabella «'.$tab.'» perché troppo lungo.',2);
}
return($str);
}
$contextopts=array(
'http'=>array(
'timeout'=>$opts['timeout']
),
'socket'=>array(
'tcp_nodelay'=>true
)
);
$context=stream_context_create($contextopts);
$blacklist=array();
lecho('Carico la blacklist dal database...'.N);
$res=mysqli_query($link,'SELECT * FROM Blacklist')
or mexit(mysqli_error($link).N,3);
lecho(mysqli_num_rows($res).' istanze nella blacklist.'.N);
while($row=mysqli_fetch_assoc($res)) {
$blacklist[$row['Domain']]=$row;
}
function pgdatetomy($pgdate) {
if (preg_match('/^(\d+)-(\d+)-(\d+)[ T]{1}(\d+):(\d+):(\d+)(\.\d+)?Z?$/',$pgdate,$buf)===1) {
$mtime=mktime($buf[4],$buf[5],$buf[6],$buf[2],$buf[3],$buf[1]);
if (array_key_exists(7,$buf))
$mtime=$mtime+floatval('0'.$buf[7]);
return($mtime);
} else {
return(false);
}
}
function blpgdumplinetomy($line) {
$truefalse=array('f'=>0,'t'=>1);
$row=explode("\t",$line);
$row=array('Domain'=>$row[0],
'CreatedAt'=>pgdatetomy($row[1]),
'ModifiedAt'=>pgdatetomy($row[2]),
'Severity'=>$row[3],
'RejectMedia'=>$truefalse[$row[4]],
'RejectReports'=>$truefalse[$row[5]],
'PublicComment'=>$row[6]);
return($row);
}
$blacklistnew=array();
$insts=array();
lecho('Carico le istanze di partenza...'.N);
$res=mysqli_query($link,'SELECT Domain FROM StartNodes')
or mexit(mysqli_error($link).N,3);
lecho(mysqli_num_rows($res).' istanze di partenza.'.N);
while($row=mysqli_fetch_assoc($res)) {
$insts[$row['Domain']]=null;
lecho('Recupero la lista delle istanze note a «'.$row['Domain'].'» ... ');
$buf=@file_get_contents('https://'.$row['Domain'].'/api/v1/instance/peers',false,$context);
if ($buf!==false) {
lecho('OK :-)'.N);
$peers=json_decode($buf,true);
foreach ($peers as $pdom) {
if (!array_key_exists($pdom,$insts) && strlen($pdom)<=64) {
$insts[$pdom]=null;
}
}
} else {
lecho('ERRORE :-('.N);
}
lecho('Recupero la blacklist di «'.$row['Domain'].'» ... ');
$buf=@file_get_contents('https://'.$row['Domain'].'/domain_blocks.txt',false,$context);
if ($buf!==false) {
lecho('OK :-)'.N);
$buf=explode(N,$buf);
foreach ($buf as $line) {
if (preg_match('/(^#.*$)|(^\s*$)/',$line)===0) {
$brow=blpgdumplinetomy($line);
if (!array_key_exists($brow['Domain'],$blacklist)) {
$blacklistnew[$brow['Domain']]=$brow;
}
$blacklist[$brow['Domain']]=$brow;
}
}
} else {
lecho('ERRORE :-('.N);
}
}
//lecho('Carico le istanze note dal DB e aggiungo alla lista di quelle da controllare quelle che non ci sono già.'.N);
$res=mysqli_query($link,'SELECT URI FROM Instances')
or mexit(mysqli_error($link).N,3);
while($row=mysqli_fetch_assoc($res)) {
if (!array_key_exists($row['URI'],$insts))
$insts[$row['URI']]=null;
}
ksort($insts);
ksort($blacklist);
ksort($blacklistnew);
lecho('Istanze recuperate: '.count($insts).N);
lecho('Istanze blacklistate: '.count($blacklist).', di cui '.count($blacklistnew).' nuove da aggiungere al DB.'.N);
foreach ($blacklistnew as $row) {
foreach($row as $key=>$val)
$row[$key]=myesc($link,$val);
mysqli_query($link,'INSERT INTO Blacklist (ID, Domain, CreatedAt, ModifiedAt, Severity, RejectMedia, RejectReports, PrivateComment, PublicComment) VALUES (NULL, \''.trunc($row['Domain'],'Blacklist','Domain').'\', \''.$row['CreatedAt'].'\', \''.$row['ModifiedAt'].'\', \''.$row['Severity'].'\', \''.$row['RejectMedia'].'\', \''.$row['RejectReports'].'\', NULL, \''.trunc($row['PublicComment'],'Blacklist','PublicComment').'\')')
or mexit(mysqli_error($link).N,3);
}
function b2i($bool) {
if ($bool)
return(1);
else
return(0);
}
//is array, array key exists and value is not null
function akeavinn($key,&$arr) {
if (is_array($arr) && array_key_exists($key,$arr) && !is_null($arr[$key]))
return(true);
else
return(false);
}
function nempty($str) {
if (preg_match('/^\s*$/',$str)===1)
return(null);
else
return($str);
}
function subarim($glue,$key,&$arr) {
$str='';
$i=1;
$carr=count($arr);
foreach ($arr as $inarr) {
$str.=$inarr[$key];
if ($i<$carr)
$str.=$glue;
$i++;
}
return($str);
}
function notify($msg,$sev) {
global $link, $tables;
mysqli_query($link,'INSERT INTO Notifications (ID, Notification, Severity, Microtime) VALUES (NULL, \''.myesc($link,mb_substr($msg,0,$tables['Notifications']['Notification'],'UTF-8')).'\', '.$sev.', \''.microtime(true).'\')')
or mexit(mysqli_error($link).N,3);
}
function langs($isnewinst,$instid) {
global $info, $instrow, $link;
$instlangs=array();
if (akeavinn('languages',$info)) {
$pos=0;
foreach ($info['languages'] as $lang) {
$res=mysqli_query($link,'SELECT * FROM Languages WHERE Code=\''.myesc($link,$lang).'\'')
or mexit(mysqli_error($link).N,3);
if (mysqli_num_rows($res)<1) {
mysqli_query($link,'INSERT INTO Languages (ID, Code, Name) VALUES (NULL, \''.myesc($link,trunc($lang,'Languages','Code')).'\', NULL)')
or mexit(mysqli_error($link).N,3);
$langid=mysqli_insert_id($link);
if ($isnewinst) {
$subj='Linserimento';
} else {
$subj='Laggiornamento';
}
notify($subj.' dei dati relativi allistanza «<a href="editinst.php?id='.$instid.'">'.$instrow['URI'].'</a>» ha aggiunto un codice lingua non ancora noto, «'.$lang.'», di cui non conosco il nome per esteso. Puoi <a href="editlang.php?id='.$langid.'">editarlo qui</a>.',1);
} else {
$row=mysqli_fetch_assoc($res);
$langid=$row['ID'];
}
$pos++;
$instlangs[]=array('InstID'=>$instid,'LangID'=>$langid,'Pos'=>$pos,'Code'=>$lang);
}
}
return($instlangs);
}
function varbdump($var) {
ob_start();
var_dump($var);
$content=ob_get_contents();
ob_end_clean();
return($content);
}
function mdasortbykey(&$arr,$key,$rev=false) {
$karr=array();
foreach ($arr as $akey=>$subarr)
$karr[$subarr[$key]]=array($akey,$subarr);
if (!$rev)
ksort($karr);
else
krsort($karr);
$arr=array();
foreach ($karr as $akey=>$subarr)
$arr[$subarr[0]]=$subarr[1];
}
/*
* Nodeinfo ('https://'.$dom.'/nodeinfo/2.0') è stato aggiunto nella 3.0.0
* Trends ('https://'.$dom.'/api/v1/trends') è stato aggiunto nella 3.0.0
* Activity ('https://'.$dom.'/api/v1/instance/activity') è stato aggiunto nella 2.1.2
*/
if ($opts['jsonwrite']) {
$jsonf=@fopen(__DIR__.'/'.$opts['jsonfp'],'w')
or mexit('Non ho potuto aprire in scrittura il file di dump delle info json «'.$opts['jsonfp'].'».',1);
fwrite($jsonf,'{'.N);
}
$cinsts=count($insts);
$i=0;
$qok=0;
$qgood=0;
foreach ($insts as $dom=>$row) {
$i++;
$ok=true;
$info=null;
lecho('~~~~~~~~~~~~~~~'.N);
lecho('Provo a recuperare info su «'.$dom.'» ['.$i.'/'.$cinsts.' ('.$qok.' OK; '.$qgood.' BUONE) - '.round(100/$cinsts*$i).'%]'.N);
lecho('Provo a recuperare le informazioni API sullistanza ... ');
$buf=@file_get_contents('https://'.$dom.'/api/v1/instance',false,$context);
if ($buf!==false) {
$info=json_decode($buf,true);
if (is_array($info)) {
lecho('OK :-)'.N);
lecho('Provo a recuperare le informazioni Nodeinfo sullistanza ... ');
$buf=@file_get_contents('https://'.$dom.'/nodeinfo/2.0',false,$context);
if ($buf!==false) {
lecho('OK :-)'.N);
$info['x-nodeinfo']=json_decode($buf,true);
if (is_array($info['x-nodeinfo']) && array_key_exists('software',$info['x-nodeinfo']) && array_key_exists('name',$info['x-nodeinfo']['software']) && preg_match('/^mastodon|corgidon/',$info['x-nodeinfo']['software']['name'])===0) {
$ok=false;
lecho('Il software «'.$info['x-nodeinfo']['software']['name'].'» non è mastodon o derivati.'.N);
}
} else {
lecho('ERRORE :-('.N);
}
if ($ok && array_key_exists('version',$info)) {
if ($info['version']>='2.1.2') {
lecho('Provo a recuperare le informazioni API sullattività dellistanza ... ');
$buf=@file_get_contents('https://'.$dom.'/api/v1/instance/activity',false,$context);
if ($buf!==false) {
lecho('OK :-)'.N);
$info['x-activity']=json_decode($buf,true);
} else {
lecho('ERRORE :-('.N);
}
}
if ($info['version']>='3.0.0') {
lecho('Provo a recuperare le informazioni API sui trends dellistanza ... ');
$buf=@file_get_contents('https://'.$dom.'/api/v1/trends',false,$context);
if ($buf!==false) {
lecho('OK :-)'.N);
$info['x-trends']=json_decode($buf,true);
} else {
lecho('ERRORE :-('.N);
}
}
}
} else {
$ok=false;
lecho('ERRORE :-('.N);
}
} else {
$ok=false;
lecho('ERRORE :-('.N);
// questo è anche il limbo delle istanze che non rispondono, perciò controlliamo se già esistono nel db e, nel caso, aggiorniamo InstChecks
$res=mysqli_query($link,'SELECT * FROM Instances WHERE URI=\''.myesc($link,mb_substr($dom,0,$tables['Instances']['URI'],'UTF-8')).'\'')
or mexit(mysqli_error($link).N,3,true);
if (mysqli_num_rows($res)>0) {
lecho('«'.$dom.'» non risponde, ma è presente nel database; aggiorno InstChecks.');
$row=mysqli_fetch_assoc($res);
mysqli_query($link,'INSERT INTO InstChecks (InstID, Time, Status) VALUES ('.$row['ID'].', '.time().', 0)')
or mexit(mysqli_error($link).N,3,true);
}
}
if ($ok && !is_null($info) && akeavinn('uri',$info) && !is_null(nempty($info['uri'])) && akeavinn('version',$info) && preg_match('/pleroma|pixelfed/i',$info['version'])===0) {
$qok++;
lecho(json_encode($info,JSON_PRETTY_PRINT).N,true);
if ($opts['jsonwrite'])
fwrite($jsonf,'"'.$info['uri'].'": '.json_encode($info,JSON_PRETTY_PRINT).','.N);
$instrow=array('ID'=>null, 'New'=>0, 'Good'=>0, 'Chosen'=>0, 'Visible'=>0, 'BlackListed'=>0, 'URI'=>null, 'Title'=>null, 'ShortDesc'=>null, 'LongDesc'=>null, 'OurDesc'=>null, 'PlaceID'=>null, 'Email'=>null, 'Software'=>null, 'Version'=>null, 'UserCount'=>null, 'StatusCount'=>null, 'DomainCount'=>null, 'ActiveUsersMonth'=>null, 'ActiveUsersHalfYear'=>null, 'Thumb'=>null, 'RegOpen'=>null, 'RegReqApproval'=>null, 'MaxTootChars'=>null, 'AdmAccount'=>null, 'AdmDisplayName'=>null, 'AdmCreatedAt'=>null, 'AdmNote'=>null, 'AdmURL'=>null, 'AdmAvatar'=>null, 'AdmHeader'=>null);
if (array_key_exists($info['uri'],$blacklist))
$instrow['BlackListed']=1;
$instrow['URI']=nempty(trunc($info['uri'],'Instances','URI'));
if (akeavinn('title',$info))
$instrow['Title']=nempty(trunc($info['title'],'Instances','Title'));
if (akeavinn('short_description',$info))
$instrow['ShortDesc']=nempty(trunc($info['short_description'],'Instances','ShortDesc'));
if (akeavinn('description',$info))
$instrow['LongDesc']=nempty(trunc($info['description'],'Instances','LongDesc'));
if (akeavinn('email',$info))
$instrow['Email']=nempty(trunc($info['email'],'Instances','Email'));
if (akeavinn('version',$info))
$instrow['Version']=nempty(trunc($info['version'],'Instances','Version'));
if (akeavinn('stats',$info)) {
if (akeavinn('user_count',$info['stats']))
$instrow['UserCount']=$info['stats']['user_count'];
if (akeavinn('status_count',$info['stats']))
$instrow['StatusCount']=$info['stats']['status_count'];
if (akeavinn('domain_count',$info['stats']))
$instrow['DomainCount']=$info['stats']['domain_count'];
}
if (akeavinn('thumbnail',$info))
$instrow['Thumb']=nempty(trunc($info['thumbnail'],'Instances','Thumb'));
if (akeavinn('max_toot_chars',$info))
$instrow['MaxTootChars']=$info['max_toot_chars'];
if (akeavinn('registrations',$info))
$instrow['RegOpen']=b2i($info['registrations']);
if (akeavinn('approval_required',$info))
$instrow['RegReqApproval']=b2i($info['approval_required']);
if (akeavinn('contact_account',$info)) {
if (akeavinn('acct',$info['contact_account']))
$instrow['AdmAccount']=nempty(trunc($info['contact_account']['acct'],'Instances','AdmAccount'));
if (akeavinn('display_name',$info['contact_account']))
$instrow['AdmDisplayName']=nempty(trunc($info['contact_account']['display_name'],'Instances','AdmAccount'));
if (akeavinn('created_at',$info['contact_account']))
$instrow['AdmCreatedAt']=pgdatetomy($info['contact_account']['created_at']);
if (akeavinn('note',$info['contact_account']))
$instrow['AdmNote']=nempty(trunc(strip_tags($info['contact_account']['note'],'<a>'),'Instances','AdmNote'));
if (akeavinn('url',$info['contact_account']))
$instrow['AdmURL']=nempty(trunc($info['contact_account']['url'],'Instances','AdmURL'));
if (akeavinn('avatar',$info['contact_account']))
$instrow['AdmAvatar']=nempty(trunc($info['contact_account']['avatar'],'Instances','AdmAvatar'));
if (akeavinn('header',$info['contact_account']))
$instrow['AdmHeader']=nempty(trunc($info['contact_account']['header'],'Instances','AdmHeader'));
}
if (akeavinn('x-nodeinfo',$info)) {
if (akeavinn('software',$info['x-nodeinfo']) && akeavinn('name',$info['x-nodeinfo']['software']))
$instrow['Software']=nempty(trunc($info['x-nodeinfo']['software']['name'],'Instances','Software'));
if (akeavinn('usage',$info['x-nodeinfo']) && akeavinn('users',$info['x-nodeinfo']['usage'])) {
if (akeavinn('activeMonth',$info['x-nodeinfo']['usage']['users']))
$instrow['ActiveUsersMonth']=$info['x-nodeinfo']['usage']['users']['activeMonth'];
if (akeavinn('activeHalfyear',$info['x-nodeinfo']['usage']['users']))
$instrow['ActiveUsersHalfYear']=$info['x-nodeinfo']['usage']['users']['activeHalfyear'];
}
}
$whynot=array();
if ($instrow['BlackListed']==1)
$whynot[]='è nella blacklist';
if (is_null($instrow['RegOpen'])) {
$whynot[]='non se ne conosce lo stato delle registrazioni (aperte/chiuse)';
} elseif ($instrow['RegOpen']==0) {
$whynot[]='ha le registrazioni chiuse';
}
if (is_null($instrow['UserCount'])) {
$whynot[]='non se ne conosce il numero di utenti';
} elseif ($instrow['UserCount']<10 || $instrow['UserCount']>30000) {
$whynot[]='il numero di utenti non è compreso tra 10 e 30.000';
}
if (is_null($instrow['DomainCount'])) {
$whynot[]='non se ne conosce il numero di istanze note';
} elseif ($instrow['DomainCount']<500) {
$whynot[]='il numero di istanze note è minore di 500';
}
if (!is_null($instrow['ActiveUsersMonth'])) {
if ($instrow['ActiveUsersMonth']<10)
$whynot[]='il numero di utenti attivi nellultimo mese è minore di 10';
} elseif (!is_null($instrow['StatusCount']) && $instrow['StatusCount']/$instrow['UserCount']<10) {
$whynot[]='il numero medio di toots per utente è minore di 10';
}
if (count($whynot)==0) {
$instrow['Good']=1;
lecho('Siamo in presenza di unistanza BUONA!'.N);
$qgood++;
}
$res=mysqli_query($link,'SELECT * FROM Instances WHERE URI=\''.myesc($link,$instrow['URI']).'\'')
or mexit(mysqli_error($link).N,3,true);
if (mysqli_num_rows($res)>0) {
lecho('«'.$instrow['URI'].'» è già presente nel DB, la aggiorno...'.N);
$oldinstrow=mysqli_fetch_assoc($res);
$instid=$oldinstrow['ID'];
$instrow['ID']=$oldinstrow['ID'];
$instrow['New']=$oldinstrow['New'];
if ($instrow['Good']==1 && $oldinstrow['Good']==0) {
notify('Listanza «<a href="editinst.php?id='.$instrow['ID'].'">'.$instrow['URI'].'</a>» non era papabile, ma lo è diventata!',1);
} elseif ($instrow['Good']==0 && $oldinstrow['Good']==1) {
notify('Listanza «<a href="editinst.php?id='.$instrow['ID'].'">'.$instrow['URI'].'</a>» era papabile, ma non lo è più per i seguenti motivi: '.implode('; ',$whynot),3);
}
$instrow['Chosen']=$oldinstrow['Chosen'];
$instrow['Visible']=$oldinstrow['Visible'];
if ($instrow['ShortDesc']!=$oldinstrow['ShortDesc'])
notify('<p>La «Descrizione breve» dellistanza «<a href="editinst.php?id='.$instrow['ID'].'">'.$instrow['URI'].'</a>» è cambiata da...</p><div class="valdesc">'.$oldinstrow['ShortDesc'].'</div><p>...a...</p><div class="valdesc">«'.$instrow['ShortDesc'].'</div>',1);
if ($instrow['LongDesc']!=$oldinstrow['LongDesc'])
notify('<p>La «Descrizione lunga» dellistanza «<a href="editinst.php?id='.$instrow['ID'].'">'.$instrow['URI'].'</a>» è cambiata da...</p><div class="valdesc">'.$oldinstrow['LongDesc'].'</div><p>...a...</p><div class="valdesc">«'.$instrow['LongDesc'].'</div>',1);
$instrow['PlaceID']=$oldinstrow['PlaceID'];
$query='UPDATE Instances SET ';
foreach ($instrow as $field=>$value) {
if (!is_null($value))
$query.=$field.'=\''.myesc($link,$value).'\', ';
else
$query.=$field.'=NULL, ';
}
$query=substr($query,0,-2).' WHERE Instances.ID='.$instrow['ID'];
lecho('QUERONA DI UPDATE: «'.$query.'».'.N);
mysqli_query($link,$query)
or mexit(mysqli_error($link).N,3,true);
$res=mysqli_query($link,'SELECT InstID, LangID, Pos, Code FROM InstLangs LEFT JOIN Languages ON Languages.ID=LangID WHERE InstID='.$instrow['ID'].' ORDER BY Pos ASC')
or mexit(mysqli_error($link).N,3,true);
$oldinstlangs=array();
while ($row=mysqli_fetch_assoc($res))
$oldinstlangs[]=$row;
$instlangs=langs(false,$instrow['ID']);
if ($instlangs!=$oldinstlangs) {
notify('La lista delle lingue utilizzate dichiarate dallistanza «<a href="editinst.php?id='.$instrow['ID'].'">'.$instrow['URI'].'</a>» è cambiata da «'.subarim(', ','Code',$oldinstlangs).'» a «'.subarim(', ','Code',$instlangs).'».',1);
mysqli_query($link,'DELETE FROM InstLangs WHERE InstID='.$instrow['ID'])
or mexit(mysqli_error($link).N,3,true);
foreach ($instlangs as $row) {
mysqli_query($link,'INSERT INTO InstLangs (InstID, LangID, Pos) VALUES ('.$row['InstID'].', '.$row['LangID'].', '.$row['Pos'].')')
or mexit(mysqli_error($link).N,3,true);
}
}
} else {
lecho('«'.$info['uri'].'» non è già presente nel DB, la aggiungo...'.N);
$instrow['New']=1;
$fields=array();
$values='';
foreach ($instrow as $field=>$value) {
$fields[]=$field;
if (!is_null($value))
$values.='\''.myesc($link,$value).'\', ';
else
$values.='NULL, ';
}
$values=substr($values,0,-2);
$query='INSERT INTO Instances ('.implode(', ',$fields).') VALUES ('.$values.')';
lecho('QUERONA DI INSERT: «'.$query.'»'.N);
mysqli_query($link,$query)
or mexit(mysqli_error($link).N,3,true);
$instid=mysqli_insert_id($link);
$instlangs=langs(false,$instid);
foreach ($instlangs as $row) {
mysqli_query($link,'INSERT INTO InstLangs (InstID, LangID, Pos) VALUES ('.$row['InstID'].', '.$row['LangID'].', '.$row['Pos'].')')
or mexit(mysqli_error($link).N,3,true);
}
}
if (array_key_exists('x-activity',$info) && is_array($info['x-activity'])) {
mysqli_query($link,'DELETE FROM InstActivity WHERE InstID='.$instid);
foreach ($info['x-activity'] as $buf) {
if (akeavinn('week',$buf) && akeavinn('statuses',$buf) && akeavinn('logins',$buf) && akeavinn('registrations',$buf)) {
$query='INSERT INTO InstActivity (InstID, Week, Statuses, Logins, Registrations) VALUES (\''.$instid.'\', \''.myesc($link,$buf['week']).'\', \''.myesc($link,$buf['statuses']).'\', \''.myesc($link,$buf['logins']).'\', \''.myesc($link,$buf['registrations']).'\')';
mysqli_query($link,$query)
or mexit(mysqli_error($link).N,3,true);
}
}
}
if (array_key_exists('x-trends',$info) && is_array($info['x-trends'])) {
mysqli_query($link,'DELETE FROM InstTrends WHERE InstID='.$instid);
$trends=array();
foreach ($info['x-trends'] as $buf) {
if (akeavinn('name',$buf) && akeavinn('url',$buf) && akeavinn('history',$buf) && is_array($buf['history'])) {
$trend=0;
foreach ($buf['history'] as $row) {
if ($row['uses']>0)
$trend+=($row['accounts']/$row['uses']);
}
$trends[]=array(
'InstID'=>$instid,
'LastDay'=>$buf['history'][0]['day'],
'Name'=>myesc($link,trunc($buf['name'],'InstTrends','Name')),
'URL'=>myesc($link,trunc($buf['url'],'InstTrends','URL')),
'Pos'=>null,
'trend'=>$trend
);
}
}
mdasortbykey($trends,'trend',true);
// print_r($trends);
$pos=0;
foreach ($trends as $trend) {
$pos++;
$query='INSERT INTO InstTrends (InstID, LastDay, Name, URL, Pos) VALUES ('.$instid.', \''.myesc($link,$trend['LastDay']).'\', \''.myesc($link,trunc($trend['Name'],'InstTrends','Name')).'\', \''.myesc($link,trunc($trend['URL'],'InstTrends','URL')).'\', '.$pos.')';
mysqli_query($link,$query)
or mexit(mysqli_error($link).N,3,true);
}
}
mysqli_query($link,'INSERT INTO InstChecks (InstID, Time, Status) VALUES ('.$instid.', '.time().', 1)')
or mexit(mysqli_error($link).N,3,true);
}
}
mysqli_close($link);
if ($opts['jsonwrite']) {
fwrite($jsonf,'"Fine?": true'.N.'}'.N);
fclose($jsonf);
}
if ($opts['log'])
fclose($logf);
exit(0);
?>