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