mc-cms-namelessmc/core/classes/Core/Input.php

198 lines
7.4 KiB
PHP
Raw Permalink Normal View History

<?php
2021-12-07 22:14:12 -08:00
/**
* Input class.
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.0.0-pr8
* @license MIT
*/
class Input
{
/**
* Check that specified input type exists.
2021-12-07 22:14:12 -08:00
*
* @param string $type Check for either POST or GET submission (optional, defaults to POST)
* @return bool Whether it exists or not.
*/
public static function exists(string $type = 'post'): bool
{
2020-12-13 20:42:04 -08:00
switch ($type) {
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);
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;
}
}
/**
* Get input with specified name.
2021-12-07 22:14:12 -08:00
*
* @param string $item Name of element containing input to get.
* @return mixed Value of element in input.
*/
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 '';
}
/**
* Create a new TinyMCE instance.
2021-12-07 22:14:12 -08:00
*
* @param Language $language Instance of language class to use for translation.
* @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
*
* @return string Script to render on page
*/
public static function createTinyEditor(Language $language, string $name, ?string $content = null, bool $mentions = false, bool $admin = false): string
{
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 = [];
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()))
2022-03-30 21:15:17 -07:00
resolve(results);
});
});
},
onAction: function (autocompleteApi, rng, value) {
editor.selection.setRng(rng);
editor.insertContent(value);
autocompleteApi.hide();
},
});
});
}());
})();
";
}
$js .= '
2022-03-26 10:16:18 -07:00
tinymce.init({
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',
],
external_plugins: {
'spoiler': '" . (defined('CONFIG_PATH') ? CONFIG_PATH : '') . "/core/assets/plugins/tinymce_spoiler/plugin.min.js',
},
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',
skin: '$skin'," .
($content ?
'
setup: (editor) => {
editor.on(\'init\', () => {
editor.setContent(' . json_encode($content) . ');
});
},
'
: '') . "
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);
},
" . ($admin ? 'valid_children: "+body[style],+body[link],+*[*]",' : '') . '
extended_valid_elements: ' . ($admin ?
'"script[src|async|defer|type|charset],+@[data-options]"'
: 'undefined') . '
2022-03-26 10:16:18 -07:00
});
';
2022-03-30 21:15:17 -07:00
return $js;
2020-12-13 19:38:04 +01:00
}
}