aboutsummaryrefslogtreecommitdiffstats
path: root/admin/group_list.php
blob: 9691f4913bee87ea797813da246607b540270a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery                                    |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2016 Piwigo Team                  http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

if( !defined("PHPWG_ROOT_PATH") )
{
  die ("Hacking attempt!");
}

include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');

// +-----------------------------------------------------------------------+
// | tabs                                                                  |
// +-----------------------------------------------------------------------+

include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');

$my_base_url = get_root_url().'admin.php?page=';

$tabsheet = new tabsheet();
$tabsheet->set_id('groups');
$tabsheet->select('group_list');
$tabsheet->assign();

// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok                      |
// +-----------------------------------------------------------------------+
check_status(ACCESS_ADMINISTRATOR);

if (!empty($_POST) or isset($_GET['delete']) or isset($_GET['toggle_is_default']))
{
  check_pwg_token();
}
// +-----------------------------------------------------------------------+
// |                              add a group                              |
// +-----------------------------------------------------------------------+

if (isset($_POST['submit_add']))
{
  if (empty($_POST['groupname']))
  {
    $page['errors'][] = l10n('The name of a group must not contain " or \' or be empty.');
  }
  if (count($page['errors']) == 0)
  {
    // is the group not already existing ?
    $query = '
SELECT COUNT(*)
  FROM '.GROUPS_TABLE.'
  WHERE name = \''.$_POST['groupname'].'\'
;';
    list($count) = pwg_db_fetch_row(pwg_query($query));
    if ($count != 0)
    {
      $page['errors'][] = l10n('This name is already used by another group.');
    }
  }
  if (count($page['errors']) == 0)
  {
    // creating the group
    $query = '
INSERT INTO '.GROUPS_TABLE.'
  (name)
  VALUES
  (\''.pwg_db_real_escape_string($_POST['groupname']).'\')
;';
    pwg_query($query);

    $page['infos'][] = l10n('group "%s" added', $_POST['groupname']);
  }
}

// +-----------------------------------------------------------------------+
// |                             action send                               |
// +-----------------------------------------------------------------------+
if (isset($_POST['submit']) and isset($_POST['selectAction']) and isset($_POST['group_selection']))
{
  // if the user tries to apply an action, it means that there is at least 1
  // photo in the selection
  $groups = $_POST['group_selection'];
  if (count($groups) == 0)
  {
    $page['errors'][] = l10n('Select at least one group');
  }

  $action = $_POST['selectAction'];

  // +
  // |rename a group
  // +

  if ($action=="rename")
  {
    // is the group not already existing ?
    $query = '
SELECT name
  FROM '.GROUPS_TABLE.'
;';
    $group_names = array_from_query($query, 'name');
    foreach($groups as $group)
    {
      if (  in_array($_POST['rename_'.$group.''], $group_names))
      {
        $page['errors'][] = $_POST['rename_'.$group.''].' | '.l10n('This name is already used by another group.');
      }
      elseif ( !empty($_POST['rename_'.$group.'']))
      {
        $query = '
        UPDATE '.GROUPS_TABLE.'
        SET name = \''.pwg_db_real_escape_string($_POST['rename_'.$group.'']).'\'
        WHERE id = '.$group.'
      ;';
        pwg_query($query);
      }
    }
  }

  // +
  // |delete a group
  // +

  if ($action=="delete" and isset($_POST['confirm_deletion']) and $_POST['confirm_deletion'])
  {
    foreach($groups as $group)
    {
        // destruction of the access linked to the group
      $query = '
    DELETE
      FROM '.GROUP_ACCESS_TABLE.'
      WHERE group_id = '.$group.'
    ;';
      pwg_query($query);
      
      // destruction of the users links for this group
      $query = '
    DELETE
      FROM '.USER_GROUP_TABLE.'
      WHERE group_id = '.$group.'
    ;';
      pwg_query($query);
    
      $query = '
    SELECT name
      FROM '.GROUPS_TABLE.'
      WHERE id = '.$group.'
    ;';
      list($groupname) = pwg_db_fetch_row(pwg_query($query));
      
      // destruction of the group
      $query = '
    DELETE
      FROM '.GROUPS_TABLE.'
      WHERE id = '.$group.'
    ;';
      pwg_query($query);
    
      $page['infos'][] = l10n('group "%s" deleted', $groupname);
    }
  }

  // +
  // |merge groups into a new one
  // +

  if ($action=="merge" and count($groups) > 1)
  {
    // is the group not already existing ?
    $query = '
SELECT COUNT(*)
  FROM '.GROUPS_TABLE.'
  WHERE name = \''.pwg_db_real_escape_string($_POST['merge']).'\'
;';
    list($count) = pwg_db_fetch_row(pwg_query($query));
    if ($count != 0)
    {
      $page['errors'][] = l10n('This name is already used by another group.');
    }
    else
    {
      // creating the group
      $query = '
  INSERT INTO '.GROUPS_TABLE.'
    (name)
    VALUES
    (\''.pwg_db_real_escape_string($_POST['merge']).'\')
  ;';
      pwg_query($query);
      $query = '
      SELECT id
        FROM '.GROUPS_TABLE.'
        WHERE name = \''.pwg_db_real_escape_string($_POST['merge']).'\'
      ;';
      list($groupid) = pwg_db_fetch_row(pwg_query($query));
    }
    $grp_access = array();
    $usr_grp = array();
    foreach($groups as $group)
    {
      $query = '
    SELECT *
      FROM '.GROUP_ACCESS_TABLE.'
      WHERE group_id = '.$group.'
    ;';
      $res=pwg_query($query);
      while ($row = pwg_db_fetch_assoc($res))
      {
        $new_grp_access= array(
          'cat_id' => $row['cat_id'],
          'group_id' => $groupid
        );
        if (!in_array($new_grp_access,$grp_access))
        {
          $grp_access[]=$new_grp_access;
        }
      }

      $query = '
    SELECT *
      FROM '.USER_GROUP_TABLE.'
      WHERE group_id = '.$group.'
    ;';
      $res=pwg_query($query);
      while ($row = pwg_db_fetch_assoc($res))
      {
        $new_usr_grp= array(
          'user_id' => $row['user_id'],
          'group_id' => $groupid
        );
        if (!in_array($new_usr_grp,$usr_grp))
        {
          $usr_grp[]=$new_usr_grp;
        }
      }
    }
    mass_inserts(USER_GROUP_TABLE, array('user_id','group_id'), $usr_grp);
    mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $grp_access);
    
    $page['infos'][] = l10n('group "%s" added', $_POST['merge']);
  }
  
  // +
  // |duplicate a group
  // +

  if ($action=="duplicate" )
  {
    foreach($groups as $group)
    {
      if ( empty($_POST['duplicate_'.$group.'']) )
      {
        break;
      }
      // is the group not already existing ?
      $query = '
  SELECT COUNT(*)
    FROM '.GROUPS_TABLE.'
    WHERE name = \''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\'
  ;';
      list($count) = pwg_db_fetch_row(pwg_query($query));
      if ($count != 0)
      {
        $page['errors'][] = l10n('This name is already used by another group.');
        break;
      }
      // creating the group
      $query = '
  INSERT INTO '.GROUPS_TABLE.'
    (name)
    VALUES
    (\''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\')
  ;';
      pwg_query($query);
      $query = '
      SELECT id
        FROM '.GROUPS_TABLE.'
        WHERE name = \''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\'
      ;';
      
      list($groupid) = pwg_db_fetch_row(pwg_query($query));
      $query = '
    SELECT *
      FROM '.GROUP_ACCESS_TABLE.'
      WHERE group_id = '.$group.'
    ;';
      $grp_access = array();
      $res=pwg_query($query);
      while ($row = pwg_db_fetch_assoc($res))
      {
          $grp_access[] = array(
            'cat_id' => $row['cat_id'],
            'group_id' => $groupid
          );
      }
      mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $grp_access);

      $query = '
    SELECT *
      FROM '.USER_GROUP_TABLE.'
      WHERE group_id = '.$group.'
    ;';
      $usr_grp = array();
      $res=pwg_query($query);
      while ($row = pwg_db_fetch_assoc($res))
      {
          $usr_grp[] = array(
            'user_id' => $row['user_id'],
            'group_id' => $groupid
          );
      }
      mass_inserts(USER_GROUP_TABLE, array('user_id','group_id'), $usr_grp);
  
      $page['infos'][] = l10n('group "%s" added', $_POST['duplicate_'.$group.'']);
    }
  }


  // +
  // | toggle_default
  // +
  
  if ($action=="toggle_default")
  {
    foreach($groups as $group)
    {
      $query = '
    SELECT name, is_default
      FROM '.GROUPS_TABLE.'
      WHERE id = '.$group.'
    ;';
      list($groupname, $is_default) = pwg_db_fetch_row(pwg_query($query));
      
      // update of the group
      $query = '
    UPDATE '.GROUPS_TABLE.'
      SET is_default = \''.boolean_to_string(!get_boolean($is_default)).'\'
      WHERE id = '.$group.'
    ;';
      pwg_query($query);
    
      $page['infos'][] = l10n('group "%s" updated', $groupname);
    }
  }
  invalidate_user_cache();
}
// +-----------------------------------------------------------------------+
// |                             template init                             |
// +-----------------------------------------------------------------------+

$template->set_filenames(array('group_list' => 'group_list.tpl'));

$template->assign(
  array(
    'F_ADD_ACTION' => get_root_url().'admin.php?page=group_list',
    'U_HELP' => get_root_url().'admin/popuphelp.php?page=group_list',
    'PWG_TOKEN' => get_pwg_token(),
    )
  );

// +-----------------------------------------------------------------------+
// |                              group list                               |
// +-----------------------------------------------------------------------+

$query = '
SELECT id, name, is_default
  FROM '.GROUPS_TABLE.'
  ORDER BY name ASC
;';
$result = pwg_query($query);

$admin_url = get_root_url().'admin.php?page=';
$perm_url    = $admin_url.'group_perm&amp;group_id=';
$del_url     = $admin_url.'group_list&amp;delete=';
$toggle_is_default_url     = $admin_url.'group_list&amp;toggle_is_default=';

while ($row = pwg_db_fetch_assoc($result))
{
  $query = '
SELECT u.'. $conf['user_fields']['username'].' AS username
  FROM '.USERS_TABLE.' AS u
  INNER JOIN '.USER_GROUP_TABLE.' AS ug
    ON u.'.$conf['user_fields']['id'].' = ug.user_id
  WHERE ug.group_id = '.$row['id'].'
;';
  $members=array();
  $res=pwg_query($query);
  while ($us= pwg_db_fetch_assoc($res))
  {
    $members[]=$us['username'];
  }
  $template->append(
    'groups',
    array(
      'NAME' => $row['name'],
      'ID' => $row['id'],
      'IS_DEFAULT' => (get_boolean($row['is_default']) ? ' ['.l10n('default').']' : ''),
      'NB_MEMBERS' => count($members),
      'L_MEMBERS' => implode(' <span class="userSeparator">&middot;</span> ', $members),
      'MEMBERS' => l10n_dec('%d member', '%d members', count($members)),
      'U_DELETE' => $del_url.$row['id'].'&amp;pwg_token='.get_pwg_token(),
      'U_PERM' => $perm_url.$row['id'],
      'U_ISDEFAULT' => $toggle_is_default_url.$row['id'].'&amp;pwg_token='.get_pwg_token(),
      )
    );
}

// +-----------------------------------------------------------------------+
// |                           sending html code                           |
// +-----------------------------------------------------------------------+

$template->assign_var_from_handle('ADMIN_CONTENT', 'group_list');

?>