pull.php 11.7 KB
Newer Older
MatrixCrawler's avatar
MatrixCrawler committed
1
<?php
2
//* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */
3

David Morley's avatar
David Morley committed
4 5 6
$debug   = isset($_GET['debug']) || (isset($argv) && in_array('debug', $argv, true));
$newline = PHP_SAPI === 'cli' ? "\n" : '<br>';

7 8 9
// Other parameters.
$_domain = $_GET['domain'] ?? '';

10
require_once __DIR__ . '/../config.php';
dmorley's avatar
cleanup  
dmorley committed
11

dmorley's avatar
dmorley committed
12
//get master code version for diaspora pods
dmorley's avatar
cleanup  
dmorley committed
13
$mv = curl_init();
14
curl_setopt($mv, CURLOPT_URL, 'https://raw.githubusercontent.com/diaspora/diaspora/master/config/defaults.yml');
dmorley's avatar
cleanup  
dmorley committed
15 16 17 18 19 20 21
curl_setopt($mv, CURLOPT_POST, 0);
curl_setopt($mv, CURLOPT_HEADER, 0);
curl_setopt($mv, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($mv, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($mv, CURLOPT_NOBODY, 0);
$outputmv = curl_exec($mv);
curl_close($mv);
dmorley's avatar
dmorley committed
22
preg_match('/number:.*"(.*)"/', $outputmv, $version);
dmorley's avatar
cleanup  
dmorley committed
23
$dmasterversion = trim($version[1], '"');
David Morley's avatar
David Morley committed
24
_debug('Diaspora Masterversion', $dmasterversion);
dmorley's avatar
cleanup  
dmorley committed
25

dmorley's avatar
dmorley committed
26
//get master code version for friendica pods
dmorley's avatar
cleanup  
dmorley committed
27
$mv = curl_init();
28
curl_setopt($mv, CURLOPT_URL, 'https://raw.githubusercontent.com/friendica/friendica/master/boot.php');
dmorley's avatar
cleanup  
dmorley committed
29 30 31 32 33 34 35
curl_setopt($mv, CURLOPT_POST, 0);
curl_setopt($mv, CURLOPT_HEADER, 0);
curl_setopt($mv, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($mv, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($mv, CURLOPT_NOBODY, 0);
$outputmv = curl_exec($mv);
curl_close($mv);
dmorley's avatar
dmorley committed
36
preg_match('/define.*\'FRIENDICA_VERSION\'.*\'(.*)\'/', $outputmv, $version);
dmorley's avatar
cleanup  
dmorley committed
37
$fmasterversion = trim($version[1], '"');
David Morley's avatar
David Morley committed
38
_debug('Friendica Masterversion: ' . $fmasterversion);
39

dmorley's avatar
dmorley committed
40 41 42 43 44 45 46 47 48 49
//get master code version for hubzilla pods
$mv = curl_init();
curl_setopt($mv, CURLOPT_URL, 'https://raw.githubusercontent.com/redmatrix/hubzilla/master/boot.php');
curl_setopt($mv, CURLOPT_POST, 0);
curl_setopt($mv, CURLOPT_HEADER, 0);
curl_setopt($mv, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($mv, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($mv, CURLOPT_NOBODY, 0);
$outputmv = curl_exec($mv);
curl_close($mv);
dmorley's avatar
dmorley committed
50
preg_match('/define.*\'STD_VERSION\'.*\'(.*)\'/', $outputmv, $version);
dmorley's avatar
dmorley committed
51 52 53
$hmasterversion = trim($version[1], '"');
_debug('Hubzilla Masterversion: ' . $hmasterversion);

54 55
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
$dbh || die('Error in connection: ' . pg_last_error());
MatrixCrawler's avatar
MatrixCrawler committed
56

dmorley's avatar
cleanup  
dmorley committed
57
//foreach pod check it and update db
58
if ($_domain) {
David Morley's avatar
David Morley committed
59
  $sql = 'SELECT domain,stats_apikey,score,date_created,adminrating,weight FROM pods WHERE domain = $1';
60 61
  $result = pg_query_params($dbh, $sql, [$_domain]);
} elseif (PHP_SAPI === 'cli') {
David Morley's avatar
David Morley committed
62
  $sql = 'SELECT domain,stats_apikey,score,date_created,adminrating,weight FROM pods';
dmorley's avatar
cleanup  
dmorley committed
63
  $result = pg_query($dbh, $sql);
David Morley's avatar
David Morley committed
64 65
} else {
  die('No valid input');
dmorley's avatar
cleanup  
dmorley committed
66
}
67 68
$result || die('Error in SQL query1: ' . pg_last_error());

David Morley's avatar
David Morley committed
69 70 71 72 73 74 75 76 77
while ($row = pg_fetch_assoc($result)) {
  $domain    = $row['domain'];
  $score     = (int) $row['score'];
  $dateadded = $row['date_created'];
  $admindb   = (int) $row['adminrating'];
  $weight    = $row['weight'];
  $sqlforr   = 'SELECT admin,rating FROM rating_comments WHERE domain = $1';
  $ratings   = pg_query_params($dbh, $sqlforr, [$domain]);
  $ratings || die('Error in SQL query2: ' . pg_last_error());
78

David Morley's avatar
David Morley committed
79
  _debug('Domain', $domain);
noplanman's avatar
noplanman committed
80

David Morley's avatar
David Morley committed
81 82 83 84 85 86 87
  $user_ratings  = [];
  $admin_ratings = [];
  while ($rating = pg_fetch_assoc($ratings)) {
    if ($rating['admin'] == 0) {
      $user_ratings[] = $rating['rating'];
    } elseif ($rating['admin'] == 1) {
      $admin_ratings[] = $rating['rating'];
88
    }
David Morley's avatar
David Morley committed
89 90 91
  }
  $user_rating  = empty($user_ratings) ? 0 : max(10, round(array_sum($user_ratings) / count($user_ratings), 2));
  $admin_rating = empty($admin_ratings) ? 0 : max(10, round(array_sum($admin_ratings) / count($admin_ratings), 2));
noplanman's avatar
noplanman committed
92

David Morley's avatar
David Morley committed
93 94 95
  if ($admindb == -1) {
    $admin_rating = -1;
  }
96

David Morley's avatar
David Morley committed
97 98 99 100 101 102 103 104 105
  $chss = curl_init();
  curl_setopt($chss, CURLOPT_URL, 'https://' . $domain . '/nodeinfo/1.0');
  curl_setopt($chss, CURLOPT_POST, 0);
  curl_setopt($chss, CURLOPT_HEADER, 0);
  curl_setopt($chss, CURLOPT_CONNECTTIMEOUT, 9);
  curl_setopt($chss, CURLOPT_TIMEOUT, 9);
  curl_setopt($chss, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($chss, CURLOPT_NOBODY, 0);
  curl_setopt($chss, CURLOPT_CERTINFO, 1);
dmorley's avatar
dmorley committed
106
  curl_setopt($chss, CURLOPT_VERBOSE, 0);
David Morley's avatar
David Morley committed
107 108 109
  $outputssl      = curl_exec($chss);
  $outputsslerror = curl_error($chss);
  $info           = curl_getinfo($chss, CURLINFO_CERTINFO);
110
  $ttl            = curl_getinfo($chss, CURLINFO_CONNECT_TIME);
dmorley's avatar
dmorley committed
111
  $sslexpire      = $info[0]['Expire date'] ?? null;
David Morley's avatar
David Morley committed
112
  curl_close($chss);
113

David Morley's avatar
David Morley committed
114 115
  _debug('Nodeinfo output', $outputssl, true);
  _debug('Nodeinfo output error', $outputsslerror, true);
116 117 118
  _debug('Cert expire date', $sslexpire);
  _debug('TTL', $ttl);
  
David Morley's avatar
David Morley committed
119 120
  //get new json from nodeinfo
  $jsonssl = json_decode($outputssl);
noplanman's avatar
noplanman committed
121

dmorley's avatar
updates  
dmorley committed
122
  if (!$jsonssl) {    
dmorley's avatar
dmorley committed
123
    _debug('Connection', 'Can not connect to pod');
David Morley's avatar
David Morley committed
124

125 126
    $sql_errors    = 'INSERT INTO checks (domain, online, error, ttl) VALUES ($1, $2, $3, $4)';
    $result_errors = pg_query_params($dbh, $sql_errors, [$domain, (int) false, $outputsslerror, $ttl]);
dmorley's avatar
dmorley committed
127
    $result_errors || die('Error in SQL query: ' . pg_last_error());
David Morley's avatar
David Morley committed
128
  }
noplanman's avatar
noplanman committed
129

David Morley's avatar
David Morley committed
130
  if ($jsonssl !== null) {
131 132
    $sql_checks    = 'INSERT INTO checks (domain, online, ttl) VALUES ($1, $2, $3)';
    $result_checks = pg_query_params($dbh, $sql_checks, [$domain, (int) true, $ttl]);
dmorley's avatar
dmorley committed
133
    $result_checks || die('Error in SQL query: ' . pg_last_error());
dmorley's avatar
dmorley committed
134 135
    
    (!$jsonssl->software->version) || $score += 1;
David Morley's avatar
David Morley committed
136 137 138 139
    $xdver        = $jsonssl->software->version ?? 0;
    $dverr        = explode('-', trim($xdver));
    $shortversion = $dverr[0];
    _debug('Version code', $shortversion);
dmorley's avatar
dmorley committed
140
    $signup                = ($jsonssl->openRegistrations === true) ? true : false;
David Morley's avatar
David Morley committed
141 142 143 144 145 146 147 148 149 150 151
    $softwarename          = $jsonssl->software->name ?? 'null';
    $name                  = $jsonssl->metadata->nodeName ?? 'null';
    $total_users           = $jsonssl->usage->users->total ?? 0;
    $active_users_halfyear = $jsonssl->usage->users->activeHalfyear ?? 0;
    $active_users_monthly  = $jsonssl->usage->users->activeMonth ?? 0;
    $local_posts           = $jsonssl->usage->localPosts ?? 0;
    $comment_counts        = $jsonssl->usage->localComments ?? 0;
    $service_facebook      = in_array('facebook', $jsonssl->services->outbound, true);
    $service_twitter       = in_array('twitter', $jsonssl->services->outbound, true);
    $service_tumblr        = in_array('tumblr', $jsonssl->services->outbound, true);
    $service_wordpress     = in_array('wordpress', $jsonssl->services->outbound, true);
dmorley's avatar
updates  
dmorley committed
152
    $service_xmpp          = $jsonssl->metadata->xmppChat === true ?? false;
David Morley's avatar
David Morley committed
153 154 155 156 157 158 159
  } else {
    $score -= 1;
    $dver         = '.connect error';
    $shortversion = 0;
  }

  _debug('Signup Open', $signup);
160
  $ip6 = exec(escapeshellcmd('dig @74.82.42.42 +nocmd ' . $domain . ' aaaa +noall +short'));
161
  $iplookup = [];
162
  exec(escapeshellcmd('delv @74.82.42.42 ' . $domain), $iplookup);
dmorley's avatar
dmorley committed
163
  if ($iplookup) {
dmorley's avatar
dmorley committed
164
    _debug('Iplookup', $iplookup, true);
dmorley's avatar
updates  
dmorley committed
165
    $dnssec = in_array('; fully validated', $iplookup) ?? false ;
166 167
    $getaonly = array_values(preg_grep('/A\s.*/', $iplookup));
    preg_match('/A\s(.*)/', $getaonly[0], $version);
dmorley's avatar
dmorley committed
168 169
    $ip   = trim($version[1]);
  }
dmorley's avatar
dmorley committed
170
  $ip || $score -= 2;
David Morley's avatar
David Morley committed
171 172
  $ipv6 = strpos($ip6, ':') !== false;
  _debug('IP', $ip);
dmorley's avatar
dmorley committed
173
  _debug('IPv6', $ip6);
David Morley's avatar
David Morley committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

  $location = geoip_record_by_name($ip);
  _debug('Location', $location, true);

  if ($location) {
    $country = !empty($location['country_code']) ? iconv('UTF-8', 'UTF-8//IGNORE', $location['country_code']) : null;
    $city    = !empty($location['city']) ? iconv('UTF-8', 'UTF-8//IGNORE', $location['city']) : null;
    $state   = !empty($location['region']) ? iconv('UTF-8', 'UTF-8//IGNORE', $location['region']) : null;
    $lat     = !empty($location['latitude']) ? $location['latitude'] : null;
    $long    = !empty($location['longitude']) ? $location['longitude'] : null;
  }
  echo $newline;
  $statslastdate = date('Y-m-d H:i:s');
  $ping          = curl_init();
  curl_setopt($ping, CURLOPT_URL, 'https://api.uptimerobot.com/getMonitors?format=json&noJsonCallback=1&customUptimeRatio=7-30-60-90&responseTimes=1&responseTimesAverage=86400&apiKey=' . $row['stats_apikey']);
  curl_setopt($ping, CURLOPT_POST, 0);
  curl_setopt($ping, CURLOPT_HEADER, 0);
  curl_setopt($ping, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ping, CURLOPT_CONNECTTIMEOUT, 8);
  curl_setopt($ping, CURLOPT_NOBODY, 0);
  curl_setopt($ping, CURLOPT_MAXCONNECTS, 5);
  curl_setopt($ping, CURLOPT_FOLLOWLOCATION, true);
  $uptr = json_decode(curl_exec($ping));
  curl_close($ping);
  _debug('Uptime Robot', $uptr, true);
dmorley's avatar
dmorley committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  
  if ($uptr->stat === 'ok') {
    $responsetime    = $uptr->monitors->monitor{'0'}->responsetime{'0'}->value ?? 'n/a';
    $uptimerobotstat = $uptr->stat;
    $uptime          = $uptr->monitors->monitor{'0'}->alltimeuptimeratio;
    $uptime_custom   = $uptr->monitors->monitor{'0'}->customuptimeratio;
    $diff            = (new DateTime())->diff(new DateTime($dateadded));
    $months          = $diff->m + ($diff->y * 12);
    if ($uptr->monitors->monitor{'0'}->status == 2) {
      $status = 'Up';
    }
    if ($uptr->monitors->monitor{'0'}->status == 0) {
      $status = 'Paused';
    }
    if ($uptr->monitors->monitor{'0'}->status == 1) {
      $status = 'Not Checked Yet';
    }
    if ($uptr->monitors->monitor{'0'}->status == 8) {
      $status = 'Seems Down';
    }
    if ($uptr->monitors->monitor{'0'}->status == 9) {
      $status = 'Down';
    }
      $statslastdate = date('Y-m-d H:i:s');
David Morley's avatar
David Morley committed
223
  }
noplanman's avatar
noplanman committed
224

David Morley's avatar
David Morley committed
225 226 227 228
  if ($softwarename === 'diaspora') {
    $masterversion = $dmasterversion;
  } elseif ($softwarename === 'friendica') {
    $masterversion = $fmasterversion;
dmorley's avatar
dmorley committed
229 230
  } elseif ($softwarename === 'redmatrix') {
    $masterversion = $hmasterversion;
David Morley's avatar
David Morley committed
231 232 233 234 235 236 237 238 239 240 241
  }
  $hidden = $score <= 70;
  _debug('Hidden', $hidden ? 'yes' : 'no');
  // lets cap the scores or you can go too high or too low to never be effected by them
  if ($score > 100) {
    $score = 100;
  } elseif ($score < 0) {
    $score = 0;
  }
  $weightedscore = ($uptime + $score + ($active_users_monthly / 19999) - ((10 - $weight) * .12));
  //sql it
noplanman's avatar
noplanman committed
242

David Morley's avatar
David Morley committed
243 244
  $timenow = date('Y-m-d H:i:s');
  $sql_set     = 'UPDATE pods SET secure = $2, hidden = $3, ip = $4, ipv6 = $5, monthsmonitored = $6, uptime_alltime = $7, status = $8, date_laststats = $9, date_updated = $10, responsetime = $11, score = $12, adminrating = $13, country = $14, city = $15, state = $16, lat = $17, long = $18, userrating = $19, shortversion = $20, masterversion = $21, signup = $22, total_users = $23, active_users_halfyear = $24, active_users_monthly = $25, local_posts = $26, name = $27, comment_counts = $28, service_facebook = $29, service_tumblr = $30, service_twitter = $31, service_wordpress = $32, weightedscore = $33, service_xmpp = $34, softwarename = $35, sslvalid = $36, uptime_custom = $37, dnssec = $38, sslexpire = $39 WHERE domain = $1';
dmorley's avatar
dmorley committed
245
  $result_set  = pg_query_params($dbh, $sql_set, [$domain, 1, (int) $hidden, $ip, (int) $ipv6, $months, $uptime, $status, $statslastdate, $timenow, $responsetime, $score, $admin_rating, $country, $city, $state, $lat, $long, $user_rating, $shortversion, $masterversion, (int) $signup, $total_users, $active_users_halfyear, $active_users_monthly, $local_posts, $name, $comment_counts, (int) $service_facebook, (int) $service_tumblr, (int) $service_twitter, (int) $service_wordpress, $weightedscore, (int) $service_xmpp, $softwarename, $outputsslerror, $uptime_custom, (int) $dnssec, $sslexpire]);
David Morley's avatar
David Morley committed
246 247 248
  $result_set || die('Error in SQL query3: ' . pg_last_error());

  _debug('Score out of 100', $score);
MatrixCrawler's avatar
MatrixCrawler committed
249

David Morley's avatar
David Morley committed
250
  echo 'Success';
dmorley's avatar
dmorley committed
251
  
David Morley's avatar
David Morley committed
252 253 254
  echo $newline;
  echo $newline;
}
255

David Morley's avatar
David Morley committed
256 257 258 259 260 261 262 263 264
/**
 * Output a debug message and variable value
 * 
 * @param string $label
 * @param mixed  $var
 * @param bool   $dump
 */
function _debug($label, $var = null, $dump = false) {
  global $debug, $newline;
265

David Morley's avatar
David Morley committed
266 267
  if (!$debug) {
    return;
dmorley's avatar
cleanup  
dmorley committed
268
  }
David Morley's avatar
David Morley committed
269 270 271 272 273 274 275 276 277 278
  
  if ($dump || is_array($var)) {
    $output = print_r($var, true); 
  } elseif (is_bool($var)) {
    $output = $var ? 'true' : 'false';
  } else {
    $output = (string) $var;
  }
  
  printf('%s: %s%s', $label, $output, $newline);
279
}