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