wall.deblan.org/functions.php

186 lines
5.4 KiB
PHP

<?php
$config = array(
'valid_languages' => array('php', 'html', 'xml', 'css', 'javascript', 'sql', 'actionscript', 'perl', 'asp', 'python', 'bash', 'texte', 'c', 'yaml'),
'extensions' => array('php', 'html', 'xml', 'css', 'js', 'sql', 'as', 'pl', 'asp', 'py', 'sh', 'txt', 'c', 'yml'),
'valid_lines' => array('0', '1')
);
function slugify($str)
{
$str = trim($str);
$convertedCharacters = array(
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A',
'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a',
'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O',
'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o',
'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E',
'é' => 'e', 'è' => 'e', 'ê' => 'e', 'ë' => 'e',
'Ç' => 'C', 'ç' => 'c',
'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U',
'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
'ÿ' => 'y',
'Ñ' => 'N', 'ñ' => 'n'
);
$str = strtr($str, $convertedCharacters);
$str = preg_replace('`[^a-zA-Z0-9-]+`', '-', $str);
$str = preg_replace('`\s+`', '-', $str);
$str = preg_replace('`--+`', '-', $str);
$str = trim($str, '-');
return $str;
}
function hasValidPostDatas()
{
if (empty($_POST)) {
return false;
}
if (!isset($_POST['code'])) {
return false;
}
if (trim($_POST['code']) === '' && (!isset($_FILES['file']) || (isset($_FILES['file']) && !empty($_FILES['file']['error'])))) {
return false;
}
if (!isset($_POST['line'])) {
return false;
}
return true;
}
function hasValidGetDatas()
{
if (!isset($_GET['code']) || !trim($_GET['code'])) {
return false;
}
return file_exists('datas/sources/'.str_replace(array('.', '/'), '', $_GET['code']));
}
function language_to_extension($language)
{
}
function getCode()
{
global $config;
$language = in_array($_GET['language'], $config['valid_languages']) ? $_GET['language'] : 'html';
$line = in_array($_GET['line'], $config['valid_lines']) ? $_GET['line'] : '1';
$code = $_GET['code'];
$content = file_get_contents('datas/sources/'.$code);
$glanguage = str_replace('html', 'html4strict', $language);
require_once 'geshi.php';
$geshi = new GeSHi(
$content,
$glanguage
);
if (!(bool) $line) {
$geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS);
} else {
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
}
return array(
'language' => $language,
'line' => $line,
'extension' => str_replace($config['valid_languages'], $config['extensions'], $language),
'code' => $code,
'title' => htmlspecialchars($_GET['title']),
'content' => $geshi->parse_code()
);
}
function createCode()
{
global $config;
$language = in_array($_POST['language'], $config['valid_languages']) ? $_POST['language'] : 'html';
$line = in_array($_POST['line'], $config['valid_lines']) ? $_POST['line'] : '1';
$filename = createNewFilename();
$file = 'datas/sources/'.$filename;
if (!empty($_POST['aes'])) {
$language = 'texte';
$line = '0';
}
$code = $_POST['code'];
if (!trim($_POST['code'])) {
move_uploaded_file($_FILES['file']['tmp_name'], $file);
$code = file_get_contents($file);
} else {
file_put_contents($file, $code);
}
if (in_array($language, ['html', 'xml', 'javascript', 'php']) && !empty($_POST['indent'])) {
if ($language === 'php') {
$tmp_file = $file.'.php';
copy($file, $tmp_file);
shell_exec(sprintf('php ./bin/php-cs-fixer.phar fix %s', escapeshellarg($tmp_file)));
rename($tmp_file, $file);
} elseif ($language === 'javascript') {
$tmp_file = $file.'.js';
copy($file, $tmp_file);
shell_exec(sprintf('./bin/indentation %s', escapeshellarg($tmp_file)));
rename($tmp_file, $file);
} else {
shell_exec(sprintf('./bin/indentation %s', escapeshellarg($file)));
}
}
$title = (isset($_POST['title']) && $t = trim($_POST['title'])) ? $t : '';
$link = getCodeLink($filename, $language, $line, $title);
mail(
'logs@deblan.fr',
'WALL',
$_SERVER['REMOTE_ADDR'].' '.$_SERVER['HTTP_USER_AGENT'].' https://'.$_SERVER['SERVER_NAME'].$link
);
if (!empty($_POST['aes'])) {
$link.= '#aes='.$_POST['aes'];
}
header('location: '.$link);
}
function createNewFilename()
{
$counter = file_exists('datas/counter') ? file_get_contents('datas/counter') : 1;
$counter+= 1;
$filename= 'x'.dechex($counter);
file_put_contents('datas/counter', $counter);
return $filename;
}
function getCodeLink($filename, $language, $line, $title)
{
return '/'.implode('/', array($filename, $language, $line)).'/'.slugify($title);
}
function get_download_link($code, $title, $extension)
{
return '/download.php?code='.$code.'&extension='.$extension;
//return '/'.implode('/', array('download', $code)).'/'.slugify($title);
}
function getRawLink($code, $title)
{
return '/raw.php?code='.$code;
}