The basic php script used for private servers to submit information about their server to the Ashita backend services. (Used to display private server information within the Ashita launcher.)
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.

159 lines
6.3 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.
*/
// Prevent hack attempts..
if (!defined('ASHITA_PSCAPI')) { exit; }
/**
* Generates a JSON response object.
*
* @param {number} $errorc - The error code of the response.
* @param {string} $errorm - The error message of the response.
* @return {$string} A JSON formatted object of the error information.
*/
function generateErrorResponse($errorc, $errorm)
{
$err = "{ ";
$err .= "\"status\": 1,";
$err .= "\"errorc\": " . $errorc;
$err .= "\"errorm\": " . $errorm;
$err .= "}";
return $err;
}
/**
* Checks if the given configuration object is valid.
*
* @param {object} $config - The configuration object to validate.
* @return {bool} True on success, false otherwise.
*/
function isValidConfiguration($config)
{
// Ensure the configuration arrays are valid and exist..
if (!is_array($config) ||
!array_key_exists('db', $config) || !is_array($config['db']) ||
!array_key_exists('cache', $config) || !is_array($config['cache']) ||
!array_key_exists('server', $config) || !is_array($config['server']))
{
return false;
}
// Validate the database configurations..
if (!array_key_exists('host', $config['db']) || !is_string($config['db']['host']) ||
!array_key_exists('port', $config['db']) || !is_string($config['db']['port']) ||
!array_key_exists('user', $config['db']) || !is_string($config['db']['user']) ||
!array_key_exists('pass', $config['db']) || !is_string($config['db']['pass']) ||
!array_key_exists('name', $config['db']) || !is_string($config['db']['name']))
{
return false;
}
// Validate the cache configurations..
if (!array_key_exists('lifetime', $config['cache']) || !is_numeric($config['cache']['lifetime']))
{
return false;
}
// Validate the server configurations..
if (!array_key_exists('lifetime', $config['server']) || !is_numeric($config['server']['lifetime']) ||
!array_key_exists('name', $config['server']) || !is_string($config['server']['name']) ||
!array_key_exists('levelcap', $config['server']) || !is_numeric($config['server']['levelcap']) ||
!array_key_exists('ip', $config['server']) || !is_string($config['server']['ip']) ||
!array_key_exists('website', $config['server']) || !is_string($config['server']['website']) ||
!array_key_exists('discord', $config['server']) || !is_string($config['server']['discord']))
{
return false;
}
return true;
}
/**
* Checks if the given cache data is valid.
*
* @param {object} $data - The cache object to validate.
* @return {bool} True on success, false otherwise.
*/
function isValidCachedFile($data)
{
// Ensure the incoming object is valid..
if (!$data || $data === null || !is_object($data)) { return false; }
// Check for the required data entries of the cached data..
if (!isset($data->status) || !is_numeric($data->status) || $data->status !== 0) { return false; }
if (!isset($data->version) || !is_string($data->version)) { return false; }
if (!isset($data->lifetime) || !is_numeric($data->lifetime)) { return false; }
if (!isset($data->timestamp) || !is_numeric($data->timestamp)) { return false; }
if (!isset($data->name) || !is_string($data->name)) { return false; }
if (!isset($data->levelcap) || !is_numeric($data->levelcap)) { return false; }
if (!isset($data->ip) || !is_string($data->ip)) { return false; }
if (!isset($data->website) || !is_string($data->website)) { return false; }
if (!isset($data->discord) || !is_string($data->discord)) { return false; }
if (!isset($data->total) || !is_numeric($data->total)) { return false; }
if (!isset($data->unique) || !is_numeric($data->unique)) { return false; }
return true;
}
/**
* Obtains the cached data from the given file.
*
* @param {string} $file - The file path to the cache data to obtain.
* @return {bool|string} False if invalid or not found, string of the cached data if found and valid.
*/
function getCachedData($file)
{
// Ensure the cache file exists..
if (!file_exists($file)) { return false; }
try
{
// Open the cache file for reading..
$f = fopen($file, "r");
if ($f === FALSE) { return false; }
// Read the cached file contents..
$cache = fread($f, filesize($file));
fclose($f);
// Parse the file data as json..
$data = json_decode($cache);
if (json_last_error() !== JSON_ERROR_NONE) { return false; }
// Validate the cache file..
if (!isValidCachedFile($data)) { return false; }
// Cache is valid, return it..
return $cache;
}
catch (Exception $e)
{
return false;
}
}