]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/User.php
fix notice -- drop unused return value of variable that isn't initialized :) thx...
[quix0rs-gnu-social.git] / classes / User.php
index 007662131c70a0a44702f1caa114e38f680d1ba5..6ea975202d2c732427a930a54451e647263605f3 100644 (file)
@@ -103,10 +103,7 @@ class User extends Memcached_DataObject
         }
         $toupdate = implode(', ', $parts);
 
-        $table = $this->tableName();
-        if(common_config('db','quote_identifiers')) {
-            $table = '"' . $table . '"';
-        }
+        $table = common_database_tablename($this->tableName());
         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
           ' WHERE id = ' . $this->id;
         $orig->decache();
@@ -117,11 +114,10 @@ class User extends Memcached_DataObject
         return $result;
     }
 
-    function allowed_nickname($nickname)
+    static function allowed_nickname($nickname)
     {
         // XXX: should already be validated for size, content, etc.
-
-        $blacklist = array();
+        $blacklist = common_config('nickname', 'blacklist');
 
         //all directory and file names should be blacklisted
         $d = dir(INSTALLDIR);
@@ -129,8 +125,15 @@ class User extends Memcached_DataObject
             $blacklist[]=$entry;
         }
         $d->close();
-        $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
-        return !in_array($nickname, $merged);
+
+        //all top level names in the router should be blacklisted
+        $router = Router::get();
+        foreach(array_keys($router->m->getPaths()) as $path){
+            if(preg_match('/^\/(.*?)[\/\?]/',$path,$matches)){
+                $blacklist[]=$matches[1];
+            }
+        }
+        return !in_array($nickname, $blacklist);
     }
 
     function getCurrentNotice($dt=null)
@@ -177,6 +180,27 @@ class User extends Memcached_DataObject
         return $result;
     }
 
+    /**
+     * Register a new user account and profile and set up default subscriptions.
+     * If a new-user welcome message is configured, this will be sent.
+     *
+     * @param array $fields associative array of optional properties
+     *              string 'bio'
+     *              string 'email'
+     *              bool 'email_confirmed' pass true to mark email as pre-confirmed
+     *              string 'fullname'
+     *              string 'homepage'
+     *              string 'location' informal string description of geolocation
+     *              float 'lat' decimal latitude for geolocation
+     *              float 'lon' decimal longitude for geolocation
+     *              int 'location_id' geoname identifier
+     *              int 'location_ns' geoname namespace to interpret location_id
+     *              string 'nickname' REQUIRED
+     *              string 'password' (may be missing for eg OpenID registrations)
+     *              string 'code' invite code
+     *              ?string 'uri' permalink to notice; defaults to local notice URL
+     * @return mixed User object or false on failure
+     */
     static function register($fields) {
 
         // MAGICALLY put fields into current scope
@@ -187,7 +211,17 @@ class User extends Memcached_DataObject
 
         $profile->query('BEGIN');
 
+        if(!empty($email))
+        {
+            $email = common_canonical_email($email);
+        }
+
+        $nickname = common_canonical_nickname($nickname);
         $profile->nickname = $nickname;
+        if(! User::allowed_nickname($nickname)){
+            common_log(LOG_WARNING, sprintf("Attempted to register a nickname that is not allowed: %s", $profile->nickname),
+                           __FILE__);
+        }
         $profile->profileurl = common_profile_url($nickname);
 
         if (!empty($fullname)) {
@@ -201,6 +235,15 @@ class User extends Memcached_DataObject
         }
         if (!empty($location)) {
             $profile->location = $location;
+
+            $loc = Location::fromName($location);
+
+            if (!empty($loc)) {
+                $profile->lat         = $loc->lat;
+                $profile->lon         = $loc->lon;
+                $profile->location_id = $loc->location_id;
+                $profile->location_ns = $loc->location_ns;
+            }
         }
 
         $profile->created = common_sql_now();
@@ -230,12 +273,14 @@ class User extends Memcached_DataObject
             }
         }
 
-        $inboxes = common_config('inboxes', 'enabled');
-
-        if ($inboxes === true || $inboxes == 'transitional') {
-            $user->inboxed = 1;
+        if(isset($email_confirmed) && $email_confirmed) {
+            $user->email = $email;
         }
 
+        // This flag is ignored but still set to 1
+
+        $user->inboxed = 1;
+
         $user->created = common_sql_now();
         $user->uri = common_user_uri($user);
 
@@ -246,6 +291,20 @@ class User extends Memcached_DataObject
             return false;
         }
 
+        // Everyone gets an inbox
+
+        $inbox = new Inbox();
+
+        $inbox->user_id = $user->id;
+        $inbox->notice_ids = '';
+
+        $result = $inbox->insert();
+
+        if (!$result) {
+            common_log_db_error($inbox, 'INSERT', __FILE__);
+            return false;
+        }
+
         // Everyone is subscribed to themself
 
         $subscription = new Subscription();
@@ -305,7 +364,7 @@ class User extends Memcached_DataObject
 
         $profile->query('COMMIT');
 
