source: branches/2.6/admin/group_list.php @ 27996

Last change on this file since 27996 was 27996, checked in by plg, 10 years ago

bug 3065 fixed: avoid SQL errors with external authentication

  • Property svn:eol-style set to LF
File size: 12.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if( !defined("PHPWG_ROOT_PATH") )
25{
26  die ("Hacking attempt!");
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | tabs                                                                  |
33// +-----------------------------------------------------------------------+
34
35include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
36
37$my_base_url = get_root_url().'admin.php?page=';
38
39$tabsheet = new tabsheet();
40$tabsheet->set_id('groups');
41$tabsheet->select('group_list');
42$tabsheet->assign();
43
44// +-----------------------------------------------------------------------+
45// | Check Access and exit when user status is not ok                      |
46// +-----------------------------------------------------------------------+
47check_status(ACCESS_ADMINISTRATOR);
48
49if (!empty($_POST) or isset($_GET['delete']) or isset($_GET['toggle_is_default']))
50{
51  check_pwg_token();
52}
53// +-----------------------------------------------------------------------+
54// |                              add a group                              |
55// +-----------------------------------------------------------------------+
56
57if (isset($_POST['submit_add']))
58{
59  if (empty($_POST['groupname']))
60  {
61    $page['errors'][] = l10n('The name of a group must not contain " or \' or be empty.');
62  }
63  if (count($page['errors']) == 0)
64  {
65    // is the group not already existing ?
66    $query = '
67SELECT COUNT(*)
68  FROM '.GROUPS_TABLE.'
69  WHERE name = \''.$_POST['groupname'].'\'
70;';
71    list($count) = pwg_db_fetch_row(pwg_query($query));
72    if ($count != 0)
73    {
74      $page['errors'][] = l10n('This name is already used by another group.');
75    }
76  }
77  if (count($page['errors']) == 0)
78  {
79    // creating the group
80    $query = '
81INSERT INTO '.GROUPS_TABLE.'
82  (name)
83  VALUES
84  (\''.pwg_db_real_escape_string($_POST['groupname']).'\')
85;';
86    pwg_query($query);
87
88    $page['infos'][] = l10n('group "%s" added', $_POST['groupname']);
89  }
90}
91
92// +-----------------------------------------------------------------------+
93// |                             action send                               |
94// +-----------------------------------------------------------------------+
95if (isset($_POST['submit']) and isset($_POST['selectAction']) and isset($_POST['group_selection']))
96{
97  // if the user tries to apply an action, it means that there is at least 1
98  // photo in the selection
99  $groups = $_POST['group_selection'];
100  if (count($groups) == 0)
101  {
102    $page['errors'][] = l10n('Select at least one group');
103  }
104
105  $action = $_POST['selectAction'];
106
107  // +
108  // |rename a group
109  // +
110
111  if ($action=="rename")
112  {
113    // is the group not already existing ?
114    $query = '
115SELECT name
116  FROM '.GROUPS_TABLE.'
117;';
118    $group_names = array_from_query($query, 'name');
119    foreach($groups as $group)
120    {
121      if (  in_array($_POST['rename_'.$group.''], $group_names))
122      {
123        $page['errors'][] = $_POST['rename_'.$group.''].' | '.l10n('This name is already used by another group.');
124      }
125      elseif ( !empty($_POST['rename_'.$group.'']))
126      {
127        $query = '
128        UPDATE '.GROUPS_TABLE.'
129        SET name = \''.pwg_db_real_escape_string($_POST['rename_'.$group.'']).'\'
130        WHERE id = '.$group.'
131      ;';
132        pwg_query($query);
133      }
134    }
135  }
136
137  // +
138  // |delete a group
139  // +
140
141  if ($action=="delete" and isset($_POST['confirm_deletion']) and $_POST['confirm_deletion'])
142  {
143    foreach($groups as $group)
144    {
145        // destruction of the access linked to the group
146      $query = '
147    DELETE
148      FROM '.GROUP_ACCESS_TABLE.'
149      WHERE group_id = '.$group.'
150    ;';
151      pwg_query($query);
152     
153      // destruction of the users links for this group
154      $query = '
155    DELETE
156      FROM '.USER_GROUP_TABLE.'
157      WHERE group_id = '.$group.'
158    ;';
159      pwg_query($query);
160   
161      $query = '
162    SELECT name
163      FROM '.GROUPS_TABLE.'
164      WHERE id = '.$group.'
165    ;';
166      list($groupname) = pwg_db_fetch_row(pwg_query($query));
167     
168      // destruction of the group
169      $query = '
170    DELETE
171      FROM '.GROUPS_TABLE.'
172      WHERE id = '.$group.'
173    ;';
174      pwg_query($query);
175   
176      $page['infos'][] = l10n('group "%s" deleted', $groupname);
177    }
178  }
179
180  // +
181  // |merge groups into a new one
182  // +
183
184  if ($action=="merge" and count($groups) > 1)
185  {
186    // is the group not already existing ?
187    $query = '
188SELECT COUNT(*)
189  FROM '.GROUPS_TABLE.'
190  WHERE name = \''.pwg_db_real_escape_string($_POST['merge']).'\'
191;';
192    list($count) = pwg_db_fetch_row(pwg_query($query));
193    if ($count != 0)
194    {
195      $page['errors'][] = l10n('This name is already used by another group.');
196    }
197    else
198    {
199      // creating the group
200      $query = '
201  INSERT INTO '.GROUPS_TABLE.'
202    (name)
203    VALUES
204    (\''.pwg_db_real_escape_string($_POST['merge']).'\')
205  ;';
206      pwg_query($query);
207      $query = '
208      SELECT id
209        FROM '.GROUPS_TABLE.'
210        WHERE name = \''.pwg_db_real_escape_string($_POST['merge']).'\'
211      ;';
212      list($groupid) = pwg_db_fetch_row(pwg_query($query));
213    }
214    $grp_access = array();
215    $usr_grp = array();
216    foreach($groups as $group)
217    {
218      $query = '
219    SELECT *
220      FROM '.GROUP_ACCESS_TABLE.'
221      WHERE group_id = '.$group.'
222    ;';
223      $res=pwg_query($query);
224      while ($row = pwg_db_fetch_assoc($res))
225      {
226        $new_grp_access= array(
227          'cat_id' => $row['cat_id'],
228          'group_id' => $groupid
229        );
230        if (!in_array($new_grp_access,$grp_access))
231        {
232          $grp_access[]=$new_grp_access;
233        }
234      }
235
236      $query = '
237    SELECT *
238      FROM '.USER_GROUP_TABLE.'
239      WHERE group_id = '.$group.'
240    ;';
241      $res=pwg_query($query);
242      while ($row = pwg_db_fetch_assoc($res))
243      {
244        $new_usr_grp= array(
245          'user_id' => $row['user_id'],
246          'group_id' => $groupid
247        );
248        if (!in_array($new_usr_grp,$usr_grp))
249        {
250          $usr_grp[]=$new_usr_grp;
251        }
252      }
253    }
254    mass_inserts(USER_GROUP_TABLE, array('user_id','group_id'), $usr_grp);
255    mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $grp_access);
256   
257    $page['infos'][] = l10n('group "%s" added', $_POST['merge']);
258  }
259 
260  // +
261  // |duplicate a group
262  // +
263
264  if ($action=="duplicate" )
265  {
266    foreach($groups as $group)
267    {
268      if ( empty($_POST['duplicate_'.$group.'']) )
269      {
270        break;
271      }
272      // is the group not already existing ?
273      $query = '
274  SELECT COUNT(*)
275    FROM '.GROUPS_TABLE.'
276    WHERE name = \''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\'
277  ;';
278      list($count) = pwg_db_fetch_row(pwg_query($query));
279      if ($count != 0)
280      {
281        $page['errors'][] = l10n('This name is already used by another group.');
282        break;
283      }
284      // creating the group
285      $query = '
286  INSERT INTO '.GROUPS_TABLE.'
287    (name)
288    VALUES
289    (\''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\')
290  ;';
291      pwg_query($query);
292      $query = '
293      SELECT id
294        FROM '.GROUPS_TABLE.'
295        WHERE name = \''.pwg_db_real_escape_string($_POST['duplicate_'.$group.'']).'\'
296      ;';
297     
298      list($groupid) = pwg_db_fetch_row(pwg_query($query));
299      $query = '
300    SELECT *
301      FROM '.GROUP_ACCESS_TABLE.'
302      WHERE group_id = '.$group.'
303    ;';
304      $grp_access = array();
305      $res=pwg_query($query);
306      while ($row = pwg_db_fetch_assoc($res))
307      {
308          $grp_access[] = array(
309            'cat_id' => $row['cat_id'],
310            'group_id' => $groupid
311          );
312      }
313      mass_inserts(GROUP_ACCESS_TABLE, array('group_id','cat_id'), $grp_access);
314
315      $query = '
316    SELECT *
317      FROM '.USER_GROUP_TABLE.'
318      WHERE group_id = '.$group.'
319    ;';
320      $usr_grp = array();
321      $res=pwg_query($query);
322      while ($row = pwg_db_fetch_assoc($res))
323      {
324          $usr_grp[] = array(
325            'user_id' => $row['user_id'],
326            'group_id' => $groupid
327          );
328      }
329      mass_inserts(USER_GROUP_TABLE, array('user_id','group_id'), $usr_grp);
330 
331      $page['infos'][] = l10n('group "%s" added', $_POST['duplicate_'.$group.'']);
332    }
333  }
334
335
336  // +
337  // | toggle_default
338  // +
339 
340  if ($action=="toggle_default")
341  {
342    foreach($groups as $group)
343    {
344      $query = '
345    SELECT name, is_default
346      FROM '.GROUPS_TABLE.'
347      WHERE id = '.$group.'
348    ;';
349      list($groupname, $is_default) = pwg_db_fetch_row(pwg_query($query));
350     
351      // update of the group
352      $query = '
353    UPDATE '.GROUPS_TABLE.'
354      SET is_default = \''.boolean_to_string(!get_boolean($is_default)).'\'
355      WHERE id = '.$group.'
356    ;';
357      pwg_query($query);
358   
359      $page['infos'][] = l10n('group "%s" updated', $groupname);
360    }
361  }
362  invalidate_user_cache();
363}
364// +-----------------------------------------------------------------------+
365// |                             template init                             |
366// +-----------------------------------------------------------------------+
367
368$template->set_filenames(array('group_list' => 'group_list.tpl'));
369
370$template->assign(
371  array(
372    'F_ADD_ACTION' => get_root_url().'admin.php?page=group_list',
373    'U_HELP' => get_root_url().'admin/popuphelp.php?page=group_list',
374    'PWG_TOKEN' => get_pwg_token(),
375    )
376  );
377
378// +-----------------------------------------------------------------------+
379// |                              group list                               |
380// +-----------------------------------------------------------------------+
381
382$query = '
383SELECT id, name, is_default
384  FROM '.GROUPS_TABLE.'
385  ORDER BY name ASC
386;';
387$result = pwg_query($query);
388
389$admin_url = get_root_url().'admin.php?page=';
390$perm_url    = $admin_url.'group_perm&amp;group_id=';
391$del_url     = $admin_url.'group_list&amp;delete=';
392$toggle_is_default_url     = $admin_url.'group_list&amp;toggle_is_default=';
393
394while ($row = pwg_db_fetch_assoc($result))
395{
396  $query = '
397SELECT u.'. $conf['user_fields']['username'].' AS username
398  FROM '.USERS_TABLE.' AS u
399  INNER JOIN '.USER_GROUP_TABLE.' AS ug
400    ON u.'.$conf['user_fields']['id'].' = ug.user_id
401  WHERE ug.group_id = '.$row['id'].'
402;';
403  $members=array();
404  $res=pwg_query($query);
405  while ($us= pwg_db_fetch_assoc($res))
406  {
407    $members[]=$us['username'];
408  }
409  $template->append(
410    'groups',
411    array(
412      'NAME' => $row['name'],
413      'ID' => $row['id'],
414      'IS_DEFAULT' => (get_boolean($row['is_default']) ? ' ['.l10n('default').']' : ''),
415      'NB_MEMBERS' => count($members),
416      'L_MEMBERS' => implode(' <span class="userSeparator">&middot;</span> ', $members),
417      'MEMBERS' => l10n_dec('%d member', '%d members', count($members)),
418      'U_DELETE' => $del_url.$row['id'].'&amp;pwg_token='.get_pwg_token(),
419      'U_PERM' => $perm_url.$row['id'],
420      'U_ISDEFAULT' => $toggle_is_default_url.$row['id'].'&amp;pwg_token='.get_pwg_token(),
421      )
422    );
423}
424
425// +-----------------------------------------------------------------------+
426// |                           sending html code                           |
427// +-----------------------------------------------------------------------+
428
429$template->assign_var_from_handle('ADMIN_CONTENT', 'group_list');
430
431?>
Note: See TracBrowser for help on using the repository browser.