Page MenuHomeCode

No OneTemporary

This document is not UTF8. It was detected as Shift JIS and converted to UTF8 for display.
diff --git a/controllers/anonymous.php b/controllers/anonymous.php
index cbcfca9..bbd982c 100644
--- a/controllers/anonymous.php
+++ b/controllers/anonymous.php
@@ -1,152 +1,152 @@
<?php
/**
* Content for anonymous users
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* This controller handles the pages for not logged in users.
*
* It recognizes the following URLs:
- * /tour a redirect to tour.html file, a visite guid馥 drom Zed ;
+ * /tour a redirect to tour.html file, a visite guidテゥe drom Zed ;
* /invite the page to claim the invites.
*
* In all other cases, it prints the login form.
*
* @package Zed
* @subpackage Controllers
- * @author S饕astien Santoro aka Dereckson <dereckson@espace-win.org>
- * @copyright 2010 S饕astien Santoro aka Dereckson
+ * @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @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
*/
//
// Prepares the page
//
switch ($url[0]) {
case 'tour':
//The user have forgotten .html, let's redirect him
header('Location: ' . $Config['StaticContentURL'] . '/tour.html');
exit;
case 'invite':
//Invite form
if ($_POST['form'] == 'account.create') {
//User tries to claim its invite to create an account
require_once('includes/objects/invite.php');
require_once('includes/objects/user.php');
//Gets invite
$invite = new Invite($_POST['invite_code']);
if ($invite->lastError != '') {
//Not existant invite.
$smarty->assign('NOTIFY', lang_get("IncorrectInviteCode"));
} elseif ($invite->is_claimed()) {
//The invitation have already claimed by someone else.
$smarty->assign('NOTIFY', lang_get("InviteCodeAlreadyClaimed"));
} else {
//Checks if the given information is correct
//We ignore bad mails. All we really need is a login and a pass.
//We fill our array $errors with all the errors
$errors = array();
if (!$_POST['username']) {
$errors[] = lang_get('MissingUsername');
} elseif (!User::is_available_login($_POST['username'])) {
$errors[] = lang_get('LoginUnavailable');
}
if (User::get_username_from_email($_POST['email']) !== false) {
$errors[] = "There is already an account with this e-mail.";
}
if (!$_POST['passwd']) {
$errors[] = lang_get('MissingPassword');
}
if (count($errors)) {
$smarty->assign('WAP', join('<br />', $errors));
} else {
//Creates account
$user = new User();
$user->regdate = time();
$user->generate_id();
$user->name = $_POST['username'];
$user->active = 1;
$user->email = $_POST['email'];
$user->set_password($_POST['passwd']);
$user->save_to_database();
//Updates invite
$invite->to_user_id = $user->id;
$invite->save_to_database();
//Notifies inviter
require_once('includes/objects/message.php');
$message = new Message();
$message->from = 0;
$message->to = $invite->from_perso_id;
$message->text = sprintf(lang_get('InviteHaveBeenClaimed'), $invite->code);
$message->send();
//Logs in user
login($user->id, $user->name);
//Prints confirm message
$smarty->assign('WAP', lang_get("AccountCreated"));
//Redirects users to homepage
header('refresh: 5; url=' . get_url());
//Calls void controller
$smarty->assign('screen', 'user.create');
define('NO_FOOTER_EXTRA', true);
include("void.php");
exit;
}
}
//Keeps username, email, invite code printed on account create form
$smarty->assign('username', $_POST['username']);
$smarty->assign('invite_code', $_POST['invite_code']);
$smarty->assign('email', $_POST['email']);
}
//If the invite code is specified, checks format
if ($url[1]) {
if (preg_match("/^([A-Z]){3}([0-9]){3}$/i", $url[1])) {
$smarty->assign('invite_code', strtoupper($url[1]));
} else {
$smarty->assign('NOTIFY', lang_get("IncorrectInviteCode"));
}
}
$template = 'account_create.tpl';
break;
default:
//Login form
if (array_key_exists('LastUsername', $_COOKIE))
$smarty->assign('username', $_COOKIE['LastUsername']);
if (array_key_exists('LastOpenID', $_COOKIE))
$smarty->assign('OpenID', $_COOKIE['LastOpenID']);
$smarty->assign('LoginError', $LoginError);
$template = 'login.tpl';
break;
}
//
// HTML output
//
if ($template) $smarty->display($template);
?>
\ No newline at end of file
diff --git a/includes/SmartLine/SmartLine.php b/includes/SmartLine/SmartLine.php
index 7f44c1c..a11ecd5 100644
--- a/includes/SmartLine/SmartLine.php
+++ b/includes/SmartLine/SmartLine.php
@@ -1,524 +1,523 @@
<?php
/**
* SmartLine 0.1
*
- * http://www.espace-win.org/EWOSP/SmartLine
- * (c) 2007 Espace Win Open Source Project, some rights reserved.
- * Released under BSD License
- *
- * Developer leader: Sテゥbastien Santoro aka Dereckson
- * http://purl.espace-win.org/Who/Dereckson
- *
- * Support: http://purl.espace-win.org/EWOSP/Support
+ * Zed. The immensity of stars. The HyperShip. The people.
*
+ * (c) 2010, Dereckson, some rights reserved.
+ * Released under BSD license.
+ *
* 0.1 2007-07-28 01:36 [DcK] Initial release
* 2010-07-02 00:39 [Dck] Documentation
*
- * @package SmartLine
+ * @package Zed
+ * @subpackage SmartLine
* @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
- * @copyright 2007 Espace Win Open Source Project
+ * @copyright 2007 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/
+ * @link http://bitbucket.org/dereckson/smartline
* @filesource
- */
///////////////////////////////////////////////////////////////////////////////
// SECTION I - INITIALIZATION
///////////////////////////////////////////////////////////////////////////////
//Constants
/**
* The standard, regular output (like STDOUT on POSIX systems)
*/
if (!defined('STDOUT')) define('STDOUT', 1, true);
/**
* The error output (like STDERR on POSIX systems)
*/
if (!defined('STDERR')) define('STDERR', -1, true);
///////////////////////////////////////////////////////////////////////////////
// SECTION Ibis - L10n
///////////////////////////////////////////////////////////////////////////////
//Ensures $lang is a standard array
if (empty($lang) || !is_array($lang)) {
$lang = array();
}
$lang = array_merge($lang, array(
//Errors
'InvalidCommand' => "Invalid command %s. Use <strong>showcommands</strong> to show all commands.",
'RegisteredButNotExistingCommand' => "[CRITICAL ERROR] The command %s has correctly been registered but its method or class doesn't exist.",
'NotYetHelpForThiscommand' => "This command hasn't been documented yet.",
//Help
'DefaultHelp' => "This SmartLine is a command line interface.
<br /><br /><strong>showcommands</strong> prints the list.
<br /><strong>help &lt;command&gt;</strong> prints help for this command.",
'Help' => array(
'help' => "<strong>help &lt;command&gt;</strong> prints command help.",
'showcommands' => 'show available commands'
)
));
///////////////////////////////////////////////////////////////////////////////
// SECTION II - HELPERS FUNCTIONS
///////////////////////////////////////////////////////////////////////////////
/**
* Error handler called during SmartLine command execution.
*
* Any error occuring during command execution will be set in STDERR.
*
* To get an array with all the errors:
* <code>$errors = $yourSmartLine->gets_all(STDERR)</code>
*
* Or to prints all the error:
* <code>$yourSmartLine->prints_all(STDERR)</code>
*
* Or to pops (gets and deletes) only the last error:
* <code>$lastError = $yourSmartLine->gets(STDERR)</code>
*
* @link http://www.php.net/manual/en/function.set-error-handler.php set_error_handler, PHP manual
* @link http://www.php.net/manual/en/errorfunc.examples.php Error handling examples, PHP manual
*
* @param int $level The PHP error level
* @param string $error The error description
* @param string $file The script where the error occured
* @param int $line The line where the error occured
*/
function SmartLineHandler($level, $error, $file, $line) {
switch ($level) {
case E_NOTICE:
$type = 'Notice';
break;
CASE E_WARNING:
$type = 'Warning';
break;
CASE E_ERROR:
$type = 'Error';
break;
default:
$type = "#$level";
}
$_SESSION['SmartLineOutput'][STDERR][] = "[PHP $type] $error ";
return true;
}
///////////////////////////////////////////////////////////////////////////////
// SECTION III - BASE CLASSES
///////////////////////////////////////////////////////////////////////////////
//SmartLineCommand is a class implemanting a SmartLine command.
//If you want to create a more complex command, extends this class.
/**
* The SmartLine command base class.
*
* To add a command, create an instance of the class, like:
* <code>
* class HelloWorldSmartLineCommand extends SmartLineCommand {
* public function run ($argv, $argc) {
* $this->SmartLine->puts('Hello World!');
* }
* }
* </code>
*
* Then, registers your command:
* <code>
* $yourSmartLine->register_object('hello', 'HelloWorldSmartLineCommand');
* </code>
*
* @see SmartLine::register_object
*/
class SmartLineCommand {
/**
* Initializes a new instance of the SmartLine Command
*
* @param SmartLine $SmartLine the SmartLine the command belongs
*/
public function __construct ($SmartLine) {
$this->SmartLine = $SmartLine;
}
/**
* Gets the command help text or indicates help should be fetched from $lang array
*
* @return string|bool a string containing the command help or the bool value false, to enable the default behavior (ie prints $lang['help']['nameOfTheCommand'])
*/
public function help () {
return false;
}
/**
* Runs the command
*
* @param array $argv an array of string, each item a command argument
* @param int $argc the number of arguments
*/
public function run ($argv, $argc) {
}
/**
* The SmartLine where this instance of the command is registered
*
* @var SmartLine
*/
public $SmartLine;
}
/**
* This class represents a SmartLine instance
*
* If you use only register_object, you can use it directly.
* If you use register_method, extends this class in your SmartLine.
*/
class SmartLine {
/**
* Initializes a new instance of the SmartLine object.
*/
public function __construct () {
//Assumes we've an empty array where store registered commands.
$this->commands = array();
//Let's register standard commands
$this->register_object('showcommands', 'ShowCommandsSmartLineCommand');
$this->register_object('help', 'HelpSmartLineCommand');
}
/**
* Registers a private method as command.
*
* @param string $command The name of the command to register
* @param string $method The method to register [OPTIONAL]. If omitted, the method regisered will be the method having the same name as the command.
* @param bool $useArgvArgc If true, indicates the method uses $argv, $argc as parameters. If false, indicates the method uses its parameters (default behavior). [OPTIONAL]
*
* @return bool true if the command have successfully been registered ; otherwise, false.
*/
public function register_method ($command, $method = null, $useArgvArgc = false) {
if (is_null($function)) $method = $command;
if (!method_exists($this, $method)) {
$this->lastError = "Registration failed. Unknown method $method";
return false;
}
$className = ucfirst($method) . 'SmartLineCommand';
//If class exists, add a uniqid after function
while (class_exists($method)) {
$className = uniqid(ucfirst($method)) . 'SmartLineCommand';
}
//Creates the class
if ($useArgvArgc) {
$call = "$this->SmartLine->$method(\$argv, \$argc);";
} else {
//We don't know how many args we've, so we use call_user_func_array
$call = "array_shift(\$argv);
call_user_func_array(
array(&\$this->SmartLine, '$method'),
\$argv
);";
}
$code = "class $className extends SmartLineCommand {
public function run (\$argv, \$argc) {
$call
}
}";
eval($code);
$this->register_object($command, $className);
return true;
}
/**
* Registers an object extending SmartLineCommand as command.
*
* @param string $command The name of the command to register
* @param SmartLineCommand|string $object The object extending SmartLineCommand. This can be the name of the class (string) or an instance already initialized of the object (SmartLineCommand).
* @return bool true if the command have successfully been registered ; otherwise, false.
*/
public function register_object ($command, $object) {
if (is_object($object)) {
//Sets SmartLine property
$object->SmartLine = $this;
} elseif (is_string($object)) {
//Creates a new instance of $object
$object = new $object($this);
} else {
$this->lastError = "Registration failed. register_object second parameter must be a class name (string) or an already initialized instance of such class (object) and not a " . gettype($object);
return false;
}
if (!$this->caseSensitive) $command = strtolower($command);
$this->commands[$command] = $object;
return true;
}
/**
* Determines wheter the specified command have been registered.
*
* @param string $command The name of the command to check
* @return true if the specified command have been registered ; otherwise, false.
*/
public function isRegistered ($command) {
if (!$this->caseSensitive) $command = strtolower($command);
return array_key_exists($command, $this->commands);
}
/**
* Executes the specified expression.
*
* If an error occurs during the command execution:
* the STDERR output will contains the errors,
* the value returned by this methos will be false.
*
* To execute the command and prints error:
* <code>
* $fooSmartLine = new SmartLine();
* //...
* $result = $fooSmartLine->execute($expression);
* $fooSmartLine->prints_all();
* if (!$result) {
* //Errors!
* echo "<h3>Errors</h3>";
* $fooSmartLine->prints_all(STDERR);
* }
* </code>
*
* @param string $expression The expression containing the command to execute
* @return bool true if the command have been successfuly executed ; otherwise, false.
*/
public function execute ($expression) {
//Does nothing if blank line
if (!$expression) return;
//Prepares $argv and $argc
$argv = $this->expression2argv($expression);
$argc = count($argv);
//Gets command
$command = $this->caseSensitive ? $argv[0] : strtolower($argv[0]);
//If command doesn't exist, throws an error
if (!array_key_exists($command, $this->commands)) {
global $lang;
$this->puts(sprintf($lang['InvalidCommand'], $command), STDERR);
return false;
}
//Executes command, intercepting error and returns result
set_error_handler("SmartLineHandler");
try {
$result = $this->commands[$command]->run($argv, $argc);
} catch (Exception $ex) {
$this->puts("<pre>$ex</pre>", STDERR);
}
restore_error_handler();
return $result;
}
/**
* Adds a message to the specified output queue.
*
* @param string $message the message to queue
* @param int $output The output queue (common values are STDERR and STDOUT constants). It's an optionnal parameter ; if ommited, the default value will be STDOUT.
*/
public function puts ($message, $output = STDOUT) {
//
$_SESSION['SmartLineOutput'][$output][] = $message;
}
/**
* Truncates the specified output queue.
*
* @param int $output The output queue (common values are STDERR and STDOUT constants). It's an optionnal parameter ; if ommited, the default value will be STDOUT.
*/
public function truncate ($output = STDOUT) {
unset($_SESSION['SmartLineOutput'][$output]);
}
/**
* Pops (gets and clears) the first message from the specified output queue.
*
* @param int $output The output queue (common values are STDERR and STDOUT constants). It's an optionnal parameter ; if ommited, the default value will be STDOUT.
* @return string the message
*/
public function gets ($output = STDOUT) {
if (count($_SESSION['SmartLineOutput'][$output] > 0))
return array_pop($_SESSION['SmartLineOutput'][$output]);
}
/**
* Gets the number of messages in the specified output queue.
*
* @param int $output The output queue (common values are STDERR and STDOUT constants). It's an optionnal parameter ; if ommited, the default value will be STDOUT.
*/
public function count ($output = STDOUT) {
return count($_SESSION['SmartLineOutput'][$output]);
}
/**
* Gets all the message from the specified output queue.
*
* @param int $output The output queue (common values are STDERR and STDOUT constants). It's an optionnal parameter ; if ommited, the default value will be STDOUT.
* @param string $prefix The string to prepend each message with. It's an optionnal parameter ; if ommited, '<p>'.
* @param string $suffix The string to append each message with. It's an optionnal parameter ; if ommited, '</p>'.
* @return Array an array of string, each item a message from the specified output queue
*/
public function gets_all ($output = STDOUT, $prefix = '<p>', $suffix = '</p>') {
$count = count($_SESSION['SmartLineOutput'][$output]);
if ($count == 0) return;
for ($i = 0 ; $i < $count ; $i++)
$buffer .= $prefix . $_SESSION['SmartLineOutput'][$output][$i] . $suffix;
unset ($_SESSION['SmartLineOutput'][$output]);
return $buffer;
}
/**
* Prints all the message from the specified output queue.
*
* @param int $output The output queue (common values are STDERR and STDOUT constants). It's an optionnal parameter ; if ommited, the default value will be STDOUT.
* @param string $prefix The string to prepend each message with. It's an optionnal parameter ; if ommited, '<p>'.
* @param string $suffix The string to append each message with. It's an optionnal parameter ; if ommited, '</p>'.
*/
public function prints_all ($output = STDOUT, $prefix = '<p>', $suffix = '</p>') {
$count = count($_SESSION['SmartLineOutput'][$output]);
if ($count == 0) return;
for ($i = 0 ; $i < $count ; $i++)
echo $prefix, $_SESSION['SmartLineOutput'][$output][$i], $suffix;
unset ($_SESSION['SmartLineOutput'][$output]);
}
/**
* Gets the command help
*
* @param string $command The command to get help from
* @param string The command help
*/
public function gethelp ($command) {
return $this->commands[$command]->help();
}
/**
* Gets an an argv array from the specified expression
*
* @param string $expression The expression to transform into a argv array
* @return Array An array of string, the first item the command, the others those arguments.
*/
private function expression2argv ($expression) {
//Checks if expression contains "
$pos1 = strpos($expression, '"');
//We isolate "subexpression"
if ($pos1 !== false) {
$pos2 = $pos1;
do {
$pos2 = strpos($expression, '"', $pos2 + 1);
} while ($pos2 !== false && ($expression[$pos2 - 1] == "\\" && $expression[$pos2 - 2] != "\\"));
if ($pos2 === false) {
//If final quote is missing, throws a warning and autoadds it.
$this->puts("[Warning] Final \" missing in $expression.", STDERR);
$argv = $this->expression2argv(substr($expression, 0, $pos1));
$argv[] = substr($expression, $pos1 + 1);
return $argv;
}
return array_merge(
$this->expression2argv(substr($expression, 0, $pos1)),
array(substr($expression, $pos1 + 1, $pos2 - $pos1 - 1)),
$this->expression2argv(substr($expression, $pos2 + 1))
);
}
//Standard expression (ie without ")
$argv = array();
$items = explode(' ', $expression);
foreach ($items as $item) {
$item = trim($item);
if (!$item) {
//blank, we ignore
continue;
}
$argv[] = $item;
}
return $argv;
}
//Contains last error
public $lastError = '';
//If true, command isn't equal to Command
public $caseSensitive = true;
}
///////////////////////////////////////////////////////////////////////////////
// SECTION IV - STANDARD COMMANDS
///////////////////////////////////////////////////////////////////////////////
/*
* These commands are availaible in all default smartlines instance
*/
/**
* The standard command "showcommands"
*
* This command returns a list, with all the available commands
*/
class ShowCommandsSmartLineCommand extends SmartLineCommand {
/**
* Runs the command
*
* @param array $argv an array of string, each item a command argument
* @param int $argc the number of arguments
*/
public function run ($argv, $argc) {
$commands = array_keys($this->SmartLine->commands);
sort($commands);
$this->SmartLine->puts(implode(' ', $commands));
}
}
/**
* The standard command "help"
*
* This command prints command help.
*
* Help could be defined
* in the command classes, as a return value from the help method ;
* in the $lang['Help'] array, at the command key (e.g. $lang['Help']['quux'] for the quux command).
*/
class HelpSmartLineCommand extends SmartLineCommand {
/**
* Runs the command
*
* @param array $argv an array of string, each item a command argument
* @param int $argc the number of arguments
*/
public function run ($argv, $argc) {
global $lang;
if ($argc == 1) {
$this->SmartLine->puts($lang['DefaultHelp']);
} elseif (!$this->SmartLine->isRegistered($argv[1])) {
$this->SmartLine->puts(sprintf($lang['InvalidCommand'], str_replace(' ', '&nbsp;', $argv[1])), STDERR);
} else {
$command = strtolower($argv[1]);
if (!$help = $this->SmartLine->gethelp($command)) {
if (array_key_exists($command, $lang['Help'])) {
$help = $lang['Help'][$command];
} else {
$help = $lang['NotYetHelpForThiscommand'];
}
}
$this->SmartLine->puts($help);
}
}
}
///////////////////////////////////////////////////////////////////////////////
?>
\ No newline at end of file
diff --git a/includes/cache/cache.php b/includes/cache/cache.php
index beff123..a545c7b 100644
--- a/includes/cache/cache.php
+++ b/includes/cache/cache.php
@@ -1,91 +1,91 @@
<?php
/**
* Cache calling class.
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* This file provides a calling class, which read the configuration, ensures
* the cache class for the cache engine given in config exists and initializes
* it.
*
* You'll find a sample of implementation in the CacheMemcached.
* @see CacheMemcached
*
* If no caching mechanism, a "blackhole" void cache will be used.
* @see CacheVoid.
*
* The class to call is determined from the following preference:
* <code>
* $Config['cache']['engine'] = 'memcached'; //will use CacheMemcached class.
* </code>
*
* 0.1 2010-07-06 22:45 Initial version [DcK]
*
* @package Zed
* @subpackage Cache
* @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
* @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
*/
/**
* Cache caller
*/
class Cache {
/**
* Gets the cache instance, initializing it if needed
*
* The correct cache instance to initialize will be determined from the
* $Config['cache']['engine'] preference.
*
* The class cache to use will be Cache + (preference engine, capitalized)
*
* This method will creates an instance of the specified object,
* calling the load static method from this object class.
*
* Example:
* <code>
* $Config['cache']['engine'] = 'quux';
* $cache = Cache::load(); //Cache:load() will call CacheQuux:load();
* </code>
*
- * @eturn Cache the cache instance, or null if nothing is cached
+ * @return Cache the cache instance, or null if nothing is cached
*/
static function load () {
global $Config;
if (
!array_key_exists('cache', $Config) ||
!array_key_exists('engine', $Config['cache'])
) {
//cache is not configured or engine is not specified
$engine = 'void';
} else {
//engine is specified in the configuration
$engine = $Config['cache']['engine'];
}
$engine_file = 'includes/cache/' . $engine . '.php';
$engine_class = 'Cache' . ucfirst($engine);
if (!file_exists($engine_file)) {
message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />$engine_file not found.", 'Cache');
}
require_once($engine_file);
if (!class_exists($engine_class)) {
message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />$engine_class class not found.", 'Cache');
}
return call_user_func(array($engine_class, 'load'));
}
}
?>
\ No newline at end of file
diff --git a/includes/cache/memcached.php b/includes/cache/memcached.php
index f1a9c52..6193a16 100644
--- a/includes/cache/memcached.php
+++ b/includes/cache/memcached.php
@@ -1,112 +1,112 @@
<?php
/**
* Memcached cache.
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* Cache class: memcached
*
* 0.1 2010-07-06 22:55 Initial version [DcK]
*
* @package Zed
* @subpackage Cache
* @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
* @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
*/
/**
* Memcached cache
*
* !!! This class uses the Memcached extension AND NOT the Memcache ext !!!!
*
* References:
- * @linkhttp://www.php.net/manual/en/book/memcached.php
- * @lnk http://memcached.org
+ * @link http://www.php.net/manual/en/book/memcached.php
+ * @link http://memcached.org
*
* This class implements a singleton pattern.
*/
class CacheMemcached {
/**
* The current cache instance
*
* @var CacheMemcached
*/
static $instance = null;
/**
* The Memcached object
*
* @var Memcached
*/
private $memcached = null;
/**
* Gets the cache instance, initializing it if needed
*
- * @eturn Cache the cache instance, or null if nothing is cached
+ * @return Cache the cache instance, or null if nothing is cached
*/
static function load () {
//Checks extension is okay
if (!extension_loaded('memcached')) {
if (extension_loaded('memcache')) {
message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />PHP extension memcached not loaded.<br /><strong>!!! This class uses the Memcached extension AND NOT the Memcache extension (this one is loaded) !!!</strong>", 'Cache');
} else {
message_die(GENERAL_ERROR, "Can't initialize $engine cache engine.<br />PHP extension memcached not loaded.", 'Cache');
}
}
//Creates the Memcached object if needed
if (self::$instance === null) {
global $Config;
self::$instance = new CacheMemcached();
self::$instance->memcached = new Memcached();
self::$instance->memcached->addServer(
$Config['cache']['server'],
$Config['cache']['port']
);
}
return self::$instance;
}
/**
* Gets the specified key's data
*
* @param string $key the key to fetch
* @return mixed the data at the specified key
*/
function get ($key) {
return $this->memcached->get($key);
}
/**
* Sets the specified data at the specified key
*
* @param string $key the key where to store the specified data
* @param mixed $value the data to store
*/
function set ($key, $value) {
return $this->memcached->set($key, $value);
}
/**
* Deletes the specified key's data
*
* @param string $key the key to delete
*/
function delete ($key) {
return $this->memcached->delete($key);
}
}
?>
\ No newline at end of file
diff --git a/includes/cache/void.php b/includes/cache/void.php
index 9edc9c2..2d203fd 100644
--- a/includes/cache/void.php
+++ b/includes/cache/void.php
@@ -1,81 +1,81 @@
<?php
/**
* "Blackhole" void cache.
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* Cache class: void
*
* 0.1 2010-07-06 22:55 Initial version [DcK]
*
* @package Zed
* @subpackage Cache
* @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
* @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
*/
/**
* "blackhole" void cache
*
* This class doesn't cache information, it'a void wrapper
* get will always return null
* set and delete do nothing
*
* It will be used by default if no cache is specified.
*
* This class implements a singleton pattern.
*/
class CacheVoid {
/**
* @var CacheVoid the current cache instance
*/
static $instance = null;
/**
* Gets the cache instance, initializing it if needed
*
- * @eturn Cache the cache instance, or null if nothing is cached
+ * @return Cache the cache instance, or null if nothing is cached
*/
static function load () {
if (self::$instance === null) {
self::$instance = new CacheVoid();
}
return self::$instance;
}
/**
* Gets the specified key's data
*
* @param string $key the key to fetch
* @return mixed the data at the specified key
*/
function get ($key) {
return null;
}
/**
* Sets the specified data at the specified key
*
* @param string $key the key where to store the specified data
* @param mixed $value the data to store
*/
function set ($key, $value) { }
/**
* Deletes the specified key's data
*
* @param string $key the key to delete
*/
function delete ($key) { }
}
?>
\ No newline at end of file
diff --git a/includes/objects/application.php b/includes/objects/application.php
index 9ac250a..b0a50e7 100644
--- a/includes/objects/application.php
+++ b/includes/objects/application.php
@@ -1,177 +1,177 @@
-<?php
-
-/**
- * Application class
- *
- * Zed. The immensity of stars. The HyperShip. The people.
- *
- * (c) 2010, Dereckson, some rights reserved.
- * Released under BSD license.
- *
- * 0.1 2010-02-10 02:40 Autogenerated by Pluton Scaffolding
- * Some API bits
- *
- * @package Zed
- * @subpackage Model
- * @author S饕astien Santoro aka Dereckson <dereckson@espace-win.org>
- * @copyright 2010 S饕astien 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('perso.php');
-
-/**
- * Application class
- *
- * This class maps the Application table.
- *
- * This class also provides methods application API helper methods, to generate
- * an application user key or to gets the perso ID from such a key.
- */
-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;
- }
-}
-
+<?php
+
+/**
+ * Application class
+ *
+ * Zed. The immensity of stars. The HyperShip. The people.
+ *
+ * (c) 2010, Dereckson, some rights reserved.
+ * Released under BSD license.
+ *
+ * 0.1 2010-02-10 02:40 Autogenerated by Pluton Scaffolding
+ * Some API bits
+ *
+ * @package Zed
+ * @subpackage Model
+ * @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @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('perso.php');
+
+/**
+ * Application class
+ *
+ * This class maps the Application table.
+ *
+ * This class also provides methods application API helper methods, to generate
+ * an application user key or to gets the perso ID from such a key.
+ */
+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/perso.php b/includes/objects/perso.php
index edfd02d..9747476 100644
--- a/includes/objects/perso.php
+++ b/includes/objects/perso.php
@@ -1,517 +1,517 @@
<?php
/**
* Perso class
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
* 0.1 2010-01-27 00:39 Autogenerated by Pluton Scaffolding
* 0.2 2010-01-29 14:39 Adding flags support
* 0.3 2010-02-06 17:50 Adding static perso hashtable
*
* @package Zed
* @subpackage Model
* @author S饕astien Santoro aka Dereckson <dereckson@espace-win.org>
* @copyright 2010 S饕astien 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");
/**
* Perso class
*
* This class maps the persos table.
*
* The class also provides methods
* to move or locate a perso,
* to gets and sets perso's flags and notes (tables persos_flags and persos_notes),
* to gets user's perso or check if a perso is online,
* to handle on select and logout events.
*
*/
class Perso {
public $id;
public $user_id;
public $name;
public $nickname;
public $race;
public $sex;
public $avatar;
public $location_global;
public $location_local;
public $flags;
public static $hashtable_id = array();
public static $hashtable_name = array();
/**
* Initializes a new instance
*
* @param mixed $data perso ID or nickname
*/
function __construct ($data = null) {
if ($data) {
if (is_numeric($data)) {
$this->id = $data;
} else {
$this->nickname = $data;
}
$this->load_from_database();
} else {
$this->generate_id();
}
}
/**
* Initializes a new Perso instance if needed or get already available one.
*
* @param mixed $data perso ID or nickname
- * @eturn Perso the perso instance
+ * @return Perso the perso instance
*/
static function get ($data = null) {
if ($data) {
//Checks in the hashtables if we already have loaded this instance
if (is_numeric($data)) {
if (array_key_exists($data, Perso::$hashtable_id)) {
return Perso::$hashtable_id[$data];
}
} else {
if (array_key_exists($data, Perso::$hashtable_name)) {
return Perso::$hashtable_name[$data];
}
}
}
$perso = new Perso($data);
return $perso;
}
/**
* Loads the object Perso (ie fill the properties) from the $_POST array
*/
function load_from_form () {
if (array_key_exists('user_id', $_POST)) $this->user_id = $_POST['user_id'];
if (array_key_exists('name', $_POST)) $this->name = $_POST['name'];
if (array_key_exists('nickname', $_POST)) $this->nickname = $_POST['nickname'];
if (array_key_exists('race', $_POST)) $this->race = $_POST['race'];
if (array_key_exists('sex', $_POST)) $this->sex = $_POST['sex'];
if (array_key_exists('avatar', $_POST)) $this->avatar = $_POST['avatar'];
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'];
}
/**
* Loads the object Perso (ie fill the properties) from the database
*/
function load_from_database () {
global $db;
//Gets perso
$sql = "SELECT * FROM " . TABLE_PERSOS;
if ($this->id) {
$id = $db->sql_escape($this->id);
$sql .= " WHERE perso_id = '" . $id . "'";
} else {
$nickname = $db->sql_escape($this->nickname);
$sql .= " WHERE perso_nickname = '" . $nickname . "'";
}
if ( !($result = $db->sql_query($sql)) ) message_die(SQL_ERROR, "Unable to query persos", '', __LINE__, __FILE__, $sql);
if (!$row = $db->sql_fetchrow($result)) {
$this->lastError = "Perso unkwown: " . $this->id;
return false;
}
$this->id = $row['perso_id'];
$this->user_id = $row['user_id'];
$this->name = $row['perso_name'];
$this->nickname = $row['perso_nickname'];
$this->race = $row['perso_race'];
$this->sex = $row['perso_sex'];
$this->avatar = $row['perso_avatar'];
$this->location_global = $row['location_global'];
$this->location_local = $row['location_local'];
//Gets flags
$sql = "SELECT flag_key, flag_value FROM " . TABLE_PERSOS_FLAGS .
" WHERE perso_id = $this->id";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Can't get flags", '', __LINE__, __FILE__, $sql);
}
while ($row = $db->sql_fetchrow($result)) {
$this->flags[$row["flag_key"]] = $row["flag_value"];
}
//Gets location
$this->location = new GeoLocation(
$this->location_global,
$this->location_local
);
//Puts object in hashtables
Perso::$hashtable_id[$this->id] = $this;
Perso::$hashtable_name[$this->nickname] = $this;
return true;
}
/**
* Saves to database
*/
function save_to_database () {
global $db;
$id = $this->id ? "'" . $db->sql_escape($this->id) . "'" : 'NULL';
$user_id = $db->sql_escape($this->user_id);
$name = $db->sql_escape($this->name);
$nickname = $db->sql_escape($this->nickname);
$race = $db->sql_escape($this->race);
$sex = $db->sql_escape($this->sex);
$avatar = $db->sql_escape($this->avatar);
$location_global = $this->location_global ? "'" . $db->sql_escape($this->location_global) . "'" : 'NULL';
$location_local = $this->location_local ? "'" . $db->sql_escape($this->location_local) . "'" : 'NULL';
//Updates or inserts
$sql = "REPLACE INTO " . TABLE_PERSOS . " (`perso_id`, `user_id`, `perso_name`, `perso_nickname`, `perso_race`, `perso_sex`, `perso_avatar`, `location_global`, `location_local`) VALUES ($id, '$user_id', '$name', '$nickname', '$race', '$sex', '$avatar', $location_global, $location_local)";
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();
}
}
/**
* Updates the specified field in the database record
*
* @param string $field The field to save
*/
function save_field ($field) {
global $db;
if (!$this->id) {
message_die(GENERAL_ERROR, "You're trying to update a perso record not yet saved in the database: $field");
}
$id = $db->sql_escape($this->id);
$value = $db->sql_escape($this->$field);
$sql = "UPDATE " . TABLE_PERSOS . " SET `$field` = '$value' WHERE perso_id = '$id'";
if (!$db->sql_query($sql)) {
message_die(SQL_ERROR, "Unable to save $field field", '', __LINE__, __FILE__, $sql);
}
}
/**
* Gets perso location
*
* @return string The location names
*/
public function where () {
return $this->location->__toString();
}
/**
* Moves the perso to a new location
*
* @param string $global the global target location
* @param string $global the local target location
*/
public function move_to ($global = null, $local = null) {
//Sets global location
if ($global != null) {
$this->location_global = $global;
}
//Sets local location
if ($local != null) {
$this->location_local = $local;
}
//Updates database record
if ($global != null && $local != null) {
global $db;
$perso_id = $db->sql_escape($this->id);
$g = $db->sql_escape($this->location_global);
$l = $db->sql_escape($this->location_local);
$sql = "UPDATE " . TABLE_PERSOS .
" SET location_global = '$g', location_local = '$l'" .
" WHERE perso_id = '$perso_id'";
if (!$db->sql_query($sql))
message_die(SQL_ERROR, "Can't save new $global $local location.", '', __LINE__, __FILE__, $sql);
} elseif ($global != null) {
$this->save_field('location_global');
} elseif ($local != null) {
$this->save_field('location_local');
}
//Updates location member
$this->location = new GeoLocation(
$this->location_global,
$this->location_local
);
}
/**
* Gets the specified flag value
*
* @param string $key flag key
* @param mixed $defaultValue default value if the flag doesn't exist
* @return mixed the flag value (string) or null if not existing
*/
public function get_flag ($key, $defaultValue = null) {
return $this->flag_exists($key) ? $this->flags[$key] : $defaultValue;
}
/**
* Determines if the specified flag exists
*
* @param string $key the flag key to check
* @return boolean true if the specified flag exists ; otherwise, false.
*/
public function flag_exists ($key) {
return array_key_exists($key, $this->flags);
}
/**
* Sets the specified flag
*
* @param string $key flag key
* @param string $value flag value (optional, default value: 1)
*/
public function set_flag ($key, $value = 1) {
//Checks if flag isn't already set at this value
if ($this->flags != null && array_key_exists($key, $this->flags) && $this->flags[$key] === $value)
return;
//Saves flag to database
global $db;
$id = $db->sql_escape($this->id);
$key = $db->sql_escape($key);
$value = $db->sql_escape($value);
$sql = "REPLACE " . TABLE_PERSOS_FLAGS . " SET perso_id = '$id', flag_key = '$key', flag_value = '$value'";
if (!$db->sql_query($sql))
message_die(SQL_ERROR, "Can't save flag", '', __LINE__, __FILE__, $sql);
//Sets flag in this perso instance
$this->flags[$key] = $value;
}
/**
* Deletes the specified flag
*
* @param string $key flag key
*/
public function delete_flag ($key) {
global $db;
if (!array_key_exists($key, $this->flags)) return;
$id = $db->sql_escape($this->id);
$key = $db->sql_escape($key);
$sql = "DELETE FROM " . TABLE_PERSOS_FLAGS .
" WHERE flag_key = '$key' AND perso_id = '$id' LIMIT 1";
if (!$db->sql_query($sql))
message_die(SQL_ERROR, "Can't delete flag", '', __LINE__, __FILE__, $sql);
}
/**
* Ensures the current perso have the specified flag or exits.
*
*
* @param string $flag the flag to assert
* @param int $threshold value the flasg must stricly be greater than (optional, the default value is 0)
*
* Example:
* <code>
* $perso->set_flag('quux.foo', 1);
* //The perso wants to read quux, which we allow with the flag quux.foo
* $perso->request_flag('quux.foo'); //will be okay
*
* //The perso wants also to write quux, which we all allow if quux.foo = 2
* //The threshold will so be 1, as 2 > 1
* $perso->request_flag('quux.foo', 1); //Will exits, with a "You don't have quux.foo permission" message
* </code>
*/
public function request_flag ($flag, $threshold = 0) {
if (!array_key_exists($flag, $this->flags) || $this->flags[$flag] <= $threshold) {
message_die(HACK_ERROR, "You don't have $flag permission.", "Permissions");
}
}
/**
* Gets the specified note
*
* @param string $code the note code
* @return string the note content
*/
public function get_note ($code) {
global $db;
$id = $db->sql_escape($this->id);
$code = $db->sql_escape($code);
$sql = "SELECT note_text FROM " . TABLE_PERSOS_NOTES . " WHERE perso_id = '$id' AND note_code LIKE '$code'";
return $db->sql_query_express($sql);
}
/**
* Sets the specified note
*
* @param string $code the note code
* @param string $text the note content
*/
public function set_note ($code, $text) {
global $db;
$id = $db->sql_escape($this->id);
$code = $db->sql_escape($code);
$text = $db->sql_escape($text);
$sql = "REPLACE INTO " . TABLE_PERSOS_NOTES . " (perso_id, note_code, note_text) VALUES ('$id', '$code', '$text')";
if (!$db->sql_query($sql))
message_die(SQL_ERROR, "Can't save note", '', __LINE__, __FILE__, $sql);
}
/**
* Counts the amount of notes the perso have saved
*
* @return int the amount of notes assigned to the this perso
*/
public function count_notes () {
global $db;
$id = $db->sql_escape($this->id);
$sql = "SELECT COUNT(*) FROM " . TABLE_PERSOS_NOTES . " WHERE perso_id = '$id'";
return $db->sql_query_express($sql);
}
/*
* Determines if the specified ID is available
*
* @param integer $id The perso ID to check
* @return boolean true if the specified ID is available ; otherwise, false
*/
public static function is_available_id ($id) {
global $db;
$sql = "SELECT COUNT(*) FROM " . TABLE_PERSOS . " WHERE perso_id = $id LOCK IN SHARE MODE";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Can't access users table", '', __LINE__, __FILE__, $sql);
}
$row = $db->sql_fetchrow($result);
return ($row[0] == 0);
}
/**
* Generates a unique ID for the current object
*/
private function generate_id () {
do {
$this->id = rand(2001, 5999);
} while (!Perso::is_available_id($this->id));
}
/**
* Checks if the nickname is available
*
* @param string $nickname the nickname to check
*/
public static function is_available_nickname ($nickname) {
global $db;
$nickname = $db->sql_escape($nickname);
$sql = "SELECT COUNT(*) FROM " . TABLE_PERSOS . " WHERE perso_nickname LIKE '$nickname' LOCK IN SHARE MODE;";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Utilisateurs non parsable", '', __LINE__, __FILE__, $sql);
}
$row = $db->sql_fetchrow($result);
return ($row[0] == 0);
}
/**
* Counts the perso a user have
*
* @param int user_id the user ID
* @return the user's perso count
*/
public static function get_persos_count ($user_id) {
global $db;
$sql = "SELECT COUNT(*) FROM " . TABLE_PERSOS . " WHERE user_id = $user_id";
return $db->sql_query_express($sql);
}
/**
* Gets an array with all the perso of the specified user
*
* @param int $user_id the user ID
*/
public static function get_persos ($user_id) {
global $db;
$user_id = $db->sql_escape($user_id);
$sql = "SELECT perso_id FROM " . TABLE_PERSOS . " WHERE user_id = $user_id";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Can't get persos", '', __LINE__, __FILE__, $sql);
}
while ($row = $db->sql_fetchrow($result)) {
$persos[] = Perso::get($row[perso_id]);
}
return $persos;
}
/**
* Gets the first perso a user have
* (typically to be used when get_persos_count returns 1 to autoselect)
*
* @param int user_id the user ID
*/
public static function get_first_perso ($user_id) {
global $db;
$sql = "SELECT perso_id FROM " . TABLE_PERSOS ." WHERE user_id = $user_id LIMIT 1";
if ($perso_id = $db->sql_query_express($sql)) {
return new Perso($perso_id);
}
}
/**
* Determines wheter the perso is online
*
* @return bool true if the perso is online ; otherwise, false.
*/
public function is_online () {
global $db;
$id = $db->sql_escape($this->id);
$sql = "SELECT MAX(online) FROM " . TABLE_SESSIONS ." WHERE perso_id = $id";
if (!$result = $db->sql_query($sql)) {
message_die(SQL_ERROR, "Unable to query the table", '', __LINE__, __FILE__, $sql);
}
$row = $db->sql_fetchrow($result);
return ($row[0] == 1);
}
/**
* This event method is called when the user selects a new perso
*/
public function on_select () {
//Session
set_info('perso_id', $this->id);
$this->set_flag("site.lastlogin", $_SERVER['REQUEST_TIME']);
define("PersoSelected", true);
}
/**
* This event method is called when the user logs off its account or perso
*/
public function on_logout () {
//Clears perso information in $_SESSION and session table
set_info('perso_id', null);
clean_session();
}
}
?>
\ No newline at end of file
diff --git a/includes/objects/ship.php b/includes/objects/ship.php
index b51f161..ddfad17 100644
--- a/includes/objects/ship.php
+++ b/includes/objects/ship.php
@@ -1,321 +1,321 @@
-<?php
-
-/**
- * Ship class
- *
- * Zed. The immensity of stars. The HyperShip. The people.
- *
- * (c) 2010, Dereckson, some rights reserved.
- * Released under BSD license.
- *
- * 0.1 2010-02-05 18:51 Autogenerated by Pluton Scaffolding
- * 0.2 2010-02-06 17:30 Ship API
- *
- * @package Zed
- * @subpackage Model
- * @author S饕astien Santoro aka Dereckson <dereckson@espace-win.org>
- * @copyright 2010 S饕astien 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("perso.php");
-require_once("includes/geo/location.php");
-
-/**
- * Ship class
- *
- * This class maps the ship table.
- *
- * It also provides helper methods to handle landing and fly out,
- * or locate the ship.
- *
- * The class also provides methods for the ship API.
- */
-class Ship {
-
- /*
- * ----------------------------------------------------------------------- *
- * Ship class definition
- * ----------------------------------------------------------------------- *
- */
-
- public $id;
- public $name;
- public $location_global;
- public $location_local;
- public $api_key;
- public $description;
-
- private static $hashtable = array();
-
- /**
- * Initializes a new instance
- *
- * @param int $id the primary key
- */
- function __construct ($id = null) {
- if ($id) {
- if (preg_match("/^S[0-9]{5}$/", $id)) {
- $id = substr($id, 1);
- }
-
- $this->id = $id;
- $this->load_from_database();
- }
- }
-
- /**
- * Initializes a new Ship instance if needed or gets already available one.
- *
- * @param mixed $data ship ID
- * @eturn Ship the ship instance
- */
- static function get ($data = null) {
- if ($data !== null) {
- //Checks in the hashtable if we already have loaded this instance
- if (array_key_exists($data, self::$hashtable)) {
- return self::$hashtable[$data];
- }
- }
-
- $ship = new Ship($data);
- return $ship;
- }
-
- /**
- * 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_global', $_POST)) $this->location = $_POST['location_global'];
- if (array_key_exists('location_local', $_POST)) $this->location = $_POST['location_local'];
- 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_global = $row['location_global'];
- $this->location_local = $row['location_local'];
- $this->api_key = $row['api_key'];
- $this->description = $row['ship_description'];
-
-
- //Puts object in hashtables
- self::$hashtable[$this->id] = $this;
-
- 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_global = $db->sql_escape($this->location_global);
- $location_local = $db->sql_escape($this->location_local);
- $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`, `location_global`, `location_local`, `api_key`, `ship_description`) VALUES ($id, '$name', '$location_global', '$location_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();
- }
- }
-
- /**
- * Gets the ship code, as a string representation of the instance.
- *
- * @return the ship's code
- */
- function __toString () {
- return $this->get_code();
- }
-
- /**
- * Get ships at specified location
- *
- * @param string $location_global global location
- * @param string $location_local local location
- * @return array An array of Ship items, each one a ship at the specified location
- */
- static function get_ships_at ($location_global, $location_local = null) {
- global $db;
-
- //Gets ships
- $sql = "SELECT ship_id, location_global, location_local FROM " . TABLE_SHIPS . " WHERE location_global IS NOT NULL";
- if (!$result = $db->sql_query($sql)) {
- message_die(SQL_ERROR, "Can't get ships", '', __LINE__, __FILE__, $sql);
- }
- $ships = array();
- $location = new GeoLocation($location_global, $location_local);
- while ($row = $db->sql_fetchrow($result)) {
- $shipLocation = new GeoLocation($row['location_global'], $row['location_local']);
- if ($location->equals($shipLocation)) {
- $ships[] = self::get($row['ship_id']);
- }
- }
- return $ships;
- }
-
- /*
- * ----------------------------------------------------------------------- *
- * 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 bool true if the ship is at a spatioport ; false if the ship is in space
- */
- function in_spatioport () {
- return $this->location_local !== null;
- }
-
- /**
- * Flies in the sip
- *
- * @param string $location_local the spatioport location
- *
- * @todo Completes location global e.g. B00001 -> B00001003
- */
- function fly_in ($location_local = null) {
- $this->location_local = ($location_local == null) ? 0 : $location_local;
- }
-
- /**
- * Flies out.
- */
- 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;
- }
-}
-
-?>
+<?php
+
+/**
+ * Ship class
+ *
+ * Zed. The immensity of stars. The HyperShip. The people.
+ *
+ * (c) 2010, Dereckson, some rights reserved.
+ * Released under BSD license.
+ *
+ * 0.1 2010-02-05 18:51 Autogenerated by Pluton Scaffolding
+ * 0.2 2010-02-06 17:30 Ship API
+ *
+ * @package Zed
+ * @subpackage Model
+ * @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @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("perso.php");
+require_once("includes/geo/location.php");
+
+/**
+ * Ship class
+ *
+ * This class maps the ship table.
+ *
+ * It also provides helper methods to handle landing and fly out,
+ * or locate the ship.
+ *
+ * The class also provides methods for the ship API.
+ */
+class Ship {
+
+ /*
+ * ----------------------------------------------------------------------- *
+ * Ship class definition
+ * ----------------------------------------------------------------------- *
+ */
+
+ public $id;
+ public $name;
+ public $location_global;
+ public $location_local;
+ public $api_key;
+ public $description;
+
+ private static $hashtable = array();
+
+ /**
+ * Initializes a new instance
+ *
+ * @param int $id the primary key
+ */
+ function __construct ($id = null) {
+ if ($id) {
+ if (preg_match("/^S[0-9]{5}$/", $id)) {
+ $id = substr($id, 1);
+ }
+
+ $this->id = $id;
+ $this->load_from_database();
+ }
+ }
+
+ /**
+ * Initializes a new Ship instance if needed or gets already available one.
+ *
+ * @param mixed $data ship ID
+ * @return Ship the ship instance
+ */
+ static function get ($data = null) {
+ if ($data !== null) {
+ //Checks in the hashtable if we already have loaded this instance
+ if (array_key_exists($data, self::$hashtable)) {
+ return self::$hashtable[$data];
+ }
+ }
+
+ $ship = new Ship($data);
+ return $ship;
+ }
+
+ /**
+ * 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_global', $_POST)) $this->location = $_POST['location_global'];
+ if (array_key_exists('location_local', $_POST)) $this->location = $_POST['location_local'];
+ 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_global = $row['location_global'];
+ $this->location_local = $row['location_local'];
+ $this->api_key = $row['api_key'];
+ $this->description = $row['ship_description'];
+
+
+ //Puts object in hashtables
+ self::$hashtable[$this->id] = $this;
+
+ 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_global = $db->sql_escape($this->location_global);
+ $location_local = $db->sql_escape($this->location_local);
+ $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`, `location_global`, `location_local`, `api_key`, `ship_description`) VALUES ($id, '$name', '$location_global', '$location_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();
+ }
+ }
+
+ /**
+ * Gets the ship code, as a string representation of the instance.
+ *
+ * @return the ship's code
+ */
+ function __toString () {
+ return $this->get_code();
+ }
+
+ /**
+ * Get ships at specified location
+ *
+ * @param string $location_global global location
+ * @param string $location_local local location
+ * @return array An array of Ship items, each one a ship at the specified location
+ */
+ static function get_ships_at ($location_global, $location_local = null) {
+ global $db;
+
+ //Gets ships
+ $sql = "SELECT ship_id, location_global, location_local FROM " . TABLE_SHIPS . " WHERE location_global IS NOT NULL";
+ if (!$result = $db->sql_query($sql)) {
+ message_die(SQL_ERROR, "Can't get ships", '', __LINE__, __FILE__, $sql);
+ }
+ $ships = array();
+ $location = new GeoLocation($location_global, $location_local);
+ while ($row = $db->sql_fetchrow($result)) {
+ $shipLocation = new GeoLocation($row['location_global'], $row['location_local']);
+ if ($location->equals($shipLocation)) {
+ $ships[] = self::get($row['ship_id']);
+ }
+ }
+ return $ships;
+ }
+
+ /*
+ * ----------------------------------------------------------------------- *
+ * 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 bool true if the ship is at a spatioport ; false if the ship is in space
+ */
+ function in_spatioport () {
+ return $this->location_local !== null;
+ }
+
+ /**
+ * Flies in the sip
+ *
+ * @param string $location_local the spatioport location
+ *
+ * @todo Completes location global e.g. B00001 -> B00001003
+ */
+ function fly_in ($location_local = null) {
+ $this->location_local = ($location_local == null) ? 0 : $location_local;
+ }
+
+ /**
+ * Flies out.
+ */
+ 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;
+ }
+}
+
+?>
diff --git a/includes/story/choice.php b/includes/story/choice.php
index 04b4b44..a7cb0a3 100644
--- a/includes/story/choice.php
+++ b/includes/story/choice.php
@@ -1,107 +1,110 @@
<?php
/**
* Story choice.
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
+ *
+ * @package Zed
+ * @subpackage Story
+ * @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
+ * @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
+ */
+
+/**
+ * Story choice class
*
* This class is a PHP mapping from the Story XML format's <choice> tag.
*
* <code>
* $xml = "<choice goto=\"city.entry\">Descendre vers le centre ville</choice>";
* $parser = new SimpleXmlElement($xml);
* $choice = StoryChoice::from_xml($parser);
*
* echo $choice->text; //That will output Descendre vers le centre ville
* echo $choice->goto; //That will output city.entry
* echo $choice->guid; //That will output any guid generated for this instance
*
* $thesamechoice = StoryChoice::from_xml($parser);
* echo $thesamechoice->guid; //That will ouput another guid
* </code>
- *
- * @package Zed
- * @subpackage Story
- * @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
- * @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
*/
-
class StoryChoice {
/**
* The section key this choices links to
*
* @var string
*/
public $goto;
/**
* The choice text
*
* @var string
*/
public $text;
/**
* The choice GUID
*
* It will be automatically generated by the constructor, and so is an
* ephemere data for this StoryChoice instance.
*
* @see new_guid
*
* @var string
*/
public $guid;
/**
* Constructor
*/
function __construct () {
//The guid allows to build temporary URLs to get to right choice
$this->guid = new_guid();
}
/**
* Gets the story text as a string representation of the class
*
* @return string The story text
*/
function __toString () {
return $this->text;
}
/**
* Initializes a new instance of StoryChoice class from a XML element
*
* @param SimpleXMLElement the xml element to parse
* @return StoryChoice the story choice class
*/
static function from_xml ($xml) {
$choice = new StoryChoice();
//Parses attribute
foreach ($xml->attributes() as $key => $value) {
switch ($key) {
case 'goto':
$choice->$key = (string)$value;
break;
default:
message_die(GENERAL_ERROR, "Unknown attribute: $key = \"$value\"", "Story error");
}
}
//Parses content
$choice->text = (string)$xml;
return $choice;
}
}
\ No newline at end of file
diff --git a/includes/story/hook.php b/includes/story/hook.php
index cee8fbd..453d56e 100644
--- a/includes/story/hook.php
+++ b/includes/story/hook.php
@@ -1,100 +1,101 @@
<?php
/**
* Story hook class
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
- * This class allows to hook PHP code to a textual story.
- *
- * It allows the story to be completed by site elements.
- *
- * @example hook_demo.php for a class implementation example
- *
* @package Zed
* @subpackage Story
* @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
* @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
*/
/**
* Story hook class
+ *
+ * This class allows to hook PHP code to a textual story.
+ *
+ * It allows the story to be completed by site elements.
+ *
+ * For a class implementation example:
+ * @see DemoStoryHook
*/
abstract class StoryHook {
/**
* The current story
*
* @var Story
*/
public $story;
/*
* The current story section
*
* @var StorySection
*/
public $section;
/**
* The character involved in the story
*
* @var Perso
*/
public $perso;
/**
* Constructor
*
* @param Story $story The story including this hook
* @param StorySection $section The section including this hook
*/
function __construct ($story, $section) {
$this->story = $story;
$this->section = $section;
$this->perso = $GLOBALS['CurrentPerso'];
$this->initialize();
}
/**
* Initializes hook. Called after constructor.
*/
abstract function initialize ();
/**
* Gets choices extra links
*
* @param Array $links the hooks links array
*/
function get_choices_links (&$links) {}
/**
* Updates description
*
* @param string the description text (from section and previous hooks)
*/
function update_description (&$description) {}
/**
* Adds HTML code *AT THE END* of the story content block
*
* @return string HTML code to print
*/
function add_content () {}
/**
* Adds HTML code *AFTER* the content block
*
* @return string HTML code to print
*/
function add_html () {}
}
?>
\ No newline at end of file
diff --git a/includes/story/section.php b/includes/story/section.php
index bd66fa8..86d949b 100644
--- a/includes/story/section.php
+++ b/includes/story/section.php
@@ -1,160 +1,160 @@
<?php
/**
* Story section class
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
*
- * This class is a PHP mapping from the Story XML format's <section> tag.
- *
- * This class also a method to get the section where a specific choice links to.
- *
* @package Zed
* @subpackage Story
* @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
* @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('choice.php');
require_once('hook.php');
/**
* Story section class
+ *
+ * This class is a PHP mapping from the Story XML format's <section> tag.
+ *
+ * This class also a method to get the section where a specific choice links to.
*/
class StorySection {
/**
* The section ID
*
* @var string
*/
public $id;
/**
* The section title
*
* @var string
*/
public $title;
/**
* The section description
*
* @var string
*/
public $description;
/**
* @var string the local location
*/
public $location_local;
/**
* @var Array the section choices (array of StoryChoice items)
*/
public $choices = array();
/*
* @var Array the section hooks (array of StoryHook items)
*/
public $hooks = array();
/**
* @var boolean if true, it's the story start ; otherwise, false;
*/
public $start;
/**
* @var Story the story calling the section
*/
public $story;
/**
* Initializes a new instance of StorySection class
*/
function __construct ($id, $story = null) {
$this->id = $id;
if ($story !== null) {
$this->story = $story;
}
}
/**
* Gets choice from specified guid
*
* @return StoryChoice the wanted choice, or null if it doesn't exist
*/
function get_choice ($guid) {
foreach ($this->choices as $choice) {
if ($choice->guid == $guid)
return $choice;
}
return null;
}
/**
* Initializes a story section from an SimpleXMLElement XML fragment
*
* @param SimpleXMLElement $xml the XML fragment
* @param Story $story the calling story
* @return StorySection the section instance
*/
static function from_xml ($xml, $story = null) {
//Reads attributes
$id = '';
$start = false;
foreach ($xml->attributes() as $key => $value) {
switch ($key) {
case 'start':
if ($value) $start = true;
break;
case 'id':
$id = (string)$value;
break;
default:
message_die(GENERAL_ERROR, "Unknown attribute: $key = \"$value\"", "Story error");
}
}
if (!$id) {
message_die(GENERAL_ERROR, "Section without id. Please add id='' in <section> tag", "Story error");
}
$section = new StorySection($id, $story);
$section->title = (string)$xml->title;
$section->description = (string)$xml->description;
$section->location_local = (string)$xml->local;
$section->start = $start;
//Adds choices
if ($xml->choices) {
foreach ($xml->choices->choice as $choice) {
$section->choices[] = StoryChoice::from_xml($choice);
}
}
//Adds hooks
if ($xml->hooks) {
foreach ($xml->hooks->hook as $hook) {
//<hook type="spatioport" /> will assign 'spatioport' to $hook;
$hook = (string)$hook->attributes()->type;
require_once("hook_$hook.php");
$section->hooks[] = new $class($section->story, $section);
}
}
return $section;
}
}
?>
diff --git a/includes/story/story.php b/includes/story/story.php
index 7a713a4..12abd2e 100644
--- a/includes/story/story.php
+++ b/includes/story/story.php
@@ -1,133 +1,133 @@
<?php
/**
* Story class
*
* Zed. The immensity of stars. The HyperShip. The people.
*
* (c) 2010, Dereckson, some rights reserved.
* Released under BSD license.
- *
- * This class is a PHP mapping from the Story XML format.
- *
- * This class also provides a collection of helper methods to explore the story.
*
* @package Zed
* @subpackage Story
* @author Sテゥbastien Santoro aka Dereckson <dereckson@espace-win.org>
* @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
*/
/**
* Story class
+ *
+ * This class is a PHP mapping from the Story XML format.
+ *
+ * This class also provides a collection of helper methods to explore the story.
*/
class Story {
/**
* The file path
*
* @var string
*/
public $file;
/**
* The story title
*
* @var string
*/
public $title;
/**
* An array of StorySection elements
*
* @var Array
*/
public $sections = array();
/**
* The SimpleXML parser
*
* @var SimpleXMLElement
*/
private $xml;
/**
* The index of start section in sections array
*
* @var string
*/
private $startSection = null;
/**
* An array of StorySection elements, indexed by location
*
* @var Array
*/
private $sectionsByLocation = array();
function __construct ($file) {
//Opens .xml
if (!file_exists($file)) {
message_die(GENERAL_ERROR, "$file not found.", "Story loading error");
}
$this->file = $file;
$this->parse();
}
/**
* Gets start section
*
* @return StorySection the section where the story starts, or null if not defined
*/
function get_start_section () {
return ($this->startSection != null) ? $this->sections[$this->startSection] : null;
}
/**
* Gets section from local location
*
* @return StorySection the default section at this location, or null if not defined
*/
function get_section_from_location ($location) {
return array_key_exists($location, $this->sectionsByLocation) ? $this->sectionsByLocation[$location] : null;
}
/**
* Parses XML file
*/
function parse () {
//Parses it
$this->xml = simplexml_load_file($this->file);
$this->title = (string)$this->xml->title;
foreach ($this->xml->section as $section) {
//Gets section
$section = StorySection::from_xml($section, $this);
//Have we a start section?
if ($section->start) {
//Ensures we've only one start section
if ($this->startSection != null) {
message_die(GENERAL_ERROR, "Two sections have start=\"true\": $section->id and $this->startSection.", "Story error");
}
$this->startSection = $section->id;
}
//By location
if ($section->location_local) {
$this->sectionsByLocation[$section->location_local] = $section;
}
//Adds to sections array
$this->sections[$section->id] = $section;
}
}
}
?>
\ No newline at end of file

File Metadata

Mime Type
text/x-diff
Expires
Mon, Nov 11, 14:25 (2 w, 22 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
21079
Default Alt Text
(100 KB)

Event Timeline