forked from petitssuisses/piwigo-ShareAlbum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintain.class.php
274 lines (243 loc) · 7.23 KB
/
maintain.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
*
* @author petitssuisses
*
*/
defined('PHPWG_ROOT_PATH') or die('Hacking attempt!');
// load functions
class ShareAlbum_maintain extends PluginMaintain
{
private $default_conf = array(
'option_hide_menus' => true, // option to hide menus for automatically logged in users
'option_show_login_menu' => true, // option to show a login menu
'option_replace_breadcrumbs' => true, // option to replace navigation breadcrumbs with album name
'option_remember_me' => true, // option to indicate remember me option for logged in users
'option_pics_per_page' => 15, // option to specify the maximum pictures per page
'option_enable_powerusers' => false, // option to enable power to selected non-admin users to share private albums
'option_recursive_shares' => true, // option to enable recursive shares (on sub-albums)
);
private $table;
private $table_log;
private $dir;
function __construct($plugin_id)
{
parent::__construct($plugin_id); // always call parent constructor
global $prefixeTable;
// Class members can't be declared with computed values so initialization is done here
$this->table = $prefixeTable . 'sharealbum';
$this->table_log = $prefixeTable . 'sharealbum_log';
$this->dir = PHPWG_ROOT_PATH . PWG_LOCAL_DIR . 'ShareAlbum/';
}
/**
* Plugin installation
*
* Perform here all needed step for the plugin installation such as create default config,
* add database tables, add fields to existing tables, create local folders...
*/
function install($plugin_version, &$errors=array())
{
global $conf;
// add config parameter
if (empty($conf['sharealbum']))
{
// conf_update_param well serialize and escape array before database insertion
// the third parameter indicates to update $conf['sharealbum'] global variable as well
conf_update_param('sharealbum', $this->default_conf, true);
}
// add the piwigo_sharealbum configuration table
pwg_query('
CREATE TABLE IF NOT EXISTS `'. $this->table .'` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cat` smallint(5) unsigned NOT NULL,
`user_id` mediumint(8) unsigned NOT NULL,
`code` varchar(32) NOT NULL,
`creation_date` datetime DEFAULT NULL,
`created_by` mediumint(8) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
');
pwg_query('
CREATE TABLE IF NOT EXISTS `'. $this->table_log .'` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cat_id` smallint(5) unsigned NOT NULL,
`ip` varchar(40) DEFAULT NULL,
`visit_d` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
');
// create the sharealbum group
pwg_query("
INSERT INTO `".GROUPS_TABLE."` (`name`)
VALUES ('sharealbum')
");
// 11.4 create the sharealbum_powerusers group
pwg_query("
INSERT INTO `".GROUPS_TABLE."` (`name`)
VALUES ('sharealbum_powerusers')
");
// create a local directory
if (!file_exists($this->dir))
{
mkdir($this->dir, 0755);
}
}
/**
* Plugin activation
*
* This function is triggered after installation, by manual activation or after a plugin update
* for this last case you must manage updates tasks of your plugin in this function
*/
function activate($plugin_version, &$errors=array())
{
}
/**
* Plugin deactivation
*
* Triggered before uninstallation or by manual deactivation
*/
function deactivate()
{
}
/**
* Plugin (auto)update
*
* This function is called when Piwigo detects that the registered version of
* the plugin is older than the version exposed in main.inc.php
* Thus it's called after a plugin update from admin panel or a manual update by FTP
*/
function update($old_version, $new_version, &$errors=array())
{
global $conf;
// Check missing params from successive update
foreach (array_keys($this->default_conf) as $default_conf_key) {
$old_conf = safe_unserialize($conf['sharealbum']);
if (!array_key_exists($default_conf_key,$old_conf)) {
$old_conf += [ $default_conf_key => $this->default_conf[$default_conf_key] ];
conf_update_param('sharealbum', $old_conf, true);
}
}
// sharealbum group
$group_id = -1;
$result_group = pwg_query("
SELECT `id`
FROM ".GROUPS_TABLE."
WHERE `name`='sharealbum'"
);
if (pwg_db_num_rows($result_group))
{
$row = pwg_db_fetch_assoc($result_group);
$group_id = $row['id'];
}
if ($group_id < 0) {
pwg_query("
INSERT INTO `".GROUPS_TABLE."` (`name`)
VALUES ('sharealbum')
");
// automatically assign group to previously created users
pwg_query("
INSERT IGNORE INTO ".USER_GROUP_TABLE." (group_id, user_id)
SELECT g.id, u.id
FROM ".GROUPS_TABLE." g, ".USERS_TABLE." u
WHERE g.name like 'sharealbum'
AND u.username like 'share_%'
");
}
// 11.4 sharealbum_powerusers group
$group_id = -1;
$result_group = pwg_query("
SELECT `id`
FROM ".GROUPS_TABLE."
WHERE `name`='sharealbum_powerusers'"
);
if (pwg_db_num_rows($result_group))
{
$row = pwg_db_fetch_assoc($result_group);
$group_id = $row['id'];
}
if ($group_id < 0) {
pwg_query("
INSERT INTO `".GROUPS_TABLE."` (`name`)
VALUES ('sharealbum_powerusers')
");
}
// 11.4 + 11.5 add created_by column sharealbum table
pwg_query('
ALTER TABLE `' . $this->table . '` ADD COLUMN IF NOT EXISTS created_by mediumint(8) NULL
;');
//$this->install($new_version, $errors);
$old_conf = safe_unserialize($conf['sharealbum']);
conf_update_param('sharealbum', $old_conf, true);
}
/**
* Plugin uninstallation
*
* Perform here all cleaning tasks when the plugin is removed
* you should revert all changes made in 'install'
*/
function uninstall()
{
// delete configuration
conf_delete_param('sharealbum');
// purge user access for shared albums
pwg_query("
DELETE
FROM ".USER_ACCESS_TABLE."
WHERE user_id IN (
SELECT s.user_id
FROM ".$this->table." s
)
");
// purge user infos
pwg_query("
DELETE
FROM ".USER_INFOS_TABLE."
WHERE user_id IN (
SELECT s.user_id
FROM ".$this->table." s
)
");
// purge group membership
pwg_query("
DELETE
FROM ".USER_GROUP_TABLE."
WHERE user_id IN (
SELECT s.user_id
FROM ".$this->table." s
)
");
// delete sharealbum users
// purge group membership
pwg_query("
DELETE
FROM ".USERS_TABLE."
WHERE id IN (
SELECT s.user_id
FROM ".$this->table." s
)
");
// delete groups
pwg_query("
DELETE
FROM ".GROUPS_TABLE."
WHERE name like 'sharealbum'
");
// 11.4
pwg_query("
DELETE
FROM ".GROUPS_TABLE."
WHERE name like 'sharealbum_powerusers'
");
// delete tables
pwg_query('DROP TABLE `'. $this->table .'`;');
pwg_query('DROP TABLE `'. $this->table_log .'`;');
// delete local folder
// use a recursive function if you plan to have nested directories
foreach (scandir($this->dir) as $file)
{
if ($file == '.' or $file == '..') continue;
unlink($this->dir.$file);
}
rmdir($this->dir);
}
}