1<?php
2/**
3 * @package tikiwiki
4 */
5// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
6//
7// All Rights Reserved. See copyright.txt for details and a complete list of authors.
8// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9// $Id$
10
11$inputConfiguration = [
12				[
13					'staticKeyFilters' => [
14							'save' => 'alpha',
15							'load' => 'alpha',
16							'pref' => 'striptags',
17							'section' => 'striptags',
18					],
19				]
20];
21
22$auto_query_args = ['section', 'comments', 'autoreload', 'view_mode'];
23
24require_once 'tiki-setup.php';
25require_once 'lib/toolbars/toolbarslib.php';
26
27$access->check_permission('tiki_p_admin');
28$access->check_feature(['javascript_enabled', 'feature_jquery_ui']);
29
30$sections = [ 'global' => tra('Global'), 'admin' => tra('Admin')];
31$sections2 = [];
32
33if ($prefs['feature_wiki'] == 'y') {
34	$sections2['wiki page'] = tra('Wiki Pages');
35}
36if ($prefs['feature_trackers'] == 'y') {
37	$sections2['trackers'] = tra('Trackers');
38}
39if ($prefs['feature_blogs'] == 'y') {
40	$sections2['blogs'] = tra('Blogs');
41}
42if ($prefs['feature_calendar'] == 'y') {
43	$sections2['calendar'] = tra('Calendars');
44}
45if ($prefs['feature_articles'] == 'y') {
46	$sections2['cms'] = tra('Articles');
47}
48if ($prefs['feature_faqs'] == 'y') {
49	$sections2['faqs'] = tra('FAQs');
50}
51if ($prefs['feature_newsletters'] == 'y') {
52	$sections2['newsletters'] = tra('Newsletters');
53}
54if ($prefs['feature_forums'] == 'y') {
55	$sections2['forums'] = tra('Forums');
56}
57if ($prefs['feature_sheet'] == 'y') {
58	$sections2['sheet'] = tra('Spreadsheets');
59}
60if ($prefs['wikiplugin_wysiwyg'] == 'y') {
61	$sections2['wysiwyg_plugin'] = tra('WYSIWYG Plugin');
62}
63
64asort($sections2);
65$sections = array_merge($sections, $sections2);
66
67
68if (isset($_REQUEST['section']) && in_array($_REQUEST['section'], array_keys($sections))) {
69	$section = $_REQUEST['section'];
70} else {
71	$keys = array_keys($sections);
72	$section = reset($keys);
73}
74if (isset($_REQUEST['comments']) && $_REQUEST['comments'] == 'on') {
75	$comments = true;
76} else {
77	$comments = false;
78}
79
80foreach ($sections as $skey => $sval) {
81	if (isset($prefs['toolbar_' . $skey . ($comments ? '_comments' : '') . 'modified'])
82		&& $prefs['toolbar_' . $skey . ($comments ? '_comments' : '') . 'modified'] == 'y') {
83		$sections[$skey] = $sval . ' *';
84	}
85}
86
87$view_mode = isset($_REQUEST['view_mode']) ? $_REQUEST['view_mode'] : '';
88if ($view_mode === 'sheet' && $section !== 'sheet') {
89	$view_mode = '';
90	$_REQUEST['view_mode'] = '';
91}
92$smarty->assign('view_mode', $view_mode);
93
94if (! empty($_REQUEST['reset_all_custom_tools'])) {
95	$access->check_authenticity(tra('Are you sure you want to delete all your custom tools?'));
96	Toolbar::deleteAllCustomTools();
97	$access->redirect('tiki-admin_toolbars.php');
98}
99
100if (isset($_REQUEST['save'], $_REQUEST['pref'])) {
101	$prefName = 'toolbar_' . $section . ($comments ? '_comments' : '');
102	$tikilib->set_preference($prefName, $_REQUEST['pref']);
103	$tikilib->set_preference($prefName . 'modified', 'y');
104}
105
106if ((isset($_REQUEST['reset']) && $section != 'global') || (isset($_REQUEST['reset_global']) && $section == 'global')) {
107	$prefName = 'toolbar_' . $section . ($comments ? '_comments' : '');
108	$tikilib->delete_preference($prefName);
109	$tikilib->set_preference($prefName . 'modified', 'n');
110	$smarty->loadPlugin('smarty_function_query');
111	header('location: ?' . smarty_function_query(['_urlencode' => 'n'], $smarty->getEmptyInternalTemplate()));
112}
113
114if (! empty($_REQUEST['save_tool']) && ! empty($_REQUEST['tool_name'])) {	// input from the tool edit form
115	Toolbar::saveTool(
116		$_REQUEST['tool_name'],
117		$_REQUEST['tool_label'],
118		$_REQUEST['tool_icon'],
119		$_REQUEST['tool_token'],
120		$_REQUEST['tool_syntax'],
121		$_REQUEST['tool_type'],
122		$_REQUEST['tool_plugin']
123	);
124
125	$smarty->loadPlugin('smarty_function_query');
126	header('location: ?' . smarty_function_query(['_urlencode' => 'n'], $smarty->getEmptyInternalTemplate()));
127}
128
129$current = $tikilib->get_preference('toolbar_' . $section . ($comments ? '_comments' : ''));
130if (empty($current)) {
131	$current = $tikilib->get_preference('toolbar_global' . ($comments ? '_comments' : ''));
132	$smarty->assign('not_global', false);
133} else {
134	$smarty->assign('not_global', true);
135}
136$smarty->assign('not_default', false);
137if ($section == 'global') {
138	$cachelib = TikiLib::lib('cache');
139	if ($defprefs = $cachelib->getSerialized("tiki_default_preferences_cache")) {
140		if ($defprefs['toolbar_global' . ($comments ? '_comments' : '')] != $current) {
141			$smarty->assign('not_default', true);
142		}
143	}
144}
145
146if (! empty($_REQUEST['delete_tool']) && ! empty($_REQUEST['tool_name'])) {	// input from the tool edit form
147	Toolbar::deleteTool($_REQUEST['tool_name']);
148	if (strpos($_REQUEST['tool_name'], $current) !== false) {
149		$current = str_replace($_REQUEST['tool_name'], '', $current);
150		$current = str_replace(',,', ',', $current);
151		$prefName = 'toolbar_' . $section . ($comments ? '_comments' : '');
152		$tikilib->set_preference($prefName, $current);
153	}
154}
155
156if (! empty($current)) {
157	$current = preg_replace('/\s+/', '', $current);
158	$current = trim($current, '/');
159	$current = explode('/', $current);
160	$loadedRows = count($current);
161	foreach ($current as &$line) {
162		$line = str_replace('|', ',|,', $line);
163		$line = [explode(',', $line)];
164	}
165	$rowCount = max($loadedRows, 1) + 1;
166} else {
167	$rowCount = 1;
168}
169
170$init = '';
171$setup = '';
172$map = [];
173
174$qtlist = Toolbar::getList();
175$usedqt = [];
176$qt_p_list = [];
177$qt_w_list = [];
178
179foreach ($current as &$line) {
180	foreach ($line as $bit) {
181		$usedqt = array_merge($usedqt, $bit);
182	}
183}
184
185$customqt = Toolbar::getCustomList();
186
187$view_mode = ! empty($_REQUEST['view_mode']) ? $_REQUEST['view_mode'] : '';
188
189foreach ($qtlist as $name) {
190	$tag = Toolbar::getTag($name);
191	if (! $tag) {
192		$tag = Toolbar::getTag($name, true);
193		if (! $tag) {
194			$tag = Toolbar::getTag($name, true, true);
195			continue;
196		}
197	}
198
199	$wys = strlen($tag->getWysiwygToken('dummy', false)) ? 'qt-wys' : '';
200	$wyswik = strlen($tag->getWysiwygWikiToken('dummy', false)) ? 'qt-wyswik' : '';
201	$test_html = $tag->getWikiHtml('');
202	$wiki = strlen($test_html) > 0 ? 'qt-wiki' : '';
203	$wiki = strpos($test_html, 'qt-sheet') !== false ? 'qt-sheet' : $wiki;
204	$cust = Toolbar::isCustomTool($name) ? 'qt-custom' : '';
205	$avail = $tag->isAccessible() ? '' : 'qt-noaccess';
206	$icon = $tag->getIconHtml();
207	$margins = $test_html === '||' ? 'mx-auto' : '';
208
209	if (strpos($name, 'wikiplugin_') !== false) {
210		$plug = 'qt-plugin';
211		$label = substr($name, 11);
212		$qt_p_list[] = $name;
213	} else {
214		$plug = '';
215		$label = $name;
216		if (empty($cust)) {
217			$qt_w_list[] = $name;
218		}
219	}
220
221	$label = htmlspecialchars($label);
222	$label .= '<input type="hidden" name="token" value="' . $tag->getWysiwygToken('dummy', false) . '" />';
223	$label .= '<input type="hidden" name="syntax" value="' . htmlspecialchars($tag->getSyntax('dummy')) . '" />';
224	$label .= '<input type="hidden" name="type" value="' . $tag->getType() . '" />';
225
226	if ($tag->getType() == 'Wikiplugin') {
227		$label .= '<input type="hidden" name="plugin" value="' . $tag->getPluginName() . '" />';
228	}
229
230	$visible = true;
231	if ($view_mode === 'both') {
232		$visible = (! empty($wys) || ! empty($wiki));
233	} elseif ($view_mode === 'wiki') {
234		$visible = ! empty($wiki);
235	} elseif ($view_mode === 'wysiwyg') {
236		$visible = ! empty($wys);
237	} elseif ($view_mode === 'wysiwyg_wiki') {
238		$visible = ! empty($wyswik);
239	} elseif ($view_mode === 'sheet') {
240		$visible = (strpos($wiki, 'qt-sheet') !== false);
241	}
242
243	$qtelement[$name] = [
244					'name' => $name,
245					'class' => "toolbar qt-$name $wys $wiki $wyswik $plug $cust $avail $margins",
246					'html' => "$icon<span>$label</span>",
247					'visible' => $visible,
248	];
249}
250
251$headerlib->add_js(
252	"var toolbarsadmin_rowStr = '" . substr(implode(",#row-", range(0, $rowCount)), 2) . "';" . "var toolbarsadmin_fullStr = '#full-list-w,#full-list-p,#full-list-c';" . "var toolbarsadmin_delete_text = '" . tra('Are you sure you want to delete this custom tool?') . "'\n"
253);
254
255$headerlib->add_jsfile('lib/toolbars/tiki-admin_toolbars.js');
256
257$display_w = array_diff($qt_w_list, $usedqt);
258
259if (! in_array('-', $display_w)) {
260	array_unshift($display_w, '-');
261}
262
263$display_p = array_diff($qt_p_list, $usedqt);
264$display_c = array_diff($customqt, $usedqt);
265
266sort($display_c);
267sort($display_p);
268sort($display_w);
269
270$headerlib->add_cssfile('themes/base_files/feature_css/admin.css');
271
272$plugins = [];
273
274$parserlib = TikiLib::lib('parser');
275
276foreach ($parserlib->plugin_get_list() as $name) {
277	$info = $parserlib->plugin_info($name);
278	if (isset($info['prefs']) && is_array($info['prefs']) && count($info['prefs']) > 0) {
279		$plugins[$name] = $info;
280	}
281}
282
283$smarty->assign('plugins', $plugins);
284
285$smarty->assign('comments', $comments);
286$smarty->assign('loaded', $section);
287$smarty->assign('rows', range(0, $rowCount - 1));
288$smarty->assign('rowCount', $rowCount);
289$smarty->assign('sections', $sections);
290$smarty->assign_by_ref('qtelement', $qtelement);
291$smarty->assign_by_ref('display_w', $display_w);
292$smarty->assign_by_ref('display_p', $display_p);
293$smarty->assign_by_ref('display_c', $display_c);
294
295$smarty->assign_by_ref('current', $current);
296$smarty->assign('mid', 'tiki-admin_toolbars.tpl');
297$smarty->display('tiki.tpl');
298