Subversion Repositories svnkaklik

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
6 kaklik 1
<?php
2
/*************************
3
  Coppermine Photo Gallery
4
  ************************
5
  Copyright (c) 2003-2005 Coppermine Dev Team
6
  v1.1 originaly written by Gregory DEMAR
7
 
8
  This program is free software; you can redistribute it and/or modify
9
  it under the terms of the GNU General Public License as published by
10
  the Free Software Foundation; either version 2 of the License, or
11
  (at your option) any later version.
12
  ********************************************
13
  Coppermine version: 1.3.3
14
  $Source: /cvsroot/coppermine/stable/bridge/punbb.inc.php,v $
15
  $Revision: 1.3 $
16
  $Author: gaugau $
17
  $Date: 2005/04/19 03:17:13 $
18
**********************************************/
19
 
20
// ------------------------------------------------------------------------- //
21
// PunBB 1.1.5 Integration for Coppermine                                    //
22
// ------------------------------------------------------------------------- //
23
// Modify the values below according to your Board installation              //
24
// ------------------------------------------------------------------------- //
25
 
26
// URL of your punbb
27
$path = 'http://www.mysite.com/punbb';
28
 
29
// local path to your punbb config file
30
require_once('../punbb/config.php');
31
 
32
// punbb mod == cpg Admin ?
33
define('MOD_IS_ADMIN', FALSE);
34
 
35
// ------------------------------------------------------------------------- //
36
// Nothing to edit below this line
37
// ------------------------------------------------------------------------- //
38
 
39
// automatic configuration
40
define('PUNBB_DB_NAME', $db_name); // The name of the database used by the board
41
define('PUNBB_DB_HOST', $db_host); // The name of the database server
42
define('PUNBB_DB_USERNAME', $db_username); // The username to use to connect to the database
43
define('PUNBB_DB_PASSWORD', $db_password); // The password to use to connect to the database
44
define('PUNBB_TABLE_PREFIX', $db_prefix); // The prefix used for the DB tables
45
define('PUNBB_WEB_PATH', $path); // The prefix used for the DB tables
46
define('PUNBB_USER_TABLE', 'users'); // The members table
47
 
48
// Group definitions
49
define('PUNBB_MOD_GROUP', 4);
50
define('PUNBB_GUEST_GROUP', 3);
51
define('PUNBB_MEMBERS_GROUP', 2);
52
define('PUNBB_ADMIN_GROUP', 1);
53
 
