diff --git a/includes/geo/sceneindex.php b/includes/geo/sceneindex.php index e7ac4a1..557db30 100755 --- a/includes/geo/sceneindex.php +++ b/includes/geo/sceneindex.php @@ -1,218 +1,218 @@ * @copyright 2010 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource * * This class implements a singleton pattern. */ require_once('includes/cache/cache.php'); /** * Geo scene class * * This class provides an index of available scene template files. */ class GeoSceneIndex { /** * Global location templates array * * Keys are global location codes. * Values the relevant template file. * * @var Array */ public $global_templates; /** * Local location templates 2D array * * Keys are global and local location codes. * Values the relevant template file. * * e.g. $local_templates['B00017001']['(10, 50, 8)'] => 'B00017_port.tpl' * * @var Array */ public $local_templates; /** * Time of the last updated file in the scenes directory * * @var int */ public $updated; /** * The directory where templates are stored * * @var string */ public $directory; /** * The current index instance array * * Keys are scene directories (string) * Items are GeoSceneIndex instances * * @var Array */ static $instance = []; /** * Gets the index instance, initializing it if needed * * @return index the index instance */ static function load ($directory) { //Creates the index object if needed if (!array_key_exists($directory, self::$instance)) { self::$instance[$directory] = new GeoSceneIndex($directory); } return self::$instance[$directory]; } /** * Initializes a new GeoSceneIndex instance * * @param string $directory the scene templates directory */ public function __construct ($directory) { $this->directory = $directory; if (!$this->get_cached_information() || !$this->is_up_to_date()) { $this->refresh_information(); $this->set_cached_information(); } } /** * Caches index data */ public function set_cached_information () { $cache = Cache::load(); $cache->set('zed_sceneindex', serialize($this)); } /** * Gets index from cache */ public function get_cached_information () { $cache = Cache::load(); $cached_index =$cache->get('zed_sceneindex'); if ($index = unserialize($cache->get('zed_sceneindex'))) { $index = unserialize($cache->get('zed_sceneindex')); $this->global_templates = $index->global_templates; $this->local_templates = $index->local_templates; $this->updated = $index->updated; $this->directory = $index->directory; return true; } return false; } /** * Reads scene templates and indexes information */ public function refresh_information () { $this->global_templates = []; $this->local_templates = []; $this->updated = filemtime($this->directory); if ($handle = opendir($this->directory)) { while (false !== ($file = readdir($handle))) { if (GeoScene::get_file_extension($file) == 'tpl') { - $template = file_get_contents($this->directory . '/' . $file, false, NULL, 0, 1024); + $template = file_get_contents($this->directory . '/' . $file, false, null, 0, 1024); $location = self::get_template_location($template); - if ($location[1] !== NULL) { + if ($location[1] !== null) { $this->local_templates[$location[0]][$location[1]] = $file; - } elseif ($location[0] != NULL) { + } elseif ($location[0] != null) { $this->global_templates[$location[0]] = $file; } } } closedir($handle); } } /** * Determines if the information is still up to date * * @return bool true if the information is up to date ; otherwise, false. */ public function is_up_to_date () { return ($this->updated == filemtime($this->directory)); } /** * Gets template location * * @return Array an string array of the location (two items; global, local) * At key 0, a string with global location, or NULL if not specified * At key 1, a string with local location, or NULL if not specified */ private static function get_template_location ($template) { - $location = [NULL, NULL]; + $location = [null, null]; //Gets global location $pos1 = strpos($template, "Global location: "); if ($pos1 === false) { throw new Exception("No location in template. Any template file must contain a comment line with the string 'Global location: ' followed by the global location matching the template. It should also contains a line 'Local location: ' when applicable."); } $pos1 += 17; $pos2 = strpos($template, "\n", $pos1); $location[0] = trim(substr($template, $pos1, $pos2 - $pos1)); //Gets local location $pos1 = strpos($template, "Local location: "); if ($pos1 !== false) { $pos1 += 16; $pos2 = strpos($template, "\n", $pos1); $location[1] = trim(substr($template, $pos1, $pos2 - $pos1)); } return $location; } /** * Gets local template file from index * * @param string $location_global the global location * @param string $location_global the local location * @return string the relevant template scene file, or NULL if not existing */ public function get_local_template ($location_global, $location_local) { if (isset($this->local_templates[$location_global][$location_local])) { return $this->local_templates[$location_global][$location_local]; } - return NULL; + return null; } /** * Gets global template file from index * * @param string $location_global the global location * @return string the relevant template scene file, or NULL if not existing */ public function get_global_template ($location_global) { if (isset($this->global_templates[$location_global])) { return $this->global_templates[$location_global]; } - return NULL; + return null; } } diff --git a/includes/objects/invite.php b/includes/objects/invite.php index 784e2d8..b3c8dc9 100755 --- a/includes/objects/invite.php +++ b/includes/objects/invite.php @@ -1,197 +1,197 @@ * @copyright 2010 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource */ /** * User invite class * * This class maps the users_invites table. */ class Invite { public $code; public $date; public $from_user_id; public $from_perso_id; /** * The user_id who have been claimed the invite * Will be NULL as long as the invite haven't been claimed * * @var int */ - public $to_user_id = NULL; + public $to_user_id = null; /** * Initializes a new instance * * @param int $code the primary key */ - function __construct ($code = NULL) { + function __construct ($code = null) { if ($code) { $this->code = $code; $this->load_from_database(); } else { //New invite code $this->generate_code(); $this->date = time(); } } /** * Generates a unique invite code and sets it in the code property. */ function generate_code () { global $db; do { $this->code = generate_random_string("AAA111"); $sql = "SELECT COUNT(*) FROM " . TABLE_USERS_INVITES . " WHERE invite_code = '$this->code' LOCK IN SHARE MODE;"; if (!$result = $db->sql_query($sql)) { message_die(SQL_ERROR, "Can't access invite users table", '', __LINE__, __FILE__, $sql); } $row = $db->sql_fetchrow($result); } while ($row[0]); } /** * Loads the object Invite (ie fill the properties) from the database */ function load_from_database () { global $db; $code = $db->sql_escape($this->code); $sql = "SELECT * FROM " . TABLE_USERS_INVITES . " WHERE invite_code = '" . $code . "'"; if ( !($result = $db->sql_query($sql)) ) { message_die(SQL_ERROR, "Unable to query invite codes", '', __LINE__, __FILE__, $sql); } if (!$row = $db->sql_fetchrow($result)) { $this->lastError = "Invite code unknown: " . $this->code; return false; } $this->code = $row['invite_code']; $this->date = $row['invite_date']; $this->from_user_id = $row['invite_from_user_id']; $this->from_perso_id = $row['invite_from_perso_id']; $this->to_user_id = $row['invite_to_user_id']; return true; } /** * Determines whether the current invite code have been claimed by an user. * * @return true if the code have been claimed ; otherwise, false. */ function is_claimed () { return (bool)$this->to_user_id; } /** * Saves to database */ function save_to_database () { global $db; $code = $db->sql_escape($this->code); $date = $db->sql_escape($this->date); $from_user_id = $db->sql_escape($this->from_user_id); $from_perso_id = $db->sql_escape($this->from_perso_id); $to_user_id = $this->to_user_id ? "'" . $db->sql_escape($this->to_user_id) . "'" : 'NULL'; //Updates or inserts $sql = "REPLACE INTO " . TABLE_USERS_INVITES . " (`invite_code`, `invite_date`, `invite_from_user_id`, `invite_from_perso_id`, `invite_to_user_id`) VALUES ('$code', '$date', '$from_user_id', '$from_perso_id', $to_user_id)"; if (!$db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to save invite code", '', __LINE__, __FILE__, $sql); } } /** * Deletes the invite */ function delete () { global $db; $code = $db->sql_escape($this->code); $sql = "DELETE FROM " . TABLE_USERS_INVITES . " WHERE invite_code = '$code'"; if (!$db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to save delete code", '', __LINE__, __FILE__, $sql); } } /** * Creates an invite code * * @param int $user_id user id * @param int $perso_id perso id * @return string the invite code */ static function create ($user_id, $perso_id) { $invite = new Invite(); $invite->from_perso_id = $perso_id; $invite->from_user_id = $user_id; $invite->save_to_database(); return $invite->code; } /** * Gets invites generated by the specified perso ID * * @param int $perso_id the perso whom to get the invites * @return Array an array of string, each line being an invite code */ static function get_invites_from ($perso_id) { global $db; $sql = "SELECT invite_code FROM " . TABLE_USERS_INVITES . " WHERE invite_from_perso_id = $perso_id AND invite_to_user_id IS NULL ORDER BY invite_date ASC"; if (!$result = $db->sql_query($sql)) { message_die(SQL_ERROR, "Can't access invite users table", '', __LINE__, __FILE__, $sql); } $codes = []; while ($row = $db->sql_fetchrow($result)) { $codes[] = $row['invite_code']; } return $codes; } /** * Gets the perso ID who invited the specified perso * * @param int $perso_id the perso whom to get the invites * @return int|null the perso whom to get the invites ; or null, if nobody have invited him */ static function who_invited ($perso_id) { global $db; $perso = Perso::get($perso_id); if ($user_id = $perso->user_id) { $sql = "SELECT invite_from_perso_id FROM " . TABLE_USERS_INVITES . " WHERE invite_to_user_id = '$user_id'"; if (!$result = $db->sql_query($sql)) { message_die(SQL_ERROR, "Can't access invite users table", '', __LINE__, __FILE__, $sql); } if ($row = $db->sql_fetchrow($result)) { return $row[0]; } } return null; } } diff --git a/includes/objects/port.php b/includes/objects/port.php index 23185a6..750276a 100755 --- a/includes/objects/port.php +++ b/includes/objects/port.php @@ -1,211 +1,211 @@ * @copyright 2010 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource */ require_once("includes/geo/location.php"); /** * Port class * * This class maps the ports table. * * The class also provides helper methods to handle ports at specified location. */ 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) { + 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 " . TABLE_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 unknown: " . $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 * * @return string the status field value (e.g. "requiresPTA,default") */ function getStatus () { $flags = ['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->getStatus(); //Updates or inserts $sql = "REPLACE INTO " . TABLE_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); + return (get_port_id($location_global) !== null); } /** * 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 " . TABLE_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 " . TABLE_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/request.php b/includes/objects/request.php index a603c05..3ff377a 100755 --- a/includes/objects/request.php +++ b/includes/objects/request.php @@ -1,143 +1,143 @@ * @copyright 2011 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource */ /** * Request class * * This class maps the requests table. */ class Request { public $id; public $code; public $title; public $date; public $author; public $to; public $message; public $location_global; public $location_local; public $status; /** * Initializes a new instance * @param int $id the primary key */ - function __construct ($id = NULL) { + function __construct ($id = null) { if ($id) { $this->id = $id; $this->load_from_database(); } else { $this->date = time(); $this->status = 'NEW'; } } /** * Loads the object Request (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('title', $_POST)) { $this->title = $_POST['title']; } if (array_key_exists('date', $_POST)) { $this->date = $_POST['date']; } if (array_key_exists('author', $_POST)) { $this->author = $_POST['author']; } if (array_key_exists('to', $_POST)) { $this->to = $_POST['to']; } if (array_key_exists('message', $_POST)) { $this->message = $_POST['message']; } 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('status', $_POST)) { $this->status = $_POST['status']; } } /** * Loads the object Request (ie fill the properties) from the database */ function load_from_database () { global $db; $id = $db->sql_escape($this->id); $sql = "SELECT * FROM " . TABLE_REQUESTS . " WHERE request_id = '" . $id . "'"; if (!$result = $db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to query requests", '', __LINE__, __FILE__, $sql); } if (!$row = $db->sql_fetchrow($result)) { $this->lastError = "Request unknown: " . $this->id; return false; } $this->code = $row['request_code']; $this->title = $row['request_title']; $this->date = $row['request_date']; $this->author = $row['request_author']; $this->message = $row['request_message']; $this->to = $row['request_to']; $this->location_global = $row['location_global']; $this->location_local = $row['location_local']; $this->status = $row['request_status']; 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); $title = $db->sql_escape($this->title); $date = $db->sql_escape($this->date); $author = $db->sql_escape($this->author); $message = $db->sql_escape($this->message); $to = $db->sql_escape($this->to); $location_global = $db->sql_escape($this->location_global); $location_local = $db->sql_escape($this->location_local); $status = $db->sql_escape($this->status); //Updates or inserts $sql = "REPLACE INTO " . TABLE_REQUESTS . " (`request_id`, `request_code`, `request_title`, `request_date`, `request_author`, `request_message`, `request_to`, `location_global`, `location_local`, `request_status`) VALUES ('$id', '$code', '$title', '$date', '$author', '$message', '$to', '$location_global', '$location_local', '$status')"; if (!$db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql); } if (!$this->id) { //Gets new record id value $this->id = $db->sql_nextid(); } } } diff --git a/includes/objects/requestreply.php b/includes/objects/requestreply.php index b79fb9f..2d5c35c 100755 --- a/includes/objects/requestreply.php +++ b/includes/objects/requestreply.php @@ -1,110 +1,110 @@ * @copyright 2011 Sébastien Santoro aka Dereckson * @license http://www.opensource.org/licenses/bsd-license.php BSD * @version 0.1 * @link http://scherzo.dereckson.be/doc/zed * @link http://zed.dereckson.be/ * @filesource */ /** * RequestReply class * * This class maps the requests_replies table. */ class RequestReply { public $id; public $request_id; public $author; public $date; public $text; /** * Initializes a new instance * @param int $id the primary key */ - function __construct ($id = NULL) { + function __construct ($id = null) { if ($id) { $this->id = $id; $this->load_from_database(); } } /** * Loads the object RequestReply (ie fill the properties) from the $_POST array */ function load_from_form () { if (array_key_exists('request_id', $_POST)) { $this->request_id = $_POST['request_id']; } if (array_key_exists('author', $_POST)) { $this->author = $_POST['author']; } if (array_key_exists('date', $_POST)) { $this->date = $_POST['date']; } if (array_key_exists('text', $_POST)) { $this->text = $_POST['text']; } } /** * Loads the object RequestReply (ie fill the properties) from the database */ function load_from_database () { global $db; $id = $db->sql_escape($this->id); $sql = "SELECT * FROM " . TABLE_REQUESTS_REPLIES . " WHERE request_reply_id = '" . $id . "'"; if (!$result = $db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to query requests_replies", '', __LINE__, __FILE__, $sql); } if (!$row = $db->sql_fetchrow($result)) { $this->lastError = "RequestReply unknown: " . $this->id; return false; } $this->request_id = $row['request_id']; $this->author = $row['request_reply_author']; $this->date = $row['request_reply_date']; $this->text = $row['request_reply_text']; return true; } /** * Saves to database */ function save_to_database () { global $db; $id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL'; $request_id = $db->sql_escape($this->request_id); $author = $db->sql_escape($this->author); $date = $db->sql_escape($this->date); $text = $db->sql_escape($this->text); //Updates or inserts $sql = "REPLACE INTO " . TABLE_REQUESTS_REPLIES . "(`request_reply_id`, `request_id`, `request_reply_author`, `request_reply_date`, `request_reply_text`) VALUES ('$id', '$request_id', '$author', '$date', '$text')"; if (!$db->sql_query($sql)) { message_die(SQL_ERROR, "Unable to save", '', __LINE__, __FILE__, $sql); } if (!$this->id) { //Gets new record id value $this->id = $db->sql_nextid(); } } }