forked from FriendsOfREDAXO/tinymce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfiles.php
More file actions
193 lines (163 loc) · 9.02 KB
/
Copy pathProfiles.php
File metadata and controls
193 lines (163 loc) · 9.02 KB
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
<?php
namespace FriendsOfRedaxo\TinyMce\Creator;
use FriendsOfRedaxo\TinyMce\Handler\Database as TinyMceDatabaseHandler;
use FriendsOfRedaxo\TinyMce\Utils\AssetUrl;
use rex_addon;
use rex_addon_interface;
use rex_file;
use rex_functional_exception;
use rex_i18n;
use rex_url;
use function count;
use function is_string;
class Profiles
{
/** @api */
public const PROFILES_FILENAME = 'profiles.js';
/** @api */
public const ALLOWED_FIELDS = [
'toolbar' => ['|', 'styleselect', 'undo', 'redo', 'save', 'bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript', 'forecolor', 'backcolor', 'ltr', 'rtl', 'table', 'visualblocks', 'visualchars', 'link', 'image', 'media', 'codesample', 'fontselect', 'align', 'alignleft', 'aligncenter', 'alignright', 'alignjustify', 'numlist', 'bullist', 'outdent', 'indent', 'removeformat', 'code', 'hr', 'print', 'preview', 'media', 'fullscreen', 'searchreplace', 'emoticons', 'visualaid', 'cut', 'copy', 'paste', 'pastetext', 'selectall', 'wordcount', 'charmap', 'pagebreak', 'nonbreaking', 'anchor', 'toc', 'insertdatetime'],
'plugins' => ['autoresize', 'save', 'print', 'preview', 'searchreplace', 'autolink', 'directionality', 'visualblocks', 'visualchars', 'fullscreen', 'image', 'link', 'media', 'codesample', 'table', 'charmap', 'hr', 'pagebreak', 'nonbreaking', 'anchor', 'toc', 'insertdatetime', 'advlist', 'lists', 'wordcount', 'imagetools', 'textpattern', 'help', 'emoticons', 'paste', 'code'],
];
/**
* @param array<string, mixed>|null $getProfile
* @throws rex_functional_exception
*/
public static function profilesCreate(?array $getProfile = null): void
{
$profiles = TinyMceDatabaseHandler::getAllProfiles();
$content = '';
if (null !== $profiles && [] !== $profiles) {
$jsonProfiles = [];
$extras = [];
foreach ($profiles as $profile) {
if (isset($getProfile['name']) && $profile['name'] === $getProfile['name']) {
$profile = $getProfile;
}
$result = self::mapProfile($profile);
$key_defaults = uniqid();
$key_extras = uniqid();
$extras[$key_defaults] = $result;
$jsonProfiles[$profile['name']][$key_defaults] = $key_defaults;
$jsonProfiles[$profile['name']][$key_extras] = $key_extras;
}
$extraValues = [];
$extraKeys = [];
foreach ($extras as $key => $value) {
$extraKeys[$key] = "\"$key\":\"$key\"";
$extraValues[$key] = $value ?: '';
}
$profiles = json_encode($jsonProfiles);
$profiles = is_string($profiles) ? $profiles : '{}';
/** @var array<string> $extraKeysValues */
$extraKeysValues = array_values($extraKeys);
/** @var array<string> $extraValuesValues */
$extraValuesValues = array_values($extraValues);
$profiles = str_replace($extraKeysValues, $extraValuesValues, $profiles);
$profiles = str_replace(',,', ',', $profiles);
// External plugins are provided at runtime via rex_view::setJsProperty() in Assets::provideBaseAssets()
// This ensures correct absolute URLs. The empty object here serves only as a fallback.
// See base.js: rex.tinyExternalPlugins (runtime) takes precedence over tinyExternalPlugins (static).
$externalPluginsJs = '{}';
// Frontend fallback (no rex_view::setJsProperty available there).
// These constants are consumed by base.js if rex.* properties are missing.
$tinyAssetBasePathJs = json_encode(AssetUrl::getTinyAssetBaseUrl(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$tinyPluginBasePathJs = json_encode(AssetUrl::getTinyPluginBaseUrl(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// CleanPaste config – embedded here so it works in frontend too (rex_view::setJsProperty is backend-only)
$cleanPasteCfg = self::getAddon()->getConfig('cleanpaste_settings', [
'strip_ms_office' => true,
'strip_google_docs' => true,
'remove_styles' => true,
'preserve_styles' => [],
'remove_classes' => true,
'preserve_classes' => [],
'remove_ids' => true,
'remove_data_attrs' => true,
'max_br' => 2,
'max_empty_paragraphs' => 2,
'allowed_tags' => [],
'clean_internal_paste' => false,
]);
$cleanPasteConfigJs = json_encode($cleanPasteCfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// MediaUpload config – embedded here so it works in frontend too
/** @var array{upload_enabled?: bool, upload_default_category?: int, upload_media_manager_type?: string} $mediaUploadSettings */
$mediaUploadSettings = self::getAddon()->getConfig('media_upload_settings', []);
$mediaUploadCfg = [
'enabled' => (bool) ($mediaUploadSettings['upload_enabled'] ?? false),
'default_category' => (int) ($mediaUploadSettings['upload_default_category'] ?? -1),
'upload_url' => rex_url::backendController(['rex-api-call' => 'tinymce_media_upload'], false),
'categories_url' => rex_url::backendController(['rex-api-call' => 'tinymce_media_categories'], false),
];
$mediaUploadConfigJs = json_encode($mediaUploadCfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$content =
"
const tinyExternalPlugins = $externalPluginsJs;
const tinyAssetBasePath = $tinyAssetBasePathJs;
const tinyPluginBasePath = $tinyPluginBasePathJs;
const tinyCleanPasteConfig = $cleanPasteConfigJs;
const tinyMediaUploadConfig = $mediaUploadConfigJs;
const tinyprofiles = $profiles;
";
}
if (!rex_file::put(self::getAddon()->getAssetsPath('generated/' . self::PROFILES_FILENAME), $content)) {
throw new rex_functional_exception(rex_i18n::msg('tinymce_profiles_creation_exception'));
}
}
/**
* @param array<string, mixed> $profile
*/
public static function mapProfile(array $profile): string
{
$jsonProfile = '';
if (!empty($profile['profile'])) {
$extra = (string) $profile['profile'];
$addonAssetBase = AssetUrl::getTinyAssetBaseUrl();
$addonAssetBaseEscaped = str_replace('/', '\\/', $addonAssetBase);
// Normalize legacy hardcoded plugin paths so subfolder installs work.
$extra = str_replace('"/assets/addons/tinymce/', '"' . $addonAssetBase . '/', $extra);
$extra = str_replace('"assets/addons/tinymce/', '"' . $addonAssetBase . '/', $extra);
$extra = str_replace('"../assets/addons/tinymce/', '"' . $addonAssetBase . '/', $extra);
// Also cover escaped variants from imported JSON payloads.
$extra = str_replace('"\/assets\/addons\/tinymce\/', '"' . $addonAssetBaseEscaped . '\\/', $extra);
$extra = str_replace('"assets\\/addons\\/tinymce\\/', '"' . $addonAssetBaseEscaped . '\\/', $extra);
$extra = str_replace('"..\\/assets\\/addons\\/tinymce\\/', '"' . $addonAssetBaseEscaped . '\\/', $extra);
if (!self::profileHasPlugin('quickbars', $extra)) {
$sanitized = preg_replace('/^\s*quickbars_(?:selection|insert|image)_toolbar\s*:\s*[^,\r\n]+,\s*$/mi', '', $extra);
if (is_string($sanitized)) {
$extra = $sanitized;
}
}
if (1 !== preg_match('/(?:^|[,\{\r\n])\s*license_key\s*:/', $extra)) {
$extra = "license_key: 'gpl',\n" . $extra;
}
return $extra;
}
return $jsonProfile;
}
/**
* Checks whether a plugin is active by scanning the `extra` config block.
* Supports both string syntax (`plugins: 'a b c'`) and array syntax
* (`plugins: ['a', 'b', 'c']`).
*/
private static function profileHasPlugin(string $plugin, string $extra): bool
{
$pluginPattern = '/(^|\s)' . preg_quote($plugin, '/') . '(\s|$)/';
// String syntax: plugins: 'a b quickbars'
if (preg_match('/(?:^|[,\{\r\n])\s*plugins\s*:\s*(["\'])([^"\']*)\1/mi', $extra, $stringMatch) === 1) {
if (preg_match($pluginPattern, $stringMatch[2]) === 1) {
return true;
}
}
// Array syntax: plugins: ['a', 'b', 'quickbars']
if (preg_match('/(?:^|[,\{\r\n])\s*plugins\s*:\s*\[([^\]]*)\]/mi', $extra, $arrayMatch) === 1) {
if (preg_match('/(["\'])' . preg_quote($plugin, '/') . '\\1/', $arrayMatch[1]) === 1) {
return true;
}
}
return false;
}
private static function getAddon(): rex_addon|rex_addon_interface
{
return rex_addon::get('tinymce');
}
}