Fixed minor bugs
[speedfreak] / Server / application / controllers / users.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /*
3  * API for registering users and updating profile information
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 Users_Controller extends Controller{
11     
12     
13         /**
14          * When no parameters are supplied visitor is redirected to project's website
15          * 
16          * @access public
17          * @return void
18          */
19         public function index(){
20         url::redirect(Kohana::config('api.default_redirect'),301);
21     }
22     
23     
24     /**
25      * Register new user
26      * 
27      * @access public
28      * @return string Returns "OK" string upon succession and error message otherwise
29      */
30     public function register(){
31         $xml = apiler::get_xml();
32         try {
33            $user = new User_Model($xml->login, $xml->password, $xml->email, $xml->description);
34            $this->store_avatar($user->get_id($xml->login));
35            echo "OK";
36         }
37         catch (Exception $e) {
38             echo $e->getMessage() . "\n";
39             die;
40         } 
41     }
42     
43     
44     /**
45      * Display user's information
46      * 
47      * @access public
48      * @param string Username that we wish to get information for
49      * @return string Returns information as XML or error message
50      */
51     public function info($username){
52         if (apiler::is_authorized()){
53                         $view = new View('api/user_info');
54                         $user = new User_Model();
55                         $view->user=$user->get_info($username);
56                         if ($view->user==false)
57                                 die('User not found');
58                         if (file_exists(Kohana::config('upload.directory').'/'.$view->user->id.'.jpg'))
59                                 $view->avatar=url::site('static/uploads/avatars/'.$view->user->id.'.jpg', 'http');
60                         $view->render(true);
61         }
62                 else
63                         apiler::not_authorized();
64     }
65     
66     
67     /**
68      * View all registered users
69      * 
70      * @access public
71      * @return string Returns XML containing list of all users or error message
72      */
73     public function list_all(){
74         if (apiler::is_authorized()){
75                 $users = new User_Model();
76                 $list = $users->list_all_users();
77                 $view = new View('api/user_list');
78                 $view->list = $list;
79                 $view->render(true);
80             }
81             else
82                 apiler::not_authorized();
83     }
84     
85     /**
86     * Verify credentials
87     *
88     * @return string Returns string "OK" if login is successful and error otherwise
89     */
90     public function login(){
91         if (apiler::is_authorized()){
92                 print "OK";
93             die;
94         }
95         else
96                 apiler::not_authorized();
97     }
98     
99     
100     /**
101      * Check that supplied avatar is valid and store it
102      * 
103      * @access private
104      * @param array $image Uploaded item found in $_FILES array
105      * @param integer $id User id that will be used as filename
106      * @return boolean Returns TRUE upon succession and FALSE otherwise
107      */
108     private function store_avatar($id){
109         if (isset($_FILES['avatar'])){
110                 $info = getimagesize($_FILES['avatar']['tmp_name']);
111          
112                         if ($_FILES['avatar']['size']<=Kohana::config('api.avatar_max_filesize') AND in_array($info['mime'], Kohana::config('api.avatar_allowed_filetypes')))
113                         {
114                                 if (upload::save('avatar', $id.'.jpg'))
115                                         return True;
116                                 else
117                                         return False;
118                         }
119                         else
120                                 return False;
121                 }
122     }
123    
124 }