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