1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function wikiplugin_wysiwyg_info()
9{
10	global $prefs;
11
12	return [
13		'name' => 'WYSIWYG',
14		'documentation' => 'PluginWYSIWYG',
15		'description' => tra('Use a WYSIWYG editor to edit a section of content'),
16		'prefs' => ['wikiplugin_wysiwyg'],
17		'iconname' => 'wysiwyg',
18		'introduced' => 9,
19		'tags' => [ 'experimental' ], // Several important bugs, notably #6551 and serious #6476. Most bugs are probably not specific to the WYSIWYG *plugin* (see feature_wysiwyg). Chealer 2018-01-24
20		'filter' => 'purifier',			/* N.B. uses htmlpurifier to ensure only "clean" html gets in */
21		'format' => 'html',
22		'body' => tra('Content'),
23		'extraparams' => true,
24		'params' => [
25			'width' => [
26				'required' => false,
27				'name' => tra('Width'),
28				'description' => tra('Minimum width for DIV. Default:') . ' <code>100px</code>',
29				'since' => '9.0',
30				'filter' => 'text',
31				'default' => '100px',
32			],
33			'height' => [
34				'required' => false,
35				'name' => tra('Height'),
36				'description' => tra('Minimum height for DIV. Default:') . ' <code>300px</code>',
37				'since' => '9.0',
38				'filter' => 'text',
39				'default' => '300px',
40			],
41			'use_html' => [
42				'required' => false,
43				'name' => tra('Use HTML'),
44				'description' => tr('By default, the body (content) of calls to the WYSIWYG plugin is interpreted according to the "Use Wiki syntax in WYSIWYG" (%0wysiwyg_htmltowiki%1) preference. By default, "Use HTML" is considered enabled if "Use Wiki syntax in WYSIWYG" is disabled, and vice versa.
45				 This parameter allows overriding that preference if needed.', '<code>', '</code>'),
46				'since' => '14.1',
47				'filter' => 'alpha',
48				'default' => $prefs['wysiwyg_htmltowiki'] == 'y' ? 'n' : 'y',
49				'options' => [
50					['text' => '', 'value' => ''],
51					['text' => tra('Yes'), 'value' => 'y'],
52					['text' => tra('No'), 'value' => 'n']
53				]
54			],
55		],
56	];
57} // wikiplugin_wysiwyg_info()
58
59
60function wikiplugin_wysiwyg($data, $params)
61{
62	// TODO refactor: defaults for plugins?
63	$defaults = [];
64	$plugininfo = wikiplugin_wysiwyg_info();
65	foreach ($plugininfo['params'] as $key => $param) {
66		$defaults["$key"] = $param['default'];
67	}
68	$params = array_merge($defaults, $params);
69
70	global $tiki_p_edit, $page, $prefs, $user;
71	static $execution = 0;
72
73	global $wikiplugin_included_page;
74	if (! empty($wikiplugin_included_page)) {
75		$sourcepage = $wikiplugin_included_page;
76	} else {
77		$sourcepage = $page;
78	}
79
80	$contentIsHTML = ! ($params['use_html'] !== 'y');
81	$html = TikiLib::lib('edit')->parseToWysiwyg($data, true, $contentIsHTML, ['page' => $sourcepage]);
82
83	if (TikiLib::lib('tiki')->user_has_perm_on_object($user, $sourcepage, 'wiki page', 'tiki_p_edit')) {
84		$class = "wp_wysiwyg";
85		$exec_key = $class . '_' . ++ $execution;
86		$style = " style='min-width:{$params['width']};min-height:{$params['height']}'";
87
88		$params['section'] = empty($params['section']) ? 'wysiwyg_plugin' : $params['section'];
89		$params['_wysiwyg'] = 'y';
90		$params['is_html'] = $contentIsHTML;
91		$params['_is_html'] = $contentIsHTML;    // needed for toolbars
92		//$params['comments'] = true;
93		$ckoption = TikiLib::lib('wysiwyg')->setUpEditor($contentIsHTML, $exec_key, $params, '');
94
95		if ($prefs['namespace_enabled'] == 'y' && $prefs['namespace_force_links'] == 'y') {
96			$namespace = TikiLib::lib('wiki')->get_namespace($sourcepage);
97			if ($namespace) {
98				$namespace .= $prefs['namespace_separator'];
99			}
100		} else {
101			$namespace = '';
102		}
103		$namespace = htmlspecialchars($namespace);
104
105		$smarty = TikiLib::lib('smarty');
106		$smarty->loadPlugin('smarty_function_ticket');
107
108		$html = "<div id='$exec_key' class='{$class}'$style data-initial='$namespace' data-html='{$params['use_html']}' data-ticket='"
109			. smarty_function_ticket(['mode' => 'get'], $smarty->getEmptyInternalTemplate()) . "'>" . $html . '</div>';
110
111		$js = '$("#' . $exec_key . '").wysiwygPlugin("' . $execution . '", "' . $sourcepage . '", ' . $ckoption . ');';
112
113		TikiLib::lib('header')
114			->add_jsfile('lib/ckeditor_tiki/tiki-ckeditor.js')
115			->add_jq_onready($js);
116	}
117	return $html;
118}
119