Commit eac0f5e2 authored by noplanman's avatar noplanman Committed by dmorley
Browse files

PHP 7.2 and PSR-2 compliance (#170)

* Add PHP Code Sniffer, clean up composer.json, ignore node_modules

* Initial PSR2 flash.

* Declare strict types, add file comments, remove copyright notices.

* Fix SQL code indentation.

* Smaller visual code changes.

* Modify / remove invalid or unnecessary code.

* Add some variable data fail safes.

* Move logging into lib and introduce Poduptime namespace.
parent ea73a847
bower_components bower_components
config.php config.php
node_modules
vendor vendor
<?php <?php
//Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file.
/**
* API access for pod data.
*/
declare(strict_types=1);
use RedBeanPHP\R; use RedBeanPHP\R;
...@@ -21,7 +26,7 @@ R::testConnection() || die('Error in DB connection'); ...@@ -21,7 +26,7 @@ R::testConnection() || die('Error in DB connection');
R::usePartialBeans(true); R::usePartialBeans(true);
if ($_format === 'georss') { if ($_format === 'georss') {
echo <<<EOF echo <<<EOF
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss"> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">
<title>Diaspora Pods</title> <title>Diaspora Pods</title>
...@@ -30,26 +35,26 @@ if ($_format === 'georss') { ...@@ -30,26 +35,26 @@ if ($_format === 'georss') {
EOF; EOF;
try { try {
$pods = R::getAll(' $pods = R::getAll('
SELECT name, monthsmonitored, responsetimelast7, uptimelast7, dateupdated, score, domain, country, lat, long SELECT name, monthsmonitored, responsetimelast7, uptimelast7, dateupdated, score, domain, country, lat, long
FROM pods_apiv1 FROM pods_apiv1
'); ');
} catch (\RedBeanPHP\RedException $e) { } catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage()); die('Error in SQL query: ' . $e->getMessage());
} }
foreach ($pods as $pod) { foreach ($pods as $pod) {
$summary = 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', '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',
htmlentities($pod['name'], ENT_QUOTES), htmlentities($pod['name'], ENT_QUOTES),
$pod['monthsmonitored'], $pod['monthsmonitored'],
$pod['responsetimelast7'], $pod['responsetimelast7'],
$pod['uptimelast7'], $pod['uptimelast7'],
$pod['dateupdated'], $pod['dateupdated'],
$pod['score'] $pod['score']
); );
echo <<<EOF echo <<<EOF
<entry> <entry>
<title>https://{$pod['domain']}</title> <title>https://{$pod['domain']}</title>
<link href="https://{$pod['domain']}"/> <link href="https://{$pod['domain']}"/>
...@@ -62,51 +67,50 @@ EOF; ...@@ -62,51 +67,50 @@ EOF;
</entry> </entry>
EOF; EOF;
} }
echo '</feed>'; echo '</feed>';
} elseif ($_format === 'json') { } elseif ($_format === 'json') {
try {
$pods = R::getAll('
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_apiv1
');
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
}
//json output, thx Vipul A M for fixing this
header('Content-type: application/json');
try { $obj = [
$pods = R::getAll(' 'podcount' => count($pods),
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 'pods' => allToString($pods),
FROM pods_apiv1 ];
'); if ($_method === 'jsonp') {
} catch (\RedBeanPHP\RedException $e) { print $_callback . '(' . json_encode($obj) . ')';
die('Error in SQL query: ' . $e->getMessage()); } else {
} print json_encode($obj);
}
//json output, thx Vipul A M for fixing this
header('Content-type: application/json');
$obj = [
'podcount' => count($pods),
'pods' => allToString($pods),
];
if ($_method === 'jsonp') {
print $_callback . '(' . json_encode($obj) . ')';
} else {
print json_encode($obj);
}
} else { } else {
try { try {
$pods = R::getAll(' $pods = R::getAll('
SELECT domain, uptimelast7, country SELECT domain, uptimelast7, country
FROM pods_apiv1 FROM pods_apiv1
'); ');
} catch (\RedBeanPHP\RedException $e) { } catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage()); die('Error in SQL query: ' . $e->getMessage());
} }
$i = 0; $i = 0;
foreach ($pods as $pod) { foreach ($pods as $pod) {
$i++ > 0 && print ','; $i++ > 0 && print ',';
printf( printf(
'%1$s Up %2$s%% This Month - Located in: %3$s', '%1$s Up %2$s%% This Month - Located in: %3$s',
$pod['domain'], $pod['domain'],
$pod['uptimelast7'], $pod['uptimelast7'],
$pod['country'] $pod['country']
); );
} }
} }
/** /**
...@@ -121,18 +125,18 @@ EOF; ...@@ -121,18 +125,18 @@ EOF;
*/ */
function allToString(array $arr) function allToString(array $arr)
{ {
$ret = $arr; $ret = $arr;
foreach ($ret as &$item) { foreach ($ret as &$item) {
if (is_array($item)) { if (is_array($item)) {
/** @var array $item */ /** @var array $item */
foreach ($item as &$field) { foreach ($item as &$field) {
$field !== null && $field = (string) $field; $field !== null && $field = (string) $field;
} }
} else { } else {
$item !== null && $item = (string) $item; $item !== null && $item = (string) $item;
}
unset($field, $item);
} }
unset($field, $item);
}
return $ret; return $ret;
} }
{ {
"name": "poduptime", "name": "poduptime",
"homepage": "https://github.com/diasporg/Poduptime", "homepage": "https://github.com/diasporg/Poduptime",
"authors": [ "authors": [
"David Morley <support@diasp.org>" "David Morley <support@diasp.org>"
], ],
"description": "Diaspora pod checking site", "description": "Diaspora pod checking site",
"main": "", "main": "",
"keywords": [ "keywords": [
"diaspora" "diaspora"
], ],
"license": "AGPL", "license": "AGPL",
"private": true, "private": true,
"ignore": [ "ignore": [
"**/.*", "**/.*",
"node_modules", "node_modules",
"bower_components", "bower_components",
"test", "test",
"tests" "tests"
], ],
"dependencies": { "dependencies": {
"facebox": "*" "facebox": "*"
} }
} }
...@@ -5,16 +5,24 @@ ...@@ -5,16 +5,24 @@
} }
}, },
"require": { "require": {
"php" : "^7.2", "php": "^7.2",
"gabordemooij/redbean": "^5.0", "gabordemooij/redbean": "^5.0",
"nesbot/carbon": "^1.31", "nesbot/carbon": "^1.31",
"commerceguys/enum": "^1.0", "commerceguys/enum": "^1.0",
"noplanman/xec": "0.1.0", "noplanman/xec": "0.1.0",
"jaybizzle/crawler-detect" :"1.*", "jaybizzle/crawler-detect": "1.*",
"twbs/bootstrap": "^4.1.0", "twbs/bootstrap": "^4.1.0",
"patrickschur/language-detection": "^3.3" "patrickschur/language-detection": "^3.3"
}, },
"require-dev": {
"squizlabs/php_codesniffer": "^3.3"
},
"autoload": { "autoload": {
"classmap": ["lib"] "classmap": ["lib"]
},
"scripts": {
"check-code": [
"vendor/bin/phpcs --standard=PSR2 -snp --encoding=utf-8 --report-width=150 lib *.php"
]
} }
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "b4a8beac9ae1722bdbae0b3d81d7eb37", "content-hash": "d447ea75efb92876c1bee13e93c2c5aa",
"packages": [ "packages": [
{ {
"name": "commerceguys/enum", "name": "commerceguys/enum",
...@@ -467,7 +467,59 @@ ...@@ -467,7 +467,59 @@
"time": "2018-04-30T16:04:33+00:00" "time": "2018-04-30T16:04:33+00:00"
} }
], ],
"packages-dev": [], "packages-dev": [
{
"name": "squizlabs/php_codesniffer",
"version": "3.3.0",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
"reference": "d86873af43b4aa9d1f39a3601cc0cfcf02b25266"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/d86873af43b4aa9d1f39a3601cc0cfcf02b25266",
"reference": "d86873af43b4aa9d1f39a3601cc0cfcf02b25266",
"shasum": ""
},
"require": {
"ext-simplexml": "*",
"ext-tokenizer": "*",
"ext-xmlwriter": "*",
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"bin": [
"bin/phpcs",
"bin/phpcbf"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Greg Sherwood",
"role": "lead"
}
],
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "http://www.squizlabs.com/php-codesniffer",
"keywords": [
"phpcs",
"standards"
],
"time": "2018-06-06T23:58:19+00:00"
}
],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": [],
......
<?php <?php
/* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */ /**
* Config for Poduptime.
*/
declare(strict_types=1);
//backup directory //backup directory
$backup_dir = '/backup'; $backup_dir = '/backup';
//log directory //log directory
$log_dir = '/log'; $log_dir = '/log';
//location of pg dump //location of pg dump
$pg_dump_dir = '/usr/bin'; $pg_dump_dir = '/usr/bin';
//db host //db host
$pghost = 'localhost'; $pghost = 'localhost';
//db username //db username
$pguser = ''; $pguser = '';
//db password //db password
$pgpass = ''; $pgpass = '';
//db name //db name
$pgdb = ''; $pgdb = '';
//admin email for forms //admin email for forms
$adminemail = ''; $adminemail = '';
//DNS server for dnssec testing. 1.1.1.1 tests the best //DNS server for dnssec testing. 1.1.1.1 tests the best
$dnsserver = ''; $dnsserver = '';
//CA for curl to use - suggest wget http://curl.haxx.se/ca/cacert.pem as they pull from mozilla if you use a system CA then certs like wosign and startssl will be valid but users will be unable to connect to them //CA for curl to use - suggest wget http://curl.haxx.se/ca/cacert.pem as they pull from mozilla if you use a system CA then certs like wosign and startssl will be valid but users will be unable to connect to them
$cafullpath = ''; $cafullpath = '';
//Mapbox.com API key. https://www.mapbox.com/help/how-access-tokens-work/ //Mapbox.com API key. https://www.mapbox.com/help/how-access-tokens-work/
$mapboxkey = ''; $mapboxkey = '';
.sub-header { .sub-header {
border-bottom: 1px solid #eee; border-bottom: 1px solid #eee;
padding-bottom: 10px; padding-bottom: 10px;
} }
.sidebar { .sidebar {
background-color: #fff; background-color: #fff;
border-right: 1px solid #eee; border-right: 1px solid #eee;
display: block; display: block;
overflow-x: hidden; overflow-x: hidden;
overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */ overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
padding: 10px; padding: 10px;
position: fixed; position: fixed;
top: 56px; top: 56px;
} }
.sidebar .nav { .sidebar .nav {
margin-bottom: 20px; margin-bottom: 20px;
} }
.sidebar .nav .nav-link { .sidebar .nav .nav-link {
padding: .3em .7em; padding: .3em .7em;
} }
.main { .main {
padding: 20px; padding: 20px;
} }
.placeholders { .placeholders {
margin-bottom: 30px; margin-bottom: 30px;
text-align: center; text-align: center;
} }
.placeholders h4 { .placeholders h4 {
margin-bottom: 0; margin-bottom: 0;
} }
.placeholder { .placeholder {
margin-bottom: 20px; margin-bottom: 20px;
} }
.placeholder img { .placeholder img {
border-radius: 50%; border-radius: 50%;
display: inline-block; display: inline-block;
} }
.rating { .rating {
font-size:9px; font-size: 9px;
} }
.tfont { .tfont {
font-size:12px; font-size: 12px;
} }
.smlogo { .smlogo {
width: 16px; width: 16px;
height: 16px; height: 16px;
background: url('/images/smlogo.png') 0 0; background: url('/images/smlogo.png') 0 0;
display: inline-block; display: inline-block;
margin: 0 2px; margin: 0 2px;
} }
.smlogo-twitter { .smlogo-twitter {
background-position: 0 -776px; background-position: 0 -776px;
} }
.smlogo-facebook { .smlogo-facebook {
background-position: 0 -824px; background-position: 0 -824px;
} }
.smlogo-tumblr { .smlogo-tumblr {
background-position: 0 -792px; background-position: 0 -792px;
} }
.smlogo-wordpress { .smlogo-wordpress {
background-position: 0 -656px; background-position: 0 -656px;
} }
.smlogo-xmpp { .smlogo-xmpp {
background: none; background: none;
} }
.smlogo-xmpp img { .smlogo-xmpp img {
vertical-align: unset; vertical-align: unset;
} }
#map { #map {
height: 580px; height: 580px;
} }
.mycluster { .mycluster {
width: 35px; width: 35px;
height: 35px; height: 35px;
background-color: blue; background-color: blue;
text-align: left; text-align: left;
font-size: 17px; font-size: 17px;
background: url('/bower_components/leaflet/dist/images/marker-icon-2x.png') repeat-y right bottom; background: url('/bower_components/leaflet/dist/images/marker-icon-2x.png') repeat-y right bottom;
background-size: 25px 37px; background-size: 25px 37px;
} }
.icon { .icon {
color: red; color: red;
} }
table {
empty-cells: show; table {
empty-cells: show;
} }
#facebox { #facebox {
z-index: 1000 !important; z-index: 1000 !important;
} }
#facebox .content { #facebox .content {
width: 800px !important; width: 800px !important;
} }
.tablesorter-pager .btn-group-sm .btn { .tablesorter-pager .btn-group-sm .btn {
font-size: 1.2em; /* make pager arrows more visible */ font-size: 1.2em; /* make pager arrows more visible */
} }
.tablesorter-filter-row td { .tablesorter-filter-row td {
background: #eee;