2014-09-04 17:52:18 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This software is governed by the CeCILL-B license. If a copy of this license
|
|
|
|
* is not distributed with this file, you can obtain one at
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
|
|
|
|
*
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Raphaël DROZ
|
2016-08-04 22:26:37 +02:00
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
2014-09-04 17:52:18 +02:00
|
|
|
*
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* Ce logiciel est régi par la licence CeCILL-B. Si une copie de cette licence
|
|
|
|
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
|
|
|
|
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
|
|
|
|
*
|
|
|
|
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Raphaël DROZ
|
|
|
|
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
|
|
|
*/
|
|
|
|
namespace Framadate;
|
|
|
|
|
2018-02-20 19:05:24 +01:00
|
|
|
use Parsedown;
|
|
|
|
|
2015-01-01 23:24:17 +01:00
|
|
|
class Utils {
|
2014-12-03 14:30:25 +01:00
|
|
|
/**
|
|
|
|
* @return string Server name
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function get_server_name(): string
|
|
|
|
{
|
2015-10-26 15:02:02 +01:00
|
|
|
$scheme = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')) ? 'https' : 'http';
|
2018-02-28 15:15:52 +01:00
|
|
|
$port = in_array($_SERVER['SERVER_PORT'], ['80', '443'], true) ? '' : ':' . $_SERVER['SERVER_PORT'];
|
2014-12-03 21:08:08 +01:00
|
|
|
$dirname = dirname($_SERVER['SCRIPT_NAME']);
|
|
|
|
$dirname = $dirname === '\\' ? '/' : $dirname . '/';
|
2021-12-20 17:46:50 +01:00
|
|
|
$dirname = str_replace(['/admin', '/action'], '', $dirname);
|
2015-06-23 22:03:56 +02:00
|
|
|
$server_name = (defined('APP_URL') ? APP_URL : $_SERVER['SERVER_NAME']) . $port . $dirname;
|
2015-01-01 23:24:17 +01:00
|
|
|
|
2015-04-13 14:52:43 +02:00
|
|
|
return $scheme . '://' . preg_replace('#//+#', '/', $server_name);
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
|
2014-12-23 00:58:00 +01:00
|
|
|
/**
|
|
|
|
* @param string $title
|
2021-12-20 17:46:50 +01:00
|
|
|
*
|
2014-12-23 00:58:00 +01:00
|
|
|
* @deprecated
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function print_header($title = ''): void {
|
2015-04-13 11:24:44 +02:00
|
|
|
global $locale;
|
2014-09-04 17:52:18 +02:00
|
|
|
|
|
|
|
echo '<!DOCTYPE html>
|
2015-04-13 11:24:44 +02:00
|
|
|
<html lang="' . $locale . '">
|
2014-09-04 17:52:18 +02:00
|
|
|
<head>
|
2014-11-27 01:15:01 +01:00
|
|
|
<meta charset="utf-8" />';
|
2014-09-04 17:52:18 +02:00
|
|
|
|
2015-01-01 23:24:17 +01:00
|
|
|
if (!empty($title)) {
|
2014-11-27 01:15:01 +01:00
|
|
|
echo '<title>' . stripslashes($title) . ' - ' . NOMAPPLICATION . '</title>';
|
|
|
|
} else {
|
|
|
|
echo '<title>' . NOMAPPLICATION . '</title>';
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
2014-11-27 01:15:01 +01:00
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/bootstrap.min.css" />
|
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/datepicker3.css" />
|
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/style.css" />
|
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/frama.css" />
|
|
|
|
<link rel="stylesheet" href="' . self::get_server_name() . 'css/print.css" media="print" />
|
2021-12-20 17:46:50 +01:00
|
|
|
<script src="' . self::get_server_name() . 'js/jquery-3.6.0.min.js"></script>
|
2021-12-17 14:05:19 +01:00
|
|
|
<script src="' . self::get_server_name() . 'js/bootstrap.min.js"></script>
|
|
|
|
<script src="' . self::get_server_name() . 'js/bootstrap-datepicker.js"></script>';
|
2018-02-19 00:18:43 +01:00
|
|
|
if ('en' !== $locale) {
|
2016-06-18 22:14:15 +02:00
|
|
|
echo '
|
2021-12-17 14:05:19 +01:00
|
|
|
<script src="' . self::get_server_name() . 'js/locales/bootstrap-datepicker.' . $locale . '.js"></script>';
|
2016-06-18 22:14:15 +02:00
|
|
|
}
|
|
|
|
echo '
|
2021-12-17 14:05:19 +01:00
|
|
|
<script src="' . self::get_server_name() . 'js/core.js"></script>';
|
2015-09-03 08:03:37 +02:00
|
|
|
if (is_file($_SERVER['DOCUMENT_ROOT'] . "/nav/nav.js")) {
|
2021-12-17 14:05:19 +01:00
|
|
|
echo '<script src="/nav/nav.js" id="nav_js" charset="utf-8"></script><!-- /Framanav -->';
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
|
|
|
</head>
|
|
|
|
<body>
|
2014-10-21 01:31:26 +02:00
|
|
|
<div class="container ombre">';
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-02 16:52:46 +02:00
|
|
|
* Function allowing to generate poll's url
|
2021-12-20 17:46:50 +01:00
|
|
|
* @param string $id The poll's id
|
|
|
|
* @param bool $admin True to generate an admin URL, false for a public one
|
|
|
|
* @param string $vote_id (optional) The vote's unique id
|
|
|
|
* @param string|null $action
|
|
|
|
* @param string|null $action_value
|
|
|
|
* @return string The poll's URL.
|
2014-09-04 17:52:18 +02:00
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function getUrlSondage(string $id, bool $admin = false, string $vote_id = '', string $action = null, string $action_value = null): string
|
|
|
|
{
|
2015-11-05 22:43:54 +01:00
|
|
|
// URL-Encode $action_value
|
2021-12-20 17:46:50 +01:00
|
|
|
$action_value = $action_value ? self::base64url_encode($action_value) : null;
|
2015-11-05 22:43:54 +01:00
|
|
|
|
2014-09-04 17:52:18 +02:00
|
|
|
if (URL_PROPRE) {
|
|
|
|
if ($admin === true) {
|
2015-04-11 17:03:10 +02:00
|
|
|
$url = self::get_server_name() . $id . '/admin';
|
2014-09-04 17:52:18 +02:00
|
|
|
} else {
|
2015-04-13 14:52:43 +02:00
|
|
|
$url = self::get_server_name() . $id;
|
2015-04-11 17:03:10 +02:00
|
|
|
}
|
2018-02-19 00:18:43 +01:00
|
|
|
if ($vote_id !== '') {
|
2015-04-13 14:52:43 +02:00
|
|
|
$url .= '/vote/' . $vote_id . "#edit";
|
2018-03-19 10:24:31 +01:00
|
|
|
} elseif ($action) {
|
|
|
|
if ($action_value) {
|
2015-11-30 20:26:23 +01:00
|
|
|
$url .= '/action/' . $action . '/' . $action_value;
|
|
|
|
} else {
|
|
|
|
$url .= '/action/' . $action;
|
|
|
|
}
|
2015-04-13 12:33:43 +02:00
|
|
|
}
|
2014-09-04 17:52:18 +02:00
|
|
|
} else {
|
|
|
|
if ($admin === true) {
|
2015-04-11 17:03:10 +02:00
|
|
|
$url = self::get_server_name() . 'adminstuds.php?poll=' . $id;
|
2014-09-04 17:52:18 +02:00
|
|
|
} else {
|
2015-04-11 17:03:10 +02:00
|
|
|
$url = self::get_server_name() . 'studs.php?poll=' . $id;
|
|
|
|
}
|
2018-02-19 00:18:43 +01:00
|
|
|
if ($vote_id !== '') {
|
2015-04-13 14:52:43 +02:00
|
|
|
$url .= '&vote=' . $vote_id . "#edit";
|
2018-03-29 11:29:04 +02:00
|
|
|
} elseif ($action) {
|
|
|
|
if ($action_value) {
|
2015-11-30 20:26:23 +01:00
|
|
|
$url .= '&' . $action . "=" . $action_value;
|
|
|
|
} else {
|
|
|
|
$url .= '&' . $action . "=";
|
|
|
|
}
|
2015-04-13 12:33:43 +02:00
|
|
|
}
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2014-12-17 13:52:52 +01:00
|
|
|
/**
|
|
|
|
* This method pretty prints an object to the page framed by pre tags.
|
2015-01-01 23:24:17 +01:00
|
|
|
*
|
2014-12-23 00:30:05 +01:00
|
|
|
* @param mixed $object The object to print.
|
2014-12-17 13:52:52 +01:00
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function debug($object): void
|
|
|
|
{
|
2014-12-06 19:42:01 +01:00
|
|
|
echo '<pre>';
|
|
|
|
print_r($object);
|
|
|
|
echo '</pre>';
|
|
|
|
}
|
2014-12-31 15:19:15 +01:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function table(string $tableName): string
|
|
|
|
{
|
2014-12-31 15:19:15 +01:00
|
|
|
return TABLENAME_PREFIX . $tableName;
|
|
|
|
}
|
2015-01-01 23:24:17 +01:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function markdown(string $md, bool $clear=false, bool $line=true): string
|
|
|
|
{
|
2018-02-20 19:05:24 +01:00
|
|
|
$parseDown = new Parsedown();
|
2015-01-01 23:24:17 +01:00
|
|
|
|
2016-05-11 17:38:34 +02:00
|
|
|
$parseDown
|
|
|
|
->setBreaksEnabled(true)
|
2018-04-11 17:50:49 +02:00
|
|
|
->setSafeMode(true)
|
|
|
|
;
|
2016-05-11 17:38:34 +02:00
|
|
|
|
|
|
|
if ($line) {
|
|
|
|
$html = $parseDown->line($md);
|
|
|
|
} else {
|
|
|
|
$md = preg_replace_callback(
|
|
|
|
'#( ){2,}#',
|
2021-12-20 17:46:50 +01:00
|
|
|
static function ($m) {
|
2016-05-11 17:38:34 +02:00
|
|
|
return str_repeat(' ', strlen($m[0]));
|
|
|
|
},
|
|
|
|
$md
|
|
|
|
);
|
|
|
|
$html = $parseDown->text($md);
|
2015-01-01 23:24:17 +01:00
|
|
|
}
|
|
|
|
|
2016-05-10 17:53:06 +02:00
|
|
|
$text = strip_tags($html);
|
2015-01-01 23:24:17 +01:00
|
|
|
|
2015-01-03 17:24:39 +01:00
|
|
|
return $clear ? $text : $html;
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function htmlEscape(string $html): string {
|
2015-01-17 16:20:42 +01:00
|
|
|
return htmlentities($html, ENT_HTML5 | ENT_QUOTES);
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function htmlMailEscape(string $html): string
|
|
|
|
{
|
2016-05-02 17:51:01 +02:00
|
|
|
return htmlspecialchars($html, ENT_HTML5 | ENT_QUOTES);
|
2016-05-02 17:44:14 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function csvEscape(string $text): string
|
|
|
|
{
|
|
|
|
$escaped = str_replace(['"', "\r\n", "\n"], ['""', '', ''], $text);
|
2017-01-05 18:48:46 +01:00
|
|
|
$escaped = preg_replace("/^(=|\+|\-|\@)/", "'$1", $escaped);
|
2015-01-03 17:24:39 +01:00
|
|
|
|
|
|
|
return '"' . $escaped . '"';
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function cleanFilename(string $title): string {
|
2015-01-03 17:24:39 +01:00
|
|
|
$cleaned = preg_replace('[^a-zA-Z0-9._-]', '_', $title);
|
2021-12-20 17:46:50 +01:00
|
|
|
return preg_replace(' {2,}', ' ', $cleaned);
|
2015-01-01 23:24:17 +01:00
|
|
|
}
|
2015-03-29 22:13:22 +02:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function fromPostOrDefault(string $postKey, ?string $default = '') {
|
2016-06-30 23:10:47 +02:00
|
|
|
return !empty($_POST[$postKey]) ? $_POST[$postKey] : $default;
|
2015-03-29 22:13:22 +02:00
|
|
|
}
|
2015-11-30 20:26:23 +01:00
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function base64url_encode(string $input): string
|
|
|
|
{
|
2015-11-30 20:26:23 +01:00
|
|
|
return rtrim(strtr(base64_encode($input), '+/', '-_'), '=');
|
|
|
|
}
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
public static function base64url_decode(string $input): string {
|
2018-02-19 00:18:43 +01:00
|
|
|
return base64_decode(str_pad(strtr($input, '-_', '+/'), strlen($input) % 4, '=', STR_PAD_RIGHT), true);
|
2015-11-30 20:26:23 +01:00
|
|
|
}
|
2014-09-04 17:52:18 +02:00
|
|
|
}
|