pull.php 11.4 KB
Newer Older
David Morley's avatar
David Morley committed
1
<?php
root's avatar
root committed
2
$debug = isset($_GET['debug'])?1:0;
dmorley's avatar
dmorley committed
3
$debug = isset($argv[1])?1:0;
4
//* 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
5
 include('config.php');
6 7 8 9 10 11 12 13 14 15
//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
16
	preg_match('/number: "(.*?)"/',$outputmv,$version);
17
 $masterversion = trim($version[1], '"');
David Morley's avatar
David Morley committed
18
 $dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
19 20
 $dbh2 = pg_connect("dbname=$pgdb user=$pguser password=$pgpass"); 
    if (!$dbh) {
David Morley's avatar
David Morley committed
21 22
         die("Error in connection: " . pg_last_error());
     }
David Morley's avatar
David Morley committed
23
//foreach pod check it and update db
root's avatar
root committed
24 25
 $domain = isset($_GET['domain'])?$_GET['domain']:null;
 if ($domain) {$sql = "SELECT domain,pingdomurl,score,datecreated FROM pods WHERE domain = '$domain'";$sleep="0";} 
26
 else {$sql = "SELECT domain,pingdomurl,score,datecreated,adminrating FROM pods";$sleep="1";}
27

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

David Morley's avatar
David Morley committed
58 59
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
60
if ($debug) {echo "Domain: ".$domain."<br>";}
61 62
#echo $userrating."\n";
#echo $adminrating."\n";
63

David Morley's avatar
David Morley committed
64
if (!$userrating) {$userrating=0;}
65
if ($userrating > 10) {$userrating=10;}
David Morley's avatar
David Morley committed
66
if (!$adminrating) {$adminrating=0;}
67
if ($adminrating > 10) {$adminrating=10;}
68
if ($admindb == -1) {$adminrating=-1;}
69
     pg_free_result($ratings);
70 71
#echo $userrating."\n";
#echo $adminrating."\n";
72 73 74
$userrate=0;$adminrate=0;
unset($userratingavg);
unset($adminratingavg);
75
     //curl the header of pod with and without https
76 77

        $chss = curl_init();
78
        curl_setopt($chss, CURLOPT_URL, "https://".$domain."/users/sign_in"); 
79 80
        curl_setopt($chss, CURLOPT_POST, 1);
        curl_setopt($chss, CURLOPT_HEADER, 1);
David Morley's avatar
David Morley committed
81
        curl_setopt($chss, CURLOPT_CONNECTTIMEOUT, 5);
