Started with JSON API module system

This commit is contained in:
Stefan Brannfjell 2014-03-09 04:42:28 +01:00
parent 7861292449
commit 733766dd45
8 changed files with 166 additions and 5 deletions

30
api/api.php Normal file
View File

@ -0,0 +1,30 @@
<?php
// Verify the PHP version, gives tutorial if fail.
if (version_compare(phpversion(), '5.3.3', '<')) die('PHP 5.3.3 or higher is required');
if (!isset($filepath)) $filepath = '../';
$version = '1.5_SVN';
session_start();
ob_start();
require $filepath.'config.php';
require $filepath.'engine/database/connect.php';
require $filepath.'engine/function/general.php';
require $filepath.'engine/function/cache.php';
// Initiate default config if nothing is specified (outdated config file)
if (!isset($config['api']['debug'])) $config['api']['debug'] = false;
$response = array(
'version' => array(
'znote' => $version,
'ot' => $config['TFSVersion']
),
);
if (isset($moduleVersion)) $response['version']['module'] = $moduleVersion;
function SendResponse($response) {
global $config;
if ($config['api']['debug']) data_dump($response, false, "Response (debug mode)");
else echo json_encode($response);
}
?>

45
api/index.php Normal file
View File

@ -0,0 +1,45 @@
<?php $filepath = '../'; require_once 'module.php';
// Autofetch API modules
$directory = 'modules';
$plugins = array();
$iterator = new DirectoryIterator($directory);
foreach($iterator as $entity) {
if($entity->isDot())
continue;
$iterator = new DirectoryIterator($entity->getPathname());
foreach($iterator as $entity) {
if($entity->isFile()) {
$file_extension = pathinfo($entity->getFilename(), PATHINFO_EXTENSION);
if ($file_extension == 'php') {
$path = explode('/', $entity->getPathname());
if (count($path) === 1) $path = explode('\\', $entity->getPathname());
$plugins[$path[1]] = $path[2];
}
}
}
}
$response['modules'] = $plugins;
$response['data']['title'] = $config['site_title'];
$response['data']['slogan'] = $config['site_title_context'];
$response['data']['time'] = getClock(time(), false, true);
$response['data']['time_formatted'] = getClock(time(), true, true);
// Account count
$accounts = mysql_select_single("SELECT COUNT('id') AS `count` FROM `accounts`;");
$response['data']['accounts'] = ($accounts !== false) ? $accounts['count'] : 0;
// Player count
$players = mysql_select_single("SELECT COUNT('id') AS `count` FROM `players`;");
$response['data']['players'] = ($players !== false) ? $players['count'] : 0;
// online player count
if ($config['TFSVersion'] != 'TFS_10') $online = mysql_select_single("SELECT COUNT('id') AS `count` FROM `players` WHERE `status`='1';");
else $online = mysql_select_single("SELECT COUNT('player_id') AS `count` FROM `players_online`;");
$response['data']['online'] = ($online !== false) ? $online['count'] : 0;
$response['data']['client'] = $config['client'];
$response['data']['port'] = $config['port'];
$response['data']['guildwar'] = $config['guildwar_enabled'];
$response['data']['forum'] = $config['forum']['enabled'];
SendResponse($response);
?>

3
api/module.php Normal file
View File

@ -0,0 +1,3 @@
<?php if (!isset($filepath)) $filepath = '../../../';
$moduleVersion = 1;
require 'api.php'; ?>

View File

@ -0,0 +1,23 @@
<?php require_once '../../module.php';
// Configure module version number
$response['version']['module'] = 1;
// Fetch number of rows
$rows = getValue($_GET['rows']);
if (!$rows || $rows == 0) $rows = 10;
else $rows = (int)$rows;
// Show which configuration is used
$response['config']['rows'] = $rows;
// Fetch top 10 players
$players = mysql_select_multi("SELECT `p`.`name`, `p`.`level`, `p`.`experience`, `p`.`vocation`, `p`.`lastlogin`, `z`.`created` FROM `players` AS `p` INNER JOIN `znote_players` AS `z` ON `p`.`id` = `z`.`player_id` WHERE `p`.`group_id`<'2' ORDER BY `p`.`experience` DESC LIMIT $rows;");
for ($i = 0; $i < count($players); $i++) {
$players[$i]['vocation_name'] = $config['vocations'][$players[$i]['vocation']];
}
$response['data']['players'] = $players;
SendResponse($response);
?>

