1<?php
2// This file is part of BOINC.
3// http://boinc.berkeley.edu
4// Copyright (C) 2008 University of California
5//
6// BOINC is free software; you can redistribute it and/or modify it
7// under the terms of the GNU Lesser General Public License
8// as published by the Free Software Foundation,
9// either version 3 of the License, or (at your option) any later version.
10//
11// BOINC 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.
14// See the GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19require_once("../inc/boinc_db.inc");
20
21class BoincCategory {
22    static function lookup_id($id) {
23        $db = BoincDb::get();
24        return $db->lookup_id($id, 'category', 'BoincCategory');
25    }
26    static function lookup($clause) {
27        $db = BoincDb::get();
28        return $db->lookup('category', 'BoincCategory', $clause);
29    }
30    static function enum($clause=null) {
31        $db = BoincDb::get();
32        return $db->enum('category', 'BoincCategory', $clause);
33    }
34}
35
36class BoincForum {
37    static function insert($clause) {
38        $db = BoincDb::get();
39        $ret = $db->insert('forum', $clause);
40        if (!$ret) return null;
41        return $db->insert_id();
42    }
43    static function lookup_id($id) {
44        $db = BoincDb::get();
45        return $db->lookup_id($id, 'forum', 'BoincForum');
46    }
47    static function lookup($clause) {
48        $db = BoincDb::get();
49        return $db->lookup('forum', 'BoincForum', $clause);
50    }
51    static function enum($clause) {
52        $db = BoincDb::get();
53        return $db->enum('forum', 'BoincForum', $clause);
54    }
55    function update($clause) {
56        $db = BoincDb::get();
57        return $db->update($this, 'forum', $clause);
58    }
59    function delete() {
60        $db = BoincDb::get();
61        return $db->delete($this, 'forum');
62    }
63}
64
65class BoincThread {
66    static function insert($clause) {
67        $db = BoincDb::get();
68        $ret = $db->insert('thread', $clause);
69        if (!$ret) return null;
70        return $db->insert_id();
71
72    }
73    static function lookup_id($id) {
74        $db = BoincDb::get();
75        return $db->lookup_id($id, 'thread', 'BoincThread');
76    }
77    function update($clause) {
78        $db = BoincDb::get();
79        return $db->update($this, 'thread', $clause);
80    }
81    static function enum($clause="") {
82        $db = BoincDb::get();
83        return $db->enum('thread', 'BoincThread', $clause);
84    }
85
86    function rating() {
87        return $this->score*$this->votes;
88    }
89    function delete() {
90        $db = BoincDb::get();
91        return $db->delete($this, 'thread');
92    }
93}
94
95class BoincPost {
96    static function insert($clause) {
97        $db = BoincDb::get();
98        $ret = $db->insert('post', $clause);
99        if (!$ret) return null;
100        return $db->insert_id();
101    }
102    static function lookup_id($id) {
103        $db = BoincDb::get();
104        return $db->lookup_id($id, 'post', 'BoincPost');
105    }
106    static function count($clause) {
107        $db = BoincDb::get();
108        return $db->count('post', $clause);
109    }
110    function update($clause) {
111        $db = BoincDb::get();
112        return $db->update($this, 'post', $clause);
113    }
114    static function enum($clause) {
115        $db = BoincDb::get();
116        return $db->enum('post', 'BoincPost', $clause);
117    }
118    static function enum_general($query) {
119        $db = BoincDb::get();
120        return $db->enum_general('BoincPost', $query);
121    }
122    function rating() {
123        return $this->score*$this->votes;
124    }
125    function delete() {
126        $db = BoincDb::get();
127        return $db->delete($this, 'post');
128    }
129    static function delete_aux($clause) {
130        $db = BoincDb::get();
131        return $db->delete_aux('post', $clause);
132    }
133}
134
135class BoincForumPrefs {
136    static $cache;
137    static function lookup_userid($id) {
138        $db = BoincDb::get();
139        return $db->lookup('forum_preferences', 'BoincForumPrefs', "userid=$id");
140    }
141    static function insert($clause) {
142        $db = BoincDb::get();
143        $ret = $db->insert('forum_preferences', $clause);
144    }
145    static function lookup(&$user, $nocache=false) {
146        if (!$user) return;
147        if (isset($user->prefs)) return;
148        if (!$nocache && isset(self::$cache[$user->id])) {
149            $prefs = self::$cache[$user->id];
150        } else {
151            $prefs = self::lookup_userid($user->id);
152            if (!$prefs) {
153                $db = BoincDb::get();
154                if ($db->readonly) {
155                    return;
156                }
157                self::insert("(userid, last_post, forum_sorting, thread_sorting, rated_posts, ignorelist, pm_notification) values ($user->id, 0, 0, 8, '', '', 0)");
158                $prefs = self::lookup_userid($user->id);
159                $prefs->userid = $user->id;
160                $prefs->thread_sorting = 6;
161            }
162            self::$cache[$user->id] = $prefs;
163        }
164        $user->prefs = $prefs;
165    }
166    function privilege($specialbit) {
167         return (substr($this->special_user, $specialbit,1)==1);
168    }
169    function update($clause) {
170        $db = BoincDb::get();
171        $clause = "$clause where userid=$this->userid";
172        return $db->update_aux('forum_preferences', $clause);
173    }
174    function delete() {
175        $db = BoincDb::get();
176        return $db->delete_aux('forum_preferences', "userid=$this->userid");
177    }
178    static function enum($clause=null) {
179        $db = BoincDb::get();
180        return $db->enum('forum_preferences', 'BoincForumPrefs', $clause);
181    }
182}
183
184class BoincForumLogging {
185    static $cache;
186    static function replace($userid, $threadid, $timestamp) {
187        $db = BoincDb::get();
188        return $db->replace('forum_logging', "userid=$userid, threadid=$threadid, timestamp=$timestamp");
189    }
190    static function lookup($userid, $threadid) {
191        $db = BoincDb::get();
192        return $db->lookup('forum_logging', 'BoincForumLogging', "userid=$userid and threadid=$threadid");
193    }
194    static function lookup_cached($userid, $threadid) {
195        if (isset(self::$cache[$threadid])) {
196            return self::$cache[$threadid];
197        }
198        $x = self::lookup($userid, $threadid);
199        if (!$x) {
200            $x = new BoincForumLogging();
201            $x->timestamp = 0;
202        }
203        self::$cache[$threadid] = $x;
204    }
205    static function delete_aux($clause) {
206        $db = BoincDb::get();
207        return $db->delete_aux('forum_logging', $clause);
208    }
209    static function cleanup() {
210        // Every 28 days, delete records older than 28 days.
211        // Keep track of the last time we did this in a special record
212        // with userid = threadid = 0.
213        // This gets called from forum_index.php
214        // (i.e. each time the forum main page is loaded).
215        //
216        $fl = BoincForumLogging::lookup(0, 0);
217        if ($fl) {
218            if ($fl->timestamp<time()-MAX_FORUM_LOGGING_TIME){
219                BoincForumLogging::delete_aux("timestamp<'".(time()-MAX_FORUM_LOGGING_TIME)."' and userid != 0");
220                BoincForumLogging::replace(0, 0, time());
221            }
222        } else {
223            // No cleanup timestamp found, make one
224            //
225            BoincForumLogging::replace(0, 0, 0);
226        }
227    }
228}
229
230class BoincSubscription {
231    static function lookup($userid, $threadid) {
232        $db = BoincDb::get();
233        return $db->lookup('subscriptions', 'BoincSubscription', "userid=$userid and threadid=$threadid");
234    }
235    static function delete($userid, $threadid) {
236        $db = BoincDb::get();
237        return $db->delete_aux('subscriptions', "userid=$userid and threadid=$threadid");
238    }
239    static function enum($clause) {
240        $db = BoincDb::get();
241        return $db->enum('subscriptions', 'BoincSubscription', $clause);
242    }
243    static function replace($userid, $threadid) {
244        $db = BoincDb::get();
245        return $db->replace('subscriptions', "userid=$userid, threadid=$threadid");
246    }
247}
248
249class BoincPostRating {
250    static function lookup($userid, $postid) {
251        $db = BoincDb::get();
252        return $db->lookup('post_ratings', 'BoincPostRating', "user=$userid and post=$postid");
253    }
254    static function replace($userid, $postid, $rating) {
255        $db = BoincDb::get();
256        return $db->replace('post_ratings', "user=$userid, post=$postid, rating=$rating");
257    }
258}
259
260class BoincFriend {
261    static function insert($clause) {
262        $db = BoincDb::get();
263        return $db->insert('friend', $clause);
264    }
265    static function lookup($uid1, $uid2) {
266        $db = BoincDb::get();
267        return $db->lookup('friend', 'BoincFriend', "user_src=$uid1 and user_dest=$uid2");
268    }
269    function update($clause) {
270        $db = BoincDb::get();
271        return $db->update_aux('friend', "$clause where user_src=$this->user_src and user_dest=$this->user_dest");
272    }
273    static function enum($clause) {
274        $db = BoincDb::get();
275        return $db->enum('friend', 'BoincFriend', $clause);
276    }
277    static function delete_aux($clause) {
278        $db = BoincDb::get();
279        return $db->delete_aux('friend', $clause);
280    }
281    static function delete($id1, $id2) {
282        $db = BoincDb::get();
283        $db->delete_aux('friend', "user_src=$id1 and user_dest=$id2");
284        $db->delete_aux('friend', "user_src=$id2 and user_dest=$id1");
285    }
286    static function replace($clause) {
287        $db = BoincDb::get();
288        return $db->replace('friend', $clause);
289    }
290}
291
292class BoincNotify {
293    static function insert($clause) {
294        $db = BoincDb::get();
295        $ret = $db->insert('notify', $clause);
296        if (!$ret) return null;
297        return $db->insert_id();
298    }
299    static function replace($clause) {
300        $db = BoincDb::get();
301        return $db->replace('notify', $clause);
302    }
303    static function enum($clause) {
304        $db = BoincDb::get();
305        return $db->enum('notify', 'BoincNotify', $clause);
306    }
307    static function lookup($userid, $type, $opaque) {
308        $db = BoincDb::get();
309        return $db->lookup('notify', 'BoincNotify', "userid=$userid and type=$type and opaque=$opaque");
310    }
311    function delete() {
312        $db = BoincDb::get();
313        return $db->delete($this, 'notify');
314    }
315    static function delete_aux($clause) {
316        $db = BoincDb::get();
317        $db->delete_aux('notify', $clause);
318    }
319    static function enum_general($query) {
320        $db = BoincDb::get();
321        return $db->enum_general('BoincNotify', $query);
322    }
323}
324
325define ('NOTIFY_FRIEND_REQ', 1);
326define ('NOTIFY_FRIEND_ACCEPT', 2);
327define ('NOTIFY_PM', 3);
328define ('NOTIFY_SUBSCRIBED_POST', 4);
329
330?>
331