source: extensions/community/admin_permissions.php @ 26432

Last change on this file since 26432 was 23085, checked in by plg, 11 years ago

New feature: user album. Only for registered users, no recursivity. Piwigo
will automatically create an upload album for each user with appropriate
community permissions, at first connection.

Bug fixed: on activation, do not create a new "Community" album if it already
exists.

Bug fixed: remove debug for quota

Bug fixed: round corners for number of pending pictures in admin menu.

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