Page MenuHomeCode

No OneTemporary

This document is not UTF8. It was detected as Shift JIS and converted to UTF8 for display.
diff --git a/api.php b/api.php
index a30a4b3..ce5b7b3 100644
--- a/api.php
+++ b/api.php
@@ -1,131 +1,221 @@
<?php
/*
* Zed
* (c) 2010, Dereckson, some rights reserved
* Released under BSD license
*
* API entry point
*
*/
//API Preferences
define('URL', 'http://' . $_SERVER['HTTP_HOST'] . '/index.php');
//Pluton library
require_once('includes/core.php');
require_once('includes/config.php');
//API libs
require_once('includes/api/api_helpers.php');
require_once('includes/api/cerbere.php');
//Use our URL controller method if you want to mod_rewrite the API
$url = explode('/', substr($_SERVER['PATH_INFO'], 1));
switch ($module = $url[0]) {
+/* -------------------------------------------------------------
+ Site API
+
+ /time
+ /location
+ /perso (disabled)
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
case '':
//Nothing to do
//TODO: offer documentation instead
die();
case 'time':
//Hypership time
api_output(get_hypership_time(), "time");
break;
case 'location':
//Checks creditentials
cerbere();
//Gets location info
require_once("includes/geo/location.php");
$location = new GeoLocation($url[1], $url[2]);
api_output($location, "location");
break;
//case 'perso':
// //Checks creditentials
// cerbere();
// //Gets perso info
// require_once("includes/objects/perso.php");
// $perso = new Perso($url[1]);
// api_output($perso, "perso");
// break;
+/* -------------------------------------------------------------
+ Ship API
+
+ /authenticate
+ /appauthenticate
+ /appauthenticated
+ /move
+ /land
+ /flyout
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
case 'ship':
//Ship API
//Gets ship from Ship API key (distinct of regular API keys)
require_once('includes/objects/ship.php');
$ship = Ship::from_api_key($_REQUEST['key']) or cerbere_die("Invalid ship API key");
switch ($command = $url[1]) {
case '':
//Nothing to do
//TODO: offer documentation instead
die();
case 'authenticate':
//TODO: web authenticate
break;
case 'appauthenticate':
- //Allows desktop application to authenticate
+ //Allows desktop application to authenticate an user
$tmp_session_id = $url[2] or cerbere_die("/appauthenticate/ must be followed by any session identifier");
if ($_REQUEST['name']) {
//Perso will be offered auth invite at next login.
//Handy for devices like PDA, where it's not easy to auth.
$perso = new Perso($_REQUEST['name']);
if ($perso->lastError) {
cerbere_die($perso->lastError);
}
if (!$ship->is_perso_authenticated($perso->id)) {
$ship->request_perso_authenticate($perso->id);
}
$ship->request_perso_confirm_session($tmp_session_id, $perso->id);
} else {
//Delivers an URL. App have to redirects user to this URL
//launching a browser or printing the link.
$ship_code = $ship->get_code();
registry_set("api.ship.session.$ship_code.$tmp_session_id", -1);
$url = get_server_url() . get_url() . "?action=api.ship.appauthenticate&session_id=" . $tmp_session_id;
api_output($url, "URL");
}
break;
case 'appauthenticated':
- //Checks the authentication
+ //Checks the user authentication
$tmp_session_id = $url[2] or cerbere_die("/appauthenticated/ must be followed by any session identifier you used in /appauthenticate");
$perso_id = $ship->get_perso_from_session($tmp_session_id);
if (!$isPersoAuth = $ship->is_perso_authenticated($perso_id)) {
//Global auth not ok/revoked.
$auth->status = -1;
} else {
$perso = Perso::get($perso_id);
$auth->status = 1;
$auth->perso->id = $perso->id;
$auth->perso->nickname = $perso->nickname;
$auth->perso->name = $perso->name;
//$auth->perso->location = $perso->location;
//Is the perso on board? Yes if its global location is S...
$auth->perso->onBoard = (
$perso->location_global[0] == 'S' &&
substr($perso->location_global, 1, 5) == $ship->id
);
if ($auth->perso->onBoard) {
//If so, give local location
$auth->perso->location_local = $perso->location_local;
}
}
api_output($auth, "auth");
break;
+
+ case 'move':
+ //Moves the ship to a new location, given absolute coordinates
+ //TODO: handle relative moves
+ if (count($url) < 2) cerbere_die("/move/ must be followed by a location expression");
+
+ //Gets location class
+ //It's allow: (1) to normalize locations between formats
+ // (2) to ensure the syntax
+ //==> if the ship want to communicate free forms coordinates, must be added on GeoLocation a free format
+ try {
+ $location = new GeoLocation($url[2]);
+ } catch (Exception $ex) {
+ $reply->success = 0;
+ $reply->error = $ex->getMessage();
+ api_output($reply, "move");
+ break;
+ }
+
+ $ship->location_global = $location->global;
+ $ship->save_to_database();
+
+ $reply->success = 1;
+ $reply->location = $ship->location;
+ api_output($reply, "move");
+ break;
+
+ case 'land':
+ case 'flyin':
+ //Flies in
+ try {
+ $location = new GeoLocation($location);
+ } catch (Exception $ex) {
+ $reply->success = 0;
+ $reply->error = $ex->getMessage();
+ api_output($reply, "land");
+ break;
+ }
+
+ break;
+
+ case 'flyout':
+ //Flies out
+
+ break;
+
}
break;
+/* -------------------------------------------------------------
+ Application API
+
+ /checkuserkey
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+ case 'app':
+ //Application API
+ require_once("includes/objects/application.php");
+ $app = Application::from_api_key($_REQUEST['key']) or cerbere_die("Invalid application API key");
+
+ switch ($command = $url[1]) {
+ case '':
+ //Nothing to do
+ //TODO: offer documentation instead
+ die();
+
+ case 'checkuserkey':
+ if (count($url) < 2) cerbere_die("/checkuserkey/ must be followed by an user key");
+ $reply = (boolean)$app->get_perso_id($url[2]);
+ api_output($reply, "check");
+ break;
+ }
+ break;
+
default:
echo "Unknown module:";
dprint_r($url);
break;
}
?>
\ No newline at end of file
diff --git a/includes/geo/galaxy.php b/includes/geo/galaxy.php
new file mode 100644
index 0000000..2efd28d
--- /dev/null
+++ b/includes/geo/galaxy.php
@@ -0,0 +1,53 @@
+<?php
+
+/*
+ * Geo galaxy class
+ * A 3D grid of objects
+ *
+ * 0.1 2010-02-08 14:02 DcK
+ *
+ * @package Zed
+ * @subpackage Geo
+ * @copyright Copyright (c) 2010, Dereckson
+ * @license Released under BSD license
+ * @version 0.1
+ *
+ */
+class GeoGalaxy {
+
+ /*
+ * ----------------------------------------------------------------------- *
+ * Helper methods - math
+ * ----------------------------------------------------------------------- *
+ */
+
+ /*
+ * Normalizes an angle, so 0 < angle < 2 PI
+ * @param $angle angle in radians (use deg2rad() if you've degrees)
+ *
+ */
+
+ /*
+ * Converts polar coordinates in cartesian x y coordinates
+ * @param float $angle angle in radians (use deg2rad() if you've degrees)
+ * @param float $height height
+ * @return array an array of 2 float items: x, y
+ */
+ static function polar_to_cartesian ($angle, $height) {
+ //A story of numbers
+ if ($height < 0) {
+ //Adds 180ー and gets absolute value
+ $height *= -1;
+ $angle + M_PI;
+ }
+ $x = abs(sin($angle)) . $height;
+ $y = abs(cos($angle)) . $height;
+
+ //And now, the sign
+
+
+ //Returns our coordinates
+ return array($x, $y);
+ }
+
+}
\ No newline at end of file
diff --git a/includes/geo/location.php b/includes/geo/location.php
index 56ecb94..475b841 100644
--- a/includes/geo/location.php
+++ b/includes/geo/location.php
@@ -1,288 +1,306 @@
<?php
require_once('body.php');
require_once('place.php');
require_once('includes/objects/ship.php');
/*
* Geo location class
*
* 0.1 2010-01-28 18:52 DcK
*
* @package Zed
* @subpackage Geo
* @copyright Copyright (c) 2010, Dereckson
* @license Released under BSD license
* @version 0.1
*
*/
class GeoLocation {
private $data;
/*
* @var GeoBody a body object
*/
public $body = null;
/*
* @var GeoPlace a place object
*/
public $place = null;
/*
* @var Ship a ship object
*/
public $ship = null;
function __construct ($global = null, $local = null) {
if (!$global) {
$this->data = array();
} elseif (ereg("[BS][0-9]{5}[0-9]{3}", $global)) {
$this->data[0] = $global;
} elseif (ereg("[BS][0-9]{5}", $global)) {
$this->data[0] = $global;
} else {
global $db;
$name = $db->sql_escape($global);
$sql = "SELECT location_code FROM " . TABLE_LOCATIONS . " WHERE location_name LIKE '$name'";
$code = $db->sql_query_express($sql);
if ($code) {
$this->data[0] = $code;
return;
}
throw new Exception("Invalid expression: $global");
}
- $this->load_classes();
+ //TODO: handle $local in a better way: from the global location, gets
+ //a local location handler. Or a some inheriance, like a class
+ //HypershipGeoLocation extending GeoLocation.
+ if ($local !== null) $this->data[1] = $local;
- //TODO: handle $local
+ $this->load_classes();
}
function load_classes () {
//No data, no class to load
if (!count($this->data))
return;
//Loads global classes
$global = $this->data[0];
$code = substr($global, 1, 5);
switch ($global[0]) {
case 'B':
switch (strlen($global)) {
case 9:
$this->place = GeoPlace::from_code($global);
case 6:
$this->body = new GeoBody($code);
break;
}
break;
case 'S':
$this->ship = new Ship($code);
break;
}
}
function __get ($variable) {
switch ($variable) {
/* main variables */
case 'global':
return $this->data[0];
break;
case 'local':
return $this->data[1];
break;
/* global location */
case 'type':
return $this->data[0][0];
case 'body_code':
if ($this->data[0][0] == 'B') {
return substr($this->data[0], 1, 5);
}
return null;
case 'place_code':
if ($this->data[0][0] == 'B') {
return substr($this->data[0], 6, 3);
}
return null;
case 'ship_code':
if ($this->data[0][0] == 'S') {
return substr($this->data[0], 1, 5);
}
return null;
case 'body_kind':
if ($this->data[0][0] == 'B' && $this->body != null) {
if ($kind = $this->body->kind()) {
return $kind;
}
} elseif ($this->data[0][0] == 'S') {
return 'ship';
}
return 'place';
case 'containsGlobalLocation':
return count($this->data) > 0;
case 'containsLocalLocation':
return count($this->data) > 1;
default:
throw new Exception("Unknown variable: $variable");
break;
}
}
/*
* Checks if the place exists
*
* @return boolean true if the place exists ; false otherwise
*/
function exists () {
$n = count($this->data);
//If not defined, it doesn't exist
if ($n == 0) return false;
//Checks global location
switch ($this->data[0][0]) {
case 'B':
switch (strlen($this->data[0])) {
case 9:
if (!$place = GeoPlace::from_code($this->data[0]))
return false;
break;
case 6:
$body = new GeoBody(substr($this->data[0], 1));
if ($body->lastError) return false;
break;
default:
message_die(GENERAL_ERROR, "Invalid global location expression size: " . $this->data[0], "GeoLocation exists method", __LINE__, __FILE__);
}
break;
case 'S':
- message_die(GENERAL_ERROR, "Handle S", 'TODO', __LINE__, __FILE__);
+ $ship = new Ship(substr($this->data[0], 1));
+ if ($body->lastError) return false;
break;
default:
message_die(GENERAL_ERROR, "Invalid global location expression size: " . $this->data[0], "GeoLocation exists method", __LINE__, __FILE__);
return false;
}
if ($n > 1) {
message_die(GENERAL_ERROR, "Can't check if a local place exists yet.", "GeoLocation exists method", __LINE__, __FILE__);
}
return true;
}
function equals ($expression) {
if ($expression == $this->data[0]) return true;
$n1 = strlen($expression);
$n2 = strlen($this->data[0]);
if ($n1 > $n2) {
return substr($expression, 0, $n2) == $this->data[0];
}
return false;
}
function __toString () {
if (!$this->data[0])
return "";
switch ($this->data[0][0]) {
case 'S':
$ship = new Ship($this->ship_code);
$location[] = $ship->name;
break;
case 'B':
$body = new GeoBody($this->body_code);
$location[] = $body->name ? $body->name : lang_get('UnknownBody');
if (strlen($this->data[0]) == 9) {
$place = GeoPlace::from_code($this->data[0]);
$location[] = $place->name ? $place->name : lang_get('UnknownPlace');
}
break;
default:
message_die(GENERAL_ERROR, "Unknown location identifier: $type.<br />Expected: B or S.");
}
return implode(", ", array_reverse($location));
}
function __set ($variable, $value) {
switch ($variable) {
/* main variables */
case 'global':
$this->data[0] = $value;
break;
case 'local':
$this->data[1] = $value;
break;
/* global location */
case 'type':
if ($value == 'B' || $value == 'S') {
if (!$this->data[0]) {
$this->data[0] = $value;
} else {
$this->data[0][0] = $value;
}
}
break;
case 'body_code':
- if (ereg("[0-9]{1,3}", $value)) {
- $value = sprintf("%03d", $value);
+ if (ereg("[0-9]{1,5}", $value)) {
+ $value = sprintf("%05d", $value);
if (!$this->data[0]) {
$this->data[0] = "B" . $value;
return;
} elseif ($this->data[0][0] == 'B') {
- $this->data[0] = "B" . $value . substr($this->data[0], 5);
+ $this->data[0] = "B" . $value . substr($this->data[0], 6);
return;
}
throw new Exception("Global location isn't a body.");
}
throw new Exception("$value isn't a valid body code");
+
+ case 'ship_code':
+ if (ereg("[0-9]{1,5}", $value)) {
+ $value = sprintf("%05d", $value);
+ if (!$this->data[0]) {
+ $this->data[0] = "S" . $value;
+ return;
+ } elseif ($this->data[0][0] == 'S') {
+ $this->data[0] = "S" . $value . substr($this->data[0], 6);
+ return;
+ }
+ throw new Exception("Global location isn't a ship.");
+ }
+ throw new Exception("$value isn't a valid ship code");
case 'place_code':
- if (!ereg("[0-9]{1,5}", $value)) {
+ if (!ereg("[0-9]{1,3}", $value)) {
throw new Exception("$value isn't a valid place code");
}
- $value = sprintf("%05d", $value);
+ $value = sprintf("%03d", $value);
if ($this->data[0][0] == 'B') {
$this->data[0] = substr($this->data[0], 0, 6) . $value;
}
throw new Exception("Global location isn't a body.");
default:
throw new Exception("Unknown variable: $variable");
break;
}
}
}
?>
\ No newline at end of file
diff --git a/includes/objects/application.php b/includes/objects/application.php
new file mode 100644
index 0000000..e52e5aa
--- /dev/null
+++ b/includes/objects/application.php
@@ -0,0 +1,156 @@
+<?php
+
+/*
+ * Application class
+ *
+ * 0.1 2010-02-10 02:40 Autogenerated by Pluton Scaffolding
+ * Some API bits
+ *
+ * @package Zed
+ * @copyright Copyright (c) 2010, Dereckson
+ * @license Released under BSD license
+ * @version 0.1
+ *
+ */
+
+require_once('perso.php');
+
+class Application {
+
+ public $id;
+ public $code;
+ public $name;
+ public $api_key;
+ public $description;
+
+ /*
+ * Initializes a new instance
+ * @param int $id the primary key
+ */
+ function __construct ($id = null) {
+ if ($id) {
+ $this->id = $id;
+ $this->load_from_database();
+ }
+ }
+
+ /*
+ * Loads the object Application (ie fill the properties) from the $_POST array
+ */
+ function load_from_form () {
+ if (array_key_exists('code', $_POST)) $this->code = $_POST['code'];
+ if (array_key_exists('name', $_POST)) $this->name = $_POST['name'];
+ if (array_key_exists('api_key', $_POST)) $this->api_key = $_POST['api_key'];
+ if (array_key_exists('description', $_POST)) $this->description = $_POST['description'];
+ }
+
+ /*
+ * Loads the object Application (ie fill the properties) from the database
+ */
+ function load_from_database () {
+ global $db;
+ $id = $db->sql_escape($this->id);
+ $sql = "SELECT * FROM applications WHERE application_id = '" . $id . "'";
+ if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query applications", '', __LINE__, __FILE__, $sql);
+ if (!$row = $db->sql_fetchrow($result)) {
+ $this->lastError = "Application unkwown: " . $this->id;
+ return false;
+ }
+ $this->code = $row['application_code'];
+ $this->name = $row['application_name'];
+ $this->api_key = $row['api_key'];
+ $this->description = $row['application_description'];
+ return true;
+ }
+
+ /*
+ * Saves to database
+ */
+ function save_to_database () {
+ global $db;
+
+ $id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL';
+ $code = $db->sql_escape($this->code);
+ $name = $db->sql_escape($this->name);
+ $api_key = $db->sql_escape($this->api_key);
+ $description = $db->sql_escape($this->description);
+
+ //Updates or inserts
+ $sql = "REPLACE INTO applications (`application_id`, `application_code`, `application_name`, `api_key`, `application_description`) VALUES ($id, '$code', '$name', '$api_key', '$description')";
+ if (!$db->sql_query($sql)) {
+ message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql);
+ }
+
+ if (!$id) {
+ //Gets new record id value
+ $this->id = $db->sql_nextid();
+ }
+ }
+
+ /*
+ * ----------------------------------------------------------------------- *
+ * Application API methods
+ * ----------------------------------------------------------------------- *
+ */
+
+ /*
+ * Loads an Application object from its API key
+ * @param string Application API key GUID
+ * @return Application the application matching the API key
+ */
+ static function from_api_key ($key) {
+ global $db;
+ $key = $db->sql_escape($key);
+ $sql = "SELECT * FROM applications WHERE api_key = '" . $key . "'";
+ if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query applications", '', __LINE__, __FILE__, $sql);
+ if (!$row = $db->sql_fetchrow($result))
+ return null;
+
+ //Fills app information
+ $app = new Application();
+ $app->id = $row['application_id'];
+ $app->code = $row['application_code'];
+ $app->name = $row['application_name'];
+ $app->api_key = $row['api_key'];
+ $app->description = $row['application_description'];
+
+ return $app;
+ }
+
+ /*
+ * Gets the perso ID from an application user key
+ * @param string $userkey User application key GUID
+ * @return int the perso ID
+ */
+ function get_perso_id ($userkey) {
+ global $db;
+
+ $id = $db->sql_escape($this->id);
+ $userkey = $db->sql_escape($userkey);
+
+ $sql = "SELECT perso_id FROM applications_userkeys WHERE api_userkey = '$userkey' AND application_id = '$id'";
+ return $db->sql_query_express($sql);
+ }
+
+ /*
+ * Generates a key for the specified perso and current application.
+ * @param int $perso_id The perso ID
+ * @param string $userkey User application key GUID (optionnal)
+ * @return Application User application key GUID
+ */
+ function generate_userkey ($perso_id = null, $userkey = null) {
+ global $CurrentPerso;
+
+ //Ensures we've a key and someone to be assigned it
+ if ($userkey === null) $userkey = new_guid();
+ $perso = ($perso_id === null) ? $CurrentPerso : Perso::get($perso_id);
+
+ //Saves key
+ $perso->set_flag('api.app.keys.' . $this->id, $userkey);
+
+ //Returns it
+ return $userkey;
+ }
+}
+
+?>
\ No newline at end of file
diff --git a/includes/objects/port.php b/includes/objects/port.php
new file mode 100644
index 0000000..6ef5466
--- /dev/null
+++ b/includes/objects/port.php
@@ -0,0 +1,176 @@
+<?php
+
+/*
+ * Port class
+ *
+ * 0.1 2010-02-09 19:17 Autogenerated by Pluton Scaffolding
+ *
+ * @package Zed
+ * @copyright Copyright (c) 2010, Dereckson
+ * @license Released under BSD license
+ * @version 0.1
+ *
+ */
+class Port {
+
+ public $id;
+ public $location_global;
+ public $location_local;
+ public $name;
+
+ public $hidden;
+ public $requiresPTA;
+ public $default;
+
+ /*
+ * Initializes a new instance
+ * @param int $id the primary key
+ */
+ function __construct ($id = null) {
+ if ($id) {
+ $this->id = $id;
+ $this->load_from_database();
+ }
+ }
+
+ /*
+ * Loads the object Port (ie fill the properties) from the $_POST array
+ */
+ function load_from_form () {
+ if (array_key_exists('location_global', $_POST)) $this->location_global = $_POST['location_global'];
+ if (array_key_exists('location_local', $_POST)) $this->location_local = $_POST['location_local'];
+ if (array_key_exists('name', $_POST)) $this->name = $_POST['name'];
+
+ if (array_key_exists('hidden', $_POST)) $this->hidden = $_POST['hidden'] ? true : false;
+ if (array_key_exists('requiresPTA', $_POST)) $this->requiresPTA = $_POST['requiresPTA'] ? true : false;
+ if (array_key_exists('default', $_POST)) $this->hidden = $_POST['default'] ? true : false;
+ }
+
+ /*
+ * Loads the object Port (ie fill the properties) from the database
+ */
+ function load_from_database () {
+ global $db;
+ $id = $db->sql_escape($this->id);
+ $sql = "SELECT * FROM ports WHERE port_id = '" . $id . "'";
+ if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query ports", '', __LINE__, __FILE__, $sql);
+ if (!$row = $db->sql_fetchrow($result)) {
+ $this->lastError = "Port unkwown: " . $this->id;
+ return false;
+ }
+ $this->location_global = $row['location_global'];
+ $this->location_local = $row['location_local'];
+ $this->name = $row['port_name'];
+
+ //Explodes place_status SET field in boolean variables
+ if ($row['place_status']) {
+ $flags = explode(',', $row['port_status']);
+ foreach ($flags as $flag) {
+ $this->$flag = true;
+ }
+ }
+
+ return true;
+ }
+
+ /*
+ * Gets status field value
+ */
+ function get_status () {
+ $flags = array('hidden', 'requiresPTA', 'default');
+ foreach ($flags as $flag) {
+ if ($this->$flag) {
+ $status[] = $flag;
+ }
+ }
+ return implode(',', $status);
+ }
+
+ /*
+ * Saves to database
+ */
+ function save_to_database () {
+ global $db;
+
+ $id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL';
+ $location_global = $db->sql_escape($this->location_global);
+ $location_local = $db->sql_escape($this->location_local);
+ $name = $db->sql_escape($this->name);
+ $status = $this->get_status();
+
+ //Updates or inserts
+ $sql = "REPLACE INTO ports (`port_id`, `location_global`, `location_local`, `port_name`, `port_status`) VALUES ($id, '$location_global', '$location_local', '$name', '$status')";
+ if (!$db->sql_query($sql)) {
+ message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql);
+ }
+
+ if (!$id) {
+ //Gets new record id value
+ $this->id = $db->sql_nextid();
+ }
+ }
+
+ /*
+ * Determines if the specified location have a port
+ * @param string $location_global the global location
+ * @return boolean true if there is a spatioport exactly at the specified location ; otherwise, false.
+ */
+ static function have_port ($location_global) {
+ return (get_port_id($location_global) === null) ? false : true;
+ }
+
+ /*
+ * Gets the port situated exactly at the specified global location
+ * @param string $location_global the global location
+ * @return int the port ID
+ */
+ static function get_port_id ($location_global) {
+ global $db;
+ $location_global = $db->sql_escape($location_global);
+ $sql = "SELECT port_id FROM ports WHERE location_global = '$location_global'";
+ if (!$result = $db->sql_query($sql)) {
+ message_die(SQL_ERROR, "Unable to get ports", '', __LINE__, __FILE__, $sql);
+ }
+ if ($row = $db->sql_fetchrow($result)) {
+ return $row['port_id'];
+ }
+ return null;
+ }
+
+ /*
+ * Gets default port, from specified global location
+ * @param string $location_global the global location
+ * @return Port the port near this location ; null if there isn't port there.
+ */
+ static function from_location ($location_global) {
+ $havePlace = strlen($location_global) == 9;
+ $port_id = null;
+
+ if ($havePlace) {
+ //Checks if there's a port at specified location
+ $port_id = self::get_port_id($location_global);
+ }
+
+ if ($port_id == null) {
+ //Nearest default port.
+ //If place have been specified (B0001001), we've to found elsewhere
+ //==> B00001%
+ global $db;
+ $loc = $db->sql_escape(substr($location_global, 0, 6));
+ $sql = "SELECT port_id FROM ports WHERE location_global LIKE '$loc%'";
+ if (!$result = $db->sql_query($sql)) {
+ message_die(SQL_ERROR, "Can't get port", '', __LINE__, __FILE__, $sql);
+ }
+ if ($row = $db->sql_fetchrow($result)) {
+ $port_id = $row['port_id'];
+ } else {
+ return null;
+ }
+ }
+
+ return new Port($port_id);
+ }
+}
+
+?>
+
diff --git a/includes/objects/ship.php b/includes/objects/ship.php
index bde4ff6..6345d6b 100644
--- a/includes/objects/ship.php
+++ b/includes/objects/ship.php
@@ -1,202 +1,225 @@
<?php
/*
* Ship class
*
* 0.1 2010-02-05 18:51 Autogenerated by Pluton Scaffolding
* 0.2 2010-02-06 17:30 Ship API
*
* @package Zed
* @copyright Copyright (c) 2010, Dereckson
* @license Released under BSD license
* @version 0.1
*
*/
require_once("perso.php");
class Ship {
/*
* ----------------------------------------------------------------------- *
* Ship class definition
* ----------------------------------------------------------------------- *
*/
- public $id;
+ public $id;
public $name;
- public $location;
+ public $location_global;
+ public $location_local;
public $api_key;
public $description;
/*
* Initializes a new instance
* @param int $id the primary key
*/
function __construct ($id = null) {
if ($id) {
$this->id = $id;
$this->load_from_database();
}
}
/*
* Loads the object Ship (ie fill the properties) from the $_POST array
*/
function load_from_form () {
if (array_key_exists('name', $_POST)) $this->name = $_POST['name'];
if (array_key_exists('location', $_POST)) $this->location = $_POST['location'];
if (array_key_exists('api_key', $_POST)) $this->api_key = $_POST['api_key'];
if (array_key_exists('description', $_POST)) $this->description = $_POST['description'];
}
/*
* Loads the object Ship (ie fill the properties) from the database
*/
function load_from_database () {
global $db;
$id = $db->sql_escape($this->id);
$sql = "SELECT * FROM ships WHERE ship_id = '" . $id . "'";
if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query Ships", '', __LINE__, __FILE__, $sql);
if (!$row = $db->sql_fetchrow($result)) {
$this->lastError = "Ship unkwown: " . $this->id;
return false;
}
$this->name = $row['ship_name'];
$this->location = $row['ship_location'];
$this->api_key = $row['api_key'];
$this->description = $row['ship_description'];
return true;
}
/*
* Saves to database
*/
function save_to_database () {
global $db;
$id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL';
$name = $db->sql_escape($this->name);
$location = $db->sql_escape($this->location);
$api_key = $db->sql_escape($this->api_key);
$description = $db->sql_escape($this->description);
//Updates or inserts
$sql = "REPLACE INTO ships (`ship_id`, `ship_name`, `ship_location`, `api_key`, `ship_description`) VALUES ($id, '$name', '$location', '$api_key', '$description')";
if (!$db->sql_query($sql)) {
message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql);
}
if (!$id) {
//Gets new record id value
$this->id = $db->sql_nextid();
}
}
function __toString () {
return $this->get_code();
}
/*
* ----------------------------------------------------------------------- *
* Helper methods
* ----------------------------------------------------------------------- *
*/
/*
* Gets ship code, e.g. S00001
+ * @return string the ship code
*/
function get_code () {
return sprintf("S%05d", $this->id);
}
+ /*
+ * Determines if the ship is at a spatioport (or assimilated)
+ * @return boolean true if the ship is at a spatioport ; false if the ship is in space
+ */
+ function in_spatioport () {
+ return $this->location_local !== null;
+ }
+
+ /*
+ *
+ * @param string $location_local the spatioport location
+ */
+ function fly_in ($location_local = null) {
+ //TODO: completes location global e.g. B00001 -> B00001003
+ $this->location_local = ($location_local == null) ? 0 : $location_local;
+ }
+
+ function fly_out () {
+ $this->location_local = null;
+ }
+
/*
* ----------------------------------------------------------------------- *
* Ship API methods
* ----------------------------------------------------------------------- *
*/
/*
* Requests the specified perso to authenticate to this ship
* @param mixed $perso_data the perso id or name
*/
function request_perso_authenticate ($perso_data) {
$perso = Perso::get($perso_data);
$flag = sprintf("request.api.ship.auth.%s", $this->get_code());
$perso->set_flag($flag);
$perso->set_flag("site.requests");
}
/*
* Determines if a perso is authenticated to this ship
* @param mixed $perso_data the perso id or name
* @return boolean true if the specified perso name is authenticated to this ship ; otherwise, false.
*/
function is_perso_authenticated ($perso_data) {
$flag = sprintf("api.ship.auth.%s", $this->get_code());
return Perso::get($perso_data)->flags[$flag] == 1;
}
/*
* Requests the specified perso to confirm the ship API session
* @param string $session_id a session ID provided by calling application
* @param mixed $perso_data the perso id or name
*/
function request_perso_confirm_session ($session_id, $perso_data) {
$perso = Perso::get($perso_data);
$flag = sprintf("request.api.ship.session.%s.%s", $this->get_code(), $session_id);
$perso->set_flag($flag);
$perso->set_flag("site.requests");
}
/*
* Cleans ship API temporary sessions
*/
static function clean_ship_sessions () {
//Cleans old sessions
global $db;
$sql = "DELETE FROM " . TABLE_REGISTRY . " WHERE registry_key LIKE 'api.ship.session.%' AND registry_updated < NOW() - 7200";
if (!$db->sql_query($sql))
message_die(SQL_ERROR, "Can't delete old ship API sessions", '', __LINE__, __FILE__, $sql);
}
/*
* Gets perso id from specified session
* @param string $session_id a session ID provided by calling application
* @return mixed the session
*/
function get_perso_from_session ($session_id) {
//Cleands old session
self::clean_ship_sessions();
//Reads api.ship.session.S00001.$session_id
//This registry key contains perso_id if it exists and valid
$key = sprintf("api.ship.session.%s.%s", $this->get_code(), $session_id);
return registry_get($key);
}
/*
* Loads a Ship object from its API key
* @param string $key API key GUID
* @return Ship the ship matching the API key
*/
static function from_api_key ($key) {
global $db;
$key = $db->sql_escape($key);
$sql = "SELECT * FROM ships WHERE api_key = '" . $key . "'";
if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query ships", '', __LINE__, __FILE__, $sql);
if (!$row = $db->sql_fetchrow($result))
return null;
//Fills ship information
$ship = new Ship();
$ship->id = $row['ship_id'];
$ship->name = $row['ship_name'];
$ship->location = $row['ship_location'];
$ship->api_key = $row['api_key'];
$ship->description = $row['ship_description'];
return $ship;
}
}
?>
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 3, 23:57 (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
21126
Default Alt Text
(40 KB)

Event Timeline