This commit is contained in:
2026-05-13 17:11:09 +02:00
commit ea63897455
2785 changed files with 359868 additions and 0 deletions

View File

@@ -0,0 +1,194 @@
<?php
class Snippets
{
public $snippets;
public $total_snippets;
private $xml_file;
private $xml;
private $allow_edit = false;
private $allow_script = false;
private $allow_php = false;
private $script_found = false;
private $php_found = false;
public function __construct($xml_file, $allow_edit)
{
$this->allow_edit = $allow_edit;
$this->xml_file = $xml_file;
}
public function getSnippets()
{
$this->xml = simplexml_load_file($this->xml_file);
$this->snippets = $this->xml->snippet;
$this->total_snippets = count($this->snippets);
}
public function render()
{
$html = '';
if (empty($this->total_snippets)) {
$html .= '<div class="col-xs-12">' . " \n";
$html .= '<p>&nbsp;</p><p>' . NO_SNIPPET_TO_DISPLAY . '</p>' . " \n";
$html .= '</div>' . " \n";
} else {
for ($i=0; $i < $this->total_snippets; $i++) {
$snp = $this->snippets[$i];
$html .= '<div class="col-sm-6">' . " \n";
$html .= ' <div class="text-center">' . " \n";
$html .= ' <div class="choice selector select-snippet" data-index="' . $i . '">' . " \n";
$html .= ' ' . $snp->title;
$html .= ' </div>' . " \n";
$html .= ' </div>' . " \n";
$html .= '</div>' . " \n";
$html .= '<div class="hidden" id="content-' . $i . '">' . " \n";
$html .= htmlspecialchars_decode($snp->content);
$html .= '</div>' . " \n";
}
}
if ($this->allow_edit == 'true') {
$html .= '<div class="col-sm-6">' . " \n";
$html .= ' <div class="text-center">' . " \n";
$html .= ' <button class="btn btn-primary" id="add-new-snippet-btn">' . ADD_NEW_SNIPPET . '</button>' . " \n";
$html .= ' </div>' . " \n";
$html .= '</div>' . " \n";
}
return $html;
}
public function addNewSnippet($title, $content)
{
if (!empty($title) && !empty($content)) {
libxml_use_internal_errors(true); // avoid warnings if using html5 tags with $dom->loadXML
if ($this->allow_php == false) {
$title = $this->removePhp($title);
$content = $this->removePhp($content);
}
if ($this->allow_script == false) {
$title = $this->removeScripts($title);
$content = $this->removeScripts($content);
}
$xml = $this->xml;
$total_snippets = $this->total_snippets;
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$new_snippet = $dom->createElement('snippet');
$new_title = $dom->createElement('title');
$new_content = $dom->createElement('content');
$title_text = $dom->createTextNode($title);
$content_text = $dom->createTextNode($content);
$new_title->appendChild($title_text);
$new_content->appendChild($content_text);
$new_snippet->appendChild($new_title);
$new_snippet->appendChild($new_content);
$dom->documentElement->appendChild($new_snippet);
$dom->save($this->xml_file);
$this->getSnippets();
if ($this->script_found == true) {
return 'script_forbidden';
} elseif ($this->php_found == true) {
return 'php_forbidden';
} else {
return true;
}
} else {
return false;
}
}
public function editSnippet($index, $title, $content)
{
libxml_use_internal_errors(true); // avoid warnings if using html5 tags with $dom->loadXML
if ($this->allow_php == false) {
$title = $this->removePhp($title);
$content = $this->removePhp($content);
}
if ($this->allow_script == false) {
$title = $this->removeScripts($title);
$content = $this->removeScripts($content);
}
$xml = $this->xml;
$total_snippets = $this->total_snippets;
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$new_snippet = $dom->createElement('snippet');
$new_title = $dom->createElement('title');
$new_content = $dom->createElement('content');
$title_text = $dom->createTextNode($title);
$content_text = $dom->createTextNode($content);
$new_title->appendChild($title_text);
$new_content->appendChild($content_text);
$new_snippet->appendChild($new_title);
$new_snippet->appendChild($new_content);
$old_snippet = $dom->documentElement->getElementsByTagName('snippet')->Item($index);
$dom->documentElement->replaceChild($new_snippet, $old_snippet);
$dom->save($this->xml_file);
$this->getSnippets();
if ($this->script_found == true) {
return 'script_forbidden';
} elseif ($this->php_found == true) {
return 'php_forbidden';
} else {
return true;
}
}
public function deleteSnippet($index)
{
libxml_use_internal_errors(true); // avoid warnings if using html5 tags with $dom->loadXML
$xml = $this->xml;
$total_snippets = $this->total_snippets;
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
$old_snippet = $dom->documentElement->getElementsByTagName('snippet')->Item($index);
$dom->documentElement->removeChild($old_snippet);
$dom->save($this->xml_file);
$this->getSnippets();
return true;
}
/**
* Removes unwanted script tags from snippet
* @param $snippet_tag
* @return $snippet_tag
*/
private function removeScripts($element)
{
$dom = new DOMDocument();
$dom->loadHtml($element);
$xpath = new DOMXPath($dom);
while ($node = $xpath->query('//script')->item(0)) {
$node->parentNode->removeChild($node);
$this->script_found = true;
}
return preg_replace('/^<!DOCTYPE.+?>/', '', str_replace(array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $dom->saveHTML()));
}
/**
* Removes unwanted php scripts from snippet
* @param $element title | content
* @return $element element cleaned
*/
private function removePhp($element)
{
if (preg_match_all('/<\?php(.+?)\?>/is', $element, $out)) {
$this->php_found = true;
$element = preg_replace('/<\?php(.+?)\?>/is', '', $element);
}
return $element;
}
}