-        if ($email && !$user->email) {
+        if (!empty($email) && !$user->email) {
             mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
         }
 
@@ -324,6 +383,7 @@ class User extends Memcached_DataObject
                                                   common_config('site', 'name'),
                                                   $user->nickname),
                                           'system');
+
             }
         }
 
@@ -436,55 +496,28 @@ class User extends Memcached_DataObject
 
     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
-        $enabled = common_config('inboxes', 'enabled');
-
-        // Complicated code, depending on whether we support inboxes yet
-        // XXX: make this go away when inboxes become mandatory
-
-        if ($enabled === false ||
-            ($enabled == 'transitional' && $this->inboxed == 0)) {
-            $qry =
-              'SELECT notice.* ' .
-              'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
-              'WHERE subscription.subscriber = %d ' .
-              'AND notice.is_local != ' . Notice::GATEWAY;
-            return Notice::getStream(sprintf($qry, $this->id),
-                                     'user:notices_with_friends:' . $this->id,
-                                     $offset, $limit, $since_id, $before_id,
-                                     $order, $since);
-        } else if ($enabled === true ||
-                   ($enabled == 'transitional' && $this->inboxed == 1)) {
-
-            $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false);
-
-            return Notice::getStreamByIds($ids);
-        }
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false);
+        return Notice::getStreamByIds($ids);
     }
 
     function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
-        $enabled = common_config('inboxes', 'enabled');
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true);
+        return Notice::getStreamByIds($ids);
+    }
 
-        // Complicated code, depending on whether we support inboxes yet
-        // XXX: make this go away when inboxes become mandatory
+    function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
+    {
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false);
 
-        if ($enabled === false ||
-            ($enabled == 'transitional' && $this->inboxed == 0)) {
-            $qry =
-              'SELECT notice.* ' .
-              'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
-              'WHERE subscription.subscriber = %d ';
-            return Notice::getStream(sprintf($qry, $this->id),
-                                     'user:notices_with_friends:' . $this->id,
-                                     $offset, $limit, $since_id, $before_id,
-                                     $order, $since);
-        } else if ($enabled === true ||
-                   ($enabled == 'transitional' && $this->inboxed == 1)) {
+        return Notice::getStreamByIds($ids);
+    }
 
-            $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true);
+    function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
+    {
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true);
 
-            return Notice::getStreamByIds($ids);
-        }
+        return Notice::getStreamByIds($ids);
     }
 
     function blowFavesCache()
@@ -516,6 +549,19 @@ class User extends Memcached_DataObject
     {
         // Add a new block record
 
+        // no blocking (and thus unsubbing from) yourself
+
+        if ($this->id == $other->id) {
+            common_log(LOG_WARNING,
+                sprintf(
+                    "Profile ID %d (%s) tried to block his or herself.",
+                    $profile->id,
+                    $profile->nickname
+                )
+            );
+            return false;
+        }
+
         $block = new Profile_block();
 
         // Begin a transaction
@@ -534,15 +580,10 @@ class User extends Memcached_DataObject
 
         // Cancel their subscription, if it exists
 
-        $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
-                                           'subscribed' => $this->id));
+        $otherUser = User::staticGet('id', $other->id);
 
