]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
correct some SQL and add some spaces
[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         function noticesWithFriendsWindow() {
151                 
152                 
153                 $notice = new Notice();
154                 
155                 $notice->query('SELECT notice.* ' .
156                                            'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
157                                            'WHERE subscription.subscriber = ' . $this->id . ' ' .
158                                            'ORDER BY created DESC, notice.id DESC ' .
159                                            'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
160                 
161         }
162         
163         static function register($fields) {
164
165                 # MAGICALLY put fields into current scope
166
167                 extract($fields);
168
169                 $profile = new Profile();
170
171                 $profile->query('BEGIN');
172
173                 $profile->nickname = $nickname;
174                 $profile->profileurl = common_profile_url($nickname);
175
176                 if ($fullname) {
177                         $profile->fullname = $fullname;
178                 }
179                 if ($homepage) {
180                         $profile->homepage = $homepage;
181                 }
182                 if ($bio) {
183                         $profile->bio = $bio;
184                 }
185                 if ($location) {
186                         $profile->location = $location;
187                 }
188
189                 $profile->created = common_sql_now();
190
191                 $id = $profile->insert();
192
193                 if (!$id) {
194                         common_log_db_error($profile, 'INSERT', __FILE__);
195                     return FALSE;
196                 }
197
198                 $user = new User();
199
200                 $user->id = $id;
201                 $user->nickname = $nickname;
202
203                 if ($password) { # may not have a password for OpenID users
204                         $user->password = common_munge_password($password, $id);
205                 }
206
207                 # Users who respond to invite email have proven their ownership of that address
208
209                 if ($code) {
210                         $invite = Invitation::staticGet($code);
211                         if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
212                                 $user->email = $invite->address;
213                         }
214                 }
215
216                 $user->created = common_sql_now();
217                 $user->uri = common_user_uri($user);
218
219                 $result = $user->insert();
220
221                 if (!$result) {
222                         common_log_db_error($user, 'INSERT', __FILE__);
223                         return FALSE;
224                 }
225
226                 # Everyone is subscribed to themself
227
228                 $subscription = new Subscription();
229                 $subscription->subscriber = $user->id;
230                 $subscription->subscribed = $user->id;
231                 $subscription->created = $user->created;
232
233                 $result = $subscription->insert();
234
235                 if (!$result) {
236                         common_log_db_error($subscription, 'INSERT', __FILE__);
237                         return FALSE;
238                 }
239
240                 if ($email && !$user->email) {
241
242                         $confirm = new Confirm_address();
243                         $confirm->code = common_confirmation_code(128);
244                         $confirm->user_id = $user->id;
245                         $confirm->address = $email;
246                         $confirm->address_type = 'email';
247
248                         $result = $confirm->insert();
249                         if (!$result) {
250                                 common_log_db_error($confirm, 'INSERT', __FILE__);
251                                 return FALSE;
252                         }
253                 }
254
255                 if ($code && $user->email) {
256                         $user->emailChanged();
257                 }
258
259                 $profile->query('COMMIT');
260
261                 if ($email && !$user->email) {
262                         mail_confirm_address($confirm->code,
263                                                                  $profile->nickname,
264                                                                  $email);
265                 }
266
267                 return $user;
268         }
269
270         # Things we do when the email changes
271
272         function emailChanged() {
273
274                 $invites = new Invitation();
275                 $invites->address = $this->email;
276                 $invites->address_type = 'email';
277
278                 if ($invites->find()) {
279                         while ($invites->fetch()) {
280                                 $other = User::staticGet($invites->user_id);
281                                 subs_subscribe_to($other, $this);
282                         }
283                 }
284         }
285
286         function hasFave($notice) {
287                 $fave = new Fave();
288                 $fave->user_id = $this->id;
289                 $fave->notice_id = $notice->id;
290                 if ($fave->find()) {
291                         $result = true;
292                 } else {
293                         $result = false;
294                 }
295                 $fave->free();
296                 unset($fave);
297                 return $result;
298         }
299         
300         function mutuallySubscribed($other) {
301                 return $this->isSubscribed($other) &&
302                   $other->isSubscribed($this);
303         }
304         
305         function mutuallySubscribedUsers() {
306
307                 # 3-way join; probably should get cached
308                 
309                 $qry = 'SELECT user.* ' .
310                   'FROM subscription sub1 JOIN user ON sub1.subscribed = user.id ' .
311                   'JOIN subscription sub2 ON user.id = sub2.subscriber ' .
312                   'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
313                   'ORDER BY user.nickname';
314                 
315                 $user = new User();
316                 $user->query(sprintf($qry, $this->id, $this->id));
317
318                 return $user;
319         }
320
321         function getReplies($offset=0, $limit=NOTICES_PER_PAGE) {
322                 $qry =
323                   'SELECT notice.* ' .
324                   'FROM notice JOIN reply ON notice.id = reply.notice_id ' .
325                   'WHERE reply.profile_id = %d ';
326                 
327                 return Notice::getStream(sprintf($qry, $this->id),
328                                                                  'user:replies:'.$this->id,
329                                                                  $offset, $limit);
330         }
331         
332         function getNotices($offset=0, $limit=NOTICES_PER_PAGE) {
333                 $qry =
334                   'SELECT * ' .
335                   'FROM notice ' .
336                   'WHERE profile_id = %d ';
337                 
338                 return Notice::getStream(sprintf($qry, $this->id),
339                                                                  'user:notices:'.$this->id,
340                                                                  $offset, $limit);
341         }
342         
343         function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE) {
344                 $qry =
345                   'SELECT notice.* ' .
346                   'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
347                   'WHERE fave.user_id = %d ';
348                 
349                 return Notice::getStream(sprintf($qry, $this->id),
350                                                                  'user:faves:'.$this->id,
351                                                                  $offset, $limit);
352         }
353         
354         function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE) {
355                 $qry =
356                   'SELECT notice.* ' .
357                   'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
358                   'WHERE subscription.subscriber = %d ';
359                 
360                 return Notice::getStream(sprintf($qry, $this->id),
361                                                                  'user:notices_with_friends:' . $this->id,
362                                                                  $offset, $limit);
363         }
364         
365         function blowFavesCache() {
366                 $cache = common_memcache();
367                 if ($cache) {
368                         $cache->delete(common_cache_key('user:faves:'.$this->id));
369                 }
370         }
371 }