View File

@@ -0,0 +1,39 @@
<?php
$error = false;
$data['snippetsList'] = '';
$data['totalSnippets'] = '';
$data['returnMsg'] = '';
$data['returnDangerMsg'] = '';
if (file_exists('../langs/' . $_POST['language'] . '.php')) {
$lang = $_POST['language'];
} else { // default
$lang = 'en_EN';
}
require_once '../langs/' . $lang . '.php';
require_once 'Snippets.php';
$snippets = new Snippets('snippets.xml', true);
$snippets->getSnippets();
if (!isset($_POST['title']) || !isset($_POST['code']) || !preg_match('`[a-zA-Z0-9_ -]{1,150}`', $_POST['title'])) {
$error = true;
if (!preg_match('`[a-zA-Z0-9_ -]{1,150}`', $_POST['title'])) {
$return_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . TITLE_MUST_MATCH . '</div>';
} else {
$return_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . WRONG_DATA . '</div>';
}
} else {
$out = $snippets->addNewSnippet(utf8_decode(urldecode($_POST['title'])), utf8_decode(urldecode($_POST['code'])));
$return_msg = '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . SNIPPET_ADDED . '</div>';
$return_danger_msg = '';
if ($out === 'script_forbidden') {
$return_danger_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . SCRIPT_FORBIDDEN . '</div>';
} elseif ($out === 'php_forbidden') {
$return_danger_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . PHP_FORBIDDEN . '</div>';
}
}
// if ($error == false) {
$data['snippetsList'] = $snippets->render();
$data['totalSnippets'] = $snippets->total_snippets;
// }
$data['returnMsg'] = $return_msg;
$data['returnDangerMsg'] = $return_danger_msg;
echo json_encode($data);

View File

@@ -0,0 +1,26 @@
<?php
$error = false;
$data['snippetsList'] = '';
$data['totalSnippets'] = '';
if (file_exists('../langs/' . $_GET['language'] . '.php')) {
$lang = $_GET['language'];
} else { // default
$lang = 'en_EN';
}
require_once '../langs/' . $lang . '.php';
if (!isset($_GET['index']) || !is_numeric($_GET['index'])) {
$error = true;
$return_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . WRONG_DATA . '</div>';
} else {
require_once 'Snippets.php';
$snippets = new Snippets('snippets.xml', true);
$snippets->getSnippets();
$out = $snippets->deleteSnippet($_GET['index']);
$return_msg = '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . SNIPPET_DELETED . '</div>';
}
if ($error == false) {
$data['snippetsList'] = $snippets->render();
$data['totalSnippets'] = $snippets->total_snippets;
}
$data['returnMsg'] = $return_msg;
echo json_encode($data);

View File

@@ -0,0 +1,39 @@
<?php
$error = false;
$data['snippetsList'] = '';
$data['totalSnippets'] = '';
$data['returnMsg'] = '';
$data['returnDangerMsg'] = '';
if (file_exists('../langs/' . $_POST['language'] . '.php')) {
$lang = $_POST['language'];
} else { // default
$lang = 'en_EN';
}
require_once '../langs/' . $lang . '.php';
require_once 'Snippets.php';
$snippets = new Snippets('snippets.xml', true);
$snippets->getSnippets();
if (!isset($_POST['index']) || !is_numeric($_POST['index']) || !isset($_POST['title']) || !isset($_POST['code']) || !preg_match('`[a-zA-Z0-9_ -]{1,150}`', $_POST['title'])) {
$error = true;
if (!preg_match('`[a-zA-Z0-9_ -]{1,150}`', $_POST['title'])) {
$return_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . TITLE_MUST_MATCH . '</div>';
} else {
$return_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . WRONG_DATA . '</div>';
}
} else {
$out = $snippets->editSnippet($_POST['index'], utf8_decode(urldecode($_POST['title'])), utf8_decode(urldecode($_POST['code'])));
$return_msg = '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . SNIPPET_UPDATED . '</div>';
$return_danger_msg = '';
if ($out === 'script_forbidden') {
$return_danger_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . SCRIPT_FORBIDDEN . '</div>';
} elseif ($out === 'php_forbidden') {
$return_danger_msg = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' . PHP_FORBIDDEN . '</div>';
}
}
// if ($error == false) {
$data['snippetsList'] = $snippets->render();
$data['totalSnippets'] = $snippets->total_snippets;
// }
$data['returnMsg'] = $return_msg;
$data['returnDangerMsg'] = $return_danger_msg;
echo json_encode($data);

File diff suppressed because one or more lines are too long