diff --git a/api.php b/api.php index a4ffcd0be73ac73bb0b439ec842c7de94a3cbb8f..5cde5dfe223bbfc7a1113b9b5bc371a012b996cc 100644 --- a/api.php +++ b/api.php @@ -1,5 +1,8 @@ EOF; - $sql = "SELECT name,monthsmonitored,responsetimelast7,uptimelast7,dateupdated,score,domain,country,lat,long FROM pods_apiv1"; - $result = pg_query($dbh, $sql); - $result || die('Error in SQL query: ' . pg_last_error()); - - $numrows = pg_num_rows($result); - while ($row = pg_fetch_array($result)) { - $pod_name = htmlentities($row['name'], ENT_QUOTES); - $summary = sprintf( + + 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', - $pod_name, - $row['monthsmonitored'], - $row['responsetimelast7'], - $row['uptimelast7'], - $row['dateupdated'], - $row['score'] + htmlentities($pod['name'], ENT_QUOTES), + $pod['monthsmonitored'], + $pod['responsetimelast7'], + $pod['uptimelast7'], + $pod['dateupdated'], + $pod['score'] ); echo << - https://{$row['domain']} - - urn:{$row['domain']} - Pod Location is: {$row['country']} + https://{$pod['domain']} + + urn:{$pod['domain']} + Pod Location is: {$pod['country']} {$summary} - {$row['lat']} {$row['long']} - {$row['domain']} + {$pod['lat']} {$pod['long']} + {$pod['domain']} EOF; } echo ''; } elseif ($_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_apiv1'; - $result = pg_query($dbh, $sql); - $result || die('Error in SQL query: ' . pg_last_error()); + + 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'); - $numrows = pg_num_rows($result); - $rows = array_values(pg_fetch_all($result)); - $obj = [ - 'podcount' => $numrows, - 'pods' => $rows, + $obj = [ + 'podcount' => count($pods), + 'pods' => allToString($pods), ]; if ($_method === 'jsonp') { print $_callback . '(' . json_encode($obj) . ')'; @@ -72,20 +87,51 @@ EOF; print json_encode($obj); } } else { - $i = 0; - $sql = "SELECT domain,uptimelast7,country FROM pods_apiv1"; - $result = pg_query($dbh, $sql); - $result || die('Error in SQL query: ' . pg_last_error()); - - $numrows = pg_num_rows($result); - while ($row = pg_fetch_array($result)) { + 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', - $row['domain'], - $row['uptimelast7'], - $row['country'] + $pod['domain'], + $pod['uptimelast7'], + $pod['country'] ); } } + +/** + * Convert all passed items to strings. + * + * This method is for backwards compatibility of APIv1 only! + * After v2 is released and stable, this can safely be removed. + * + * @param array $arr List of all elements to stringify. + * + * @return array + */ +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; + } + unset($field, $item); + } + + return $ret; +} diff --git a/cleanup.php b/cleanup.php index b03ffe0c85cf70b27359f3d5eb685e1b7243f03e..b614feeb1fe1ae1e19384335b23b0c1ef4b85778 100644 --- a/cleanup.php +++ b/cleanup.php @@ -1,16 +1,22 @@ getMessage()); +} ?> - pods that are open for signup now. + pods that are open for signup now.
@@ -36,59 +42,60 @@ $numrows = pg_num_rows($result); ' . $row['domain'] . '
?
'; + echo '' . $pod['domain'] . '
?
'; - if (stristr($row['shortversion'], 'head')) { + if (stristr($pod['shortversion'], 'head')) { $version = '.dev'; $pre = 'This pod runs pre release development code'; - } elseif (!$row['shortversion']) { + } elseif (!$pod['shortversion']) { $version = '0'; $pre = 'This pod runs unknown code'; } else { - $version = $row['shortversion']; + $version = $pod['shortversion']; $pre = 'This pod runs production code'; } - if ($row['shortversion'] === $row['masterversion'] && $row['shortversion'] !== '') { + if ($pod['shortversion'] === $pod['masterversion'] && $pod['shortversion'] !== '') { $classver = 'text-success'; } elseif ($verdiff > 6) { $classver = 'text-warning'; } else { $classver = 'black'; } - echo '
' . $version . '
'; - echo '' . $row['uptime_alltime'] . ''; - echo '' . $row['latency'] . ''; - echo '' . ($row['signup'] === 't' ? 'Open' : 'Closed') . ''; - echo '' . $row['total_users'] . ''; - echo '' . $row['active_users_halfyear'] . ''; - echo '' . $row['active_users_monthly'] . ''; - echo '' . $row['local_posts'] . ''; - echo '' . $row['comment_counts'] . ''; - $moreurl = 'https://api.uptimerobot.com/getMonitors?format=json&noJsonCallback=1&customUptimeRatio=7-30-60-90&apiKey=' . $row['stats_apikey']; - echo ''; - echo '' . $row['score'] . ''; - echo '
con info'; + echo '
' . $version . '
'; + echo '' . $pod['uptime_alltime'] . ''; + echo '' . $pod['latency'] . ''; + echo '' . ($pod['signup'] ? 'Open' : 'Closed') . ''; + echo '' . $pod['total_users'] . ''; + echo '' . $pod['active_users_halfyear'] . ''; + echo '' . $pod['active_users_monthly'] . ''; + echo '' . $pod['local_posts'] . ''; + echo '' . $pod['comment_counts'] . ''; + $moreurl = 'https://api.uptimerobot.com/getMonitors?format=json&noJsonCallback=1&customUptimeRatio=7-30-60-90&apiKey=' . $pod['stats_apikey']; + echo ''; + echo '' . $pod['score'] . ''; + echo '
con info'; ?>
- + - + diff --git a/composer.json b/composer.json index 3384d9bf8aa345e9d1d08fa1dad39cd626d67b68..b9a8aecbd92923eb27191573333d52160ec882bd 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,6 @@ { "require": { - "noplanman/xec": "0.1.0" + "noplanman/xec": "0.1.0", + "gabordemooij/redbean": "^4.3" } } diff --git a/composer.lock b/composer.lock index a5ed2714b4a711d27d7e1d61b7251ee22328965e..fe5372f8b595f3583557dcea240a93f8b55e58f6 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,49 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "ebe2243c0fd188ec12ddf7a9f99980ad", + "content-hash": "8e8b0627248a269ae9d624bef32e61d6", "packages": [ + { + "name": "gabordemooij/redbean", + "version": "v4.3.3", + "source": { + "type": "git", + "url": "https://github.com/gabordemooij/redbean.git", + "reference": "1c7ec69850e9f7966ff7feb87b01d8f43a9753d3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/gabordemooij/redbean/zipball/1c7ec69850e9f7966ff7feb87b01d8f43a9753d3", + "reference": "1c7ec69850e9f7966ff7feb87b01d8f43a9753d3", + "shasum": "" + }, + "require": { + "php": ">=5.3.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "RedBeanPHP\\": "RedBeanPHP" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "New BSD and GPLv2" + ], + "authors": [ + { + "name": "Gabor de Mooij", + "email": "gabor@redbeanphp.com", + "homepage": "http://redbeanphp.com" + } + ], + "description": "RedBeanPHP ORM", + "homepage": "http://redbeanphp.com/", + "keywords": [ + "orm" + ], + "time": "2016-10-03T21:25:17+00:00" + }, { "name": "noplanman/xec", "version": "v0.1.0", diff --git a/config.php.example b/config.php.example index 090caed94e386428aea359484cf63ec085939b2c..c9f53bf1702021a8e9a6aa4ed9a6964be0900a15 100644 --- a/config.php.example +++ b/config.php.example @@ -6,6 +6,8 @@ $backup_dir = '/backup'; $log_dir = '/log'; //location of pg dump $pg_dump_dir = '/usr/bin'; +//db host +$pghost = 'localhost'; //db username $pguser = ''; //db password diff --git a/db/add.php b/db/add.php index 5dbc26a0bbe9c9d6870eeed2c55457d38a682bf6..c836e060c287046e057f14c288098faf230f2a0b 100644 --- a/db/add.php +++ b/db/add.php @@ -1,7 +1,14 @@ lfile(__DIR__ . '/../' . $log_dir . '/add.log'); if (!($_domain = $_GET['domain'] ?? null)) { @@ -9,6 +16,7 @@ if (!($_domain = $_GET['domain'] ?? null)) { die('no pod domain given'); } +// Other parameters. $_email = $_GET['email'] ?? ''; $_podmin_statement = $_GET['podmin_statement'] ?? ''; $_podmin_notify = $_GET['podmin_notify'] ?? 0; @@ -18,29 +26,41 @@ 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)'); } -$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass"); -$dbh || die('Error in connection: ' . pg_last_error()); +// Set up global DB connection. +R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true); +R::testConnection() || die('Error in DB connection'); -$sql = 'SELECT domain, stats_apikey, publickey, email FROM pods'; -$result = pg_query($dbh, $sql); -$result || die('Error in SQL query: ' . pg_last_error()); +try { + $pods = R::getAll(' + SELECT id, domain, stats_apikey, publickey, email + FROM pods + '); +} catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); +} -while ($row = pg_fetch_array($result)) { - if ($row['domain'] === $_domain ) { - if ($row['email']) { +foreach ($pods as $pod) { + if ($pod['domain'] === $_domain) { + if ($pod['email']) { $log->lwrite('domain already exists and is registered to an owner' . $_domain); die('domain already exists and is registered to an owner, use the edit function to modify'); } $digtxt = exec(escapeshellcmd('dig ' . $_domain . ' TXT +short')); - if (strpos($digtxt, $row['publickey']) !== false) { - echo 'domain validated, you can now add details '; - $uuid = md5(uniqid($_domain, true)); - $expire = time() + 2700; - $sql = 'UPDATE pods SET token = $1, tokenexpire = $2 WHERE domain = $3'; - $result = pg_query_params($dbh, $sql, [$uuid, date('Y-m-d H:i:s', $expire), $_domain]); - $result || die('Error in SQL query: ' . pg_last_error()); - + if (strpos($digtxt, $pod['publickey']) !== false) { + echo 'domain validated, you can now add details '; + $uuid = md5(uniqid($_domain, true)); + $expire = time() + 2700; + + try { + $p = R::findOne('pods', $pod['id']); + $p['token'] = $uuid; + $p['tokenexpire'] = date('Y-m-d H:i:s', $expire); + R::store($p); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } + echo << @@ -51,11 +71,11 @@ while ($row = pg_fetch_array($result)) { EOF; - + die; } else { $log->lwrite('domain already exists and can be registered' . $_domain); - die('domain already exists, you can claim the domain by adding a DNS TXT record that states
' . $_domain . ' IN TXT "' . $row['publickey'] . '"'); + die('domain already exists, you can claim the domain by adding a DNS TXT record that states
' . $_domain . ' IN TXT "' . $pod['publickey'] . '"'); } } } @@ -74,10 +94,19 @@ if (stristr($outputssl, 'openRegistrations')) { $log->lwrite('Your pod has ssl and is valid ' . $_domain); echo 'Your pod has ssl and is valid
'; - $publickey = md5(uniqid($domain, true)); - $sql = 'INSERT INTO pods (domain, email, podmin_statement, podmin_notify, publickey) VALUES ($1, $2, $3, $4, $5)'; - $result = pg_query_params($dbh, $sql, [$_domain, $_email, $_podmin_statement, $_podmin_notify, $publickey]); - $result || die('Error in SQL query: ' . pg_last_error()); + $publickey = md5(uniqid($_domain, true)); + + try { + $p = R::dispense('pods'); + $p['domain'] = $_domain; + $p['email'] = $_email; + $p['podmin_statement'] = $_podmin_statement; + $p['podmin_notify'] = $_podmin_notify; + $p['publickey'] = $publickey; + R::store($p); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } if ($_email) { $to = $adminemail; @@ -94,7 +123,7 @@ if (stristr($outputssl, 'openRegistrations')) { @mail($to, $subject, implode("\r\n", $message_lines), implode("\r\n", $headers)); } - + echo 'Data successfully inserted! Your pod will be reviewed and live on the list in a few hours!'; } else { diff --git a/db/api-more.php b/db/api-more.php index 3908c492aad9d1443b377fa94fdaf8f119ec6d05..f175000a25bbf3a267af174dfe077b3380d23d0e 100644 --- a/db/api-more.php +++ b/db/api-more.php @@ -2,33 +2,43 @@ //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 +use RedBeanPHP\R; + // Required parameters. ($_domain = $_GET['domain'] ?? null) || die('no domain given'); // Other parameters. $_format = $_GET['format'] ?? ''; +require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../config.php'; -$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass"); -$dbh || die('Error in connection: ' . pg_last_error()); - -$sql = 'SELECT hgitdate,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 WHERE domain = $1'; -$result = pg_query_params($dbh, $sql, [$_domain]); -$result || die('Error in SQL query: ' . pg_last_error()); - -while ($row = pg_fetch_array($result)) { - if ($_format === 'json') { - echo json_encode($row); - } else { - echo 'Status: ' . $row['status'] . '
'; - echo 'Last Git Pull: ' . $row['hgitdate'] . '
'; - echo 'Uptime This Month ' . $row['uptimelast7'] . '
'; - echo 'Months Monitored: ' . $row['monthsmonitored'] . '
'; - echo 'Response Time: ' . $row['responsetimelast7'] . '
'; - echo 'User Rating: ' . $row['userrating'] . '
'; - echo 'Server Location: ' . $row['country'] . '
'; - echo 'Latitude: ' . $row['lat'] . '
'; - echo 'Longitude: ' . $row['long'] . '
'; - } +define('PODUPTIME', microtime(true)); + +// Set up global DB connection. +R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true); +R::testConnection() || die('Error in DB connection'); + +try { + $pod = R::getRow(' + SELECT hgitdate, 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 + WHERE domain = ? + ', [$_domain]); +} catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); +} + +if ($_format === 'json') { + echo json_encode($pod); +} else { + echo 'Status: ' . $pod['status'] . '
'; + echo 'Last Git Pull: ' . $pod['hgitdate'] . '
'; + echo 'Uptime This Month ' . $pod['uptimelast7'] . '
'; + echo 'Months Monitored: ' . $pod['monthsmonitored'] . '
'; + echo 'Response Time: ' . $pod['responsetimelast7'] . '
'; + echo 'User Rating: ' . $pod['userrating'] . '
'; + echo 'Server Location: ' . $pod['country'] . '
'; + echo 'Latitude: ' . $pod['lat'] . '
'; + echo 'Longitude: ' . $pod['long'] . '
'; } diff --git a/db/edit.php b/db/edit.php index 566df85460cfca5d2c171d2669bbd2e488475088..ec454a1fd500465eec948c76d863e9d8e4374679 100644 --- a/db/edit.php +++ b/db/edit.php @@ -1,4 +1,7 @@ = date('Y-m-d H:i:s') || die('token expired'); +try { + $pod = R::findOne('pods', 'domain = ?', [$_domain]); + $pod || die('domain not found'); +} catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); +} - // Delete and exit. - if ('delete' === $_action) { - $sql = 'DELETE FROM pods WHERE domain = $1'; - $result = pg_query_params($dbh, $sql, [$_domain]); - $result || die('Error in SQL query: ' . pg_last_error()); +$pod['token'] === $_token || die('token mismatch'); +$pod['tokenexpire'] >= date('Y-m-d H:i:s') || die('token expired'); - die('pod removed from DB'); - } +// Delete and exit. +if ('delete' === $_action) { + R::trash($pod); + die('pod removed from DB'); +} - // Save and exit - if ('save' === $_action) { - $_weight <= 10 || die('10 is max weight'); +// Save and exit. +if ('save' === $_action) { + $_weight <= 10 || die('10 is max weight'); - $sql = 'UPDATE pods SET email = $1, weight = $2, podmin_statement = $3, podmin_notify = $4 WHERE domain = $5'; - $result = pg_query_params($dbh, $sql, [$_email, $_weight, $_podmin_statement, $_podmin_notify, $_domain]); - $result || die('Error in SQL query: ' . pg_last_error()); + try { + $pod['email'] = $_email; + $pod['weight'] = $_weight; + $pod['podmin_statement'] = $_podmin_statement; + $pod['podmin_notify'] = $_podmin_notify; + R::store($pod); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } - $to = $_email; - $headers = ['From: ' . $adminemail, 'Cc: ' . $row['email'], 'Bcc: ' . $adminemail]; - $subject = 'Edit notice from poduptime'; - $message = 'Data for ' . $_domain . ' updated. If it was not you reply and let me know!'; - @mail($to, $subject, $message, implode("\r\n", $headers)); + $to = $_email; + $headers = ['From: ' . $adminemail, 'Cc: ' . $pod['email'], 'Bcc: ' . $adminemail]; + $subject = 'Edit notice from poduptime'; + $message = 'Data for ' . $_domain . ' updated. If it was not you reply and let me know!'; + @mail($to, $subject, $message, implode("\r\n", $headers)); - die('Data saved. Will go into effect on next hourly change'); - } + die('Data saved. Will go into effect on next hourly change'); +} - // Forms. - ?> - Authorized to edit until
+// Forms. +?> + Authorized to edit until
-
-
-
-
+
+
+
+

@@ -70,5 +82,4 @@ while ($row = pg_fetch_array($result)) { WARNING: This can not be undone, you will need to add your pod again if you want back on list: - 0 || die('domain not found'); - -while ($row = pg_fetch_array($result)) { - // Set up common variables. - $uuid = md5(uniqid($_domain, true)); - $link = sprintf('https://%1$s/db/edit.php?domain=%2$s&token=%3$s', $_SERVER['HTTP_HOST'], $_domain, $uuid); - $headers = ['From: ' . $adminemail]; - $message_lines = []; - - if ($_email) { - $row['email'] === $_email || die('email mismatch'); - - $to = $_email; - $subject = 'Temporary edit key for ' . $_SERVER['HTTP_HOST']; - $headers[] = 'Bcc: ' . $adminemail; - $expire = time() + 2700; - $output = 'Link sent to your email'; - } elseif (!$row['email']) { - echo "domain is registered but no email associated, to add an email use the add a pod feature";die; - } else { - $to = $adminemail; - $subject = 'FORWARD REQUEST: Temporary edit key for ' . $_SERVER['HTTP_HOST']; - $message_lines[] = 'User trying to edit pod without email address.'; - $message_lines[] = 'Email found: ' . $row['email']; - $expire = time() + 9700; - $output = 'Link sent to administrator to review and verify, if approved they will forward the edit key to you.'; - } - - $sql = 'UPDATE pods SET token = $1, tokenexpire = $2 WHERE domain = $3'; - $result = pg_query_params($dbh, $sql, [$uuid, date('Y-m-d H:i:s', $expire), $_domain]); - $result || die('Error in SQL query: ' . pg_last_error()); - - $message_lines[] = 'Link: ' . $link; - $message_lines[] = 'Expires: ' . date('Y-m-d H:i:s T', $expire); - - @mail($to, $subject, implode("\r\n", $message_lines), implode("\r\n", $headers)); - - echo $output; +define('PODUPTIME', microtime(true)); + +// Set up global DB connection. +R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true); +R::testConnection() || die('Error in DB connection'); + +try { + $pod = R::findOne('pods', 'domain = ?', [$_domain]); + $pod || die('domain not found'); +} catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); +} + +// Set up common variables. +$uuid = md5(uniqid($_domain, true)); +$link = sprintf('https://%1$s/db/edit.php?domain=%2$s&token=%3$s', $_SERVER['HTTP_HOST'], $_domain, $uuid); +$headers = ['From: ' . $adminemail]; +$message_lines = []; + +if ($_email) { + $pod['email'] === $_email || die('email mismatch'); + + $to = $_email; + $subject = 'Temporary edit key for ' . $_SERVER['HTTP_HOST']; + $headers[] = 'Bcc: ' . $adminemail; + $expire = time() + 2700; + $output = 'Link sent to your email'; +} elseif (!$pod['email']) { + die('domain is registered but no email associated, to add an email use the add a pod feature'); +} else { + $to = $adminemail; + $subject = 'FORWARD REQUEST: Temporary edit key for ' . $_SERVER['HTTP_HOST']; + $message_lines[] = 'User trying to edit pod without email address.'; + $message_lines[] = 'Email found: ' . $pod['email']; + $expire = time() + 9700; + $output = 'Link sent to administrator to review and verify, if approved they will forward the edit key to you.'; +} + +try { + $pod['token'] = $uuid; + $pod['tokenexpire'] = date('Y-m-d H:i:s', $expire); + R::store($pod); +} catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); } + +$message_lines[] = 'Link: ' . $link; +$message_lines[] = 'Expires: ' . date('Y-m-d H:i:s T', $expire); + +@mail($to, $subject, implode("\r\n", $message_lines), implode("\r\n", $headers)); + +echo $output; diff --git a/db/kill.php b/db/kill.php index d4f7843a84d2688256ddea769b605cc4622dc3b0..7d752094ea51c862a21c68256ad367004f93b2af 100644 --- a/db/kill.php +++ b/db/kill.php @@ -1,30 +1,37 @@ getMessage()); +} + +if ($pod) { + $email = $pod['email']; if ($_action === 'delete') { - $sql = 'DELETE FROM pods WHERE domain = $1'; - $res_delete = pg_query_params($dbh, $sql, [$_domain]); - $res_delete || die('two Error in SQL query: ' . pg_last_error()); + R::trash('pods', $pod['id']); if ($email) { $to = $email; @@ -42,6 +49,4 @@ while ($row = pg_fetch_array($result)) { @mail($to, $subject, $message, implode("\r\n", $headers)); } } - - echo $result; } diff --git a/db/migration00002.sql b/db/migration00002.sql new file mode 100644 index 0000000000000000000000000000000000000000..708830826763d702531012f346ff6ce1f8a641b4 --- /dev/null +++ b/db/migration00002.sql @@ -0,0 +1,20 @@ +/* http://pointbeing.net/weblog/2008/03/mysql-versus-postgresql-adding-an-auto-increment-column-to-a-table.html */ +CREATE SEQUENCE apikeys_id_seq; +ALTER TABLE apikeys ADD id serial8 UNIQUE PRIMARY KEY; +ALTER TABLE apikeys ALTER COLUMN id SET DEFAULT NEXTVAL('apikeys_id_seq'); +UPDATE apikeys SET id = NEXTVAL('apikeys_id_seq'); + +CREATE SEQUENCE checks_id_seq; +ALTER TABLE checks ADD id serial8 UNIQUE PRIMARY KEY; +ALTER TABLE checks ALTER COLUMN id SET DEFAULT NEXTVAL('checks_id_seq'); +UPDATE checks SET id = NEXTVAL('checks_id_seq'); + +CREATE SEQUENCE clicks_id_seq; +ALTER TABLE clicks ADD id serial8 UNIQUE PRIMARY KEY; +ALTER TABLE clicks ALTER COLUMN id SET DEFAULT NEXTVAL('clicks_id_seq'); +UPDATE clicks SET id = NEXTVAL('clicks_id_seq'); + +CREATE SEQUENCE masterversions_id_seq; +ALTER TABLE masterversions ADD id serial8 UNIQUE PRIMARY KEY; +ALTER TABLE masterversions ALTER COLUMN id SET DEFAULT NEXTVAL('masterversions_id_seq'); +UPDATE masterversions SET id = NEXTVAL('masterversions_id_seq'); diff --git a/db/pull-masterversions.php b/db/pull-masterversions.php index 40534f0bd7669076db15b05c9f66f01d16eb123f..e8181b3372ac8b4dd023aa9f0c56b829e5fb7525 100644 --- a/db/pull-masterversions.php +++ b/db/pull-masterversions.php @@ -1,15 +1,22 @@ ['url' => 'https://raw.githubusercontent.com/diaspora/diaspora/master/config/defaults.yml', 'regex' => '/number:.*"(.*)"/'], - 'friendica' => ['url' => 'https://raw.githubusercontent.com/friendica/friendica/master/boot.php', 'regex' => '/define.*\'FRIENDICA_VERSION\'.*\'(.*)\'/'], - 'redmatrix' => ['url' => 'https://raw.githubusercontent.com/redmatrix/hubzilla/master/boot.php', 'regex' => '/define.*\'STD_VERSION\'.*\'(.*)\'/'], - 'socialhome' => ['url' => 'https://raw.githubusercontent.com/jaywink/socialhome/master/bower.json', 'regex' => '/version":.*"(.*)"/'], + 'diaspora' => ['url' => 'https://raw.githubusercontent.com/diaspora/diaspora/master/config/defaults.yml', 'regex' => '/number:.*"(.*)"/'], + 'friendica' => ['url' => 'https://raw.githubusercontent.com/friendica/friendica/master/boot.php', 'regex' => '/define.*\'FRIENDICA_VERSION\'.*\'(.*)\'/'], + 'redmatrix' => ['url' => 'https://raw.githubusercontent.com/redmatrix/hubzilla/master/boot.php', 'regex' => '/define.*\'STD_VERSION\'.*\'(.*)\'/'], + 'socialhome' => ['url' => 'https://raw.githubusercontent.com/jaywink/socialhome/master/bower.json', 'regex' => '/version":.*"(.*)"/'], 'social-relay' => ['url' => 'https://raw.githubusercontent.com/jaywink/social-relay/master/social_relay/config.py', 'regex' => '/VERSION.*"(.*)"/'], ]; @@ -22,9 +29,14 @@ foreach ($softwares as $software => $details) { curl_close($mv); if ($masterversion = preg_match($details['regex'], $outputmv, $version) ? $version[1] : '') { - $sql = 'INSERT INTO masterversions (software,version) VALUES ($1,$2)'; - $result = pg_query_params($dbh, $sql, [$software, $masterversion]); - $result || die('Error in SQL query: ' . pg_last_error()); + try { + $m = R::dispense('masterversions'); + $m['software'] = $software; + $m['version'] = $masterversion; + R::store($m); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } } printf('%s:%s ', $software, $masterversion ?: 'n/a'); diff --git a/db/pull.php b/db/pull.php index 18ddeff5a3aa71493ae5ae2a1e3130c0d20880a7..aa1b7253f282cba35f52688fe979d4ffd8aefeb8 100644 --- a/db/pull.php +++ b/db/pull.php @@ -1,46 +1,67 @@ '; -$_domain = $_GET['domain'] ?? ''; +$_domain = $_GET['domain'] ?? null; + +// Must have a domain, except if called from CLI. +$_domain || PHP_SAPI === 'cli' || die('No valid input'); -require_once __DIR__ . '/../config.php'; require_once __DIR__ . '/../vendor/autoload.php'; +require_once __DIR__ . '/../config.php'; + +define('PODUPTIME', microtime(true)); -$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass"); -$dbh || die('Error in connection: ' . pg_last_error()); - -if ($_domain) { - $sql = 'SELECT domain,score,date_created,adminrating,weight,hidden,podmin_notify,email FROM pods WHERE domain = $1'; - $result = pg_query_params($dbh, $sql, [$_domain]); -} elseif (PHP_SAPI === 'cli') { - $sql = 'SELECT domain,score,date_created,adminrating,weight,hidden,podmin_notify,email FROM pods'; - $result = pg_query($dbh, $sql); -} else { - die('No valid input'); +// Set up global DB connection. +R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true); +R::testConnection() || die('Error in DB connection'); + +try { + $sql = ' + SELECT domain, score, date_created, adminrating, weight, hidden, podmin_notify, email + FROM pods + '; + + $pods = []; + if ($_domain) { + $sql .= ' WHERE domain = ?'; + $pods = R::getAll($sql, [$_domain]); + } elseif (PHP_SAPI === 'cli') { + $pods = R::getAll($sql); + } +} catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); } -$result || die('Error in SQL query1: ' . pg_last_error()); - -while ($row = pg_fetch_assoc($result)) { - $domain = $row['domain']; - $score = (int) $row['score']; - $dateadded = $row['date_created']; - $admindb = (int) $row['adminrating']; - $weight = $row['weight']; - $hiddennow = $row['hidden']; - $email = $row['email']; - $notify = $row['podmin_notify']; - $sqlforr = 'SELECT admin,rating FROM rating_comments WHERE domain = $1'; - $ratings = pg_query_params($dbh, $sqlforr, [$domain]); - $ratings || die('Error in SQL query2: ' . pg_last_error()); + +foreach ($pods as $pod) { + $domain = $pod['domain']; + $score = (int) $pod['score']; + $dateadded = $pod['date_created']; + $admindb = (int) $pod['adminrating']; + $weight = $pod['weight']; + $hiddennow = $pod['hidden']; + $email = $pod['email']; + $notify = $pod['podmin_notify']; + + try { + $ratings = R::getAll(' + SELECT admin, rating + FROM rating_comments + WHERE domain = ? + ', [$domain]); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } _debug('Domain', $domain); $user_ratings = []; $admin_ratings = []; - while ($rating = pg_fetch_assoc($ratings)) { + foreach ($ratings as $rating) { if ($rating['admin'] == 0) { $user_ratings[] = $rating['rating']; } elseif ($rating['admin'] == 1) { @@ -76,7 +97,7 @@ while ($row = pg_fetch_assoc($result)) { _debug('Conntime', $conntime); _debug('NStime', $nstime); _debug('Latency', $latency); - + $jsonssl = json_decode($outputssl); $xdver = $jsonssl->software->version ?? 0; @@ -97,29 +118,49 @@ while ($row = pg_fetch_assoc($result)) { $service_wordpress = false; if (json_last_error() === 0) { (!$jsonssl->software->version) || $score += 1; - $service_facebook = in_array('facebook', $jsonssl->services->outbound, true); - $service_twitter = in_array('twitter', $jsonssl->services->outbound, true); - $service_tumblr = in_array('tumblr', $jsonssl->services->outbound, true); - $service_wordpress = in_array('wordpress', $jsonssl->services->outbound, true); + $service_facebook = in_array('facebook', $jsonssl->services->outbound, true); + $service_twitter = in_array('twitter', $jsonssl->services->outbound, true); + $service_tumblr = in_array('tumblr', $jsonssl->services->outbound, true); + $service_wordpress = in_array('wordpress', $jsonssl->services->outbound, true); } - + if ($jsonssl !== null) { - $status = 'Up'; - $sql_checks = 'INSERT INTO checks (domain, online, latency, total_users, local_posts, comment_counts, shortversion) VALUES ($1, $2, $3, $4, $5, $6, $7)'; - $result_checks = pg_query_params($dbh, $sql_checks, [$domain, 1, $latency, $total_users, $local_posts, $comment_counts, $shortversion]); - $result_checks || die('Error in SQL query: ' . pg_last_error()); + $status = 'Up'; + + try { + $c = R::dispense('checks'); + $c['domain'] = $domain; + $c['online'] = true; + $c['latency'] = $latency; + $c['total_users'] = $total_users; + $c['local_posts'] = $local_posts; + $c['comment_counts'] = $comment_counts; + $c['shortversion'] = $shortversion; + R::store($c); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } } - - if (!$jsonssl) { + + if (!$jsonssl) { _debug('Connection', 'Can not connect to pod'); - $sql_errors = 'INSERT INTO checks (domain, online, error, latency) VALUES ($1, $2, $3, $4)'; - $result_errors = pg_query_params($dbh, $sql_errors, [$domain, 0, $outputsslerror, $latency]); - $result_errors || die('Error in SQL query: ' . pg_last_error()); - $score -= 1; - $shortversion = '0.error'; - $status = 'Down'; - } - + + try { + $c = R::dispense('checks'); + $c['domain'] = $domain; + $c['online'] = false; + $c['error'] = $outputsslerror; + $c['latency'] = $latency; + R::store($c); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } + + $score -= 1; + $shortversion = '0.error'; + $status = 'Down'; + } + _debug('Version code', $shortversion); _debug('Signup Open', $signup); @@ -156,44 +197,48 @@ while ($row = pg_fetch_assoc($result)) { $state = !empty($location['region']) ? iconv('UTF-8', 'UTF-8//IGNORE', $location['region']) : null; $lat = !empty($location['latitude']) ? $location['latitude'] : 0; $long = !empty($location['longitude']) ? $location['longitude'] : 0; - + echo $newline; $statslastdate = date('Y-m-d H:i:s'); - $diff = (new DateTime())->diff(new DateTime($dateadded)); - $months = $diff->m + ($diff->y * 12); - - $avglatency = 0; - $sqllatency = 'SELECT round(avg(latency) * 1000) AS latency FROM checks WHERE domain = $1'; - $resultlatency = pg_query_params($dbh, $sqllatency, [$domain]); - $resultlatency || die('Error in SQL query resultchecks: ' . pg_last_error()); - $avglatency = pg_fetch_result($resultlatency, 0); - - $uptime = 0; - $sqlonline = 'SELECT avg(online::int) * 100 AS online FROM checks WHERE domain = $1'; - $resultonline = pg_query_params($dbh, $sqlonline, [$domain]); - $resultonline || die('Error in SQL query resultchecks: ' . pg_last_error()); - $uptime = round(pg_fetch_result($resultonline, 0),2); - + $diff = (new DateTime())->diff(new DateTime($dateadded)); + $months = $diff->m + ($diff->y * 12); + + try { + $checks = R::getRow(' + SELECT + round(avg(latency) * 1000) AS latency, + round(avg(online::INT) * 100, 2) AS online + FROM checks + WHERE domain = ? + ', [$_domain]); + + $avglatency = $checks['latency'] ?? 0; + $uptime = $checks['online'] ?? 0; + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } + _debug('Uptime', $uptime); - $sqlmasters = 'SELECT version FROM masterversions WHERE software = $1 ORDER BY date_checked LIMIT 1'; - $resultmasters = pg_query_params($dbh, $sqlmasters, [$softwarename]); - $resultmasters || die('Error in SQL query: ' . pg_last_error()); - $masterversion = pg_fetch_result($resultmasters, 0); + try { + $masterversion = R::getCell('SELECT version FROM masterversions WHERE software = ?', [$softwarename]); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } _debug('Masterversion', $masterversion); - + $hidden = $score <= 70; _debug('Hidden', $hidden ? 'yes' : 'no'); - if ($hiddennow === 'f' && $hidden && $notify === 't') { + if (!$hiddennow && $hidden && $notify) { $to = $email; $headers = ['From: ' . $adminemail, 'Bcc: ' . $adminemail]; $subject = 'Monitoring notice from poduptime'; $message = 'Notice for ' . $domain . '. Your score fell to ' . $score . ' and your pod is now marked as hidden.'; @mail($to, $subject, $message, implode("\r\n", $headers)); - _debug('Mail Notice', 'sent to '.$email); + _debug('Mail Notice', 'sent to ' . $email); } if ($score > 100) { $score = 100; @@ -203,40 +248,85 @@ while ($row = pg_fetch_assoc($result)) { $weightedscore = ($uptime + $score - (10 - $weight)) / 2; _debug('Weighted Score', $weightedscore); - $timenow = date('Y-m-d H:i:s'); - $sql_set = 'UPDATE pods SET secure = $2, hidden = $3, ip = $4, ipv6 = $5, monthsmonitored = $6, uptime_alltime = $7, status = $8, date_laststats = $9, date_updated = $10, latency = $11, score = $12, adminrating = $13, country = $14, city = $15, state = $16, lat = $17, long = $18, userrating = $19, shortversion = $20, masterversion = $21, signup = $22, total_users = $23, active_users_halfyear = $24, active_users_monthly = $25, local_posts = $26, name = $27, comment_counts = $28, service_facebook = $29, service_tumblr = $30, service_twitter = $31, service_wordpress = $32, weightedscore = $33, service_xmpp = $34, softwarename = $35, sslvalid = $36, dnssec = $37, sslexpire = $38 WHERE domain = $1'; - $result_set = pg_query_params($dbh, $sql_set, [$domain, 1, (int) $hidden, $ip, (int) ($ipv6 !== null), $months, $uptime, $status, $statslastdate, $timenow, $avglatency, $score, $admin_rating, $country, $city, $state, $lat, $long, $user_rating, $shortversion, $masterversion, (int) $signup, $total_users, $active_users_halfyear, $active_users_monthly, $local_posts, $name, $comment_counts, (int) $service_facebook, (int) $service_tumblr, (int) $service_twitter, (int) $service_wordpress, $weightedscore, (int) $service_xmpp, $softwarename, $outputsslerror, (int) $dnssec, $sslexpire]); - $result_set || die('Error in SQL query3: ' . pg_last_error()); + try { + $p = R::findOne('pods', 'domain = ?', [$domain]); + $p['secure'] = true; + $p['hidden'] = $hidden; + $p['ip'] = $ip; + $p['ipv6'] = ($ipv6 !== null); + $p['monthsmonitored'] = $months; + $p['uptime_alltime'] = $uptime; + $p['status'] = $status; + $p['date_laststats'] = $statslastdate; + $p['date_updated'] = date('Y-m-d H:i:s'); + $p['latency'] = $avglatency; + $p['score'] = $score; + $p['adminrating'] = $admin_rating; + $p['country'] = $country; + $p['city'] = $city; + $p['state'] = $state; + $p['lat'] = $lat; + $p['long'] = $long; + $p['userrating'] = $user_rating; + $p['shortversion'] = $shortversion; + $p['masterversion'] = $masterversion; + $p['signup'] = $signup; + $p['total_users'] = $total_users; + $p['active_users_halfyear'] = $active_users_halfyear; + $p['active_users_monthly'] = $active_users_monthly; + $p['local_posts'] = $local_posts; + $p['name'] = $name; + $p['comment_counts'] = $comment_counts; + $p['service_facebook'] = $service_facebook; + $p['service_tumblr'] = $service_tumblr; + $p['service_twitter'] = $service_twitter; + $p['service_wordpress'] = $service_wordpress; + $p['service_xmpp'] = $service_xmpp; + $p['weightedscore'] = $weightedscore; + $p['softwarename'] = $softwarename; + $p['sslvalid'] = $outputsslerror; + $p['dnssec'] = $dnssec; + $p['sslexpire'] = $sslexpire; + + // This is a silly fix which is necessary due to: + // https://github.com/gabordemooij/redbean/issues/547 + $p['podmin_notify'] = $p['podmin_notify']; + + R::store($p); + } catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); + } _debug('Score out of 100', $score); - echo 'Success '.$domain; - + echo 'Success ' . $domain; + echo $newline; echo $newline; } /** * Output a debug message and variable value - * + * * @param string $label * @param mixed $var * @param bool $dump */ -function _debug($label, $var = null, $dump = false) { +function _debug($label, $var = null, $dump = false) +{ global $debug, $newline; if (!$debug) { return; } - + if ($dump || is_array($var)) { - $output = print_r($var, true); + $output = print_r($var, true); } elseif (is_bool($var)) { $output = $var ? 'true' : 'false'; } else { $output = (string) $var; } - + printf('%s: %s%s', $label, $output, $newline); } diff --git a/db/saverating.php b/db/saverating.php index b498f52f2ff20320fa10377bee5076f24aa16625..785f33f5acb2b4feb73769f6154aa687e32b59af 100644 --- a/db/saverating.php +++ b/db/saverating.php @@ -1,5 +1,7 @@ getMessage()); +} $to = $adminemail; $headers = ['From: ' . $_email]; diff --git a/db/tables.sql b/db/tables.sql index 45ef1b3b6fea71f03c2fba88a3f1795cc79461b6..fbc84387f7d940fa2c3296464d43d1b3a7bf10ad 100644 --- a/db/tables.sql +++ b/db/tables.sql @@ -59,6 +59,7 @@ CREATE TABLE rating_comments ( date_created timestamp DEFAULT current_timestamp ); CREATE TABLE apikeys ( + id serial8 UNIQUE PRIMARY KEY, key text, email text, usage int, @@ -66,6 +67,7 @@ CREATE TABLE apikeys ( ); CREATE TABLE clicks ( + id serial8 UNIQUE PRIMARY KEY, domain text, manualclick int, autoclick int, @@ -73,6 +75,7 @@ CREATE TABLE clicks ( ); CREATE TABLE checks ( + id serial8 UNIQUE PRIMARY KEY, domain text, online boolean, error text, @@ -85,8 +88,8 @@ CREATE TABLE checks ( ); CREATE TABLE masterversions ( + id serial8 UNIQUE PRIMARY KEY, software text, version text, date_checked timestamp DEFAULT current_timestamp ); - diff --git a/go.php b/go.php index cedac23bf73b8190458eeb5cd95bf91925b708f7..95c7b69ce97b4220d69047d6884640bd0b79f7c0 100644 --- a/go.php +++ b/go.php @@ -1,36 +1,44 @@ 90 AND masterversion = shortversion AND signup ORDER BY RANDOM() LIMIT 1'; - $result = pg_query($dbh, $sql); - $result || die('Error in SQL query: ' . pg_last_error()); - - $row = pg_fetch_all($result); - - $sql = 'INSERT INTO clicks (domain, autoclick) VALUES ($1, $2)'; - $result = pg_query_params($dbh, $sql, [$row[0]['domain'], '1']); - $result || die('Error in SQL query: ' . pg_last_error()); - - header('Location: https://' . $row[0]['domain']); +define('PODUPTIME', microtime(true)); + +// Set up global DB connection. +R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true); +R::testConnection() || die('Error in DB connection'); + +try { + if ($_domain) { + $click = 'manualclick'; + $domain = R::getCell('SELECT domain FROM pods WHERE domain LIKE ?', [$_domain]); + $domain || die('unknown domain'); + } else { + $click = 'autoclick'; + $domain = R::getCell(' + SELECT domain + FROM pods + WHERE signup + AND score > 90 + AND pods.masterversion = shortversion + ORDER BY random() + LIMIT 1 + '); + $domain || die('no domains exist'); + } + + $c = R::dispense('clicks'); + $c['domain'] = $domain; + $c[$click] = 1; + R::store($c); + + header('Location: https://' . $domain); +} catch (\RedBeanPHP\RedException $e) { + die('Error in SQL query: ' . $e->getMessage()); } diff --git a/index.php b/index.php index afbddb9d922172b079a2cb221faab4a8e18e8a11..e4e58bc0137f7f064c834ba3af9a126b17a20e77 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,19 @@ getMessage()); +} ?> - + +
getMessage()); + } - $numrows = pg_num_rows($result); echo '

Podupti.me ratings for ' . $_domain . ' pod


'; - if (!$numrows) { + if (empty($ratings)) { echo 'This pod has no rating yet!'; - } - while ($row = pg_fetch_array($result)) { - if ($row['admin'] === '1') { - echo 'Poduptime Approved Comment - User: ' . $row['username'] . ' Url: ' . $row['userurl'] . ' Rating: ' . $row['rating'] . '
'; - echo '' . $row['comment'] . '' . $row['date_created'] . '
'; - } else { - echo 'User Comment - User: ' . $row['username'] . ' Url: ' . $row['userurl'] . ' Rating: ' . $row['rating'] . '
'; - echo '' . $row['comment'] . '' . $row['date_created'] . '
'; + } else { + foreach ($ratings as $rating) { + if ($rating['admin'] === '1') { + echo 'Poduptime Approved Comment - User: ' . $rating['username'] . ' Url: ' . $rating['userurl'] . ' Rating: ' . $rating['rating'] . '
'; + echo '' . $rating['comment'] . '' . $rating['date_created'] . '
'; + } else { + echo 'User Comment - User: ' . $rating['username'] . ' Url: ' . $rating['userurl'] . ' Rating: ' . $rating['rating'] . '
'; + echo '' . $rating['comment'] . '' . $rating['date_created'] . '
'; + } } } ?> diff --git a/show.php b/show.php index dc676a9d0d51cfbf151acff7a9bd7dfd08682938..3c6e7021480d1187fefb53b92c58602e963c9d87 100644 --- a/show.php +++ b/show.php @@ -1,22 +1,24 @@ getMessage()); +} -$sql = "SELECT domain,masterversion,shortversion,softwarename,monthsmonitored,podmin_statement,score,signup,name,country,city,state,uptime_alltime,active_users_halfyear,active_users_monthly,service_facebook,service_twitter,service_tumblr,service_wordpress,service_xmpp FROM pods WHERE NOT hidden AND signup ORDER BY weightedscore DESC"; - -$result = pg_query($dbh, $sql); -$result || die('Error in SQL query: ' . pg_last_error()); - -$numrows = pg_num_rows($result); ?> - +
Scroll right or rotate device for more
@@ -33,35 +35,35 @@ $numrows = pg_num_rows($result); '; + echo ''; - echo ''; - if ($row['active_users_halfyear'] > 0) { - echo ''; + echo ''; + if ($pod['active_users_halfyear'] > 0) { + echo ''; } else { echo ''; } - if ($country_code === $row['country']) { - echo ''; + if ($country_code === $pod['country']) { + echo ''; } else { - echo ''; + echo ''; } echo ''; - echo ''; + $pod['service_facebook'] && print ''; + $pod['service_twitter'] && print ''; + $pod['service_tumblr'] && print ''; + $pod['service_wordpress'] && print ''; + $pod['service_xmpp'] && print ''; + echo ''; } ?> diff --git a/showfull.php b/showfull.php index 5b3feaa62c71309a14ccdc327e50ff7dfc66ce42..7b3c9a8cecebda1d76cfda3bbcc20a786afaf3a1 100644 --- a/showfull.php +++ b/showfull.php @@ -1,22 +1,21 @@ getMessage()); +} ?> - +
' . $row['uptime_alltime'] . '%' . $row['active_users_halfyear'] . '' . $pod['uptime_alltime'] . '%' . $pod['active_users_halfyear'] . '' . $row['country'] . '' . $pod['country'] . '' . $row['country'] . '' . $pod['country'] . ''; - $row['service_facebook'] === 't' && print ''; - $row['service_twitter'] === 't' && print ''; - $row['service_tumblr'] === 't' && print ''; - $row['service_wordpress'] === 't' && print ''; - $row['service_xmpp'] === 't' && print '' . ($row['podmin_statement'] ? '💬' : ' ') . '
' . ($pod['podmin_statement'] ? '💬' : ' ') . '
@@ -45,59 +44,58 @@ $numrows = pg_num_rows($result); '; + echo ''; - if ($row['shortversion'] > $row['masterversion']) { - $version = $row['shortversion']; - $pre = 'This pod runs pre release development code'; - } elseif (!$row['shortversion']) { + if ($pod['shortversion'] > $pod['masterversion']) { + $version = $pod['shortversion']; + $pre = 'This pod runs pre release development code'; + } elseif (!$pod['shortversion']) { $version = ''; - $pre = 'This pod runs unknown code'; + $pre = 'This pod runs unknown code'; } else { - $version = $row['shortversion']; - $pre = 'This pod runs production code'; + $version = $pod['shortversion']; + $pre = 'This pod runs production code'; } - if (version_compare($row['shortversion'], $row['masterversion'], '=')) { + if (version_compare($pod['shortversion'], $pod['masterversion'], '=')) { $classver = 'text-success'; - } elseif (version_compare($row['shortversion'], $row['masterversion'], '<')) { + } elseif (version_compare($pod['shortversion'], $pod['masterversion'], '<')) { $classver = 'text-warning'; } else { $classver = 'black'; } - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - echo ''; - if ($country_code === $row['country']) { - echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + if ($country_code === $pod['country']) { + echo ''; } else { - echo ''; + echo ''; } echo ''; - - echo ''; + + echo ''; } ?> diff --git a/showmap.php b/showmap.php index 3f3e3276a4745621fedc9334e4dcabc24f5d692e..6ad73ca60178be1849cf17f45f12f34f557f4f77 100644 --- a/showmap.php +++ b/showmap.php @@ -1,7 +1,20 @@ getMessage()); +} $csv = array_map('str_getcsv', file('db/country_latlon.csv')); foreach ($csv as $cords) { @@ -23,40 +36,31 @@ foreach ($csv as $cords) { 'type': 'FeatureCollection', 'features': [ 0 && print ','; $feat = ''; - $row['service_facebook'] === 't' && $feat .= ''; - $row['service_twitter'] === 't' && $feat .= ''; - $row['service_tumblr'] === 't' && $feat .= ''; - $row['service_wordpress'] === 't' && $feat .= ''; - $row['service_xmpp'] === 't' && $feat .= ''; + $pod['service_facebook'] && $feat .= ''; + $pod['service_twitter'] && $feat .= ''; + $pod['service_tumblr'] && $feat .= ''; + $pod['service_wordpress'] && $feat .= ''; + $pod['service_xmpp'] && $feat .= ''; - $pod_name = htmlentities($row['name'], ENT_QUOTES); - $signup = $row['signup'] === 't' ? 'yes' : 'no'; + $pod_name = htmlentities($pod['name'], ENT_QUOTES); + $signup = $pod['signup'] ? 'yes' : 'no'; echo <<{$pod_name}
Software: {$row['softwarename']}
Open Signup: {$signup}
Users: {$row['active_users_halfyear']}
Uptime: {$row['uptime_alltime']}%
Services:{$feat}' + 'html': '{$pod_name}
Software: {$pod['softwarename']}
Open Signup: {$signup}
Users: {$pod['active_users_halfyear']}
Uptime: {$pod['uptime_alltime']}%
Services:{$feat}' }, 'geometry': { 'type': 'Point', - 'coordinates': [{$row['long']},{$row['lat']}] + 'coordinates': [{$pod['long']},{$pod['lat']}] } } EOF; diff --git a/statsviewjs.php b/statsviewjs.php index 48a7b8cb5005bc4b5ceb3b511d595e994aafba82..17188c5966fef0b23e9e879f44397177ab64457f 100644 --- a/statsviewjs.php +++ b/statsviewjs.php @@ -1,18 +1,28 @@ getMessage()); +} -$sql_totals = 'SELECT softwarename, count(*) AS pods, sum(total_users) AS users, round(avg(uptime_alltime),2) AS uptime FROM pods GROUP BY softwarename'; -$result_totals = pg_query($dbh, $sql_totals); -$result_totals || die('Error in SQL query: ' . pg_last_error()); -$totals = pg_fetch_all($result_totals); ?>
' . $row['domain'] . ' 🔒
' . $pod['domain'] . ' 🔒
' . $version . '
' . $row['softwarename'] . '' . ($row['uptime_alltime'] > 0 ? $row['uptime_alltime'].'%' : '') . '' . ($row['ipv6'] === 't' ? '✓' : '') . '' . ($row['latency'] > 0 ? $row['latency'] : '') . '' . ($row['signup'] === 't' ? '✓' : '') . '' . ($row['total_users'] > 0 ? $row['total_users'] : '') . '' . ($row['active_users_halfyear'] > 0 ? $row['active_users_halfyear'] : '') . '' . ($row['active_users_monthly'] > 0 ? $row['active_users_monthly'] : '') . '' . ($row['local_posts'] > 0 ? $row['local_posts'] : '') . '' . ($row['comment_counts'] > 0 ? $row['comment_counts'] : '') . '
' . $row['monthsmonitored'] . '
' . $row['userrating']; - echo '' . $row['score'] . '' . ($row['dnssec'] === 't' ? '✓' : '') . '' . $row['country'] . '
' . $version . '
' . $pod['softwarename'] . '' . ($pod['uptime_alltime'] > 0 ? $pod['uptime_alltime'] . '%' : '') . '' . ($pod['ipv6'] ? '✓' : '') . '' . ($pod['latency'] > 0 ? $pod['latency'] : '') . '' . ($pod['signup'] ? '✓' : '') . '' . ($pod['total_users'] > 0 ? $pod['total_users'] : '') . '' . ($pod['active_users_halfyear'] > 0 ? $pod['active_users_halfyear'] : '') . '' . ($pod['active_users_monthly'] > 0 ? $pod['active_users_monthly'] : '') . '' . ($pod['local_posts'] > 0 ? $pod['local_posts'] : '') . '' . ($pod['comment_counts'] > 0 ? $pod['comment_counts'] : '') . '
' . $pod['monthsmonitored'] . '
' . $pod['userrating'] . '' . $pod['score'] . '' . ($pod['dnssec'] ? '✓' : '') . '' . $pod['country'] . '' . $row['country'] . '' . $pod['country'] . ''; - $row['service_facebook'] === 't' && print ''; - $row['service_twitter'] === 't' && print ''; - $row['service_tumblr'] === 't' && print ''; - $row['service_wordpress'] === 't' && print ''; - $row['service_xmpp'] === 't' && print ''; + $pod['service_facebook'] && print ''; + $pod['service_twitter'] && print ''; + $pod['service_tumblr'] && print ''; + $pod['service_wordpress'] && print ''; + $pod['service_xmpp'] && print ''; echo '' . ($row['podmin_statement'] ? '💬' : ' ') . '
' . ($pod['podmin_statement'] ? '💬' : ' ') . '