From bd1316d1ef9a1cf5224d452824e5d5af19a02dd7 Mon Sep 17 00:00:00 2001 From: Artem Daniliants Date: Wed, 17 Mar 2010 14:53:00 +0200 Subject: [PATCH] Added support for fetching categories list --- Server/application/controllers/api.php | 56 +++++++++++++++++++++------ Server/application/models/category.php | 31 +++++++++++++++ Server/application/models/user.php | 2 +- Server/application/views/api/categories.php | 5 +++ 4 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 Server/application/models/category.php create mode 100644 Server/application/views/api/categories.php diff --git a/Server/application/controllers/api.php b/Server/application/controllers/api.php index 6b05e45..e40b263 100644 --- a/Server/application/controllers/api.php +++ b/Server/application/controllers/api.php @@ -54,21 +54,53 @@ class Api_Controller extends Controller{ * */ public function login(){ - if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){ - $user = new User_Model(); - if ($user->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) + if ($this->is_authorized()){ print "OK"; - else { - header('HTTP/1.0 401 Unauthorized'); - print "Invalid credentials"; die; - } } - else { - header('HTTP/1.0 401 Unauthorized'); - print "No credentials supplied"; - die; + else + $this->not_authorized(); + } + + /* + * Validate supplied credentials + */ + public function is_authorized(){ + if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){ + $user = new User_Model(); + if ($user->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) + return true; + else + return false; + } + else + return false; + + } + + /* + * Display "You're not authorized error to client + * + * @todo Need to create function for generally displaying errors + */ + public function not_authorized(){ + header('HTTP/1.0 401 Unauthorized'); + print "Invalid credentials or not registered"; + die; + } + + /* + * Get categories list and output it as XML + * + */ + public function categories(){ + if ($this->is_authorized()){ + $xml = new View('api/categories'); + $cat = new Category_Model(); + $xml->categories=$cat->get_all(); + $xml->render(true); } - + else + $this->not_authorized(); } } \ No newline at end of file diff --git a/Server/application/models/category.php b/Server/application/models/category.php new file mode 100644 index 0000000..f47f0e4 --- /dev/null +++ b/Server/application/models/category.php @@ -0,0 +1,31 @@ + + * @copyright (c) 2010 Speed Freak team + * @license http://opensource.org/licenses/gpl-license.php GNU Public License + */ + +class Category_Model extends Model { + + public function __construct(){ + + // load database library into $this->db + parent::__construct(); + } + + /* + * Fetch all categories + * + * @return object|bool Returns object containing results if everything is ok and false otherwise + */ + public function get_all(){ + $results = $this->db->query("SELECT slug,description,unit FROM categories"); + if ($results->count()>0) + return $results; + else + return false; + } + +} \ No newline at end of file diff --git a/Server/application/models/user.php b/Server/application/models/user.php index 4440cdf..855ffaf 100644 --- a/Server/application/models/user.php +++ b/Server/application/models/user.php @@ -1,6 +1,6 @@ * @copyright (c) 2010 Speed Freak team diff --git a/Server/application/views/api/categories.php b/Server/application/views/api/categories.php new file mode 100644 index 0000000..287fa97 --- /dev/null +++ b/Server/application/views/api/categories.php @@ -0,0 +1,5 @@ +xml version="1.0" encoding="utf-8" "; ?> + + + slug; ?> + \ No newline at end of file -- 1.7.9.5