Page Menu
Home
Code
Search
Configure Global Search
Log In
Files
F1138445
application.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Subscribers
None
application.php
View Options
<?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
;
}
}
?>
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Jul 16, 05:55 (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
23227
Default Alt Text
application.php (5 KB)
Attached To
rZED Zed
Event Timeline
Log In to Comment