82 83 84 85 86 87
        curl_setopt($chss, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($chss, CURLOPT_NOBODY, 1);
        $outputssl = curl_exec($chss);      
        curl_close($chss);

        $ch = curl_init();
88
        curl_setopt($ch, CURLOPT_URL, "http://".$domain."/users/sign_in");
89 90
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
David Morley's avatar
David Morley committed
91
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
92 93 94 95 96 97
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        $output = curl_exec($ch);
        curl_close($ch);

if (stristr($outputssl, 'Set-Cookie: _diaspora_session=')) {
98
//parse header data
99 100 101
$secure="true";
//$hidden="no";
$score = $score +1;
102 103
preg_match('/X-Git-Update: (.*?)\n/',$outputssl,$xgitdate);
$gitdate = trim($xgitdate[1]);
104
//$gitdate = strtotime($gitdate);
105 106
preg_match('/X-Git-Revision: (.*?)\n/',$outputssl,$xgitrev);
$gitrev = trim($xgitrev[1]);
107
preg_match('/X-Diaspora-Version: (.*?)\n/',$outputssl,$xdver);
108 109
$dverr = split("-",trim($xdver[1]));
$dver = $dverr[0];
root's avatar
root committed
110
if ($debug) {echo "Version code: ".$dverr[1]."<br>";}
111
if (!$dver) {$score = $score-2;}
112
preg_match('/X-Runtime: (.*?)\n/',$outputssl,$xruntime);
dmorley's avatar
dmorley committed
113
$runtime = isset($xruntime[1])?trim($xruntime[1]):null;
114
preg_match('/Server: (.*?)\n/',$outputssl,$xserver);
dmorley's avatar
dmorley committed
115
$server = isset($xserver[1])?trim($xserver[1]):null;
116
preg_match('/Content-Encoding: (.*?)\n/',$outputssl,$xencoding);
root's avatar
root committed
117
if ($xencoding) {$encoding = trim($xencoding[1]);} else {$encoding = null;}
118 119

} elseif (stristr($output, 'Set-Cookie: _diaspora_session=')) {
120 121 122
"not";$secure="false";
//$hidden="no";
$score = $score +1;
123 124
//parse header data
preg_match('/X-Git-Update: (.*?)\n/',$output,$xgitdate);
dmorley's avatar
dmorley committed
125
$gitdate = isset($xgitdate[1])?trim($xgitdate[1]):null;
126
preg_match('/X-Git-Revision: (.*?)\n/',$output,$xgitrev);
dmorley's avatar
dmorley committed
127
$gitrev = isset($xgitrev[1])?trim($xgitrev[1]):null;
128
preg_match('/X-Diaspora-Version: (.*?)\n/',$output,$xdver);
129 130
$dverr = split("-",trim($xdver[1]));
$dver = $dverr[0];
root's avatar
root committed
131
if ($debug) {echo "Version code: ".$dverr[1]."<br>";}
132
if (!$dver) {$score = $score-2;}
133
preg_match('/X-Runtime: (.*?)\n/',$output,$xruntime);
dmorley's avatar
dmorley committed
134
$runtime = isset($xruntime[1])?trim($xruntime[1]):null;
135
preg_match('/Server: (.*?)\n/',$output,$xserver);
dmorley's avatar
dmorley committed
136
$server = isset($xserver[1])?trim($xserver[1]):null
137
preg_match('/Content-Encoding: (.*?)\n/',$output,$xencoding);
dmorley's avatar
dmorley committed
138
$encoding = isset($xencoding[1])?trim($xencoding[1]):null
139
} else {
140 141 142
$secure="false";
$score = $score - 1;
//$hidden="yes";
143
//no diaspora cookie on either, lets set this one as hidden and notify someone its not really a pod
144
//could also be a ssl pod with a bad cert, I think its ok to call that a dead pod now
145
}
root's avatar
root committed
146
if ($debug) {echo "SSL: ".$secure."<br>";}
147
if (!$gitdate) {
148 149
//if a pod is not displaying the git header data its really really really old lets lower your score
//$hidden="yes";
root's avatar
root committed
150
if ($debug) {echo "Valid Headers: ".$gitdate."<br>";}
David Morley's avatar
David Morley committed
151
$score = $score - 2;
152 153 154 155 156
}
if ($score > 5) {
$hidden = "no";
} else {
$hidden = "yes";
157
}
158 159 160 161 162 163
// 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;
}
164 165 166 167

