index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /*
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. require 'lib/validhostname.php';
  15. require 'lib/ckratelimit.php';
  16. require 'lib/ght.php';
  17. const SNAME='acck';
  18. const SVERS='0.1.7';
  19. const SREPO='https://git.lattuga.net/jones/acck';
  20. const MAXACCLEN=30+253+2;
  21. const MAXHOSTLEN=253;
  22. const INIFP='sec/conf.ini';
  23. $usname=ucfirst(SNAME);
  24. $timeout=5;
  25. $cjr=rand(0,999999);
  26. header('Content-Language: en');
  27. if (file_exists(INIFP)) {
  28. $conf=@parse_ini_file(INIFP,false,INI_SCANNER_RAW);
  29. (isset($conf['use_proxy_with'])) ? $conf['use_proxy_with']=explode(',',$conf['use_proxy_with']) : $conf['use_proxy_with']=null;
  30. if ($conf===false)
  31. die('Configuration file «'.INIFP."» exists but {$usname} could not open it.");
  32. }
  33. $errors=[];
  34. if (isset($_GET['acctock'])) {
  35. if (strlen($_GET['acctock'])>MAXACCLEN) {
  36. $_GET['acctock']='';
  37. $errors[]='Value for «Fediverse account address» is too long';
  38. }
  39. $_GET['acctock']=trim($_GET['acctock']);
  40. if ($_GET['acctock']!='' && preg_match('#^@?[0-9a-zA-Z_]+(@[a-z0-9.-]{4,253})?$#',$_GET['acctock'])!==1)// todo: make it better, like split ecc.
  41. $errors[]='Value for «Fediverse account address» is not valid';
  42. } else {
  43. $_GET['acctock']='';
  44. }
  45. $hostok=false;
  46. if (isset($_GET['host'])) {
  47. if (strlen($_GET['host'])>MAXHOSTLEN) {
  48. $_GET['host']='';
  49. $errors[]='Value for «Mastodon instance domain» is too long';
  50. }
  51. $_GET['host']=trim($_GET['host']);
  52. if ($_GET['host']!='' && !validhostname($_GET['host']))
  53. $errors[]='Value for «Mastodon instance domain» is not valid';
  54. else
  55. $hostok=true;
  56. } else {
  57. $_GET['host']='';
  58. }
  59. $rl=['remaining'=>400,'restime'=>0];
  60. if ($hostok) {
  61. $rlfp='sec/'.$_GET['host'].'.rl.state';
  62. if (file_exists($rlfp)) {
  63. $buf=@file($rlfp,FILE_IGNORE_NEW_LINES);
  64. if ($buf===false)
  65. die('Could not open rate limiting state file.');
  66. if (count($buf)!=2 || preg_match('#^\d+$#',$buf[0])!==1 || preg_match('#^\d+$#',$buf[1])!==1)
  67. die('Malformed rate limiting state file.');
  68. $rl=['remaining'=>$buf[0]+0,'restime'=>$buf[1]+0];
  69. }
  70. }
  71. $now=time();
  72. if ($rl['remaining']<=10 && $now<=$rl['restime'])// ten to leave a margin for "many people using it" and to account for 3 calls to host's endpoints
  73. $errors[]="This {$usname} instance has reached rate limit on «{$_GET['host']}», please wait at least ".ght($rl['restime']-$now,null,0).'.';
  74. if (count($errors)>0)
  75. $errors='<div class="warning">There are some errors<ul><li>'.implode('</li><li>',$errors).'</li></ul></div><div class="horsep"></div>';
  76. else
  77. $errors='';
  78. echo "<!DOCTYPE HTML>
  79. <html lang=\"en\">
  80. <head>
  81. <title>{$usname}</title>
  82. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
  83. <meta name=\"description\" content=\"A tool to check if a fediverse account is moderated by a Mastodon instance\">
  84. <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">
  85. <meta property=\"og:image\" content=\"imgs/ogimage.png\">
  86. <link rel=\"icon\" type=\"image/png\" href=\"imgs/icon-16.png\" sizes=\"16x16\">
  87. <link rel=\"icon\" type=\"image/png\" href=\"imgs/icon-32.png\" sizes=\"32x32\">
  88. <link rel=\"icon\" type=\"image/png\" href=\"imgs/icon-192.png\" sizes=\"192x192\">
  89. <link rel=\"icon\" type=\"image/png\" href=\"imgs/icon-512.png\" sizes=\"512x512\">
  90. <link rel=\"apple-touch-icon-precomposed\" href=\"imgs/icon-180.png\">
  91. <link rel=\"stylesheet\" type=\"text/css\" href=\"css/main.css?v={$cjr}\">
  92. </head>
  93. <body>
  94. <div id=\"main\">
  95. <h1>{$usname}</h1>
  96. <div class=\"normtext\">
  97. <p class=\"firstp\">Hello, this is {$usname}, a tool to easily check if a fediverse account is <a href=\"https://docs.joinmastodon.org/admin/moderation/#limit-user\">limited</a> (AKA «silenced») and-or <a href=\"https://docs.joinmastodon.org/admin/moderation/#suspend-user\">suspended</a> (AKA «blocked») by a Mastodon instance.</p>
  98. <p>Since an account is reported as limited and-or suspended by a Mastodon instance even when that instance has limited and-or suspended the whole account’s instance, {$usname} also tries to detect if this is the case, and tells about it.</p>
  99. <!-- <p>To check if an account is moderated by the given server, {$usname} tries to use the server’s <a href=\"https://docs.joinmastodon.org/methods/accounts/#lookup\">«/api/v1/accounts/lookup» API endpoint</a>, that is public and was introduced in Mastodon v3.4.0; to check if the server is moderating the whole account’s domain, {$usname} tries to use the server’s <a href=\"https://docs.joinmastodon.org/methods/instance/#domain_blocks\">«/api/v1/instance/domain_blocks» API endpoint</a>, that is not always set to public by the server’s admins, and was introduced in Mastodon v4.0.0. If it can’t use one or both of these endpoints, it tells.</p> -->
  100. <p>{$usname} does not use cookies or Javascript and does not store any data about you anywhere.</p>
  101. <p>You can find {$usname}’s code <a href=\"".SREPO."\">here</a>, and you can contact its developer on Mastodon <a href=\"https://puntarella.party/@j0nes\">here</a>.</p>
  102. </div>
  103. ".$errors."
  104. <form method=\"get\" id=\"mainform\" name=\"mainform\">
  105. <div class=\"inputdiv\"><label for=\"acctock\">Fediverse account address</label><input type=\"text\" id=\"acctock\" name=\"acctock\" class=\"input\" placeholder=\"Example: DonaldDuck@duckburg.social\" value=\"{$_GET['acctock']}\" maxlength=\"".MAXACCLEN."\" required></div>
  106. <div class=\"inputdiv\"><label for=\"host\">Mastodon instance domain</label><input type=\"text\" id=\"host\" name=\"host\" class=\"input\" placeholder=\"Example: mastodon.social\" value=\"{$_GET['host']}\" maxlength=\"".MAXHOSTLEN."\" required></div>
  107. <div class=\"lastinputdiv\"><button type=\"submit\" id=\"button\" class=\"button\">Check</button></div>\n</form>\n";
  108. if ($errors=='' && $_GET['acctock']!='' && $_GET['host']!='') {
  109. echo "<div class=\"horsep\"></div>\n";
  110. $context=[
  111. 'http'=>[
  112. 'method'=>'GET',
  113. 'ignore_errors'=>true,
  114. 'protocol_version'=>1.1,
  115. 'user_agent'=>'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0',
  116. 'timeout'=>5
  117. ]
  118. ];
  119. if (isset($conf['proxy']) && (is_null($conf['use_proxy_with']) || in_array(strtolower($_GET['host']),$conf['use_proxy_with'])))
  120. $context['http']['proxy']=$conf['proxy'];
  121. $context=stream_context_create($context);
  122. $hhost=htmlentities($_GET['host']);
  123. $acchost=preg_replace('#^@?[^@]+@(.*)$#','$1',$_GET['acctock']);
  124. if ($acchost==$_GET['acctock'] || $acchost=='')
  125. $acchost=$_GET['host'];
  126. $acchostck=false;
  127. $http_response_header=null;
  128. $url=$_GET['host'].'/api/v1/instance/domain_blocks';
  129. $hurl=htmlentities($url);
  130. $res=@file_get_contents('https://'.$url,false,$context);
  131. //echo preprint($http_response_header);
  132. if (isset($http_response_header))
  133. $rl=ckratelimit($http_response_header);
  134. if ($res!==false && is_array($http_response_header) && gethttpcode($http_response_header)=='200' && null!==$res=@json_decode($res,true)) {
  135. foreach ($res as $val) {
  136. if (isset($val['domain'],$val['severity']) && $val['domain']==$acchost) {
  137. if ($val['severity']=='silence')
  138. $acchostck="The whole domain of the account to check, «{$acchost}», is limited by «{$_GET['host']}».";
  139. elseif ($val['severity']=='suspend')
  140. $acchostck="The whole domain of the account to check, «{$acchost}», is suspended by «{$_GET['host']}».";
  141. else
  142. $acchostck="The whole domain of the account to check, «{$acchost}», is moderated by «{$_GET['host']}», but {$usname} could not detect the moderation severity.";
  143. break;
  144. }
  145. }
  146. if ($acchostck===false)
  147. $acchostck="The domain of the account to check, «{$acchost}», is neither limited nor suspended by «{$_GET['host']}».";
  148. } else {
  149. $acchostck="{$usname} could not detect if the domain of the account to check, «{$acchost}», is limited or suspended by «{$_GET['host']}».";
  150. }
  151. $acchostck=htmlentities($acchostck);
  152. $http_response_header=null;
  153. $httpcode=null;
  154. $url=$_GET['host'].'/api/v1/accounts/lookup?acct='.urlencode($_GET['acctock']);
  155. $hacctock=htmlentities($_GET['acctock']);
  156. $hurl=htmlentities($url);
  157. $res=@file_get_contents('https://'.$url,false,$context);
  158. //echo preprint($http_response_header);
  159. if (isset($http_response_header)) {
  160. $httpcode=gethttpcode($http_response_header);
  161. $rl=ckratelimit($http_response_header);
  162. }
  163. if ($res===false) {
  164. echo "<div class=\"error\">Error: could not connect to «{$hhost}».</div>\n";
  165. } elseif (isset($httpcode) && $httpcode!='200') {
  166. echo "<div class=\"error\">Error: «{$hurl}» returned an HTTP code that is different than «200»: <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/{$httpcode}\">{$httpcode}</a>.</div>\n";
  167. } elseif (null===$res=@json_decode($res,true)) {
  168. echo "<div class=\"error\">Error: «{$hurl}» returned data that could not be parsed as JSON (".json_last_error().': '.json_last_error_msg().").</div>\n";
  169. } elseif (isset($res['error'])) {
  170. if ($res['error']=='Record not found')
  171. echo "<div class=\"neutral\">«{$hhost}» does not know the «{$hacctock}» account.<br><br>{$acchostck}</div>\n";
  172. else
  173. echo "<div class=\"error\">Error: «{$hurl}» replied with this error message: «".htmlentities($res['error'])."».</div>\n";
  174. } elseif (!isset($res['id'])) {
  175. echo "<div class=\"error\">Error: «{$hurl}» returned data in an unexpected format.</div>\n";
  176. } else {
  177. $out="<div class=\"neutral\">\n«{$hacctock}» ";
  178. (isset($res['limited']) && $res['limited']) ? $out.='is' : $out.='is not';
  179. $out.=" limited by «{$hhost}».<br>\n«{$hacctock}» ";
  180. (isset($res['suspended']) && $res['suspended']) ? $out.='is' : $out.='is not';
  181. $out.=" suspended by «{$hhost}».<br>\n<br>\n{$acchostck}</div>\n";
  182. echo $out;
  183. }
  184. if (isset($rl['ok']) && $rl['ok'] && @file_put_contents($rlfp,$rl['remaining']."\n".($rl['sleep']+time())."\n")===false)
  185. echo "<div class=\"warning\">Warning: could not write to rate limit state file.</div>\n";
  186. }
  187. if (isset($conf['footer']))
  188. echo "<div id=\"almfooter\">{$conf['footer']}</div>\n";
  189. echo "<div id=\"footer\"><a href=\"".SREPO."\">{$usname} ".SVERS."</a></div>
  190. </div>
  191. </body>
  192. </html>\n";
  193. function preprint($var) {
  194. return '<pre>'.print_r($var,true)."</pre>\n";
  195. }
  196. function gethttpcode($headers) {
  197. return preg_replace('#^[^ ]+ (\d+).*$#','$1',$headers[0]);
  198. }
  199. ?>