]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
log a db error for inserting the notice
[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 INSTALLDIR.'/classes/Memcached_DataObject.php';
31 require_once 'Validate.php';
32 require_once(INSTALLDIR.'/lib/noticewrapper.php');
33
34 class User extends Memcached_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 Memcached_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                 $orig->decache();
113                 $result = $this->query($qry);
114                 if ($result) {
115                         $this->encache();
116                 }
117                 return $result;
118         }
119
120         function allowed_nickname($nickname) {
121                 # XXX: should already be validated for size, content, etc.
122                 static $blacklist = array('rss', 'xrds', 'doc', 'main',
123                                                                   'settings', 'notice', 'user',
124                                                                   'search', 'avatar', 'tag', 'tags',
125                                                                   'api', 'message');
126                 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
127                 return !in_array($nickname, $merged);
128         }
129
130         function getCurrentNotice($dt=NULL) {
131                 $profile = $this->getProfile();
132                 if (!$profile) {
133                         return NULL;
134                 }
135                 return $profile->getCurrentNotice($dt);
136         }
137
138         function getCarrier() {
139                 return Sms_carrier::staticGet($this->carrier);
140         }
141
142         function subscribeTo($other) {
143                 $sub = new Subscription();
144                 $sub->subscriber = $this->id;
145                 $sub->subscribed = $other->id;
146
147                 $sub->created = common_sql_now(); # current time
148
149                 if (!$sub->insert()) {
150                         return false;
151                 }
152
153                 return true;
154         }
155
156         function noticesWithFriends($offset=0, $limit=20) {
157
158                 # We clearly need a more elegant way to make this work.
159                 
160                 if (common_config('memcached', 'enabled')) {
161                         if ($offset + $limit <= WITHFRIENDS_CACHE_WINDOW) {
162                                 $cached = $this->noticesWithFriendsWindow();
163                                 $wrapper = new NoticeWrapper(array_slice($cached, $offset, $limit));
164                                 return $wrapper;
165                         } 
166                 }
167                 
168                 $notice = new Notice();
169         
170                 $query='SELECT notice.* ' .
171                         'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
172                         'WHERE subscription.subscriber = ' . $this->id . ' ' .
173                         'ORDER BY created DESC, notice.id DESC ';
174                 if(common_config('db','type')=='pgsql') {
175                         $query=$query . 'LIMIT ' . $limit . ' OFFSET ' . $offset;
176                 } else {
177                         $query=$query . 'LIMIT ' . $offset . ', ' . $limit;
178                 }
179                 $notice->query($query);
180
181                 return $notice;
182         }
183
184         function favoriteNotices($offset=0, $limit=20) {
185
186                 $notice = new Notice();
187
188                 $notice->query('SELECT notice.* ' .
189                                            'FROM notice JOIN fave on notice.id = fave.notice_id ' .
190                                            'WHERE fave.user_id = ' . $this->id . ' ' .
191                                            'ORDER BY notice.created DESC, notice.id DESC ' .
192                                            'LIMIT ' . $offset . ', ' . $limit);
193
194                 return $notice;
195         }
196
197         function noticesWithFriendsWindow() {
198                 
199                 $cache = new Memcache();
200                 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
201                 
202                 if (!$res) {
203                         return NULL;
204                 }
205                 
206                 $notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
207
208                 if ($notices) {
209                         return $notices;
210                 }
211                 
212                 $notice = new Notice();
213                 
214                 $notice->query('SELECT notice.* ' .
215                                            'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
216                                            'WHERE subscription.subscriber = ' . $this->id . ' ' .
217                                            'ORDER BY created DESC, notice.id DESC ' .
218                                            'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
219                 
220                 $notices = array();
221                 
222                 while ($notice->fetch()) {
223                         $notices[] = clone($notice);
224                 }
225
226                 $cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
227                 return $notices;
228         }
229         
230         static function register($fields) {
231
232                 # MAGICALLY put fields into current scope
233
234                 extract($fields);
235
236                 $profile = new Profile();
237
238                 $profile->query('BEGIN');
239
240                 $profile->nickname = $nickname;
241                 $profile->profileurl = common_profile_url($nickname);
242
243                 if ($fullname) {
244                         $profile->fullname = $fullname;
245                 }
246                 if ($homepage) {
247                         $profile->homepage = $homepage;
248                 }
249                 if ($bio) {
250                         $profile->bio = $bio;
251                 }
252                 if ($location) {
253                         $profile->location = $location;
254                 }
255
256                 $profile->created = common_sql_now();
257
258                 $id = $profile->insert();
259
260                 if (!$id) {
261                         common_log_db_error($profile, 'INSERT', __FILE__);
262                     return FALSE;
263                 }
264
265                 $user = new User();
266
267                 $user->id = $id;
268                 $user->nickname = $nickname;
269
270                 if ($password) { # may not have a password for OpenID users
271                         $user->password = common_munge_password($password, $id);
272                 }
273
274                 # Users who respond to invite email have proven their ownership of that address
275
276                 if ($code) {
277                         $invite = Invitation::staticGet($code);
278                         if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
279                                 $user->email = $invite->address;
280                         }
281                 }
282
283                 $user->created = common_sql_now();
284                 $user->uri = common_user_uri($user);
285
286                 $result = $user->insert();
287
288                 if (!$result) {
289                         common_log_db_error($user, 'INSERT', __FILE__);
290                         return FALSE;
291                 }
292
293                 # Everyone is subscribed to themself
294
295                 $subscription = new Subscription();
296                 $subscription->subscriber = $user->id;
297                 $subscription->subscribed = $user->id;
298                 $subscription->created = $user->created;
299
300                 $result = $subscription->insert();
301
302                 if (!$result) {
303                         common_log_db_error($subscription, 'INSERT', __FILE__);
304                         return FALSE;
305                 }
306
307                 if ($email && !$user->email) {
308
309                         $confirm = new Confirm_address();
310                         $confirm->code = common_confirmation_code(128);
311                         $confirm->user_id = $user->id;
312                         $confirm->address = $email;
313                         $confirm->address_type = 'email';
314
315                         $result = $confirm->insert();
316                         if (!$result) {
317                                 common_log_db_error($confirm, 'INSERT', __FILE__);
318                                 return FALSE;
319                         }
320                 }
321
322                 if ($code && $user->email) {
323                         $user->emailChanged();
324                 }
325
326                 $profile->query('COMMIT');
327
328                 if ($email && !$user->email) {
329                         mail_confirm_address($confirm->code,
330                                                                  $profile->nickname,
331                                                                  $email);
332                 }
333
334                 return $user;
335         }
336
337         # Things we do when the email changes
338
339         function emailChanged() {
340
341                 $invites = new Invitation();
342                 $invites->address = $this->email;
343                 $invites->address_type = 'email';
344
345                 if ($invites->find()) {
346                         while ($invites->fetch()) {
347                                 $other = User::staticGet($invites->user_id);
348                                 subs_subscribe_to($other, $this);
349                         }
350                 }
351         }
352
353         function hasFave($notice) {
354                 $fave = new Fave();
355                 $fave->user_id = $this->id;
356                 $fave->notice_id = $notice->id;
357                 if ($fave->find()) {
358                         $result = true;
359                 } else {
360                         $result = false;
361                 }
362                 $fave->free();
363                 unset($fave);
364                 return $result;
365         }
366         
367         function mutuallySubscribed($other) {
368                 return $this->isSubscribed($other) &&
369                   $other->isSubscribed($this);
370         }
371         
372         function mutuallySubscribedUsers() {
373
374                 # 3-way join; probably should get cached
375                 
376                 $qry = 'SELECT user.* ' .
377                   'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
378                   'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
379                   'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
380                   'ORDER BY user.nickname';
381                 
382                 $user = new User();
383                 $user->query(sprintf($qry, $this->id, $this->id));
384
385                 return $user;
386         }
387 }