source: temp/test-xoops.ec-cube.net/html/modules/newbb/functions.php @ 405

Revision 405, 11.3 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2/***************************************************************************
3                           functions.php  -  description
4                             -------------------
5    begin                : Sat June 17 2000
6    copyright            : (C) 2001 The phpBB Group
7    email                : [email protected]
8
9    $Id: functions.php,v 1.2 2005/03/18 12:52:25 onokazu Exp $
10
11 ***************************************************************************/
12
13/***************************************************************************
14 *
15 *   This program is free software; you can redistribute it and/or modify
16 *   it under the terms of the GNU General Public License as published by
17 *   the Free Software Foundation; either version 2 of the License, or
18 *   (at your option) any later version.
19 *
20 ***************************************************************************/
21
22
23/* ¥Ç¥Ð¥Ã¥°ÍÑ ------------------------------------------------------------------------------------------------*/
24function sfPrintR($obj) {
25    print("<div style='font-size: 12px;color: #00FF00;'>\n");
26    print("<strong>**¥Ç¥Ð¥Ã¥°Ãæ**</strong><br />\n");
27    print("<pre>\n");
28    print_r($obj);
29    print("</pre>\n");
30    print("<strong>**¥Ç¥Ð¥Ã¥°Ãæ**</strong></div>\n");
31}
32
33/*
34 * Gets the total number of topics in a form
35 */
36function get_total_topics($forum_id="")
37{
38    global $xoopsDB;
39    if ( $forum_id ) {
40        $sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix("bb_topics")." WHERE forum_id = $forum_id";
41    } else {
42        $sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix("bb_topics");
43    }
44    if ( !$result = $xoopsDB->query($sql) ) {
45        return _MD_ERROR;
46    }
47
48    if ( !$myrow = $xoopsDB->fetchArray($result) ) {
49        return _MD_ERROR;
50    }
51
52    return $myrow['total'];
53}
54
55/*
56 * Returns the total number of posts in the whole system, a forum, or a topic
57 * Also can return the number of users on the system.
58 */
59function get_total_posts($id, $type)
60{
61    global $xoopsDB;
62    switch ( $type ) {
63    case 'users':
64        $sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix("users")." WHERE (uid > 0) AND ( level >0 )";
65        break;
66    case 'all':
67        $sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix("bb_posts");
68        break;
69    case 'forum':
70        $sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix("bb_posts")." WHERE forum_id = $id";
71        break;
72    case 'topic':
73        $sql = "SELECT COUNT(*) AS total FROM ".$xoopsDB->prefix("bb_posts")." WHERE topic_id = $id";
74        break;
75    // Old, we should never get this.
76    case 'user':
77        exit("Should be using the users.user_posts column for this.");
78    }
79    if ( !$result = $xoopsDB->query($sql) ) {
80        return "ERROR";
81    }
82    if ( !$myrow = $xoopsDB->fetchArray($result) ) {
83        return 0;
84    }
85    return $myrow['total'];
86}
87
88/*
89 * Returns the most recent post in a forum, or a topic
90 */
91function get_last_post($id, $type)
92{
93    global $xoopsDB;
94    switch ( $type ) {
95    case 'time_fix':
96        $sql = "SELECT post_time FROM ".$xoopsDB->prefix("bb_posts")." WHERE topic_id = $id ORDER BY post_time DESC";
97        break;
98    case 'forum':
99        $sql = "SELECT p.post_time, p.uid, u.uname FROM ".$xoopsDB->prefix("bb_posts")." p, ".$xoopsDB->prefix("users")." u WHERE p.forum_id = $id AND p.uid = u.uid ORDER BY post_time DESC";
100        break;
101    case 'topic':
102        $sql = "SELECT p.post_time, u.uname FROM ".$xoopsDB->prefix("bb_posts")." p, ".$xoopsDB->prefix("users")." u WHERE p.topic_id = $id AND p.uid = u.uid ORDER BY post_time DESC";
103        break;
104    case 'user':
105        $sql = "SELECT post_time FROM ".$xoopsDB->prefix("bb_posts")." WHERE uid = $id";
106        break;
107    }
108    if ( !$result = $xoopsDB->query($sql,1,0) ) {
109        return _MD_ERROR;
110    }
111    if ( !$myrow = $xoopsDB->fetchArray($result) ) {
112        return _MD_NOPOSTS;
113    }
114    if ( ($type != 'user') && ($type != 'time_fix') ) {
115        $val = sprintf("%s <br /> %s %s", $myrow['post_time'], _MD_BY, $myrow['uname']);
116    } else {
117        $val = $myrow['post_time'];
118    }
119    return $val;
120}
121
122/*
123 * Returns an array of all the moderators of a forum
124 */
125function get_moderators($forum_id)
126{
127    global $xoopsDB;
128    $sql = "SELECT u.uid, u.uname FROM ".$xoopsDB->prefix("users")." u, ".$xoopsDB->prefix("bb_forum_mods")." f WHERE f.forum_id = $forum_id and f.user_id = u.uid";
129    //echo $sql;
130    if ( !$result = $xoopsDB->query($sql) ) {
131        return array();
132    }
133    if ( !$myrow = $xoopsDB->fetchArray($result) ) {
134        return array();
135    }
136    do {
137        $array[] = array($myrow['uid'] => $myrow['uname']);
138    } while ( $myrow = $xoopsDB->fetchArray($result) );
139    return $array;
140}
141
142/*
143 * Checks if a user (user_id) is a moderator of a perticular forum (forum_id)
144 * Retruns 1 if TRUE, 0 if FALSE or Error
145 */
146function is_moderator($forum_id, $user_id)
147{
148    global $xoopsDB;
149    $sql = "SELECT COUNT(*) FROM ".$xoopsDB->prefix("bb_forum_mods")." WHERE forum_id = $forum_id AND user_id = $user_id";
150    $ret = false;
151    if ( $result = $xoopsDB->query($sql) ) {
152        if ( $myrow = $xoopsDB->fetchRow($result) ) {
153            if ( $myrow[0] > 0 ) {
154                $ret = true;
155            }
156        }
157    }
158    return $ret;
159}
160
161/*
162 * Checks if a topic is locked
163 */
164function is_locked($topic)
165{
166    global $xoopsDB;
167    $ret = false;
168    $sql = "SELECT topic_status FROM ".$xoopsDB->prefix("bb_topics")." WHERE topic_id = $topic";
169    if ( $r = $xoopsDB->query($sql) ) {
170        if ( $m = $xoopsDB->fetchArray($r) ) {
171            if ( $m['topic_status'] == 1 ) {
172                $ret = true;
173            }
174        }
175    }
176    return $ret;
177}
178
179/**
180 * Checks if the given userid is allowed to log into the given (private) forumid.
181 * If the "is_posting" flag is true, checks if the user is allowed to post to that forum.
182 */
183function check_priv_forum_auth($userid, $forumid, $is_posting)
184{
185    global $xoopsDB;
186    $sql = "SELECT count(*) AS user_count FROM ".$xoopsDB->prefix("bb_forum_access")." WHERE (user_id = $userid) AND (forum_id = $forumid) ";
187
188    if ( $is_posting ) {
189        $sql .= "AND (can_post = 1)";
190    }
191
192    if ( !$result = $xoopsDB->query($sql) ) {
193        // no good..
194        return false;
195    }
196
197    if ( !$row = $xoopsDB->fetchArray($result) ) {
198        return false;
199    }
200
201    if ( $row['user_count'] <= 0 ) {
202        return false;
203    }
204
205    return true;
206}
207
208function make_jumpbox($selected=0)
209{
210    global $xoopsDB;
211    $myts = MyTextSanitizer::getInstance();
212    $box = '<form action="viewforum.php" method="get">
213    <select name="forum">
214    ';
215    $sql = 'SELECT cat_id, cat_title FROM '.$xoopsDB->prefix('bb_categories').' ORDER BY cat_order';
216    if ( $result = $xoopsDB->query($sql) ) {
217        $myrow = $xoopsDB->fetchArray($result);
218        $myrow['cat_title'] = $myts->makeTboxData4Show($myrow['cat_title']);
219        do {
220            $box .= '<option value="-1">________________</option>';
221            $box .= '<option value="-1">'.$myrow['cat_title'].'</option>';
222            //$box .= "<option value=\"-1\">----------------</option>\n";
223            $sub_sql = "SELECT forum_id, forum_name FROM ".$xoopsDB->prefix("bb_forums")." WHERE cat_id ='".$myrow['cat_id']."' ORDER BY forum_id";
224            if ( $res = $xoopsDB->query($sub_sql) ) {
225                if ( $row = $xoopsDB->fetchArray($res) ) {
226                    do {
227                        $name = $myts->makeTboxData4Show($row['forum_name']);
228                        $box .= "<option value='".$row['forum_id']."'";
229                        if ( !empty($selected) && $row['forum_id'] == $selected ) {
230                            $box .= ' selected="selected"';
231                        }
232                        $box .= ">&nbsp;&nbsp;- $name</option>\n";
233                    } while ( $row = $xoopsDB->fetchArray($res) );
234                }
235            } else {
236                $box .= "<option value=\"0\">ERROR</option>\n";
237            }
238        } while ( $myrow = $xoopsDB->fetchArray($result) );
239    } else {
240        $box .= "<option value=\"-1\">ERROR</option>\n";
241    }
242    $box .= "</select>\n<input type=\"submit\" class=\"formButton\" value=\""._MD_GO."\" />\n</form>";
243    return $box;
244}
245
246function sync($id, $type)
247{
248    global $xoopsDB;
249    switch ( $type ) {
250    case 'forum':
251        $sql = "SELECT MAX(post_id) AS last_post FROM ".$xoopsDB->prefix("bb_posts")." WHERE forum_id = $id";
252        if ( !$result = $xoopsDB->query($sql) ) {
253            exit("Could not get post ID");
254        }
255        if ( $row = $xoopsDB->fetchArray($result) ) {
256            $last_post = $row['last_post'];
257        }
258
259        $sql = "SELECT COUNT(post_id) AS total FROM ".$xoopsDB->prefix("bb_posts")." WHERE forum_id = $id";
260        if ( !$result = $xoopsDB->query($sql) ) {
261            exit("Could not get post count");
262        }
263        if ( $row = $xoopsDB->fetchArray($result) ) {
264            $total_posts = $row['total'];
265        }
266
267        $sql = "SELECT COUNT(topic_id) AS total FROM ".$xoopsDB->prefix("bb_topics")." WHERE forum_id = $id";
268        if ( !$result = $xoopsDB->query($sql) ) {
269            exit("Could not get topic count");
270        }
271        if ( $row = $xoopsDB->fetchArray($result) ) {
272            $total_topics = $row['total'];
273        }
274
275        $sql = sprintf("UPDATE %s SET forum_last_post_id = %u, forum_posts = %u, forum_topics = %u WHERE forum_id = %u", $xoopsDB->prefix("bb_forums"), $last_post, $total_posts, $total_topics, $id);
276        if ( !$result = $xoopsDB->queryF($sql) ) {
277            exit("Could not update forum $id");
278        }
279        break;
280    case 'topic':
281        $sql = "SELECT max(post_id) AS last_post FROM ".$xoopsDB->prefix("bb_posts")." WHERE topic_id = $id";
282        if ( !$result = $xoopsDB->query($sql) ) {
283            exit("Could not get post ID");
284        }
285        if ( $row = $xoopsDB->fetchArray($result) ) {
286            $last_post = $row['last_post'];
287        }
288        if ( $last_post > 0 ) {
289            $sql = "SELECT COUNT(post_id) AS total FROM ".$xoopsDB->prefix("bb_posts")." WHERE topic_id = $id";
290            if ( !$result = $xoopsDB->query($sql) ) {
291                exit("Could not get post count");
292            }
293            if ( $row = $xoopsDB->fetchArray($result) ) {
294                $total_posts = $row['total'];
295            }
296            $total_posts -= 1;
297            $sql = sprintf("UPDATE %s SET topic_replies = %u, topic_last_post_id = %u WHERE topic_id = %u", $xoopsDB->prefix("bb_topics"), $total_posts, $last_post, $id);
298            if ( !$result = $xoopsDB->queryF($sql) ) {
299                exit("Could not update topic $id");
300            }
301        }
302        break;
303    case 'all forums':
304        $sql = "SELECT forum_id FROM ".$xoopsDB->prefix("bb_forums");
305        if ( !$result = $xoopsDB->query($sql) ) {
306            exit("Could not get forum IDs");
307        }
308        while ( $row = $xoopsDB->fetchArray($result) ) {
309            $id = $row['forum_id'];
310            sync($id, "forum");
311        }
312        break;
313    case 'all topics':
314        $sql = "SELECT topic_id FROM ".$xoopsDB->prefix("bb_topics");
315        if ( !$result = $xoopsDB->query($sql) ) {
316            exit("Could not get topic ID's");
317        }
318        while ( $row = $xoopsDB->fetchArray($result) ) {
319            $id = $row['topic_id'];
320            sync($id, "topic");
321        }
322        break;
323    }
324    return true;
325}
326
327// Functions for unserialize() vulnerability in < 4.3.10,
328// based on the code provided by GIJOE
329// Servers with 4.3.10 or up can use the code with serialize/unserialize
330// functions, as commented out below
331function newbb_get_topics_viewed()
332{
333    if (empty($_COOKIE['newbb_topics_viewed'])) {
334        return array();
335    }
336    $topics_tmp = explode(',', $_COOKIE['newbb_topics_viewed']);
337    $topics = array();
338    foreach ($topics_tmp as $tmp) {
339        $idmin = explode('|', $tmp);
340        $id = empty($idmin[0]) ? 0 : intval($idmin[0]);
341        $min = empty($idmin[1]) ? 0 : intval($idmin[1]);
342        $topics[$id] = $min * 60 ;
343    }
344    //$topics = !empty($_COOKIE['newbb_topic_lastread']) ? unserialize($_COOKIE['newbb_topic_lastread']) : array();
345    return $topics;
346}
347
348function newbb_add_topics_viewed($topicsViewed, $topicId, $timeViewed, $cookiePath, $cookieDomain, $cookieSecure)
349{
350    $topicsViewed[$topicId] = time();
351    arsort($topicsViewed);
352    $counter = 300 ;
353    foreach (array_keys($topicsViewed) as $id) {
354        $tmp[] = intval($id) . '|' . intval(ceil($topicsViewed[$id] / 60));
355        if (--$counter < 0) {
356            break;
357        }
358    }
359    setcookie('newbb_topics_viewed', implode(',', $tmp), time()+365*24*3600, $cookiePath, $cookieDomain, $cookieSecure);
360    //$topicsViewed[$topicId] = time();
361    //setcookie('newbb_topic_lastread', serialize($topicsViewed), time()+365*24*3600, $cookiePath, $cookieDomain, $cookieSecure);
362}
363?>
Note: See TracBrowser for help on using the repository browser.