1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 * API for registering users and updating profile information
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
10 class Users_Controller extends Controller{
14 * When no parameters are supplied visitor is redirected to project's website
19 public function index(){
20 url::redirect(Kohana::config('api.default_redirect'),301);
28 * @return string Returns "OK" string upon succession and error message otherwise
30 public function register(){
31 $xml = apiler::get_xml();
33 $user = new User_Model($xml->login, $xml->password, $xml->email, $xml->description);
34 $this->store_avatar($user->get_id($xml->login));
37 catch (Exception $e) {
38 echo $e->getMessage() . "\n";
45 * Display user's information
48 * @param string Username that we wish to get information for
49 * @return string Returns information as XML or error message
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');
63 apiler::not_authorized();
68 * View all registered users
71 * @return string Returns XML containing list of all users or error message
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');
82 apiler::not_authorized();
88 * @return string Returns string "OK" if login is successful and error otherwise
90 public function login(){
91 if (apiler::is_authorized()){
96 apiler::not_authorized();
101 * Check that supplied avatar is valid and store it
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
108 private function store_avatar($id){
109 if (isset($_FILES['avatar'])){
110 $info = getimagesize($_FILES['avatar']['tmp_name']);
112 if ($_FILES['avatar']['size']<=Kohana::config('api.avatar_max_filesize') AND in_array($info['mime'], Kohana::config('api.avatar_allowed_filetypes')))
114 if (upload::save('avatar', $id.'.jpg'))