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
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;
......@@ -65,7 +70,6 @@ EOF;
}
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
......
......@@ -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 = '';
......@@ -2,6 +2,7 @@
border-bottom: 1px solid #eee;
padding-bottom: 10px;
}
.sidebar {
background-color: #fff;
border-right: 1px solid #eee;
......@@ -12,35 +13,45 @@
position: fixed;
top: 56px;
}
.sidebar .nav {
margin-bottom: 20px;
}
.sidebar .nav .nav-link {
padding: .3em .7em;
}
.main {
padding: 20px;
}
.placeholders {
margin-bottom: 30px;
text-align: center;
}
.placeholders h4 {
margin-bottom: 0;
}
.placeholder {
margin-bottom: 20px;
}
.placeholder img {
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;
......@@ -48,27 +59,35 @@
display: inline-block;
margin: 0 2px;
}
.smlogo-twitter {
background-position: 0 -776px;
}
.smlogo-facebook {
background-position: 0 -824px;
}
.smlogo-tumblr {
background-position: 0 -792px;
}
.smlogo-wordpress {
background-position: 0 -656px;
}
.smlogo-xmpp {
background: none;
}
.smlogo-xmpp img {
vertical-align: unset;
}
#map {
height: 580px;
}
.mycluster {
width: 35px;
height: 35px;
......@@ -78,15 +97,19 @@
background: url('/bower_components/leaflet/dist/images/marker-icon-2x.png') repeat-y right bottom;
background-size: 25px 37px;
}
.icon {
color: red;
}
table {
empty-cells: show;
}
#facebox {
z-index: 1000 !important;
}
#facebox .content {
width: 800px !important;
}
......
<!-- /* 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));
......@@ -85,7 +90,7 @@ EOF;
if ($infos = json_decode(file_get_contents('https://' . $_domain . '/.well-known/nodeinfo'), true)) {
$link = max($infos['links'])['href'];
} else {
$link= 'https://' . $_domain . '/.well-known/nodeinfo';
$link = 'https://' . $_domain . '/.well-known/nodeinfo';
}
$chss = curl_init();
......
<?php
//Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file.
//this is just a single api for a pod for the android app to get data
/**
* This is just a single API for a pod for the Android app to get data.
*/
declare(strict_types=1);
use Poduptime\PodStatus;
use RedBeanPHP\R;
// Required parameters.
......@@ -33,10 +38,10 @@ try {
if ($_format === 'json') {
echo json_encode($pod);
} else {
if ($pod['status'] === PodStatus::Up) {
if ($pod['status'] === PodStatus::UP) {
echo 'Status: Up<br>';
}
if ($pod['status'] === PodStatus::Down) {
if ($pod['status'] === PodStatus::DOWN) {
echo 'Status: Down<br>';
}
echo 'Last Git Pull: ' . $pod['hgitdate'] . '<br>';
......
<?php
/**
* Backup / dump PostgreSQL database.
*/
declare(strict_types=1);
if (PHP_SAPI !== 'cli') {
header('HTTP/1.0 403 Forbidden');
exit;
......@@ -9,10 +15,10 @@ require_once __DIR__ . '/../config.php';
$keep = (60 * 60 * 6) * 1;
$dump_date = date('Ymd_Hs');
$file_name = __DIR__ . '/..' .$backup_dir . '/dump_' . $dump_date . '.sql';
$file_name = __DIR__ . '/..' . $backup_dir . '/dump_' . $dump_date . '.sql';
system("export PGPASSWORD=$pgpass && $pg_dump_dir/pg_dump --username=$pguser $pgdb >> $file_name");
echo "pg backup of $pgdb made";
$dirh = dir(__DIR__ . '/..' .$backup_dir);
$dirh = dir(__DIR__ . '/..' . $backup_dir);
while ($entry = $dirh->read()) {
$old_file_time = (date('U') - $keep);
$file_created = filectime(__DIR__ . '/..' . "$backup_dir/$entry");
......
<?php
/**
* Edit an existing pod.
*/
declare(strict_types=1);
use Poduptime\PodStatus;
use RedBeanPHP\R;
use CommerceGuys\Enum\AbstractEnum;
// Required parameters.
($_domain = $_GET['domain'] ?? null) || die('no pod domain given');
......@@ -31,7 +37,7 @@ $pod['tokenexpire'] >= date('Y-m-d H:i:s') || die('token expired');
// Delete and exit.
if ('delete' === $_action) {
try {
$pod['status'] = PodStatus::User_Deleted;
$pod['status'] = PodStatus::USER_DELETED;
R::store($pod);
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
......@@ -42,7 +48,7 @@ if ('delete' === $_action) {
// Pause and exit.
if ('pause' === $_action) {
try {
$pod['status'] = PodStatus::Paused;
$pod['status'] = PodStatus::PAUSED;
R::store($pod);
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
......@@ -100,7 +106,7 @@ if ('save' === $_action) {
<input type="submit" name="action" value="save">
</form>
<br>
<br>Your pod status is currently: <?php echo PodStatus::getKey((int)$pod['status']); ?>
<br>Your pod status is currently: <?php echo PodStatus::getKey((int) $pod['status']); ?>
<br>
<form>
<input type="hidden" name="edit">
......@@ -122,8 +128,8 @@ if ('save' === $_action) {
</form>
<button type="button" class="openBtn" value="<?php echo $_domain; ?>">Do a debug test pull of your pod</button>
<!-- Modal -->
<div class="modal fade" id="podpull" role="dialog">
<!-- Modal -->
<div class="modal fade" id="podpull" role="dialog">
<div class="modal-dialog modal-xlg">
<div class="modal-content">
<div class="modal-header">
......@@ -138,7 +144,4 @@ if ('save' === $_action) {
</div>
</div>
</div>
</div>
<?php
</div>
<?php
/**
* Get token to allow pod editing.
*/
declare(strict_types=1);
use RedBeanPHP\R;
// Required parameters.
......
<?php
/**
* Compute monthly stats.
*/
declare(strict_types=1);
if (PHP_SAPI !== 'cli') {
header('HTTP/1.0 403 Forbidden');
exit;
......@@ -36,7 +42,7 @@ try {
foreach ($monthly_totals as $monthly) {
// Format date to timestamp.
$timestamp = $monthly['yymm'].'-01 01:01:01-01';
$timestamp = $monthly['yymm'] . '-01 01:01:01-01';
try {
$p = R::findOrCreate('monthlystats', ['date_checked' => $timestamp]);
......
<?php
/**
* Crawl and add all pods from the-federation.info list.
*/
declare(strict_types=1);
if (PHP_SAPI !== 'cli') {
header('HTTP/1.0 403 Forbidden');
exit;
......
<?php
//* Copyright (c) 2017, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */
/**
* Pull master software versions.
*/
declare(strict_types=1);
if (PHP_SAPI !== 'cli') {
header('HTTP/1.0 403 Forbidden');
......@@ -29,7 +34,7 @@ $softwares = [
];
$opts = [
'http' => ['method' => 'GET', 'header' => ['User-Agent: Poduptime']]
'http' => ['method' => 'GET', 'header' => ['User-Agent: Poduptime']],
];
foreach ($softwares as $software => $details) {
......
<?php
//* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */
/**
* Pull pod info.
*/
declare(strict_types=1);
if ($_SERVER['SERVER_ADDR'] !== $_SERVER['REMOTE_ADDR']) {
header('HTTP/1.0 403 Forbidden');
exit;
}
use RedBeanPHP\R;
use LanguageDetection\Language;
use Poduptime\PodStatus;
use RedBeanPHP\R;
$debug = isset($_GET['debug']) || (isset($argv) && in_array('debug', $argv, true));
$sqldebug = isset($_GET['sqldebug']) || (isset($argv) && in_array('sqldebug', $argv, true));
......@@ -42,10 +48,10 @@ try {
$pods = R::getAll($sql, [$_domain]);
} elseif (PHP_SAPI === 'cli' && (isset($argv) && in_array('Check_System_Deleted', $argv, true))) {
$sql .= ' WHERE status = ?';
$pods = R::getAll($sql, [PodStatus::System_Deleted]);
$pods = R::getAll($sql, [PodStatus::SYSTEM_DELETED]);
} elseif (PHP_SAPI === 'cli') {
$sql .= ' WHERE status < ?';
$pods = R::getAll($sql, [PodStatus::Paused]);
$pods = R::getAll($sql, [PodStatus::PAUSED]);
}
} catch (\RedBeanPHP\RedException $e) {
die('Error in SQL query: ' . $e->getMessage());
......@@ -179,7 +185,7 @@ foreach ($pods as $pod) {
die('Error in SQL query: ' . $e->getMessage());
}
$status = PodStatus::Up;
$status = PodStatus::UP;
}
if (!$jsonssl) {
......@@ -201,7 +207,7 @@ foreach ($pods as $pod) {
}
$score -= 1;
$status = PodStatus::Down;
$status = PodStatus::DOWN;
}
_debug('Version code', $shortversion);
......@@ -270,10 +276,10 @@ foreach ($pods as $pod) {
$masterversion = $masterdata['version'];
_debug('Masterversion', $masterversion);
$masterversioncheck = explode('.',$masterversion);
$shortversioncheck = explode('.',$shortversion);
$masterversioncheck = explode('.', $masterversion);
$shortversioncheck = explode('.', $shortversion);
_debug('Days since master code release', date_diff((new DateTime($masterdata['releasedate'])),(new DateTime()))->format('%d'));
_debug('Days since master code release', date_diff((new DateTime($masterdata['releasedate'])), (new DateTime()))->format('%d'));
try {
......@@ -282,25 +288,28 @@ foreach ($pods as $pod) {
die('Error in SQL query: ' . $e->getMessage());
}
$devlastdays = $masterdata['devlastcommit'] ? date_diff((new DateTime($masterdata['devlastcommit'])),(new DateTime()))->format('%a') : 30;//tmp//if no dev branch then what?
$devlastdays = $masterdata['devlastcommit'] ? date_diff((new DateTime($masterdata['devlastcommit'])), (new DateTime()))->format('%a') : 30;//tmp//if no dev branch then what?
_debug('Dev git last commit was ', $devlastdays);
if (strpos($xdver,'dev') !== false || strpos($xdver,'rc') !== false || $shortversioncheck > $masterversioncheck) {
if (strpos($xdver, 'dev') !== false || strpos($xdver, 'rc') !== false || $shortversioncheck > $masterversioncheck) {
//tmp//if pod is on the development branch - see when you last updated your pod and when the last commit was made to dev branch - if the repo is active and your not updating every 120 days why are you on dev branch?
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])),(new DateTime($masterdata['devlastcommit'])))->format('%a');
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])), (new DateTime($masterdata['devlastcommit'])))->format('%a');
if ($updategap + $devlastdays > 130) {
_debug('Outdated', 'Yes');$score -= 2;
_debug('Outdated', 'Yes');
$score -= 2;
}
} elseif (($masterversioncheck[1] - $shortversioncheck[1]) > 1) {
///tmp/If pod is two versions off AND it's been more than 60 days since that release came out AND your on the master production branch
_debug('Outdated', 'Yes');$score -= 2;
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])),(new DateTime($masterdata['releasedate'])))->format('%a');
} elseif ($updategap - date_diff((new DateTime($masterdata['releasedate'])),(new DateTime()))->format('%a') > 90) {
_debug('Outdated', 'Yes');$score -= 2;
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])),(new DateTime($masterdata['releasedate'])))->format('%a');
_debug('Outdated', 'Yes');
$score -= 2;
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])), (new DateTime($masterdata['releasedate'])))->format('%a');
} elseif ($updategap - date_diff((new DateTime($masterdata['releasedate'])), (new DateTime()))->format('%a') > 90) {
_debug('Outdated', 'Yes');
$score -= 2;
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])), (new DateTime($masterdata['releasedate'])))->format('%a');
} else {
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])),(new DateTime($masterdata['releasedate'])))->format('%a');
$updategap = date_diff((new DateTime($lastpodupdates['date_checked'])), (new DateTime($masterdata['releasedate'])))->format('%a');
}
_debug('Pod code was updated after ', $updategap);
......@@ -320,7 +329,7 @@ foreach ($pods as $pod) {
} elseif ($score < 0) {
$score = 0;
if ($masterv <> $shortv) {
$status = PodStatus::System_Deleted;
$status = PodStatus::SYSTEM_DELETED;
}
}
_debug('Score', $score);
......@@ -355,7 +364,7 @@ foreach ($pods as $pod) {
$p['sslvalid'] = $outputsslerror;
$p['dnssec'] = $dnssec;
$p['sslexpire'] = $sslexpire;
if ($dbstatus == PodStatus::Up && $status == PodStatus::Up) {
if ($dbstatus == PodStatus::UP && $status == PodStatus::UP) {
$p['shortversion'] = $shortversion;
$p['signup'] = $signup;
$p['total_users'] = $total_users;
......
<?php
/**
* Save pod rating.
*/
declare(strict_types=1);
use RedBeanPHP\R;
// Required parameters.
......
<?php
/**
* Check status of previously pulled data.
*/
declare(strict_types=1);
$dur = (time() - filemtime('last.data'));
echo $dur;
if ($dur > 4500) {
......