-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalternative_styles.php
437 lines (345 loc) · 15.9 KB
/
alternative_styles.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/**
*
* @package adm
* @copyright (c) 2007 Kleeja.com
* @license ./docs/license.txt
*
*/
// not for directly open
if (! defined('IN_ADMIN')) {
exit();
}
//set _get form key
$GET_FORM_KEY = kleeja_add_form_key_get('adm_styles_get');
$H_FORM_KEYS = kleeja_add_form_key('adm_styles');
$action = ADMIN_PATH . '?cp=' . basename(__file__, '.php');
$style_select_link = $action . '&case=select&' . $GET_FORM_KEY . '&style=';
$style_download_link = $action . '&case=download&' . $GET_FORM_KEY . '&style=';
$style_delete_link = $action . '&case=dfolder&' . $GET_FORM_KEY . '&style=';
$style_upload_link = $action . '&case=upload';
$stylee = 'alternative_styles';
$styleePath = dirname(__FILE__);
$case = g('case', 'str', 'local');
//check _GET Csrf token
if (! empty($case) && in_array($case, ['select', 'download', 'dfolder'])) {
if (! kleeja_check_form_key_get('adm_styles_get')) {
header('HTTP/1.0 401 Unauthorized');
kleeja_admin_err($lang['INVALID_GET_KEY'], $action);
}
}
//check _POST Csrf token
if (ip('newstyle')) {
if (! kleeja_check_form_key('adm_styles')) {
header('HTTP/1.0 401 Unauthorized');
kleeja_admin_err($lang['INVALID_FORM_KEY'], true, $lang['ERROR'], true, $action);
}
$case = 'upload';
}
switch ($case):
default:
case 'local':
case 'store':
//get styles
$available_styles = [];
if ($dh = @opendir(PATH . 'styles')) {
while (false !== ($folder_name = readdir($dh))) {
if (is_dir(PATH . 'styles/' . $folder_name) && preg_match('/[a-z0-9_.]{3,}/', $folder_name)) {
//info
$style_info_arr = [
'name' => $folder_name,
'desc' => '',
'copyright' => '',
'version' => ''
];
if (($style_info = kleeja_style_info($folder_name)) != false) {
foreach (['name', 'desc', 'copyright', 'version'] as $InfoKey) {
if (array_key_exists($InfoKey, $style_info)) {
if (is_array($style_info[$InfoKey])) {
$style_info_arr[$InfoKey] = ! empty($style_info[$InfoKey][$config['language']])
? htmlspecialchars($style_info[$InfoKey][$config['language']])
: htmlspecialchars($style_info[$InfoKey]['en']);
} else {
$style_info_arr[$InfoKey] = htmlspecialchars($style_info[$InfoKey]);
}
}
}
}
$available_styles[$folder_name] = [
'name' => $folder_name,
'is_default' => $config['style'] == $folder_name ? true : false,
'link_mk_default' => basename(ADMIN_PATH) . '?cp=' . basename(__file__, '.php') . '&style_choose=' . $folder_name,
'icon' => file_exists(PATH . 'styles/' . $folder_name . '/screenshot.png')
? PATH . 'styles/' . $folder_name . '/screenshot.png'
: $STYLE_PATH_ADMIN . 'images/style.png',
'info' => $style_info_arr
];
}
}
@closedir($dh);
}
//do not proceed if not store case
if (! in_array($case, ['store', 'check'])) {
break;
}
// plugins avilable in kleeja remote catalog
if (! ($store_catalog = $cache->get('store_catalog'))) {
$store_link = 'https://raw.githubusercontent.com/kleeja-official/store-catalog/master/catalog.json';
$store_catalog = FetchFile::make($store_link)->get();
$store_catalog = json_decode($store_catalog, true);
if (json_last_error() == JSON_ERROR_NONE) {
$cache->save('store_catalog', $store_catalog);
}
}
// make an array for all styles in kleeja remote catalog
// that are not exsisted locally.
$store_styles = [];
$available_styles_names = array_column($available_styles, 'name');
foreach ($store_catalog as $style_info) {
if ($style_info['type'] != 'style') {
continue;
}
if ($case == 'store' && ! empty($available_styles[$style_info['name']])) {
continue;
}
// is there a new version of this in the store
elseif (
$case == 'check' && (! empty($available_styles[$style_info['name']]['info']['version']) &&
version_compare(
strtolower($available_styles[$style_info['name']]['info']['version']),
strtolower($style_info['file']['version']),
'>='
) || empty($available_styles[$style_info['name']]['info']['version']))
) {
continue;
}
$store_styles[$style_info['name']] = [
'name' => $style_info['name'],
'developer' => $style_info['developer'],
'version' => $style_info['file']['version'],
'title' => ! empty($style_info['title'][$config['language']]) ? $style_info['title'][$config['language']] : $style_info['title']['en'],
'website' => $style_info['website'],
'current_version' => ! empty($available_styles[$style_info['name']]) ? strtolower($available_styles[$style_info['name']]['info']['version']) : '',
'kj_min_version' => $style_info['kleeja_version']['min'],
'kj_max_version' => $style_info['kleeja_version']['max'],
'kj_version_cmtp' => sprintf($lang['KLJ_VER_NO_PLUGIN'], $style_info['kleeja_version']['min'], $style_info['kleeja_version']['max']),
'icon' => $style_info['icon'],
'NotCompatible' => version_compare(strtolower($style_info['kleeja_version']['min']), KLEEJA_VERSION, '<=')
&& version_compare(strtolower($style_info['kleeja_version']['max']), KLEEJA_VERSION, '>=')
? false : true,
];
}
$store_styles_count = sizeof($store_styles);
break;
case 'select':
$style_name = preg_replace('/[^a-z0-9_\-\.]/i', '', g('style'));
//if empty, let's ignore it
if (empty($style_name)) {
redirect(basename(ADMIN_PATH) . '?cp=' . basename(__file__, '.php'));
}
//
//check if this style depend on other style and
//check kleeja version that required by this style
//
if (($style_info = kleeja_style_info($style_name)) != false) {
if (isset($style_info['depend_on']) && ! is_dir(PATH . 'styles/' . $style_info['depend_on'])) {
kleeja_admin_err(sprintf($lang['DEPEND_ON_NO_STYLE_ERR'], $style_info['depend_on']));
}
if (isset($style_info['kleeja_version']) && version_compare(strtolower($style_info['kleeja_version']), strtolower(KLEEJA_VERSION), '>')) {
kleeja_admin_err(sprintf($lang['KLJ_VER_NO_STYLE_ERR'], $style_info['kleeja_version']));
}
//is this style require some plugins to be installed
if (isset($style_info['plugins_required'])) {
$plugins_required = explode(',', $style_info['plugins_required']);
$plugins_required = array_map('trim', $plugins_required);
$query = [
'SELECT' => 'plg_name, plg_disabled',
'FROM' => "{$dbprefix}plugins",
];
$result = $SQL->build($query);
if ($SQL->num_rows($result) != 0) {
$plugins_required = array_flip($plugins_required);
while ($row = $SQL->fetch_array($result)) {
if (in_array($row['plg_name'], $plugins_required) and $row['plg_disabled'] != 1) {
unset($plugins_required[$row['plg_name']]);
}
}
}
$SQL->freeresult($result);
$plugins_required = array_flip($plugins_required);
if (sizeof($plugins_required)) {
kleeja_admin_err(sprintf($lang['PLUGINS_REQ_NO_STYLE_ERR'], implode(', ', $plugins_required)));
}
}
}
//make it as default
update_config('style', $style_name);
update_config('style_depend_on', isset($style_info['depend_on']) ? $style_info['depend_on'] : '');
//delete all cache to get new style
delete_cache('', true);
//show msg
kleeja_admin_info(sprintf($lang['STYLE_NOW_IS_DEFAULT'], $style_name), $action);
break;
case 'upload':
if (intval($userinfo['founder']) !== 1) {
$ERRORS[] = $lang['HV_NOT_PRVLG_ACCESS'];
}
$ERRORS = [];
//is uploaded?
if (empty($_FILES['style_file']['tmp_name'])) {
$ERRORS[] = $lang['CHOSE_F'];
}
//extract it to plugins folder
if (! sizeof($ERRORS)) {
if (class_exists('ZipArchive')) {
$zip = new ZipArchive;
if ($zip->open($_FILES['style_file']['tmp_name']) === true) {
if (! $zip->extractTo(PATH . 'styles')) {
$ERRORS[] = sprintf($lang['EXTRACT_ZIP_FAILED'], 'styles');
}
$zip->close();
} else {
$ERRORS[] = sprintf($lang['EXTRACT_ZIP_FAILED'], 'styles');
}
} else {
$ERRORS[] = $lang['NO_ZIP_ARCHIVE'];
}
}
if (! empty($_FILES['style_file']['tmp_name'])) {
@unlink($_FILES['style_file']['tmp_name']);
}
if (! sizeof($ERRORS)) {
kleeja_admin_info($lang['NO_PROBLEM_AFTER_ZIP'], true, '', true, $action);
} else {
kleeja_admin_err('- ' . implode('<br>- ', $ERRORS), $action);
}
break;
case 'dfolder':
$style_name = preg_replace('/[^a-z0-9_\-\.]/i', '', g('style'));
//can not delete default style
if ($config['style'] === $style_name) {
kleeja_admin_info($lang['CANT_DEL_DEFAULT_STYLE'], true, '', true, $action);
}
$style_folder_path = PATH . 'styles/' . $style_name;
if (file_exists($style_folder_path)) {
if (! is_writable($style_folder_path)) {
@chmod($style_folder_path, K_DIR_CHMOD);
}
kleeja_unlink($style_folder_path);
}
if (! file_exists($style_folder_path)) {
kleeja_admin_info(sprintf($lang['ITEM_DELETED'], $style_name), $action);
}
kleeja_admin_err($lang['ERROR_TRY_AGAIN'], $action);
break;
case 'download':
if (intval($userinfo['founder']) !== 1) {
header('HTTP/1.0 401 Unauthorized');
kleeja_admin_err($lang['HV_NOT_PRVLG_ACCESS']);
}
$style_name = g('style');
$is_update = false;
if (! is_writable(PATH . 'styles')) {
@chmod(PATH . 'styles', K_DIR_CHMOD);
}
//if style exists before, then trigger update action. rename folder to rollback in case of failure
if (file_exists(PATH . 'styles/' . $style_name)) {
$is_update = true;
if (! rename(
PATH . 'styles/' . $style_name,
PATH . 'styles/' . $style_name . '_backup'
)) {
if (file_exists(PATH . 'styles/' . $style_name)) {
kleeja_unlink(PATH . 'styles/' . $style_name);
}
}
}
// plugins avilable in kleeja store
$store_link = 'https://raw.githubusercontent.com/kleeja-official/store-catalog/master/catalog.json';
$catalog_styles = FetchFile::make($store_link)->get();
if ($catalog_styles) {
$catalog_styles = json_decode($catalog_styles, true);
$store_styles = [];
// make an arry for all plugins in kleeja store that not included in our server
foreach ($catalog_styles as $style_info) {
if ($style_info['type'] != 'style') {
continue;
}
$store_styles[$style_info['name']] = [
'name' => $style_info['name'] ,
'plg_version' => $style_info['file']['version'] ,
'url' => $style_info['file']['url'] ,
'kj_min_version' => $style_info['kleeja_version']['min'] ,
'kj_max_version' => $style_info['kleeja_version']['max'] ,
];
}
// this style is hosted in our store
if (isset($store_styles[$style_name])) {
// check if the version of the plugin is compatible with our kleeja version or not
if (
version_compare(strtolower($store_styles[$style_name]['kj_min_version']), KLEEJA_VERSION, '<=')
&& version_compare(strtolower($store_styles[$style_name]['kj_max_version']), KLEEJA_VERSION, '>=')
) {
$style_name_link = $store_styles[$style_name]['url'];
$style_archive = FetchFile::make($style_name_link)
->setDestinationPath(PATH . 'cache/' . $style_name . '.zip')
->isBinaryFile(true)
->get();
if ($style_archive) {
if (file_exists(PATH . 'cache/' . $style_name . '.zip')) {
$zip = new ZipArchive();
if ($zip->open(PATH . 'cache/' . $style_name . '.zip') === true) {
if ($zip->extractTo(PATH . 'styles')) {
// we dont need the zip file anymore
kleeja_unlink(PATH . 'cache/' . $style_name . '.zip');
// uploaded style's archive has different name, so we change it
rename(
PATH . 'styles/' . trim($zip->getNameIndex(0), '/'),
PATH . 'styles/' . $style_name
);
$zip->close();
// download or update msg
$adminAjaxContent = '1:::' . sprintf($lang[$is_update ? 'ITEM_UPDATED' : 'ITEM_DOWNLOADED'], $style_name);
//in case of update, delete back up version
if (file_exists(PATH . 'styles/' . $style_name . '_backup')) {
kleeja_unlink(PATH . 'styles/' . $style_name . '_backup');
}
} else {
$adminAjaxContent = '1003:::' . sprintf($lang['EXTRACT_ZIP_FAILED'], PATH . 'styles');
}
}
} else {
$adminAjaxContent = '1004:::' . $lang['DOWNLOADED_FILE_NOT_FOUND'];
}
} else {
$adminAjaxContent = '1005:::' . $lang['STORE_SERVER_ERROR'];
}
} else {
$adminAjaxContent = '1006:::' . $lang['PACKAGE_N_CMPT_KLJ'];
}
} else {
$adminAjaxContent = '1007:::' . sprintf($lang['PACKAGE_REMOTE_FILE_MISSING'], $style_name);
}
} else {
$adminAjaxContent = '1008:::' . $lang['STORE_SERVER_ERROR'];
}
//in case of update failure, rollback to current plugin version
if (strpos($adminAjaxContent, '1:::') === false) {
if (file_exists(PATH . 'styles/' . $style_name . '_backup')) {
rename(
PATH . 'styles/' . $style_name . '_backup',
PATH . 'styles/' . $style_name
);
}
}
$message = explode(':::', $adminAjaxContent)[1];
kleeja_admin_info(
$message,
true,
'',
true,
ADMIN_PATH . '?cp=' . basename(__FILE__, '.php')
);
exit;
break;
endswitch;