pull.php 14.4 KB
Newer Older
MatrixCrawler's avatar
MatrixCrawler committed
1
<?php
dmorley's avatar
dmorley committed
2 3
$debug = isset($_GET['debug'])?1:0;
$debug=1;
4 5
//$debug = isset($argv[1])?1:0;
//* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */
dmorley's avatar
cleanup  
dmorley committed
6 7
include('config.php');

dmorley's avatar
dmorley committed
8
//get master code version for diaspora pods
dmorley's avatar
cleanup  
dmorley committed
9 10 11 12 13 14 15 16 17 18 19 20 21
$mv = curl_init();
curl_setopt($mv, CURLOPT_URL, "https://raw.githubusercontent.com/diaspora/diaspora/master/config/defaults.yml");
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);
preg_match('/number: "(.*?)"/',$outputmv,$version);
$dmasterversion = trim($version[1], '"');
if ($debug) {echo "Diaspora Masterversion: ".$dmasterversion."<br>";} 

dmorley's avatar
dmorley committed
22
//get master code version for freindica pods
dmorley's avatar
cleanup  
dmorley committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
$mv = curl_init();
curl_setopt($mv, CURLOPT_URL, "https://raw.githubusercontent.com/friendica/friendica/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);
preg_match('/FRIENDICA_VERSION\',      \'(.*?)\'/',$outputmv,$version);
$fmasterversion = trim($version[1], '"');
if ($debug) {echo "Frendica Masterversion: ".$fmasterversion."<br>";}
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
$dbh2 = pg_connect("dbname=$pgdb user=$pguser password=$pgpass"); 
if (!$dbh) {
  die("Error in connection: " . pg_last_error());
}
MatrixCrawler's avatar
MatrixCrawler committed
40

dmorley's avatar
cleanup  
dmorley committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
//foreach pod check it and update db
$domain = isset($_GET['domain'])?$_GET['domain']:null;
if ($domain) {
  $sql = "SELECT domain,pingdomurl,score,datecreated,weight FROM pods WHERE domain = $1";
  $sleep="0";
  $result = pg_query_params($dbh, $sql, array($domain));
} else {
  $sql = "SELECT domain,pingdomurl,score,datecreated,adminrating,weight FROM pods";
  $sleep="1";
  $result = pg_query($dbh, $sql);
}
if (!$result) {die("Error in SQL query1: " . pg_last_error());}
 
while ($row = pg_fetch_all($result)) {
  $numrows = pg_num_rows($result);
   for ($i = 0; $i < $numrows; $i++) {
57 58 59 60
     $domain =  $row[$i]['domain'];
     $score = $row[$i]['score'];
     $dateadded = $row[$i]['datecreated'];
     $admindb = $row[$i]['adminrating'];
61
     $weight = $row[$i]['weight'];
dmorley's avatar
cleanup  
dmorley committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
     //get ratings
     $userrate=0;$adminrate=0;$userratingavg = array();$adminratingavg = array();$userrating = array();$adminrating = array();
     $sqlforr = "SELECT * FROM rating_comments WHERE domain = $1";
     $ratings = pg_query_params($dbh, $sqlforr, array($domain));
     if (!$ratings) {
       die("Error in SQL query2: " . pg_last_error());
     }
     $numratings = pg_num_rows($ratings);
     while($myrow = pg_fetch_assoc($ratings)) {
       if ($myrow['admin'] == 0) {
         $userratingavg[] = $myrow['rating'];$userrate++;
       } elseif ($myrow['admin'] == 1) {
         $adminratingavg[] = $myrow['rating'];$adminrate++;
       }  
     }
MatrixCrawler's avatar
MatrixCrawler committed
77

dmorley's avatar
cleanup  
dmorley committed
78 79 80 81 82 83 84 85
     if ($userrate > 0) {$userrating = round(array_sum($userratingavg) / $userrate,2);}
     if ($adminrate > 0) {$adminrating = round(array_sum($adminratingavg) / $adminrate,2);}
     if ($debug) {echo "Domain: ".$domain."<br>";}
     if (!$userrating) {$userrating=0;}
     if ($userrating > 10) {$userrating=10;}
     if (!$adminrating) {$adminrating=0;}
     if ($adminrating > 10) {$adminrating=10;}
     if ($admindb == -1) {$adminrating=-1;}
86
     pg_free_result($ratings);
dmorley's avatar
cleanup  
dmorley committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
     $userrate=0;$adminrate=0;
     unset($userratingavg);
     unset($adminratingavg);
     unset($name);
     unset($total_users);
     unset($active_users_halfyear);
     unset($active_users_monthly);
     unset($local_posts);
     unset($registrations_open);
     unset($comment_counts);
     unset($service_facebook);
     unset($service_twitter);
     unset($service_tumblr);
     unset($service_wordpess);
     unset($dver);
     unset($dverr);
     unset($xdver);
     unset($xmpp);
     unset($softwarename);
     $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);
dmorley's avatar
dmorley committed
110 111 112
     curl_setopt($chss, CURLOPT_CONNECTTIMEOUT, 5);
     curl_setopt($chss, CURLOPT_TIMEOUT, 9);
     curl_setopt($chss, CURLOPT_RETURNTRANSFER, 5);
dmorley's avatar
cleanup  
dmorley committed
113 114 115
     curl_setopt($chss, CURLOPT_NOBODY, 0);
     $outputssl = curl_exec($chss);      
     curl_close($chss);
116

dmorley's avatar
cleanup  
dmorley committed
117 118 119 120
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "http://".$domain."/nodeinfo/1.0");
     curl_setopt($ch, CURLOPT_POST, 0);
     curl_setopt($ch, CURLOPT_HEADER, 0);
dmorley's avatar
dmorley committed
121 122
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
dmorley's avatar
cleanup  
dmorley committed
123 124 125 126 127 128
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_NOBODY, 0);
     $output = curl_exec($ch);
     curl_close($ch);
     if ($debug) {echo "not-e"; print $output;}
     if ($debug) {echo "e"; var_dump($outputssl);}
