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

Last change on this file since 28566 was 27657, checked in by plg, 10 years ago

bug fixed: activation of Community with API does not load admin functions

File size: 6.9 KB
Line 
1<?php
2defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
3
4if (!defined("COMMUNITY_PATH"))
5{
6  define('COMMUNITY_PATH', PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)));
7}
8
9include_once(COMMUNITY_PATH.'/include/install.inc.php');
10
11function plugin_install()
12{
13  community_install();
14  define('community_installed', true);
15}
16
17function plugin_uninstall()
18{
19  global $prefixeTable;
20 
21  $query = 'DROP TABLE '.$prefixeTable.'community_permissions;';
22  pwg_query($query);
23
24  $query = 'DROP TABLE '.$prefixeTable.'community_pendings;';
25  pwg_query($query);
26
27  $query = 'ALTER TABLE '.$prefixeTable.'categories drop column community_user;';
28  pwg_query($query);
29
30  // delete configuration
31  pwg_query('DELETE FROM `'. CONFIG_TABLE .'` WHERE param IN ("community", "community_cache_key");');
32}
33
34function plugin_activate()
35{
36  global $prefixeTable;
37 
38  if (!defined('community_installed')) // a plugin is activated just after its installation
39  {
40    community_install();
41  }
42
43  community_get_data_from_core21();
44  community_get_data_from_community21();
45
46  $query = '
47SELECT
48    COUNT(*)
49  FROM '.$prefixeTable.'community_permissions
50;';
51  list($counter) = pwg_db_fetch_row(pwg_query($query));
52  if (0 == $counter)
53  {
54    community_create_default_permission();
55  }
56
57  include_once(dirname(__FILE__).'/include/functions_community.inc.php');
58  community_update_cache_key();
59}
60
61function community_get_data_from_core21()
62{
63  global $conf, $prefixeTable;
64 
65  $from_piwigo21_file = $conf['data_location'].'/plugins/core_user_upload_to_community.php';
66  if (is_file($from_piwigo21_file))
67  {
68    include($from_piwigo21_file);
69    $user_upload_conf = unserialize($user_upload_conf);
70   
71    $user_upload_conf['upload_user_access'] = 1;
72
73    if (isset($user_upload_conf['uploadable_categories']) and is_array($user_upload_conf['uploadable_categories']))
74    {
75      $type = 'any_registered_user';
76      if (isset($user_upload_conf['upload_user_access']) and 1 == $user_upload_conf['upload_user_access'])
77      {
78        $type = 'any_visitor';
79      }
80
81      $inserts = array();
82   
83      foreach ($user_upload_conf['uploadable_categories'] as $category_id)
84      {
85        array_push(
86          $inserts,
87          array(
88            'type' => $type,
89            'category_id' => $category_id,
90            'recursive' => 'false',
91            'create_subcategories' => 'false',
92            'moderated' => 'true',
93            )
94          );
95      }
96     
97      if (count($inserts) > 0)
98      {
99        mass_inserts(
100          $prefixeTable.'community_permissions',
101          array_keys($inserts[0]),
102          $inserts
103          );
104      }
105    }
106   
107    if (isset($user_upload_conf['waiting_rows']) and is_array($user_upload_conf['waiting_rows']))
108    {
109      $id_of_user = array();
110     
111      $query = '
112SELECT
113    '.$conf['user_fields']['id'].' AS id,
114    '.$conf['user_fields']['username'].' AS username
115  FROM '.USERS_TABLE.'
116;';
117      $result = pwg_query($query);
118      while ($row = pwg_db_fetch_assoc($result))
119      {
120        $id_of_user[ $row['username'] ] = $row['id'];
121      }
122     
123      $inserts = array();
124     
125      foreach ($user_upload_conf['waiting_rows'] as $pending)
126      {
127        $source_path = get_complete_dir($pending['storage_category_id']).$pending['file'];
128       
129        if (is_file($source_path))
130        {
131          $image_id = add_uploaded_file($source_path, $pending['file'], array($pending['storage_category_id']), 16);
132         
133          array_push(
134            $inserts,
135            array(
136              'image_id' => $image_id,
137              'added_on' => date ('Y-m-d H:i:s', $pending['date']),
138              'state' => 'moderation_pending',
139              )
140            );
141
142          $data = array();
143         
144          if (isset($pending['username']) and isset($id_of_user[ $pending['username'] ]))
145          {
146            $data['added_by'] = $id_of_user[ $pending['username'] ];
147          }
148         
149          foreach (array('date_creation', 'author', 'name', 'comment') as $field)
150          {
151            $value = getAttribute($pending['infos'], $field);
152            if (!empty($value))
153            {
154            $data[$field] = pwg_db_real_escape_string($value);
155            }
156          }
157         
158          if (count($data) > 0)
159          {
160            $data['id'] = $image_id;
161           
162            mass_updates(
163              IMAGES_TABLE,
164              array(
165                'primary' => array('id'),
166                'update'  => array_keys($data)
167                ),
168              array($data)
169              );
170          }
171         
172          // deletion
173          unlink($source_path);
174          if (!isset($pending['tn_ext']))
175          {
176            $pending['tn_ext'] = 'jpg';
177          }
178          @unlink(get_thumbnail_path(array('path'=>$source_path, 'tn_ext'=>$pending['tn_ext'])));
179        }
180      }
181     
182      if (count($inserts) > 0)
183      {
184        mass_inserts(
185          $prefixeTable.'community_pendings',
186          array_keys($inserts[0]),
187          $inserts
188          );
189      }
190    }
191    unlink($from_piwigo21_file);
192  }
193}
194
195function community_get_data_from_community21()
196{
197  global $prefixeTable;
198 
199  $old_community_table = $prefixeTable.'community';
200  $query = 'SHOW TABLES;';
201  $result = pwg_query($query);
202  while ($row = pwg_db_fetch_row($result))
203  {
204    if ($old_community_table == $row[0])
205    {
206      $inserts = array();
207     
208      $query = '
209SELECT
210    *
211  FROM '.$old_community_table.'
212;';
213      $result = pwg_query($query);
214      while ($row = pwg_db_fetch_assoc($result))
215      {
216        array_push(
217          $inserts,
218          array(
219            'type' => 'user',
220            'user_id' => $row['user_id'],
221            'category_id' => null,
222            'recursive' => 'true',
223            'create_subcategories' => $row['permission_level'] == 2 ? 'true' : 'false',
224            'moderated' => 'false',
225            )
226          );
227      }
228     
229      if (count($inserts) > 0)
230      {
231        mass_inserts(
232          $prefixeTable.'community_permissions',
233          array_keys($inserts[0]),
234          $inserts
235          );
236      }
237     
238      $query = 'DROP TABLE '.$old_community_table.';';
239      pwg_query($query);
240     
241      break;
242    }
243  }
244}
245
246function community_create_default_permission()
247{
248  global $prefixeTable;
249
250  // is there a "Community" album?
251  $query = '
252SELECT
253    id
254  FROM '.CATEGORIES_TABLE.'
255  WHERE name = \'Community\'
256;';
257  $result = pwg_query($query);
258  while ($row = pwg_db_fetch_assoc($result))
259  {
260    $category_id = $row['id'];
261    break;
262  }
263
264  if (!isset($category_id))
265  {
266    // create an album "Community"
267    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
268    $category_info = create_virtual_category('Community');
269    $category_id = $category_info['id'];
270  }
271
272  $insert = array(
273    'type' => 'any_registered_user',
274    'category_id' => $category_id,
275    'recursive' => 'true',
276    'create_subcategories' => 'true',
277    'moderated' => 'true',
278    );
279
280  mass_inserts($prefixeTable.'community_permissions', array_keys($insert), array($insert));
281}
282?>
Note: See TracBrowser for help on using the repository browser.