source: extensions/community/admin_permissions.php @ 9511

Last change on this file since 9511 was 9511, checked in by plg, 13 years ago

smart detection of implicit edition of an existing permission

File size: 12.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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');
30load_language('plugin.lang', COMMUNITY_PATH);
31
32$admin_base_url = get_root_url().'admin.php?page=plugin-community-permissions';
33
34$who_options = array(
35  'any_visitor' => l10n('any visitor'),
36  'any_registered_user' => l10n('any registered user'),
37  'user' => l10n('a specific user'),
38  'group' => l10n('a group'),
39  );
40
41// +-----------------------------------------------------------------------+
42// | Check Access and exit when user status is not ok                      |
43// +-----------------------------------------------------------------------+
44
45check_status(ACCESS_ADMINISTRATOR);
46
47// +-----------------------------------------------------------------------+
48// |                            add permissions                            |
49// +-----------------------------------------------------------------------+
50
51if (isset($_POST['submit_add']))
52{
53  if (!in_array($_POST['who'], array_keys($who_options)))
54  {
55    die('hacking attempt: invalid "who" option');
56  }
57 
58  if ('user' == $_POST['who'])
59  {
60    check_input_parameter('who_user', $_POST, false, PATTERN_ID);
61  }
62
63  if ('group' == $_POST['who'])
64  {
65    check_input_parameter('who_group', $_POST, false, PATTERN_ID);
66  }
67
68  if (-1 != $_POST['category'])
69  {
70    check_input_parameter('category', $_POST, false, PATTERN_ID);
71  }
72
73  check_input_parameter('moderated', $_POST, false, '/^(true|false)$/');
74
75  // creating the permission
76  $insert = array(
77    'type' => $_POST['who'],
78    'group_id' => ('group' == $_POST['who']) ? $_POST['who_group'] : null,
79    'user_id' => ('user' == $_POST['who']) ? $_POST['who_user'] : null,
80    'category_id' => ($_POST['category'] > 0) ? $_POST['category'] : null,
81    'recursive' => isset($_POST['recursive']) ? 'true' : 'false',
82    'create_subcategories' => isset($_POST['create_subcategories']) ? 'true' : 'false',
83    'moderated' => $_POST['moderated'],
84    );
85
86  // does this permission already exist?
87  //
88  // a permission is identified by a who+where
89  $query = '
90SELECT
91    id
92  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
93  WHERE type = \''.$insert['type'].'\'
94    AND user_id '.(isset($insert['user_id']) ? '= '.$insert['user_id'] : 'is null').'
95    AND group_id '.(isset($insert['group_id']) ? '= '.$insert['group_id'] : 'is null').'
96;';
97  $result = pwg_query($query);
98  $row = pwg_db_fetch_assoc($result);
99  if (isset($row['id']))
100  {
101    if (isset($_POST['edit']))
102    {
103      check_input_parameter('edit', $_POST, false, PATTERN_ID);
104     
105      if ($_POST['edit'] != $row['id'])
106      {
107        // we have to delete the edited permission
108        $query = '
109DELETE
110  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
111  WHERE id = '.$_POST['edit'].'
112;';
113        pwg_query($query);
114      }
115    }
116
117    $_POST['edit'] = $row['id'];
118  }
119
120  if (isset($_POST['edit']))
121  {
122    check_input_parameter('edit', $_POST, false, PATTERN_ID);
123
124    $insert['id'] = $_POST['edit'];
125
126    mass_updates(
127      COMMUNITY_PERMISSIONS_TABLE,
128      array(
129        'primary' => array('id'),
130        'update' => array_keys($insert),
131        ),
132      array($insert)
133      );
134
135    $page['highlight'] = $insert['id'];
136
137    array_push(
138      $page['infos'],
139      l10n('Permission updated')
140      );
141  }
142  else
143  {
144    mass_inserts(
145      COMMUNITY_PERMISSIONS_TABLE,
146      array_keys($insert),
147      array($insert)
148      );
149
150    $page['highlight'] = pwg_db_insert_id(COMMUNITY_PERMISSIONS_TABLE);
151 
152    array_push(
153      $page['infos'],
154      l10n('Permission added')
155      );
156  }
157
158  conf_update_param('community_update', time());
159}
160
161// +-----------------------------------------------------------------------+
162// |                           remove permissions                          |
163// +-----------------------------------------------------------------------+
164
165if (isset($_GET['delete']))
166{
167  check_input_parameter('delete', $_GET, false, PATTERN_ID);
168 
169  $query = '
170DELETE
171  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
172  WHERE id = '.$_GET['delete'].'
173;';
174  pwg_query($query);
175
176  conf_update_param('community_update', time());
177
178  $_SESSION['page_infos'] = array(l10n('Permission removed'));
179  redirect($admin_base_url);
180}
181
182// +-----------------------------------------------------------------------+
183// | template init                                                         |
184// +-----------------------------------------------------------------------+
185
186$template->set_filenames(
187  array(
188    'plugin_admin_content' => dirname(__FILE__).'/admin_permissions.tpl'
189    )
190  );
191
192// +-----------------------------------------------------------------------+
193// | prepare form                                                          |
194// +-----------------------------------------------------------------------+
195
196// edit mode?
197if (isset($_GET['edit']))
198{
199  check_input_parameter('edit', $_GET, false, PATTERN_ID);
200 
201  $query = '
202SELECT
203    *
204  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
205  WHERE id = '.$_GET['edit'].'
206;';
207  $result = pwg_query($query);
208  $row = pwg_db_fetch_assoc($result);
209
210  if (isset($row['id']))
211  {
212    $template->assign(
213      array(
214        'edit' => $row['id'],
215        'who_options_selected' => $row['type'],
216        'user_options_selected' => $row['user_id'],
217        'group_options_selected' => $row['group_id'],
218        'category_options_selected' => $row['category_id'],
219        'recursive' => get_boolean($row['recursive']),
220        'create_subcategories' => get_boolean($row['create_subcategories']),
221        'moderated' => get_boolean($row['moderated']),
222        )
223      );
224  }
225}
226else
227{
228  $template->assign(
229    array(
230      'moderated' => true,
231      )
232    );
233}
234
235// who options
236$template->assign(
237  array(
238    'who_options' => $who_options,
239    )
240  );
241
242// list of users
243$users = array();
244
245$query = '
246SELECT
247    '.$conf['user_fields']['id'].' AS id,
248    '.$conf['user_fields']['username'].' AS username
249  FROM '.USERS_TABLE.' AS u
250    INNER JOIN '.USER_INFOS_TABLE.' AS uf ON uf.user_id = id
251  WHERE uf.status IN (\'normal\',\'generic\')
252;';
253$result = pwg_query($query);
254while ($row = pwg_db_fetch_assoc($result))
255{
256  $users[$row['id']] = $row['username'];
257}
258
259natcasesort($users);
260
261$template->assign(
262  array(
263    'user_options' => $users,
264    )
265  );
266
267// list of groups
268$groups = array();
269
270$query = '
271SELECT
272    id,
273    name
274  FROM '.GROUPS_TABLE.'
275;';
276$result = pwg_query($query);
277while ($row = pwg_db_fetch_assoc($result))
278{
279  $groups[$row['id']] = $row['name'];
280}
281
282natcasesort($groups);
283
284$template->assign(
285  array(
286    'group_options' => $groups,
287    )
288  );
289
290
291$template->assign(
292  array(
293    'F_ADD_ACTION' => COMMUNITY_BASE_URL.'-'.$page['tab'],
294    )
295  );
296
297// list of albums
298$query = '
299SELECT id,name,uppercats,global_rank
300  FROM '.CATEGORIES_TABLE.'
301;';
302
303display_select_cat_wrapper(
304  $query,
305  array(),
306  'category_options'
307  );
308
309// +-----------------------------------------------------------------------+
310// | permission list                                                       |
311// +-----------------------------------------------------------------------+
312
313// user with community permissions
314$query = '
315SELECT
316    *
317  FROM '.COMMUNITY_PERMISSIONS_TABLE.'
318  ORDER BY id DESC
319;';
320$result = pwg_query($query);
321
322$permissions = array();
323$user_ids = array();
324$group_ids = array();
325$category_ids = array();
326
327while ($row = mysql_fetch_assoc($result))
328{
329  array_push($permissions, $row);
330
331  if (!empty($row['user_id']))
332  {
333    array_push($user_ids, $row['user_id']);
334  }
335
336  if (!empty($row['group_id']))
337  {
338    array_push($group_ids, $row['group_id']);
339  }
340
341  if (!empty($row['category_id']))
342  {
343    array_push($category_ids, $row['category_id']);
344  }
345}
346
347if (!empty($user_ids))
348{
349  $query = '
350SELECT
351    '.$conf['user_fields']['id'].' AS id,
352    '.$conf['user_fields']['username'].' AS username
353  FROM '.USERS_TABLE.'
354  WHERE '.$conf['user_fields']['id'].' IN ('.implode(',', $user_ids).')
355;';
356  $result = pwg_query($query);
357  while ($row = pwg_db_fetch_assoc($result))
358  {
359    $name_of_user[ $row['id'] ] = $row['username'];
360  }
361}
362
363if (!empty($group_ids))
364{
365  $query = '
366SELECT
367    id,
368    name
369  FROM '.GROUPS_TABLE.'
370  WHERE id IN ('.implode(',', $group_ids).')
371;';
372  $result = pwg_query($query);
373  while ($row = pwg_db_fetch_assoc($result))
374  {
375    $name_of_group[ $row['id'] ] = $row['name'];
376  }
377}
378
379if (!empty($category_ids))
380{
381  $query = '
382SELECT
383    id,
384    uppercats
385  FROM '.CATEGORIES_TABLE.'
386  WHERE id IN ('.implode(',', $category_ids).')
387;';
388  $result = pwg_query($query);
389
390  while ($row = pwg_db_fetch_assoc($result))
391  {
392    $name_of_category[ $row['id'] ] = get_cat_display_name_cache(
393      $row['uppercats'],
394      null,
395      false
396      );
397  }
398}
399
400foreach ($permissions as $permission)
401{
402  $where = l10n('The whole gallery');
403  if (isset($permission['category_id']))
404  {
405    $where = $name_of_category[ $permission['category_id'] ];
406  }
407
408  $who = l10n('any visitor');
409  if ('any_registered_user' == $permission['type'])
410  {
411    $who = l10n('any registered user');
412  }
413  elseif ('user' == $permission['type'])
414  {
415    $who = sprintf(
416      l10n('%s (the user)'),
417      $name_of_user[$permission['user_id']]
418      );
419  }
420  elseif ('group' == $permission['type'])
421  {
422    $who = sprintf(
423      l10n('%s (the group)'),
424      $name_of_group[$permission['group_id']]
425      );
426  }
427
428  $trust = l10n('low trust');
429  $trust_tooltip = l10n('uploaded photos must be validated by an administrator');
430  if ('false' == $permission['moderated'])
431  {
432    $trust = l10n('high trust');
433    $trust_tooltip = l10n('uploaded photos are directly displayed in the gallery');
434  }
435
436  $highlight = false;
437  if (isset($_GET['edit']) and $permission['id'] == $_GET['edit'])
438  {
439    $highlight = true;
440  }
441  if (isset($page['highlight']) and $permission['id'] == $page['highlight'])
442  {
443    $highlight = true;
444  }
445 
446 
447  $template->append(
448    'permissions',
449    array(
450      'WHO' => $who,
451      'WHERE' => $where,
452      'TRUST' => $trust,
453      'TRUST_TOOLTIP' => $trust_tooltip,
454      'RECURSIVE' => get_boolean($permission['recursive']),
455      'RECURSIVE_TOOLTIP' => l10n('Apply to sub-albums'),
456      'CREATE_SUBCATEGORIES' => get_boolean($permission['create_subcategories']),
457      'U_DELETE' => $admin_base_url.'&amp;delete='.$permission['id'],
458      'U_EDIT' => $admin_base_url.'&amp;edit='.$permission['id'],
459      'HIGHLIGHT' => $highlight,
460      )
461    );
462}
463
464// +-----------------------------------------------------------------------+
465// | sending html code                                                     |
466// +-----------------------------------------------------------------------+
467
468$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
469?>
Note: See TracBrowser for help on using the repository browser.