forked from jrast/Foldergallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.php
279 lines (259 loc) · 12.3 KB
/
upgrade.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
275
276
277
278
279
<?php
// prevent this file from being accessed directly
/* -------------------------------------------------------- */
// Must include code to prevent this file from being accessed directly
if (!defined('SYSTEM_RUN')) { header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found'); flush(); exit; }
/* -------------------------------------------------------- */
/*******************************************************************************
*
* Update from Version 1.17 and higher to newest Versions
*
* new Settings Table
* new Thumbnail-Settings
* categorie text field now as text
* delete unused files (many of them are moved to the admin folder)
*
******************************************************************************/
/*
* BEGIN: Create new settings table
*/
// next 2 vars not needed if workout with simpledisatcher
$sAddonPath = __DIR__;
if (is_readable($sAddonPath.'/init.php')) {require ($sAddonPath.'/init.php');}
// check if upgrade startet by upgrade-script to echo a message
$globalStarted = preg_match('/upgrade\-script\.php$/', $_SERVER["SCRIPT_NAME"]);
$sWbVersion = ($globalStarted && defined('VERSION') ? VERSION : WB_VERSION);
Translate::getInstance()->enableAddon(ADMIN_DIRECTORY.'\\addons');
if (is_writable(WB_PATH.'/temp/cache')) {
Translate::getInstance()->clearCache();
}
$OK = '<span class="ok">OK</span>';
$FAIL = '<span class="error">FAILED</span>';
$iErr = false;
$sNameValPairs = '';
$bOldStructure = false;
// $sTableName = 'fg134_mod_foldergallery_settings';
$sTableName = TABLE_PREFIX.'mod_foldergallery_settings';
$i = (!isset($i) ? 1 : $i);
$aMsg = [];
$sInstallStruct = $sAddonPath.'/install-struct.sql';
$sInstallUpdate = $sAddonPath.'/install-upgrade.sql';
$dbName = DB_NAME;
$aSettings = [];
$aNewSettings = [];
$aOldSettings = [];
$aDefaultSettings = [];
if (is_readable($sAddonPath.'/presets/thumbPresets.php')){require($sAddonPath.'/presets/thumbPresets.php');}
if (!is_readable($sInstallUpdate)) {
$aMsg[] = '<strong>\'missing or not readable file [install-upgrade.sql]\'</strong> '.$FAIL.'<br />';
$iErr = true;
} else {
// get settings from table and return an array otherwise false
$selectSettings = (function() use ($database, $sTableName) {
$aSettings = [];
$sql = 'SELECT * FROM `'.$sTableName.'`';
if ($oRes = $database->query($sql)){
$aSettings = $oRes->fetchAll(MYSQLI_ASSOC);
}
return ((sizeof($aSettings) > 0) ? $aSettings : false);
});
// prepare an array to create the sql statement
$prepareSettings = (function($aOldSettings) use ($MediaRel,$aDefaults)
{
$i=0;
$iSectionId = 0;
$aInvisibile = explode(',',$aDefaults['invisible']);
$iSectionId = $aOldSettings[$i]['section_id'];
$aTmpSettings = [];
$aTmpSettings[$i]['section_id'] = $iSectionId;
foreach ($aOldSettings as $aSettings) {
$sIndexName = $aSettings['s_name'];
$sValue = $aSettings['s_value'];
$sValue = ($sIndexName=='root_dir'?str_replace($MediaRel, '', $sValue):$sValue);
$aTmpSettings[$i][$sIndexName] = $sValue;
if ($iSectionId != $aSettings['section_id']){
$i++;
$iSectionId = $aSettings['section_id'];
$aTmpSettings[$i]['section_id'] = $iSectionId;
}
if ($sIndexName == 'invisible'){
$aForbidden['invisible'] = array_diff($aInvisibile, explode(',',$aTmpSettings[$i]['invisible']));
$sForbidden = ','.implode(',',$aForbidden['invisible']);
$aTmpSettings[$i]['invisible'] = trim($aTmpSettings[$i]['invisible'].$sForbidden,',');
}
}
return ((sizeof($aTmpSettings) > 0) ? $aTmpSettings : false);
});
// try to create table if not exists
$database->SqlImport($sInstallStruct, TABLE_PREFIX, true );
// read settings first
$sql = 'SELECT * FROM `'.$sTableName.'`';
// check if table already upgraded
if ($bOldStructure = $database->field_exists($sTableName, 'page_id'))
{
if (!($aOldSettings = $selectSettings()))
{
$aMsg[] = '<strong>\'Folder Gallery backup old settings\'</strong> '.$FAIL.'<br />';
$iErr = true;
}
$i=0;
for ($i=0; $i < sizeof($aOldSettings); $i++){
$aOldSettings[$i]['root_dir'] = str_replace($MediaRel, '', $aOldSettings[$i]['root_dir']);
}
} elseif ($database->field_exists($sTableName, 'id'))
{
if (!($aOldSettings = $selectSettings()))
{
$aMsg[] = '<strong>\'Folder Gallery backup old settings\'</strong> '.$FAIL.'<br />';
$iErr = true;
} else {
$aTmp = $aOldSettings;
$i=0;
$aOldSettings = [];
// firstremove id
foreach ($aTmp as $key=>$aValue){
$del = array_shift($aValue);
$aOldSettings[$key] = $aValue;
}
// prepare an array to create the sql statement
$aOldSettings = $prepareSettings($aOldSettings);
}
} elseif ($database->field_exists($sTableName, 's_name'))
{
// overwrite standardsettings ($aOldSettings)
if (!($aOldSettings = $selectSettings()))
{
$aMsg[] = '<strong>\'FolderGallery backup old settings\'</strong> '.$FAIL.'<br />';
$aMsg[] = '<strong>\'FolderGallery has no old settings\'</strong> '.$FAIL.'<br />';
$iErr = true;
} else {
// prepare an array to create the sql statement
$aOldSettings = $prepareSettings($aOldSettings);
}
} // end handling new structure
// delete none allowed entries in settings.s_name
$aSettingsDenied = ['section_id','s_name','s_value'];
// drop old table and create new one ident_foldergallery
if ((count($aOldSettings)
&& !$iErr && count($aDefaults))
&& $database->SqlImport($sInstallUpdate, TABLE_PREFIX, false)
) {
// restore old settings if there any
if ($aOldSettings && !$iErr)
{
$iSectionId = 0;
foreach ($aOldSettings as $aSettings) {
// add new default settings
$aSettings = array_replace_recursive( $aDefaults, $aSettings );
$iSectionId = $aSettings['section_id'];
// update sections modulename if changed with same table structure
$sqlSection = 'UPDATE `'.TABLE_PREFIX.'sections` SET '
. '`module` = \''.$sAddonName.'\' '
. 'WHERE `'.TABLE_PREFIX.'sections`.`section_id` = '.$iSectionId.';';
if (!$database->query($sqlSection)){
$aMsg[] = $database->get_error().'<br />';
$aMsg[] = '<strong>Folder Gallery couldn\'t change sections entry ID ('.$iSectionId.')</strong> '.$FAIL.'<br />';
}
$sNameValPairs = '';
// insert settings per section
foreach ($aSettings as $index => $val) {
if (in_array($index, $aSettingsDenied) ) { continue; }
$sNameValPairs .= ','."\n".' ('.$iSectionId.', \''.$index.'\', \''.$database->escapeString($val).'\')';
}
$sValues = ltrim($sNameValPairs, ', ');
$sTableName = TABLE_PREFIX.'mod_foldergallery_settings';
$sql = 'INSERT INTO `'.$sTableName.'` (`section_id`, `s_name`, `s_value`) '
. 'VALUES '.$sValues.';';
if (!$database->query($sql)) {
$aMsg[] = $database->get_error().'<br />';
$aMsg[] = '<strong>\'Folder Gallery INSERT INTO (restore) old settings\'</strong> '.$FAIL.'<br />';
$iErr = true;
}
} // end for settings per section
}
} else {
$aMsg[] = $database->get_error().'<br />';
$aMsg[] = '<strong>\'Folder Gallery recreate table\'</strong> '.$FAIL.'<br />';
$iErr = true;
}
if (!$iErr) {
$aMsg[] = '<strong>\'Folder Gallery successful updated\'</strong> '.$OK.'<br />';
} else {
$aMsg[] = '<strong>\'Folder Gallery couldn\'t updated\'</strong> '.$FAIL.'<br />';
}
// unset($getSettings);
}
/*--------------------------------------------------------------------------------------------------*/
/**
* There are files which are moved or no longer needed.
* So we need to delete the old files and directories
*/
/*--------------------------------------------------------------------------------------------------*/
$aDirDelete = array(
);
$aFilesDelete = array(
'[MODULES]/modify_cat.php',
'[MODULES]/modify_cat_sort.php',
'[MODULES]/modify_settings.php',
'[MODULES]/modify_thumb.php',
'[MODULES]/save_cat.ph',
'[MODULES]/save_files.php',
'[MODULES]/save_settings.php',
'[MODULES]/sync.php',
'[MODULES]/frontend.css.orig',
'[MODULES]/scripts/backend.functions.ph',
'[MODULES]/scripts/delete_cat.php',
'[MODULES]/scripts/delete_img.php',
'[MODULES]/scripts/move_down.php',
'[MODULES]/scripts/move_up.php',
'[MODULES]/scripts/quick_img_sort.php',
'[MODULES]/scripts/reorderCNC.php',
'[MODULES]/scripts/reorderDND.php',
'[MODULES]/templates/Unbenannt1.html',
'[MODULES]/templates/modify_cat.htt',
'[MODULES]/templates/modify_cat_sort.htt',
'[MODULES]/templates/modify_settings.htt',
'[MODULES]/templates/modify.htt',
'[MODULES]/templates/view_ad_gallery.htt',
'[MODULES]/templates/view_galleryview30.htt',
'[MODULES]/templates/view_pirobox.htt',
'[MODULES]/templates/view_prettyphoto.htt',
'[MODULES]/templates/default/view_responsiveLightboxCaption.htt',
'[MODULES]/templates/default/view_colorboxCaption.htt',
'[MODULES]/templates/default/view_jqueryFancyboxCaption.htt',
'[MODULES]/templates/default/view_magnific_popupCaption.htt',
'[MODULES]/themes/default/FineUpload/fine-uploader.js',
'[MODULES]/themes/default/FineUpload/jquery.fine-uploader.js',
'[MODULES]/images/eck.gif',
'[MODULES]/images/crumbs.gif',
);
$aSearches = array(
'[MODULES]',
);
$aReplacements = array(
'/modules/'.$sAddonName,
);
$aMsg = [];
array_walk(
$aFilesDelete,
function (&$sFile) use($aSearches, $aReplacements) {
$sFile = WB_PATH.str_replace( '\\', '/', str_replace($aSearches, $aReplacements, $sFile) );
}
);
$WbPath = WB_PATH.'/modules/'.$sAddonName;
foreach ( $aFilesDelete as $sFileToDelete ) {
if (false !== ($aExistingFiles = glob(dirname($sFileToDelete).'/*', GLOB_MARK)) ) {
if ( in_array($sFileToDelete, $aExistingFiles) ) {
if ( is_writable($sFileToDelete) && unlink($sFileToDelete) ) {
$aMsg[] = '<strong>Delete '.str_replace($WbPath, '',$sFileToDelete).'</strong>'." $OK<br />";
} else {
$aMsg[] = str_replace($WbPath, '',$sFileToDelete).' deleted '.$FAIL;
}
}
}
}
unset($aExistingFiles);
/*--------------------------------------------------------------------------------------------------*/
end:
if (!$globalStarted && sizeof($aMsg)) {print implode("\n", $aMsg)."\n";}
// end of file