2016-11-29 22:27:19 +00:00
|
|
|
<?php
|
2021-12-07 22:14:12 -08:00
|
|
|
|
2022-02-14 20:48:15 -08:00
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Input class.
|
2016-11-29 22:27:19 +00:00
|
|
|
*
|
2022-02-14 20:48:15 -08:00
|
|
|
* @package NamelessMC\Core
|
|
|
|
|
* @author Samerton
|
|
|
|
|
* @version 2.0.0-pr8
|
|
|
|
|
* @license MIT
|
2016-11-29 22:27:19 +00:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
class Input
|
|
|
|
|
{
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Check that specified input type exists.
|
2021-12-07 22:14:12 -08:00
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $type Check for either POST or GET submission (optional, defaults to POST)
|
|
|
|
|
* @return bool Whether it exists or not.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public static function exists(string $type = 'post'): bool
|
|
|
|
|
{
|
2020-12-13 20:42:04 -08:00
|
|
|
switch ($type) {
|
2024-03-09 03:19:55 -08:00
|
|
|
case 'post':
|
2020-12-13 19:38:04 +01:00
|
|
|
// Check the $_POST variable
|
2021-10-08 20:39:22 +02:00
|
|
|
return !empty($_POST);
|
2024-03-09 03:19:55 -08:00
|
|
|
case 'get':
|
2020-12-13 19:38:04 +01:00
|
|
|
// Check the $_GET variable
|
2021-10-08 20:39:22 +02:00
|
|
|
return !empty($_GET);
|
2020-12-13 19:38:04 +01:00
|
|
|
default:
|
|
|
|
|
// Otherwise, return false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
|
|
|
|
* Get input with specified name.
|
2021-12-07 22:14:12 -08:00
|
|
|
*
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $item Name of element containing input to get.
|
|
|
|
|
* @return mixed Value of element in input.
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public static function get(string $item)
|
|
|
|
|
{
|
2020-12-13 20:42:04 -08:00
|
|
|
if (isset($_POST[$item])) {
|
2020-12-13 19:38:04 +01:00
|
|
|
return $_POST[$item];
|
2022-01-15 15:18:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($_GET[$item])) {
|
|
|
|
|
return $_GET[$item];
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 18:36:34 -07:00
|
|
|
/**
|
2024-03-09 03:19:55 -08:00
|
|
|
* Create a new TinyMCE instance.
|
2021-12-07 22:14:12 -08:00
|
|
|
*
|
2021-04-12 18:36:34 -07:00
|
|
|
* @param Language $language Instance of language class to use for translation.
|
2024-03-09 03:19:55 -08:00
|
|
|
* @param string $name Name of input field ID.
|
|
|
|
|
* @param ?string $content Any default content to insert
|
|
|
|
|
* @param bool $mentions Whether to enable mention autocompletion/parsing or not.
|
|
|
|
|
* @param bool $admin Enable admin only features
|
2022-07-20 19:18:29 +01:00
|
|
|
*
|
|
|
|
|
* @return string Script to render on page
|
2021-04-12 18:36:34 -07:00
|
|
|
*/
|
2024-03-09 03:19:55 -08:00
|
|
|
public static function createTinyEditor(Language $language, string $name, ?string $content = null, bool $mentions = false, bool $admin = false): string
|
|
|
|
|
{
|
2022-06-12 16:08:59 -06:00
|
|
|
if (
|
|
|
|
|
(defined('DARK_MODE') && DARK_MODE) ||
|
|
|
|
|
(Cookie::exists('nmc_panel_theme') && Cookie::get('nmc_panel_theme') === 'dark')
|
|
|
|
|
) {
|
|
|
|
|
$skin = 'oxide-dark';
|
|
|
|
|
} else {
|
|
|
|
|
$skin = 'oxide';
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 21:15:17 -07:00
|
|
|
$js = '';
|
|
|
|
|
|
|
|
|
|
if ($mentions) {
|
|
|
|
|
$js .= "
|
|
|
|
|
(function () {
|
|
|
|
|
let flags = (function () {
|
|
|
|
|
'use strict';
|
|
|
|
|
tinymce.PluginManager.add('mentions', function (editor, url) {
|
|
|
|
|
editor.ui.registry.addAutocompleter('autocompleter-mentions', {
|
|
|
|
|
ch: '@',
|
|
|
|
|
minChars: 2,
|
|
|
|
|
columns: 1,
|
|
|
|
|
fetch: function (pattern) {
|
|
|
|
|
return new tinymce.util.Promise(function (resolve) {
|
2022-03-31 10:58:40 -07:00
|
|
|
fetch('" . URL::build('/queries/mention_users', 'nickname') . "=' + pattern)
|
2022-03-30 21:15:17 -07:00
|
|
|
.then((resp) => resp.json())
|
|
|
|
|
.then(function (data) {
|
2022-03-30 22:04:50 -07:00
|
|
|
const results = [];
|
2025-04-25 00:32:04 -07:00
|
|
|
|
2022-03-31 08:44:30 -07:00
|
|
|
for (const user of data) {
|
2022-03-30 21:15:17 -07:00
|
|
|
results.push({
|
2022-03-31 08:56:22 -07:00
|
|
|
value: '@' + user.nickname,
|
|
|
|
|
text: user.nickname,
|
2022-03-30 21:55:19 -07:00
|
|
|
icon: '<img style=\"height:20px; width:20px;\" src=\"' + user.avatar_url + '\">'
|
2022-03-30 21:15:17 -07:00
|
|
|
});
|
|
|
|
|
}
|
2022-03-30 22:04:50 -07:00
|
|
|
|
|
|
|
|
results.sort((a, b) => a.text.toLowerCase().localeCompare(b.text.toLowerCase()))
|
2025-04-25 00:32:04 -07:00
|
|
|
|
2022-03-30 21:15:17 -07:00
|
|
|
resolve(results);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
onAction: function (autocompleteApi, rng, value) {
|
|
|
|
|
editor.selection.setRng(rng);
|
|
|
|
|
editor.insertContent(value);
|
|
|
|
|
autocompleteApi.hide();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}());
|
|
|
|
|
})();
|
|
|
|
|
";
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
$js .= '
|
2022-03-26 10:16:18 -07:00
|
|
|
tinymce.init({
|
2024-03-09 03:19:55 -08:00
|
|
|
verify_html: ' . ($admin ? 'false' : 'true') . ",
|
2022-03-26 10:16:18 -07:00
|
|
|
selector: '#$name',
|
|
|
|
|
browser_spellcheck: true,
|
|
|
|
|
contextmenu: false,
|
|
|
|
|
branding: false,
|
|
|
|
|
menubar: 'table',
|
|
|
|
|
convert_urls: false,
|
|
|
|
|
plugins: [
|
2022-03-30 21:15:17 -07:00
|
|
|
'autolink', 'codesample', 'directionality', 'emoticons', " . ($mentions ? "'mentions', " : '') . "
|
2022-03-26 10:16:18 -07:00
|
|
|
'hr', 'image', 'link', 'lists', 'spoiler', 'code', 'table',
|
|
|
|
|
],
|
2022-05-09 12:16:04 -06:00
|
|
|
external_plugins: {
|
2022-10-17 12:18:19 -07:00
|
|
|
'spoiler': '" . (defined('CONFIG_PATH') ? CONFIG_PATH : '') . "/core/assets/plugins/tinymce_spoiler/plugin.min.js',
|
2022-05-09 12:16:04 -06:00
|
|
|
},
|
2025-04-25 00:32:04 -07:00
|
|
|
toolbar: 'undo redo | bold italic underline strikethrough formatselect fontsizeselect fontselect forecolor backcolor ltr rtl emoticons | alignleft aligncenter alignright alignjustify | codesample " . ($admin ? 'code' : '') . " hr image link numlist bullist | spoiler-add spoiler-remove',
|
2022-03-26 10:16:18 -07:00
|
|
|
spoiler_caption: '{$language->get('general', 'spoiler')}',
|
|
|
|
|
default_link_target: '_blank',
|
2022-04-02 17:44:13 +01:00
|
|
|
skin: '$skin'," .
|
|
|
|
|
($content ?
|
|
|
|
|
'
|
|
|
|
|
setup: (editor) => {
|
|
|
|
|
editor.on(\'init\', () => {
|
|
|
|
|
editor.setContent(' . json_encode($content) . ');
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
'
|
|
|
|
|
: '') . "
|
2022-03-30 10:55:27 -07:00
|
|
|
images_upload_handler: function (blobInfo, success, failure, progress) {
|
|
|
|
|
let xhr, formData;
|
|
|
|
|
|
|
|
|
|
xhr = new XMLHttpRequest();
|
|
|
|
|
xhr.withCredentials = false;
|
|
|
|
|
xhr.open('POST', '" . URL::build('/queries/tinymce_image_upload') . "');
|
|
|
|
|
|
|
|
|
|
xhr.upload.onprogress = function (e) {
|
|
|
|
|
progress(e.loaded / e.total * 100);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
|
let json;
|
|
|
|
|
|
|
|
|
|
if (xhr.status !== 200) {
|
|
|
|
|
failure('HTTP Error ' + xhr.status + ': ' + xhr.responseText);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json = JSON.parse(xhr.responseText);
|
|
|
|
|
|
|
|
|
|
if (!json || typeof json.location != 'string') {
|
|
|
|
|
failure('Invalid JSON: ' + xhr.responseText);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
success(json.location);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
xhr.onerror = function () {
|
|
|
|
|
failure('Image upload failed due to a XHR Transport error. Code: ' + xhr.status);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
formData = new FormData();
|
|
|
|
|
formData.append('file', blobInfo.blob(), blobInfo.filename());
|
|
|
|
|
formData.append('token', '" . Token::get() . "');
|
|
|
|
|
|
|
|
|
|
xhr.send(formData);
|
|
|
|
|
},
|
2024-03-09 03:19:55 -08:00
|
|
|
" . ($admin ? 'valid_children: "+body[style],+body[link],+*[*]",' : '') . '
|
|
|
|
|
extended_valid_elements: ' . ($admin ?
|
2022-07-20 19:18:29 +01:00
|
|
|
'"script[src|async|defer|type|charset],+@[data-options]"'
|
2024-03-09 03:19:55 -08:00
|
|
|
: 'undefined') . '
|
2022-03-26 10:16:18 -07:00
|
|
|
});
|
2024-03-09 03:19:55 -08:00
|
|
|
';
|
2022-03-30 21:15:17 -07:00
|
|
|
|
|
|
|
|
return $js;
|
2020-12-13 19:38:04 +01:00
|
|
|
}
|
2020-11-07 13:16:19 +01:00
|
|
|
}
|