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

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

manage quota (number of photos, disk usage)

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