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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
|
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class acp_styles
{
public $u_action;
protected $u_base_action;
protected $s_hidden_fields;
protected $mode;
protected $styles_path;
protected $styles_path_absolute = 'styles';
protected $default_style = 0;
protected $styles_list_cols = 0;
protected $reserved_style_names = array('adm', 'admin', 'all');
/** @var \phpbb\config\config */
protected $config;
/** @var \phpbb\db\driver\driver_interface */
protected $db;
/** @var \phpbb\user */
protected $user;
/** @var \phpbb\template\template */
protected $template;
/** @var \phpbb\request\request_interface */
protected $request;
/** @var \phpbb\cache\driver\driver_interface */
protected $cache;
/** @var \phpbb\auth\auth */
protected $auth;
/** @var \phpbb\textformatter\cache_interface */
protected $text_formatter_cache;
/** @var string */
protected $phpbb_root_path;
/** @var string */
protected $php_ext;
/** @var \phpbb\event\dispatcher_interface */
protected $dispatcher;
public function main($id, $mode)
{
global $db, $user, $phpbb_admin_path, $phpbb_root_path, $phpEx, $template, $request, $cache, $auth, $config, $phpbb_dispatcher, $phpbb_container;
$this->db = $db;
$this->user = $user;
$this->template = $template;
$this->request = $request;
$this->cache = $cache;
$this->auth = $auth;
$this->text_formatter_cache = $phpbb_container->get('text_formatter.cache');
$this->config = $config;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $phpEx;
$this->dispatcher = $phpbb_dispatcher;
$this->default_style = $config['default_style'];
$this->styles_path = $this->phpbb_root_path . $this->styles_path_absolute . '/';
$this->u_base_action = append_sid("{$phpbb_admin_path}index.{$this->php_ext}", "i={$id}");
$this->s_hidden_fields = array(
'mode' => $mode,
);
$this->user->add_lang('acp/styles');
$this->tpl_name = 'acp_styles';
$this->page_title = 'ACP_CAT_STYLES';
$this->mode = $mode;
$action = $this->request->variable('action', '');
$post_actions = array('install', 'activate', 'deactivate', 'uninstall');
foreach ($post_actions as $key)
{
if ($this->request->is_set_post($key))
{
$action = $key;
}
}
// The uninstall action uses confirm_box() to verify the validity of the request,
// so there is no need to check for a valid token here.
if (in_array($action, $post_actions) && $action != 'uninstall')
{
$is_valid_request = check_link_hash($request->variable('hash', ''), $action) || check_form_key('styles_management');
if (!$is_valid_request)
{
trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
}
}
if ($action != '')
{
$this->s_hidden_fields['action'] = $action;
}
$this->template->assign_vars(array(
'U_ACTION' => $this->u_base_action,
'S_HIDDEN_FIELDS' => build_hidden_fields($this->s_hidden_fields)
)
);
/**
* Run code before ACP styles action execution
*
* @event core.acp_styles_action_before
* @var int id Module ID
* @var string mode Active module
* @var string action Module that should be run
* @since 3.1.7-RC1
*/
$vars = array('id', 'mode', 'action');
extract($this->dispatcher->trigger_event('core.acp_styles_action_before', compact($vars)));
// Execute actions
switch ($action)
{
case 'install':
$this->action_install();
return;
case 'uninstall':
$this->action_uninstall();
return;
case 'activate':
$this->action_activate();
return;
case 'deactivate':
$this->action_deactivate();
return;
case 'details':
$this->action_details();
return;
default:
$this->frontend();
}
}
/**
* Main page
*/
protected function frontend()
{
add_form_key('styles_management');
// Check mode
switch ($this->mode)
{
case 'style':
$this->welcome_message('ACP_STYLES', 'ACP_STYLES_EXPLAIN');
$this->show_installed();
return;
case 'install':
$this->welcome_message('INSTALL_STYLES', 'INSTALL_STYLES_EXPLAIN');
$this->show_available();
return;
}
trigger_error($this->user->lang['NO_MODE'] . adm_back_link($this->u_action), E_USER_WARNING);
}
/**
* Install style(s)
*/
protected function action_install()
{
// Get list of styles to install
$dirs = $this->request_vars('dir', '', true);
// Get list of styles that can be installed
$styles = $this->find_available(false);
// Install each style
$messages = array();
$installed_names = array();
$installed_dirs = array();
foreach ($dirs as $dir)
{
if (in_array($dir, $this->reserved_style_names))
{
$messages[] = $this->user->lang('STYLE_NAME_RESERVED', htmlspecialchars($dir));
continue;
}
$found = false;
foreach ($styles as &$style)
{
// Check if:
// 1. Directory matches directory we are looking for
// 2. Style is not installed yet
// 3. Style with same name or directory hasn't been installed already within this function
if ($style['style_path'] == $dir && empty($style['_installed']) && !in_array($style['style_path'], $installed_dirs) && !in_array($style['style_name'], $installed_names))
{
// Install style
$style['style_active'] = 1;
$style['style_id'] = $this->install_style($style);
$style['_installed'] = true;
$found = true;
$installed_names[] = $style['style_name'];
$installed_dirs[] = $style['style_path'];
$messages[] = sprintf($this->user->lang['STYLE_INSTALLED'], htmlspecialchars($style['style_name']));
}
}
if (!$found)
{
$messages[] = sprintf($this->user->lang['STYLE_NOT_INSTALLED'], htmlspecialchars($dir));
}
}
// Invalidate the text formatter's cache for the new styles to take effect
if (!empty($installed_names))
{
$this->text_formatter_cache->invalidate();
}
// Show message
if (!count($messages))
{
trigger_error($this->user->lang['NO_MATCHING_STYLES_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
}
$message = implode('<br />', $messages);
$message .= '<br /><br /><a href="' . $this->u_base_action . '&mode=style' . '">« ' . $this->user->lang('STYLE_INSTALLED_RETURN_INSTALLED_STYLES') . '</a>';
$message .= '<br /><br /><a href="' . $this->u_base_action . '&mode=install' . '">» ' . $this->user->lang('STYLE_INSTALLED_RETURN_UNINSTALLED_STYLES') . '</a>';
trigger_error($message, E_USER_NOTICE);
}
/**
* Confirm styles removal
*/
protected function action_uninstall()
{
// Get list of styles to uninstall
$ids = $this->request_vars('id', 0, true);
// Check if confirmation box was submitted
if (confirm_box(true))
{
// Uninstall
$this->action_uninstall_confirmed($ids, $this->request->variable('confirm_delete_files', false));
return;
}
// Confirm box
$s_hidden = build_hidden_fields(array(
'action' => 'uninstall',
'ids' => $ids
));
$this->template->assign_var('S_CONFIRM_DELETE', true);
confirm_box(false, $this->user->lang['CONFIRM_UNINSTALL_STYLES'], $s_hidden, 'acp_styles.html');
// Canceled - show styles list
$this->frontend();
}
/**
* Uninstall styles(s)
*
* @param array $ids List of style IDs
* @param bool $delete_files If true, script will attempt to remove files for selected styles
*/
protected function action_uninstall_confirmed($ids, $delete_files)
{
global $user, $phpbb_log;
$default = $this->default_style;
$uninstalled = array();
$messages = array();
// Check styles list
foreach ($ids as $id)
{
if (!$id)
{
trigger_error($this->user->lang['INVALID_STYLE_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
}
if ($id == $default)
{
trigger_error($this->user->lang['UNINSTALL_DEFAULT'] . adm_back_link($this->u_action), E_USER_WARNING);
}
$uninstalled[$id] = false;
}
// Order by reversed style_id, so parent styles would be removed after child styles
// This way parent and child styles can be removed in same function call
$sql = 'SELECT *
FROM ' . STYLES_TABLE . '
WHERE style_id IN (' . implode(', ', $ids) . ')
ORDER BY style_id DESC';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
// Uinstall each style
$uninstalled = array();
foreach ($rows as $style)
{
$result = $this->uninstall_style($style, $delete_files);
if (is_string($result))
{
$messages[] = $result;
continue;
}
$messages[] = sprintf($this->user->lang['STYLE_UNINSTALLED'], $style['style_name']);
$uninstalled[] = $style['style_name'];
// Attempt to delete files
if ($delete_files)
{
$messages[] = sprintf($this->user->lang[$this->delete_style_files($style['style_path']) ? 'DELETE_STYLE_FILES_SUCCESS' : 'DELETE_STYLE_FILES_FAILED'], $style['style_name']);
}
}
if (empty($messages))
{
// Nothing to uninstall?
trigger_error($this->user->lang['NO_MATCHING_STYLES_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
}
// Log action
if (count($uninstalled))
{
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_STYLE_DELETE', false, array(implode(', ', $uninstalled)));
}
// Clear cache
$this->cache->purge();
// Show message
trigger_error(implode('<br />', $messages) . adm_back_link($this->u_action), E_USER_NOTICE);
}
/**
* Activate styles
*/
protected function action_activate()
{
// Get list of styles to activate
$ids = $this->request_vars('id', 0, true);
// Activate styles
$sql = 'UPDATE ' . STYLES_TABLE . '
SET style_active = 1
WHERE style_id IN (' . implode(', ', $ids) . ')';
$this->db->sql_query($sql);
// Purge cache
$this->cache->destroy('sql', STYLES_TABLE);
// Show styles list
$this->frontend();
}
/**
* Deactivate styles
*/
protected function action_deactivate()
{
// Get list of styles to deactivate
$ids = $this->request_vars('id', 0, true);
// Check for default style
foreach ($ids as $id)
{
if ($id == $this->default_style)
{
trigger_error($this->user->lang['DEACTIVATE_DEFAULT'] . adm_back_link($this->u_action), E_USER_WARNING);
}
}
// Reset default style for users who use selected styles
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_style = ' . (int) $this->default_style . '
WHERE user_style IN (' . implode(', ', $ids) . ')';
$this->db->sql_query($sql);
// Deactivate styles
$sql = 'UPDATE ' . STYLES_TABLE . '
SET style_active = 0
WHERE style_id IN (' . implode(', ', $ids) . ')';
$this->db->sql_query($sql);
// Purge cache
$this->cache->destroy('sql', STYLES_TABLE);
// Show styles list
$this->frontend();
}
/**
* Show style details
*/
protected function action_details()
{
global $user, $phpbb_log;
$id = $this->request->variable('id', 0);
if (!$id)
{
trigger_error($this->user->lang['NO_MATCHING_STYLES_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
}
// Get all styles
$styles = $this->get_styles();
usort($styles, array($this, 'sort_styles'));
// Find current style
$style = false;
foreach ($styles as $row)
{
if ($row['style_id'] == $id)
{
$style = $row;
break;
}
}
if ($style === false)
{
trigger_error($this->user->lang['NO_MATCHING_STYLES_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
}
// Read style configuration file
$style_cfg = $this->read_style_cfg($style['style_path']);
// Find all available parent styles
$list = $this->find_possible_parents($styles, $id);
// Add form key
$form_key = 'acp_styles';
add_form_key($form_key);
// Change data
if ($this->request->variable('update', false))
{
if (!check_form_key($form_key))
{
trigger_error($this->user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
}
$update = array(
'style_name' => trim($this->request->variable('style_name', $style['style_name'])),
'style_parent_id' => $this->request->variable('style_parent', (int) $style['style_parent_id']),
'style_active' => $this->request->variable('style_active', (int) $style['style_active']),
);
$update_action = $this->u_action . '&action=details&id=' . $id;
// Check style name
if ($update['style_name'] != $style['style_name'])
{
if (!strlen($update['style_name']))
{
trigger_error($this->user->lang['STYLE_ERR_STYLE_NAME'] . adm_back_link($update_action), E_USER_WARNING);
}
foreach ($styles as $row)
{
if ($row['style_name'] == $update['style_name'])
{
trigger_error($this->user->lang['STYLE_ERR_NAME_EXIST'] . adm_back_link($update_action), E_USER_WARNING);
}
}
}
else
{
unset($update['style_name']);
}
// Check parent style id
if ($update['style_parent_id'] != $style['style_parent_id'])
{
if ($update['style_parent_id'] != 0)
{
$found = false;
foreach ($list as $row)
{
if ($row['style_id'] == $update['style_parent_id'])
{
$found = true;
$update['style_parent_tree'] = ($row['style_parent_tree'] != '' ? $row['style_parent_tree'] . '/' : '') . $row['style_path'];
break;
}
}
if (!$found)
{
trigger_error($this->user->lang['STYLE_ERR_INVALID_PARENT'] . adm_back_link($update_action), E_USER_WARNING);
}
}
else
{
$update['style_parent_tree'] = '';
}
}
else
{
unset($update['style_parent_id']);
}
// Check style_active
if ($update['style_active'] != $style['style_active'])
{
if (!$update['style_active'] && $this->default_style == $style['style_id'])
{
trigger_error($this->user->lang['DEACTIVATE_DEFAULT'] . adm_back_link($update_action), E_USER_WARNING);
}
}
else
{
unset($update['style_active']);
}
// Update data
if (count($update))
{
$sql = 'UPDATE ' . STYLES_TABLE . '
SET ' . $this->db->sql_build_array('UPDATE', $update) . "
WHERE style_id = $id";
$this->db->sql_query($sql);
$style = array_merge($style, $update);
if (isset($update['style_parent_id']))
{
// Update styles tree
$styles = $this->get_styles();
if ($this->update_styles_tree($styles, $style))
{
// Something was changed in styles tree, purge all cache
$this->cache->purge();
}
}
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_STYLE_EDIT_DETAILS', false, array($style['style_name']));
}
// Update default style
$default = $this->request->variable('style_default', 0);
if ($default)
{
if (!$style['style_active'])
{
trigger_error($this->user->lang['STYLE_DEFAULT_CHANGE_INACTIVE'] . adm_back_link($update_action), E_USER_WARNING);
}
$this->config->set('default_style', $id);
$this->cache->purge();
}
// Show styles list
$this->frontend();
return;
}
// Show page title
$this->welcome_message('ACP_STYLES', null);
// Show parent styles
foreach ($list as $row)
{
$this->template->assign_block_vars('parent_styles', array(
'STYLE_ID' => $row['style_id'],
'STYLE_NAME' => htmlspecialchars($row['style_name']),
'LEVEL' => $row['level'],
'SPACER' => str_repeat(' ', $row['level']),
)
);
}
// Show style details
$this->template->assign_vars(array(
'S_STYLE_DETAILS' => true,
'STYLE_ID' => $style['style_id'],
'STYLE_NAME' => htmlspecialchars($style['style_name']),
'STYLE_PATH' => htmlspecialchars($style['style_path']),
'STYLE_VERSION' => htmlspecialchars($style_cfg['style_version']),
'STYLE_COPYRIGHT' => strip_tags($style['style_copyright']),
'STYLE_PARENT' => $style['style_parent_id'],
'S_STYLE_ACTIVE' => $style['style_active'],
'S_STYLE_DEFAULT' => ($style['style_id'] == $this->default_style)
)
);
}
/**
* List installed styles
*/
protected function show_installed()
{
// Get all installed styles
$styles = $this->get_styles();
if (!count($styles))
{
trigger_error($this->user->lang['NO_MATCHING_STYLES_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
}
usort($styles, array($this, 'sort_styles'));
// Get users
$users = $this->get_users();
// Add users counter to rows
foreach ($styles as &$style)
{
$style['_users'] = isset($users[$style['style_id']]) ? $users[$style['style_id']] : 0;
}
// Set up styles list variables
// Addons should increase this number and update template variable
$this->styles_list_cols = 4;
$this->template->assign_var('STYLES_LIST_COLS', $this->styles_list_cols);
// Show styles list
$this->show_styles_list($styles, 0, 0);
// Show styles with invalid inherits_id
foreach ($styles as $style)
{
if (empty($style['_shown']))
{
$style['_note'] = sprintf($this->user->lang['REQUIRES_STYLE'], htmlspecialchars($style['style_parent_tree']));
$this->list_style($style, 0);
}
}
// Add buttons
$this->template->assign_block_vars('extra_actions', array(
'ACTION_NAME' => 'activate',
'L_ACTION' => $this->user->lang['STYLE_ACTIVATE'],
)
);
$this->template->assign_block_vars('extra_actions', array(
'ACTION_NAME' => 'deactivate',
'L_ACTION' => $this->user->lang['STYLE_DEACTIVATE'],
)
);
if (isset($this->style_counters) && $this->style_counters['total'] > 1)
{
$this->template->assign_block_vars('extra_actions', array(
'ACTION_NAME' => 'uninstall',
'L_ACTION' => $this->user->lang['STYLE_UNINSTALL'],
)
);
}
}
/**
* Show list of styles that can be installed
*/
protected function show_available()
{
// Get list of styles
$styles = $this->find_available(true);
// Show styles
if (empty($styles))
{
trigger_error($this->user->lang['NO_UNINSTALLED_STYLE'] . adm_back_link($this->u_base_action), E_USER_NOTICE);
}
usort($styles, array($this, 'sort_styles'));
$this->styles_list_cols = 3;
$this->template->assign_vars(array(
'STYLES_LIST_COLS' => $this->styles_list_cols,
'STYLES_LIST_HIDE_COUNT' => true
)
);
// Show styles
foreach ($styles as &$style)
{
// Check if style has a parent style in styles list
$has_parent = false;
if ($style['_inherit_name'] != '')
{
foreach ($styles as $parent_style)
{
if ($parent_style['style_name'] == $style['_inherit_name'] && empty($parent_style['_shown']))
{
// Show parent style first
$has_parent = true;
}
}
}
if (!$has_parent)
{
$this->list_style($style, 0);
$this->show_available_child_styles($styles, $style['style_name'], 1);
}
}
// Show styles that do not have parent style in styles list
foreach ($styles as $style)
{
if (empty($style['_shown']))
{
$this->list_style($style, 0);
}
}
// Add button
if (isset($this->style_counters) && $this->style_counters['caninstall'] > 0)
{
$this->template->assign_block_vars('extra_actions', array(
'ACTION_NAME' => 'install',
'L_ACTION' => $this->user->lang['INSTALL_STYLES'],
)
);
}
}
/**
* Find styles available for installation
*
* @param bool $all if true, function will return all installable styles. if false, function will return only styles that can be installed
* @return array List of styles
*/
protected function find_available($all)
{
// Get list of installed styles
$installed = $this->get_styles();
$installed_dirs = array();
$installed_names = array();
foreach ($installed as $style)
{
$installed_dirs[] = $style['style_path'];
$installed_names[$style['style_name']] = array(
'path' => $style['style_path'],
'id' => $style['style_id'],
'parent' => $style['style_parent_id'],
'tree' => (strlen($style['style_parent_tree']) ? $style['style_parent_tree'] . '/' : '') . $style['style_path'],
);
}
// Get list of directories
$dirs = $this->find_style_dirs();
// Find styles that can be installed
$styles = array();
foreach ($dirs as $dir)
{
if (in_array($dir, $installed_dirs))
{
// Style is already installed
continue;
}
$cfg = $this->read_style_cfg($dir);
if ($cfg === false)
{
// Invalid style.cfg
continue;
}
// Style should be available for installation
$parent = $cfg['parent'];
$style = array(
'style_id' => 0,
'style_name' => $cfg['name'],
'style_copyright' => $cfg['copyright'],
'style_active' => 0,
'style_path' => $dir,
'bbcode_bitfield' => $cfg['template_bitfield'],
'style_parent_id' => 0,
'style_parent_tree' => '',
// Extra values for styles list
// All extra variable start with _ so they won't be confused with data that can be added to styles table
'_inherit_name' => $parent,
'_available' => true,
'_note' => '',
);
// Check style inheritance
if ($parent != '')
{
if (isset($installed_names[$parent]))
{
// Parent style is installed
$row = $installed_names[$parent];
$style['style_parent_id'] = $row['id'];
$style['style_parent_tree'] = $row['tree'];
}
else
{
// Parent style is not installed yet
$style['_available'] = false;
$style['_note'] = sprintf($this->user->lang['REQUIRES_STYLE'], htmlspecialchars($parent));
}
}
if ($all || $style['_available'])
{
$styles[] = $style;
}
}
return $styles;
}
/**
* Show styles list
*
* @param array $styles styles list
* @param int $parent parent style id
* @param int $level style inheritance level
*/
protected function show_styles_list(&$styles, $parent, $level)
{
foreach ($styles as &$style)
{
if (empty($style['_shown']) && $style['style_parent_id'] == $parent)
{
$this->list_style($style, $level);
$this->show_styles_list($styles, $style['style_id'], $level + 1);
}
}
}
/**
* Show available styles tree
*
* @param array $styles Styles list, passed as reference
* @param string $name Name of parent style
* @param int $level Styles tree level
*/
protected function show_available_child_styles(&$styles, $name, $level)
{
foreach ($styles as &$style)
{
if (empty($style['_shown']) && $style['_inherit_name'] == $name)
{
$this->list_style($style, $level);
$this->show_available_child_styles($styles, $style['style_name'], $level + 1);
}
}
}
/**
* Update styles tree
*
* @param array $styles Styles list, passed as reference
* @param array|false $style Current style, false if root
* @return bool True if something was updated, false if not
*/
protected function update_styles_tree(&$styles, $style = false)
{
$parent_id = ($style === false) ? 0 : $style['style_id'];
$parent_tree = ($style === false) ? '' : ($style['style_parent_tree'] == '' ? '' : $style['style_parent_tree']) . $style['style_path'];
$update = false;
$updated = false;
foreach ($styles as &$row)
{
if ($row['style_parent_id'] == $parent_id)
{
if ($row['style_parent_tree'] != $parent_tree)
{
$row['style_parent_tree'] = $parent_tree;
$update = true;
}
$updated |= $this->update_styles_tree($styles, $row);
}
}
if ($update)
{
$sql = 'UPDATE ' . STYLES_TABLE . "
SET style_parent_tree = '" . $this->db->sql_escape($parent_tree) . "'
WHERE style_parent_id = {$parent_id}";
$this->db->sql_query($sql);
$updated = true;
}
return $updated;
}
/**
* Find all possible parent styles for style
*
* @param array $styles list of styles
* @param int $id id of style
* @param int $parent current parent style id
* @param int $level current tree level
* @return array Style ids, names and levels
*/
protected function find_possible_parents($styles, $id = -1, $parent = 0, $level = 0)
{
$results = array();
foreach ($styles as $style)
{
if ($style['style_id'] != $id && $style['style_parent_id'] == $parent)
{
$results[] = array(
'style_id' => $style['style_id'],
'style_name' => $style['style_name'],
'style_path' => $style['style_path'],
'style_parent_id' => $style['style_parent_id'],
'style_parent_tree' => $style['style_parent_tree'],
'level' => $level
);
$results = array_merge($results, $this->find_possible_parents($styles, $id, $style['style_id'], $level + 1));
}
}
return $results;
}
/**
* Show item in styles list
*
* @param array $style style row
* @param int $level style inheritance level
*/
protected function list_style(&$style, $level)
{
// Mark row as shown
if (!empty($style['_shown']))
{
return;
}
$style['_shown'] = true;
// Generate template variables
$actions = array();
$row = array(
// Style data
'STYLE_ID' => $style['style_id'],
'STYLE_NAME' => htmlspecialchars($style['style_name']),
'STYLE_PHPBB_VERSION' => $this->read_style_cfg($style['style_path'])['phpbb_version'],
'STYLE_PATH' => htmlspecialchars($style['style_path']),
'STYLE_COPYRIGHT' => strip_tags($style['style_copyright']),
'STYLE_ACTIVE' => $style['style_active'],
// Additional data
'DEFAULT' => ($style['style_id'] && $style['style_id'] == $this->default_style),
'USERS' => (isset($style['_users'])) ? $style['_users'] : '',
'LEVEL' => $level,
'PADDING' => (4 + 16 * $level),
'SHOW_COPYRIGHT' => ($style['style_id']) ? false : true,
'STYLE_PATH_FULL' => htmlspecialchars($this->styles_path_absolute . '/' . $style['style_path']) . '/',
// Comment to show below style
'COMMENT' => (isset($style['_note'])) ? $style['_note'] : '',
// The following variables should be used by hooks to add custom HTML code
'EXTRA' => '',
'EXTRA_OPTIONS' => ''
);
// Status specific data
if ($style['style_id'])
{
// Style is installed
// Details
$actions[] = array(
'U_ACTION' => $this->u_action . '&action=details&id=' . $style['style_id'],
'L_ACTION' => $this->user->lang['DETAILS']
);
// Activate/Deactive
$action_name = ($style['style_active'] ? 'de' : '') . 'activate';
$actions[] = array(
'U_ACTION' => $this->u_action . '&action=' . $action_name . '&hash=' . generate_link_hash($action_name) . '&id=' . $style['style_id'],
'L_ACTION' => $this->user->lang['STYLE_' . ($style['style_active'] ? 'DE' : '') . 'ACTIVATE']
);
/* // Export
$actions[] = array(
'U_ACTION' => $this->u_action . '&action=export&hash=' . generate_link_hash('export') . '&id=' . $style['style_id'],
'L_ACTION' => $this->user->lang['EXPORT']
); */
// Uninstall
$actions[] = array(
'U_ACTION' => $this->u_action . '&action=uninstall&hash=' . generate_link_hash('uninstall') . '&id=' . $style['style_id'],
'L_ACTION' => $this->user->lang['STYLE_UNINSTALL']
);
// Preview
$actions[] = array(
'U_ACTION' => append_sid($this->phpbb_root_path . 'index.' . $this->php_ext, 'style=' . $style['style_id']),
'L_ACTION' => $this->user->lang['PREVIEW']
);
}
else
{
// Style is not installed
if (empty($style['_available']))
{
$actions[] = array(
'HTML' => $this->user->lang['CANNOT_BE_INSTALLED']
);
}
else
{
$actions[] = array(
'U_ACTION' => $this->u_action . '&action=install&hash=' . generate_link_hash('install') . '&dir=' . urlencode($style['style_path']),
'L_ACTION' => $this->user->lang['INSTALL_STYLE']
);
}
}
// todo: add hook
// Assign template variables
$this->template->assign_block_vars('styles_list', $row);
foreach ($actions as $action)
{
$this->template->assign_block_vars('styles_list.actions', $action);
}
// Increase counters
$counter = ($style['style_id']) ? ($style['style_active'] ? 'active' : 'inactive') : (empty($style['_available']) ? 'cannotinstall' : 'caninstall');
if (!isset($this->style_counters))
{
$this->style_counters = array(
'total' => 0,
'active' => 0,
'inactive' => 0,
'caninstall' => 0,
'cannotinstall' => 0
);
}
$this->style_counters[$counter]++;
$this->style_counters['total']++;
}
/**
* Show welcome message
*
* @param string $title main title
* @param string $description page description
*/
protected function welcome_message($title, $description)
{
$this->template->assign_vars(array(
'L_TITLE' => $this->user->lang[$title],
'L_EXPLAIN' => (isset($this->user->lang[$description])) ? $this->user->lang[$description] : ''
)
);
}
/**
* Find all directories that have styles
*
* @return array Directory names
*/
protected function find_style_dirs()
{
$styles = array();
$dp = @opendir($this->styles_path);
if ($dp)
{
while (($file = readdir($dp)) !== false)
{
$dir = $this->styles_path . $file;
if ($file[0] == '.' || !is_dir($dir))
{
continue;
}
if (file_exists("{$dir}/style.cfg"))
{
$styles[] = $file;
}
}
closedir($dp);
}
return $styles;
}
/**
* Sort styles
*/
public function sort_styles($style1, $style2)
{
if ($style1['style_active'] != $style2['style_active'])
{
return ($style1['style_active']) ? -1 : 1;
}
if (isset($style1['_available']) && $style1['_available'] != $style2['_available'])
{
return ($style1['_available']) ? -1 : 1;
}
return strcasecmp(isset($style1['style_name']) ? $style1['style_name'] : $style1['name'], isset($style2['style_name']) ? $style2['style_name'] : $style2['name']);
}
/**
* Read style configuration file
*
* @param string $dir style directory
* @return array|bool Style data, false on error
*/
protected function read_style_cfg($dir)
{
static $required = array('name', 'phpbb_version', 'copyright');
$cfg = parse_cfg_file($this->styles_path . $dir . '/style.cfg');
// Check if it is a valid file
foreach ($required as $key)
{
if (!isset($cfg[$key]))
{
return false;
}
}
// Check data
if (!isset($cfg['parent']) || !is_string($cfg['parent']) || $cfg['parent'] == $cfg['name'])
{
$cfg['parent'] = '';
}
if (!isset($cfg['template_bitfield']))
{
$cfg['template_bitfield'] = $this->default_bitfield();
}
return $cfg;
}
/**
* Install style
*
* @param array $style style data
* @return int Style id
*/
protected function install_style($style)
{
global $user, $phpbb_log;
// Generate row
$sql_ary = array();
foreach ($style as $key => $value)
{
if ($key != 'style_id' && substr($key, 0, 1) != '_')
{
$sql_ary[$key] = $value;
}
}
// Add to database
$this->db->sql_transaction('begin');
$sql = 'INSERT INTO ' . STYLES_TABLE . '
' . $this->db->sql_build_array('INSERT', $sql_ary);
$this->db->sql_query($sql);
$id = $this->db->sql_nextid();
$this->db->sql_transaction('commit');
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_STYLE_ADD', false, array($sql_ary['style_name']));
return $id;
}
/**
* Lists all styles
*
* @return array Rows with styles data
*/
protected function get_styles()
{
$sql = 'SELECT *
FROM ' . STYLES_TABLE;
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
return $rows;
}
/**
* Count users for each style
*
* @return array Styles in following format: [style_id] = number of users
*/
protected function get_users()
{
$sql = 'SELECT user_style, COUNT(user_style) AS style_count
FROM ' . USERS_TABLE . '
GROUP BY user_style';
$result = $this->db->sql_query($sql);
$style_count = array();
while ($row = $this->db->sql_fetchrow($result))
{
$style_count[$row['user_style']] = $row['style_count'];
}
$this->db->sql_freeresult($result);
return $style_count;
}
/**
* Uninstall style
*
* @param array $style Style data
* @return bool|string True on success, error message on error
*/
protected function uninstall_style($style)
{
$id = $style['style_id'];
$path = $style['style_path'];
// Check if style has child styles
$sql = 'SELECT style_id
FROM ' . STYLES_TABLE . '
WHERE style_parent_id = ' . (int) $id . " OR style_parent_tree = '" . $this->db->sql_escape($path) . "'";
$result = $this->db->sql_query($sql);
$conflict = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);
if ($conflict !== false)
{
return sprintf($this->user->lang['STYLE_UNINSTALL_DEPENDENT'], $style['style_name']);
}
// Change default style for users
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_style = ' . (int) $this->default_style . '
WHERE user_style = ' . $id;
$this->db->sql_query($sql);
// Uninstall style
$sql = 'DELETE FROM ' . STYLES_TABLE . '
WHERE style_id = ' . $id;
$this->db->sql_query($sql);
return true;
}
/**
* Delete all files in style directory
*
* @param string $path Style directory
* @param string $dir Directory to remove inside style's directory
* @return bool True on success, false on error
*/
protected function delete_style_files($path, $dir = '')
{
$dirname = $this->styles_path . $path . $dir;
$result = true;
$dp = @opendir($dirname);
if ($dp)
{
while (($file = readdir($dp)) !== false)
{
if ($file == '.' || $file == '..')
{
continue;
}
$filename = $dirname . '/' . $file;
if (is_dir($filename))
{
if (!$this->delete_style_files($path, $dir . '/' . $file))
{
$result = false;
}
}
else
{
if (!@unlink($filename))
{
$result = false;
}
}
}
closedir($dp);
}
if (!@rmdir($dirname))
{
return false;
}
return $result;
}
/**
* Get list of items from posted data
*
* @param string $name Variable name
* @param string|int $default Default value for array
* @param bool $error If true, error will be triggered if list is empty
* @return array Items
*/
protected function request_vars($name, $default, $error = false)
{
$item = $this->request->variable($name, $default);
$items = $this->request->variable($name . 's', array($default));
if (count($items) == 1 && $items[0] == $default)
{
$items = array();
}
if ($item != $default && !count($items))
{
$items[] = $item;
}
if ($error && !count($items))
{
trigger_error($this->user->lang['NO_MATCHING_STYLES_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
}
return $items;
}
/**
* Generates default bitfield
*
* This bitfield decides which bbcodes are defined in a template.
*
* @return string Bitfield
*/
protected function default_bitfield()
{
static $value;
if (isset($value))
{
return $value;
}
// Hardcoded template bitfield to add for new templates
$default_bitfield = '1111111111111';
$bitfield = new bitfield();
for ($i = 0; $i < strlen($default_bitfield); $i++)
{
if ($default_bitfield[$i] == '1')
{
$bitfield->set($i);
}
}
return $bitfield->get_base64();
}
}
|