54
function udb_authenticate()
55
{
56
    global $USER_DATA, $CONFIG, $cookie_name, $UDB_DB_LINK_ID, $UDB_DB_NAME_PREFIX;
57
 
58
    // For error checking
59
    $CONFIG['TABLE_USERS'] = '**ERROR**';
60
 
61
    function unescape($str)
62
        {
63
                return (get_magic_quotes_gpc() == 1) ? stripslashes($str) : $str;
64
        }
65
 
66
    // Retrieve cookie stored login information
67
 
68
    // default user info
69
        $USER_DATA['user_id'] = 0;
70
        $USER_DATA['status'] = -1;
71
        $USER_DATA['user_name'] = 'Guest';
72
 
73
        if (isset($_COOKIE[$cookie_name]))
74
        {
75
                list($cookie['username'], $cookie['password_hash']) = unserialize(unescape($_COOKIE[$cookie_name]));
76
 
77
                if (strcasecmp($cookie['username'], 'Guest'))
78
                {
79
                        $result = db_query("SELECT id AS user_id, username AS user_name, status FROM ". $UDB_DB_NAME_PREFIX . PUNBB_TABLE_PREFIX . PUNBB_USER_TABLE ." WHERE username='" . addslashes($cookie['username']). "' AND password='". addslashes($cookie['password_hash'])."'",$UDB_DB_LINK_ID);
80
                        $USER_DATA = mysql_fetch_assoc($result);
81
                }
82
        }
83
 
84
        $USER_DATA['groups'] = array();
85
 
86
        // Define the basic groups
87
 
88
        switch ($USER_DATA['status']) {
89
 
90
                case 0:
91
                        $USER_DATA['groups'][0] = PUNBB_MEMBERS_GROUP;
92
                        break;
93
                case 1:
94
                        $USER_DATA['groups'][0] = PUNBB_MOD_GROUP;
95
                        break;
96
                case 2:
97
                        $USER_DATA['groups'][0] = PUNBB_ADMIN_GROUP;
98
                        break;
99
                default:
100
                        $USER_DATA['groups'][0] = PUNBB_GUEST_GROUP;
101
                        break;
102
        }
103
 
104
        if ($USER_DATA['status'] == -1) {
105
                define('USER_ID', 0);
106
        } else {
107
                define('USER_ID', (int)$USER_DATA['user_id']);
108
        }
109
 
110
        $user_group_set = '(' . implode(',', $USER_DATA['groups']) . ')';
111
 
112
        // Default group data
113
        $USER_DATA['group_quota'] = 1;
114
        $USER_DATA['can_rate_pictures'] = 0;
115
        $USER_DATA['can_send_ecards'] = 0;
116
        $USER_DATA['can_post_comments'] = 0;
117
        $USER_DATA['can_upload_pictures'] = 0;
118
        $USER_DATA['can_create_albums'] = 0;
119
        $USER_DATA['pub_upl_need_approval'] = 1;
120
        $USER_DATA['priv_upl_need_approval'] = 1;
121
        $USER_DATA['upload_form_config'] = 0;
122
        $USER_DATA['num_file_upload'] = 0;
123
        $USER_DATA['num_URI_upload'] = 0;
124
        $USER_DATA['custom_user_upload'] = 0;
125
 
126
        $USER_DATA = array_merge($USER_DATA, cpgGetUserData($USER_DATA['groups'][0], $USER_DATA['groups'], PUNBB_GUEST_GROUP));
127
 
128
        $USER_DATA['has_admin_access'] = (($USER_DATA['status'] == 2) || (($USER_DATA['status'] == 1) && MOD_IS_ADMIN)) ? 1 : 0;
129
        $USER_DATA['can_see_all_albums'] = $USER_DATA['has_admin_access'];
130
 
131
        define('USER_NAME', $USER_DATA['user_name']);
132
        define('USER_GROUP', $USER_DATA['group_name']);
133
        define('USER_GROUP_SET', $user_group_set);
134
        define('USER_IS_ADMIN', $USER_DATA['has_admin_access']);
135
        define('USER_CAN_SEND_ECARDS', (int)$USER_DATA['can_send_ecards']);
136
        define('USER_CAN_RATE_PICTURES', (int)$USER_DATA['can_rate_pictures']);
137
        define('USER_CAN_POST_COMMENTS', (int)$USER_DATA['can_post_comments']);
138
        define('USER_CAN_UPLOAD_PICTURES', (int)$USER_DATA['can_upload_pictures']);
139
        define('USER_CAN_CREATE_ALBUMS', (int)$USER_DATA['can_create_albums']);
140
        define('USER_UPLOAD_FORM', (int)$USER_DATA['upload_form_config']);
141
        define('CUSTOMIZE_UPLOAD_FORM', (int)$USER_DATA['custom_user_upload']);
142
        define('NUM_FILE_BOXES', (int)$USER_DATA['num_file_upload']);
143
        define('NUM_URI_BOXES', (int)$USER_DATA['num_URI_upload']);
144
}
145
 
