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