From 1006cbd43618bc41e947c678103fd9c9b0ea4bbf Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Wed, 18 Dec 2013 16:38:21 -0500 Subject: [PATCH] Got that first API call done Had to work out the API parent class a bit. Also closing #8 as the API docs are about done. Time to knock out the rest of the API calls so I can start using this shit. --- classes/APIv1.php | 67 ++++++++++++++++++++++++++++++++- classes/CustomModule.php | 15 +++++--- modules/api/v1/leaderboards.php | 6 ++- modules/leaderboards.php | 28 +++++++++----- templates/leaderboards.phtml | 5 +-- 5 files changed, 100 insertions(+), 21 deletions(-) diff --git a/classes/APIv1.php b/classes/APIv1.php index 9887cae..4627a16 100644 --- a/classes/APIv1.php +++ b/classes/APIv1.php @@ -1,14 +1,77 @@ request_methods) && !in_array($this->request_methods, $_SERVER['REQUEST_METHOD'])) + || $this->request_methods != $_SERVER['REQUEST_METHOD']) + { + $response_code = 400; + $error = 'Invalid request method.'; + } // Checks the key + try + { + if (isset($_REQUEST['key'])) + { + if (strlen($_REQUEST['key']) == 40) + { + $uid = $this->redis->get('user:api:' . $_REQUEST['key']); + + if ($uid) + { + $this->uid = $uid; + } + else + { + throw new Exception('Invalid key.'); + } + } + else + { + throw new Exception('Invalid key length.'); + } + } + else + { + throw new Exception('Missing key.'); + } + } + catch (Exception $e) + { + $response_code = 401; + $error = $e->getMessage(); + } + + if (isset($_REQUEST['suppress_response_codes'])) + { + $response_code = 200; + } + + $this->response_code = $response_code; + + if ($response_code != 200) + { + $this->error = $error; + } + + Browser::status($response_code); + } + + public function __destruct() + { + // Unsure why I had to put this here, I guess it's being overridden somewhere in PICKLES + header('Content-type: application/json'); } } diff --git a/classes/CustomModule.php b/classes/CustomModule.php index b49c898..663bdab 100644 --- a/classes/CustomModule.php +++ b/classes/CustomModule.php @@ -11,14 +11,17 @@ class CustomModule extends Module $this->redis = new CustomRedis(); - if (isset($_COOKIE['__auth'])) + if (substr(get_class($this), 0, 3) != 'api') { - list($uid, $auth_token) = explode('|', base64_decode($_COOKIE['__auth'])); - - if ($this->redis->hget('user:' . $uid, 'auth') === $auth_token) + if (isset($_COOKIE['__auth'])) { - $this->uid = $uid; - $this->return['uid'] = $uid; + list($uid, $auth_token) = explode('|', base64_decode($_COOKIE['__auth'])); + + if ($this->redis->hget('user:' . $uid, 'auth') === $auth_token) + { + $this->uid = $uid; + $this->return['uid'] = $uid; + } } } } diff --git a/modules/api/v1/leaderboards.php b/modules/api/v1/leaderboards.php index f0b0672..e7dfd08 100644 --- a/modules/api/v1/leaderboards.php +++ b/modules/api/v1/leaderboards.php @@ -2,9 +2,13 @@ class api_v1_leaderboards extends APIv1 { + protected $request_methods = 'GET'; + public function __default() { - + $leaderboards = new leaderboards(); + + return $leaderboards->__default(); } } diff --git a/modules/leaderboards.php b/modules/leaderboards.php index 0206b28..8dc44ce 100644 --- a/modules/leaderboards.php +++ b/modules/leaderboards.php @@ -6,27 +6,37 @@ class leaderboards extends UserModule { // Grabs the user's leaderboards $leaderboards = $this->redis->zrevrange('user:' . $this->uid . ':leaderboards:updated', 0, -1, 'WITHSCORES'); - $data = array(); if ($leaderboards) { $this->redis->multi(); - $leaderboard_uids = array(); - + // Grabs the additional fields foreach ($leaderboards as $uid => $updated_at) { - $leaderboard_uids[] = $uid; + unset($leaderboards[$uid]); + $leaderboards[(int)$uid] = $updated_at; + $this->redis->hmget('leaderboard:' . $uid, array('name', 'created_at')); } - $data = array_combine($leaderboard_uids, $this->redis->exec()); + $fields = $this->redis->exec(); + + foreach ($leaderboards as $uid => $updated_at) + { + $data = current($fields); + + $leaderboards[$uid] = array( + 'name' => $data['name'], + 'created_at' => (int)$data['created_at'], + 'updated_at' => (int)$updated_at, + ); + + next($fields); + } } - return array( - 'leaderboards' => $leaderboards, - 'data' => $data, - ); + return array('leaderboards' => $leaderboards); } } diff --git a/templates/leaderboards.phtml b/templates/leaderboards.phtml index d121373..4ec9403 100644 --- a/templates/leaderboards.phtml +++ b/templates/leaderboards.phtml @@ -22,15 +22,14 @@ if ($this->module['leaderboards']) module['leaderboards'] as $uid => $updated_at) + foreach ($this->module['leaderboards'] as $uid => $data) { - $data = $this->module['data'][$uid]; ?> - +
Delete