dmorley's avatar
dmorley committed
129
     if (!$output && !$outpulssl && !$domain) {continue;echo "no connection to pod";}
dmorley's avatar
cleanup  
dmorley committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212
     if ($outputssl) {$secure="true";$outputresults=$outputssl;} elseif ($output) {$secure="false";$outputresults=$output;}
     if (stristr($outputresults, 'openRegistrations')) {
       $score = $score +1;
       if ($debug) {echo "Secure: ".$secure."<br>";}
       //get new json from nodeinfo
       $jsonssl = json_decode($outputresults);
       var_dump($jsonssl);
       if ($jsonssl->openRegistrations === true) {$registrations_open=1;}
       $xdver = isset($jsonssl->software->version)?$jsonssl->software->version:0;
       $dverr = explode("-",trim($xdver));
       $dver = $dverr[0];
       if ($debug) {echo " <br> Version code: ".$dver."<br>";}
       if (!$dver) {$score = $score-2;}
       $softwarename = isset($jsonssl->software->name)?$jsonssl->software->name:"null";
       $name = isset($jsonssl->metadata->nodeName)?$jsonssl->metadata->nodeName:"null";
       $total_users = isset($jsonssl->usage->users->total)?$jsonssl->usage->users->total:0;
       $active_users_halfyear = isset($jsonssl->usage->users->activeHalfyear)?$jsonssl->usage->users->activeHalfyear:0;
       $active_users_monthly = isset($jsonssl->usage->users->activeMonth)?$jsonssl->usage->users->activeMonth:0;
       $local_posts = isset($jsonssl->usage->localPosts)?$jsonssl->usage->localPosts:0;
       $comment_counts = isset($jsonssl->usage->localComments)?$jsonssl->usage->localComments:0;
       if (array_search('facebook', $jsonssl->services->outbound) !== false) {$service_facebook='true';} else {$service_facebook='false';}
       if (array_search('twitter', $jsonssl->services->outbound) !== false) {$service_twitter='true';} else {$service_twitter='false';}
       if (array_search('tumblr', $jsonssl->services->outbound) !== false) {$service_tumblr='true';} else {$service_tumblr='false';}
       if (array_search('wordpress', $jsonssl->services->outbound) !== false) {$service_wordpress='true';} else {$service_wordpress='false';}
       if ($jsonssl->metadata->xmppChat === true) {$xmpp = 'true';} else {$xmpp = 'false';}
     } else {
     $secure="false";
     $score = $score - 1;
     $dver =".connect error";
     $dverr=0;
     //no diaspora cookie on either, lets set this one as hidden and notify someone its not really a pod
     //could also be a ssl pod with a bad cert, I think its ok to call that a dead pod now
     }
     $signup = $registrations_open;
     if ($debug) {echo "<br>Signup Open: ".$signup."<br>";}
     if ($score > 5) {
       $hidden = "no";
     } else {
       $hidden = "yes";
     }
     if ($debug) {echo "Hidden: ".$hidden."<br>";}
     // lets cap the scores or you can go too high or too low to never be effected by them
     if ($score > 20) {
       $score = 20;
     } elseif ($score < -20) {
       $score = -20;
     }
     $ip6 = escapeshellcmd('dig +nocmd '.$domain.' aaaa +noall +short');
     $ip = escapeshellcmd('dig +nocmd '.$domain.' a +noall +short');
     $ip6num = exec($ip6);
     $ipnum = exec($ip);
     $test = strpos($ip6num, ":");
     if ($test === false) {
       $ipv6="no";
     } else {
       $ipv6="yes";
     }
     if ($debug) {echo "IP: ".$ipnum."<br>";}
     $location = geoip_record_by_name($ipnum);
     if ($debug) {echo " Location: "; var_dump($location); echo "<br>";}
     if ($location) {
       $ipdata = "Country: ".$location["country_name"]."\n";
       $whois = "Country: ".$location["country_name"]."\n Lat:".$location["latitude"]." Long:".$location["longitude"];
       $country=$location["country_code"];
       $city=isset($location->city)?iconv("UTF-8", "UTF-8//IGNORE", $location->city):null;
       $state="";
       $months=0;
       $uptime=0;
       $lat=$location["latitude"];
       $long=$location["longitude"];
       //if lat and long are just a generic country with no detail lets make some tail up or openmap just stacks them all on top another
       if (strlen($lat) < 4) {
         $lat = $lat + (rand(1, 15) / 10);
       }
       if (strlen($long) < 4) {
         $long = $long + (rand(1, 15) / 10);
       }
     }
     echo "<br>";
     $connection="";
     $pingdomdate = date('Y-m-d H:i:s');
     if (strpos($row[$i]['pingdomurl'], "pingdom.com")) {
     //curl the pingdom page 
213 214 215 216 217 218 219 220 221 222 223 224 225 226
        $ping = curl_init();
        $thismonth = "/".date("Y")."/".date("m");
        curl_setopt($ping, CURLOPT_URL, $row[$i]['pingdomurl'].$thismonth);
	if ($debug) {echo $row[$i]['pingdomurl'].$thismonth;}
        curl_setopt($ping, CURLOPT_POST, 0);
        curl_setopt($ping, CURLOPT_HEADER, 1);
        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);
        $pingdom = curl_exec($ping);
	$info = curl_getinfo($ping);
        curl_close($ping);
