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