146
// Retrieve the name of a user
147
function udb_get_user_name($uid)
148
{
149
    global $CONFIG, $UDB_DB_LINK_ID, $UDB_DB_NAME_PREFIX;
150
 
151
    $sql = "SELECT username as user_name FROM " . $UDB_DB_NAME_PREFIX . PUNBB_TABLE_PREFIX . PUNBB_USER_TABLE . " WHERE id = '$uid'";
152
 
153
    $result = db_query($sql, $UDB_DB_LINK_ID);
154
 
155
    if (mysql_num_rows($result)) {
156
        $row = mysql_fetch_array($result);
157
        mysql_free_result($result);
158
        return $row['user_name'];
159
    } else {
160
        return '';
161
    }
162
}
163
// Retrieve the id of a user (Added to fix banning w/ bb integration - Nibbler)
164
function udb_get_user_id($username)
165
{
166
    global $CONFIG, $UDB_DB_LINK_ID, $UDB_DB_NAME_PREFIX;
167
 
168
    $username = addslashes($username);
169
 
170
    $sql = "SELECT id AS user_id FROM " . $UDB_DB_NAME_PREFIX . PUNBB_TABLE_PREFIX . PUNBB_USER_TABLE . " WHERE username = '$username'";
171
 
172
    $result = db_query($sql, $UDB_DB_LINK_ID);
173
 
174
    if (mysql_num_rows($result)) {
175
        $row = mysql_fetch_array($result);
176
        mysql_free_result($result);
177
        return $row['user_id'];
178
    } else {
179
        return '';
180
    }
181
}
182
 
183
// Redirect
184
function udb_redirect($target)
185
{
186
    header('Location: '. PUNBB_WEB_PATH . $target);
187
    exit;
188
}
189
 
190
// Register
191
function udb_register_page()
192
{
193
    $target = '/register.php';
194
    udb_redirect($target);
195
}
196
// Login
197
function udb_login_page()
198
{
199
        global $path;
200
 
201
        echo '<html><body onload="document.redir.submit();"><form name="redir" method="post" action="'.$path.'/redir.php"><input type="hidden" name="redir" value="login.php" /></form></body></html>';
202
        exit;
203
}
204
// Logout
205
function udb_logout_page()
206
{
207
        global $path;
208
 
209
        echo '<html><body onload="document.redir.submit();"><form name="redir" method="post" action="'.$path.'/redir.php"><input type="hidden" name="redir" value="login.php?action=out" /></form></body></html>';
210
        exit;
211
}
212
// Edit users
213
function udb_edit_users()
214
{
215
    $target = '/userlist.php';
216
    udb_redirect($target);
217
}
218
// Get user information
219
function udb_get_user_infos($uid)
220
{
221
        global $UDB_DB_NAME_PREFIX, $UDB_DB_LINK_ID, $lang_register_php;
222
 
223
    $sql = "SELECT username AS user_name, email AS user_email, registered AS user_regdate, location AS user_location, url AS user_website FROM " . $UDB_DB_NAME_PREFIX . PUNBB_TABLE_PREFIX . PUNBB_USER_TABLE . " WHERE id = '$uid'";
224
    $result = db_query($sql, $UDB_DB_LINK_ID);
225
    if (!mysql_num_rows($result)) cpg_die(ERROR, $lang_register_php['err_unk_user'], __FILE__, __LINE__);
226
 
227
    $user_data = mysql_fetch_array($result);
228
    $user_data['group_name'] = '';
229
    mysql_free_result($result);
230
 
231
    return $user_data;
232
}
233
 
234
// Edit user profile
235
function udb_edit_profile($uid)
236
{
237
    $target = "/profile.php?id=$uid";
238
    udb_redirect($target);
239
}
240
 
241
// Query used to list users
242
function udb_list_users_query(&$user_count)
243
{
244
    global $CONFIG, $FORBIDDEN_SET, $UDB_DB_LINK_ID;
245
 
246
    if ($FORBIDDEN_SET != "") $forbidden = "AND $FORBIDDEN_SET";
247
    $sql = "SELECT (category - " . FIRST_USER_CAT . ") as user_id," . "        '???' as user_name," . "        COUNT(DISTINCT a.aid) as alb_count," . "        COUNT(DISTINCT pid) as pic_count," . "        MAX(pid) as thumb_pid " . "FROM {$CONFIG['TABLE_ALBUMS']} AS a " . "INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.aid = a.aid " . "WHERE approved = 'YES' AND category > " . FIRST_USER_CAT . " $forbidden GROUP BY category " . "ORDER BY category ";
248
    $result = db_query($sql, $UDB_DB_LINK_ID);
249
 
250
    $user_count = mysql_num_rows($result);
251
 
252
    return $result;
253
}
254
 
