Fixed user model issue with SQL binding
[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     // escape string for db
70     public static function e($string){
71         return mysql_escape_string($string);
72     }
73     
74     /**
75      * Verify user's credentials
76      *  
77      * @access public
78      * @return string Outputs "OK" or error
79      */
80     public function login(){
81                 if ($this->is_authorized()){
82                 print "OK";
83                 die;
84             }else
85                         $this->not_authorized();
86      }
87     
88 }