]>
Commit | Line | Data |
---|---|---|
997305cf | 1 | <?php |
79ffa771 JP |
2 | /* |
3 | (c) 2005 Joe Presbrey | |
4 | */ | |
997305cf | 5 | |
8988dbad | 6 | require_once('joe/util.lib.php'); |
997305cf JP |
7 | |
8 | function DBMaster($sql) { | |
9 | sessTime($sql); | |
10 | $res = mysql_query($sql); | |
11 | sessTime(); | |
12 | return $res; | |
13 | } | |
14 | function DBSlave($sql) { | |
15 | sessTime($sql); | |
16 | $res = mysql_query($sql); | |
17 | sessTime(); | |
82ffc3b5 | 18 | if (mysql_error()) trigger_error($sql."<br />\n".mysql_error(),E_USER_ERROR); |
997305cf JP |
19 | return $res; |
20 | } | |
21 | ||
22 | function DBSelect($sql) { return DBSlave($sql); } | |
377015e0 JP |
23 | function DBInsert($sql) { |
24 | DBMaster($sql); | |
82ffc3b5 | 25 | if (mysql_error()) trigger_error($sql."<br />\n".mysql_error(),E_USER_ERROR); |
377015e0 JP |
26 | return mysql_insert_id(); |
27 | } | |
28 | function DBUpdate($sql) { DBInsert($sql); } | |
29 | function DBDelete($sql) { DBInsert($sql); } | |
30 | function DBCreate($sql) { DBMaster($sql); } | |
31 | function DBDrop($sql) { DBMaster($sql); } | |
32 | function DBGrant($sql) { DBInsert($sql); } | |
33 | function DBRevoke($sql) { DBInsert($sql); } | |
34 | function DBSet($sql) { DBInsert($sql); } | |
997305cf JP |
35 | function DBShow($sql) { return DBSlave($sql); } |
36 | ||
37 | function calcDBSize($tdb) { | |
e346f2b3 JP |
38 | $sql_result = "SHOW DATABASES LIKE '".mysql_escape_string($tdb)."'"; |
39 | $result = DBShow($sql_result); | |
40 | if (!mysql_num_rows($result)) return null; | |
41 | ||
997305cf | 42 | $sql_result = "SHOW TABLE STATUS FROM `" .mysql_escape_string($tdb)."`"; |
997305cf JP |
43 | $result = DBShow($sql_result); |
44 | ||
45 | if($result) { | |
46 | $size = 0; | |
47 | while ($data = mysql_fetch_array($result)) { | |
48 | $size += $data["Data_length"] + $data["Index_length"]; | |
49 | } | |
50 | mysql_free_result($result); | |
51 | return $size; | |
52 | } | |
53 | else { | |
54 | return null; | |
55 | } | |
56 | } | |
57 | ||
9c70b481 JP |
58 | function checkQuotas($userId=null) { |
59 | if (empty($userId)) { | |
60 | $sql = 'SELECT DatabaseId,Name FROM DB WHERE bEnabled=1'; | |
61 | } else { | |
6fd1f8c5 JP |
62 | if (is_array($userId)) { |
63 | $sql_userId = sprintf("UserId IN ('%s')", implode("','",array_map('mysql_escape_string',$userId))); | |
64 | } elseif (is_numeric($userId)) { | |
65 | $sql_userId = sprintf("UserId = '%s'", mysql_escape_string($userId)); | |
66 | } else { | |
67 | die('userId parameter error in checkQuotas'); | |
68 | } | |
69 | $sql = sprintf("SELECT DB.DatabaseId,Name FROM DB INNER JOIN DBOwner ON DB.DatabaseId = DBOwner.DatabaseId WHERE bEnabled=1 AND %s", $sql_userId); | |
9c70b481 JP |
70 | } |
71 | $databases = fetchRows(DBSelect($sql),'Name'); | |
72 | foreach($databases as $db) { | |
73 | $DBId = $db['DatabaseId']; | |
74 | $arr['dLastCheck'] = 'NOW()'; | |
75 | $arr['nBytes'] = calcDBSize($db['Name']); | |
8988dbad | 76 | $sql = sprintf("UPDATE DB SET %s WHERE DatabaseId = '%s'", |
9c70b481 JP |
77 | buildSQLSet($arr), |
78 | mysql_escape_string($DBId)); | |
79 | DBUpdate($sql); | |
80 | } | |
81 | $sql = "UPDATE UserStat SET nBytes = ( | |
82 | SELECT SUM(nBytes) | |
83 | FROM DB | |
84 | INNER JOIN DBOwner ON DBOwner.DatabaseId = DB.DatabaseId | |
85 | WHERE DBOwner.UserId = UserStat.UserId | |
86 | AND DB.bEnabled=1 | |
87 | GROUP BY UserId | |
88 | ), dLastCheck = NOW()"; | |
6fd1f8c5 | 89 | if (!empty($userId)) $sql .= " WHERE $sql_userId"; |
9c70b481 | 90 | DBUpdate($sql); |
9840d102 JP |
91 | $sql = "UPDATE UserStat SET nDatabases = ( |
92 | SELECT COUNT(*) | |
93 | FROM DB | |
94 | INNER JOIN DBOwner ON DBOwner.DatabaseId = DB.DatabaseId | |
95 | WHERE DBOwner.UserId = UserStat.UserId | |
96 | AND DB.bEnabled=1 | |
97 | GROUP BY UserId | |
98 | ), dLastCheck = NOW()"; | |
6fd1f8c5 | 99 | if (!empty($userId)) $sql .= " WHERE $sql_userId"; |
9840d102 | 100 | DBUpdate($sql); |
9c70b481 JP |
101 | } |
102 | ||
997305cf | 103 | ?> |