View File

@ -0,0 +1,44 @@
<?php require_once '../../module.php';
// Blank/empty module, nice code to start with when making custom stuff.
// Configure module version number
$response['version']['module'] = 1;
/* Do PHP logic, you got access to:
-Znote AAC sql functions:
:mysql_select_single("QUERY");
:mysql_select_multi("QUERY");
:mysql_update("QUERY"), mysql_insert("QUERY"), mysql_delete("QUERY")
-Config values
:etc $config['vocations']
-Cache system
:Sample:
$cache = new Cache('engine/cache/api/ApiModuleName');
if ($cache->hasExpired()) {
$players = mysql_select_multi("SELECT `name`, `level`, `experience` FROM `players` ORDER BY `experience` DESC LIMIT 5;");
$cache->setContent($players);
$cache->save();
} else {
$players = $cache->load();
}
-Functions found in general.php
:When fetching GET or POST from parameters, ALWAYS use getValue($value)
:Etc if you want to fetch character name from url, do it like this:
$playername = getValue($_GET['name']);
if ($playername !== false) {
// $playername either contains player name, or false if failed to fetch name from GET.
}
:getValue is often used in 3 ways: Fetch GET and POST values, or sanitize/secure any value you wish.
:Check ZnoteAAC\engine\function\general.php for full list of available functions.
*/
// Save the results of previous logic to the response
$response['data']['title'] = "The fabulous blank page!";
// Send the response through JSON API
SendResponse($response);
?>

View File

@ -0,0 +1,15 @@
<?php require_once '../../module.php';
// Configure module version number
$response['version']['module'] = 1;
// Fetch towns
$response['data']['towns'] = $config['towns'];
// Fetch towns available under character creation
foreach ($config['available_towns'] as $id) {
$response['data']['available'][$id] = $response['data']['towns'][$id];
}
SendResponse($response);
?>

View File

@ -139,7 +139,6 @@
// -------------- \\
// WEBSITE STUFF \\
// -------------- \\
// Highscore configuration
$config['highscore'] = array(
'rows' => 100,
@ -232,6 +231,10 @@
//----------------\\
// ADVANCED STUFF \\
//----------------\\
// Api config
$config['api'] = array(
'debug' => false,
);
// Don't touch this unless you know what you are doing. (modifying this(key value) also requires modifications in OT files /XML/commands.xml).
$config['ingame_positions'] = array(

View File

@ -97,7 +97,6 @@ function url($path = false) {
return $protocol . $domain . $folder . '/' . $path;
}
// Get last cached
function getCache() {
$results = mysql_select_single("SELECT `cached` FROM `znote`;");
return ($results !== false) ? $results['cached'] : false;
@ -294,6 +293,7 @@ function getIP() {
return $_SERVER['REMOTE_ADDR'];
}
// Deprecated, just use count($array) instead.
function array_length($ar) {
$r = 1;
foreach($ar as $a) {
@ -329,7 +329,6 @@ function online_id_to_name($id) {
// Parameter: players.vocation_id. Returns: Configured vocation name.
function vocation_id_to_name($id) {
$vocations = config('vocations');
return ($vocations[$id] >= 0) ? $vocations[$id] : false;
}
@ -361,7 +360,6 @@ function skillid_to_name($skillid) {
// Parameter: players.town_id. Returns: Configured town name.
function town_id_to_name($id) {
$towns = config('towns');
return (array_key_exists($id, $towns)) ? $towns[$id] : 'Missing Town';
}