]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
55c0f45768b1f1008c4254b8b969fdde91273427
[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 $emailnotifymsg;                  // tinyint(1)   default_1
42     public $emailmicroid;                    // tinyint(1)   default_1
43     public $language;                        // varchar(50)  
44     public $timezone;                        // varchar(50)  
45     public $emailpost;                       // tinyint(1)   default_1
46     public $jabber;                          // varchar(255)  unique_key
47     public $jabbernotify;                    // tinyint(1)  
48     public $jabberreplies;                   // tinyint(1)  
49     public $jabbermicroid;                   // tinyint(1)   default_1
50     public $updatefrompresence;              // tinyint(1)  
51     public $sms;                             // varchar(64)  unique_key
52     public $carrier;                         // int(4)  
53     public $smsnotify;                       // tinyint(1)  
54     public $smsreplies;                      // tinyint(1)  
55     public $smsemail;                        // varchar(255)  
56     public $uri;                             // varchar(255)  unique_key
57     public $autosubscribe;                   // tinyint(1)  
58     public $created;                         // datetime()   not_null
59     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
60     public $inboxed;                         // tinyint(1)  
61     public $urlshorteningservice;            // varchar(50)   default_ur1.ca
62
63     /* Static get */
64     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
65
66     /* the code above is auto generated do not remove the tag below */
67     ###END_AUTOCODE
68
69         function getProfile() {
70                 return Profile::staticGet('id', $this->id);
71         }
72
73         function isSubscribed($other) {
74                 assert(!is_null($other));
75                 # XXX: cache results of this query
76                 $sub = Subscription::pkeyGet(array('subscriber' => $this->id,
77                                                                                    'subscribed' => $other->id));
78                 return (is_null($sub)) ? false : true;
79         }
80
81         # 'update' won't write key columns, so we have to do it ourselves.
82
83         function updateKeys(&$orig) {
84                 $parts = array();
85                 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
86                         if (strcmp($this->$k, $orig->$k) != 0) {
87                                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
88                         }
89                 }
90                 if (count($parts) == 0) {
91                         # No changes
92                         return true;
93                 }
94                 $toupdate = implode(', ', $parts);
95
96                 $table = $this->tableName();
97                 if(common_config('db','quote_identifiers')) {
98                         $table = '"' . $table . '"';
99                 }
100                 $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
101                   ' WHERE id = ' . $this->id;
102                 $orig->decache();
103                 $result = $this->query($qry);
104                 if ($result) {
105                         $this->encache();
106                 }
107                 return $result;
108         }
109
110         function allowed_nickname($nickname) {
111                 # XXX: should already be validated for size, content, etc.
112                 static $blacklist = array('rss', 'xrds', 'doc', 'main',
113                                                                   'settings', 'notice', 'user',
114                                                                   'search', 'avatar', 'tag', 'tags',
115                                                                   'api', 'message');
116                 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
117                 return !in_array($nickname, $merged);
118         }
119
120         function getCurrentNotice($dt=NULL) {
121                 $profile = $this->getProfile();
122                 if (!$profile) {
123                         return NULL;
124                 }
125                 return $profile->getCurrentNotice($dt);
126         }
127
128         function getCarrier() {
129                 return Sms_carrier::staticGet('id', $this->carrier);
130         }
131
132         function subscribeTo($other) {
133                 $sub = new Subscription();
134                 $sub->subscriber = $this->id;
135                 $sub->subscribed = $other->id;
136
137                 $sub->created = common_sql_now(); # current time
138
139                 if (!$sub->insert()) {
140                         return false;
141                 }
142
143                 return true;
144         }
145
146         static function register($fields) {
147
148                 # MAGICALLY put fields into current scope
149
150                 extract($fields);
151
152                 $profile = new Profile();
153
154                 $profile->query('BEGIN');
155
156                 $profile->nickname = $nickname;
157                 $profile->profileurl = common_profile_url($nickname);
158
159                 if ($fullname) {
160                         $profile->fullname = $fullname;
161                 }
162                 if ($homepage) {
163                         $profile->homepage = $homepage;
164                 }
165                 if ($bio) {
166                         $profile->bio = $bio;
167                 }
168                 if ($location) {
169                         $profile->location = $location;
170                 }
171
172                 $profile->created = common_sql_now();
173
174                 $id = $profile->insert();
175
176                 if (!$id) {
177                         common_log_db_error($profile, 'INSERT', __FILE__);
178                     return FALSE;
179                 }
180
181                 $user = new User();
182
183                 $user->id = $id;
184                 $user->nickname = $nickname;
185
186                 if ($password) { # may not have a password for OpenID users
187                         $user->password = common_munge_password($password, $id);
188                 }
189
190                 # Users who respond to invite email have proven their ownership of that address
191
192                 if ($code) {
193                         $invite = Invitation::staticGet($code);
194                         if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
195                                 $user->email = $invite->address;
196                         }
197                 }
198
199                 $inboxes = common_config('inboxes', 'enabled');
200                 
201                 if ($inboxes === true || $inboxes == 'transitional') {
202                         $user->inboxed = 1;
203                 }
204                 
205                 $user->created = common_sql_now();
206                 $user->uri = common_user_uri($user);
207
208                 $result = $user->insert();
209
210                 if (!$result) {
211                         common_log_db_error($user, 'INSERT', __FILE__);
212                         return FALSE;
213                 }
214
215                 # Everyone is subscribed to themself
216
217                 $subscription = new Subscription();
218                 $subscription->subscriber = $user->id;
219                 $subscription->subscribed = $user->id;
220                 $subscription->created = $user->created;
221
222                 $result = $subscription->insert();
223
224                 if (!$result) {
225                         common_log_db_error($subscription, 'INSERT', __FILE__);
226                         return FALSE;
227                 }
228
229                 if ($email && !$user->email) {
230
231                         $confirm = new Confirm_address();
232                         $confirm->code = common_confirmation_code(128);
233                         $confirm->user_id = $user->id;
234                         $confirm->address = $email;
235                         $confirm->address_type = 'email';
236
237                         $result = $confirm->insert();
238                         if (!$result) {
239                                 common_log_db_error($confirm, 'INSERT', __FILE__);
240                                 return FALSE;
241                         }
242                 }
243
244                 if ($code && $user->email) {
245                         $user->emailChanged();
246                 }
247
248                 $profile->query('COMMIT');
249
250                 if ($email && !$user->email) {
251                         mail_confirm_address($confirm->code,
252                                                                  $profile->nickname,
253                                                                  $email);
254                 }
255
256                 return $user;
257         }
258
259         # Things we do when the email changes
260
261         function emailChanged() {
262
263                 $invites = new Invitation();
264                 $invites->address = $this->email;
265                 $invites->address_type = 'email';
266
267                 if ($invites->find()) {
268                         while ($invites->fetch()) {
269                                 $other = User::staticGet($invites->user_id);
270                                 subs_subscribe_to($other, $this);
271                         }
272                 }
273         }
274
275         function hasFave($notice) {
276                 $cache = common_memcache();
277
278                 # XXX: Kind of a hack.
279                 
280                 if ($cache) {
281                         # This is the stream of favorite notices, in rev chron
282                         # order. This forces it into cache.
283                         $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
284                         
285                         while ($faves->fetch()) {
286                                 if ($faves->id < $notice->id) {
287                                         # If we passed it, it's not a fave
288                                         return false;
289                                 } else if ($faves->id == $notice->id) {
290                                         # If it matches a cached notice, then it's a fave
291                                         return true;
292                                 }
293                         }
294                         # If it's past the end of the cache window,
295                         # fall through to the default
296                 }
297                 
298                 $fave = Fave::pkeyGet(array('user_id' => $this->id,
299                                                                         'notice_id' => $notice->id));
300                 return ((is_null($fave)) ? false : true);
301         }
302         
303         function mutuallySubscribed($other) {
304                 return $this->isSubscribed($other) &&
305                   $other->isSubscribed($this);
306         }
307         
308         function mutuallySubscribedUsers() {
309
310                 # 3-way join; probably should get cached
311                 
312                 $qry = 'SELECT user.* ' .
313                   'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
314                   'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
315                   'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
316                   'ORDER BY user.nickname';
317                 
318                 $user = new User();
319                 $user->query(sprintf($qry, $this->id, $this->id));
320
321                 return $user;
322         }
323
324         function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
325                 $qry =
326                   'SELECT notice.* ' .
327                   'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
328                   'WHERE reply.profile_id = %d ';
329                 
330                 return Notice::getStream(sprintf($qry, $this->id),
331                                                                  'user:replies:'.$this->id,
332                                                                  $offset, $limit, $since_id, $before_id);
333         }
334         
335         function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
336                 $qry =
337                   'SELECT * ' .
338                   'FROM notice ' .
339                   'WHERE profile_id = %d ';
340                 
341                 return Notice::getStream(sprintf($qry, $this->id),
342                                                                  'user:notices:'.$this->id,
343                                                                  $offset, $limit, $since_id, $before_id);
344         }
345         
346         function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
347                 $qry =
348                   'SELECT notice.* ' .
349                   'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
350                   'WHERE fave.user_id = %d ';
351                 
352                 return Notice::getStream(sprintf($qry, $this->id),
353                                                                  'user:faves:'.$this->id,
354                                                                  $offset, $limit);
355         }
356         
357         function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
358                 $enabled = common_config('inboxes', 'enabled');
359
360                 # Complicated code, depending on whether we support inboxes yet
361                 # XXX: make this go away when inboxes become mandatory
362                 
363                 if ($enabled === false || 
364                         ($enabled == 'transitional' && $this->inboxed == 0)) {
365                         $qry =
366                           'SELECT notice.* ' .
367                           'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
368                           'WHERE subscription.subscriber = %d ';
369                         $order = NULL;
370                 } else if ($enabled === true ||
371                                    ($enabled == 'transitional' && $this->inboxed == 1)) {                                  
372                         $qry =
373                           'SELECT notice.* ' .
374                           'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
375                           'WHERE notice_inbox.user_id = %d ';
376                         # NOTE: we override ORDER
377                         $order = 'ORDER BY notice_inbox.created DESC, notice_inbox.notice_id DESC ';
378                 }
379                 
380                 return Notice::getStream(sprintf($qry, $this->id),
381                                                                  'user:notices_with_friends:' . $this->id,
382                                                                  $offset, $limit, $since_id, $before_id,
383                                                                  $order);
384         }
385         
386         function blowFavesCache() {
387                 $cache = common_memcache();
388                 if ($cache) {
389                         $cache->delete(common_cache_key('user:faves:'.$this->id));
390                 }
391         }
392 }