]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
cdb6568f0cb7060f10e69174d74e7043a08cdbec
[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 = common_sql_now(); # 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                 $notice = new Notice();
146
147                 $notice->query('SELECT notice.* ' .
148                                            'FROM notice JOIN subscription on notice.profile_id = subscription.subscribed ' .
149                                            'WHERE subscription.subscriber = ' . $this->id . ' ' .
150                                            'ORDER BY created DESC, notice.id DESC ' .
151                                            'LIMIT ' . $offset . ', ' . $limit);
152
153                 return $notice;
154         }
155         
156         static function register($fields) {
157
158                 # MAGICALLY put fields into current scope
159
160                 extract($fields);
161
162                 $profile = new Profile();
163
164                 $profile->query('BEGIN');
165
166                 $profile->nickname = $nickname;
167                 $profile->profileurl = common_profile_url($nickname);
168
169                 if ($fullname) {
170                         $profile->fullname = $fullname;
171                 }
172                 if ($homepage) {
173                         $profile->homepage = $homepage;
174                 }
175                 if ($bio) {
176                         $profile->bio = $bio;
177                 }
178                 if ($location) {
179                         $profile->location = $location;
180                 }
181
182                 $profile->created = common_sql_now();
183
184                 $id = $profile->insert();
185
186                 if (!$id) {
187                         common_log_db_error($profile, 'INSERT', __FILE__);
188                     return FALSE;
189                 }
190
191                 $user = new User();
192
193                 $user->id = $id;
194                 $user->nickname = $nickname;
195
196                 if ($password) { # may not have a password for OpenID users
197                         $user->password = common_munge_password($password, $id);
198                 }
199
200                 # Users who respond to invite email have proven their ownership of that address
201
202                 if ($code) {
203                         $invite = Invite::staticGet($code);
204                         if ($invite && $invite->address && $invite->address_type == 'email') {
205                                 $user->email = $invite->address;
206                         }
207                 }
208
209                 $user->created = common_sql_now();
210                 $user->uri = common_user_uri($user);
211
212                 $result = $user->insert();
213
214                 if (!$result) {
215                         common_log_db_error($user, 'INSERT', __FILE__);
216                         return FALSE;
217                 }
218
219                 # Everyone is subscribed to themself
220
221                 $subscription = new Subscription();
222                 $subscription->subscriber = $user->id;
223                 $subscription->subscribed = $user->id;
224                 $subscription->created = $user->created;
225
226                 $result = $subscription->insert();
227
228                 if (!$result) {
229                         common_log_db_error($subscription, 'INSERT', __FILE__);
230                         return FALSE;
231                 }
232
233                 if ($email && !$code) {
234
235                         $confirm = new Confirm_address();
236                         $confirm->code = common_confirmation_code(128);
237                         $confirm->user_id = $user->id;
238                         $confirm->address = $email;
239                         $confirm->address_type = 'email';
240
241                         $result = $confirm->insert();
242                         if (!$result) {
243                                 common_log_db_error($confirm, 'INSERT', __FILE__);
244                                 return FALSE;
245                         }
246                 }
247
248                 if ($code && $user->email) {
249                         $user->emailChanged();
250                 }
251
252                 $profile->query('COMMIT');
253
254                 if ($email && !$code) {
255                         mail_confirm_address($confirm->code,
256                                                                  $profile->nickname,
257                                                                  $email);
258                 }
259
260                 return $user;
261         }
262
263         # Things we do when the email changes
264
265         function emailChanged() {
266
267                 $invites = new Invitation();
268                 $invites->address = $user->email;
269                 $invites->address_type = 'email';
270
271                 if ($invites->find()) {
272                         while ($invites->fetch()) {
273                                 $other = User::staticGet($invites->user_id);
274                                 subs_subscribe_to($other, $this);
275                         }
276                 }
277         }
278 }