Added error handling to listing of users and showing of user information.
[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         $users = new User_Model();
75         $list = $users->list_all_users();
76         $view = new View('api/user_list');
77         $view->list = $list;
78         $view->render(true);
79     }
80     
81     /**
82     * Verify credentials
83     *
84     * @return string Returns string "OK" if login is successful and error otherwise
85     */
86     public function login(){
87         if (apiler::is_authorized()){
88                 print "OK";
89             die;
90         }
91         else
92                 apiler::not_authorized();
93     }
94     
95     
96     /**
97      * Check that supplied avatar is valid and store it
98      * 
99      * @access private
100      * @param array $image Uploaded item found in $_FILES array
101      * @param integer $id User id that will be used as filename
102      * @return boolean Returns TRUE upon succession and FALSE otherwise
103      */
104     private function store_avatar($id){
105         if (isset($_FILES['avatar'])){
106                 $info = getimagesize($_FILES['avatar']['tmp_name']);
107          
108                         if ($_FILES['avatar']['size']<=Kohana::config('api.avatar_max_filesize') AND in_array($info['mime'], Kohana::config('api.avatar_allowed_filetypes')))
109                         {
110                                 if (upload::save('avatar', $id.'.jpg'))
111                                         return True;
112                                 else
113                                         return False;
114                         }
115                         else
116                                 return False;
117                 }
118     }
119    
120 }