]>
Commit | Line | Data |
---|---|---|
997305cf | 1 | <?php |
79ffa771 JP |
2 | /* |
3 | (c) 2005 Joe Presbrey | |
4 | */ | |
997305cf JP |
5 | |
6 | require_once('mitsql.lib.php'); | |
7 | ||
8 | class Login { | |
e346f2b3 JP |
9 | private $id, $u, $p; |
10 | private $info; | |
997305cf | 11 | function Login($u, $p=null) { |
377015e0 | 12 | if (empty($u)) return; |
e47be57b | 13 | $this->u = $u; |
dc478ec8 | 14 | $this->p = $p; |
70776def | 15 | if ((string)intval($u)===(string)$u) { |
e47be57b | 16 | $this->id = $u; |
da0d337f | 17 | $opt = sprintf(" Username = '%s' OR UserId = '%s'", mysql_escape_string($u), mysql_escape_string($u)); |
e47be57b JP |
18 | } else { |
19 | $opt = sprintf(" Username = '%s'", mysql_escape_string($u)); | |
20 | $opt .= (is_null($p)?'':sprintf(" AND Password='%s'", mysql_escape_string(base64_encode($p)))); | |
21 | } | |
997305cf JP |
22 | $sql = sprintf("SELECT UserId, Username, Name, Email, UL, bEnabled |
23 | FROM User | |
377015e0 | 24 | WHERE %s", $opt); |
997305cf | 25 | $r = fetchRows(DBSelect($sql),'UserId'); |
e47be57b JP |
26 | $this->info = count($r)?array_shift($r):$r; |
27 | } | |
997305cf | 28 | function exists() { |
dc478ec8 | 29 | return count($this->info); |
997305cf | 30 | } |
dc478ec8 JP |
31 | function isValid() { |
32 | return $this->getUL()>0; | |
33 | } | |
997305cf JP |
34 | function isEnabled() { |
35 | return $this->exists() && $this->info['bEnabled']==1; | |
36 | } | |
dc478ec8 JP |
37 | function canLogin() { |
38 | return $this->isEnabled() && $this->isValid(); | |
39 | } | |
40 | function canSignup() { | |
41 | return !$this->isEnabled() && $this->isValid(); | |
42 | } | |
997305cf | 43 | function getUserId() { |
dc478ec8 | 44 | return $this->exists()?$this->info['UserId']:''; |
997305cf JP |
45 | } |
46 | function getUsername() { | |
dc478ec8 | 47 | return $this->exists()?$this->info['Username']:''; |
997305cf JP |
48 | } |
49 | function getName() { | |
dc478ec8 | 50 | return $this->exists()?$this->info['Name']:''; |
997305cf JP |
51 | } |
52 | function getEmail() { | |
dc478ec8 | 53 | return $this->exists()?$this->info['Email']:''; |
997305cf JP |
54 | } |
55 | function getUL() { | |
dc478ec8 | 56 | return $this->exists()?$this->info['UL']:''; |
997305cf JP |
57 | } |
58 | function expire() { | |
59 | $this->info = null; | |
60 | } | |
61 | function refresh() { | |
e47be57b JP |
62 | if (!empty($this->id)) { |
63 | $this->Login($this->id); | |
64 | } else { | |
65 | $this->Login($this->u,$this->p); | |
66 | } | |
997305cf JP |
67 | } |
68 | function update($name=null,$email=null) { | |
69 | if (!$this->exists()) return; | |
70 | $arr = array(); | |
dc478ec8 JP |
71 | if ($name == $this->getName()) $name = null; |
72 | if ($email == $this->getEmail()) $email = null; | |
997305cf JP |
73 | is_null($name) || $arr['Name'] = $name; |
74 | is_null($email) || $arr['Email'] = $email; | |
e47be57b | 75 | $upd = buildSQLSet($arr); |
8988dbad | 76 | $sql = sprintf("UPDATE User SET %s WHERE UserId = '%s'", |
3ebfe9a3 | 77 | $upd, mysql_escape_string($this->getUserId())); |
e47be57b JP |
78 | if (!empty($upd) && $upd != 'SET') |
79 | DBUpdate($sql); | |
dc478ec8 | 80 | if (isset($arr['Name'])) |
377015e0 | 81 | $this->info['Name'] = $arr['Name']; |
dc478ec8 | 82 | if (isset($arr['Email'])) |
377015e0 | 83 | $this->info['Email'] = $arr['Email']; |
dc478ec8 JP |
84 | } |
85 | } | |
86 | ||
87 | class User { | |
e346f2b3 JP |
88 | private $userId; |
89 | private $info; | |
90 | private $dblist; | |
dc478ec8 JP |
91 | function User($userId) { |
92 | $this->userId = $userId; | |
6ba4f54c | 93 | $sql = sprintf("SELECT User.UserId, Username, Name, Email, UL, bEnabled, nBytesSoft, nBytesHard, nBytes, nDatabases, nDatabasesHard, IF(nBytes>nBytesHard,1,0) AS bOverQuota |
dc478ec8 | 94 | FROM User |
e47be57b JP |
95 | INNER JOIN UserQuota ON User.UserId = UserQuota.UserId |
96 | INNER JOIN UserStat ON User.UserId = UserStat.UserId | |
88b7d384 | 97 | WHERE User.UserId = '%s'", |
dc478ec8 JP |
98 | mysql_escape_string($userId)); |
99 | $r = fetchRows(DBSelect($sql),'UserId'); | |
100 | $this->info = count($r)?array_shift($r):$r; | |
377015e0 | 101 | $this->dblist = $this->getDBList(); |
997305cf | 102 | } |
88b7d384 JP |
103 | function refresh() { |
104 | unset($this->dblist); | |
105 | $this->User($this->userId); | |
106 | /* | |
6ba4f54c | 107 | $sql = sprintf("SELECT UserId, Username, Name, Email, UL, bEnabled |
88b7d384 JP |
108 | FROM User |
109 | WHERE UserId = '%s'", | |
110 | mysql_escape_string($this->userId)); | |
111 | $r = fetchRows(DBSelect($sql),'UserId'); | |
112 | $this->info = count($r)?array_shift($r):$r; | |
113 | unset($this->dblist); | |
114 | $this->getDBList(); | |
115 | */ | |
116 | } | |
dc478ec8 JP |
117 | function exists() { |
118 | return count($this->info); | |
119 | } | |
120 | function getUserId() { | |
121 | return $this->exists()?$this->info['UserId']:''; | |
122 | } | |
123 | function getUsername() { | |
124 | return $this->exists()?$this->info['Username']:''; | |
88b7d384 | 125 | } |
9c70b481 JP |
126 | function isOverQuota() { |
127 | return $this->exists()?($this->info['bOverQuota']>0?true:false):''; | |
128 | } | |
3845bdf8 JP |
129 | function getDBQuotaHard() { |
130 | return $this->exists()?$this->info['nDatabasesHard']:0; | |
131 | } | |
88b7d384 JP |
132 | function getBytes() { |
133 | if($this->exists()) { | |
134 | $arr['nBytes'] = $this->info['nBytes']; | |
135 | $arr['nBytesSoft'] = $this->info['nBytesSoft']; | |
136 | $arr['nBytesHard'] = $this->info['nBytesHard']; | |
137 | return $arr; | |
138 | } | |
dc478ec8 JP |
139 | } |
140 | function setPassword($pwd) { | |
141 | $arr['Password'] = base64_encode($pwd); | |
8988dbad | 142 | $sql = sprintf("UPDATE User SET %s WHERE UserId = '%s'", |
dc478ec8 JP |
143 | buildSQLSet($arr), mysql_escape_string($this->getUserId())); |
144 | DBUpdate($sql); | |
377015e0 JP |
145 | $sql = sprintf('SET PASSWORD FOR \'%s\'@\'%%\'=PASSWORD(\'%s\')', |
146 | mysql_escape_string($this->getUsername()), | |
147 | mysql_escape_string($pwd)); | |
148 | DBSet($sql); | |
dc478ec8 JP |
149 | } |
150 | function signup($pwd) { | |
151 | $this->pass = $pwd; | |
152 | $arr['Password'] = base64_encode($pwd); | |
153 | $arr['bEnabled'] = 1; | |
154 | $arr['dSignup'] = 'NOW()'; | |
8988dbad | 155 | $sql = sprintf("UPDATE User SET %s WHERE UserId = '%s'", |
dc478ec8 JP |
156 | buildSQLSet($arr), mysql_escape_string($this->getUserId())); |
157 | DBUpdate($sql); | |
158 | ||
159 | $this->setUsage(); | |
160 | $this->setAccess(); | |
161 | } | |
162 | function setUsage($yes=true) { | |
163 | $verb = $yes?'GRANT':'REVOKE'; | |
164 | $prep = $yes?'TO':'FROM'; | |
3ebfe9a3 | 165 | $suffix = $yes?sprintf("IDENTIFIED BY '%s'",mysql_escape_string($this->pass)):''; |
dc478ec8 JP |
166 | $sql = sprintf("%s USAGE ON * . * %s '%s'@'%s' %s", |
167 | mysql_escape_string($verb), | |
168 | mysql_escape_string($prep), | |
169 | mysql_escape_string($this->getUsername()), | |
170 | '%', | |
3ebfe9a3 | 171 | $suffix); |
dc478ec8 JP |
172 | DBGrant($sql); |
173 | } | |
174 | function setAccess($db=null,$yes=true) { | |
175 | $verb = $yes?'GRANT':'REVOKE'; | |
176 | $prep = $yes?'TO':'FROM'; | |
177 | if (is_null($db)) { | |
88b7d384 | 178 | $dbs = $this->getDBList(); |
dc478ec8 JP |
179 | } else { |
180 | $dbs[] = array('Name'=>$db); | |
181 | } | |
182 | foreach($dbs as $db) { | |
183 | $name = $db['Name']; | |
184 | $sql = sprintf("%s ALL PRIVILEGES ON `%s` . * %s '%s'@'%s'", | |
185 | mysql_escape_string($verb), | |
186 | mysql_escape_string($name), | |
187 | mysql_escape_string($prep), | |
377015e0 | 188 | mysql_escape_string($this->getUsername()), |
dc478ec8 JP |
189 | '%'); |
190 | DBGrant($sql); | |
191 | } | |
192 | } | |
193 | function getDBList() { | |
88b7d384 JP |
194 | if (isset($this->dblist)) { |
195 | return $this->dblist; | |
196 | } else { | |
197 | // LEFT JOIN DBQuota ON DBQuota.DatabaseId = DBOwner.DatabaseId | |
198 | $sql = sprintf("SELECT * | |
dc478ec8 | 199 | FROM DBOwner |
e47be57b JP |
200 | INNER JOIN DB ON DB.DatabaseId = DBOwner.DatabaseId |
201 | INNER JOIN DBQuota ON DBQuota.DatabaseId = DBOwner.DatabaseId | |
88b7d384 | 202 | WHERE DBOwner.UserId = '%s' AND DB.bEnabled=1", |
dc478ec8 | 203 | mysql_escape_string($this->getUserId())); |
88b7d384 JP |
204 | // $r = fetchRows(DBSelect($sql),'DatabaseId'); |
205 | $r = fetchRows(DBSelect($sql),'Name'); | |
9c70b481 | 206 | ksort($r); |
88b7d384 JP |
207 | return $r; |
208 | } | |
dc478ec8 | 209 | } |
377015e0 | 210 | function addDB($name) { |
4a77eeb5 | 211 | if (in_array($name, array_keys($this->getDBList()))) return false; |
377015e0 JP |
212 | if (!addDB($name, $this->getUserId())) return false; |
213 | $this->setAccess($name); | |
214 | return true; | |
215 | } | |
e47be57b JP |
216 | function delDB($name) { |
217 | if (!in_array($name, array_keys($this->getDBList()))) return false; | |
218 | if (!delDB($name)) return false;//, $this->getUserId())) return false; | |
219 | $this->setAccess($name,false); | |
220 | return true; | |
221 | } | |
997305cf JP |
222 | } |
223 | ||
224 | ||
225 | function isLoggedIn($aLogin=null) { | |
226 | if (is_null($aLogin)) { | |
227 | global $Login; | |
228 | $aLogin = $Login; | |
229 | } | |
e346f2b3 | 230 | return !empty($aLogin) && ($aLogin instanceof Login) && $aLogin->canLogin(); |
997305cf JP |
231 | } |
232 | ||
e47be57b JP |
233 | function isAdmin($aLogin=null) { |
234 | if (is_null($aLogin)) { | |
235 | global $Login; | |
236 | $aLogin = $Login; | |
237 | } | |
e346f2b3 | 238 | return !empty($aLogin) && ($aLogin instanceof Login) && $aLogin->getUL()>=100; |
e47be57b JP |
239 | } |
240 | ||
88b7d384 JP |
241 | function isImpersonating() { |
242 | return isSess('_UserId') && isSess('UserId'); | |
243 | } | |
244 | ||
8ed5cf01 JP |
245 | function isOffline() { |
246 | return (defined('OFFLINE') && OFFLINE); | |
247 | } | |
248 | ||
249 | function isOnline() { | |
250 | return !isOffline(); | |
251 | } | |
252 | ||
88b7d384 JP |
253 | function impersonate($userId=null) { |
254 | $wasImpersonating = isImpersonating(); | |
255 | if ($wasImpersonating) { | |
224df904 | 256 | if (is_null($userId) || empty($userId)) { |
88b7d384 | 257 | sess('UserId',sess('_UserId')); |
224df904 | 258 | sess('_UserId',''); |
88b7d384 JP |
259 | } elseif ($userId>0) { |
260 | sess('UserId',$userId); | |
261 | } else { | |
262 | return false; | |
263 | } | |
264 | } elseif (isLoggedIn()) { | |
265 | sess('_UserId',sess('UserId')); | |
266 | sess('UserId',$userId); | |
267 | return true; | |
268 | } else { | |
269 | return false; | |
270 | } | |
271 | } | |
272 | ||
997305cf | 273 | function isSSL() { |
aaaa7a8f | 274 | return isset($_SERVER['SERVER_PORT'])?($_SERVER['SERVER_PORT'] == 443):false; |
997305cf JP |
275 | } |
276 | ||
277 | function getSSLCert() { | |
278 | if (DEVEL && file_exists('.forceauth')) { | |
279 | $fu = explode('|',file_get_contents('.forceauth')); | |
dc478ec8 JP |
280 | $name = trim($fu[0]); |
281 | $email = trim($fu[1]); | |
997305cf JP |
282 | } else { |
283 | $name = isset($_SERVER['SSL_CLIENT_S_DN_CN'])?$_SERVER['SSL_CLIENT_S_DN_CN']:null; | |
284 | $email = isset($_SERVER['SSL_CLIENT_S_DN_Email'])?$_SERVER['SSL_CLIENT_S_DN_Email']:null; | |
285 | } | |
286 | if (!is_null($email)) { | |
287 | $user = explode('@',$email); | |
288 | $user = $user[0]; | |
289 | return array('Username'=>$user, 'Name'=>$name, 'Email'=>$email); | |
290 | } else { | |
291 | return null; | |
292 | } | |
293 | } | |
294 | ||
295 | ## 302 REDIRECTS | |
296 | ||
1389493c | 297 | function redirect($target=null,$secure=null) { |
7654fe78 | 298 | $base = (is_null($target)||substr($target,0,1)=='?')?URI:(dirname(URI).'/'); |
dc478ec8 | 299 | redirectFull(is_null($target)?$base:($base.$target),$secure); |
997305cf | 300 | } |
224df904 JP |
301 | function redirectStart() { |
302 | redirectFull(BASE_URL,null); | |
303 | } | |
dc478ec8 | 304 | function redirectFull($target,$secure) { |
7654fe78 | 305 | redirect2((((isSSL()&&is_null($secure))||$secure==true)?BASE_HTTPS:BASE_HTTP).$target); |
997305cf JP |
306 | } |
307 | function redirect2($target) { | |
308 | header('Location: '.$target); | |
309 | exit; | |
310 | } | |
dc478ec8 | 311 | function flipSSL() { |
7654fe78 | 312 | return (isSSL()?BASE_HTTP:BASE_HTTPS).URI; |
dc478ec8 | 313 | } |
997305cf JP |
314 | |
315 | ## USER SCRIPTS | |
316 | ||
317 | function addUser($sslCredentials) { | |
dc478ec8 JP |
318 | global $_NEW_USER, $_NEW_USERQUOTA, $_NEW_USERSTAT; |
319 | ||
997305cf JP |
320 | $arr = array_merge($sslCredentials, $_NEW_USER); |
321 | $sql = sprintf("INSERT INTO User %s", | |
322 | buildSQLInsert($arr)); | |
dc478ec8 JP |
323 | $UserId = DBInsert($sql); |
324 | ||
325 | $arr = $_NEW_USERQUOTA; | |
326 | $arr['UserId'] = $UserId; | |
327 | $sql = sprintf("INSERT INTO UserQuota %s", | |
328 | buildSQLInsert($arr)); | |
329 | DBInsert($sql); | |
330 | ||
331 | $arr = $_NEW_USERSTAT; | |
332 | $arr['UserId'] = $UserId; | |
1389493c | 333 | $sql = sprintf("INSERT INTO UserStat %s", |
dc478ec8 JP |
334 | buildSQLInsert($arr)); |
335 | DBInsert($sql); | |
336 | ||
337 | return $UserId; | |
997305cf JP |
338 | } |
339 | ||
377015e0 | 340 | function addDB($dbname,$userid) { |
e47be57b | 341 | global $_NEW_DB, $_NEW_DBQUOTA, $_NEW_DBOWNER; |
377015e0 JP |
342 | |
343 | DBCreate(sprintf('CREATE DATABASE `%s`', mysql_escape_string($dbname))); | |
69e08b46 | 344 | if (mysql_error()) return false; |
377015e0 JP |
345 | |
346 | $newdb['Name'] = $dbname; | |
e47be57b | 347 | $arr = array_merge($newdb, $_NEW_DB); |
377015e0 | 348 | $arr['bEnabled'] = 1; |
e47be57b | 349 | $sql = sprintf("INSERT IGNORE INTO DB %s", |
377015e0 | 350 | buildSQLInsert($arr)); |
e47be57b JP |
351 | $DBId = DBInsert($sql); |
352 | if (empty($DBId)) { | |
353 | $sql = sprintf("SELECT DatabaseId FROM DB WHERE Name = '%s'", | |
354 | mysql_escape_string($dbname)); | |
355 | $r = fetchRows(DBSelect($sql), 'DatabaseId'); | |
356 | if (count($r)) { | |
357 | $r = array_shift($r); | |
358 | $DBId = $r['DatabaseId']; | |
359 | } else { | |
360 | return false; | |
361 | } | |
8988dbad | 362 | $sql = sprintf("UPDATE DB SET %s WHERE DB.DatabaseId = '%s'", |
e47be57b JP |
363 | buildSQLSet($arr), |
364 | $DBId); | |
365 | DBUpdate($sql); | |
e47be57b | 366 | } |
69e08b46 JP |
367 | |
368 | DBDelete(sprintf("DELETE FROM DBOwner WHERE DatabaseId = '%s'", mysql_escape_string($DBId))); | |
369 | DBDelete(sprintf("DELETE FROM DBQuota WHERE DatabaseId = '%s'", mysql_escape_string($DBId))); | |
370 | ||
371 | $arr = $_NEW_DBQUOTA; | |
372 | $arr['DatabaseId'] = $DBId; | |
373 | $sql = sprintf("INSERT IGNORE INTO DBQuota %s", | |
374 | buildSQLInsert($arr)); | |
375 | DBInsert($sql); | |
376 | ||
377 | $arr = $_NEW_DBOWNER; | |
378 | $arr['DatabaseId'] = $DBId; | |
379 | $arr['UserId'] = $userid; | |
380 | $sql = sprintf("INSERT IGNORE INTO DBOwner %s", | |
381 | buildSQLInsert($arr)); | |
382 | DBInsert($sql); | |
383 | ||
384 | return $DBId; | |
e47be57b JP |
385 | } |
386 | ||
387 | function delDB($dbname) { | |
388 | global $_NEW_DB, $_NEW_DBQUOTA, $_NEW_DBOWNER; | |
389 | ||
390 | DBCreate(sprintf('DROP DATABASE `%s`', mysql_escape_string($dbname))); | |
391 | ||
392 | $arr['bEnabled'] = 0; | |
8988dbad | 393 | $sql = sprintf("UPDATE DB SET %s WHERE DB.Name = '%s'", |
e47be57b JP |
394 | buildSQLSet($arr), |
395 | $dbname); | |
396 | DBUpdate($sql); | |
377015e0 | 397 | |
e47be57b | 398 | return true; |
377015e0 JP |
399 | } |
400 | ||
88b7d384 | 401 | ?> |