Fixed minor bugs
[speedfreak] / Server / application / helpers / apiler.php
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /*
3  * Helper class mainly for API controllers
4  * 
5  * @author      Artem Daniliants <artem@daniliants.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 class apiler_Core {
11
12     /**
13      * Get XML either from POST variable or FILES variable
14      * 
15      * @access private
16      * @static
17      * @return mixed Returns either SimpleXml object or outputs error along with HTTP 400 error
18      */
19     public static function get_xml(){
20         if (isset($_POST['xml'])){
21             $xml = simplexml_load_string($_POST['xml']);
22         }
23         elseif (isset($_FILES['xml'])){
24             $xml = simplexml_load_file($_FILES['xml']['tmp_name']);
25         }
26         else{
27             header("HTTP/1.1 400 Bad Request");
28             echo "Please supply required parameters";
29             die;
30         }
31         return $xml;
32     }
33     
34     
35     /**
36      * Check if user is authorized
37      * 
38      * @access public
39      * @static
40      * @return boolean Returns TRUE if authorized and FALSE otherwise
41      */
42     public static function is_authorized(){
43                 if (isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){
44                     $user = new User_Model();
45                     if ($user->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
46                         return true;
47                     else
48                         return false;
49                 }
50                 else
51                     return false;
52
53     }
54     
55     
56     /**
57      * Display "Unauthorized error"
58      * 
59      * @access public
60      * @static
61      * @return string Prints error text
62      */
63     public static function not_authorized(){
64                 header('HTTP/1.0 401 Unauthorized');
65         print "Invalid credentials or not registered";
66         die;
67     }
68     
69     /**
70      * Verify user's credentials
71      *  
72      * @access public
73      * @return string Outputs "OK" or error
74      */
75     public function login(){
76                 if ($this->is_authorized()){
77                 print "OK";
78                 die;
79             }else
80                         $this->not_authorized();
81      }
82     
83 }