ManagePortalConfig_Controller::formatToBBC()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 37
rs 9.6
1
<?php
2
3
/**
4
 * @package SimplePortal ElkArte
5
 *
6
 * @author SimplePortal Team
7
 * @copyright 2015-2021 SimplePortal Team
8
 * @license BSD 3-clause
9
 * @version 1.0.0
10
 */
11
12
use BBC\ParserWrapper;
13
use BBC\PreparseCode;
14
15
/**
16
 * SimplePortal Configuration controller class.
17
 * This class handles the general, blocks and articles configuration screens
18
 */
19
class ManagePortalConfig_Controller extends Action_Controller
20
{
21
	/** @var Settings_Form General settings form */
22
	protected $_generalSettingsForm;
23
24
	/** @var Settings_Form Block settings form */
25
	protected $_blockSettingsForm;
26
27
	/** @var Settings_Form Article settings form */
28
	protected $_articleSettingsForm;
29
30
	/**
31
	 * Main dispatcher.
32
	 *
33
	 * This function checks permissions and passes control through.
34
	 * If the passed section is not found it shows the information page.
35
	 */
36
	public function action_index()
37
	{
38
		global $context, $txt;
39
40
		// You need to be an admin or have manage setting permissions to change anything
41
		if (!allowedTo('sp_admin'))
42
		{
43
			isAllowedTo('sp_manage_settings');
44
		}
45
46
		// Some helpful friends
47
		require_once(SUBSDIR . '/PortalAdmin.subs.php');
48
		require_once(SUBSDIR . '/Portal.subs.php');
49
		require_once(ADMINDIR . '/ManageServer.controller.php');
50
		loadCSSFile('portal.css', ['stale' => SPORTAL_STALE]);
51
52
		// Load the Simple Portal Help file.
53
		loadLanguage('SPortalHelp');
54
55
		$subActions = array(
56
			'information' => array($this, 'action_information'),
57
			'generalsettings' => array($this, 'action_general_settings'),
58
			'blocksettings' => array($this, 'action_block_settings'),
59
			'articlesettings' => array($this, 'action_article_settings'),
60
			'formatchange' => array($this, 'action_format_change'),
61
		);
62
63
		// Start up the controller, provide a hook since we can
64
		$action = new Action('portal_main');
65
66
		$context[$context['admin_menu_name']]['tab_data'] = array(
67
			'title' => $txt['sp-adminConfiguration'],
68
			'help' => 'sp_ConfigurationArea',
69
			'description' => $txt['sp-adminConfigurationDesc'],
70
		);
71
72
		// Set the default to the information tab
73
		$subAction = $action->initialize($subActions, 'information');
74
75
		// Right then, off you go
76
		$action->dispatch($subAction);
77
	}
78
79
	/**
80
	 * General settings that control global portal actions
81
	 */
82
	public function action_general_settings()
83
	{
84
		global $context, $scripturl, $txt;
85
86
		$context['SPortal']['themes'] = sp_general_load_themes();
87
88
		// These are very likely to come in handy! (i.e. without them we're doomed!)
89
		require_once(ADMINDIR . '/ManagePermissions.controller.php');
90
		require_once(ADMINDIR . '/ManageServer.controller.php');
91
		require_once(SUBSDIR . '/SettingsForm.class.php');
92
93
		// Initialize the form
94
		$this->_initGeneralSettingsForm();
95
96
		if (isset($_GET['save']))
97
		{
98
			checkSession();
99
100
			if (!empty($_POST['sp_portal_mode']))
101
			{
102
				updateSettings(array('front_page' => 'PortalMain_Controller'));
103
			}
104
			else
105
			{
106
				updateSettings(array('front_page' => 'MessageIndex_Controller'));
107
			}
108
109
			$this->_generalSettingsForm->setConfigValues($_POST);
110
			$this->_generalSettingsForm->save();
111
			redirectexit('action=admin;area=portalconfig;sa=generalsettings');
112
		}
113
114
		$context['post_url'] = $scripturl . '?action=admin;area=portalconfig;sa=generalsettings;save';
115
		$context['settings_title'] = $txt['sp-adminGeneralSettingsName'];
116
		$context['page_title'] = $txt['sp-adminGeneralSettingsName'];
117
		$context['sub_template'] = 'show_settings';
118
119
		$this->_generalSettingsForm->prepare();
120
	}
121
122
	/**
123
	 * Initialize General Settings Form.
124
	 * Retrieve and return the general portal settings.
125
	 */
126
	private function _initGeneralSettingsForm()
127
	{
128
		global $txt, $context;
129
130
		// Instantiate the form
131
		$this->_generalSettingsForm = new Settings_Form(Settings_Form::DB_ADAPTER);
132
133
		$config_vars = array(
134
			array('select', 'sp_portal_mode', explode('|', $txt['sp_portal_mode_options'])),
135
			array('check', 'sp_maintenance'),
136
			array('text', 'sp_standalone_url'),
137
			'',
138
			array('select', 'portaltheme', $context['SPortal']['themes']),
139
			array('check', 'sp_disableColor'),
140
			array('check', 'sp_disableForumRedirect'),
141
			array('check', 'sp_disable_random_bullets'),
142
			array('check', 'sp_disable_php_validation', 'subtext' => $txt['sp_disable_php_validation_desc']),
143
			array('check', 'sp_disable_side_collapse'),
144
			array('check', 'sp_resize_images'),
145
			array('check', 'sp_disableMobile'),
146
		);
147
148
		$this->_generalSettingsForm->setConfigVars($config_vars);
149
	}
150
151
	/**
152
	 * Settings that control how blocks behave
153
	 */
154
	public function action_block_settings()
155
	{
156
		global $context, $scripturl, $txt;
157
158
		// These are very likely to come in handy! (i.e. without them we're doomed!)
159
		require_once(ADMINDIR . '/ManagePermissions.controller.php');
160
		require_once(ADMINDIR . '/ManageServer.controller.php');
161
		require_once(SUBSDIR . '/SettingsForm.class.php');
162
163
		// Initialize the form
164
		$this->_initBlockSettingsForm();
165
		$config_vars = $this->_blockSettingsForm->getConfigVars();
166
167
		if (isset($_GET['save']))
168
		{
169
			checkSession();
170
171
			$width_checkup = array('left', 'right');
172
			foreach ($width_checkup as $pos)
173
			{
174
				if (!empty($_POST[$pos . 'width']))
175
				{
176
					if (stripos($_POST[$pos . 'width'], 'px') !== false)
177
					{
178
						$suffix = 'px';
179
					}
180
					elseif (strpos($_POST[$pos . 'width'], '%') !== false)
181
					{
182
						$suffix = '%';
183
					}
184
					else
185
					{
186
						$suffix = 'px';
187
					}
188
189
					preg_match_all('/(?:([0-9]+)|.)/i', $_POST[$pos . 'width'], $matches);
190
191
					$number = (int) implode('', $matches[1]);
192
					if (!empty($number) && $number > 0)
193
					{
194
						$_POST[$pos . 'width'] = $number . $suffix;
195
					}
196
					else
197
					{
198
						$_POST[$pos . 'width'] = '';
199
					}
200
				}
201
				else
202
				{
203
					$_POST[$pos . 'width'] = '';
204
				}
205
			}
206
207
			unset($config_vars[7]);
208
			$config_vars = array_merge(
209
				$config_vars, array(
210
					array('check', 'sp_adminIntegrationHide'),
211
					array('check', 'sp_profileIntegrationHide'),
212
					array('check', 'sp_pmIntegrationHide'),
213
					array('check', 'sp_mlistIntegrationHide'),
214
					array('check', 'sp_searchIntegrationHide'),
215
					array('check', 'sp_calendarIntegrationHide'),
216
					array('check', 'sp_moderateIntegrationHide'),
217
				)
218
			);
219
220
			$this->_blockSettingsForm->setConfigVars($config_vars);
221
			$this->_blockSettingsForm->setConfigValues($_POST);
222
			$this->_blockSettingsForm->save();
223
			redirectexit('action=admin;area=portalconfig;sa=blocksettings');
224
		}
225
226
		$context['post_url'] = $scripturl . '?action=admin;area=portalconfig;sa=blocksettings;save';
227
		$context['settings_title'] = $txt['sp-adminBlockSettingsName'];
228
		$context['page_title'] = $txt['sp-adminBlockSettingsName'];
229
		$context['sub_template'] = 'show_settings';
230
231
		$this->_blockSettingsForm->prepare();
232
	}
233
234
	/**
235
	 * Initialize Block Settings Form.
236
	 * Retrieve and return the general portal settings.
237
	 */
238
	private function _initBlockSettingsForm()
239
	{
240
		global $txt;
241
242
		// instantiate the block form
243
		$this->_blockSettingsForm = new Settings_Form(Settings_Form::DB_ADAPTER);
244
245
		$config_vars = array(
246
			array('check', 'showleft'),
247
			array('check', 'showright'),
248
			array('text', 'leftwidth'),
249
			array('text', 'rightwidth'),
250
			'',
251
			array('multicheck',
252
				  'sp_IntegrationHide',
253
				  'subsettings' => array('sp_adminIntegrationHide' => $txt['admin'], 'sp_profileIntegrationHide' => $txt['profile'], 'sp_pmIntegrationHide' => $txt['personal_messages'], 'sp_mlistIntegrationHide' => $txt['members_title'], 'sp_searchIntegrationHide' => $txt['search'], 'sp_calendarIntegrationHide' => $txt['calendar'], 'sp_moderateIntegrationHide' => $txt['moderate']),
254
				  'subtext' => $txt['sp_IntegrationHide_desc']
255
			),
256
		);
257
258
		$this->_blockSettingsForm->setConfigVars($config_vars);
259
	}
260
261
	/**
262
	 * Settings to control articles
263
	 */
264
	public function action_article_settings()
265
	{
266
		global $context, $scripturl, $txt;
267
268
		// These are very likely to come in handy! (i.e. without them we're doomed!)
269
		require_once(ADMINDIR . '/ManagePermissions.controller.php');
270
		require_once(ADMINDIR . '/ManageServer.controller.php');
271
		require_once(SUBSDIR . '/SettingsForm.class.php');
272
273
		// Initialize the form
274
		$this->_initArticleSettingsForm();
275
276
		// Save away
277
		if (isset($_GET['save']))
278
		{
279
			checkSession();
280
281
			$this->_articleSettingsForm->setConfigValues($_POST);
282
			$this->_articleSettingsForm->save();
283
			redirectexit('action=admin;area=portalconfig;sa=articlesettings');
284
		}
285
286
		// Show the form
287
		$context['post_url'] = $scripturl . '?action=admin;area=portalconfig;sa=articlesettings;save';
288
		$context['settings_title'] = $txt['sp-adminArticleSettingsName'];
289
		$context['page_title'] = $txt['sp-adminArticleSettingsName'];
290
		$context['sub_template'] = 'show_settings';
291
292
		$this->_articleSettingsForm->prepare();
293
	}
294
295
	/**
296
	 * Initialize Article Settings Form.
297
	 * Retrieve and return the general portal settings.
298
	 */
299
	private function _initArticleSettingsForm()
300
	{
301
		// instantiate the article form
302
		$this->_articleSettingsForm = new Settings_Form(Settings_Form::DB_ADAPTER);
303
304
		$config_vars = array(
305
			array('check', 'sp_articles_index'),
306
			array('int', 'sp_articles_index_per_page'),
307
			array('int', 'sp_articles_index_total'),
308
			array('int', 'sp_articles_length'),
309
			'',
310
			array('int', 'sp_articles_per_page'),
311
			array('int', 'sp_articles_comments_per_page'),
312
			'',
313
			array('text', 'sp_articles_attachment_dir')
314
		);
315
316
		$this->_articleSettingsForm->setConfigVars($config_vars);
317
	}
318
319
	/**
320
	 * Our about page etc.
321
	 *
322
	 * @param boolean $in_admin
323
	 * @throws Elk_Exception
324
	 */
325
	public function action_information($in_admin = true)
326
	{
327
		global $context, $scripturl, $txt, $user_profile;
328
329
		loadTemplate('PortalAdmin');
330
		loadJavascriptFile('portal.js?sp100rc1');
331
332
		$context['sp_credits'] = array(
333
			array(
334
				'pretext' => $txt['sp-info_intro'],
335
				'title' => $txt['sp-info_team'],
336
				'groups' => array(
337
					array(
338
						'title' => $txt['sp-info_groups_pm'],
339
						'members' => array(
340
							'Eliana Tamerin',
341
							'Huw',
342
						),
343
					),
344
					array(
345
						'title' => $txt['sp-info_groups_dev'],
346
						'members' => array(
347
							'<span onclick="if (getInnerHTML(this).indexOf(\'Sinan\') === -1) setInnerHTML(this, \'Sinan &quot;[SiNaN]&quot; &Ccedil;evik\'); return false;">Selman &quot;[SiNaN]&quot; Eser</span>',
348
							'Nathaniel Baxter',
349
							'&#12487;&#12451;&#12531;1031',
350
							'spuds',
351
							'emanuele',
352
						),
353
					),
354
					array(
355
						'title' => $txt['sp-info_groups_support'],
356
						'members' => array(
357
							'<span onclick="if (getInnerHTML(this).indexOf(\'Queen\') === -1) setInnerHTML(this, \'Angelina &quot;Queen of Support&quot; Belle\'); return false;">AngelinaBelle</span>',
358
							'Chen Zhen',
359
							'andy',
360
							'Ninja ZX-10RR',
361
							'phantomm',
362
						),
363
					),
364
					array(
365
						'title' => $txt['sp-info_groups_customize'],
366
						'members' => array(
367
							'Robbo',
368
							'Berat &quot;grafitus&quot; Do&#287;an',
369
							'Blue',
370
						),
371
					),
372
					array(
373
						'title' => $txt['sp-info_groups_language'],
374
						'members' => array(
375
							'Kryzen',
376
							'Jade &quot;Alundra&quot; Elizabeth',
377
							'<span onclick="if (getInnerHTML(this).indexOf(\'King\') === -1) setInnerHTML(this, \'130 &quot;King of Pirates&quot; 860\'); return false;">130860</span>',
378
						),
379
					),
380
					array(
381
						'title' => $txt['sp-info_groups_marketing'],
382
						'members' => array(
383
							'BryanD',
384
						),
385
					),
386
					array(
387
						'title' => $txt['sp-info_groups_beta'],
388
						'members' => array(
389
							'BurkeKnight',
390
							'ARG',
391
							'Old Fossil',
392
							'David',
393
							'sharks',
394
							'Willerby',
395
							'&#214;zg&#252;r',
396
							'c23_Mike',
397
						),
398
					),
399
				),
400
			),
401
			array(
402
				'title' => $txt['sp-info_special'],
403
				'posttext' => $txt['sp-info_anyone'],
404
				'groups' => array(
405
					array(
406
						'title' => $txt['sp-info_groups_translators'],
407
						'members' => array(
408
							$txt['sp-info_translators_message'],
409
						),
410
					),
411
					array(
412
						'title' => $txt['sp-info_groups_founder'],
413
						'members' => array(),
414
					),
415
					array(
416
						'title' => $txt['sp-info_groups_original_pm'],
417
						'members' => array(),
418
					),
419
					array(
420
						'title' => $txt['sp-info_fam_fam'],
421
						'members' => array(
422
							$txt['sp-info_fam_fam_message'],
423
						),
424
					),
425
				),
426
			),
427
		);
428
429
		if (!$in_admin)
430
		{
431
			$context['robot_no_index'] = true;
432
			$context['in_admin'] = false;
433
		}
434
		else
435
		{
436
			$context['in_admin'] = true;
437
			$context['sp_version'] = SPORTAL_VERSION;
438
			$context['sp_managers'] = array();
439
440
			require_once(SUBSDIR . '/Members.subs.php');
441
			$manager_ids = loadMemberData(membersAllowedTo('sp_admin'), false, 'minimal');
442
443
			if ($manager_ids)
444
			{
445
				foreach ($manager_ids as $member)
446
				{
447
					$context['sp_managers'][] = '<a href="' . $scripturl . '?action=profile;u=' . $user_profile[$member]['id_member'] . '">' . $user_profile[$member]['real_name'] . '</a>';
448
				}
449
			}
450
		}
451
452
		$context['sub_template'] = 'information';
453
		$context['page_title'] = $txt['sp-info_title'];
454
	}
455
456
	/**
457
	 * This is an ajax return function for articles and pages and anything else that
458
	 * wants to use it, for the purpose of text format conversion
459
	 *
460
	 * Intended to "munge" source formats from a <> b Used when changing the type from
461
	 * bbc to html or markdown to bbc or php to bbc ..... you get it.
462
	 */
463
	public function action_format_change()
464
	{
465
		global $context;
466
467
		// Pretty basic
468
		checkSession('request');
469
470
		// Responding to an ajax request, that is all we do
471
		$req = request();
472
		if ($req->is_xml())
473
		{
474
			loadTemplate('PortalAdmin');
475
476
			$format_parameters = array(
477
				'text' => urldecode($_REQUEST['text']),
478
				'from' => $_REQUEST['from'],
479
				'to' => $_REQUEST['to'],
480
			);
481
482
			// Do whatever format juggle is requested
483
			$func = 'formatTo' . strtoupper($format_parameters['to']);
484
			if (method_exists($this, $func))
485
			{
486
				$this->$func($format_parameters);
487
			}
488
489
			// Return an xml response
490
			Template_Layers::instance()->removeAll();
491
			$context['sub_template'] = 'format_xml';
492
			$context['SPortal']['text'] = $format_parameters['text'];
493
		}
494
	}
495
496
	/**
497
	 * Convert various formats to BBC
498
	 *
499
	 * Convert MD->BBC, PHP->BBC, and HTML->BBC
500
	 *
501
	 * @param $format_parameters
502
	 */
503
	private function formatToBBC(&$format_parameters)
504
	{
505
		// From MD to BBC, the round about way
506
		if ($format_parameters['from'] === 'markdown')
507
		{
508
			// MD to HTML
509
			require_once(EXTDIR . '/markdown/markdown.php');
510
			$format_parameters['text'] = Markdown($format_parameters['text']);
511
512
			// HTML to BBC
513
			$parser = new Html_2_BBC($format_parameters['text']);
514
			$format_parameters['text'] = $parser->get_bbc();
515
			$format_parameters['text'] = str_replace('[br]', "\n\n", $format_parameters['text']);
516
			$format_parameters['text'] = un_htmlspecialchars($format_parameters['text']);
517
			return;
518
		}
519
520
		// From php to BBC ?
521
		if ($format_parameters['from'] === 'php')
522
		{
523
			$format_parameters['text'] = htmlspecialchars($format_parameters['text']);
524
			$format_parameters['text'] = '[code=php]' . $format_parameters['text'] . '[/code]';
525
			return;
526
		}
527
528
		// HTML to BBC
529
		if ($format_parameters['from'] === 'html')
530
		{
531
			// The converter does not treat <Hx> tags as block level :(
532
			$format_parameters['text'] = preg_replace('~(<h\d>.*?<\/h\d>)~', '<br>$1<br>', $format_parameters['text']);
533
534
			$bbc_converter = new Html_2_BBC($format_parameters['text']);
535
			$bbc_converter->skip_tags(array('font', 'span'));
536
			$bbc_converter->skip_styles(array('font-family'));
537
			$format_parameters['text'] = $bbc_converter->get_bbc();
538
			$format_parameters['text'] = un_htmlspecialchars($format_parameters['text']);
539
			$format_parameters['text'] = str_replace('[br]', "\n", $format_parameters['text']);
540
		}
541
	}
542
543
	/**
544
	 * Convert various formats to HTML
545
	 *
546
	 * Markdown->HTML, PHP->HTML, and BBC->HTML
547
	 *
548
	 * @param array $format_parameters
549
	 */
550
	private function formatToHTML(&$format_parameters)
551
	{
552
		// From MD to HTML
553
		if ($format_parameters['from'] === 'markdown')
554
		{
555
			require_once(EXTDIR . '/markdown/markdown.php');
556
			$format_parameters['text'] = htmlspecialchars(Markdown($format_parameters['text']));
557
			return;
558
		}
559
560
		// From PHP to HTML ?
561
		if ($format_parameters['from'] === 'php')
562
		{
563
			$format_parameters['text'] = "<code>\n<cite>php</cite>\n" . $format_parameters['text'] . "\n</code>";
564
			$format_parameters['text'] = htmlspecialchars($format_parameters['text']);
565
			return;
566
		}
567
568
		// BBC to HTML
569
		if ($format_parameters['from'] === 'bbc')
570
		{
571
			PreparseCode::instance()->preparsecode($format_parameters['text'], false);
572
			$format_parameters['text'] = ParserWrapper::instance()->parseMessage($format_parameters['text'] , true);
573
574
			$format_parameters['text'] = strtr($format_parameters['text'], array('&nbsp;' => ' ', '<br />' => "\n<br />"));
575
			$format_parameters['text'] = htmlspecialchars($format_parameters['text']);
576
		}
577
	}
578
579
	/**
580
	 * Convert various formats to MarkDown
581
	 *
582
	 * Convert BBC->Markdown, PHP->Markdown, and HTML->Markdown
583
	 *
584
	 * @param array $format_parameters
585
	 */
586
	private function formatToMARKDOWN(&$format_parameters)
587
	{
588
		// From BBC to MD, should have a direct way, but ...
589
		if ($format_parameters['from'] === 'bbc')
590
		{
591
			PreparseCode::instance()->preparsecode($format_parameters['text'], false);
592
			$format_parameters['text'] = ParserWrapper::instance()->parseMessage($format_parameters['text'] , true);
593
594
			// Convert this to markdown
595
			$parser = new Html_2_Md($format_parameters['text']);
596
			$format_parameters['text'] = $parser->get_markdown();
597
			$format_parameters['text'] = htmlspecialchars($format_parameters['text']);
598
			return;
599
		}
600
601
		// From PHP to MD ?
602
		if ($format_parameters['from'] === 'php')
603
		{
604
			$format_parameters['text'] = '<code>' . un_htmlspecialchars($format_parameters['text']) . '</code>';
605
			$parser = new Html_2_Md($format_parameters['text']);
606
			$format_parameters['text'] = htmlspecialchars($parser->get_markdown());
607
			return;
608
		}
609
610
		// HTML to MD
611
		if ($format_parameters['from'] === 'html')
612
		{
613
			// Convert this to markdown
614
			$parser = new Html_2_Md($format_parameters['text']);
615
			$format_parameters['text'] = $parser->get_markdown();
616
		}
617
	}
618
}
619