-        if ($sub) {
-            $result = $sub->delete();
-            if (!$result) {
-                common_log_db_error($sub, 'DELETE', __FILE__);
-                return false;
-            }
+        if (!empty($otherUser)) {
+            subs_unsubscribe_to($otherUser, $this->getProfile());
         }
 
         $block->query('COMMIT');
@@ -591,11 +632,13 @@ class User extends Memcached_DataObject
           'WHERE group_member.profile_id = %d ' .
           'ORDER BY group_member.created DESC ';
 
-        if ($offset) {
-            if (common_config('db','type') == 'pgsql') {
-                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-            } else {
-                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+        if ($offset>0 && !is_null($limit)) {
+            if ($offset) {
+                if (common_config('db','type') == 'pgsql') {
+                    $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+                } else {
+                    $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+                }
             }
         }
 
@@ -634,11 +677,7 @@ class User extends Memcached_DataObject
           'ORDER BY subscription.created DESC ';
 
         if ($offset) {
-            if (common_config('db','type') == 'pgsql') {
-                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-            } else {
-                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
-            }
+            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
         }
 
         $profile = new Profile();
@@ -661,11 +700,7 @@ class User extends Memcached_DataObject
           'AND subscription.subscribed != subscription.subscriber ' .
           'ORDER BY subscription.created DESC ';
 
-        if (common_config('db','type') == 'pgsql') {
-            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-        } else {
-            $qry .= ' LIMIT ' . $offset . ', ' . $limit;
-        }
+        $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
 
         $profile = new Profile();
 
@@ -674,38 +709,31 @@ class User extends Memcached_DataObject
         return $profile;
     }
 
-    function hasOpenID()
+    function getDesign()
     {
-        $oid = new User_openid();
-
-        $oid->user_id = $this->id;
-
-        $cnt = $oid->find();
-
-        return ($cnt > 0);
+        return Design::staticGet('id', $this->design_id);
     }
 
-    function getDesign()
+    function hasRight($right)
     {
-        return Design::staticGet('id', $this->design_id);
+        $profile = $this->getProfile();
+        return $profile->hasRight($right);
     }
 
     function delete()
     {
         $profile = $this->getProfile();
-        $profile->delete();
+        if ($profile) {
+            $profile->delete();
+        }
 
         $related = array('Fave',
-                         'User_openid',
                          'Confirm_address',
                          'Remember_me',
                          'Foreign_link',
                          'Invitation',
                          );
-
-        if (common_config('inboxes', 'enabled')) {
-            $related[] = 'Notice_inbox';
-        }
+        Event::handle('UserDeleteRelated', array($this, &$related));
 
         foreach ($related as $cls) {
             $inst = new $cls();
@@ -733,4 +761,168 @@ class User extends Memcached_DataObject
         $block->delete();
         // XXX delete group block? Reset blocker?
     }
+
+    function hasRole($name)
+    {
+        $profile = $this->getProfile();
+        return $profile->hasRole($name);
+    }
+
+    function grantRole($name)
+    {
+        $profile = $this->getProfile();
+        return $profile->grantRole($name);
+    }
+
+    function revokeRole($name)
+    {
+        $profile = $this->getProfile();
+        return $profile->revokeRole($name);
+    }
+
+    function isSandboxed()
+    {
+        $profile = $this->getProfile();
+        return $profile->isSandboxed();
+    }
+
+    function isSilenced()
+    {
+        $profile = $this->getProfile();
+        return $profile->isSilenced();
+    }
+
+    function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
+    {
+        $ids = Notice::stream(array($this, '_repeatedByMeDirect'),
+                              array(),
+                              'user:repeated_by_me:'.$this->id,
+                              $offset, $limit, $since_id, $max_id, null);
+
+        return Notice::getStreamByIds($ids);
+    }
+
+    function _repeatedByMeDirect($offset, $limit, $since_id, $max_id, $since)
+    {
+        $notice = new Notice();
+
+        $notice->selectAdd(); // clears it
+        $notice->selectAdd('id');
+
+        $notice->profile_id = $this->id;
+        $notice->whereAdd('repeat_of IS NOT NULL');
+
+        $notice->orderBy('id DESC');
+
+        if (!is_null($offset)) {
+            $notice->limit($offset, $limit);
+        }
+
+        if ($since_id != 0) {
+            $notice->whereAdd('id > ' . $since_id);
+        }
+
+        if ($max_id != 0) {
+            $notice->whereAdd('id <= ' . $max_id);
+        }
+
+        if (!is_null($since)) {
+            $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
+        }
+
+        $ids = array();
+
+        if ($notice->find()) {
+            while ($notice->fetch()) {
+                $ids[] = $notice->id;
+            }
+        }
+
+        $notice->free();
+        $notice = NULL;
+
+        return $ids;
+    }
+
+    function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
+    {
+        $ids = Notice::stream(array($this, '_repeatsOfMeDirect'),
+                              array(),
+                              'user:repeats_of_me:'.$this->id,
+                              $offset, $limit, $since_id, $max_id, null);
+
+        return Notice::getStreamByIds($ids);
+    }
+
+    function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id, $since)
+    {
+        $qry =
+          'SELECT DISTINCT original.id AS id ' .
+          'FROM notice original JOIN notice rept ON original.id = rept.repeat_of ' .
+          'WHERE original.profile_id = ' . $this->id . ' ';
+
+        if ($since_id != 0) {
+            $qry .= 'AND original.id > ' . $since_id . ' ';
+        }
+
+        if ($max_id != 0) {
+            $qry .= 'AND original.id <= ' . $max_id . ' ';
+        }
+
+        if (!is_null($since)) {
+            $qry .= 'AND original.modified > \'' . date('Y-m-d H:i:s', $since) . '\' ';
+        }
+
+        // NOTE: we sort by fave time, not by notice time!
+
+        $qry .= 'ORDER BY original.id DESC ';
+
+        if (!is_null($offset)) {
+            $qry .= "LIMIT $limit OFFSET $offset";
+        }
+
+        $ids = array();
+
+        $notice = new Notice();
+
+        $notice->query($qry);
+
+        while ($notice->fetch()) {
+            $ids[] = $notice->id;
+        }
+
+        $notice->free();
+        $notice = NULL;
+
+        return $ids;
+    }
+
+    function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
+    {
+        throw new Exception("Not implemented since inbox change.");
+    }
+
+    function shareLocation()
+    {
+        $cfg = common_config('location', 'share');
+
+        if ($cfg == 'always') {
+            return true;
+        } else if ($cfg == 'never') {
+            return false;
+        } else { // user
+            $share = true;
+
+            $prefs = User_location_prefs::staticGet('user_id', $this->id);
+
+            if (empty($prefs)) {
+                $share = common_config('location', 'sharedefault');
+            } else {
+                $share = $prefs->share_location;
+                $prefs->free();
+            }
+
+            return $share;
+        }
+    }
 }