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

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
config.php
node_modules
vendor
<?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;
......@@ -21,7 +26,7 @@ R::testConnection() || die('Error in DB connection');
R::usePartialBeans(true);
if ($_format === 'georss') {
echo <<<EOF
echo <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">
<title>Diaspora Pods</title>
......@@ -30,26 +35,26 @@ if ($_format === 'georss') {
EOF;
try {
$pods = R::getAll('
SELECT name, monthsmonitored, responsetimelast7, uptimelast7, dateupdated, score, domain, country, lat, long
FROM pods_apiv1
');
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
}
foreach ($pods as $pod) {
$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',
htmlentities($pod['name'], ENT_QUOTES),
$pod['monthsmonitored'],
$pod['responsetimelast7'],
$pod['uptimelast7'],
$pod['dateupdated'],
$pod['score']
);
echo <<<EOF
try {
$pods = R::getAll('
SELECT name, monthsmonitored, responsetimelast7, uptimelast7, dateupdated, score, domain, country, lat, long
FROM pods_apiv1
');
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
}
foreach ($pods as $pod) {
$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',
htmlentities($pod['name'], ENT_QUOTES),
$pod['monthsmonitored'],
$pod['responsetimelast7'],
$pod['uptimelast7'],
$pod['dateupdated'],
$pod['score']
);
echo <<<EOF
<entry>
<title>https://{$pod['domain']}</title>
<link href="https://{$pod['domain']}"/>
......@@ -62,51 +67,50 @@ EOF;
</entry>
EOF;
}
echo '</feed>';
}
echo '</feed>';
} 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 {
$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');
$obj = [
'podcount' => count($pods),
'pods' => allToString($pods),
];
if ($_method === 'jsonp') {
print $_callback . '(' . json_encode($obj) . ')';
} else {
print json_encode($obj);
}
$obj = [
'podcount' => count($pods),
'pods' => allToString($pods),
];
if ($_method === 'jsonp') {
print $_callback . '(' . json_encode($obj) . ')';
} else {
print json_encode($obj);
}
} else {
try {
$pods = R::getAll('
SELECT domain, uptimelast7, country
FROM pods_apiv1
');
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
}
$i = 0;
foreach ($pods as $pod) {
$i++ > 0 && print ',';
printf(
'%1$s Up %2$s%% This Month - Located in: %3$s',
$pod['domain'],
$pod['uptimelast7'],
$pod['country']
);
}
try {
$pods = R::getAll('
SELECT domain, uptimelast7, country
FROM pods_apiv1
');
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
}
$i = 0;
foreach ($pods as $pod) {
$i++ > 0 && print ',';
printf(
'%1$s Up %2$s%% This Month - Located in: %3$s',
$pod['domain'],
$pod['uptimelast7'],
$pod['country']
);
}
}
/**
......@@ -121,18 +125,18 @@ EOF;
*/
function allToString(array $arr)
{
$ret = $arr;
foreach ($ret as &$item) {
if (is_array($item)) {
/** @var array $item */
foreach ($item as &$field) {
$field !== null && $field = (string) $field;
}
} else {
$item !== null && $item = (string) $item;
$ret = $arr;
foreach ($ret as &$item) {
if (is_array($item)) {
/** @var array $item */
foreach ($item as &$field) {
$field !== null && $field = (string) $field;
}
} else {
$item !== null && $item = (string) $item;
}
unset($field, $item);
}
unset($field, $item);
}
return $ret;
return $ret;
}
{
"name": "poduptime",
"homepage": "https://github.com/diasporg/Poduptime",
"authors": [
"David Morley <support@diasp.org>"
],
"description": "Diaspora pod checking site",
"main": "",
"keywords": [
"diaspora"
],
"license": "AGPL",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"facebox": "*"
}
"name": "poduptime",
"homepage": "https://github.com/diasporg/Poduptime",
"authors": [
"David Morley <support@diasp.org>"
],
"description": "Diaspora pod checking site",
"main": "",
"keywords": [
"diaspora"
],
"license": "AGPL",
"private": true,
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"facebox": "*"
}
}
......@@ -5,16 +5,24 @@
}
},
"require": {
"php" : "^7.2",
"php": "^7.2",
"gabordemooij/redbean": "^5.0",
"nesbot/carbon": "^1.31",
"commerceguys/enum": "^1.0",
"noplanman/xec": "0.1.0",
"jaybizzle/crawler-detect" :"1.*",
"jaybizzle/crawler-detect": "1.*",
"twbs/bootstrap": "^4.1.0",
"patrickschur/language-detection": "^3.3"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.3"
},
"autoload": {
"classmap": ["lib"]
},
"scripts": {
"check-code": [
"vendor/bin/phpcs --standard=PSR2 -snp --encoding=utf-8 --report-width=150 lib *.php"
]
}
}
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b4a8beac9ae1722bdbae0b3d81d7eb37",
"content-hash": "d447ea75efb92876c1bee13e93c2c5aa",
"packages": [
{
"name": "commerceguys/enum",
......@@ -467,7 +467,59 @@
"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": [],
"minimum-stability": "stable",
"stability-flags": [],
......
<?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_dir = '/backup';
//log directory
$log_dir = '/log';
//location of pg dump
$pg_dump_dir = '/usr/bin';
//db host
$pghost = 'localhost';
//db username
$pguser = '';
//db password
$pgpass = '';
//db name
$pgdb = '';
//admin email for forms
$adminemail = '';
//DNS server for dnssec testing. 1.1.1.1 tests the best
$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
$cafullpath = '';
//Mapbox.com API key. https://www.mapbox.com/help/how-access-tokens-work/
$mapboxkey = '';
.sub-header {
border-bottom: 1px solid #eee;
padding-bottom: 10px;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
.sidebar {
background-color: #fff;
border-right: 1px solid #eee;
display: block;
overflow-x: hidden;
overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
padding: 10px;
position: fixed;
top: 56px;
background-color: #fff;
border-right: 1px solid #eee;
display: block;
overflow-x: hidden;
overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
padding: 10px;
position: fixed;
top: 56px;
}
.sidebar .nav {
margin-bottom: 20px;
margin-bottom: 20px;
}
.sidebar .nav .nav-link {
padding: .3em .7em;
padding: .3em .7em;
}
.main {
padding: 20px;
padding: 20px;
}
.placeholders {
margin-bottom: 30px;
text-align: center;
margin-bottom: 30px;
text-align: center;
}
.placeholders h4 {
margin-bottom: 0;
margin-bottom: 0;
}
.placeholder {
margin-bottom: 20px;
margin-bottom: 20px;
}
.placeholder img {
border-radius: 50%;
display: inline-block;
border-radius: 50%;
display: inline-block;
}
.rating {
font-size:9px;
font-size: 9px;
}
.tfont {
font-size:12px;
font-size: 12px;
}
.smlogo {
width: 16px;
height: 16px;
background: url('/images/smlogo.png') 0 0;
display: inline-block;
margin: 0 2px;
width: 16px;
height: 16px;
background: url('/images/smlogo.png') 0 0;
display: inline-block;
margin: 0 2px;
}
.smlogo-twitter {
background-position: 0 -776px;
background-position: 0 -776px;
}
.smlogo-facebook {
background-position: 0 -824px;
background-position: 0 -824px;
}
.smlogo-tumblr {
background-position: 0 -792px;
background-position: 0 -792px;
}
.smlogo-wordpress {
background-position: 0 -656px;
background-position: 0 -656px;
}
.smlogo-xmpp {
background: none;
background: none;
}
.smlogo-xmpp img {
vertical-align: unset;
vertical-align: unset;
}
#map {
height: 580px;
height: 580px;
}
.mycluster {
width: 35px;
height: 35px;
background-color: blue;
text-align: left;
font-size: 17px;
background: url('/bower_components/leaflet/dist/images/marker-icon-2x.png') repeat-y right bottom;
background-size: 25px 37px;
width: 35px;
height: 35px;
background-color: blue;
text-align: left;
font-size: 17px;
background: url('/bower_components/leaflet/dist/images/marker-icon-2x.png') repeat-y right bottom;
background-size: 25px 37px;
}
.icon {
color: red;
color: red;
}
table {
empty-cells: show;
table {
empty-cells: show;
}
#facebox {
z-index: 1000 !important;
z-index: 1000 !important;
}
#facebox .content {
width: 800px !important;
width: 800px !important;
}
.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 {
background: #eee;
line-height: normal;
text-align: center; /* center the input */
-webkit-transition: line-height 0.1s ease;
-moz-transition: line-height 0.1s ease;
-o-transition: line-height 0.1s ease;
transition: line-height 0.1s ease;
background: #eee;
line-height: normal;
text-align: center; /* center the input */
-webkit-transition: line-height 0.1s ease;
-moz-transition: line-height 0.1s ease;
-o-transition: line-height 0.1s ease;
transition: line-height 0.1s ease;
}
.tablesorter-filter-row .disabled {
opacity: 0.5;
filter: alpha(opacity=50);
cursor: not-allowed;
opacity: 0.5;
filter: alpha(opacity=50);
cursor: not-allowed;
}
.tablesorter-filter-row.hideme td {
/*** *********************************************** ***/
/*** change this padding to modify the thickness ***/
/*** of the closed filter row (height = padding x 2) ***/
padding: 1px;
/*** *********************************************** ***/
margin: 0;
line-height: 0;
cursor: pointer;
/*** *********************************************** ***/
/*** change this padding to modify the thickness ***/
/*** of the closed filter row (height = padding x 2) ***/
padding: 1px;
/*** *********************************************** ***/
margin: 0;
line-height: 0;
cursor: pointer;
}
.tablesorter-filter-row.hideme * {
height: 1px;
min-height: 0;
border: 0;
padding: 0;
margin: 0;
/* don't use visibility: hidden because it disables tabbing */
opacity: 0;
filter: alpha(opacity=0);
height: 1px;
min-height: 0;
border: 0;
padding: 0;
margin: 0;
/* don't use visibility: hidden because it disables tabbing */
opacity: 0;
filter: alpha(opacity=0);
}
/* filters */
.tablesorter-filter {
width: 98%;
height: inherit;
margin: 1px;
padding: 1px;
background-color: #fff;
border: 1px solid #bbb;
color: #333;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-transition: height 0.1s ease;
-moz-transition: height 0.1s ease;
-o-transition: height 0.1s ease;
transition: height 0.1s ease;
width: 98%;
height: inherit;
margin: 1px;
padding: 1px;
background-color: #fff;
border: 1px solid #bbb;
color: #333;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-transition: height 0.1s ease;
-moz-transition: height 0.1s ease;
-o-transition: height 0.1s ease;
transition: height 0.1s ease;
}
.pagination {
display: inline-block;
color: black;
float: left;
padding: 8px 16px;
margin: 0 4px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
cursor: pointer;
display: inline-block;
color: black;
float: left;
padding: 8px 16px;
margin: 0 4px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
cursor: pointer;
}
.pagination.disabled {
color: #f0f0f0;
color: #f0f0f0;
}
.pagedisplay {
display: inline-block;
color: black;
float: left;
padding: 8px 16px !important;
margin: 0 4px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
cursor: pointer;
display: inline-block;
color: black;
float: left;
padding: 8px 16px !important;
margin: 0 4px;
text-decoration: none;
transition: background-color .3s;
border: 1px solid #ddd;
cursor: pointer;
}
.tablesorter-header {
background-size: 10px 15px !important;
background-size: 10px 15px !important;
}
.modal-xlg {
max-width: 92%;
max-width: 92%;
}
<!-- /* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */ -->
<?php
/**
* Add a new pod.
*/
declare(strict_types=1);
use Poduptime\Logging;
use RedBeanPHP\R;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../logging.php';
require_once __DIR__ . '/../config.php';
define('PODUPTIME', microtime(true));
......@@ -12,8 +17,8 @@ define('PODUPTIME', microtime(true));
$log = new Logging();
$log->lfile(__DIR__ . '/../' . $log_dir . '/add.log');
if (!($_domain = $_GET['domain'] ?? null)) {
$log->lwrite('no domain given');
die('no pod domain given');
$log->lwrite('no domain given');
die('no pod domain given');
}
// Other parameters.
......@@ -23,7 +28,7 @@ $_podmin_notify = $_GET['podmin_notify'] ?? 0;
$_domain = strtolower($_domain);
if (!filter_var(gethostbyname($_domain), FILTER_VALIDATE_IP)) {
die('Could not validate the domain name, be sure to enter it as "domain.com" (no caps, no slashes, no extras)');
die('Could not validate the domain name, be sure to enter it as "domain.com" (no caps, no slashes, no extras)');
}
// Set up global DB connection.
......@@ -32,38 +37,38 @@ R::testConnection() || die('Error in DB connection');
R::usePartialBeans(true);
try {
$pods = R::getAll('
$pods = R::getAll('
SELECT id, domain, publickey, email
FROM pods
');
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
die('Error in SQL query: ' . $e->getMessage());