255
function udb_list_users_retrieve_data($result, $lower_limit, $count)
256
{
257
    global $CONFIG, $UDB_DB_NAME_PREFIX, $UDB_DB_LINK_ID;
258
 
259
    mysql_data_seek($result, $lower_limit);
260
 
261
    $rowset = array();
262
    $i = 0;
263
    $user_id_set = '';
264
 
265
    while (($row = mysql_fetch_array($result)) && ($i++ < $count)) {
266
        $user_id_set .= $row['user_id'] . ',';
267
        $rowset[] = $row;
268
    }
269
    mysql_free_result($result);
270
 
271
    $user_id_set = '(' . substr($user_id_set, 0, -1) . ')';
272
    $sql = "SELECT id AS user_id, username AS user_name FROM " . $UDB_DB_NAME_PREFIX . PUNBB_TABLE_PREFIX . PUNBB_USER_TABLE . " WHERE id IN $user_id_set";
273
    $result = db_query($sql, $UDB_DB_LINK_ID);
274
    while ($row = mysql_fetch_array($result)) {
275
        $name[$row['user_id']] = $row['user_name'];
276
    }
277
    for($i = 0; $i < count($rowset); $i++) {
278
        $rowset[$i]['user_name'] = empty($name[$rowset[$i]['user_id']]) ? '???' : $name[$rowset[$i]['user_id']];
279
    }
280
 
281
    return $rowset;
282
}
283
 
284
// Group table synchronisation
285
function udb_synchronize_groups()
286
{
287
    global $CONFIG ;
288
 
289
    $PUNBB_groups = array(
290
            PUNBB_GUEST_GROUP => 'Guests',
291
            PUNBB_MEMBERS_GROUP => 'Members',
292
        PUNBB_ADMIN_GROUP => 'Administrators',
293
        PUNBB_MOD_GROUP => 'Moderators'
294
        );
295
 
296
    $result = db_query("SELECT group_id, group_name FROM {$CONFIG['TABLE_USERGROUPS']} WHERE 1");
297
    while ($row = mysql_fetch_array($result)) {
298
        $cpg_groups[$row['group_id']] = $row['group_name'];
299
    }
300
    mysql_free_result($result);
301
    // Scan Coppermine groups that need to be deleted
302
    foreach($cpg_groups as $c_group_id => $c_group_name) {
303
        if ((!isset($PUNBB_groups[$c_group_id]))) {
304
            db_query("DELETE FROM {$CONFIG['TABLE_USERGROUPS']} WHERE group_id = '" . $c_group_id . "' LIMIT 1");
305
            unset($cpg_groups[$c_group_id]);
306
        }
307
    }
308
    // Scan punBB groups that need to be created inside Coppermine table
309
    foreach($PUNBB_groups as $i_group_id => $i_group_name) {
310
        if ((!isset($cpg_groups[$i_group_id]))) {
311
            db_query("INSERT INTO {$CONFIG['TABLE_USERGROUPS']} (group_id, group_name) VALUES ('$i_group_id', '" . addslashes($i_group_name) . "')");
312
            $cpg_groups[$i_group_id] = $i_group_name;
313
        }
314
    }
315
    // Update Group names
316
    foreach($PUNBB_groups as $i_group_id => $i_group_name) {
317
        if ($cpg_groups[$i_group_id] != $i_group_name) {
318
            db_query("UPDATE {$CONFIG['TABLE_USERGROUPS']} SET group_name = '" . addslashes($i_group_name) . "' WHERE group_id = '$i_group_id' LIMIT 1");
319
        }
320
    }
321
}
322
// Retrieve the album list used in gallery admin mode
323
function udb_get_admin_album_list()
324
{
325
    global $CONFIG, $UDB_DB_NAME_PREFIX, $UDB_DB_LINK_ID, $FORBIDDEN_SET;
326
 
327
    if (UDB_CAN_JOIN_TABLES) {
328
        $sql = "SELECT aid, CONCAT('(', username, ') ', a.title) AS cpg_title " . "FROM {$CONFIG['TABLE_ALBUMS']} AS a " . "INNER JOIN " . $UDB_DB_NAME_PREFIX . PUNBB_TABLE_PREFIX . PUNBB_USER_TABLE . " AS u ON category = (" . FIRST_USER_CAT . " + id) " . "ORDER BY cpg_title";
329
        return $sql;
330
    } else {
331
        $sql = "SELECT aid, IF(category > " . FIRST_USER_CAT . ", CONCAT('* ', a.title), CONCAT(' ', title)) AS cpg_title " . "FROM {$CONFIG['TABLE_ALBUMS']} " . "ORDER BY cpg_title";
332
        return $sql;
333
    }
334
}
335
 
