Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00007 class Friendship_Controller extends Controller implements REST { 00008 public $enable_session = true; 00009 public $template = 'template/main'; 00010 00015 public function show() { 00016 if ( 00017 (!isset($_GET['source_id']) && !isset($_GET['source_username'])) || 00018 (!isset($_GET['target_id']) && !isset($_GET['target_username'])) 00019 ) { 00020 00021 return Hub::http_error(401, "Missing required data"); 00022 } 00023 00024 $source = null; 00025 $target = null; 00026 00027 if (isset($_GET['source_id'])) { 00028 if (!is_numeric($_GET['source_id'])) { 00029 return Hub::http_error(401, "Invalid data"); 00030 } 00031 00032 $source = ActiveCache::find('User_Model', $_GET['source_id'], 43200)->sql( 00033 "SELECT * FROM users WHERE id = {$_GET['source_id']} LIMIT 1" 00034 ); 00035 } else { 00036 $username = DatabaseManager::escape($_GET['source_username']); 00037 00038 $source = ActiveCache::find('User_Model', "name:$username", 43200)->sql( 00039 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00040 ); 00041 } 00042 00043 if (isset($_GET['target_id'])) { 00044 if (!is_numeric($_GET['target_id'])) { 00045 return Hub::http_error(401, "Invalid data"); 00046 } 00047 00048 $target = ActiveCache::find('User_Model', $_GET['target_id'], 43200)->sql( 00049 "SELECT * FROM users WHERE id = {$_GET['target_id']} LIMIT 1" 00050 ); 00051 } else { 00052 $username = DatabaseManager::escape($_GET['target_username']); 00053 00054 $target = ActiveCache::find('User_Model', "name:$username", 43200)->sql( 00055 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00056 ); 00057 } 00058 00059 00060 if (!$source->is_loaded()) { 00061 return Hub::http_error(404, "Source user not found"); 00062 } elseif (!$target->is_loaded()) { 00063 return Hub::http_error(404, "Target user not found"); 00064 } 00065 00066 $relationship = new Relationship_Model(); 00067 $relationship->set($source, $target); 00068 00069 View::respond_to(array('xml', 'json', 'jsonp'), function ($format) use($relationship) { 00070 echo $relationship->{"to_$format"}(); 00071 }); 00072 } 00073 00074 public function create() { 00075 if (!$this->session->get('authenticated')) { 00076 return Hub::http_error(403, "Not authorized"); 00077 } 00078 00079 if (!isset($_POST['user_id']) && !isset($_POST['username'])) { 00080 return Hub::http_error(401, "Missing required data"); 00081 } 00082 00083 $user = null; 00084 $curUser = sess::getUser(); 00085 00086 if (isset($_POST['user_id'])) { 00087 if (!is_numeric($_POST['user_id'])) { 00088 return Hub::http_error(401, "Invalid data"); 00089 } 00090 00091 $user_id = $_POST['user_id']; 00092 $user = ActiveCache::find('User_Model', $user_id, 43200)->sql( 00093 "SELECT * FROM users WHERE id = $user_id LIMIT 1" 00094 ); 00095 00096 } else { 00097 $username = DatabaseManager::escape($_POST['username']); 00098 $user = ActiveCache::find('User_Model', "name:{$username}", 43200)->sql( 00099 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00100 ); 00101 } 00102 00103 if(!$user->is_loaded()) { 00104 return Hub::http_error(404, "User not found"); 00105 } 00106 00107 if ($curUser->is_following($user)) { 00108 return Hub::http_error(401, "User is already followed"); 00109 } 00110 00111 $curUser->follow($user); 00112 00113 View::respond_to(array('json','xml'), function ($format) use($user) { 00114 echo $user->{"to_$format"}(); 00115 }); 00116 } 00117 00118 public function destroy() { 00119 if (!$this->session->get('authenticated')) { 00120 return Hub::http_error(403, "Not authorized"); 00121 } 00122 00123 if (!isset($_POST['user_id']) && !isset($_POST['username'])) { 00124 return Hub::http_error(401, "Missing required data"); 00125 } 00126 00127 $user = null; 00128 $curUser = sess::getUser(); 00129 00130 if (isset($_POST['user_id'])) { 00131 if (!is_numeric($_POST['user_id'])) { 00132 return Hub::http_error(401, "Invalid data"); 00133 } 00134 00135 $user_id = $_POST['user_id']; 00136 $user = ActiveCache::find('User_Model', $user_id, 43200)->sql( 00137 "SELECT * FROM users WHERE id = $user_id LIMIT 1" 00138 ); 00139 00140 } else { 00141 $username = DatabaseManager::escape($_POST['username']); 00142 $user = ActiveCache::find('User_Model', "name:{$username}", 43200)->sql( 00143 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00144 ); 00145 } 00146 00147 if(!$user->is_loaded()) { 00148 return Hub::http_error(404, "User not found"); 00149 } 00150 00151 if (!$curUser->is_following($user)) { 00152 return Hub::http_error(401, "Authenticated user is not following the given user"); 00153 } 00154 00155 $curUser->unfollow($user); 00156 00157 View::respond_to(array('json','xml'), function ($format) use($user) { 00158 echo $user->{"to_$format"}(); 00159 }); 00160 } 00161 00162 public function followers($args) { 00163 if (!isset($_GET['user_id']) && !isset($_GET['username']) && !isset($args['user'])) { 00164 return Hub::http_error(401, "Missing required data"); 00165 } 00166 00167 $user = null; 00168 00169 // this is incredibly hackish 00170 if (isset($args['user'])) { 00171 $_GET['username'] = $args['user']; 00172 } 00173 00174 if (isset($_GET['user_id'])) { 00175 if (!is_numeric($_GET['user_id'])) { 00176 return Hub::http_error(401, "Invalid data"); 00177 } 00178 00179 $user_id = $_GET['user_id']; 00180 $user = ActiveCache::find('User_Model', $user_id, 43200)->sql( 00181 "SELECT * FROM users WHERE id = $user_id LIMIT 1" 00182 ); 00183 00184 } else { 00185 $username = DatabaseManager::escape($_GET['username']); 00186 $user = ActiveCache::find('User_Model', "name:{$username}", 43200)->sql( 00187 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00188 ); 00189 } 00190 00191 if(!$user->is_loaded()) { 00192 return Hub::http_error(404, "User not found"); 00193 } 00194 00195 $result = $user->followers(); 00196 $followers = array(); 00197 if (count($result) > 0) { 00198 foreach ($result as $follower_id) { 00199 $follower = ActiveCache::find('User_Model', $follower_id, 43200)->sql( 00200 "SELECT * FROM users WHERE id = $follower_id LIMIT 1" 00201 ); 00202 00203 if ($follower->is_loaded()) { 00204 $followers[] = $follower->limited_object(); 00205 } 00206 } 00207 } 00208 00209 // HACKISH, BUT WHATEVS 00210 $user->columns[] = 'followers'; 00211 $user->public_columns[] = 'followers'; 00212 $user->followers = $followers; 00213 00214 View::respond_to(array('xml', 'json', 'jsonp'), function ($format) use($user) { 00215 echo $user->{"to_$format"}(); 00216 }); 00217 00218 $template = $this->template; 00219 View::respond_to('html', function () use($user, $template) { 00220 $view = new View('user/friendlist'); 00221 $view->type = 'followers'; 00222 $view->user = $user; 00223 $template->content = $view->render(); 00224 echo $template->render(); 00225 }); 00226 } 00227 00228 public function follows($args) { 00229 if (!isset($_GET['user_id']) && !isset($_GET['username']) && !isset($args['user'])) { 00230 return Hub::http_error(401, "Missing required data"); 00231 } 00232 00233 $user = null; 00234 00235 // this is incredibly hackish 00236 if (isset($args['user'])) { 00237 $_GET['username'] = $args['user']; 00238 } 00239 00240 if (isset($_GET['user_id'])) { 00241 if (!is_numeric($_GET['user_id'])) { 00242 return Hub::http_error(401, "Invalid data"); 00243 } 00244 00245 $user_id = $_GET['user_id']; 00246 $user = ActiveCache::find('User_Model', $user_id, 43200)->sql( 00247 "SELECT * FROM users WHERE id = $user_id LIMIT 1" 00248 ); 00249 00250 } else { 00251 $username = DatabaseManager::escape($_GET['username']); 00252 $user = ActiveCache::find('User_Model', "name:{$username}", 43200)->sql( 00253 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00254 ); 00255 } 00256 00257 if(!$user->is_loaded()) { 00258 return Hub::http_error(404, "User not found"); 00259 } 00260 00261 $result = $user->follows(); 00262 $followers = array(); 00263 if (count($result) > 0) { 00264 foreach ($result as $follower_id) { 00265 $follower = ActiveCache::find('User_Model', $follower_id, 43200)->sql( 00266 "SELECT * FROM users WHERE id = $follower_id LIMIT 1" 00267 ); 00268 00269 if ($follower->is_loaded()) { 00270 $followers[] = $follower->limited_object(); 00271 } 00272 } 00273 } 00274 00275 // HACKISH, BUT WHATEVS 00276 $user->columns[] = 'follows'; 00277 $user->public_columns[] = 'follows'; 00278 $user->follows = $followers; 00279 00280 View::respond_to(array('xml', 'json', 'jsonp'), function ($format) use($user) { 00281 echo $user->{"to_$format"}(); 00282 }); 00283 00284 $template = $this->template; 00285 View::respond_to('html', function () use($user, $template) { 00286 $view = new View('user/friendlist'); 00287 $view->type = 'follows'; 00288 $view->user = $user; 00289 $template->content = $view->render(); 00290 echo $template->render(); 00291 }); 00292 } 00293 00294 }