]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
8b73923a775ea57e293ead993b7d7be0f3d3bf47
[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 $emailmicroid;                    // tinyint(1)   default_1
47     public $language;                        // varchar(50)  
48     public $timezone;                        // varchar(50)  
49     public $emailpost;                       // tinyint(1)   default_1
50     public $jabber;                          // varchar(255)  unique_key
51     public $jabbernotify;                    // tinyint(1)  
52     public $jabberreplies;                   // tinyint(1)  
53     public $jabbermicroid;                   // tinyint(1)   default_1
54     public $updatefrompresence;              // tinyint(1)  
55     public $sms;                             // varchar(64)  unique_key
56     public $carrier;                         // int(4)  
57     public $smsnotify;                       // tinyint(1)  
58     public $smsreplies;                      // tinyint(1)  
59     public $smsemail;                        // varchar(255)  
60     public $uri;                             // varchar(255)  unique_key
61     public $autosubscribe;                   // tinyint(1)  
62     public $created;                         // datetime()   not_null
63     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
64
65     /* Static get */
66     function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User',$k,$v); }
67
68     /* the code above is auto generated do not remove the tag below */
69     ###END_AUTOCODE
70
71         function getProfile() {
72                 $profile = DB_DataObject::factory('profile');
73                 $profile->id = $this->id;
74                 if ($profile->find()) {
75                         $profile->fetch();
76                         return $profile;
77                 }
78                 return NULL;
79         }
80
81         function isSubscribed($other) {
82                 assert(!is_null($other));
83                 $sub = DB_DataObject::factory('subscription');
84                 $sub->subscriber = $this->id;
85                 $sub->subscribed = $other->id;
86                 return $sub->find();
87         }
88
89         # 'update' won't write key columns, so we have to do it ourselves.
90
91         function updateKeys(&$orig) {
92                 $parts = array();
93                 foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
94                         if (strcmp($this->$k, $orig->$k) != 0) {
95                                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
96                         }
97                 }
98                 if (count($parts) == 0) {
99                         # No changes
100                         return true;
101                 }
102                 $toupdate = implode(', ', $parts);
103                 $qry = 'UPDATE ' . $this->tableName() . ' SET ' . $toupdate .
104                   ' WHERE id = ' . $this->id;
105                 return $this->query($qry);
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                 $merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
114                 return !in_array($nickname, $merged);
115         }
116
117         function getCurrentNotice($dt=NULL) {
118                 $profile = $this->getProfile();
119                 if (!$profile) {
120                         return NULL;
121                 }
122                 return $profile->getCurrentNotice($dt);
123         }
124         
125         function getCarrier() {
126                 return Sms_carrier::staticGet($this->carrier);
127         }
128         
129         function subscribeTo($other) {
130                 $sub = new Subscription();
131                 $sub->subscriber = $this->id;
132                 $sub->subscribed = $other->id;
133
134                 $sub->created = DB_DataObject_Cast::dateTime(); # current time
135
136                 if (!$sub->insert()) {
137                         return false;
138                 }
139                 
140                 return true;
141         }
142
143         function noticesWithFriends($offset=0, $limit=20) {
144
145                 # We clearly need a more elegant way to make this work.
146                 
147                 if (common_config('memcached', 'enabled')) {
148                         if ($offset + $limit < WITHFRIENDS_CACHE_WINDOW) {
149                                 $cached = $this->noticesWithFriendsCachedWindow();
150                                 if (!$cached) {
151                                         $cached = $this->noticesWithFriendsWindow();
152                                 }
153                                 $wrapper = new NoticeWrapper(array_slice($cached, $offset, $limit));
154                                 return $wrapper;
155                         } 
156                 }
157                 
158                 $notice = new Notice();
159                 
160                 $notice->query('SELECT notice.* ' .
161                                            'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
162                                            'WHERE subscription.subscriber = ' . $this->id . ' ' .
163                                            'ORDER BY created DESC, notice.id DESC ' .
164                                            'LIMIT ' . $offset . ', ' . $limit);
165                 
166                 return $notice;
167         }
168         
169         function noticesWithFriendsCachedWindow() {
170                 $cache = new Memcache();
171                 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
172                 if (!$res) {
173                         return NULL;
174                 }
175                 $notices = $cache->get(common_cache_key('user:notices_with_friends:' . $this->id));
176                 return $notices;
177         }
178
179         function noticesWithFriendsWindow() {
180                 
181                 $cache = new Memcache();
182                 $res = $cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'));
183                 
184                 if (!$res) {
185                         return NULL;
186                 }
187                 
188                 $notice = new Notice();
189                 
190                 $notice->query('SELECT notice.* ' .
191                                            'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
192                                            'WHERE subscription.subscriber = ' . $this->id . ' ' .
193                                            'ORDER BY created DESC, notice.id DESC ' .
194                                            'LIMIT 0, ' . WITHFRIENDS_CACHE_WINDOW);
195                 
196                 $notices = array();
197                 
198                 while ($notice->fetch()) {
199                         $notices[] = clone($notice);
200                 }
201
202                 $cache->set(common_cache_key('user:notices_with_friends:' . $this->id), $notices);
203                 return $notices;
204         }
205         
206         static function register($fields) {
207
208                 # MAGICALLY put fields into current scope
209                 
210                 extract($fields);
211                 
212                 $profile = new Profile();
213
214                 $profile->query('BEGIN');
215
216                 $profile->nickname = $nickname;
217                 $profile->profileurl = common_profile_url($nickname);
218                 
219                 if ($fullname) {
220                         $profile->fullname = $fullname;
221                 }
222                 if ($homepage) {
223                         $profile->homepage = $homepage;
224                 }
225                 if ($bio) {
226                         $profile->bio = $bio;
227                 }
228                 if ($location) {
229                         $profile->location = $location;
230                 }
231                 
232                 $profile->created = common_sql_now();
233                 
234                 $id = $profile->insert();
235
236                 if (!$id) {
237                         common_log_db_error($profile, 'INSERT', __FILE__);
238                     return FALSE;
239                 }
240                 
241                 $user = new User();
242                 
243                 $user->id = $id;
244                 $user->nickname = $nickname;
245
246                 if ($password) { # may not have a password for OpenID users
247                         $user->password = common_munge_password($password, $id);
248                 }
249                 
250                 $user->created = common_sql_now();
251                 $user->uri = common_user_uri($user);
252
253                 $result = $user->insert();
254
255                 if (!$result) {
256                         common_log_db_error($user, 'INSERT', __FILE__);
257                         return FALSE;
258                 }
259
260                 # Everyone is subscribed to themself
261
262                 $subscription = new Subscription();
263                 $subscription->subscriber = $user->id;
264                 $subscription->subscribed = $user->id;
265                 $subscription->created = $user->created;
266                 
267                 $result = $subscription->insert();
268                 
269                 if (!$result) {
270                         common_log_db_error($subscription, 'INSERT', __FILE__);
271                         return FALSE;
272                 }
273                 
274                 if ($email) {
275
276                         $confirm = new Confirm_address();
277                         $confirm->code = common_confirmation_code(128);
278                         $confirm->user_id = $user->id;
279                         $confirm->address = $email;
280                         $confirm->address_type = 'email';
281
282                         $result = $confirm->insert();
283                         if (!$result) {
284                                 common_log_db_error($confirm, 'INSERT', __FILE__);
285                                 return FALSE;
286                         }
287                 }
288
289                 $profile->query('COMMIT');
290
291                 if ($email) {
292                         mail_confirm_address($confirm->code,
293                                                                  $profile->nickname,
294                                                                  $email);
295                 }
296
297                 return $user;
298         }
299 }