]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
Disallow 'api' nickname
[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                                                                   'api');
115                 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
116                 return !in_array($nickname, $merged);
117         }
118
119         function getCurrentNotice($dt=NULL) {
120                 $profile = $this->getProfile();
121                 if (!$profile) {
122                         return NULL;
123                 }
124                 return $profile->getCurrentNotice($dt);
125         }
126
127         function getCarrier() {
128                 return Sms_carrier::staticGet($this->carrier);
129         }
130
131         function subscribeTo($other) {
132                 $sub = new Subscription();
133                 $sub->subscriber = $this->id;
134                 $sub->subscribed = $other->id;
135
136                 $sub->created = common_sql_now(); # current time
137
138                 if (!$sub->insert()) {
139                         return false;
140                 }
141
142                 return true;
143         }
144
145         function noticesWithFriends($offset=0, $limit=20) {
146                 
147                 $notice = new Notice();
148
149                 $notice->query('SELECT notice.* ' .
150                                            'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
151                                            'WHERE subscription.subscriber = ' . $this->id . ' ' .
152                                            'ORDER BY created DESC, notice.id DESC ' .
153                                            'LIMIT ' . $offset . ', ' . $limit);
154
155                 return $notice;
156         }
157
158         function favoriteNotices($offset=0, $limit=20) {
159
160                 $notice = new Notice();
161
162                 $notice->query('SELECT notice.* ' .
163                                            'FROM notice JOIN fave on notice.id = fave.notice_id ' .
164                                            'WHERE fave.user_id = ' . $this->id . ' ' .
165                                            'ORDER BY notice.created DESC, notice.id DESC ' .
166                                            'LIMIT ' . $offset . ', ' . $limit);
167
168                 return $notice;
169         }
170
171         function noticesWithFriendsWindow() {
172                 
173                 $cache = new Memcache();
174                 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
175                 
176                 if (!$res) {
177                         return NULL;
178                 }
179                 
180                 $notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
181
182                 if ($notices) {
183                         return $notices;
184                 }
185                 
186                 $notice = new Notice();
187                 
188                 $notice->query('SELECT notice.* ' .
189                                            'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
190                                            'WHERE subscription.subscriber = ' . $this->id . ' ' .
191                                            'ORDER BY created DESC, notice.id DESC ' .
192                                            'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
193                 
194                 $notices = array();
195                 
196                 while ($notice->fetch()) {
197                         $notices[] = clone($notice);
198                 }
199
200                 $cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
201                 return $notices;
202         }
203         
204         static function register($fields) {
205
206                 # MAGICALLY put fields into current scope
207
208                 extract($fields);
209
210                 $profile = new Profile();
211
212                 $profile->query('BEGIN');
213
214                 $profile->nickname = $nickname;
215                 $profile->profileurl = common_profile_url($nickname);
216
217                 if ($fullname) {
218                         $profile->fullname = $fullname;
219                 }
220                 if ($homepage) {
221                         $profile->homepage = $homepage;
222                 }
223                 if ($bio) {
224                         $profile->bio = $bio;
225                 }
226                 if ($location) {
227                         $profile->location = $location;
228                 }
229
230                 $profile->created = common_sql_now();
231
232                 $id = $profile->insert();
233
234                 if (!$id) {
235                         common_log_db_error($profile, 'INSERT', __FILE__);
236                     return FALSE;
237                 }
238
239                 $user = new User();
240
241                 $user->id = $id;
242                 $user->nickname = $nickname;
243
244                 if ($password) { # may not have a password for OpenID users
245                         $user->password = common_munge_password($password, $id);
246                 }
247
248                 # Users who respond to invite email have proven their ownership of that address
249
250                 if ($code) {
251                         $invite = Invitation::staticGet($code);
252                         if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
253                                 $user->email = $invite->address;
254                         }
255                 }
256
257                 $user->created = common_sql_now();
258                 $user->uri = common_user_uri($user);
259
260                 $result = $user->insert();
261
262                 if (!$result) {
263                         common_log_db_error($user, 'INSERT', __FILE__);
264                         return FALSE;
265                 }
266
267                 # Everyone is subscribed to themself
268
269                 $subscription = new Subscription();
270                 $subscription->subscriber = $user->id;
271                 $subscription->subscribed = $user->id;
272                 $subscription->created = $user->created;
273
274                 $result = $subscription->insert();
275
276                 if (!$result) {
277                         common_log_db_error($subscription, 'INSERT', __FILE__);
278                         return FALSE;
279                 }
280
281                 if ($email && !$user->email) {
282
283                         $confirm = new Confirm_address();
284                         $confirm->code = common_confirmation_code(128);
285                         $confirm->user_id = $user->id;
286                         $confirm->address = $email;
287                         $confirm->address_type = 'email';
288
289                         $result = $confirm->insert();
290                         if (!$result) {
291                                 common_log_db_error($confirm, 'INSERT', __FILE__);
292                                 return FALSE;
293                         }
294                 }
295
296                 if ($code && $user->email) {
297                         $user->emailChanged();
298                 }
299
300                 $profile->query('COMMIT');
301
302                 if ($email && !$user->email) {
303                         mail_confirm_address($confirm->code,
304                                                                  $profile->nickname,
305                                                                  $email);
306                 }
307
308                 return $user;
309         }
310
311         # Things we do when the email changes
312
313         function emailChanged() {
314
315                 $invites = new Invitation();
316                 $invites->address = $user->email;
317                 $invites->address_type = 'email';
318
319                 if ($invites->find()) {
320                         while ($invites->fetch()) {
321                                 $other = User::staticGet($invites->user_id);
322                                 subs_subscribe_to($other, $this);
323                         }
324                 }
325         }
326
327         function hasFave($notice) {
328                 $fave = new Fave();
329                 $fave->user_id = $this->id;
330                 $fave->notice_id = $notice->id;
331                 if ($fave->find()) {
332                         $result = true;
333                 } else {
334                         $result = false;
335                 }
336                 $fave->free();
337                 unset($fave);
338                 return $result;
339         }
340 }