getinstinfo.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. const N="\n";
  16. define('SNAME',basename(__FILE__));
  17. const LIBDP='/../lib';
  18. require __DIR__.LIBDP.'/parsetime.php';
  19. require __DIR__.LIBDP.'/gurl.php';
  20. require __DIR__.LIBDP.'/tables.php';
  21. require __DIR__.LIBDP.'/mb_ucfirst.php';
  22. require __DIR__.LIBDP.'/mb_lcfirst.php';
  23. require __DIR__.LIBDP.'/ghs.php';
  24. require __DIR__.LIBDP.'/ght.php';
  25. require __DIR__.LIBDP.'/supplangs.php';
  26. require __DIR__.LIBDP.'/vendor/autoload.php';
  27. use LanguageDetection\Language;
  28. use function mysqli_real_escape_string as myesc;
  29. (strtoupper(substr(PHP_OS,0,3))==='WIN') ? $iswin=true : $iswin=false;
  30. declare(ticks=1);
  31. if (function_exists('pcntl_signal')) {
  32. function signalHandler($signal) {
  33. echo(N);
  34. mexit('received signal «'.$signal.'», shutting down.'.N,0);
  35. }
  36. pcntl_signal(SIGTERM,'signalHandler');// Termination ('kill' was called)
  37. pcntl_signal(SIGHUP,'signalHandler');// Terminal log-out
  38. pcntl_signal(SIGINT,'signalHandler');// Interrupted (Ctrl-C is pressed)
  39. }
  40. $opts=[
  41. 'hostname'=>null,
  42. 'conntimeout'=>10,
  43. 'functimeout'=>20,
  44. 'ldtoots'=>40,// number of toots to check with the automatic language detection function
  45. 'dryrun'=>false,
  46. 'fetchusers'=>false,
  47. 'udiratts'=>5,
  48. 'udirfailst'=>90,
  49. 'minmsgimplev'=>1
  50. ];
  51. $msglevs=['Debug', 'Info', 'Warning', 'Error', 'None'];
  52. $help='SYNOPSIS
  53. '.SNAME.' <hostname> [options]
  54. DESCRIPTION
  55. This script tries to fetch info about the fediverse instance at the given
  56. hostname and insert or update them in mastostart’s database.
  57. OPTIONS
  58. -l, --ldtoots <number>
  59. This option defines the number of toots the script will try to fetch from
  60. the local public timelines, to try and guess the most used languages of each
  61. instance. Its minimum value is 10, its maximum value is 40.
  62. DEFAULT: '.$opts['ldtoots'].'
  63. -f, --fetchusers
  64. If this option is set, the script will try to fetch users’ info from the
  65. considered instance’s users directory, and store them in the database.
  66. -r, --udiratts <number>
  67. This option defines how many attempts the script will do at fetching a chunk
  68. of users’ info from the profile directory, before giving up.
  69. DEFAULT: '.$opts['udiratts'].'
  70. -s, --udirfailst <time>
  71. This option defines how long the script will wait after each failed attempt
  72. at fetching a chunk of users’ info from the profile directory (see above)
  73. before retrying.
  74. DEFAULT: '.ght($opts['udirfailst'],null,0).'
  75. -t, --conntimeout <time>
  76. Sets the timeout for every connection attempt. See section «TIME
  77. SPECIFICATION» below to see how to specify time.
  78. DEFAULT: '.ght($opts['conntimeout'],null,0).'
  79. -T, --functimeout <time>
  80. Sets the timeout for every downloa. See section «TIME SPECIFICATION» below
  81. to see how to specify time.
  82. DEFAULT: '.ght($opts['functimeout'],null,0).'
  83. -d, --dryrun
  84. If this option is set, the script won’t write anything in the database.
  85. -m, --minmsgimplev <«debug»|«info»|«warning»|«error»|«none»>
  86. Defines the minimum “importance level” of messages to be written to the
  87. text user interface. There are 4 “importance levels”, in this order of
  88. importance: «debug», «info», «warning», «error». Setting this option to any
  89. of these values will make the script write to the text user interface all
  90. the messages with the specified or a greater level; setting it to the
  91. special value «none» will completely disable messages.
  92. DEFAULT: '.lcfirst($msglevs[$opts['minmsgimplev']]).'
  93. -h, --help
  94. If this option is set, the script will show this help text and exit.
  95. TIME SPECIFICATION
  96. An example is better than ~5148 words :-)
  97. To specify 1 year, 6 months (made of 31 days), 2 weeks, 3 days, 5 hours,
  98. 7 minutes and 12 seconds you can use «1y,6M,2w,3d,5h,7m,12s»; but you can
  99. also use «12s,7m,5h,3d,2w,6M,1y», or even «18M,1w,1w,2d,1d,3h,2h,7m,12s».
  100. LICENSE
  101. This program comes with ABSOLUTELY NO WARRANTY; for details see the source.
  102. This is free software, and you are welcome to redistribute it under certain
  103. conditions; see <http://www.gnu.org/licenses/> for details.'.N;
  104. for ($i=1; $i<$argc; $i++) {
  105. if ($argv[$i]=='-f' || $argv[$i]=='--fetchusers') {
  106. $opts['fetchusers']=true;
  107. } elseif ($argv[$i]=='-r' || $argv[$i]=='--udiratts') {
  108. if ($i+1>=$argc || preg_match('/^\d+$/',$argv[$i+1])!==1 || $argv[$i+1]+0<1)
  109. mexit('option «'.$argv[$i].'» requires a number > 1 as an argument (use «-h» to read help).'.N,1);
  110. $i++;
  111. $opts['udiratts']=$argv[$i]+0;
  112. } elseif ($argv[$i]=='-s' || $argv[$i]=='--udirfailst') {
  113. if ($i+1>=$argc || parsetime($argv[$i+1])===false)
  114. mexit('option «'.$argv[$i].'» requires a time specification as an argument (use «-h» to read help).'.N,1);
  115. $i++;
  116. $opts['udirfailst']=parsetime($argv[$i]);
  117. } elseif ($argv[$i]=='-t' || $argv[$i]=='--conntimeout') {
  118. if ($i+1>=$argc || parsetime($argv[$i+1])===false)
  119. mexit('option «'.$argv[$i].'» requires a time specification as an argument (use «-h» to read help).'.N,1);
  120. $i++;
  121. $opts['conntimeout']=parsetime($argv[$i]);
  122. } elseif ($argv[$i]=='-T' || $argv[$i]=='--functimeout') {
  123. if ($i+1>=$argc || parsetime($argv[$i+1])===false)
  124. mexit('option «'.$argv[$i].'» requires a time specification as an argument (use «-h» to read help).'.N,1);
  125. $i++;
  126. $opts['functimeout']=parsetime($argv[$i]);
  127. } elseif ($argv[$i]=='-l' || $argv[$i]=='--ldtoots') {
  128. if ($i+1>=$argc || preg_match('/^\d+$/',$argv[$i+1])!==1 || $argv[$i+1]+0>40 || $argv[$i+1]+0<10)
  129. mexit('option «'.$argv[$i].'» requires a number >= 10 and <= 40 as an argument (use «-h» to read help).'.N,1);
  130. $i++;
  131. $opts['ldtoots']=$argv[$i]+0;
  132. } elseif ($argv[$i]=='-d' || $argv[$i]=='--dryrun') {
  133. $opts['dryrun']=true;
  134. } elseif ($argv[$i]=='-m' || $argv[$i]=='--minmsgimplev') {
  135. if ($i+1>=$argc || !in_array(ucfirst(strtolower($argv[$i+1])),$msglevs))
  136. mexit('option «'.$argv[$i].'» requires a “message importance level” value as an argument (use «-h» to read help).'.N,1);
  137. $i++;
  138. $opts['minmsgimplev']=array_search(ucfirst(strtolower($argv[$i])),$msglevs);
  139. } elseif ($argv[$i]=='-h' || $argv[$i]=='--help') {
  140. echo($help);
  141. exit(0);
  142. } elseif (is_null($opts['hostname']) && $argv[$i][0]!=='-') {
  143. $opts['hostname']=$argv[$i];
  144. } else {
  145. mexit('don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help».'.N,1);
  146. }
  147. }
  148. if (is_null($opts['hostname'])) mexit('you didn’t specify an hostname (you can read the help text using «-h» or «--help»).'.N,1);
  149. $inifp=__DIR__.'/../conf/mustard.ini';
  150. $iniarr=@parse_ini_file($inifp)
  151. or mexit('could not open config file «'.$inifp.'»'.N,1);
  152. try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
  153. catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
  154. // for php versions < 8
  155. if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
  156. try { $res=mysqli_set_charset($link,'utf8mb4'); }
  157. catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).' ['.mysqli_errno($link).'].'.N,1,true); }
  158. // for php versions < 8
  159. if ($res===false) mexit('could not set MySQL charset: '.mysqli_error($link).' ['.mysqli_errno($link).'].'.N,1,true);
  160. $mastodons=[];
  161. $res=myq($link,'SELECT Name FROM Platforms WHERE Consider=1',__LINE__);
  162. while ($row=mysqli_fetch_assoc($res))
  163. $mastodons[]=preg_quote($row['Name'],'/');
  164. if (count($mastodons)<1) mexit('in table «Platforms», there is no platform to be considered!'.N,1);
  165. $mastodons=implode('|',$mastodons);
  166. $tables=tables($link);
  167. //print_r($tables);
  168. $instints=['ID', 'FirstSeen', 'IsMastodon', 'Priority', 'Visible', 'Noxious', 'NoxLastModTS', 'LocalityID', 'OurLangsLock', 'UserCount', 'StatusCount', 'DomainCount', 'ActiveUsersMonth', 'ActiveUsersHalfYear', 'RegOpen', 'RegReqApproval', 'MaxTootChars', 'AdmCreatedAt', 'WasLastCheckOk', 'LastOkCheckTS', 'GuestID', 'LastGuestEdit', 'InsertTS', 'RPos'];
  169. $idata=[];
  170. $res=myq($link,'SHOW COLUMNS FROM Instances',__FILE__);
  171. while ($row=mysqli_fetch_assoc($res))
  172. $idata[$row['Field']]=$row['Default'];
  173. // since we later need to determine if a value is an integer, and mysql returns integers as strings...
  174. setint($instints,$idata);
  175. $idata['URI']=$opts['hostname'];
  176. $instanswered=false;
  177. $now=time();
  178. /*
  179. * Nodeinfo ('https://'.$opts['hostname'].'/nodeinfo/2.0.json') was added in v3.0.0
  180. * Trends ('https://'.$opts['hostname'].'/api/v1/trends') was added in v3.0.0
  181. * Activity ('https://'.$opts['hostname'].'/api/v1/instance/activity') was added in v2.1.2
  182. */
  183. waituntilonline();
  184. eecho(1,'[[[ Working on «'.$opts['hostname'].'» ]]]'.N);
  185. if (willtrunc($opts['hostname'],'Instances','URI'))
  186. mexit('«'.$opts['hostname'].'»: ignoring it because hostname is too long for the «URI» column of «Instances» table.'.N,2);
  187. eecho(0,'«'.$opts['hostname'].'»: trying to fetch its info from the database...'.N);
  188. $res=myq($link,'SELECT * FROM Instances WHERE URI=\''.myesc($link,$opts['hostname']).'\'',__LINE__);
  189. $count=mysqli_num_rows($res);
  190. if ($count>1) {
  191. $msg='«'.$opts['hostname'].'»: there are '.$count.' records with this URI in Instances table.';
  192. notify($msg,3,false);
  193. mexit($msg.N,3);
  194. } elseif ($count==1) {
  195. eecho(1,'«'.$opts['hostname'].'»: found 1 record with this URI in Instances table.'.N);
  196. $oidata=mysqli_fetch_assoc($res);
  197. setint($instints,$oidata);
  198. } else {
  199. eecho(1,'«'.$opts['hostname'].'»: found no record with this URI in Instances table.'.N);
  200. $oidata=null;
  201. }
  202. eecho(0,'«'.$opts['hostname'].'»: trying to fetch nodeinfo specs on https...'.N);
  203. $buf=@gurl('https://'.$opts['hostname'].'/.well-known/nodeinfo',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  204. if ($buf['cont']===false) {
  205. eecho(0,'«'.$opts['hostname'].'»: trying to fetch nodeinfo specs on http...'.N);
  206. $buf=@gurl('http://'.$opts['hostname'].'/.well-known/nodeinfo',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  207. }
  208. if ($buf['cont']!==false) {
  209. $buf=@json_decode($buf['cont'],true);
  210. if (is_array($buf)) {
  211. if (isset($buf['links']) && is_array($buf['links']) && count($buf['links'])>0) {
  212. $ok=true;
  213. $nirefs=[];
  214. foreach ($buf['links'] as $key=>$niref) {
  215. if (isset($niref['rel']) && isset($niref['href'])) {
  216. $nirefs[$niref['rel']]=$niref['href'];
  217. } else {
  218. eecho(2,'«'.$opts['hostname'].'»: nodeinfo specs “links” entitity '.$key.' has unexpected format.'.N);
  219. $ok=false;
  220. }
  221. }
  222. if ($ok) {
  223. krsort($nirefs);
  224. $niref=array_shift($nirefs);
  225. eecho(1,'«'.$opts['hostname'].'»: got and successfully parsed nodeinfo specs :-)'.N);
  226. eecho(0,'«'.$opts['hostname'].'»: trying to fetch nodeinfo data...'.N);
  227. $buf=@gurl($niref,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  228. if ($buf['cont']!==false) {
  229. $buf=@json_decode($buf['cont'],true);
  230. if (is_array($buf)) {
  231. eecho(1,'«'.$opts['hostname'].'»: got nodeinfo data :-)'.N);
  232. if (isset($buf['software']['name']) && is_string($buf['software']['name']) && !isempty($buf['software']['name'])) {
  233. $idata['Software']=trim($buf['software']['name']);
  234. (preg_match('/^'.$mastodons.'/',$idata['Software'])===1) ? $idata['IsMastodon']=true : $idata['IsMastodon']=false;
  235. $res=myq($link,'SELECT Name FROM Platforms WHERE Name=\''.myesc($link,$idata['Software']).'\'',__LINE__);
  236. if (mysqli_num_rows($res)<1) {
  237. if (!$opts['dryrun'])
  238. myq($link,'INSERT INTO Platforms (Name) VALUES (\''.myesc($link,truncs($idata['Software'], 'Platforms', 'Name', '«'.$opts['hostname'].'»')).'\')',__LINE__);
  239. notify('«'.$opts['hostname'].'» runs on «'.$idata['Software'].'», which was not present in the «Platforms» table, so it was added there. It would be good to check whether it is a Mastodon derivate and how compatible it is, to decide whether to consider instances using it as Mastodon instances by setting the «Consider» field of its record to «1».',2);
  240. }
  241. }
  242. if (isset($buf['software']['version']) && is_string($buf['software']['version']) && !isempty($buf['software']['version']))
  243. $idata['Version']=trim($buf['software']['version']);
  244. if (isset($buf['usage']['users']['total']) && is_int($buf['usage']['users']['total']))
  245. $idata['UserCount']=$buf['usage']['users']['total'];
  246. if (isset($buf['usage']['users']['activeMonth']) && is_int($buf['usage']['users']['activeMonth']))
  247. $idata['ActiveUsersMonth']=$buf['usage']['users']['activeMonth'];
  248. if (isset($buf['usage']['users']['activeHalfyear']) && is_int($buf['usage']['users']['activeHalfyear']))
  249. $idata['ActiveUsersHalfYear']=$buf['usage']['users']['activeHalfyear'];
  250. if (isset($buf['usage']['localPosts']) && is_int($buf['usage']['localPosts']))
  251. $idata['StatusCount']=$buf['usage']['localPosts'];
  252. if (isset($buf['openRegistrations']) && is_bool($buf['openRegistrations']))
  253. $idata['RegOpen']=b2i($buf['openRegistrations']);
  254. } else {
  255. eecho(2,'«'.$opts['hostname'].'»: nodeinfo data was not good JSON.'.N);
  256. }
  257. } else {
  258. eecho(2,'«'.$opts['hostname'].'»: could not fetch nodeinfo data: '.$buf['emsg'].'.'.N);
  259. }
  260. }
  261. } else {
  262. eecho(2,'«'.$opts['hostname'].'»: nodeinfo specs had unexpected format.'.N);
  263. }
  264. } else {
  265. eecho(2,'«'.$opts['hostname'].'»: nodeinfo specs where not good JSON.'.N);
  266. }
  267. } else {
  268. eecho(2,'«'.$opts['hostname'].'»: could not fetch nodeinfo specs: '.$buf['emsg'].'.'.N);
  269. }
  270. if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4.0.0') {
  271. eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance info from API v2...'.N);
  272. $buf=@gurl('https://'.$opts['hostname'].'/api/v2/instance',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  273. if ($buf['cont']!==false) {
  274. ckratelimit($buf['headers']);
  275. $buf=@json_decode($buf['cont'],true);
  276. if (is_array($buf)) {
  277. if (make(['domain', 'title', 'version', 'source_url', 'description', 'usage', 'thumbnail', 'languages', 'configuration', 'registrations', 'contact', 'rules'],$buf)) {
  278. eecho(1,'«'.$opts['hostname'].'»: got good instance info from API v2 :-)'.N);
  279. $instanswered=true;
  280. if (isset($buf['title']) && is_string($buf['title']) && !isempty($buf['title']))
  281. $idata['Title']=trim($buf['title']);
  282. if (isset($buf['description']) && is_string($buf['description']) && !isempty($buf['description']))
  283. $idata['ShortDesc']=trim($buf['description']);
  284. if (isset($buf['thumbnail']['url']) && is_string($buf['thumbnail']['url']) && !isempty($buf['thumbnail']['url'])) {
  285. $idata['Thumb']=trim($buf['thumbnail']['url']);
  286. if (!@file_get_contents($idata['Thumb'],false,null,0,512)) $idata['Thumb']='unavailable';
  287. }
  288. if (isset($buf['configuration']['statuses']['max_characters']) && is_int($buf['configuration']['statuses']['max_characters']))
  289. $idata['MaxTootChars']=$buf['configuration']['statuses']['max_characters'];
  290. if (isset($buf['registrations']['approval_required']) && is_bool($buf['registrations']['approval_required']))
  291. $idata['RegReqApproval']=b2i($buf['registrations']['approval_required']);
  292. if (isset($buf['contact']['email']) && is_string($buf['contact']['email']))
  293. $idata['Email']=trim($buf['contact']['email']);
  294. if (!isset($buf['contact']['account']['noindex']) || (isset($buf['contact']['account']['noindex']) && is_bool($buf['contact']['account']['noindex']) && $buf['contact']['account']['noindex']===false)) {
  295. if (isset($buf['contact']['account']['acct']) && is_string($buf['contact']['account']['acct']) && !isempty($buf['contact']['account']['acct']))
  296. $idata['AdmAccount']=trim($buf['contact']['account']['acct']);
  297. if (isset($buf['contact']['account']['display_name']) && is_string($buf['contact']['account']['display_name']) && !isempty($buf['contact']['account']['display_name']))
  298. $idata['AdmDisplayName']=trim($buf['contact']['account']['display_name']);
  299. if (isset($buf['contact']['account']['created_at']) && is_string($buf['contact']['account']['created_at']) && ($ts=strtotime($buf['contact']['account']['created_at']))!==false)
  300. $idata['AdmCreatedAt']=$ts;
  301. if (isset($buf['contact']['account']['note']) && is_string($buf['contact']['account']['note']) && !isempty($buf['contact']['account']['note']))
  302. $idata['AdmNote']=trim($buf['contact']['account']['note']);
  303. if (isset($buf['contact']['account']['url']) && is_string($buf['contact']['account']['url']) && !isempty($buf['contact']['account']['url']))
  304. $idata['AdmURL']=trim($buf['contact']['account']['url']);
  305. if (isset($buf['contact']['account']['avatar']) && is_string($buf['contact']['account']['avatar']) && !isempty($buf['contact']['account']['avatar'])) {
  306. $idata['AdmAvatar']=trim($buf['contact']['account']['avatar']);
  307. if (!@file_get_contents($idata['AdmAvatar'],false,null,0,512)) $idata['AdmAvatar']='unavailable';
  308. }
  309. if (isset($buf['contact']['account']['header']) && is_string($buf['contact']['account']['header']) && !isempty($buf['contact']['account']['header']))
  310. $idata['AdmHeader']=trim($buf['contact']['account']['header']);
  311. } else {
  312. if (isset($buf['contact']['account']['noindex']) && is_bool($buf['contact']['account']['noindex']) && $buf['contact']['account']['noindex']===true)
  313. $idata['AdmAccount']='OPTED OUT';// here we rely on the fact that nobody could set "acct" to "OPTED OUT" since it doesn't allow spaces
  314. $idata['AdmAvatar']='unavailable';
  315. }
  316. // domain_count is gone from api v2, and we won't resort to api v1 just to get it when ver. >= 4.0.0
  317. if (isset($buf['languages']) && is_array($buf['languages']))
  318. $idata['languages']=$buf['languages'];
  319. if (isset($buf['rules']) && is_array($buf['rules']))
  320. foreach ($buf['rules'] as $rule)
  321. if (isset($rule['id']) && is_string($rule['id']) && !isempty($rule['id']) && isset($rule['text']) && is_string($rule['text']) && !isempty($rule['text']))
  322. $idata['rules'][$rule['id']]=$rule['text'];
  323. } else {
  324. eecho(2,'«'.$opts['hostname'].'»: instance info fetched from API v2 had unexpected format.'.N);
  325. }
  326. } else {
  327. eecho(2,'«'.$opts['hostname'].'»: instance info fetched from API v2 were not good JSON.'.N);
  328. }
  329. } else {
  330. eecho(2,'«'.$opts['hostname'].'»: could not fetch instance info from API v2: '.$buf['emsg'].'.'.N);
  331. }
  332. eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance extended description from API v1...'.N);
  333. $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/extended_description',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  334. if ($buf['cont']!==false) {
  335. ckratelimit($buf['headers']);
  336. $buf=@json_decode($buf['cont'],true);
  337. if (is_array($buf)) {
  338. eecho(1,'«'.$opts['hostname'].'»: got instance extended description from API v1 :-)'.N);
  339. //print_r($buf);
  340. if (!is_null($buf['content']) && is_string($buf['content']) && !isempty($buf['content']))
  341. $idata['LongDesc']=trim($buf['content']);
  342. } else {
  343. eecho(2,'«'.$opts['hostname'].'»: instance extended description fetched from API v1 was not good JSON.'.N);
  344. }
  345. } else {
  346. eecho(2,'«'.$opts['hostname'].'»: could not fetch instance extended description from API v1: '.$buf['emsg'].'.'.N);
  347. }
  348. eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance domain blocks from API v1...'.N);
  349. $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/domain_blocks',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  350. if ($buf['cont']!==false) {
  351. ckratelimit($buf['headers']);
  352. $buf=@json_decode($buf['cont'],true);
  353. if (is_array($buf)) {
  354. eecho(1,'«'.$opts['hostname'].'»: got instance domain blocks from API v1 :-)'.N);
  355. $idata['blocks']=[];
  356. $idata['Threads']='accessible';
  357. foreach ($buf as $key=>$block) {
  358. if (make(['domain', 'digest', 'severity', 'comment'],$block) && is_string($block['domain']) && !isempty($block['domain']) && is_string($block['digest']) && preg_match('/^[a-f0-9]{64}$/',$block['digest'])===1 && is_string($block['severity']) && in_array($block['severity'], ['silence','suspend']) && (is_null($block['comment']) || is_string($block['comment']))) {
  359. if (!is_null($block['comment']) && trim($block['comment'])=='') $block['comment']=null;
  360. $idata['blocks'][]=['dom'=>$block['domain'], 'sev'=>$block['severity'], 'comm'=>$block['comment']];
  361. if (preg_match('#^(threads.net|.*\.threads.net)$#i',$block['domain'])===1) {
  362. if ($block['severity']=='suspend')
  363. $idata['Threads']='suspended';
  364. elseif ($block['severity']=='silence')
  365. $idata['Threads']='limited';
  366. else
  367. $idata['Threads']=$block['severity'];
  368. }
  369. } else {
  370. eecho(2,'«'.$opts['hostname'].'»: domain block '.$key.' has unexpected format.'.N);
  371. }
  372. }
  373. } else {
  374. eecho(2,'«'.$opts['hostname'].'»: instance domain blocks fetched from API v1 were not good JSON.'.N);
  375. $idata['Threads']=null;
  376. }
  377. } else {
  378. eecho(2,'«'.$opts['hostname'].'»: could not fetch instance domain blocks from API v1: '.$buf['emsg'].'.'.N);
  379. $idata['Threads']=null;
  380. }
  381. } else {// we still try to fetch instance info from api v1, if ver. < 4.0.0, since it could be a mastodon instance older than 2.1.2, when nodeinfo was introduced
  382. eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance info from API v1...'.N);
  383. $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  384. if ($buf['cont']!==false) {
  385. ckratelimit($buf['headers']);
  386. $buf=@json_decode($buf['cont'],true);
  387. if (is_array($buf)) {
  388. if (make(['uri', 'title', 'short_description', 'description', 'email', 'version', 'urls', 'stats', 'thumbnail', 'languages', 'registrations', 'approval_required', 'contact_account'],$buf)) {
  389. eecho(1,'«'.$opts['hostname'].'»: got good instance info from API v1 :-)'.N);
  390. //print_r($buf);
  391. $instanswered=true;
  392. if (isset($buf['title']) && is_string($buf['title']) && !isempty($buf['title']))
  393. $idata['Title']=trim($buf['title']);
  394. if (isset($buf['short_description']) && is_string($buf['short_description']) && !isempty($buf['short_description']))
  395. $idata['ShortDesc']=trim($buf['description']);
  396. if (isset($buf['description']) && is_string($buf['description']) && !isempty($buf['description']))
  397. $idata['LongDesc']=trim($buf['description']);
  398. if (isset($buf['email']) && is_string($buf['email']))
  399. $idata['Email']=trim($buf['email']);
  400. // if nodeinfo did not respond, it could be mastodon < 3.0.0, and we would not have $idata['Version'] yet, so...
  401. if (!isset($idata['Version']) && isset($buf['version']) && is_string($buf['version']) && !isempty($buf['version']))
  402. $idata['Version']=trim($buf['version']);
  403. // if nodeinfo responded we should already have these 2 below, but nodeinfo could have not responded if instance ver. is < 3.0.0
  404. if (isset($buf['stats']['user_count']) && is_int($buf['stats']['user_count']))
  405. $idata['UserCount']=$buf['stats']['user_count'];
  406. if (isset($buf['stats']['status_count']) && is_int($buf['stats']['status_count']))
  407. $idata['StatusCount']=$buf['stats']['status_count'];
  408. if (isset($buf['stats']['domain_count']) && is_int($buf['stats']['domain_count']))
  409. $idata['DomainCount']=$buf['stats']['domain_count'];
  410. if (isset($buf['thumbnail']) && is_string($buf['thumbnail']) && !isempty($buf['thumbnail'])) {
  411. $idata['Thumb']=trim($buf['thumbnail']);
  412. if (!@file_get_contents($idata['Thumb'],false,null,0,512)) $idata['Thumb']='unavailable';
  413. }
  414. if (isset($buf['max_toot_chars']) && is_int($buf['max_toot_chars']))
  415. $idata['MaxTootChars']=$buf['max_toot_chars'];
  416. elseif (isset($buf['configuration']['statuses']['max_characters']) && is_int($buf['configuration']['statuses']['max_characters']))
  417. $idata['MaxTootChars']=$buf['configuration']['statuses']['max_characters'];
  418. // if nodeinfo responded we should already have this 1 below, but nodeinfo could have not responded if instance ver. is < 3.0.0
  419. if (isset($buf['registrations']) && is_bool($buf['registrations']))
  420. $idata['RegOpen']=b2i($buf['registrations']);
  421. if (isset($buf['approval_required']) && is_bool($buf['approval_required']))
  422. $idata['RegReqApproval']=b2i($buf['approval_required']);
  423. if (isset($buf['contact_account']['acct']) && is_string($buf['contact_account']['acct']) && !isempty($buf['contact_account']['acct']))
  424. $idata['AdmAccount']=trim($buf['contact_account']['acct']);
  425. if (isset($buf['contact_account']['display_name']) && is_string($buf['contact_account']['display_name']) && !isempty($buf['contact_account']['display_name']))
  426. $idata['AdmDisplayName']=trim($buf['contact_account']['display_name']);
  427. if (isset($buf['contact_account']['created_at']) && is_string($buf['contact_account']['created_at']) && ($ts=strtotime($buf['contact_account']['created_at']))!==false)
  428. $idata['AdmCreatedAt']=$ts;
  429. if (isset($buf['contact_account']['note']) && is_string($buf['contact_account']['note']) && !isempty($buf['contact_account']['note']))
  430. $idata['AdmNote']=trim($buf['contact_account']['note']);
  431. if (isset($buf['contact_account']['url']) && is_string($buf['contact_account']['url']) && !isempty($buf['contact_account']['url']))
  432. $idata['AdmURL']=trim($buf['contact_account']['url']);
  433. if (isset($buf['contact_account']['avatar']) && is_string($buf['contact_account']['avatar']) && !isempty($buf['contact_account']['avatar'])) {
  434. $idata['AdmAvatar']=trim($buf['contact_account']['avatar']);
  435. if (!@file_get_contents($idata['AdmAvatar'],false,null,0,512)) $idata['AdmAvatar']='unavailable';
  436. }
  437. if (isset($buf['contact_account']['header']) && is_string($buf['contact_account']['header']) && !isempty($buf['contact_account']['header']))
  438. $idata['AdmHeader']=trim($buf['contact_account']['header']);
  439. if (isset($buf['languages']) && is_array($buf['languages']))
  440. $idata['languages']=$buf['languages'];
  441. if (isset($buf['rules']) && is_array($buf['rules']))
  442. foreach ($buf['rules'] as $rule)
  443. if (isset($rule['id']) && is_string($rule['id']) && !isempty($rule['id']) && isset($rule['text']) && is_string($rule['text']) && !isempty($rule['text']))
  444. $idata['rules'][$rule['id']]=$rule['text'];
  445. // some falsing
  446. if (isset($buf['pleroma'])) $idata['IsMastodon']=false;
  447. if (isset($buf['version']) && is_string($buf['version']) && preg_match('#(pleroma|pixelfed)#i',$buf['version'])===1) $idata['IsMastodon']=false;
  448. } else {
  449. eecho(2,'«'.$opts['hostname'].'»: instance info fetched from API v1 had unexpected format.'.N);
  450. }
  451. } else {
  452. eecho(2,'«'.$opts['hostname'].'»: instance info fetched from API v1 were not good JSON.'.N);
  453. }
  454. } else {
  455. eecho(2,'«'.$opts['hostname'].'»: could not fetch instance info from API v1: '.$buf['emsg'].'.'.N);
  456. }
  457. }
  458. if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='2.1.2') {
  459. eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance activity info from API v1...'.N);
  460. $buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/activity',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  461. if ($buf['cont']!==false) {
  462. ckratelimit($buf['headers']);
  463. $buf=@json_decode($buf['cont'],true);
  464. if (is_array($buf)) {
  465. eecho(1,'«'.$opts['hostname'].'»: got instance activity info from API v1 :-)'.N);
  466. $idata['activity']=$buf;
  467. } else {
  468. eecho(2,'«'.$opts['hostname'].'»: instance activity info from API v1 were not good JSON: '.$buf['emsg'].'.'.N);
  469. }
  470. } else {
  471. eecho(2,'«'.$opts['hostname'].'»: could not fetch instance activity info from API v1: '.$buf['emsg'].'.'.N);
  472. }
  473. }
  474. if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='3.0.0') {
  475. eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance tags trends info from API v1...'.N);
  476. $url='https://'.$opts['hostname'].'/api/v1/trends';
  477. if ($idata['Version']>='3.5.0') $url.='/tags';
  478. $buf=@gurl($url,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  479. if ($buf['cont']!==false) {
  480. ckratelimit($buf['headers']);
  481. $buf=@json_decode($buf['cont'],true);
  482. if (is_array($buf)) {
  483. eecho(1,'«'.$opts['hostname'].'»: got instance tags trends info from API v1 :-)'.N);
  484. $idata['trends']=$buf;
  485. } else {
  486. eecho(2,'«'.$opts['hostname'].'»: instance tags trends from API v1 were not good JSON: '.$buf['emsg'].'.'.N);
  487. }
  488. } else {
  489. eecho(2,'«'.$opts['hostname'].'»: could not fetch instance tags trends from API v1: '.$buf['emsg'].'.'.N);
  490. }
  491. }
  492. // finished fetching
  493. if (!is_null($idata['IsMastodon'])) $idata['IsMastodon']=b2i($idata['IsMastodon']);
  494. ($instanswered) ? $idata['WasLastCheckOk']=1 : $idata['WasLastCheckOk']=0;
  495. if (is_null($oidata)) {
  496. $query='INSERT INTO Instances SET ';
  497. $idata['InsertTS']=$now;
  498. $idata['TotChecks']=1;
  499. if ($instanswered) {
  500. $idata['FirstSeen']=$now;
  501. $idata['LastOkCheckTS']=$now;
  502. $idata['OkChecks']=1;
  503. } else {
  504. $idata['Thumb']='unavailable';
  505. $idata['AdmAvatar']='unavailable';
  506. $idata['OkChecks']=0;
  507. }
  508. } else {
  509. $query='UPDATE Instances SET ';
  510. ($instanswered && is_null($oidata['FirstSeen'])) ? $idata['FirstSeen']=$now : $idata['FirstSeen']=$oidata['FirstSeen'];
  511. ($instanswered) ? $idata['LastOkCheckTS']=$now : $idata['LastOkCheckTS']=$oidata['LastOkCheckTS'];
  512. $idata['TotChecks']=$oidata['TotChecks']+1;
  513. $idata['OkChecks']=$oidata['OkChecks'];
  514. if ($instanswered) $idata['OkChecks']++;
  515. $idata['Priority']=$oidata['Priority'];
  516. $idata['Visible']=$oidata['Visible'];
  517. $idata['Noxious']=$oidata['Noxious'];
  518. $idata['NoxReason']=$oidata['NoxReason'];
  519. $idata['NoxLastModTS']=$oidata['NoxLastModTS'];
  520. $idata['OurDesc']=$oidata['OurDesc'];
  521. $idata['OurDescEN']=$oidata['OurDescEN'];
  522. $idata['LocalityID']=$oidata['LocalityID'];
  523. $idata['OurLangsLock']=$oidata['OurLangsLock'];
  524. $idata['GuestID']=$oidata['GuestID'];
  525. $idata['LastGuestEdit']=$oidata['LastGuestEdit'];
  526. $idata['InsertTS']=$oidata['InsertTS'];
  527. $idata['RPos']=$oidata['RPos'];
  528. if (!$instanswered) {
  529. $idata['IsMastodon']=$oidata['IsMastodon'];
  530. $idata['Title']=$oidata['Title'];
  531. $idata['ShortDesc']=$oidata['ShortDesc'];
  532. $idata['LongDesc']=$oidata['LongDesc'];
  533. $idata['Email']=$oidata['Email'];
  534. $idata['Software']=$oidata['Software'];
  535. $idata['Version']=$oidata['Version'];
  536. $idata['UserCount']=$oidata['UserCount'];
  537. $idata['StatusCount']=$oidata['StatusCount'];
  538. $idata['DomainCount']=$oidata['DomainCount'];
  539. $idata['ActiveUsersMonth']=$oidata['ActiveUsersMonth'];
  540. $idata['ActiveUsersHalfYear']=$oidata['ActiveUsersHalfYear'];
  541. $idata['Thumb']='unavailable';
  542. $idata['RegOpen']=$oidata['RegOpen'];
  543. $idata['RegReqApproval']=$oidata['RegReqApproval'];
  544. $idata['MaxTootChars']=$oidata['MaxTootChars'];
  545. $idata['AdmAccount']=$oidata['AdmAccount'];
  546. $idata['AdmDisplayName']=$oidata['AdmDisplayName'];
  547. $idata['AdmCreatedAt']=$oidata['AdmCreatedAt'];
  548. $idata['AdmNote']=$oidata['AdmNote'];
  549. $idata['AdmURL']=$oidata['AdmURL'];
  550. $idata['AdmAvatar']='unavailable';
  551. $idata['AdmHeader']=$oidata['AdmHeader'];
  552. $idata['Threads']=$oidata['Threads'];
  553. }
  554. }
  555. $set=[];
  556. foreach ($idata as $key=>$val) {
  557. if (in_array($key,['ID','languages','rules','activity','trends','blocks'])) {
  558. true;// do nothing
  559. } elseif (is_null($val)) {
  560. $set[]=$key.'=NULL';
  561. } elseif (is_int($val)) {
  562. if (willtrunc($val,'Instances',$key)) {
  563. $msg='«'.$opts['hostname'].'»: value «'.$val.'» is less than min. admitted value or greater than max. admitted value for column «'.$key.'» of table «Instances». Shutting down.';
  564. notify($msg,3,false);
  565. mexit($msg.N,2);
  566. }
  567. $set[]=$key.'='.$val;
  568. } elseif (is_string($val)) {
  569. if (willtrunc($val,'Instances',$key)) {
  570. $msg='«'.$opts['hostname'].'»: value «'.nocrnl($val).'» is too long for column «'.$key.'» of table «Instances». Shutting down.';
  571. notify($msg,3,false);
  572. mexit($msg.N,2);
  573. }
  574. $set[]=$key.'=\''.myesc($link,$val).'\'';
  575. } else {
  576. mexit('$idata[\''.$key.'\'] value has unmanaged type, see code around line '.__LINE__.'.'.N,3);
  577. }
  578. }
  579. $query.=implode(', ',$set);
  580. if (!is_null($oidata)) $query.=' WHERE ID='.$oidata['ID'];
  581. eecho(1,'query: «'.$query.'».'.N);
  582. if (!$opts['dryrun']) {
  583. if (!is_null($oidata) || $instanswered) {
  584. myq($link,$query,__LINE__);
  585. } else {
  586. mexit('«'.$opts['hostname'].'»: not inserting unknown instance because it did not respond; shutting down after '.ght(time()-$now,null,0).' :-)'.N,0);
  587. }
  588. }
  589. if (is_null($oidata)) {
  590. (!$opts['dryrun']) ? $instid=mysqli_insert_id($link) : $instid=0;
  591. notify('«<a href="viewinst.php?id='.$instid.'">'.$opts['hostname'].'</a>» is a NEW instance! :-)',1);
  592. } else {
  593. $instid=$oidata['ID'];
  594. }
  595. // from here we know for sure $instid
  596. if (!$opts['dryrun']) myq($link,'INSERT INTO InstChecks (InstID, Time, Status) VALUES ('.$instid.', '.$now.', '.$idata['WasLastCheckOk'].')',__LINE__);
  597. if ($instanswered && isset($idata['languages']) && is_array($idata['languages']) && count($idata['languages'])>0) {
  598. eecho(1,'«'.$opts['hostname'].'»: declared languages: '.implode(', ',$idata['languages']).N);
  599. if (!$opts['dryrun'])
  600. myq($link,'DELETE FROM InstLangs WHERE InstID='.$instid,__LINE__);
  601. $langids=getlangsidsarr($idata['languages'],$supplangs,$link,$opts['hostname'],$opts['dryrun'],__LINE__);
  602. if (!$opts['dryrun']) {
  603. $pos=0;
  604. foreach ($langids as $langid) {
  605. $pos++;
  606. myq($link,'INSERT INTO InstLangs SET InstID='.$instid.', LangID='.$langid.', Pos='.$pos,__LINE__);
  607. }
  608. }
  609. if (!is_null($oidata) && $oidata['OurLangsLock']==1) {
  610. eecho(1,'«'.$opts['hostname'].'»: won’t touch “our languages” because they are locked.'.N);
  611. } else {
  612. // we try to detect languages only if first declared language (the only one currently definable by admins)
  613. // is equal to the default "en", otherwise we assume it's been set to the actual mostly used language on the instance
  614. if ($idata['languages'][0]=='en') {
  615. $idata['ourlanguages']=get_instance_langs($opts['hostname']);
  616. while (count($idata['ourlanguages'])>5)
  617. array_pop($idata['ourlanguages']);
  618. if (count($idata['ourlanguages'])>0) {
  619. eecho(1,'«'.$opts['hostname'].'»: detected languages: '.implode(', ',$idata['ourlanguages']).N);
  620. } else {
  621. $idata['ourlanguages']=$idata['languages'];
  622. eecho(1,'«'.$opts['hostname'].'»: detected languages: NONE; copied declared languages to detected languages.'.N);
  623. }
  624. } else {
  625. $idata['ourlanguages']=$idata['languages'];
  626. eecho(1,'«'.$opts['hostname'].'»: copied declared languages to detected languages.'.N);
  627. }
  628. if (!$opts['dryrun'])
  629. myq($link,'DELETE FROM InstOurLangs WHERE InstID='.$instid,__LINE__);
  630. $langids=getlangsidsarr($idata['ourlanguages'],$supplangs,$link,$opts['hostname'],$opts['dryrun'],__LINE__);
  631. if (!$opts['dryrun']) {
  632. $pos=0;
  633. foreach ($langids as $langid) {
  634. $pos++;
  635. myq($link,'INSERT INTO InstOurLangs SET InstID='.$instid.', OurLangID='.$langid.', Pos='.$pos,__LINE__);
  636. }
  637. }
  638. }
  639. }
  640. if ($instanswered && !$opts['dryrun'])
  641. myq($link,'DELETE FROM InstActivity WHERE InstID='.$instid,__LINE__);
  642. if (isset($idata['activity']) && is_array($idata['activity'])) {
  643. $pos=0;
  644. foreach ($idata['activity'] as $buf) {
  645. // these should all be int, but mastodon represents them as strings
  646. if (isset($buf['week']) && is_string($buf['week']) && preg_match('/^\d+$/',$buf['week'])===1 && isset($buf['statuses']) && is_string($buf['statuses']) && preg_match('/^\d+$/',$buf['statuses'])===1 && isset($buf['logins']) && is_string($buf['logins']) && preg_match('/^\d+$/',$buf['logins'])===1 && isset($buf['registrations']) && is_string($buf['registrations']) && preg_match('/^\d+$/',$buf['registrations'])===1) {
  647. $pos++;
  648. if (!$opts['dryrun'])
  649. myq($link,'INSERT INTO InstActivity (InstID, Week, Statuses, Logins, Registrations, Pos) VALUES ('.$instid.', '.$buf['week'].', '.$buf['statuses'].', '.$buf['logins'].', '.$buf['registrations'].', '.$pos.')',__LINE__);
  650. }
  651. }
  652. }
  653. if ($instanswered && !$opts['dryrun'])
  654. myq($link,'DELETE FROM InstTrends WHERE InstID='.$instid,__LINE__);
  655. if (isset($idata['trends']) && is_array($idata['trends'])) {
  656. $trends=[];
  657. foreach ($idata['trends'] as $buf) {
  658. if (isset($buf['name']) && is_string($buf['name']) && isset($buf['url']) && is_string($buf['url']) && isset($buf['history']) && is_array($buf['history'])) {
  659. $trend=0;
  660. foreach ($buf['history'] as $row) {
  661. // below, we check for "stringness" because, they should be integers, but they are strings
  662. if (isset($row['day']) && is_string($row['day']) && preg_match('/^\d+$/',$row['day'])===1 && isset($row['uses']) && is_string($row['uses']) && preg_match('/^\d+$/',$row['uses'])===1 && isset($row['accounts']) && is_string($row['accounts']) && preg_match('/^\d+$/',$row['accounts'])===1) {
  663. $row['day']+=0;
  664. $row['uses']+=0;
  665. $row['accounts']+=0;
  666. $trend+=$row['accounts'];
  667. }
  668. }
  669. }
  670. $trends[]=[
  671. 'InstID'=>$instid,
  672. 'LastDay'=>$buf['history'][0]['day'],
  673. 'Name'=>$buf['name'],
  674. 'URL'=>$buf['url'],
  675. 'Pos'=>null,
  676. 'trend'=>$trend
  677. ];
  678. }
  679. //print_r($trends);
  680. mdasortbykey($trends,'trend',true);
  681. $pos=0;
  682. foreach ($trends as $trend) {
  683. $pos++;
  684. $query='INSERT INTO InstTrends (InstID, LastDay, Name, URL, Pos) VALUES ('.$trend['InstID'].', \''.$trend['LastDay'].'\', \''.myesc($link, truncs($trend['Name'], 'InstTrends', 'Name', '«'.$opts['hostname'].'»')).'\', \''.myesc($link, truncs($trend['URL'], 'InstTrends', 'URL', '«'.$opts['hostname'].'»')).'\', '.$pos.')';
  685. if (!$opts['dryrun'])
  686. myq($link,$query,__LINE__);
  687. }
  688. }
  689. if (isset($idata['rules']) && is_array($idata['rules'])) {
  690. ksort($idata['rules']);
  691. if (!$opts['dryrun']) {
  692. myq($link,'DELETE FROM InstRules WHERE InstID='.$instid,__LINE__);
  693. foreach ($idata['rules'] as $rule)
  694. myq($link,'INSERT INTO InstRules SET InstID='.$instid.', Text=\''.myesc($link, truncs($rule, 'InstRules', 'Text', '«'.$opts['hostname'].'»')).'\'',__LINE__);
  695. }
  696. }
  697. if ($instanswered && !$opts['dryrun'])
  698. myq($link,'DELETE FROM InstBlocks WHERE InstID='.$instid,__LINE__);
  699. if (isset($idata['blocks']) && is_array($idata['blocks'])) {
  700. foreach ($idata['blocks'] as $block) {
  701. (is_null($block['comm'])) ? $block['comm']='NULL' : $block['comm']="'".myesc($link, truncs($block['comm'], 'InstBlocks', 'Comment', '«'.$opts['hostname'].'»'))."'";
  702. if (!$opts['dryrun'])
  703. myq($link,'INSERT INTO InstBlocks SET InstID='.$instid.', Domain=\''.myesc($link, truncs($block['dom'], 'InstBlocks', 'Domain', '«'.$opts['hostname'].'»')).'\', Severity=\''.myesc($link, truncs($block['sev'], 'InstBlocks', 'Severity', '«'.$opts['hostname'].'»')).'\', Comment='.$block['comm'],__LINE__);
  704. }
  705. }
  706. if ($instanswered && $opts['fetchusers'] && $idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4.0.0') {
  707. eecho(0,'«'.$opts['hostname'].'»: trying to fetch users info from directory API...'.N);
  708. $users=[];// array of users in this instance's directory
  709. $chunk=0;
  710. $limit=40;
  711. $end=false;
  712. while (!$end) {
  713. $offset=$chunk*$limit;
  714. for ($att=0; $att<$opts['udiratts']; $att++) {
  715. eecho(0,'«'.$opts['hostname'].'»: trying to fetch chunk '.($chunk+1).' of users info from directory API (attempt '.($att+1).'/'.$opts['udiratts'].')...'.N);
  716. $buf=@gurl('https://'.$opts['hostname'].'/api/v1/directory?local=1&order=new&limit='.$limit.'&offset='.$offset,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  717. if ($buf['cont']!==false) {
  718. $xrlr=ckratelimit($buf['headers']);
  719. eecho(1,'«'.$opts['hostname'].'»: got chunk '.($chunk+1).' of users info from directory API on attempt '.($att+1).'/'.$opts['udiratts'].' (xrlr: '.$xrlr.') :-)'.N);
  720. $buf=@json_decode($buf['cont'],true);
  721. if (is_array($buf)) {
  722. //print_r($buf);
  723. if (count($buf)<$limit) $end=true;
  724. /*if (count($buf)>0 && !array_key_exists('noindex',$buf[0])) {
  725. eecho(2,'«'.$opts['hostname'].'»: account entities reported by directory api endpoint don’t have a “noindex” attribute; skipping directory fetching.'.N);
  726. break;
  727. } else {
  728. eecho(0,'«'.$opts['hostname'].'»: account entities reported by directory api endpoint do have a “noindex” attribute; continuing with directory fetching.'.N);
  729. }*/
  730. //foreach ($buf as $user) echo($user['username'].' '); echo(N.N);
  731. foreach ($buf as $user) {
  732. if (make(['id', 'username', 'display_name', 'locked', 'bot', 'discoverable', 'created_at', 'note', 'url', 'avatar', 'header', 'statuses_count', 'last_status_at', 'fields', 'noindex'], $user)) {
  733. eecho(0,'«'.$opts['hostname'].'»: working on user «'.$user['username'].'»...'.N);
  734. // disabled because it takes too long on instances with many users; that's why we added "$idata['Version']>='4.0.0'" as a condition to the root "if" statement and "noindex" to the checked keys in the "if" statement above (ver. >= 4.0.0 do report "noindex" for account entities)
  735. /*if (!isset($user['noindex'])) {
  736. $user['noindex']=true;
  737. eecho(0,'«'.$opts['hostname'].'»: «'.$user['username'].'»: «noindex» is undefined, trying to define it by fetching user’s profile page...'.N);
  738. $page=gurl($user['url'],$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  739. // here ckratelimit is not needed because it's a normal web page, not json from mastodon api
  740. if ($page['cont']!==false) {
  741. //<meta content='noindex, noarchive' name='robots'>
  742. if (preg_match('/<meta\s+content=[\'"](noindex|noarchive)/ui',$page['cont'])!==1) {
  743. $user['noindex']=false;
  744. eecho(0,'«'.$user['url'].'»: «noindex» is not set.'.N);
  745. } else {
  746. eecho(0,'«'.$user['url'].'»: «noindex» is set.'.N);
  747. }
  748. } else {
  749. eecho(2,'«'.$opts['hostname'].'»: could not fetch «'.$user['url'].'»: '.$page['emsg'].N);
  750. }
  751. }*/
  752. $snote=strip_tags($user['note']);
  753. if (preg_match('/(?<!\w)#(nobots?|noindex)(?!\w)/iu',$snote)===1) $user['noindex']=true;
  754. if (preg_match('/(?<!\w)#(okindex|yesindex|doindex|okmhindex)(?!\w)/iu',$snote)===1) $user['noindex']=false;
  755. // disabled; see previous comment
  756. /*$user['tags']=[];
  757. if (!$user['noindex'] && !is_null($idata['Version']) && $idata['Version']>='3.3.0') {
  758. eecho(0,'«'.$opts['hostname'].'»: trying to fetch tags for user «'.$user['username'].'»...'.N);
  759. $tags=@gurl('https://'.$opts['hostname'].'/api/v1/accounts/'.$user['id'].'/featured_tags',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  760. if ($tags['cont']!==false) {
  761. ckratelimit($tags['headers']);
  762. $tags=@json_decode($tags['cont'],true);
  763. if (is_array($tags) && count($tags)>0) {
  764. eecho(1,'«'.$opts['hostname'].'»: got '.count($tags).' tag(s) for user «'.$user['username'].'» :-)'.N);
  765. foreach($tags as $tag) $user['tags'][]=$tag['name'];
  766. }
  767. } else {
  768. eecho(2,'«'.$opts['hostname'].'»: could not fetch tags for user «'.$user['username'].'» :-( ('.$tags['emsg'].').'.N);
  769. }
  770. }
  771. $user['tags']=implode(';',$user['tags']);
  772. if ($user['tags']=='') $user['tags']=null;*/
  773. $user['tags']=null;
  774. if (!is_null($user['created_at'])) $user['created_at']=strtotime($user['created_at']);
  775. if (!is_null($user['last_status_at'])) $user['last_status_at']=datetots($user['last_status_at']);
  776. $users[$user['id']]=$user;
  777. } else {
  778. eecho(2,'«'.$opts['hostname'].'»: user record missed some required keys :-('.N);
  779. //print_r($user);
  780. }
  781. }
  782. break;
  783. } else {
  784. eecho(2,'«'.$opts['hostname'].'»: ... but the chunk was not good JSON :-('.N);
  785. if ($att==$opts['udiratts']-1) $end=true;
  786. }
  787. } else {
  788. eecho(2,'«'.$opts['hostname'].'»: could not fetch chunk '.($chunk+1).' of users info from directory API: '.$buf['emsg'].N);
  789. if ($att==$opts['udiratts']-1) {
  790. eecho(2,'«'.$opts['hostname'].'»: last attempt ('.($att+1).'/'.$opts['udiratts'].') on chunk '.($chunk+1).' failed; i give up.'.N);
  791. $end=true;
  792. } else {
  793. eecho(2,'«'.$opts['hostname'].'»: attempt '.($att+1).'/'.$opts['udiratts'].' on chunk '.($chunk+1).' failed; sleeping for '.ght($opts['udirfailst'],null,0).' before retrying.'.N);
  794. sleep($opts['udirfailst']);
  795. }
  796. }
  797. }
  798. $chunk++;
  799. }
  800. $totusers=count($users);
  801. eecho(1,'«'.$opts['hostname'].'»: got '.$totusers.' users’ profiles.'.N);
  802. if ($totusers>0) {
  803. eecho(1,'«'.$opts['hostname'].'»: inserting/updating '.$totusers.' users’ profiles in the database.'.N);
  804. $exusers=[];// array of this instance's users already existing in the db
  805. $res=myq($link,'SELECT ID, locid, username FROM Users WHERE InstID='.$instid,__LINE__);
  806. while ($row=mysqli_fetch_assoc($res)) $exusers[$row['locid']]=$row;
  807. foreach ($users as $locid=>$user) {
  808. $query='SET InstID='.$instid.', host='.myv($link,$opts['hostname']).', locid='.myv($link,$user['id']).', username='.myv($link,truncs($user['username'], 'Users', 'username', '«'.$opts['hostname'].'»: «'.$user['username'].'»')).', display_name='.myv($link,truncs($user['display_name'], 'Users', 'display_name', '«'.$opts['hostname'].'»: «'.$user['username'].'»')).', locked='.myv($link,$user['locked']).', bot='.myv($link,$user['bot']).', created_at='.myv($link,$user['created_at']).', note='.myv($link,truncs($user['note'], 'Users', 'note', '«'.$opts['hostname'].'»: «'.$user['username'].'»')).', url='.myv($link,truncs($user['url'], 'Users', 'url', '«'.$opts['hostname'].'»: «'.$user['username'].'»')).', avatar='.myv($link,truncs($user['avatar'], 'Users', 'avatar', '«'.$opts['hostname'].'»: «'.$user['username'].'»')).', header='.myv($link,truncs($user['header'], 'Users', 'header', '«'.$opts['hostname'].'»: «'.$user['username'].'»')).', statuses_count='.myv($link,$user['statuses_count']).', last_status_at='.myv($link,$user['last_status_at']).', tags='.myv($link,truncs($user['tags'], 'Users', 'tags', '«'.$opts['hostname'].'»: «'.$user['username'].'»'));
  809. $uid=0;
  810. if (!array_key_exists($user['id'],$exusers)) {
  811. if (!$user['noindex']) {
  812. eecho(0,'«'.$opts['hostname'].'»: inserting new user «'.$user['username'].'»...'.N);
  813. $query='INSERT INTO Users '.$query;
  814. if (!$opts['dryrun']) {
  815. myq($link,$query,__LINE__);
  816. $uid=mysqli_insert_id($link);
  817. } else {
  818. $uid=0;
  819. }
  820. } else {
  821. eecho(0,'«'.$opts['hostname'].'»: NOT inserting user «'.$user['username'].'» because they don’t want to be indexed...'.N);
  822. }
  823. } else {
  824. $uid=$exusers[$locid]['ID'];
  825. if (!$user['noindex']) {
  826. eecho(0,'«'.$opts['hostname'].'»: updating existing user «'.$user['username'].'» ('.$uid.')...'.N);
  827. $query='UPDATE Users '.$query.' WHERE ID='.$uid;
  828. } else {
  829. eecho(0,'«'.$opts['hostname'].'»: deleting existing user «'.$user['username'].'» ('.$uid.') because they don’t want to be indexed...'.N);
  830. $query='DELETE FROM Users WHERE ID='.$uid;
  831. }
  832. if (!$opts['dryrun']) {
  833. myq($link,$query,__LINE__);
  834. myq($link,'DELETE FROM UsersFields WHERE UserID='.$uid,__LINE__);
  835. }
  836. }
  837. if ($uid!=0 && !$user['noindex'] && is_array($user['fields']) && count($user['fields'])>0) {
  838. eecho(0,'«'.$opts['hostname'].'»: saving user fields for user «'.$user['username'].'» ('.$uid.')...'.N);
  839. foreach ($user['fields'] as $field) {
  840. (is_null($field['verified_at'])) ? $field['verified_at']=0 : $field['verified_at']=1;
  841. $field['name']=truncs($field['name'],'UsersFields','name','«'.$opts['hostname'].'»: «'.$user['username'].'»');
  842. $field['value']=truncs($field['value'],'UsersFields','value','«'.$opts['hostname'].'»: «'.$user['username'].'»');
  843. if (!$opts['dryrun']) myq($link,'INSERT INTO UsersFields SET UserID='.$uid.', name='.myv($link,$field['name']).', value='.myv($link,$field['value']).', verified='.$field['verified_at'],__LINE__);
  844. }
  845. }
  846. }
  847. eecho(1,'«'.$opts['hostname'].'»: deleting possible users’ profiles which are in the database but no longer in the directory.'.N);
  848. foreach ($exusers as $locid=>$exuser) {
  849. if (!array_key_exists($locid,$users)) {
  850. eecho(0,'«'.$opts['hostname'].'»: user «'.$exusers[$locid]['username'].'» opted out of the directory, deleting their record ('.$exuser['ID'].')...'.N);
  851. if (!$opts['dryrun']) {
  852. myq($link,'DELETE FROM Users WHERE ID='.$exuser['ID'],__LINE__);
  853. myq($link,'DELETE FROM UsersFields WHERE UserID='.$exuser['ID'],__LINE__);
  854. }
  855. }
  856. }
  857. }
  858. }
  859. mexit('«'.$opts['hostname'].'»: done in '.ght(time()-$now,null,0).' :-)'.N,0);
  860. // functions
  861. function myq(&$link,$query,$line) {
  862. try {
  863. $res=mysqli_query($link,$query);
  864. }
  865. catch (Exception $error) {
  866. mexit('query «'.$query.'» on line '.$line.' failed: '.$error->getMessage().' ('.$error->getCode().').'.N,3);
  867. }
  868. // for php versions < 8, which seem to not catch mysql exceptions
  869. if ($res===false) mexit('query «'.$query.'» on line '.$line.' failed: '.mysqli_error($link).' ('.mysqli_errno($link).').'.N,3);
  870. return($res);
  871. }
  872. function eecho($lev,$msg) {
  873. global $opts, $msglevs;
  874. $time=microtime(false);
  875. $time=explode(' ',$time);
  876. $time=date('Y-m-d H:i:s',$time[1]).'.'.substr($time[0],2);
  877. $msg=$time.' '.$msglevs[$lev].': '.$msg;
  878. if ($lev>=$opts['minmsgimplev']) {
  879. if ($lev<2)
  880. echo($msg);
  881. else
  882. fwrite(STDERR,$msg);
  883. }
  884. }
  885. function mexit($msg,$code) {
  886. global $link;
  887. if (isset($link) && $link!==false) mysqli_close($link);
  888. if ($code!=0)
  889. eecho(3,$msg);
  890. else
  891. eecho(1,$msg);
  892. exit($code);
  893. }
  894. function setint($keys,&$arr) {
  895. foreach ($keys as $key)
  896. if (!is_null($arr[$key]))
  897. $arr[$key]=$arr[$key]+0;
  898. }
  899. function willtrunc($val,$tab,$col) {
  900. global $tables, $iswin;
  901. if ($iswin) $tab=strtolower($tab);
  902. if (is_string($val) && mb_strlen($val,'UTF-8')>$tables[$tab][$col]) return(true);
  903. if (is_int($val) && ($val<$tables[$tab][$col]['min'] || $val>$tables[$tab][$col]['max'])) return(true);
  904. return(false);
  905. }
  906. function truncs($str,$tab,$col,$ctx) {
  907. global $tables, $iswin;
  908. if (is_null($str)) return(null);
  909. if ($iswin)
  910. $tab=strtolower($tab);
  911. $size=$tables[$tab][$col];
  912. $len=mb_strlen($str,'UTF-8');
  913. if ($len>$size) {
  914. $str=mb_substr($str,0,$size-1,'UTF-8').'…';
  915. notify($ctx.': had to truncate string to '.$size.' chars to be able to insert it into «'.$col.'» column in «'.$tab.'» table.',3);
  916. }
  917. return($str);
  918. }
  919. function truncn($num,$tab,$col,$ctx) {
  920. global $tables, $iswin;
  921. if ($iswin)
  922. $tab=strtolower($tab);
  923. if (is_numeric($num)) {
  924. if ($num>$tables[$tab][$col]['max']) {
  925. notify($ctx.': had to ceil «'.$num.'» to «'.$tables[$tab][$col]['max'].'», ie the maximum value it can have in column «'.$col.'» of table «'.$tab.'».',3);
  926. $num=$tables[$tab][$col]['max'];
  927. } elseif ($num<$tables[$tab][$col]['min']) {
  928. notify($ctx.': had to floor «'.$num.'» to «'.$tables[$tab][$col]['min'].'», ie the minimum value it can have in column «'.$col.'» of table «'.$tab.'»).',3);
  929. $num=$tables[$tab][$col]['min'];
  930. }
  931. } else {
  932. notify($ctx.': function «truncn»: expecting a number, got something else; returning «0».',3);
  933. $num=0;
  934. }
  935. return($num);
  936. }
  937. function nocrnl($str) {
  938. return(str_replace(["\r","\n"],['\\r','\\n'],$str));
  939. }
  940. function b2i($bool) {
  941. ($bool) ? $r=1 : $r=0;
  942. return($r);
  943. }
  944. function isempty($str) {
  945. (preg_match('/^\s*$/',$str)===1) ? $r=true : $r=false;
  946. return($r);
  947. }
  948. function notify($msg,$lev,$doecho=true) {
  949. // "$lev" is to be thought of as "$lev" param of function "eecho": 0=debug, 1=info, 2=warning, 3=error
  950. global $link, $tables, $iswin, $opts;
  951. if ($doecho) eecho($lev,'*notification*: '.mb_lcfirst(strip_tags($msg)).N);
  952. if (!$opts['dryrun']) {
  953. ($iswin) ? $tab='notifications' : $tab='Notifications';
  954. myq($link,'INSERT INTO Notifications (ID, Notification, Severity, Microtime, Seen, Deleted) VALUES (NULL, \''.myesc($link,mb_substr($msg,0,$tables[$tab]['Notification'],'UTF-8')).'\', '.$lev.', \''.microtime(true).'\', 0, 0)',__LINE__);
  955. }
  956. }
  957. function mdasortbykey(&$arr,$key,$rev=false) {
  958. $karr=[];
  959. foreach ($arr as $akey=>$subarr)
  960. $karr[$subarr[$key]]=[$akey,$subarr];
  961. if (!$rev)
  962. ksort($karr);
  963. else
  964. krsort($karr);
  965. $arr=[];
  966. foreach ($karr as $akey=>$subarr)
  967. $arr[$subarr[0]]=$subarr[1];
  968. }
  969. // "multi array_key_exists"
  970. function make($keys,&$arr) {
  971. foreach ($keys as $key)
  972. if (!array_key_exists($key,$arr))
  973. return(false);
  974. return(true);
  975. }
  976. function myv(&$link,$var) {
  977. if (is_null($var)) {
  978. return('NULL');
  979. } elseif (is_bool($var)) {
  980. if ($var)
  981. return('1');
  982. else
  983. return('0');
  984. } elseif (trim($var)=='') {
  985. return('NULL');
  986. } else {
  987. return('\''.mysqli_real_escape_string($link,$var).'\'');
  988. }
  989. }
  990. function datetots($date) {
  991. $date=explode('-',$date);
  992. return(mktime(0,0,0,$date[1],$date[2],$date[0]));
  993. }
  994. function ckratelimit($httpresphead) {
  995. $headers=explode("\r\n",$httpresphead);
  996. $buff=[];
  997. array_shift($headers);
  998. foreach ($headers as $header)
  999. if (preg_match('/^([^:]+):(.*)$/Uu',$header,$matches)===1)
  1000. $buff[strtolower($matches[1])]=trim($matches[2]);
  1001. $headers=$buff;
  1002. if (isset($headers['date']) && isset($headers['x-ratelimit-reset']) && isset($headers['x-ratelimit-remaining'])) {
  1003. if ($headers['x-ratelimit-remaining']==0) {
  1004. $stosl=strtotime($headers['x-ratelimit-reset'])-strtotime($headers['date'])+1;
  1005. eecho(2,'reached rate limit, sleeping for '.ght($stosl,null,0).' ...'.N);
  1006. sleep($stosl);
  1007. }
  1008. return($headers['x-ratelimit-remaining']);
  1009. } else {
  1010. $missing=[];
  1011. if (!isset($headers['date'])) $missing[]='date';
  1012. if (!isset($headers['x-ratelimit-reset'])) $missing[]='x-ratelimit-reset';
  1013. if (!isset($headers['x-ratelimit-remaining'])) $missing[]='x-ratelimit-remaining';
  1014. eecho(2,'ckratelimit: $httpresphead did not contain «'.implode('», «',$missing).'» header(s)!'.N);
  1015. return(false);
  1016. }
  1017. }
  1018. /** <LANGUAGE MANAGEMENT> */
  1019. /**
  1020. * Executes a call to Mastodon API.
  1021. *
  1022. * @param string $host Host to be called (e.g.: "mastodon.bida.im")
  1023. * @param string $path API path (e.g.: "/api/v1/timelines/public?local=true")
  1024. * @return mixed An array representing the JSON object as returned by json_decode, or NULL if the call fails
  1025. */
  1026. function get_api($host, $path) {
  1027. global $opts;
  1028. $buf = @gurl('https://'.$host.$path,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
  1029. if ($buf['cont']!==false) {
  1030. ckratelimit($buf['headers']);
  1031. $data = json_decode($buf['cont'], true);
  1032. return $data;
  1033. } else {
  1034. return NULL;
  1035. }
  1036. }
  1037. /**
  1038. * Returns a list of known recognized languages, with the related probability, fot the toot that got passed to it
  1039. *
  1040. * @param mixed $toot The toot to be checked, as returned by the API
  1041. * @return array Associative array with language and related probability
  1042. */
  1043. function get_toot_languages($toot) {
  1044. if (is_array($toot) && array_key_exists('language',$toot))
  1045. $l = $toot['language'];
  1046. else
  1047. $l = NULL;
  1048. $langs=[];
  1049. if($l !== NULL) {
  1050. // the language is explicitly set in the toot, so use that
  1051. $langs[$l] = 1;
  1052. } elseif (is_array($toot) && array_key_exists('content',$toot)) {
  1053. // the language is not explicitly set in the toot, so try and recognize it
  1054. $text = strip_tags($toot['content']);
  1055. $ld = new Language;
  1056. $langs = $ld->detect($text)->bestResults()->close();
  1057. }
  1058. // group derived languages into two-charactes language code (e.g.: "zh-CN" into "zh")
  1059. $grouped_langs = [];
  1060. foreach($langs as $key => $value) {
  1061. $l = explode("-", $key)[0];
  1062. if(array_key_exists($l, $grouped_langs)) {
  1063. $grouped_langs[$l] = max($grouped_langs[$l], $value);
  1064. } else {
  1065. $grouped_langs[$l] = $value;
  1066. }
  1067. }
  1068. return $grouped_langs;
  1069. }
  1070. /**
  1071. * Given the probability of a language for every toot, calculate the average
  1072. *
  1073. * @param array $detected_langs Array of mappings between language and probability
  1074. * @return array Mapping between language and probability
  1075. */
  1076. function summary($detected_langs) {
  1077. $res = [];
  1078. foreach($detected_langs as $langs) {
  1079. foreach($langs as $l => $weight) {
  1080. if(!array_key_exists($l, $res)) {
  1081. $res[$l] = 0;
  1082. }
  1083. $res[$l] += $weight;
  1084. }
  1085. }
  1086. foreach($res as $l => $sumweight) {
  1087. $res[$l] = $sumweight / count($detected_langs);
  1088. }
  1089. return $res;
  1090. }
  1091. /**
  1092. * Helper function for usort: compares two arrays using the first element
  1093. *
  1094. * @param array $entry1 First array to be compared
  1095. * @param array $entry2 Second array to be compared
  1096. * @return number -1, 0 o 1 depening on $entry1[0] being less than, equal to or greater than $entry2[0]
  1097. */
  1098. function sort_weights($entry1, $entry2) {
  1099. $w1 = $entry1[0];
  1100. $w2 = $entry2[0];
  1101. if ($w1 < $w2)
  1102. $ret=1;
  1103. elseif ($w1 == $w2)
  1104. $ret=0;
  1105. else
  1106. $ret=-1;
  1107. return $ret;
  1108. }
  1109. /**
  1110. * Given a language mapping, return a list of probable languages
  1111. *
  1112. * @param array $summary Map between language and probabilty
  1113. * @return string[] List of probable languages
  1114. */
  1115. function get_languages($summary) {
  1116. $lst = [];
  1117. foreach($summary as $code => $weight) {
  1118. $lst[] = [$weight, $code];
  1119. }
  1120. usort($lst, 'sort_weights');
  1121. $languages = [];
  1122. $lastweight = 0;
  1123. foreach($lst as $entry) {
  1124. $l = $entry[1];
  1125. $weight = $entry[0];
  1126. if($weight < $lastweight * 2 / 3) {
  1127. break;
  1128. }
  1129. $languages[] = $l;
  1130. $lastweight = $weight;
  1131. }
  1132. return $languages;
  1133. }
  1134. /**
  1135. * Returns a list of probable languages for the given instance
  1136. *
  1137. * @param string $host Instance’s hostname (e.g.: "mastodon.bida.im")
  1138. * @return string[] List of probable languages
  1139. */
  1140. function get_instance_langs($host) {
  1141. global $opts;
  1142. $data = get_api($host, '/api/v1/timelines/public?local=true&limit='.$opts['ldtoots']);
  1143. if($data == NULL) {
  1144. return [];
  1145. }
  1146. $detected_langs = array_map('get_toot_languages', $data);
  1147. $summary = summary($detected_langs);
  1148. $languages = get_languages($summary);
  1149. return $languages;
  1150. }
  1151. function getlangid(&$link,$lang,&$supplangs,$hostname,$dryrun,$line) {
  1152. $code=locale_canonicalize($lang);
  1153. if (preg_match('/^\s*$/',$lang)===1 || preg_match('/__/',$code)===1) {
  1154. notify('«'.$hostname.'»: «'.$lang.'» is not a valid language code, falling back to default «en».',2,true);
  1155. $code='en';
  1156. }
  1157. $res=myq($link,'SELECT * FROM Languages WHERE Code=\''.myesc($link,$code).'\'',$line);
  1158. $nrows=mysqli_num_rows($res);
  1159. $langs=[];
  1160. if ($nrows==0) {
  1161. $code=myesc($link,truncs($code,'Languages','Code','«'.$hostname.'»'));
  1162. $NameOrig=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,$code)),'Languages','NameOrig','«'.$hostname.'»'));
  1163. foreach ($supplangs as $key=>$val)
  1164. $langs[$key]=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,$key)),'Languages','Name'.strtoupper($key),'«'.$hostname.'»'));
  1165. /*$NamePt_BR=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'pt_BR')),'Languages','NamePT_BR','«'.$hostname.'»'));
  1166. $NameDe=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'de')),'Languages','NameDE','«'.$hostname.'»'));
  1167. $NameUk=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'uk')),'Languages','NameUK','«'.$hostname.'»'));
  1168. $NameCa=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'ca')),'Languages','NameCA','«'.$hostname.'»'));
  1169. $NameEn=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'en')),'Languages','NameEN','«'.$hostname.'»'));
  1170. $NameEs=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'es')),'Languages','NameES','«'.$hostname.'»'));
  1171. $NameFr=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'fr')),'Languages','NameFR','«'.$hostname.'»'));
  1172. $NameGl=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'gl')),'Languages','NameGL','«'.$hostname.'»'));
  1173. $NameIt=myesc($link,truncs(mb_ucfirst(locale_get_display_name($code,'it')),'Languages','NameIT','«'.$hostname.'»'));
  1174. $q='INSERT INTO Languages (ID, Code, NameOrig, NamePT_BR, NameDE, NameUK, NameCA, NameEN, NameES, NameFR, NameGL, NameIT) VALUES (NULL, \''.$code.'\', \''.$NameOrig.'\', \''.$NamePt_BR.'\', \''.$NameDe.'\', \''.$NameUk.'\', \''.$NameCa.'\', \''.$NameEn.'\', \''.$NameEs.'\', \''.$NameFr.'\', \''.$NameGl.'\', \''.$NameIt.'\')';*/
  1175. $q='INSERT INTO Languages (ID, Code, NameOrig, ';
  1176. foreach ($langs as $key=>$val)
  1177. $q.='Name'.strtoupper($key).', ';
  1178. $q=substr($q,0,-2).') VALUES (NULL, \''.$code.'\', \''.$NameOrig.'\', ';
  1179. foreach ($langs as $key=>$val)
  1180. $q.='\''.$val.'\', ';
  1181. $q=substr($q,0,-2).')';
  1182. if (!$dryrun) {
  1183. myq($link,$q,$line);
  1184. $langid=mysqli_insert_id($link);
  1185. } else {
  1186. $langid=0;
  1187. }
  1188. } else {
  1189. if ($nrows>1) notify('In table Languages there are '.$nrows.' records with Code = «'.$code.'» :-(',2,true);
  1190. $row=mysqli_fetch_assoc($res);
  1191. $langid=$row['ID'];
  1192. }
  1193. return($langid);
  1194. }
  1195. function getlangsidsarr(&$langs,&$supplangs,&$link,$hostname,$dryrun,$line) {
  1196. $langids=[];
  1197. foreach ($langs as $lang) {
  1198. $langid=getlangid($link,$lang,$supplangs,$hostname,$dryrun,$line);
  1199. $langids[]=$langid;
  1200. }
  1201. $langids=array_unique($langids);
  1202. return($langids);
  1203. }
  1204. function waituntilonline() {
  1205. $url='www.google.com';
  1206. $gotoff=false;
  1207. while (false===($f=@fsockopen($url,80,$errno,$errstr,1))) {
  1208. $gotoff=true;
  1209. eecho(2,'it seems we are offline ('.$errno.': '.$errstr.'), waiting for 10 seconds before retrying...'.N);
  1210. sleep(5);
  1211. }
  1212. fclose($f);
  1213. if ($gotoff) eecho(1,'it seems we are back online! :-)'.N);
  1214. }
  1215. ?>