336
function udb_util_filloptions()
337
{
338
    global $albumtbl, $picturetbl, $categorytbl, $lang_util_php, $CONFIG, $UDB_DB_NAME_PREFIX, $UDB_DB_LINK_ID;
339
 
340
    $usertbl = $UDB_DB_NAME_PREFIX . PUNBB_TABLE_PREFIX . PUNBB_USER_TABLE;
341
 
342
    if (UDB_CAN_JOIN_TABLES) {
343
 
344
        $query = "SELECT aid, category, IF(username IS NOT NULL, CONCAT('(', username, ') ', a.title), CONCAT(' - ', a.title)) AS title " . "FROM {$CONFIG['TABLE_ALBUMS']} AS a " . "LEFT JOIN $usertbl AS u ON category = (" . FIRST_USER_CAT . " + id) " . "ORDER BY category, title";
345
        $result = db_query($query, $UDB_DB_LINK_ID);
346
        // $num=mysql_numrows($result);
347
        echo '<select size="1" name="albumid">';
348
 
349
        while ($row = mysql_fetch_array($result)) {
350
            $sql = "SELECT name FROM {$CONFIG['TABLE_CATEGORIES']} WHERE cid = " . $row["category"];
351
            $result2 = db_query($sql);
352
            $row2 = mysql_fetch_array($result2);
353
 
354
            print "<option value=\"" . $row["aid"] . "\">" . $row2["name"] . $row["title"] . "</option>\n";
355
        }
356
 
357
        print '</select> (3)';
358
        print '&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="'.$lang_util_php['submit_form'].'" class="submit" /> (4)';
359
        print '</form>';
360
 
361
    } else {
362
 
363
        // Query for list of public albums
364
 
365
        $public_albums = db_query("SELECT aid, a.title AS cpg_title, category FROM {$CONFIG['TABLE_ALBUMS']} WHERE category < " . FIRST_USER_CAT . " ORDER BY cpg_title");
366
 
367
        if (mysql_num_rows($public_albums)) {
368
            $public_result = db_fetch_rowset($public_albums);
369
        } else {
370
            $public_result = array();
371
        }
372
 
373
        // Initialize $merged_array
374
        $merged_array = array();
375
 
376
        // Count the number of albums returned.
377
        $end = count($public_result);
378
 
379
        // Cylce through the User albums.
380
        for($i=0;$i<$end;$i++) {
381
 
382
            //Create a new array sow we may sort the final results.
383
            $merged_array[$i]['id'] = $public_result[$i]['aid'];
384
            $merged_array[$i]['album_name'] = $public_result[$i]['title'];
385
 
386
            // Query the database to get the category name.
387
            $vQuery = "SELECT name, parent FROM " . $CONFIG['TABLE_CATEGORIES'] . " WHERE cid='" . $public_result[$i]['category'] . "'";
388
            $vRes = mysql_query($vQuery);
389
            $vRes = mysql_fetch_array($vRes);
390
            if (isset($merged_array[$i]['username_category'])) {
391
                $merged_array[$i]['username_category'] = (($vRes['name']) ? '(' . $vRes['name'] . ') ' : '').$merged_array[$i]['username_category'];
392
            } else {
393
                $merged_array[$i]['username_category'] = (($vRes['name']) ? '(' . $vRes['name'] . ') ' : '');
394
            }
395
 
396
        }
397
 
398
        // We transpose and divide the matrix into columns to prepare it for use in array_multisort().
399
        foreach ($merged_array as $key => $row) {
400
           $aid[$key] = $row['id'];
401
           $title[$key] = $row['album_name'];
402
           $album_lineage[$key] = $row['username_category'];
403
        }
404
 
405
        // We sort all columns in descending order and plug in $album_menu at the end so it is sorted by the common key.
406
        array_multisort($album_lineage, SORT_ASC, $title, SORT_ASC, $aid, SORT_ASC, $merged_array);
407
 
408
        // Query for list of user albums
409
 
410
        $user_albums = db_query("SELECT aid, title, category FROM {$CONFIG['TABLE_ALBUMS']} WHERE category >= " . FIRST_USER_CAT . " ORDER BY aid");
411
        if (mysql_num_rows($user_albums)) {
412
            $user_albums_list = db_fetch_rowset($user_albums);
413
        } else {
414
            $user_albums_list = array();
415
        }
416
 
417
        // Query for list of user IDs and names
418
 
419
        $user_album_ids_and_names = db_query("SELECT (id + ".FIRST_USER_CAT.") as id, CONCAT('(', username, ') ') as name FROM $usertbl ORDER BY name ASC",$UDB_DB_LINK_ID);
420
 
421
        if (mysql_num_rows($user_album_ids_and_names)) {
422
            $user_album_ids_and_names_list = db_fetch_rowset($user_album_ids_and_names);
423
        } else {
424
            $user_album_ids_and_names_list = array();
425
        }
426
 
427
        // Glue what we've got together.
428
 
429
        // Initialize $udb_i as a counter.
430
        if (count($merged_array)) {
431
            $udb_i = count($merged_array);
432
        } else {
433
            $udb_i = 0;
434
        }
435
 
436
        //Begin a set of nested loops to merge the various query results.
437
        foreach ($user_albums_list as $aq) {
438
            foreach ($user_album_ids_and_names_list as $uq) {
439
                if ($aq['category'] == $uq['id']) {
440
                    $merged_array[$udb_i]['id']= $aq['category'];
441
                    $merged_array[$udb_i]['album_name']= $aq['title'];
442
                    $merged_array[$udb_i]['username_category']= $uq['name'];
443
                    $udb_i++;
444
                }
445
            }
446
        }
447
 
448
        // The user albums and public albums have been merged into one list. Print the dropdown.
449
        echo '<select size="1" name="albumid">';
450
 
451
        foreach ($merged_array as $menu_item) {
452
 
453
            echo "<option value=\"" . $menu_item['id'] . "\">" . (isset($menu_item['username_category']) ? $menu_item['username_category'] : '') . $menu_item['album_name'] . "</option>\n";
454
 
455
        }
456
 
457
        // Close list, etc.
458
        print '</select> (3)';
459
        print '&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="'.$lang_util_php['submit_form'].'" class="submit" /> (4)';
460
        print '</form>';
461
 
462
    }
463
 
464
}
465
 
466
// ------------------------------------------------------------------------- //
467
 
468
// Define wheter we can join tables or not in SQL queries (same host & same db or user)
469
define('UDB_CAN_JOIN_TABLES', (PUNBB_DB_HOST == $CONFIG['dbserver'] && (PUNBB_DB_NAME == $CONFIG['dbname'] || PUNBB_DB_USERNAME == $CONFIG['dbuser'])));
470
// Connect to SMF Database if necessary
471
$UDB_DB_LINK_ID = 0;
472
$UDB_DB_NAME_PREFIX = PUNBB_DB_NAME ? '`' . PUNBB_DB_NAME . '`.' : '';
473
if (!UDB_CAN_JOIN_TABLES) {
474
    $UDB_DB_LINK_ID = @mysql_connect(PUNBB_DB_HOST, PUNBB_DB_USERNAME, PUNBB_DB_PASSWORD);
475
 
476
    if (!$UDB_DB_LINK_ID) die("<b>Coppermine critical error</b>:<br />Unable to connect to PunBB database !<br /><br />MySQL said: <b>" . mysql_error() . "</b>");
477
    mysql_select_db (PUNBB_DB_NAME, $UDB_DB_LINK_ID);
478
}
479
?>