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