Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00006 class User_Controller extends Controller implements REST { 00007 public $enable_session = true; 00008 public $template = 'template/main'; 00009 00014 public function authorize() { 00015 $username = $_POST['username']; 00016 $password = $_POST['password']; // Must be sha1 hashed already 00017 00018 if (!$username || !$password) { 00019 return Hub::http_error(401, "Bad request"); 00020 } 00021 00022 try { 00023 $user = User_Model::find_user_for_login($username, $password, true); 00024 00025 $access_token = Session_Controller::generate_access_token($user); 00026 00027 View::respond_to('html', function () use($access_token) { 00028 echo $access_token; 00029 }); 00030 00031 View::respond_to('json', function () use($access_token) { 00032 echo json_encode(array('access_token' => $access_token)); 00033 }); 00034 00035 } catch (Exception $e) { 00036 return Hub::http_error(403, "Invalid credentials"); 00037 } 00038 } 00039 00043 public function show($args) { 00044 if (isset($args['username'])) { 00045 $username = $args['username']; 00046 } elseif (isset($_GET['username'])) { 00047 $username = $_GET['username']; 00048 } elseif (isset($_POST['username'])) { 00049 $username = $_POST['username']; 00050 } else { 00051 View::respond_to('html', function () { 00052 echo "Not found"; 00053 }); 00054 00055 View::respond_to(array('json','jsonp','xml'), function () { 00056 Hub::http_error(401, "Missing username"); 00057 }); 00058 00059 return false; 00060 } 00061 00062 $user = ActiveCache::find('User_Model', "name:$username", 43200)->sql( 00063 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00064 ); 00065 00066 if(!$user->is_loaded()) { 00067 View::respond_to('html', function() { 00068 echo "User not found."; 00069 }); 00070 View::respond_to(array('xml','json', 'jsonp'), function() { 00071 Hub::http_error(404, "User not found"); 00072 }); 00073 00074 return false; 00075 } 00076 00077 $template = $this->template; 00078 View::respond_to('html', function() use($user, $template) { 00079 $view = new View('user/show'); 00080 $view->user = $user; 00081 00082 if (sess::isActiveUser($user)) { 00083 $template->active = 'profile'; 00084 } 00085 00086 $template->content = $view->render(); 00087 echo $template->render(); 00088 }); 00089 00090 View::respond_to(array('xml','json','jsonp'), function($format) use($user) { 00091 echo $user->{"to_$format"}(); 00092 }); 00093 } 00094 00098 public function avatar($args) { 00099 $size = $args['size']; 00100 $username = $args['username']; 00101 00102 $user = ActiveCache::find('User_Model', "name:$username", 43200)->sql( 00103 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00104 ); 00105 00106 if (!$user->is_loaded()) { 00107 return Hub::http_error(404, "User not found"); 00108 } 00109 00110 Hub::redirect($user->avatar_url($size)); 00111 } 00112 00116 public function list_users() { 00117 sess::require_login(); 00118 00119 $view = new View('user/list'); 00120 $this->template->title = "Users"; 00121 $this->template->active = 'users'; 00122 $this->template->content = $view->render(); 00123 echo $this->template->render(); 00124 } 00125 00129 public function show_list() { 00130 if (!$this->session->get('authenticated')) { 00131 return Hub::http_error(403, "Unauthorized"); 00132 } 00133 00134 if (!isset($_POST['letter']) || strlen($_POST['letter']) != 1) { 00135 return Hub::http_error(401, "Missing or invalid data"); 00136 } 00137 00138 $letter = strtolower($_POST['letter']); 00139 00140 $r = RedisManager::connection(); 00141 $user_ids = $r->smembers("users:by_first_letter:$letter"); 00142 00143 $users = array(); 00144 foreach ($user_ids as $id) { 00145 $user = ActiveCache::find('User_Model', $id, 43200)->sql( 00146 "SELECT * FROM users WHERE id = $id LIMIT 1" 00147 ); 00148 00149 if ($user->is_loaded()) { 00150 $users[] = $user->limited_object(); 00151 } 00152 } 00153 00154 if (count($users) > 0) { 00155 util::objectSort($users, 'name'); 00156 } 00157 00158 View::respond_to(array('json', 'xml'), function ($format) use($users) { 00159 if ($format == 'xml') { 00160 echo xml::encode_array($users, 'users', 'user'); 00161 } else { 00162 echo json_encode($users); 00163 } 00164 }); 00165 } 00166 00167 }