source: extensions/virtualAutoGrant/virtual_categories_granted_to_creator.php @ 6619

Last change on this file since 6619 was 6619, checked in by vdigital, 14 years ago

Fix: Missing translations
Fix: Who can see in general case

Testing phase - unstable version

File size: 7.7 KB
Line 
1<?php
2/* Virtual AutoGrant */
3if (!defined('PHPWG_ROOT_PATH') or !defined('VAG_DIR')) die('Hacking attempt!');
4global $conf;
5include_once( VAG_PATH . 'VAG_default.php');
6$vag = & $conf['virtual_AutoGrant'];
7
8// General case for set private (exception synchronisation (categories are not virtual ones))
9if ( $conf['newcat_default_status'] == 'public' and $vag['set_private'] == 1 
10    and defined('IN_ADMIN') and IN_ADMIN and !function_exists( 'VAG_set_private' ) ) {
11  add_event_handler('get_admin_plugin_menu_links', 'VAG_set_private');
12  function VAG_set_private($menu) {
13    global $page, $conf;
14    if ( isset($page['page']) and $page['page'] != 'site_manager' )
15      $conf['newcat_default_status'] = 'private';
16    return $menu;
17  }
18} 
19// General case for set public (exception synchronisation (categories are not virtual ones))
20if ( $conf['newcat_default_status'] == 'private' and $vag['set_private'] == 0 
21    and defined('IN_ADMIN') and IN_ADMIN and !function_exists( 'VAG_set_public' ) ) {
22  add_event_handler('get_admin_plugin_menu_links', 'VAG_set_public');
23  function VAG_set_public($menu) {
24    global $page, $conf;
25    if ( isset($page['page']) and $page['page'] != 'site_manager' )
26      $conf['newcat_default_status'] = 'public';
27    return $menu;
28  }
29} 
30// General case for grant to someone
31// Don't grant on physical categories
32if ( ($vag['grant_to_creator'] == 1 or $vag['grant_to_webmaster'] == 1)
33    and defined('IN_ADMIN') and IN_ADMIN and !function_exists( 'VAG_grant_to_prepare' ) ) {
34                add_event_handler('get_admin_plugin_menu_links', 'VAG_grant_to_prepare');
35  function VAG_grant_to_prepare($menu) {
36    global $conf, $page, $vag;
37    if ( !isset($page['page']) or $page['page'] == 'site_manager' ) return false; # Not Virtual
38    list($max_id) = pwg_db_fetch_row(pwg_query('SELECT MAX(id) FROM '.CATEGORIES_TABLE . ';'));
39    $vag['lastcat before'] == $max_id;
40    list($max_id) = pwg_db_fetch_row(pwg_query('SELECT MAX(id) FROM '.IMAGES_TABLE . ';'));
41    $vag['lastimg before'] == $max_id;
42    add_event_handler('loc_end_admin', 'VAG_grant_to_somebody');
43    return $menu;
44  }
45  if ( !function_exists( 'VAG_grant_to_somebody' ) ) {
46    function VAG_grant_to_somebody() {
47      global $conf, $vag, $user;
48      if ( empty($vag['lastcat before']) ) $vag['lastcat before'] = 0;
49      if ( empty($vag['lastimg before']) ) $vag['lastimg before'] = 0;
50      list($new_id) = pwg_db_fetch_row(pwg_query('SELECT MAX(id) FROM '.CATEGORIES_TABLE
51      . ' WHERE id > ' . $vag['lastcat before'] . ' AND site_id IS NULL;'));
52      if ( empty($newid) ) return false;
53      if ( $vag['grant_to_creator'] == 1 or $vag['grant_to_webmaster'] == 1 ) {
54        if ( $vag['grant_to_creator'] == 1 ) $insert[] = array('user_id' => $user['id'], 'cat_id' => $newid);
55        if ( $user['id'] != $conf['webmaster_id'] and $vag['grant_to_webmaster'] == 1 )
56          $insert[] = array('user_id' => $conf['webmaster_id'], 'cat_id' => $newid);
57        mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $insert);
58      }
59      // Who can see
60      if ( $vag['set_who_can_see'] == 0 ) return false;
61      if ( is_webmaster() and $vag['webmaster_set_who_can_see'] == 1) return false;
62      // New image_ids
63      $img_ids = array_from_query('SELECT id FROM ' . IMAGES_TABLE . ' WHERE id > ' . $vag['lastimg before'] . ';', 'id');
64      if (count($img_ids) == 0) return false;
65      // New image_ids which are associated to a public category at least
66      if ($vag['ignore_who_can_see_on_private'] == 1)
67        $img_ids = array_from_query('SELECT image_id FROM ' . IMAGE_CATEGORY_TABLE 
68            . ' WHERE category_id IN (SELECT id FROM ' . CATEGORIES_TABLE . ' WHERE status = \'public\')'
69            . ' AND image_id IN ('.implode(',', $img_ids).');');
70      $r = pwg_query('UPDATE '.IMAGES_TABLE.' SET level = ' . $user['level'] . ' WHERE level < '. $user['level'] 
71            . ' AND id IN ('.implode(',', $img_ids).');');
72    }
73  }
74} 
75
76// API cases : Just do it   
77if ( !function_exists( 'VAG_set_private_by_API' ) ) {
78        add_event_handler('invalidate_user_cache', 'VAG_set_private_by_API', 20);
79
80        function  VAG_set_private_by_API($full) {
81    if ( !function_exists( 'ws_categories_add' ) ) return false; // That isn't an API request
82          global $user, $conf;
83    $vag = & $conf['virtual_AutoGrant'];
84    if ( isset($_REQUEST['method']) and $_REQUEST['method'] == 'pwg.categories.add' ) {
85      list($inserted_id) = pwg_db_fetch_row(pwg_query('SELECT MAX(id) FROM '.CATEGORIES_TABLE
86      . ' WHERE name = \'' .pwg_db_real_escape_string($_REQUEST['name']) . '\' AND site_id IS NULL;'));
87      # Note:  AND site_id IS NULL because synchro could be running in //
88      if ( $vag['set_private_by_API'] == 1 ) set_cat_status(array($inserted_id), 'private');
89        else set_cat_status(array($inserted_id), 'public');
90      if ( ($vag['grant_to_creator'] == 1 or $vag['grant_to_webmaster'] == 1 ) and $vag['set_private_by_API'] == 1 ) {
91        if ( $vag['grant_to_creator'] == 1 ) $insert[] = array('user_id' => $user['id'], 'cat_id' => $inserted_id);
92        if ( $user['id'] != $conf['webmaster_id'] and $vag['grant_to_webmaster'] == 1 )
93          $insert[] = array('user_id' => $conf['webmaster_id'], 'cat_id' => $inserted_id);
94        mass_inserts(USER_ACCESS_TABLE, array('user_id','cat_id'), $insert);
95      }
96      $vag['lastcat before'] = $inserted_id; # Admin Photos Add does not need to redo the job
97    }
98    // "Who can see" management ( only for API, Synchro or old HTTP management on gallery side are ignored )
99    if ( isset($_REQUEST['method']) and $_REQUEST['method'] == 'pwg.images.add' and isset($_REQUEST['original_sum']) 
100        and $vag['set_who_can_see'] == 1 ) {
101      # image to update
102      if ('md5sum' == $conf['uniqueness_mode']) $where_clause = "md5sum = '".pwg_db_real_escape_string($_REQUEST['original_sum'])."'";
103      if ('filename' == $conf['uniqueness_mode'] and isset($_REQUEST['original_filename']) ) 
104          $where_clause = "file = '".pwg_db_real_escape_string($_REQUEST['original_filename'])."'";
105      if ( !isset($where_clause) or (is_webmaster() and $vag['webmaster_set_who_can_see'] == 1) ) return false;
106      if ( $user['level'] == $conf['available_permission_levels'][0] ) return false;   // Non privacy level won't force it
107      if (!isset($_REQUEST['categories'])) return false;
108      $tokens = explode(';', pwg_db_real_escape_string($_REQUEST['categories']));
109      $cat_ids = array();
110      foreach ($tokens as $token) {
111        list($cat_id) = explode(',', $token);
112        if (!preg_match('/^\d+$/', $cat_id)) continue;
113        array_push($cat_ids, $cat_id);
114      }
115      $cat_ids = array_unique($cat_ids);
116      if (count($cat_ids) != 0) $w = ' AND id IN ('.implode(',', $cat_ids).')'; else $w ='';
117      list($count_public_cat) = pwg_db_fetch_row(pwg_query('SELECT count(*) FROM '.CATEGORIES_TABLE
118      . ' WHERE status = \'public\' ' . $w . ' AND site_id IS NULL;'));
119      // No public category = all are private => maybe we can ignore "Who can see"
120      if ( $count_public_cat == 0 and $vag['ignore_who_can_see_on_private'] == 1 ) return false;
121      $r = pwg_query('UPDATE '.IMAGES_TABLE.' SET level = ' . $user['level'] . ' WHERE level < '. $user['level'] 
122          .' AND ' . $where_clause . ';');
123    }
124        }
125}
126// Configuration menu
127if ( !function_exists( 'VAG_admin_menu' ) ) {
128        if ( defined('IN_ADMIN') and IN_ADMIN ) {
129                add_event_handler('get_admin_plugin_menu_links', 'VAG_admin_menu');
130    function VAG_admin_menu($menu) {
131        array_push($menu, array(
132                'NAME' => 'Virtual AutoGrant',
133                'URL' => get_admin_plugin_menu_link( VAG_PATH .'VAG_admin.php')
134        ));
135        return $menu;
136    }
137  }
138}
139?>
Note: See TracBrowser for help on using the repository browser.