dmorley's avatar
cleanup  
dmorley committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
        if ($debug) {echo "<br>Pingdom code: ".$info['http_code']."<br>";}
        if ($info['http_code'] == 200) {
          //response time
          preg_match_all('/<h3>Avg. resp. time this month<\/h3>
          <p class="large">(.*?)</',$pingdom,$matcheach);
          $responsetime = $matcheach[1][0];
          //months monitored
          preg_match_all('/"historySelect">\s*(.*?)\s*<\/select/is',$pingdom,$matchhistory);
          $implodemonths = implode(" ", $matchhistory[1]);
          preg_match_all('/<option(.*?)/s',$implodemonths,$matchdates);
          $months = isset($matchdates[0])?count($matchdates[0]):0;
          //uptime %
          preg_match_all('/<h3>Uptime this month<\/h3>\s*<p class="large">(.*?)%</',$pingdom,$matchper);
          $uptime = isset($matchper[1][0])?preg_replace("/,/", ".", $matchper[1][0]):0;
          $pingdomdate = date('Y-m-d H:i:s');
          if (strpos($pingdom,"class=\"up\"")) { $live="up"; }
          elseif (strpos($pingdom,"class=\"down\"")) { $live="down"; }
          elseif (strpos($pingdom,"class=\"paused\"")) { $live="paused";}
          else {$live="error";$score=$score-2;}
        } else {
        //pingdom url is <> 200 so stats are gone, lower score
        $score=$score-2;
      }
    } else {
    //do uptimerobot API instead
    $ping = curl_init();
    curl_setopt($ping, CURLOPT_URL, "http://api.uptimerobot.com/getMonitors?format=json&customUptimeRatio=7-30-60-90&responseTimes=1&responseTimesAverage=86400&apiKey=".$row[$i]['pingdomurl']);
    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);
    $uptimerobot = curl_exec($ping);
    curl_close($ping);
    $json_encap = "jsonUptimeRobotApi()";
    $up2 = substr ($uptimerobot, strlen($json_encap) - 1, strlen ($uptimerobot) - strlen($json_encap)); 
    $uptr = json_decode($up2);
    if ($debug) {print_r($uptr);echo "<br>";}
    if (!$uptr) {$score=$score-2;}
    $responsetime = $uptr->monitors->monitor{'0'}->responsetime{'0'}->value;
    $uptimerobotstat = $uptr->stat;
    $uptime = $uptr->monitors->monitor{'0'}->alltimeuptimeratio;
    $diff = abs(strtotime(date('Y-m-d H:i:s')) - strtotime($dateadded));
    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
    if ($uptr->monitors->monitor{'0'}->status == 2) {$live = "Up";}
    if ($uptr->monitors->monitor{'0'}->status == 0) {$live = "Paused";}
    if ($uptr->monitors->monitor{'0'}->status == 1) {$live = "Not Checked Yet";}
    if ($uptr->monitors->monitor{'0'}->status == 8) {$live = "Seems Down";}
    if ($uptr->monitors->monitor{'0'}->status == 9) {$live = "Down";}
    $pingdomdate =  date('Y-m-d H:i:s');
    if ($uptimerobotstat == "fail" || $live <> "Up") {
      $score=$score-2;
    }
  }
  if ($softwarename == "diaspora") {$masterversion = $dmasterversion;} elseif ($softwarename == "friendica") {$masterversion = $fmasterversion;}
  $weightedscore = ($uptime + $score + ($active_users_monthly/19999) - ((10 - $weight) *.12));
  //sql it
