pull.php 12.9 KB
Newer Older
David Morley's avatar
David Morley committed
1
<?php
2
$debug = 1;//isset($_GET['debug'])?1:0;
3 4
//if ($_GET['debug'] == 1) {$debug =1;}
//$debug = isset($argv[1])?1:0;
5
//* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */
David Morley's avatar
David Morley committed
6
 include('config.php');
7 8 9 10 11 12 13 14 15 16
//get master code version
        $mv = curl_init();
        curl_setopt($mv, CURLOPT_URL, "https://raw.github.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);
root's avatar
root committed
17
	preg_match('/number: "(.*?)"/',$outputmv,$version);
18
 $masterversion = trim($version[1], '"');
19
 if ($debug) {echo "Masterversion: ".$masterversion."<br>";} 
David Morley's avatar
David Morley committed
20
 $dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
21 22
 $dbh2 = pg_connect("dbname=$pgdb user=$pguser password=$pgpass"); 
    if (!$dbh) {
David Morley's avatar
David Morley committed
23 24
         die("Error in connection: " . pg_last_error());
     }
David Morley's avatar
David Morley committed
25
//foreach pod check it and update db
root's avatar
root committed
26
 $domain = isset($_GET['domain'])?$_GET['domain']:null;
27 28 29 30 31 32 33 34 35 36
 if ($domain) {
	 $sql = "SELECT domain,pingdomurl,score,datecreated FROM pods WHERE domain = $1";
	 $sleep="0";
	 $result = pg_query_params($dbh, $sql, array($domain));
 } 
 else {
	 $sql = "SELECT domain,pingdomurl,score,datecreated,adminrating FROM pods";
	 $sleep="1";
	 $result = pg_query($dbh, $sql);
 }
37

David Morley's avatar
David Morley committed
38
 if (!$result) {
root's avatar
root committed
39
     die("Error in SQL query1: " . pg_last_error());
David Morley's avatar
David Morley committed
40
 }
41 42 43 44
 while ($row = pg_fetch_all($result)) {
 $numrows = pg_num_rows($result);
 for ($i = 0; $i < $numrows; $i++) {
     $domain =  $row[$i]['domain'];
45
     $score = $row[$i]['score'];
46
     $dateadded = $row[$i]['datecreated'];
47
     $admindb = $row[$i]['adminrating'];
David Morley's avatar
David Morley committed
48
//get ratings
David Morley's avatar
David Morley committed
49
 $userrate=0;$adminrate=0;$userratingavg = array();$adminratingavg = array();$userrating = array();$adminrating = array();
50 51
 $sqlforr = "SELECT * FROM rating_comments WHERE domain = $1";
 $ratings = pg_query_params($dbh, $sqlforr, array($domain));
David Morley's avatar
David Morley committed
52
 if (!$ratings) {
root's avatar
root committed
53
     die("Error in SQL query2: " . pg_last_error());
David Morley's avatar
David Morley committed
54
 }
55 56
 $numratings = pg_num_rows($ratings);
 while($myrow = pg_fetch_assoc($ratings)) {
David Morley's avatar
David Morley committed
57
   if ($myrow['admin'] == 0) {
58
     $userratingavg[] = $myrow['rating'];$userrate++;
David Morley's avatar
David Morley committed
59
   } elseif ($myrow['admin'] == 1) {
60 61 62
     $adminratingavg[] = $myrow['rating'];$adminrate++;
   } 
 }
63 64 65
#echo array_sum($userratingavg);
#echo "divided by";
#echo $userrate;
66

David Morley's avatar
David Morley committed
67 68
if ($userrate > 0) {$userrating = round(array_sum($userratingavg) / $userrate,2);}
if ($adminrate > 0) {$adminrating = round(array_sum($adminratingavg) / $adminrate,2);}
root's avatar
root committed
69
if ($debug) {echo "Domain: ".$domain."<br>";}
70 71
#echo $userrating."\n";
#echo $adminrating."\n";
72

David Morley's avatar
David Morley committed
73
if (!$userrating) {$userrating=0;}
74
if ($userrating > 10) {$userrating=10;}
David Morley's avatar
David Morley committed
75
if (!$adminrating) {$adminrating=0;}
76
if ($adminrating > 10) {$adminrating=10;}
77
if ($admindb == -1) {$adminrating=-1;}
78
     pg_free_result($ratings);
79 80
#echo $userrating."\n";
#echo $adminrating."\n";
81 82 83
$userrate=0;$adminrate=0;
unset($userratingavg);
unset($adminratingavg);
84
     //curl the header of pod with and without https
85 86 87 88 89 90
unset($name);
unset($total_users);
unset($active_users_halfyear);
unset($active_users_monthly);
unset($local_posts);
unset($registrations_open);
91
        $chss = curl_init();
92 93
        curl_setopt($chss, CURLOPT_URL, "https://".$domain."/statistics.json"); 
        curl_setopt($chss, CURLOPT_POST, 0);
94
        curl_setopt($chss, CURLOPT_HEADER, 1);
David Morley's avatar
David Morley committed
95
        curl_setopt($chss, CURLOPT_CONNECTTIMEOUT, 5);
96
        curl_setopt($chss, CURLOPT_RETURNTRANSFER, 1);
97
        curl_setopt($chss, CURLOPT_NOBODY, 0);
98 99 100 101
        $outputssl = curl_exec($chss);      
        curl_close($chss);

        $ch = curl_init();
102 103
        curl_setopt($ch, CURLOPT_URL, "http://".$domain."/statistics.json");
        curl_setopt($ch, CURLOPT_POST, 0);
104
        curl_setopt($ch, CURLOPT_HEADER, 1);
David Morley's avatar
David Morley committed
105
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
106
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
107
        curl_setopt($ch, CURLOPT_NOBODY, 0);
108 109
        $output = curl_exec($ch);
        curl_close($ch);
110
if ($debug) {print $output;}
111
if ($debug) {print $outputssl;}
112
if (stristr($outputssl, 'registrations_open')) {
113
//parse header data
114
$secure="true";
115
if ($debug) {echo "Secure: ".$secure."<br>";}
116 117
//$hidden="no";
$score = $score +1;
118 119
preg_match('/X-Git-Update: (.*?)\n/',$outputssl,$xgitdate);
$gitdate = trim($xgitdate[1]);
120
//$gitdate = strtotime($gitdate);
121 122
preg_match('/X-Git-Revision: (.*?)\n/',$outputssl,$xgitrev);
$gitrev = trim($xgitrev[1]);
123
if ($debug) {echo "GitRevssl: ".$gitrev."<br>";}
124
preg_match('/X-Runtime: (.*?)\n/',$outputssl,$xruntime);
dmorley's avatar
dmorley committed
125
$runtime = isset($xruntime[1])?trim($xruntime[1]):null;
126
preg_match('/Server: (.*?)\n/',$outputssl,$xserver);
dmorley's avatar
dmorley committed
127
$server = isset($xserver[1])?trim($xserver[1]):null;
128
preg_match('/Content-Encoding: (.*?)\n/',$outputssl,$xencoding);
root's avatar
root committed
129
if ($xencoding) {$encoding = trim($xencoding[1]);} else {$encoding = null;}
130 131 132 133
//get new json
preg_match_all("/{(.*?)}/", $outputssl, $jsonssl_array);
$jsonssl = json_decode($jsonssl_array[0][0]);
if ($jsonssl->registrations_open === true) {$registrations_open=1;}
root's avatar
root committed
134
$xdver = isset($jsonssl->version)?$jsonssl->version:0;
root's avatar
root committed
135
$dverr = split("-",trim($xdver));
root's avatar
root committed
136
$dver = $dverr[0];
root's avatar
root committed
137 138
if ($debug) {echo "Version code: ".$dver."<br>";}
if (!$dver) {$score = $score-2;}
139 140 141 142 143
$name = isset($jsonssl->name)?$jsonssl->name:"null";
$total_users = isset($jsonssl->total_users)?$jsonssl->total_users:0;
$active_users_halfyear = isset($jsonssl->active_users_halfyear)?$jsonssl->active_users_halfyear:0;
$active_users_monthly = isset($jsonssl->active_users_monthly)?$jsonssl->active_users_monthly:0;
$local_posts = isset($jsonssl->local_posts)?$jsonssl->local_posts:0;
root's avatar
root committed
144
} elseif (stristr($output, 'registrations_open')) {
145 146 147
"not";$secure="false";
//$hidden="no";
$score = $score +1;
148 149
//parse header data
preg_match('/X-Git-Update: (.*?)\n/',$output,$xgitdate);
dmorley's avatar
dmorley committed
150
$gitdate = isset($xgitdate[1])?trim($xgitdate[1]):null;
151
preg_match('/X-Git-Revision: (.*?)\n/',$output,$xgitrev);
dmorley's avatar
dmorley committed
152
$gitrev = isset($xgitrev[1])?trim($xgitrev[1]):null;
153
preg_match('/X-Runtime: (.*?)\n/',$output,$xruntime);
dmorley's avatar
dmorley committed
154
$runtime = isset($xruntime[1])?trim($xruntime[1]):null;
155
preg_match('/Server: (.*?)\n/',$output,$xserver);
dmorley's avatar
dmorley committed
156
$server = isset($xserver[1])?trim($xserver[1]):null;
157
preg_match('/Content-Encoding: (.*?)\n/',$output,$xencoding);
dmorley's avatar
dmorley committed
158
$encoding = isset($xencoding[1])?trim($xencoding[1]):null;
159 160 161
preg_match_all("/{(.*?)}/", $output, $jsonssl_array);
$jsonssl = json_decode($jsonssl_array[0][0]);
if ($jsonssl->registrations_open === true) {$registrations_open=1;}
root's avatar
root committed
162
$xdver = isset($jsonssl->version)?$jsonssl->version:0;
root's avatar
root committed
163
$dverr = split("-",trim($xdver));
root's avatar
root committed
164
$dver = $dverr[0];
root's avatar
root committed
165 166
if ($debug) {echo "Version code: ".$dver."<br>";}
if (!$dver) {$score = $score-2;}
167 168 169 170 171
$name = isset($jsonssl->name)?$jsonssl->name:"null";
$total_users = isset($jsonssl->total_users)?$jsonssl->total_users:0;
$active_users_halfyear = isset($jsonssl->active_users_halfyear)?$jsonssl->active_users_halfyear:0;
$active_users_monthly = isset($jsonssl->active_users_monthly)?$jsonssl->active_users_monthly:0;
$local_posts = isset($jsonssl->local_posts)?$jsonssl->local_posts:0;
172
} else {
173 174 175
$secure="false";
$score = $score - 1;
//$hidden="yes";
176
//no diaspora cookie on either, lets set this one as hidden and notify someone its not really a pod
177
//could also be a ssl pod with a bad cert, I think its ok to call that a dead pod now
178
}
179
$signup = $registrations_open;
180
if ($debug) {echo "<br>Signup Open: ".$signup."<br>";}
181 182 183 184
if ($score > 5) {
$hidden = "no";
} else {
$hidden = "yes";
185
}
dmorley's avatar
dmorley committed
186
if ($debug) {echo "Hidden: ".$hidden."<br>";}
187 188 189 190 191 192
// 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;
}
193 194 195
$ip6 = escapeshellcmd('dig +nocmd '.$domain.' aaaa +noall +short');
$ip = escapeshellcmd('dig +nocmd '.$domain.' a +noall +short');
$ip6num = exec($ip6);
196
$ipnum = exec($ip);
197
$test = strpos($ip6num, ":");
198 199 200 201 202
if ($test === false) {
$ipv6="no";
} else {
$ipv6="yes";
}
203
//curl ip
204 205 206 207
require_once "Net/GeoIP.php";
$geoip = Net_GeoIP::getInstance("GeoLiteCity.dat");
try {
    $location = $geoip->lookupLocation($ipnum);
root's avatar
root committed
208
if ($debug) {echo "GEOIP: ".$location."<br>";}
209 210 211 212 213 214
} catch (Exception $e) {
    // Handle exception
}
$ipdata = "Country: ".$location->countryName."\n";
$whois = "Country: ".$location->countryName."\n Lat:".$location->latitude." Long:".$location->longitude;
$country=$location->countryName;
dmorley's avatar
dmorley committed
215
$city=  isset($location->city)?iconv("UTF-8", "UTF-8//IGNORE", $location->city):null;
216
$state="";
dmorley's avatar
dmorley committed
217 218
$months=0;
$uptime=0;
219 220
$lat=$location->latitude;
$long=$location->longitude;
221
$connection="";
dmorley's avatar
dmorley committed
222
$pingdomdate = date('Y-m-d H:i:s');
223
if (strpos($row[$i]['pingdomurl'], "pingdom.com")) {
224 225
//curl the pingdom page 
        $ping = curl_init();
226
        $thismonth = "/".date("Y")."/".date("m");
227
        curl_setopt($ping, CURLOPT_URL, $row[$i]['pingdomurl'].$thismonth);
dmorley's avatar
dmorley committed
228
	if ($debug) {echo $row[$i]['pingdomurl'].$thismonth;}
229 230 231
        curl_setopt($ping, CURLOPT_POST, 0);
        curl_setopt($ping, CURLOPT_HEADER, 1);
        curl_setopt($ping, CURLOPT_RETURNTRANSFER, 1);
David Morley's avatar
David Morley committed
232
        curl_setopt($ping, CURLOPT_CONNECTTIMEOUT, 8);
233 234 235 236
        curl_setopt($ping, CURLOPT_NOBODY, 0);
        curl_setopt($ping, CURLOPT_MAXCONNECTS, 5);
        curl_setopt($ping, CURLOPT_FOLLOWLOCATION, true);
        $pingdom = curl_exec($ping);
dmorley's avatar
dmorley committed
237
	$info = curl_getinfo($ping);
238
        curl_close($ping);
239
if ($debug) {echo "<br>Pingdom code: ".$info['http_code']."<br>";}
dmorley's avatar
dmorley committed
240 241
if ($info['http_code'] == 200) {

242 243
//response time
preg_match_all('/<h3>Avg. resp. time this month<\/h3>
David Morley's avatar
David Morley committed
244
        <p class="large">(.*?)</',$pingdom,$matcheach);
245 246 247
$responsetime = $matcheach[1][0];

//months monitored
248 249 250
preg_match_all('/"historySelect">\s*(.*?)\s*<\/select/is',$pingdom,$matchhistory);
$implodemonths = implode(" ", $matchhistory[1]);
preg_match_all('/<option(.*?)/s',$implodemonths,$matchdates);
dmorley's avatar
dmorley committed
251
$months = isset($matchdates[0])?count($matchdates[0]):0;
252
//echo $matchdates[0];
253
//uptime %
254
preg_match_all('/<h3>Uptime this month<\/h3>\s*<p class="large">(.*?)%</',$pingdom,$matchper);
dmorley's avatar
dmorley committed
255
$uptime = isset($matchper[1][0])?preg_replace("/,/", ".", $matchper[1][0]):0;
256
$pingdomdate = date('Y-m-d H:i:s');
257 258 259
if (strpos($pingdom,"class=\"up\"")) { $live="up"; }
elseif (strpos($pingdom,"class=\"down\"")) { $live="down"; }
elseif (strpos($pingdom,"class=\"paused\"")) { $live="paused";}
260
else {$live="error";$score=$score-2;}
261
} else {
dmorley's avatar
dmorley committed
262 263 264 265
//pingdom url is <> 200 so stats are gone, lower score
$score=$score-2;
}
} else {
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
//do uptimerobot API instead
        $ping = curl_init();
        curl_setopt($ping, CURLOPT_URL, "http://api.uptimerobot.com/getMonitors?format=json&customUptimeRatio=7-30-60-90&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);
$responsetime = 'n/a';
282
$uptime = $uptr->monitors->monitor{'0'}->alltimeuptimeratio;
283 284 285 286 287 288
$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 == 1) {$live = "Paused";}
if ($uptr->monitors->monitor{'0'}->status == 8) {$live = "Seems Down";}
if ($uptr->monitors->monitor{'0'}->status == 9) {$live = "Down";}
289

290 291
$pingdomdate =  date('Y-m-d H:i:s');
}
292 293
//sql it
     $timenow = date('Y-m-d H:i:s');
294
     $sql = "UPDATE pods SET Hgitdate=$1, Hencoding=$2, secure=$3, hidden=$4, Hruntime=$5, Hgitref=$6, ip=$7, ipv6=$8, monthsmonitored=$9, 
root's avatar
root committed
295
uptimelast7=$10, status=$11, dateLaststats=$12, dateUpdated=$13, responsetimelast7=$14, score=$15, adminrating=$16, country=$17, city=$18, 
296
state=$19, lat=$20, long=$21, postalcode='', connection=$22, whois=$23, userrating=$24, longversion=$25, shortversion=$26, 
297
masterversion=$27, signup=$28, total_users=$29, active_users_halfyear=$30, active_users_monthly=$31, local_posts=$32, name=$33
298
WHERE 
299
domain=$34";
root's avatar
root committed
300
     $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));
David Morley's avatar
David Morley committed
301
     if (!$result) {
root's avatar
root committed
302
         die("Error in SQL query3: " . pg_last_error());
David Morley's avatar
David Morley committed
303 304
     }
    
305
if ($debug) {echo "<br>Score out of 20: ".$score."<br>";}
306
if (!$debug) {echo "Success";}
307

David Morley's avatar
David Morley committed
308
//end foreach
309
 }
David Morley's avatar
David Morley committed
310 311 312 313 314
 }   
     pg_free_result($result);
    
     pg_close($dbh);

315
?>