You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
5.0 KiB
141 lines
5.0 KiB
<?php |
|
|
|
/** |
|
* Ashita Private Server Collection API - Copyright (c) 2018 atom0s [[email protected]] |
|
* |
|
* This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. |
|
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/ or send a letter to |
|
* Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
|
* |
|
* By using this script, you agree to the above license and its terms. |
|
* |
|
* Attribution - You must give appropriate credit, provide a link to the license and indicate if changes were |
|
* made. You must do so in any reasonable manner, but not in any way that suggests the licensor |
|
* endorses you or your use. |
|
* |
|
* Non-Commercial - You may not use the material (this script) for commercial purposes. |
|
* |
|
* No-Derivatives - If you remix, transform, or build upon the material (this script), you may not distribute the |
|
* modified material. You are, however, allowed to submit the modified works back to the original |
|
* Ashita Private Server Collection API project in attempt to have it added to the original project. |
|
* |
|
* You may not apply legal terms or technological measures that legally restrict others |
|
* from doing anything the license permits. |
|
* |
|
* You may contact me, atom0s, at [email protected] for more information or if you are seeking commercial use. |
|
* |
|
* No warranties are given. |
|
*/ |
|
|
|
// Define the script globals.. |
|
define('ASHITA_PSCAPI', true); |
|
define('ASHITA_PSCAPI_VERSION', '1'); |
|
|
|
// Prepare needed variables and includes.. |
|
$ashita_root_path = './'; |
|
$phpEx = substr(strrchr(__FILE__, '.'), 1); |
|
require($ashita_root_path . 'config.' . $phpEx); |
|
require($ashita_root_path . 'ashita.functions.' . $phpEx); |
|
$cacheFile = $ashita_root_path . 'cache.json'; |
|
|
|
/** |
|
* Generates a timestamp used to determine the time the script was ran to compare |
|
* against the current cache data. Offset is based on Los Angeles so that all servers |
|
* cached information updates are on the same time schedule. |
|
*/ |
|
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); |
|
$timestamp = $date->getTimestamp(); |
|
|
|
// Ensure the configuration file is valid.. |
|
if (!isValidConfiguration($config)) |
|
{ |
|
echo generateErrorResponse(0x80000001, "Invalid configuration file detected."); |
|
exit; |
|
} |
|
|
|
// Attempt to get the cached data.. |
|
$data = getCachedData($cacheFile); |
|
if ($data !== false) |
|
{ |
|
// Check if the cached data has expired.. |
|
if (json_decode($data)->timestamp + $config['cache']['lifetime'] > $timestamp) |
|
{ |
|
// Erase configurations from memory.. |
|
unset($config); |
|
|
|
// Data is still valid, use it.. |
|
echo $data; |
|
exit; |
|
} |
|
} |
|
|
|
/** |
|
* No cached data present or it is invalid or expired. Instead, we must query the |
|
* database for the needed information to populate the cache with new data. |
|
*/ |
|
try |
|
{ |
|
// Connect to the database.. |
|
$db = new PDO("mysql:host={$config['db']['host']};port={$config['db']['port']};dbname={$config['db']['name']}", $config['db']['user'], $config['db']['pass']); |
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
|
|
// Empty the database configurations.. |
|
unset($config['db']); |
|
|
|
// Query the database for the current online counts.. |
|
$sql = $db->query("SELECT COUNT(DISTINCT(client_addr)) AS u, COUNT(*) AS t FROM accounts_sessions;")->fetchall(); |
|
if (!is_array($sql) || count($sql) !== 1) |
|
{ |
|
// Invalid results, return an error.. |
|
echo generateErrorResponse(0x80000002, 'Failed to query database for count information.'); |
|
exit; |
|
} |
|
|
|
// Build the new server information array.. |
|
$data = array( |
|
'status' => 0, |
|
'version' => ASHITA_PSCAPI_VERSION, |
|
'timestamp' => $timestamp, |
|
'lifetime' => $config['server']['lifetime'], |
|
'name' => $config['server']['name'], |
|
'levelcap' => $config['server']['levelcap'], |
|
'ip' => $config['server']['ip'], |
|
'website' => $config['server']['website'], |
|
'discord' => $config['server']['discord'], |
|
'total' => $sql[0]['t'], |
|
'unique' => $sql[0]['u'] |
|
); |
|
|
|
try |
|
{ |
|
// Open the cache file for writing.. |
|
$f = fopen($cacheFile, 'w'); |
|
if ($f === FALSE) |
|
{ |
|
echo generateErrorResponse(0x80000003, 'Failed to update cache file. (1)'); |
|
exit; |
|
} |
|
|
|
// Write the data to the cache file.. |
|
fwrite($f, json_encode($data)); |
|
fclose($f); |
|
} |
|
catch (Exception $e) |
|
{ |
|
echo generateErrorResponse(0x80000003, 'Failed to update cache file. (2)'); |
|
exit; |
|
} |
|
|
|
// Cleanup the pdo objects.. |
|
$sql = null; |
|
$db = null; |
|
|
|
// Echo the new data.. |
|
echo json_encode($data); |
|
exit; |
|
} |
|
catch (Exception $e) |
|
{ |
|
echo generateErrorResponse(0x80000004, 'Failed to get server information.'); |
|
exit; |
|
} |