source: extensions/community/maintain.inc.php @ 9583

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

bug fixed: invalidate_user_cache now also invalidates community user
permissions cache

bug fixed: community permissions cache was not refreshed when user connects,
I have added $_SESSIONcommunity_user_id to make sure the permissions are
related to the correct user

change: use a random key for cache update to avoid "in the same second
refresh".

filter the list of parent albums for "create a new album" based on permissions
even when create_whole_gallery is true

File size: 8.2 KB
Line 
1<?php
2
3if (!defined("COMMUNITY_PATH"))
4{
5  define('COMMUNITY_PATH', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)));
6}
7
8function plugin_install()
9{
10  global $conf, $prefixeTable;
11
12  if ('mysql' == $conf['dblayer'])
13  {
14    $query = '
15CREATE TABLE '.$prefixeTable.'community_permissions (
16  id int(11) NOT NULL AUTO_INCREMENT,
17  type varchar(255) NOT NULL,
18  group_id smallint(5) unsigned DEFAULT NULL,
19  user_id smallint(5) DEFAULT NULL,
20  category_id smallint(5) unsigned DEFAULT NULL,
21  recursive enum(\'true\',\'false\') NOT NULL DEFAULT \'true\',
22  create_subcategories enum(\'true\',\'false\') NOT NULL DEFAULT \'false\',
23  moderated enum(\'true\',\'false\') NOT NULL DEFAULT \'true\',
24  PRIMARY KEY (id)
25) ENGINE=MyISAM DEFAULT CHARACTER SET utf8
26;';
27    pwg_query($query);
28
29    $query = '
30CREATE TABLE '.$prefixeTable.'community_pendings (
31  image_id mediumint(8) unsigned NOT NULL,
32  state varchar(255) NOT NULL,
33  added_on datetime NOT NULL,
34  validated_by smallint(5) DEFAULT NULL
35) ENGINE=MyISAM DEFAULT CHARACTER SET utf8
36;';
37    pwg_query($query);
38  }
39  elseif ('pgsql' == $conf['dblayer'])
40  {
41    $query = '
42CREATE TABLE "'.$prefixeTable.'community_permissions" (
43  "id" serial NOT NULL,
44  "type" VARCHAR(255) NOT NULL,
45  "group_id" INTEGER,
46  "user_id" INTEGER,
47  "category_id" INTEGER,
48  "recursive" BOOLEAN default true,
49  "create_subcategories" BOOLEAN default false,
50  "moderated" BOOLEAN default true,
51  PRIMARY KEY ("id")
52)
53;';
54    pwg_query($query);
55
56    $query = '
57CREATE TABLE "'.$prefixeTable.'community_pendings" (
58  image_id INTEGER NOT NULL,
59  state VARCHAR(255) NOT NULL,
60  added_on TIMESTAMP NOT NULL,
61  validated_by INTEGER
62)
63;';
64    pwg_query($query);
65  }
66  else
67  {
68    $query = '
69CREATE TABLE "'.$prefixeTable.'community_permissions" (
70  "id" INTEGER NOT NULL,
71  "type" VARCHAR(255) NOT NULL,
72  "group_id" INTEGER,
73  "user_id" INTEGER,
74  "category_id" INTEGER,
75  "recursive" BOOLEAN default true,
76  "create_subcategories" BOOLEAN default false,
77  "moderated" BOOLEAN default true,
78  PRIMARY KEY ("id")
79)
80;';
81    pwg_query($query);
82
83    $query = '
84CREATE TABLE "'.$prefixeTable.'community_pendings" (
85  image_id INTEGER NOT NULL,
86  state VARCHAR(255) NOT NULL,
87  added_on TIMESTAMP NOT NULL,
88  validated_by INTEGER
89)
90;';
91    pwg_query($query);
92  }
93}
94
95function plugin_uninstall()
96{
97  global $prefixeTable;
98 
99  $query = 'DROP TABLE '.$prefixeTable.'community_permissions;';
100  pwg_query($query);
101
102  $query = 'DROP TABLE '.$prefixeTable.'community_pendings;';
103  pwg_query($query);
104}
105
106function plugin_activate()
107{
108  global $prefixeTable;
109
110  community_get_data_from_core21();
111  community_get_data_from_community21();
112
113  $query = '
114SELECT
115    COUNT(*)
116  FROM '.$prefixeTable.'community_permissions
117;';
118  list($counter) = pwg_db_fetch_row(pwg_query($query));
119  if (0 == $counter)
120  {
121    community_create_default_permission();
122  }
123}
124
125function community_get_data_from_core21()
126{
127  global $conf, $prefixeTable;
128 
129  $from_piwigo21_file = $conf['local_data_dir'].'/plugins/core_user_upload_to_community.php';
130  if (is_file($from_piwigo21_file))
131  {
132    include($from_piwigo21_file);
133    $user_upload_conf = unserialize($user_upload_conf);
134   
135    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
136    prepare_upload_configuration();
137
138    $user_upload_conf['upload_user_access'] = 1;
139
140    if (isset($user_upload_conf['uploadable_categories']) and is_array($user_upload_conf['uploadable_categories']))
141    {
142      $type = 'any_registered_user';
143      if (isset($user_upload_conf['upload_user_access']) and 1 == $user_upload_conf['upload_user_access'])
144      {
145        $type = 'any_visitor';
146      }
147
148      $inserts = array();
149   
150      foreach ($user_upload_conf['uploadable_categories'] as $category_id)
151      {
152        array_push(
153          $inserts,
154          array(
155            'type' => $type,
156            'category_id' => $category_id,
157            'recursive' => 'false',
158            'create_subcategories' => 'false',
159            'moderated' => 'true',
160            )
161          );
162      }
163     
164      if (count($inserts) > 0)
165      {
166        mass_inserts(
167          $prefixeTable.'community_permissions',
168          array_keys($inserts[0]),
169          $inserts
170          );
171      }
172    }
173   
174    if (isset($user_upload_conf['waiting_rows']) and is_array($user_upload_conf['waiting_rows']))
175    {
176      $id_of_user = array();
177     
178      $query = '
179SELECT
180    '.$conf['user_fields']['id'].' AS id,
181    '.$conf['user_fields']['username'].' AS username
182  FROM '.USERS_TABLE.'
183;';
184      $result = pwg_query($query);
185      while ($row = pwg_db_fetch_assoc($result))
186      {
187        $id_of_user[ $row['username'] ] = $row['id'];
188      }
189     
190      $inserts = array();
191     
192      foreach ($user_upload_conf['waiting_rows'] as $pending)
193      {
194        $source_path = get_complete_dir($pending['storage_category_id']).$pending['file'];
195       
196        if (is_file($source_path))
197        {
198          $image_id = add_uploaded_file($source_path, $pending['file'], array($pending['storage_category_id']), 16);
199         
200          array_push(
201            $inserts,
202            array(
203              'image_id' => $image_id,
204              'added_on' => date ('Y-m-d H:i:s', $pending['date']),
205              'state' => 'moderation_pending',
206              )
207            );
208
209          $data = array();
210         
211          if (isset($pending['username']) and isset($id_of_user[ $pending['username'] ]))
212          {
213            $data['added_by'] = $id_of_user[ $pending['username'] ];
214          }
215         
216          foreach (array('date_creation', 'author', 'name', 'comment') as $field)
217          {
218            $value = getAttribute($pending['infos'], $field);
219            if (!empty($value))
220            {
221            $data[$field] = pwg_db_real_escape_string($value);
222            }
223          }
224         
225          if (count($data) > 0)
226          {
227            $data['id'] = $image_id;
228           
229            mass_updates(
230              IMAGES_TABLE,
231              array(
232                'primary' => array('id'),
233                'update'  => array_keys($data)
234                ),
235              array($data)
236              );
237          }
238         
239          // deletion
240          unlink($source_path);
241          if (!isset($pending['tn_ext']))
242          {
243            $pending['tn_ext'] = 'jpg';
244          }
245          @unlink(get_thumbnail_path(array('path'=>$source_path, 'tn_ext'=>$pending['tn_ext'])));
246        }
247      }
248     
249      if (count($inserts) > 0)
250      {
251        mass_inserts(
252          $prefixeTable.'community_pendings',
253          array_keys($inserts[0]),
254          $inserts
255          );
256      }
257    }
258    unlink($from_piwigo21_file);
259  }
260}
261
262function community_get_data_from_community21()
263{
264  global $prefixeTable;
265 
266  $old_community_table = $prefixeTable.'community';
267  $query = 'SHOW TABLES;';
268  $result = pwg_query($query);
269  while ($row = pwg_db_fetch_row($result))
270  {
271    if ($old_community_table == $row[0])
272    {
273      $inserts = array();
274     
275      $query = '
276SELECT
277    *
278  FROM '.$old_community_table.'
279;';
280      $result = pwg_query($query);
281      while ($row = pwg_db_fetch_assoc($result))
282      {
283        array_push(
284          $inserts,
285          array(
286            'type' => 'user',
287            'user_id' => $row['user_id'],
288            'category_id' => null,
289            'recursive' => 'true',
290            'create_subcategories' => $row['permission_level'] == 2 ? 'true' : 'false',
291            'moderated' => 'false',
292            )
293          );
294      }
295     
296      if (count($inserts) > 0)
297      {
298        mass_inserts(
299          $prefixeTable.'community_permissions',
300          array_keys($inserts[0]),
301          $inserts
302          );
303      }
304     
305      $query = 'DROP TABLE '.$old_community_table.';';
306      pwg_query($query);
307     
308      break;
309    }
310  }
311}
312
313function community_create_default_permission()
314{
315  global $prefixeTable;
316 
317  // create an album "Community"
318  $category_info = create_virtual_category('Community');
319
320  $insert = array(
321    'type' => 'any_registered_user',
322    'category_id' => $category_info['id'],
323    'recursive' => 'true',
324    'create_subcategories' => 'true',
325    'moderated' => 'true',
326    );
327
328  mass_inserts($prefixeTable.'community_permissions', array_keys($insert), array($insert));
329
330  include_once(dirname(__FILE__).'/include/functions_community.inc.php');
331  community_update_cache_key();
332}
333?>
Note: See TracBrowser for help on using the repository browser.