From 1b07a7574a609aeb991b53f0e70d9abde1958aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20L=C3=BCscher?= Date: Thu, 22 Dec 2016 02:53:34 +0100 Subject: [PATCH] Clean up most of the PHP code. --- api.php | 63 +++++++++++++++++++++---------------------------- cleanup.php | 25 +++++++------------- db/api-more.php | 2 +- index.php | 30 +++++++++++------------ random.php | 10 +++----- rate.php | 41 ++++++++++++++++---------------- show.php | 32 +++++++++---------------- showfull.php | 46 ++++++++++++------------------------ showmap.php | 54 +++++++++++++----------------------------- 9 files changed, 119 insertions(+), 184 deletions(-) diff --git a/api.php b/api.php index 46f0abd..690791d 100644 --- a/api.php +++ b/api.php @@ -7,12 +7,10 @@ require_once __DIR__ . '/config.php'; $dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass"); $dbh || die('Error in connection: ' . pg_last_error()); - -if ($_GET['format'] == 'georss') { +if ($_GET['format'] === 'georss') { echo << - + Diaspora Pods IP Locations of Diaspora pods on podupti.me @@ -25,7 +23,7 @@ EOF; $numrows = pg_num_rows($result); while ($row = pg_fetch_array($result)) { $pod_name = htmlentities($row['name'], ENT_QUOTES); - $tip = sprintf( + $summary = sprintf( 'This pod %1$s has been watched for %2$s months and its average ping time is %3$s with uptime of %4$s%% this month and was last checked on %5$s. On a score of 100 this pod is a %6$s right now', $pod_name, $row['monthsmonitored'], @@ -34,19 +32,15 @@ EOF; $row['dateupdated'], $row['score'] ); - if ($row['secure'] == 'true') { - $method = 'https://'; - } else { - $method = 'http://'; - } + $scheme = $row['secure'] === 'true' ? 'https://' : 'http://'; echo << - {$method}{$row['domain']} - + {$scheme}{$row['domain']} + urn:{$row['domain']} Pod Location is: {$row['country']} -{$tip} +{$summary} {$row['lat']} {$row['long']} {$row['domain']} @@ -54,18 +48,21 @@ EOF; EOF; } echo ''; -} elseif ($_GET['format'] == 'json') { +} elseif ($_GET['format'] === 'json') { $sql = 'SELECT id,domain,status,secure,score,userrating,adminrating,city,state,country,lat,long,ip,ipv6,pingdomurl,monthsmonitored,uptimelast7,responsetimelast7,local_posts,comment_counts,dateCreated,dateUpdated,dateLaststats,hidden FROM pods'; $result = pg_query($dbh, $sql); $result || die('Error in SQL query: ' . pg_last_error()); - $numrows = pg_num_rows($result); //json output, thx Vipul A M for fixing this header('Content-type: application/json'); - $rows = array_values(pg_fetch_all($result)); - $obj->podcount = $numrows; - $obj->pods = $rows; - if ($_GET['method'] == 'jsonp') { + + $numrows = pg_num_rows($result); + $rows = array_values(pg_fetch_all($result)); + $obj = [ + 'podcount' => $numrows, + 'pods' => $rows, + ]; + if ($_GET['method'] === 'jsonp') { print $_GET['callback'] . '(' . json_encode($obj) . ')'; } else { print json_encode($obj); @@ -78,23 +75,17 @@ EOF; $numrows = pg_num_rows($result); while ($row = pg_fetch_array($result)) { - if ($row['status'] == 'up') { - $status = 'Online'; - } else { - $status = 'Offline'; - } - if ($row['secure'] == 'true') { - $method = 'https://'; - $class = 'green'; - } else { - $method = 'http://'; - $class = 'red'; - } - echo $row['domain'] . ' Up ' . $row['uptimelast7'] . '% This Month - Located in: ' . $row['country']; - if ($i < ($numrows - 1)) { - echo ','; - } - $i ++; +// $status = $row['status'] === 'up' ? 'Online' : 'Offline'; +// $scheme = $row['secure'] === 'true' ? 'https://' : 'http://'; +// $class = $row['secure'] === 'true' ? 'green' : 'red'; + + $i++ > 0 && print ','; + printf( + '%1$s Up %2$s%% This Month - Located in: %3$s', + $row['domain'], + $row['uptimelast7'], + $row['country'] + ); } pg_free_result($result); diff --git a/cleanup.php b/cleanup.php index 13c6b93..9f59201 100644 --- a/cleanup.php +++ b/cleanup.php @@ -1,5 +1,4 @@ ' . $row['domain'] . '
?
'; + echo '' . $row['domain'] . '
?
'; if (stristr($row['shortversion'], 'head')) { $version = '.dev'; @@ -74,7 +73,7 @@ $numrows = pg_num_rows($result); $version = $row['shortversion']; $pre = 'This pod runs production code'; } - if ($row['shortversion'] == $row['masterversion'] && $row['shortversion'] != '') { + if ($row['shortversion'] === $row['masterversion'] && $row['shortversion'] !== '') { $classver = 'green'; } elseif ($verdiff > 6) { $classver = 'red'; @@ -84,19 +83,13 @@ $numrows = pg_num_rows($result); echo '
' . $version . '
'; echo '' . $row['uptimelast7'] . ''; echo '' . $row['responsetimelast7'] . ''; - if ($row['signup'] == 1) { - $signup = 'Open'; - } else { - $signup = 'Closed'; - } - echo '' . $signup . ''; + echo '' . ($row['signup'] === '1' ? 'Open' : 'Closed') . ''; echo '' . $row['total_users'] . ''; echo '' . $row['active_users_halfyear'] . ''; echo '' . $row['active_users_monthly'] . ''; echo '' . $row['local_posts'] . ''; echo '' . $row['comment_counts'] . ''; - if (strpos($row['pingdomurl'], - 'pingdom.com')) { + if (strpos($row['pingdomurl'], 'pingdom.com')) { $moreurl = $row['pingdomurl']; } else { $moreurl = 'http://api.uptimerobot.com/getMonitors?format=json&customUptimeRatio=7-30-60-90&apiKey=' . $row['pingdomurl']; diff --git a/db/api-more.php b/db/api-more.php index e0d26da..5892734 100644 --- a/db/api-more.php +++ b/db/api-more.php @@ -11,7 +11,7 @@ $result = pg_query_params($dbh, $sql, [$_GET['url']]); $result || die('Error in SQL query: ' . pg_last_error()); while ($row = pg_fetch_array($result)) { - if ($_GET['format'] == 'json') { + if ($_GET['format'] === 'json') { echo json_encode($row); } else { echo 'Status: ' . $row['status'] . '
'; diff --git a/index.php b/index.php index a2592c8..ebbffc8 100644 --- a/index.php +++ b/index.php @@ -33,9 +33,9 @@ $simpleview = !($advancedview || $mapview || $cleanup || $podmin || $podminedi Poduptime @@ -43,13 +43,13 @@ $simpleview = !($advancedview || $mapview || $cleanup || $podmin || $podminedi
Confused? Auto pick a pod for you. @@ -73,17 +73,17 @@ $simpleview = !($advancedview || $mapview || $cleanup || $podmin || $podminedi
diff --git a/random.php b/random.php index 5a5c982..0022315 100644 --- a/random.php +++ b/random.php @@ -6,12 +6,8 @@ $dbh || die('Error in connection: ' . pg_last_error()); $sql = "SELECT * FROM pods WHERE adminrating <> -1 AND hidden <> 'yes' AND uptimelast7 > 95 AND masterversion = shortversion AND signup = 1 ORDER BY RANDOM() LIMIT 1"; $result = pg_query($dbh, $sql); -$result || die('Error in SQL query1: ' . pg_last_error()); +$result || die('Error in SQL query: ' . pg_last_error()); $row = pg_fetch_all($result); -if ($row[0]['secure'] == 'true') { - $ssl = 's'; -} else { - $ssl = ''; -} -header('Location: http' . $ssl . '://' . $row[0]['domain'] . '/users/sign_up'); +$scheme = $row[0]['secure'] === 'true' ? 'https://' : 'http://'; +header('Location: http' . $scheme . $row[0]['domain'] . '/users/sign_up'); diff --git a/rate.php b/rate.php index f114344..c455b2f 100644 --- a/rate.php +++ b/rate.php @@ -1,7 +1,7 @@ -