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

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

force refresh of all community user permissions cache each time the plugin is activated (it includes plugin updates)

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  include_once(dirname(__FILE__).'/include/functions_community.inc.php');
125  community_update_cache_key();
126}
127
128function community_get_data_from_core21()
129{
130  global $conf, $prefixeTable;
131 
132  $from_piwigo21_file = $conf['local_data_dir'].'/plugins/core_user_upload_to_community.php';
133  if (is_file($from_piwigo21_file))
134  {
135    include($from_piwigo21_file);
136    $user_upload_conf = unserialize($user_upload_conf);
137   
138    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
139    prepare_upload_configuration();
140
141    $user_upload_conf['upload_user_access'] = 1;
142
143    if (isset($user_upload_conf['uploadable_categories']) and is_array($user_upload_conf['uploadable_categories']))
144    {
145      $type = 'any_registered_user';
146      if (isset($user_upload_conf['upload_user_access']) and 1 == $user_upload_conf['upload_user_access'])
147      {
148        $type = 'any_visitor';
149      }
150
151      $inserts = array();
152   
153      foreach ($user_upload_conf['uploadable_categories'] as $category_id)
154      {
155        array_push(
156          $inserts,
157          array(
158            'type' => $type,
159            'category_id' => $category_id,
160            'recursive' => 'false',
161            'create_subcategories' => 'false',
162            'moderated' => 'true',
163            )
164          );
165      }
166     
167      if (count($inserts) > 0)
168      {
169        mass_inserts(
170          $prefixeTable.'community_permissions',
171          array_keys($inserts[0]),
172          $inserts
173          );
174      }
175    }
176   
177    if (isset($user_upload_conf['waiting_rows']) and is_array($user_upload_conf['waiting_rows']))
178    {
179      $id_of_user = array();
180     
181      $query = '
182SELECT
183    '.$conf['user_fields']['id'].' AS id,
184    '.$conf['user_fields']['username'].' AS username
185  FROM '.USERS_TABLE.'
186;';
187      $result = pwg_query($query);
188      while ($row = pwg_db_fetch_assoc($result))
189      {
190        $id_of_user[ $row['username'] ] = $row['id'];
191      }
192     
193      $inserts = array();
194     
195      foreach ($user_upload_conf['waiting_rows'] as $pending)
196      {
197        $source_path = get_complete_dir($pending['storage_category_id']).$pending['file'];
198       
199        if (is_file($source_path))
200        {
201          $image_id = add_uploaded_file($source_path, $pending['file'], array($pending['storage_category_id']), 16);
202         
203          array_push(
204            $inserts,
205            array(
206              'image_id' => $image_id,
207              'added_on' => date ('Y-m-d H:i:s', $pending['date']),
208              'state' => 'moderation_pending',
209              )
210            );
211
212          $data = array();
213         
214          if (isset($pending['username']) and isset($id_of_user[ $pending['username'] ]))
215          {
216            $data['added_by'] = $id_of_user[ $pending['username'] ];
217          }
218         
219          foreach (array('date_creation', 'author', 'name', 'comment') as $field)
220          {
221            $value = getAttribute($pending['infos'], $field);
222            if (!empty($value))
223            {
224            $data[$field] = pwg_db_real_escape_string($value);
225            }
226          }
227         
228          if (count($data) > 0)
229          {
230            $data['id'] = $image_id;
231           
232            mass_updates(
233              IMAGES_TABLE,
234              array(
235                'primary' => array('id'),
236                'update'  => array_keys($data)
237                ),
238              array($data)
239              );
240          }
241         
242          // deletion
243          unlink($source_path);
244          if (!isset($pending['tn_ext']))
245          {
246            $pending['tn_ext'] = 'jpg';
247          }
248          @unlink(get_thumbnail_path(array('path'=>$source_path, 'tn_ext'=>$pending['tn_ext'])));
249        }
250      }
251     
252      if (count($inserts) > 0)
253      {
254        mass_inserts(
255          $prefixeTable.'community_pendings',
256          array_keys($inserts[0]),
257          $inserts
258          );
259      }
260    }
261    unlink($from_piwigo21_file);
262  }
263}
264
265function community_get_data_from_community21()
266{
267  global $prefixeTable;
268 
269  $old_community_table = $prefixeTable.'community';
270  $query = 'SHOW TABLES;';
271  $result = pwg_query($query);
272  while ($row = pwg_db_fetch_row($result))
273  {
274    if ($old_community_table == $row[0])
275    {
276      $inserts = array();
277     
278      $query = '
279SELECT
280    *
281  FROM '.$old_community_table.'
282;';
283      $result = pwg_query($query);
284      while ($row = pwg_db_fetch_assoc($result))
285      {
286        array_push(
287          $inserts,
288          array(
289            'type' => 'user',
290            'user_id' => $row['user_id'],
291            'category_id' => null,
292            'recursive' => 'true',
293            'create_subcategories' => $row['permission_level'] == 2 ? 'true' : 'false',
294            'moderated' => 'false',
295            )
296          );
297      }
298     
299      if (count($inserts) > 0)
300      {
301        mass_inserts(
302          $prefixeTable.'community_permissions',
303          array_keys($inserts[0]),
304          $inserts
305          );
306      }
307     
308      $query = 'DROP TABLE '.$old_community_table.';';
309      pwg_query($query);
310     
311      break;
312    }
313  }
314}
315
316function community_create_default_permission()
317{
318  global $prefixeTable;
319 
320  // create an album "Community"
321  $category_info = create_virtual_category('Community');
322
323  $insert = array(
324    'type' => 'any_registered_user',
325    'category_id' => $category_info['id'],
326    'recursive' => 'true',
327    'create_subcategories' => 'true',
328    'moderated' => 'true',
329    );
330
331  mass_inserts($prefixeTable.'community_permissions', array_keys($insert), array($insert));
332}
333?>
Note: See TracBrowser for help on using the repository browser.