-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.php
More file actions
190 lines (169 loc) · 6.27 KB
/
Copy pathhook.php
File metadata and controls
190 lines (169 loc) · 6.27 KB
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
<?php
/**
* Cloud Storage for GLPI - Install/Uninstall hooks
*
* @license GPL-3.0-or-later
*/
function plugin_cloudstorage_install(): bool
{
global $DB;
// --- Migration from azureblobstorage plugin ---
// Migrate table: rename + alter column
if (
$DB->tableExists('glpi_plugin_azureblobstorage_documenttrackers')
&& !$DB->tableExists('glpi_plugin_cloudstorage_documenttrackers')
) {
if (!$DB->doQuery(
"RENAME TABLE `glpi_plugin_azureblobstorage_documenttrackers`
TO `glpi_plugin_cloudstorage_documenttrackers`"
)) {
trigger_error(
sprintf('[CloudStorage] Migration rename failed: %s', $DB->error()),
E_USER_WARNING
);
return false;
}
if (!$DB->doQuery(
"ALTER TABLE `glpi_plugin_cloudstorage_documenttrackers`
CHANGE `azure_blob_name` `remote_path` varchar(512) NOT NULL DEFAULT ''"
)) {
trigger_error(
sprintf('[CloudStorage] Migration alter failed: %s', $DB->error()),
E_USER_WARNING
);
}
}
// Migrate config: old context → new context with renamed keys
$oldConfig = Config::getConfigurationValues('plugin:azureblobstorage');
if (!empty($oldConfig)) {
$keyMap = [
'connection_string' => 'azure_connection_string',
'account_name' => 'azure_account_name',
'account_key' => 'azure_account_key',
'container_name' => 'azure_container_name',
'sas_expiry_minutes' => 'url_expiry_minutes',
'storage_mode' => 'storage_mode',
'download_method' => 'download_method',
'enabled' => 'enabled',
];
$enumMap = [
'storage_mode' => [
'azure_primary' => 'cloud_primary',
'azure_backup' => 'cloud_backup',
],
'download_method' => [
'sas_redirect' => 'redirect',
],
];
$newValues = ['provider' => 'azure'];
foreach ($keyMap as $oldKey => $newKey) {
if (isset($oldConfig[$oldKey])) {
$value = $oldConfig[$oldKey];
// Map old enum values to new ones
if (isset($enumMap[$oldKey][$value])) {
$value = $enumMap[$oldKey][$value];
}
$newValues[$newKey] = $value;
}
}
Config::setConfigurationValues('plugin:cloudstorage', $newValues);
// Remove old config context
$oldKeys = array_keys($oldConfig);
Config::deleteConfigurationValues('plugin:azureblobstorage', $oldKeys);
}
// --- Create table if not exists (fresh install) ---
if (!$DB->tableExists('glpi_plugin_cloudstorage_documenttrackers')) {
$query = "CREATE TABLE `glpi_plugin_cloudstorage_documenttrackers` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`documents_id` int unsigned NOT NULL DEFAULT 0,
`filepath` varchar(255) NOT NULL DEFAULT '',
`sha1sum` char(40) NOT NULL DEFAULT '',
`remote_path` varchar(512) NOT NULL DEFAULT '',
`uploaded_at` timestamp NULL DEFAULT NULL,
`file_size` bigint unsigned DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY `documents_id` (`documents_id`),
KEY `filepath` (`filepath`),
KEY `sha1sum` (`sha1sum`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
if (!$DB->doQuery($query)) {
trigger_error(
sprintf('[CloudStorage] Error creating table: %s', $DB->error()),
E_USER_ERROR
);
return false;
}
}
// Set default config values
$defaults = [
'provider' => 'azure',
'azure_connection_string' => '',
'azure_account_name' => '',
'azure_account_key' => '',
'azure_container_name' => 'glpi-documents',
's3_access_key_id' => '',
's3_secret_access_key' => '',
's3_region' => '',
's3_bucket_name' => '',
's3_endpoint' => '',
'storage_mode' => 'cloud_primary',
'download_method' => 'redirect',
'url_expiry_minutes' => '5',
'enabled' => '0',
];
$existing = Config::getConfigurationValues('plugin:cloudstorage');
$to_set = [];
foreach ($defaults as $key => $value) {
if (!isset($existing[$key])) {
$to_set[$key] = $value;
}
}
if (!empty($to_set)) {
Config::setConfigurationValues('plugin:cloudstorage', $to_set);
}
return true;
}
function plugin_cloudstorage_uninstall(): bool
{
global $DB;
if ($DB->tableExists('glpi_plugin_cloudstorage_documenttrackers')) {
// Block uninstall if documents are still tracked in cloud storage
$result = $DB->request([
'COUNT' => 'total',
'FROM' => 'glpi_plugin_cloudstorage_documenttrackers',
]);
$count = (int) ($result->current()['total'] ?? 0);
if ($count > 0) {
trigger_error(
sprintf(
'[CloudStorage] Cannot uninstall: %d documents are still tracked in cloud storage. '
. 'Run "php bin/console plugins:cloudstorage:migrate-local" first '
. 'to download all documents back to local storage.',
$count
),
E_USER_ERROR
);
return false;
}
$DB->doQuery("DROP TABLE IF EXISTS `glpi_plugin_cloudstorage_documenttrackers`");
}
// Remove config values
$config_keys = [
'provider',
'azure_connection_string',
'azure_account_name',
'azure_account_key',
'azure_container_name',
's3_access_key_id',
's3_secret_access_key',
's3_region',
's3_bucket_name',
's3_endpoint',
'storage_mode',
'download_method',
'url_expiry_minutes',
'enabled',
];
Config::deleteConfigurationValues('plugin:cloudstorage', $config_keys);
return true;
}