ee20517c87eab0a5dbae640f0ccd5e168f847b84
[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     /**
83      * Check that supplied avatar is valid and store it
84      * 
85      * @access private
86      * @param array $image Uploaded item found in $_FILES array
87      * @param integer $id User id that will be used as filename
88      * @return boolean Returns TRUE upon succession and FALSE otherwise
89      */
90     private function store_avatar($id){
91         if (isset($_FILES['avatar'])){
92                 $info = getimagesize($_FILES['avatar']['tmp_name']);
93          
94                         if ($_FILES['avatar']['size']<=Kohana::config('api.avatar_max_filesize') AND in_array($info['mime'], Kohana::config('api.avatar_allowed_filetypes')))
95                         {
96                                 if (upload::save('avatar', $id.'.jpg'))
97                                         return True;
98                                 else
99                                         return False;
100                         }
101                         else
102                                 return False;
103                 }
104     }
105    
106 }