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