Campustream 1.0
A social network MQP for WPI
application/controllers/account/settings.php
Go to the documentation of this file.
00001 <?
00005 class Settings_Controller extends Controller implements REST {
00006         public $enable_session = true;
00007         public $template = 'template/main';
00008         
00009         public function index($args) {
00010                 if (!$this->session->get('authenticated')) {
00011                         return Hub::redirect('/login');
00012                 }
00013                 
00014                 $facebook = new Facebook(array(
00015                         'appId' => FACEBOOK_APP_ID,
00016                         'secret' => FACEBOOK_SECRET,
00017                         'cookie' => true
00018                 ));
00019                 
00020                 $ct = new View('account/settings/_settings_template');
00021                 $ct->active_tab = $args['page'];
00022                 
00023                 $view = new View("account/settings/{$args['page']}");
00024                 $view->user = sess::getUser();
00025                 $view->facebook = $facebook;
00026                 $ct->content = $view->render();
00027                 
00028                 $this->template->active = 'settings';
00029                 $this->template->enable_fbml = true;
00030                 $this->template->content = $ct->render();
00031                 echo $this->template->render();
00032         }
00033         
00034         public function service_hooks() {
00035                 if (!$this->session->get('authenticated')) {
00036                         return;
00037                 }
00038 
00039                 if (!isset($_POST['service']) || !isset($_POST['type']) || !isset($_POST['enable'])) {
00040                         return;
00041                 }
00042 
00043                 $user = sess::getUser();
00044 
00045                 if (!isset($_POST['form_auth']) || $_POST['form_auth'] != md5('zOmg4tehcoolz!' . $user->username)) {
00046                         return;
00047                 }
00048 
00049                 $service = $_POST['service'];
00050                 $type = $_POST['type'];
00051                 $enable = $_POST['enable'];
00052                 
00053                 if ($service != 'facebook' && $service != 'twitter') {
00054                         return;
00055                 }
00056                 if ($type != 'import' && $type != 'export') {
00057                         return;
00058                 }
00059                 
00060                 if ($service == 'twitter') {
00061                         if (!$user->has_twitter()) {
00062                                 return;
00063                         }
00064                         
00065                         $user->twitter()->{"{$type}_enabled"} = ($enable == 'true' ? 1 : 0);
00066                         $user->twitter()->save();
00067                         
00068                         echo json_encode(array('success' => 1));
00069                 } elseif ($service == 'facebook') {
00070                         if (!$user->has_facebook()) {
00071                                 return;
00072                         }
00073                         
00074                         $user->facebook()->{"{$type}_enabled"} = ($enable == 'true' ? 1 : 0);
00075                         $user->facebook()->save();
00076                         
00077                         echo json_encode(array('success' => 1));
00078                 }
00079         }
00080         
00081         public function email() {
00082                 if (!isset($_POST['form_auth']) || $_POST['form_auth'] != md5("fjLDI*YF:S:F&**TF" . sess::getUser()->username)) {
00083                         return;
00084                 }
00085                 
00086                 $enable = ($_POST['enable'] == "false" ? 0 : 1);
00087                 
00088                 $user = sess::getUser();
00089                 $user->email_enabled = $enable;
00090                 $user->save();
00091                 
00092                 echo json_encode(array("success" => 1));
00093         }
00094         
00095         public function profile_photo($args) {
00096                 sess::require_login();
00097                 
00098                 $user = sess::getUser();
00099                 
00100                 if ($args['action'] == 'update') {
00101                         $photo = $_FILES['photo'];
00102                         $type = exif_imagetype($photo['tmp_name']);
00103 
00104                         switch ($type) {
00105                                 case 1: $type = 'gif'; break;
00106                                 case 2: $type = 'jpg'; break;
00107                                 case 3: $type = 'png'; break;
00108                                 default: $type = false;
00109                         }
00110                         
00111                         if (!$type) {
00112                                 return Hub::redirect('/settings/profile?error=invalid_image');
00113                         }
00114                         
00115                         $fsize = filesize($photo['tmp_name']);
00116                         if (($fsize / 1024) > 3072) { // 3MB max
00117                                 return Hub::redirect('/settings/profile?error=image_too_large');
00118                         }
00119                         
00120                         copy($photo['tmp_name'], $user->avatar_path('full', $type));
00121                         chmod($user->avatar_path('full', $type), 0777);
00122                         
00123                         $large = new Gmagick();
00124                         $large->readImage($photo['tmp_name']);
00125                         $large->cropthumbnailimage(250, 250);
00126                         $large->write($user->avatar_path('large', $type));
00127                         @chmod($user->avatar_path('large', $type), 0777);
00128                         
00129                         $medium = new Gmagick();
00130                         $medium->readImage($photo['tmp_name']);
00131                         $medium->cropthumbnailimage(128, 128);
00132                         $medium->write($user->avatar_path('medium', $type));
00133                         @chmod($user->avatar_path('medium', $type), 0777);
00134                         
00135                         $small = new Gmagick();
00136                         $small->readImage($photo['tmp_name']);
00137                         $small->cropthumbnailimage(75, 75);
00138                         $small->write($user->avatar_path('small', $type));
00139                         @chmod($user->avatar_path('small', $type), 0777);
00140                         
00141                         $user->has_avatar = 1;
00142                         $user->avatar_format = $type;
00143                         $user->save();
00144                         
00145                         return Hub::redirect('/settings/profile');
00146                         
00147                 } elseif ($args['action'] == 'remove') {
00148                         @unlink($user->avatar_path('full'));
00149                         @unlink($user->avatar_path('large'));
00150                         @unlink($user->avatar_path('medium'));
00151                         @unlink($user->avatar_path('small'));
00152                         
00153                         $user->has_avatar = 0;
00154                         $user->avatar_format = '';
00155                         $user->save();
00156                         
00157                         return Hub::redirect('/settings/profile');
00158                 }
00159         }
00160         
00161         public function update_profile($args) {
00162                 sess::require_login();
00163                 
00164                 // hackish but it works
00165                 foreach($_POST as $key=>$val) {
00166                         $_POST[$key] = trim($val);
00167                 }
00168                 
00169                 if (mb_strlen($_POST['bio']) > 160) {
00170                         $_POST['bio'] = mb_substr($_POST['bio'], 0, 160);
00171                 }
00172                 
00173                 $user = sess::getUser();
00174                 
00175                 $r = RedisManager::connection();
00176                 $first_letter = strtolower(substr($user->name, 0, 1));
00177                 $r->srem("users:by_first_letter:$first_letter", $user->id);
00178                 
00179                 $user->name = htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8');
00180                 //$user->email = $_POST['email'];
00181                 $user->website = str_replace(array('<', '>'), '', $_POST['website']);
00182                 $user->bio = htmlspecialchars($_POST['bio'], ENT_QUOTES, 'UTF-8');
00183                 $user->class_year = is_numeric($_POST['class_year']) && $_POST['class_year'] > 0 ? $_POST['class_year'] : '';
00184                 $user->college_majors = htmlspecialchars($_POST['college_majors'], ENT_QUOTES, 'UTF-8');
00185                 $user->college_minors = htmlspecialchars($_POST['college_minors'], ENT_QUOTES, 'UTF-8');
00186                 
00187                 $user->save();
00188                 
00189                 $first_letter = strtolower(substr($user->name, 0, 1));
00190                 $r->sadd("users:by_first_letter:$first_letter", $user->id);
00191                 
00192                 Hub::redirect('/settings/profile');
00193                 
00194         }
00195 }