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