Page Menu
Home
Code
Search
Configure Global Search
Log In
Files
F209158
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Subscribers
None
View Options
diff --git a/api.php b/api.php
new file mode 100644
--- /dev/null
+++ b/api.php
@@ -0,0 +1,60 @@
+<?php
+
+/*
+ * Zed
+ * (c) 2010, Dereckson, some rights reserved
+ * Released under BSD license
+ *
+ * API entry point
+ *
+ */
+
+//API Preferences
+define('URL', 'http://' . $_SERVER['HTTP_HOST'] . '/index.php');
+
+//Pluton library
+require_once('includes/core.php');
+require_once('includes/config.php');
+
+//API libs
+require_once('includes/api/api_helpers.php');
+require_once('includes/api/cerbere.php');
+
+$url = explode('/', substr($_SERVER['PATH_INFO'], 1));
+
+switch ($module = $url[0]) {
+ case '':
+ //Nothing to do
+ //TODO: offer documentation instead
+ die();
+
+ case 'time':
+ //Hypership time
+ api_output(get_hypership_time(), "time");
+ break;
+
+ case 'location':
+ //Checks creditentials
+ cerbere();
+ //Gets location info
+ require_once("includes/geo/location.php");
+ $location = new GeoLocation($url[1], $url[2]);
+ api_output($location, "location");
+ break;
+
+ case 'perso':
+ //Checks creditentials
+ cerbere();
+ //Gets perso info
+ require_once("includes/objects/perso.php");
+ $perso = new Perso($url[1]);
+ api_output($perso, "perso");
+ break;
+
+ default:
+ echo "Unknown module:";
+ dprint_r($url);
+ break;
+}
+
+?>
\ No newline at end of file
diff --git a/includes/api/api_helpers.php b/includes/api/api_helpers.php
--- a/includes/api/api_helpers.php
+++ b/includes/api/api_helpers.php
@@ -1,102 +1,128 @@
<?php
//
// API helpers function
//
// XML outputs code uses:
// - http://www.thedeveloperday.com/xml-beautifier-tool/
// - http://snipplr.com/view/3491/convert-php-array-to-xml-or-simple-xml-object-if-you-wish/
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
- * @param array $data
+ * @param mixed $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml - should only be used recursively
* @param string $unknownNodeName - name to give to unknown (numeric) keys
* @return string XML
*/
function toXml($data, $rootNodeName = 'data', $xml = null, $unknownNodeName = 'unknownNode')
{
- if ($rootNodeName == null) $rootNodeName = 'data';
- if ($unknownNodeName == null) $unknownNodeName = 'unknownNode';
+ if (!$rootNodeName) $rootNodeName = 'data';
+ if (!$unknownNodeName) $unknownNodeName = 'unknownNode';
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
- {
ini_set ('zend.ze1_compatibility_mode', 0);
- }
- if ($xml == null)
- {
+ if ($xml == null) {
+ if (!is_array($data) && !is_object($data)) {
+ //We've a singleton
+ return "<?xml version='1.0' encoding='utf-8'?><$rootNodeName>$data<$rootNodeName>";
+ }
+
+ //Starts with simple document
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
}
// loop through the data passed in.
- foreach($data as $key => $value)
- {
+ foreach($data as $key => $value) {
// no numeric keys in our xml please!
- if (is_numeric($key))
- {
+ if (is_numeric($key)) {
// make string key...
$key = $unknownNodeName . '_'. (string) $key;
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z]/i', '', $key);
- // if there is another array found recrusively call this function
- if (is_array($value))
- {
+ //If there is another array found recrusively call this function
+ if (is_array($value)) {
$node = $xml->addChild($key);
- // recrusive call.
+ //Recursive call.
toXml($value, $rootNodeName, $node, $unknownNodeName);
- }
- else
- {
- // add single node.
- $value = htmlentities($value);
- $xml->addChild($key,$value);
+ } elseif (is_object($value)) {
+ $node = $xml->addChild($key);
+ foreach ($value as $subkey => $subvalue) {
+ if ($subkey == "lastError") continue;
+ if ($subvalue === null) {
+ //Ignore null values
+ continue;
+ } elseif (is_array($subvalue) || is_object($subvalue)) {
+ //TODO: test this
+ //Recursive call.
+ $subnode = $node->addChild($subkey);
+ toXml($subvalue, $rootNodeName, $subnode, $unknownNodeName);
+ } else {
+ $node->addChild($subkey, htmlentities($subvalue));
+ }
+ }
+ //die();
+ //$array = array();
+ //$node = $xml->addChild($key);
+ //toXml($value, $rootNodeName, $node, $unknownNodeName);
+ } else {
+ //Adds single node.
+ if ($value || $value === 0) {
+ $value = htmlentities($value);
+ $xml->addChild($key,$value);
+ }
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
function api_output ($reply, $xmlRoot = null, $xmlChildren = null) {
$format = isset($_REQUEST['format']) ? $_REQUEST['format'] : 'preview';
switch ($format) {
case 'preview':
echo '<pre>';
print_r($reply);
echo '</pre>';
break;
case 'php':
echo serialize($reply);
break;
case 'wddx':
require_once('BeautyXML.class.php');
$bc = new BeautyXML();
echo $bc->format(wddx_serialize_value($reply));
break;
case 'json':
echo json_encode($reply);
break;
case 'xml':
- require_once('BeautyXML.class.php');
- $bc = new BeautyXML();
- echo $bc->format(toXml($reply, $xmlRoot, null, $xmlChildren));
+ require_once('BeautyXML.class.php');
+ $bc = new BeautyXML();
+ echo '<?xml version="1.0" encoding="utf-8"?>';
+ echo "\n";
+ echo $bc->format(toXml($reply, $xmlRoot, null, $xmlChildren));
+ break;
+
+ case 'string':
+ echo $reply;
break;
default:
echo "Unknown API format: $_GET[format]";
break;
}
}
?>
\ No newline at end of file
diff --git a/includes/api/cerbere.php b/includes/api/cerbere.php
new file mode 100644
--- /dev/null
+++ b/includes/api/cerbere.php
@@ -0,0 +1,67 @@
+<?php
+
+/*
+ * Zed
+ * (c) 2010, Dereckson, some rights reserved
+ * Released under BSD license
+ *
+ * API security
+ *
+ */
+
+define('ALLOW_LOCALHOST',false);
+define('OUTPUT_ERROR', true);
+define('FORMAT_ERROR', false);
+if (!defined('TABLE_API_KEYS')) define('TABLE_API_KEYS', 'api_keys');
+
+/*
+ * Checks if creditentials are okay and exits after a message error if not
+ */
+function cerbere () {
+ //If ALLOW_LOCALHOST is true, we allow 127.0.0.1 queries
+ //If you use one of your local IP in your webserver vhost like 10.0.0.3
+ //it could be easier to create yourself a test key
+ if (ALLOW_LOCALHOST && $_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
+ return;
+ }
+
+ //No key, no authentication
+ if (!$guid = $_REQUEST['key']) {
+ cerbere_die('You must add creditentials to your request.');
+ }
+
+ //Authenticates user
+ global $db;
+ $guid = $db->sql_escape($guid);
+ $sql = "SELECT key_active FROM " . TABLE_API_KEYS .
+ " WHERE key_guid like '$guid'";
+ if (!$result = $db->sql_query($sql)) {
+ message_die(SQL_ERROR, "Can't get key", '', __LINE__, __FILE__, $sql);
+ }
+ if ($row = $db->sql_fetchrow($result)) {
+ if ($row['key_active']) {
+ //key_hits++
+ $sql = "UPDATE " . TABLE_API_KEYS . " SET key_hits = key_hits + 1" .
+ " WHERE key_guid like '$guid'";
+ if (!$db->sql_query($sql))
+ message_die(SQL_ERROR, "Can't record api call", '', __LINE__, __FILE__, $sql);
+ } else {
+ cerbere_die("Key disabled.");
+ }
+ } else {
+ cerbere_die("Key doesn't exist.");
+ }
+}
+
+function cerbere_die ($message) {
+ if (OUTPUT_ERROR) {
+ if (FORMAT_ERROR) {
+ api_output($message, 'error');
+ } else {
+ echo $message;
+ }
+ }
+ exit;
+}
+
+?>
\ No newline at end of file
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Nov 3, 21:49 (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
20955
Default Alt Text
(8 KB)
Attached To
rZEDHG ZedLegacy
Event Timeline
Log In to Comment