*
*/
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
--- /dev/null
+<?php defined('SYSPATH') or die('No direct script access.');
+/*
+ * Categories model for creating and manipulating categories
+ *
+ * @author Artem Daniliants <artem@daniliants.com>
+ * @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
<?php defined('SYSPATH') or die('No direct script access.');
/*
- * API controller for communicating with mobile clients
+ * Users model for creating and manipulating user accounts
*
* @author Artem Daniliants <artem@daniliants.com>
* @copyright (c) 2010 Speed Freak team