Campustream 1.0
A social network MQP for WPI
application/models/user.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class User_Model extends ActiveRecord implements SessionObject {
00007         
00008         public $columns = array('id', 'username', 'name', 'bio', 'website', 'college_majors', 'college_minors', 'class_year', 'email', 'password', 'join_date', 'has_avatar', 'avatar_format', 'current_status', 'karma', 'confirmed', 'confirm_code', 'email_enabled');
00009         public $cache_keys = array( 'user/#' => 'id', 'user/name:#' => 'username' );
00010         public $has_many = array();
00011         public $public_columns = array( 'id', 'username', 'name', 'bio', 'website', 'college_majors', 'college_minors', 'class_year', 'join_date', 'current_status', 'follows_count', 'followers_count', 'avatar', 'karma');
00012         
00013         private $twitter, $facebook;
00014         private static $active_user;
00015   
00016         public static function get_session_object($user_id) {
00017                 if(!is_numeric($user_id)) {
00018                         return new User_Model;
00019                 }
00020                 
00021                 if (self::$active_user) {
00022                         return self::$active_user;
00023                 }
00024                 
00025                 $user = ActiveCache::find('User_Model', $user_id, 43200)->sql(
00026                         "SELECT * FROM users WHERE id = $user_id LIMIT 1"
00027                 );
00028                 
00029                 self::$active_user = $user;
00030                 
00031                 return $user;
00032         }
00033         
00034         public function get_session_params() {
00035                 return array('user_id' => $this->id);
00036         }
00037         
00038         public function get_session_secret() {
00039                 return md5($this->password);
00040         }
00041         
00042         public function get_session_key() {
00043                 return $this->id;
00044         }
00045   
00050         public static function find_user_for_login($username, $password, $is_hashed = false) {
00051                 
00052                 $username = DatabaseManager::escape($username);
00053                 $user = ActiveRecord::find('User_Model', "SELECT * FROM users WHERE username = '$username' LIMIT 1");
00054                 
00055                 if (!$is_hashed) {
00056                         $password = sha1($password);
00057                 }
00058                 
00059                 if(!$user->is_loaded()) {
00060                         throw new Exception("Invalid username");
00061                 } elseif($user->password != $password) {
00062                         throw new Exception("Incorrect password");
00063                 } else {
00064                         return $user;
00065                 }
00066         }
00067         
00071         public function facebook() {
00072                 if ($this->facebook === null) {
00073                         $facebook = ActiveCache::find('Userfacebook_Model', "user:{$this->id}", 43200)->sql(
00074                                 "SELECT * FROM userfacebook WHERE user_id = {$this->id} LIMIT 1"
00075                         );
00076                         
00077                         if (!$facebook->is_loaded()) {
00078                                 return new Userfacebook_Model();
00079                         } else {
00080                                 $this->facebook = $facebook;
00081                         }
00082                 }
00083                 
00084                 return $this->facebook;
00085         }
00086         
00090         public function has_facebook() {
00091                 return isset($this->facebook()->access_token);
00092         }
00093         
00097         public function facebook_api() {
00098                 if (!$this->has_facebook()) {
00099                         return false;
00100                 }
00101                 
00102                 $facebook = new Facebook(array(
00103                         'appId' => FACEBOOK_APP_ID,
00104                         'secret' => FACEBOOK_SECRET,
00105                         'cookie' => true
00106                 ));
00107                 
00108                 return $facebook;
00109         }
00110         
00114         public function fb_prepare($data) {
00115                 if (!$this->has_facebook()) {
00116                         return $data;
00117                 }
00118                 
00119                 if (!$data['access_token']) {
00120                         $data['access_token'] = $this->facebook()->access_token;
00121                 }
00122                 
00123                 return $data;
00124         }
00125         
00129         public function twitter() {
00130                 if ($this->twitter === null) {
00131                         $twitter = ActiveCache::find('Usertwitter_Model', "user:{$this->id}", 43200)->sql(
00132                                 "SELECT * FROM usertwitter WHERE user_id = {$this->id} LIMIT 1"
00133                         );
00134                         
00135                         if (!$twitter->is_loaded()) {
00136                                 return new Usertwitter_Model();
00137                         } else {
00138                                 $this->twitter = $twitter;
00139                         }
00140                 }
00141                 
00142                 return $this->twitter;
00143         }
00144         
00148         public function has_twitter() {
00149                 return isset($this->twitter()->oauth_token);
00150         }
00151         
00155         public function twitter_api() {
00156                 if (!$this->has_twitter()) {
00157                         return false;
00158                 }
00159                 
00160                 return twitter::api($this);
00161         }
00162         
00169         public function avatar_url($size = 'medium', $placeholder = false) {
00170                 if ($this->has_avatar && !$placeholder) {
00171                         return 'http://cdn.campustream.com/images/avatars/' . md5('PRobleM?' . $this->username) . "-$size.{$this->avatar_format}";
00172                 } else {
00173                         return "http://cdn.campustream.com/images/avatars/noavatar-$size.jpg";
00174                 }
00175         }
00176         
00180         public function avatar_path($size = 'medium', $format = false) {
00181                 if (!$format) {
00182                         $format = $this->avatar_format;
00183                 }
00184                 
00185                 return $GLOBALS['CDNROOT'] . 'images/avatars/' . md5('PRobleM?' . $this->username) . "-$size.$format";
00186         }
00187         
00188         /* Social graph methods */
00189         
00193         public function follow($user) {
00194             if ($user instanceof User_Model) {
00195                 $user = $user->id;
00196             }
00197             
00198             $flock = FlockDBManager::connection();
00199             $flock->add($this->id, 'follows', $user);
00200         }
00201         
00205         public function unfollow($user) {
00206                 if ($user instanceof User_Model) {
00207                         $user = $user->id;
00208                 }
00209                 
00210                 $flock = FlockDBManager::connection();
00211                 $flock->remove($this->id, 'follows', $user);
00212         }
00213         
00217         public function follows() {
00218                 try {
00219                     $flock = FlockDBManager::connection();
00220                     $cursor = $flock->select($this->id, 'follows', null);
00221                     return iterator_to_array($cursor, false);
00222                 } catch (Exception $e) {
00223                         return array();
00224                 }
00225         }
00226         
00230         public function followers() {
00231                 try {   
00232                     $flock = FlockDBManager::connection();
00233                     $cursor = $flock->select(null, 'follows', $this->id);
00234                     return iterator_to_array($cursor, false);
00235                 } catch (Exception $e) {
00236                         return array();
00237                 }
00238         }
00239         
00243         public function is_followed_by($user) {
00244             if ($user instanceof User_Model) {
00245                 $user = $user->id;
00246             }
00247             
00248             $flock = FlockDBManager::connection();
00249             $cursor = $flock->select($user, 'follows', $this->id);
00250             return (count($cursor->currentPage()) > 0);
00251         }
00252         
00256         public function is_following($user) {
00257             if ($user instanceof User_Model) {
00258                 $user = $user->id;
00259             }
00260             
00261             $flock = FlockDBManager::connection();
00262             $cursor = $flock->select($this->id, 'follows', $user);
00263             return (count($cursor->currentPage()) > 0);
00264         }
00265         
00269         public function follows_count() {
00270                 try {
00271                     $flock = FlockDBManager::connection();
00272                     return $flock->size($this->id, 'follows', null);
00273                 } catch (Exception $e) {
00274                         return 0;
00275                 }
00276         }
00277         
00281         public function followers_count() {
00282                 try {
00283                     $flock = FlockDBManager::connection();
00284                     return $flock->size(null, 'follows', $this->id);
00285                 } catch (Exception $e) {
00286                         return 0;
00287                 }
00288         }
00289         
00290         private function summary() {
00291                 $info = array();
00292                 if (strlen($this->college_majors)) {
00293                         $info[] = "Major(s): {$this->college_majors}";
00294                 }
00295                 if (strlen($this->college_minors)) {
00296                         $info[] = "Minors(s): {$this->college_minors}";
00297                 }
00298                 if ($this->class_year) {
00299                         $info[] = "Class of {$this->class_year}";
00300                 }
00301                 
00302                 return implode(" &mdash; ", $info);
00303         }
00304         
00305         public function __get($key) {
00306                 switch ($key) {
00307                         case 'follows_count': return $this->follows_count();
00308                         case 'followers_count': return $this->followers_count();
00309                         case 'avatar': return $this->avatar_url();
00310                         case 'website_formatted':
00311                                 if(mb_strlen($this->website) > 0) {
00312                                         $website = '<a class="nav-link" rel="nofollow" target="_blank" href="'.str_replace(array('"', "'"), "", $this->website).'">';
00313                                         if(mb_strlen($this->website) > 30) {
00314                                                 $website .= mb_substr($this->website, 0, 30)."...";
00315                                         } else {
00316                                                 $website .= $this->website;
00317                                         }
00318                                         $website .= "</a>";
00319                                         return $website;
00320                                 } else {
00321                                         return '';
00322                                 }
00323                         case 'summary': return $this->summary();
00324                 }
00325                 
00326                 return parent::__get($key);
00327         }
00328         
00329         public function __set($key, $value) {
00330                 if (!isset($this->$key)) {
00331                         $this->$key = $value;
00332                 }
00333                 
00334                 return parent::__set($key, $value);
00335         }
00336 }