$ip6 = escapeshellcmd('dig +nocmd '.$domain.' aaaa +noall +short');
$ip = escapeshellcmd('dig +nocmd '.$domain.' a +noall +short');
$ip6num = exec($ip6);
168
$ipnum = exec($ip);
169
$test = strpos($ip6num, ":");
170 171 172 173 174
if ($test === false) {
$ipv6="no";
} else {
$ipv6="yes";
}
175
//curl ip
176 177 178 179
require_once "Net/GeoIP.php";
$geoip = Net_GeoIP::getInstance("GeoLiteCity.dat");
try {
    $location = $geoip->lookupLocation($ipnum);
root's avatar
root committed
180
if ($debug) {echo "GEOIP: ".$location."<br>";}
181 182 183 184 185 186
} catch (Exception $e) {
    // Handle exception
}
$ipdata = "Country: ".$location->countryName."\n";
$whois = "Country: ".$location->countryName."\n Lat:".$location->latitude." Long:".$location->longitude;
$country=$location->countryName;
David Morley's avatar
David Morley committed
187
$city=  iconv("UTF-8", "UTF-8//IGNORE", $location->city);
188 189 190
$state="";
$lat=$location->latitude;
$long=$location->longitude;
191
$connection="";
192
if (strpos($row[$i]['pingdomurl'], "pingdom.com")) {
193 194
//curl the pingdom page 
        $ping = curl_init();
195
        $thismonth = "/".date("Y")."/".date("m");
196
        curl_setopt($ping, CURLOPT_URL, $row[$i]['pingdomurl'].$thismonth);
dmorley's avatar
dmorley committed
197
	if ($debug) {echo $row[$i]['pingdomurl'].$thismonth;}
198 199 200
        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
201
        curl_setopt($ping, CURLOPT_CONNECTTIMEOUT, 8);
202 203 204 205
        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
206
	$info = curl_getinfo($ping);
207
        curl_close($ping);
dmorley's avatar
dmorley committed
208 209 210
if ($debug) {echo "Pingdom code: ".$info['http_code']."<br>";}
if ($info['http_code'] == 200) {

211 212
//response time
preg_match_all('/<h3>Avg. resp. time this month<\/h3>
David Morley's avatar
David Morley committed
213
        <p class="large">(.*?)</',$pingdom,$matcheach);
214 215 216
$responsetime = $matcheach[1][0];

//months monitored
217 218 219
preg_match_all('/"historySelect">\s*(.*?)\s*<\/select/is',$pingdom,$matchhistory);
$implodemonths = implode(" ", $matchhistory[1]);
preg_match_all('/<option(.*?)/s',$implodemonths,$matchdates);
220 221
$months = count($matchdates[0]);
//uptime %
222
preg_match_all('/<h3>Uptime this month<\/h3>\s*<p class="large">(.*?)%</',$pingdom,$matchper);
223
$uptime = preg_replace("/,/", ".", $matchper[1][0]);
David Morley's avatar
David Morley committed
224
//var_dump($matchper);
225
//last check
root's avatar
root committed
226 227 228 229 230 231 232 233 234
//preg_match_all('/<h3>Last checked<\/h3>
//<p>(.*?)</',$pingdom,$matchdate);
//var_dump($matchdate);
//$pingdom_timestamp = $matchdate[1][0];
//$Date_parts = preg_split("/[\s-]+/", $pingdom_timestamp);
//if (strlen($Date_parts[0]) == "2") {
//$pingdomdate = date('Y-m-d H:i:s');
//}
//else {
235
$pingdomdate = date('Y-m-d H:i:s');
root's avatar
root committed
236
//}
237 238 239 240
//status
if (strpos($pingdom,"class=\"up\"")) { $live="up"; }
elseif (strpos($pingdom,"class=\"down\"")) { $live="down"; }
elseif (strpos($pingdom,"class=\"paused\"")) { $live="paused";}
241
else {$live="error";$score=$score-2;}
242
} else {
dmorley's avatar
dmorley committed
243 244 245 246
//pingdom url is <> 200 so stats are gone, lower score
$score=$score-2;
}
} else {
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
//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';
263
$uptime = $uptr->monitors->monitor{'0'}->alltimeuptimeratio;
264 265 266 267 268 269
$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";}
270

271 272
$pingdomdate =  date('Y-m-d H:i:s');
}
273 274
//sql it
     $timenow = date('Y-m-d H:i:s');
275 276
     $sql = "UPDATE pods SET Hgitdate='$gitdate', Hencoding='$encoding', secure='$secure', hidden='$hidden', Hruntime='$runtime', Hgitref='$gitrev', ip='$ipnum', ipv6='$ipv6', monthsmonitored='$months', 
uptimelast7='$uptime', status='$live', dateLaststats='$pingdomdate', dateUpdated='$timenow', responsetimelast7='$responsetime', score='$score', adminrating='$adminrating', country='$country', city='$city', 
root's avatar
root committed
277
state='$state', lat='$lat', long='$long', postalcode='', connection='$dver', whois='$whois', userrating='$userrating', longversion='$xdver[1]', shortversion='$dver', 
278 279 280
masterversion='$masterversion' 
WHERE 
domain='$domain'";
root's avatar
root committed
281
     if ($debug) {echo "SQL: ".$sql."<br>";}
David Morley's avatar
David Morley committed
282 283
     $result = pg_query($dbh, $sql);
     if (!$result) {
root's avatar
root committed
284
         die("Error in SQL query3: " . pg_last_error());
David Morley's avatar
David Morley committed
285 286
     }
    
root's avatar
root committed
287
if ($debug) {echo "Score out of 20: ".$score."<br>";}
288
if (!$debug) {echo "Success";}
289

David Morley's avatar
David Morley committed
290
//end foreach
291
sleep($sleep);
292
 }
David Morley's avatar
David Morley committed
293 294 295 296 297
 }   
     pg_free_result($result);
    
     pg_close($dbh);

298
?>