Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00006 class Status_Controller extends Controller implements REST { 00007 public $enable_session = true; 00008 public $template = 'template/main'; 00009 00013 public function public_timeline() { 00014 $this->output_statuses('statuses:public_timeline'); 00015 } 00016 00020 public function user_timeline() { 00021 if (!isset($_GET['username'])) { 00022 return Hub::http_error(401, "Missing required parameter: username"); 00023 } 00024 00025 $username = DatabaseManager::escape(trim($_GET['username'])); 00026 if (strlen($username) === 0) { 00027 return Hub::http_error(401, "Missing required parameter: username"); 00028 } 00029 00030 $user = ActiveCache::find('User_Model', "name:$username", 43200)->sql( 00031 "SELECT * FROM users WHERE username = '$username' LIMIT 1" 00032 ); 00033 00034 if (!$user->is_loaded()) { 00035 return Hub::http_error(404, "User not found"); 00036 } 00037 00038 $this->output_statuses("statuses:user_timeline:{$user->id}"); 00039 } 00040 00045 public function friend_timeline() { 00046 if (!$this->session->get('authenticated')) { 00047 return Hub::http_error(403, "Not authorized"); 00048 } 00049 00050 $user = sess::getUser(); 00051 $this->output_statuses("statuses:friend_timeline:{$user->id}"); 00052 } 00053 00057 private function output_statuses($r_key) { 00058 $r = RedisManager::connection(); 00059 $status_ids = $r->lrange($r_key, 0, Status_Model::$list_max_length); 00060 00061 if (isset($_GET['count']) && is_numeric($_GET['count']) && $_GET['count'] < count($status_ids)) { 00062 $status_ids = array_slice($status_ids, 0, $_GET['count']); 00063 } 00064 00065 $comments = false; 00066 if (isset($_GET['comments']) && $_GET['comments']) { 00067 $comments = true; 00068 } 00069 00070 if (count($status_ids) > 0) { 00071 $statuses = array(); 00072 foreach ($status_ids as $status) { 00073 $status_id = stream::id($status); 00074 $type = stream::type($status); 00075 00076 $status = ActiveCache::find('Status_Model', $status_id, 43200)->sql( 00077 "SELECT * FROM statuses WHERE id = $status_id LIMIT 1" 00078 ); 00079 00080 if (!$status->is_loaded() || $status->deleted) { 00081 continue; 00082 } 00083 00084 // HACK - UTC FIX 00085 $status->post_date = date('Y-m-d H:i:s', strtotime($status->post_date) - (4 * 60 * 60)); 00086 00087 if ($comments) { 00088 $status->load_comments(); 00089 foreach ($status->comments as $i => $comment) { 00090 // HACK AGAIN, MAYBE SHOULD CENTRALIZE THIS? 00091 $comment->post_date = date('Y-m-d H:i:s', strtotime($comment->post_date) - (4 * 60 * 60)); 00092 $status->comments[$i] = $comment; 00093 } 00094 } 00095 00096 $statuses[] = $status; 00097 } 00098 00099 View::respond_to(array('json', 'jsonp', 'xml'), function ($format) use($statuses) { 00100 if ($format == 'json' || $format == 'jsonp') { 00101 foreach ($statuses as $i=>$status) { 00102 $status->load_user(); 00103 $statuses[$i] = $status->limited_object(); 00104 } 00105 00106 if ($format == 'jsonp') { 00107 if (isset($_GET['callback'])) { 00108 $callback = $_GET['callback']; 00109 } else { 00110 $callback = 'campustreamCallback'; 00111 } 00112 00113 echo "$callback(" . json_encode($statuses) . ");"; 00114 } else { 00115 echo json_encode($statuses); 00116 } 00117 } else { 00118 // implement XML 00119 } 00120 }); 00121 } else { 00122 View::respond_to(array('json', 'jsonp', 'xml'), function ($format) { 00123 if ($format == 'json') { 00124 echo json_encode(array()); 00125 return; 00126 } elseif ($format == 'jsonp') { 00127 if (isset($_GET['callback'])) { 00128 $callback = $_GET['callback']; 00129 } else { 00130 $callback = 'campustreamCallback'; 00131 } 00132 00133 echo "$callback(" . json_encode(array()) . ");"; 00134 } 00135 }); 00136 } 00137 } 00138 00144 public function show($args) { 00145 $id = null; 00146 00147 if (isset($args['id'])) { 00148 $id = base_convert($args['id'], 36, 10); 00149 } elseif (isset($_GET['id']) && is_numeric($_GET['id'])) { 00150 $id = $_GET['id']; 00151 } else { 00152 return Hub::http_error(401, "Bad request"); 00153 } 00154 00155 $status = ActiveCache::find('Status_Model', $id, 43200)->sql( 00156 "SELECT * FROM statuses WHERE id = $id LIMIT 1" 00157 ); 00158 00159 if (!$status->is_loaded() || $status->deleted != 0) { 00160 View::respond_to(array('json', 'xml'), function () { 00161 return Hub::http_error(404, "Status not found"); 00162 }); 00163 View::respond_to('html', function () { 00164 return Hub::redirect('/'); 00165 }); 00166 00167 return false; 00168 } 00169 00170 $status->load_user(); 00171 $status->load_comments(); 00172 00173 View::respond_to(array('json','jsonp','xml'), function ($format) use($status) { 00174 echo $status->{"to_$format"}(); 00175 }); 00176 00177 View::respond_to('html', function () use($status) { 00178 $status->load_content(); 00179 00180 $view = new View('statuses/single'); 00181 $view->status = $status; 00182 echo $view->render(); 00183 }); 00184 } 00185 00189 public function comments() { 00190 $id = $_GET['status_id']; 00191 00192 if (!is_numeric($id)) { 00193 return Hub::http_error(401, "Bad request"); 00194 } 00195 00196 $status = ActiveCache::find('Status_Model', $id, 43200)->sql( 00197 "SELECT * FROM statuses WHERE id = $id LIMIT 1" 00198 ); 00199 00200 if (!$status->is_loaded()) { 00201 return Hub::http_error(404, "Status not found"); 00202 } 00203 00204 $status->load_comments(); 00205 00206 if (count($status->comments) > 0) { 00207 for ($i = 0; $i < count($status->comments); $i++) { 00208 $status->comments[$i]->load_user(); 00209 } 00210 } 00211 00212 View::respond_to(array('json', 'jsonp', 'xml'), function ($format) use($status) { 00213 echo $status->{"to_$format"}(); 00214 }); 00215 } 00216 00221 public function update() { 00222 if (!$this->session->get('authenticated')) { 00223 return Hub::http_error(403, "Not authenticated"); 00224 } 00225 00226 $source = 'site'; // temporary 00227 $message = trim($_POST['message']); 00228 00229 if (!$message || !strlen($message)) { 00230 return Hub::http_error(401, "Missing status message"); 00231 } 00232 00233 $type = 'text'; 00234 if (isset($_POST['type'])) { 00235 $type = $_POST['type']; 00236 } 00237 00238 $public = true; 00239 if (isset($_POST['public'])) { 00240 $public = $_POST['public']; 00241 } 00242 00243 $user = sess::getUser(); 00244 00245 $status = new Status_Model(); 00246 $status->user_id = $user->id; 00247 $status->source = $source; 00248 $status->message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); 00249 $status->post_date = ActiveRecord::NOW(); 00250 $status->type = $type; 00251 $status->is_public = $public; 00252 00253 $status->save(); 00254 00255 View::respond_to(array('json', 'xml'), function($format) use($status) { 00256 $status->load_user(); 00257 echo $status->{"to_$format"}(); 00258 }); 00259 } 00260 00265 public function delete() { 00266 if (!$this->session->get('authenticated')) { 00267 return Hub::http_error(403, "Not authorized"); 00268 } 00269 00270 $status_id = $_POST['id']; 00271 if (!$status_id || !is_numeric($status_id)) { 00272 return Hub::http_error(401, "Missing or invalid status ID"); 00273 } 00274 00275 $status = ActiveCache::find('Status_Model', $status_id, 43200)->sql( 00276 "SELECT * FROM statuses WHERE id = $status_id LIMIT 1" 00277 ); 00278 00279 if (!$status->is_loaded()) { 00280 return Hub::http_error(404, "Status not found"); 00281 } 00282 00283 if ($status->user_id != sess::getUserID()) { 00284 return Hub::http_error(403, "Not authorized"); 00285 } 00286 00287 $status->delete(); 00288 00289 View::respond_to(array('json', 'xml'), function () { 00290 return true; 00291 }); 00292 } 00293 }