Page Menu
Home
Code
Search
Configure Global Search
Log In
Files
F390559
ship.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
9 KB
Subscribers
None
ship.php
View Options
<?php
/*
* Ship class
*
* 0.1 2010-02-05 18:51 Autogenerated by Pluton Scaffolding
* 0.2 2010-02-06 17:30 Ship API
*
* @package Zed
* @copyright Copyright (c) 2010, Dereckson
* @license Released under BSD license
* @version 0.1
*
*/
require_once
(
"perso.php"
);
require_once
(
"includes/geo/location.php"
);
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
();
}
}
function
__toString
()
{
return
$this
->
get_code
();
}
/*
* Get ships at specified location
* @param string $location_global global location
* @param string $location_local local 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 boolean true if the ship is at a spatioport ; false if the ship is in space
*/
function
in_spatioport
()
{
return
$this
->
location_local
!==
null
;
}
/*
*
* @param string $location_local the spatioport location
*/
function
fly_in
(
$location_local
=
null
)
{
//TODO: completes location global e.g. B00001 -> B00001003
$this
->
location_local
=
(
$location_local
==
null
)
?
0
:
$location_local
;
}
function
fly_out
()
{
$this
->
location_local
=
null
;
}
/*
* ----------------------------------------------------------------------- *
* Ship API methods
* ----------------------------------------------------------------------- *
*/
/*
* Requests the specified perso to authenticate to this ship
* @param mixed $perso_data the perso id or name
*/
function
request_perso_authenticate
(
$perso_data
)
{
$perso
=
Perso
::
get
(
$perso_data
);
$flag
=
sprintf
(
"request.api.ship.auth.%s"
,
$this
->
get_code
());
$perso
->
set_flag
(
$flag
);
$perso
->
set_flag
(
"site.requests"
);
}
/*
* Determines if a perso is authenticated to this ship
* @param mixed $perso_data the perso id or name
* @return boolean true if the specified perso name is authenticated to this ship ; otherwise, false.
*/
function
is_perso_authenticated
(
$perso_data
)
{
$flag
=
sprintf
(
"api.ship.auth.%s"
,
$this
->
get_code
());
return
Perso
::
get
(
$perso_data
)->
flags
[
$flag
]
==
1
;
}
/*
* Requests the specified perso to confirm the ship API session
* @param string $session_id a session ID provided by calling application
* @param mixed $perso_data the perso id or name
*/
function
request_perso_confirm_session
(
$session_id
,
$perso_data
)
{
$perso
=
Perso
::
get
(
$perso_data
);
$flag
=
sprintf
(
"request.api.ship.session.%s.%s"
,
$this
->
get_code
(),
$session_id
);
$perso
->
set_flag
(
$flag
);
$perso
->
set_flag
(
"site.requests"
);
}
/*
* Cleans ship API temporary sessions
*/
static
function
clean_ship_sessions
()
{
//Cleans old sessions
global
$db
;
$sql
=
"DELETE FROM "
.
TABLE_REGISTRY
.
" WHERE registry_key LIKE 'api.ship.session.%' AND registry_updated < NOW() - 7200"
;
if
(!
$db
->
sql_query
(
$sql
))
message_die
(
SQL_ERROR
,
"Can't delete old ship API sessions"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
}
/*
* Gets perso id from specified session
* @param string $session_id a session ID provided by calling application
* @return mixed the session
*/
function
get_perso_from_session
(
$session_id
)
{
//Cleands old session
self
::
clean_ship_sessions
();
//Reads api.ship.session.S00001.$session_id
//This registry key contains perso_id if it exists and valid
$key
=
sprintf
(
"api.ship.session.%s.%s"
,
$this
->
get_code
(),
$session_id
);
return
registry_get
(
$key
);
}
/*
* Loads a Ship object from its API key
* @param string $key API key GUID
* @return Ship the ship matching the API key
*/
static
function
from_api_key
(
$key
)
{
global
$db
;
$key
=
$db
->
sql_escape
(
$key
);
$sql
=
"SELECT * FROM ships WHERE api_key = '"
.
$key
.
"'"
;
if
(
!(
$result
=
$db
->
sql_query
(
$sql
))
)
message_die
(
SQL_ERROR
,
"Unable to query ships"
,
''
,
__LINE__
,
__FILE__
,
$sql
);
if
(!
$row
=
$db
->
sql_fetchrow
(
$result
))
return
null
;
//Fills ship information
$ship
=
new
Ship
();
$ship
->
id
=
$row
[
'ship_id'
];
$ship
->
name
=
$row
[
'ship_name'
];
$ship
->
location
=
$row
[
'ship_location'
];
$ship
->
api_key
=
$row
[
'api_key'
];
$ship
->
description
=
$row
[
'ship_description'
];
return
$ship
;
}
}
?>
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Feb 22, 15:27 (8 h, 11 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
21835
Default Alt Text
ship.php (9 KB)
Attached To
rZED Zed
Event Timeline
Log In to Comment