MatrixCrawler's avatar
MatrixCrawler committed
286

dmorley's avatar
cleanup  
dmorley committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  $timenow = date('Y-m-d H:i:s');
  $sql = "UPDATE pods SET Hgitdate=$1, Hencoding=$2, secure=$3, hidden=$4, Hruntime=$5, Hgitref=$6, ip=$7, ipv6=$8, monthsmonitored=$9, 
  uptimelast7=$10, status=$11, dateLaststats=$12, dateUpdated=$13, responsetimelast7=$14, score=$15, adminrating=$16, country=$17, city=$18, 
  state=$19, lat=$20, long=$21, postalcode='', connection=$22, whois=$23, userrating=$24, longversion=$25, shortversion=$26, 
  masterversion=$27, signup=$28, total_users=$29, active_users_halfyear=$30, active_users_monthly=$31, local_posts=$32, name=$33, 
  comment_counts=$35, service_facebook=$36, service_tumblr=$37, service_twitter=$38, service_wordpress=$39, weightedscore=$40, xmpp=$41, softwarename=$42
  WHERE domain=$34";
  $result = pg_query_params($dbh, $sql, array($gitdate, $encoding, $secure, $hidden, $runtime, $gitrev, $ipnum, $ipv6, $months, $uptime, $live, $pingdomdate, $timenow, $responsetime, $score, $adminrating, $country, $city, $state, $lat, $long, $dver, $whois, $userrating, $xdver, $dver, $masterversion, $signup, $total_users, $active_users_halfyear, $active_users_monthly, $local_posts, $name, $domain, $comment_counts, $service_facebook, $service_tumblr, $service_twitter, $service_wordpress, $weightedscore, $xmpp, $softwarename));
  if (!$result) {
    die("Error in SQL query3: " . pg_last_error());
  }
  if ($debug) {echo "<br>Score out of 20: ".$score."<br>";}
  if (!$debug) {echo "Success";}
  //end foreach
  }
}     
pg_free_result($result);
pg_close($dbh);
MatrixCrawler's avatar
MatrixCrawler committed
305

306
?>