]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
Merge branch 'master' of /var/www/mublog
[quix0rs-gnu-social.git] / classes / User.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 /**
23  * Table Definition for user
24  */
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26 require_once 'Validate.php';
27
28 class User extends Memcached_DataObject
29 {
30     ###START_AUTOCODE
31     /* the code below is auto generated do not remove the above tag */
32
33     public $__table = 'user';                            // table name
34     public $id;                              // int(4)  primary_key not_null
35     public $nickname;                        // varchar(64)  unique_key
36     public $password;                        // varchar(255)
37     public $email;                           // varchar(255)  unique_key
38     public $incomingemail;                   // varchar(255)  unique_key
39     public $emailnotifysub;                  // tinyint(1)   default_1
40     public $emailnotifyfav;                  // tinyint(1)   default_1
41     public $emailnotifynudge;                // tinyint(1)   default_1
42     public $emailnotifymsg;                  // tinyint(1)   default_1
43     public $emailmicroid;                    // tinyint(1)   default_1
44     public $language;                        // varchar(50)
45     public $timezone;                        // varchar(50)
46     public $emailpost;                       // tinyint(1)   default_1
47     public $jabber;                          // varchar(255)  unique_key
48     public $jabbernotify;                    // tinyint(1)
49     public $jabberreplies;                   // tinyint(1)
50     public $jabbermicroid;                   // tinyint(1)   default_1
51     public $updatefrompresence;              // tinyint(1)
52     public $sms;                             // varchar(64)  unique_key
53     public $carrier;                         // int(4)
54     public $smsnotify;                       // tinyint(1)
55     public $smsreplies;                      // tinyint(1)
56     public $smsemail;                        // varchar(255)
57     public $uri;                             // varchar(255)  unique_key
58     public $autosubscribe;                   // tinyint(1)
59     public $urlshorteningservice;            // varchar(50)   default_ur1.ca
60     public $inboxed;                         // tinyint(1)
61     public $created;                         // datetime()   not_null
62     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
63
64     /* Static get */
65     function staticGet($k,$v=null)
66     { return Memcached_DataObject::staticGet('User',$k,$v); }
67
68     /* the code above is auto generated do not remove the tag below */
69     ###END_AUTOCODE
70
71     function getProfile()
72     {
73         return Profile::staticGet('id', $this->id);
74     }
75
76     function isSubscribed($other)
77     {
78         assert(!is_null($other));
79         # XXX: cache results of this query
80         $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
81                                            'subscribed' => $other->id));
82         return (is_null($sub)) ? false : true;
83     }
84
85     # 'update' won't write key columns, so we have to do it ourselves.
86
87     function updateKeys(&$orig)
88     {
89         $parts = array();
90         foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
91             if (strcmp($this->$k, $orig->$k) != 0) {
92                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
93             }
94         }
95         if (count($parts) == 0) {
96             # No changes
97             return true;
98         }
99         $toupdate = implode(', ', $parts);
100
101         $table = $this->tableName();
102         if(common_config('db','quote_identifiers')) {
103             $table = '"' . $table . '"';
104         }
105         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
106           ' WHERE id = ' . $this->id;
107         $orig->decache();
108         $result = $this->query($qry);
109         if ($result) {
110             $this->encache();
111         }
112         return $result;
113     }
114
115     function allowed_nickname($nickname)
116     {
117         # XXX: should already be validated for size, content, etc.
118         static $blacklist = array('rss', 'xrds', 'doc', 'main',
119                                   'settings', 'notice', 'user',
120                                   'search', 'avatar', 'tag', 'tags',
121                                   'api', 'message', 'group', 'groups');
122         $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
123         return !in_array($nickname, $merged);
124     }
125
126     function getCurrentNotice($dt=null)
127     {
128         $profile = $this->getProfile();
129         if (!$profile) {
130             return null;
131         }
132         return $profile->getCurrentNotice($dt);
133     }
134
135     function getCarrier()
136     {
137         return Sms_carrier::staticGet('id', $this->carrier);
138     }
139
140     function subscribeTo($other)
141     {
142         $sub = new Subscription();
143         $sub->subscriber = $this->id;
144         $sub->subscribed = $other->id;
145
146         $sub->created = common_sql_now(); # current time
147
148         if (!$sub->insert()) {
149             return false;
150         }
151
152         return true;
153     }
154
155     function hasBlocked($other)
156     {
157
158         $block = Profile_block::get($this->id, $other->id);
159
160         if (is_null($block)) {
161             $result = false;
162         } else {
163             $result = true;
164             $block->free();
165         }
166
167         return $result;
168     }
169
170     static function register($fields) {
171
172         # MAGICALLY put fields into current scope
173
174         extract($fields);
175
176         $profile = new Profile();
177
178         $profile->query('BEGIN');
179
180         $profile->nickname = $nickname;
181         $profile->profileurl = common_profile_url($nickname);
182
183         if ($fullname) {
184             $profile->fullname = $fullname;
185         }
186         if ($homepage) {
187             $profile->homepage = $homepage;
188         }
189         if ($bio) {
190             $profile->bio = $bio;
191         }
192         if ($location) {
193             $profile->location = $location;
194         }
195
196         $profile->created = common_sql_now();
197
198         $id = $profile->insert();
199
200         if (!$id) {
201             common_log_db_error($profile, 'INSERT', __FILE__);
202             return false;
203         }
204
205         $user = new User();
206
207         $user->id = $id;
208         $user->nickname = $nickname;
209
210         if ($password) { # may not have a password for OpenID users
211             $user->password = common_munge_password($password, $id);
212         }
213
214         # Users who respond to invite email have proven their ownership of that address
215
216         if ($code) {
217             $invite = Invitation::staticGet($code);
218             if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
219                 $user->email = $invite->address;
220             }
221         }
222
223         $inboxes = common_config('inboxes', 'enabled');
224
225         if ($inboxes === true || $inboxes == 'transitional') {
226             $user->inboxed = 1;
227         }
228
229         $user->created = common_sql_now();
230         $user->uri = common_user_uri($user);
231
232         $result = $user->insert();
233
234         if (!$result) {
235             common_log_db_error($user, 'INSERT', __FILE__);
236             return false;
237         }
238
239         # Everyone is subscribed to themself
240
241         $subscription = new Subscription();
242         $subscription->subscriber = $user->id;
243         $subscription->subscribed = $user->id;
244         $subscription->created = $user->created;
245
246         $result = $subscription->insert();
247
248         if (!$result) {
249             common_log_db_error($subscription, 'INSERT', __FILE__);
250             return false;
251         }
252
253         if ($email && !$user->email) {
254
255             $confirm = new Confirm_address();
256             $confirm->code = common_confirmation_code(128);
257             $confirm->user_id = $user->id;
258             $confirm->address = $email;
259             $confirm->address_type = 'email';
260
261             $result = $confirm->insert();
262             if (!$result) {
263                 common_log_db_error($confirm, 'INSERT', __FILE__);
264                 return false;
265             }
266         }
267
268         if ($code && $user->email) {
269             $user->emailChanged();
270         }
271
272         $profile->query('COMMIT');
273
274         if ($email && !$user->email) {
275             mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
276         }
277
278         return $user;
279     }
280
281     # Things we do when the email changes
282
283     function emailChanged()
284     {
285
286         $invites = new Invitation();
287         $invites->address = $this->email;
288         $invites->address_type = 'email';
289
290         if ($invites->find()) {
291             while ($invites->fetch()) {
292                 $other = User::staticGet($invites->user_id);
293                 subs_subscribe_to($other, $this);
294             }
295         }
296     }
297
298     function hasFave($notice)
299     {
300         $cache = common_memcache();
301
302         # XXX: Kind of a hack.
303         if ($cache) {
304             # This is the stream of favorite notices, in rev chron
305             # order. This forces it into cache.
306             $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
307             $cnt = 0;
308             while ($faves->fetch()) {
309                 if ($faves->id < $notice->id) {
310                     # If we passed it, it's not a fave
311                     return false;
312                 } else if ($faves->id == $notice->id) {
313                     # If it matches a cached notice, then it's a fave
314                     return true;
315                 }
316                 $cnt++;
317             }
318             # If we're not past the end of the cache window,
319             # then the cache has all available faves, so this one
320             # is not a fave.
321             if ($cnt < NOTICE_CACHE_WINDOW) {
322                 return false;
323             }
324             # Otherwise, cache doesn't have all faves;
325             # fall through to the default
326         }
327         $fave = Fave::pkeyGet(array('user_id' => $this->id,
328                                     'notice_id' => $notice->id));
329         return ((is_null($fave)) ? false : true);
330     }
331     function mutuallySubscribed($other)
332     {
333         return $this->isSubscribed($other) &&
334           $other->isSubscribed($this);
335     }
336
337         function mutuallySubscribedUsers()
338         {
339
340         # 3-way join; probably should get cached
341         $qry = 'SELECT user.* ' .
342           'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
343           'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
344           'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
345           'ORDER BY user.nickname';
346         $user = new User();
347         $user->query(sprintf($qry, $this->id, $this->id));
348
349         return $user;
350     }
351
352     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
353     {
354         $qry =
355           'SELECT notice.* ' .
356           'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
357           'WHERE reply.profile_id = %d ';
358         return Notice::getStream(sprintf($qry, $this->id),
359                                  'user:replies:'.$this->id,
360                                  $offset, $limit, $since_id, $before_id, null, $since);
361     }
362
363         function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
364         {
365         $profile = $this->getProfile();
366         if (!$profile) {
367             return null;
368         } else {
369             return $profile->getNotices($offset, $limit, $since_id, $before_id);
370         }
371     }
372
373       function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
374       {
375         $qry =
376           'SELECT notice.* ' .
377           'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
378           'WHERE fave.user_id = %d ';
379         return Notice::getStream(sprintf($qry, $this->id),
380                                  'user:faves:'.$this->id,
381                                  $offset, $limit);
382     }
383
384         function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
385         {
386         $enabled = common_config('inboxes', 'enabled');
387
388         # Complicated code, depending on whether we support inboxes yet
389         # XXX: make this go away when inboxes become mandatory
390
391         if ($enabled === false ||
392             ($enabled == 'transitional' && $this->inboxed == 0)) {
393             $qry =
394               'SELECT notice.* ' .
395               'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
396               'WHERE subscription.subscriber = %d ';
397             $order = null;
398         } else if ($enabled === true ||
399                ($enabled == 'transitional' && $this->inboxed == 1)) {
400
401             $qry =
402               'SELECT notice.* ' .
403               'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
404               'WHERE notice_inbox.user_id = %d ';
405             # NOTE: we override ORDER
406             $order = null;
407         }
408         return Notice::getStream(sprintf($qry, $this->id),
409                                  'user:notices_with_friends:' . $this->id,
410                                  $offset, $limit, $since_id, $before_id,
411                                  $order, $since);
412     }
413
414         function blowFavesCache()
415         {
416         $cache = common_memcache();
417         if ($cache) {
418             # Faves don't happen chronologically, so we need to blow
419             # ;last cache, too
420             $cache->delete(common_cache_key('user:faves:'.$this->id));
421             $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
422         }
423     }
424
425         function getSelfTags()
426         {
427         return Profile_tag::getTags($this->id, $this->id);
428     }
429
430         function setSelfTags($newtags)
431         {
432         return Profile_tag::setTags($this->id, $this->id, $newtags);
433     }
434
435     function block($other)
436     {
437
438         # Add a new block record
439
440         $block = new Profile_block();
441
442         # Begin a transaction
443
444         $block->query('BEGIN');
445
446         $block->blocker = $this->id;
447         $block->blocked = $other->id;
448
449         $result = $block->insert();
450
451         if (!$result) {
452             common_log_db_error($block, 'INSERT', __FILE__);
453             return false;
454         }
455
456         # Cancel their subscription, if it exists
457
458         $sub = Subscription::pkeyGet(array('subscriber' => $other->id,
459                                            'subscribed' => $this->id));
460
461         if ($sub) {
462             $result = $sub->delete();
463             if (!$result) {
464                 common_log_db_error($sub, 'DELETE', __FILE__);
465                 return false;
466             }
467         }
468
469         $block->query('COMMIT');
470
471         return true;
472     }
473
474     function unblock($other)
475     {
476
477         # Get the block record
478
479         $block = Profile_block::get($this->id, $other->id);
480
481         if (!$block) {
482             return false;
483         }
484
485         $result = $block->delete();
486
487         if (!$result) {
488             common_log_db_error($block, 'DELETE', __FILE__);
489             return false;
490         }
491
492         return true;
493     }
494
495     function isMember($group)
496     {
497         $profile = $this->getProfile();
498         return $profile->isMember($group);
499     }
500
501     function isAdmin($group)
502     {
503         $profile = $this->getProfile();
504         return $profile->isAdmin($group);
505     }
506
507     function getGroups($offset=0, $limit=null)
508     {
509         $qry =
510           'SELECT user_group.* ' .
511           'FROM user_group JOIN group_member '.
512           'ON user_group.id = group_member.group_id ' .
513           'WHERE group_member.profile_id = %d ' .
514           'ORDER BY group_member.created DESC ';
515
516         if ($offset) {
517             if (common_config('db','type') == 'pgsql') {
518                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
519             } else {
520                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
521             }
522         }
523
524         $groups = new User_group();
525
526         $cnt = $groups->query(sprintf($qry, $this->id));
527
528         return $groups;
529     }
530
531     function getSubscriptions($offset=0, $limit=null)
532     {
533         $qry =
534           'SELECT profile.* ' .
535           'FROM profile JOIN subscription ' .
536           'ON profile.id = subscription.subscribed ' .
537           'WHERE subscription.subscriber = %d ' .
538           'AND subscription.subscribed != subscription.subscriber ' .
539           'ORDER BY subscription.created DESC ';
540
541         if (common_config('db','type') == 'pgsql') {
542             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
543         } else {
544             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
545         }
546
547         $profile = new Profile();
548
549         $profile->query(sprintf($qry, $this->id));
550
551         return $profile;
552     }
553
554     function getSubscribers($offset=0, $limit=null)
555     {
556         $qry =
557           'SELECT profile.* ' .
558           'FROM profile JOIN subscription ' .
559           'ON profile.id = subscription.subscriber ' .
560           'WHERE subscription.subscribed = %d ' .
561           'AND subscription.subscribed != subscription.subscriber ' .
562           'ORDER BY subscription.created DESC ';
563
564         if ($offset) {
565             if (common_config('db','type') == 'pgsql') {
566                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
567             } else {
568                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
569             }
570         }
571
572         $profile = new Profile();
573
574         $cnt = $profile->query(sprintf($qry, $this->id));
575
576         return $profile;
577     }
578
579     function getTaggedSubscribers($tag, $offset=0, $limit=null)
580     {
581         $qry =
582           'SELECT profile.* ' .
583           'FROM profile JOIN subscription ' .
584           'ON profile.id = subscription.subscriber ' .
585           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
586           'AND profile_tag.tagger = subscription.subscribed) ' .
587           'WHERE subscription.subscribed = %d ' .
588           'AND profile_tag.tag = "%s" ' .
589           'AND subscription.subscribed != subscription.subscriber ' .
590           'ORDER BY subscription.created DESC ';
591
592         if ($offset) {
593             if (common_config('db','type') == 'pgsql') {
594                 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
595             } else {
596                 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
597             }
598         }
599
600         $profile = new Profile();
601
602         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
603
604         return $profile;
605     }
606
607     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
608     {
609         $qry =
610           'SELECT profile.* ' .
611           'FROM profile JOIN subscription ' .
612           'ON profile.id = subscription.subscribed ' .
613           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
614           'AND profile_tag.tagger = subscription.subscriber) ' .
615           'WHERE subscription.subscriber = %d ' .
616           'AND profile_tag.tag = "%s" ' .
617           'AND subscription.subscribed != subscription.subscriber ' .
618           'ORDER BY subscription.created DESC ';
619
620         if (common_config('db','type') == 'pgsql') {
621             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
622         } else {
623             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
624         }
625
626         $profile = new Profile();
627
628         $profile->query(sprintf($qry, $this->id, $tag));
629
630         return $profile;
631     }
632 }