2019-12-15 10:47:18 +01:00
|
|
|
<?php
|
2021-02-14 11:53:11 +01:00
|
|
|
|
2022-10-01 16:31:20 +02:00
|
|
|
class helper
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
|
|
|
|
/** Statut de la réécriture d'URL (pour éviter de lire le contenu du fichier .htaccess à chaque self::baseUrl()) */
|
|
|
|
public static $rewriteStatus = null;
|
|
|
|
|
|
|
|
/** Filtres personnalisés */
|
|
|
|
const FILTER_BOOLEAN = 1;
|
|
|
|
const FILTER_DATETIME = 2;
|
|
|
|
const FILTER_FLOAT = 3;
|
|
|
|
const FILTER_ID = 4;
|
|
|
|
const FILTER_INT = 5;
|
|
|
|
const FILTER_MAIL = 6;
|
|
|
|
const FILTER_PASSWORD = 7;
|
|
|
|
const FILTER_STRING_LONG = 8;
|
|
|
|
const FILTER_STRING_SHORT = 9;
|
|
|
|
const FILTER_TIMESTAMP = 10;
|
|
|
|
const FILTER_URL = 11;
|
|
|
|
|
2020-06-27 16:21:22 +02:00
|
|
|
|
2022-09-02 15:36:05 +02:00
|
|
|
/**
|
|
|
|
* Traduire le message dans la langue déterminée
|
|
|
|
*/
|
|
|
|
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function translate($text)
|
|
|
|
{
|
2023-03-03 11:14:03 +01:00
|
|
|
|
|
|
|
// La traduction existe déjà dans le core
|
2023-07-19 17:56:03 +02:00
|
|
|
/*
|
2024-03-29 19:18:17 +01:00
|
|
|
if (array_key_exists($text, core::$dialog) === false && !empty($text)) {
|
|
|
|
$dialogues = json_decode(file_get_contents('core/module/install/ressource/i18n/fr_FR.json' ), true);
|
|
|
|
$data = array_merge($dialogues,[$text => '']);
|
|
|
|
file_put_contents ('core/module/install/ressource/i18n/fr_FR.json', json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT), LOCK_EX);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return (array_key_exists($text, core::$dialog) && !empty(core::$dialog[$text]) ? core::$dialog[$text] : $text);
|
2022-09-02 15:36:05 +02:00
|
|
|
}
|
2020-06-27 16:21:22 +02:00
|
|
|
|
2022-10-08 17:41:26 +02:00
|
|
|
/**
|
|
|
|
* Formate la date avec le script strftime en UTF8
|
|
|
|
* Date au format time()
|
|
|
|
* $format strftime
|
|
|
|
*/
|
2023-10-03 17:39:02 +02:00
|
|
|
public static function dateUTF8($format, $date, $locale = 'fr_FR')
|
2022-10-08 17:41:26 +02:00
|
|
|
{
|
2022-10-09 10:50:28 +02:00
|
|
|
require_once 'core/class/strftime/php-8.1-strftime.class.php';
|
2023-10-03 17:39:02 +02:00
|
|
|
return mb_convert_encoding(\PHP81_BC\strftime($format, $date, $locale), 'UTF-8', mb_list_encodings());
|
2022-10-08 17:41:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 14:21:05 +02:00
|
|
|
/**
|
|
|
|
* Fonction pour assurer la traduction des messages
|
|
|
|
*/
|
2023-03-20 11:31:53 +01:00
|
|
|
public static function googleTranslate($to, $text)
|
|
|
|
{
|
2022-11-09 15:56:53 +01:00
|
|
|
if (!file_exists('site/i18n/' . $to . '.json')) {
|
2023-03-20 11:31:53 +01:00
|
|
|
file_put_contents('site/i18n/' . $to . '.json', json_encode([]));
|
2022-10-10 10:33:29 +02:00
|
|
|
}
|
2024-03-29 19:18:17 +01:00
|
|
|
if (!empty($text)) {
|
2022-10-03 14:21:05 +02:00
|
|
|
//Lecture des données en ligne
|
2022-11-09 15:56:53 +01:00
|
|
|
$data = json_decode(file_get_contents('site/i18n/' . $to . '.json'), true);
|
2022-10-03 14:21:05 +02:00
|
|
|
// Mode traduction
|
|
|
|
if ($to !== 'fr_FR') {
|
2023-03-20 11:31:53 +01:00
|
|
|
$arrayjson = json_decode(file_get_contents('https://clients5.google.com/translate_a/t?client=dict-chrome-ex&sl=auto&tl=' . $to . '&q=' . rawurlencode($text)), true);
|
|
|
|
$response = $arrayjson[0][0];
|
2022-10-03 14:21:05 +02:00
|
|
|
// Captation
|
|
|
|
if ($data !== '') {
|
2023-03-20 11:31:53 +01:00
|
|
|
if (array_key_exists($text, $data)) {
|
2022-10-03 14:21:05 +02:00
|
|
|
$data[$text] = $response;
|
|
|
|
} else {
|
2023-03-20 11:31:53 +01:00
|
|
|
$data = array_merge($data, [$text => $response]);
|
2022-10-03 14:21:05 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-20 11:31:53 +01:00
|
|
|
// Mode alimentation des chaines
|
2022-10-03 14:21:05 +02:00
|
|
|
} else {
|
|
|
|
// Créer la variable
|
2023-03-20 11:31:53 +01:00
|
|
|
$data = array_merge($data, [$text => '']);
|
2022-10-03 14:21:05 +02:00
|
|
|
}
|
2024-04-09 17:23:26 +02:00
|
|
|
file_put_contents('site/i18n/' . $to . '.json', json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
|
2022-10-03 14:21:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-14 11:53:11 +01:00
|
|
|
/**
|
2021-04-22 20:39:33 +02:00
|
|
|
* Récupérer l'adresse IP sans tenir compte du proxy
|
2021-10-11 17:07:09 +02:00
|
|
|
* @param integer Niveau d'anonymat 0 aucun, 1 octet à droite, etc..
|
2020-06-27 16:21:22 +02:00
|
|
|
* @return string IP adress
|
2021-04-22 20:39:33 +02:00
|
|
|
* Cette fonction est utilisée par user
|
2022-10-01 16:31:20 +02:00
|
|
|
*/
|
2020-06-27 16:21:22 +02:00
|
|
|
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function getIp($anon = 4)
|
|
|
|
{
|
2024-03-29 19:18:17 +01:00
|
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
2022-10-01 16:31:20 +02:00
|
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
2024-03-29 19:18:17 +01:00
|
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
2022-10-01 16:31:20 +02:00
|
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
|
} else {
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
2020-06-27 16:21:22 +02:00
|
|
|
}
|
2021-10-11 17:54:19 +02:00
|
|
|
|
2021-10-11 17:53:41 +02:00
|
|
|
// Anonymiser l'adresse IP v4
|
|
|
|
$d = array_slice(explode('.', $ip), 0, $anon);
|
2022-10-01 16:31:20 +02:00
|
|
|
$d = implode('.', $d);
|
2021-10-11 17:53:41 +02:00
|
|
|
$j = array_fill(0, 4 - $anon, 'x');
|
2022-10-01 16:31:20 +02:00
|
|
|
$k = implode('.', $j);
|
2021-10-11 17:53:41 +02:00
|
|
|
$ip = count($j) == 0 ? $d : $d . '.' . $k;
|
2020-06-27 16:21:22 +02:00
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:28:44 +02:00
|
|
|
/**
|
2021-03-10 13:37:13 +01:00
|
|
|
* Fonction pour récupérer le numéro de version en ligne et le catalogue des modules
|
2020-04-24 09:28:44 +02:00
|
|
|
* @param string $url à récupérer
|
|
|
|
* @return mixed données récupérées
|
|
|
|
*/
|
|
|
|
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function getUrlContents($url)
|
|
|
|
{
|
2020-05-28 07:19:06 +02:00
|
|
|
// Ejecter free.fr
|
2022-10-01 16:31:20 +02:00
|
|
|
if (strpos(self::baseUrl(), 'free.fr') > 0) {
|
2020-05-28 07:19:06 +02:00
|
|
|
return false;
|
2020-04-24 09:28:44 +02:00
|
|
|
}
|
2022-10-01 16:31:20 +02:00
|
|
|
if (
|
|
|
|
function_exists('file_get_contents') &&
|
|
|
|
ini_get('allow_url_fopen')
|
|
|
|
) {
|
|
|
|
$url_get_contents_data = @file_get_contents($url); // Masque un warning éventuel
|
|
|
|
} elseif (function_exists('curl_version')) {
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
$url_get_contents_data = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
} elseif (
|
|
|
|
function_exists('fopen') &&
|
|
|
|
function_exists('stream_get_contents') &&
|
|
|
|
ini_get('allow_url_fopen')
|
|
|
|
) {
|
|
|
|
$handle = fopen($url, "r");
|
|
|
|
$url_get_contents_data = stream_get_contents($handle);
|
|
|
|
} else {
|
|
|
|
$url_get_contents_data = false;
|
|
|
|
}
|
2020-05-28 07:19:06 +02:00
|
|
|
return $url_get_contents_data;
|
|
|
|
}
|
2020-04-24 09:28:44 +02:00
|
|
|
|
2019-12-15 10:47:18 +01:00
|
|
|
/**
|
|
|
|
* Retourne les valeurs d'une colonne du tableau de données
|
|
|
|
* @param array $array Tableau cible
|
|
|
|
* @param string $column Colonne à extraire
|
|
|
|
* @param string $sort Type de tri à appliquer au tableau (SORT_ASC, SORT_DESC, ou null)
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function arraycolumn($array, $column, $sort = null)
|
|
|
|
{
|
2020-05-28 07:19:06 +02:00
|
|
|
$newArray = [];
|
2024-03-29 19:18:17 +01:00
|
|
|
if (empty($array) === false) {
|
2022-10-01 16:31:20 +02:00
|
|
|
$newArray = array_map(function ($element) use ($column) {
|
2019-12-15 10:47:18 +01:00
|
|
|
return $element[$column];
|
|
|
|
}, $array);
|
2022-10-01 16:31:20 +02:00
|
|
|
switch ($sort) {
|
2019-12-15 10:47:18 +01:00
|
|
|
case 'SORT_ASC':
|
|
|
|
asort($newArray);
|
|
|
|
break;
|
|
|
|
case 'SORT_DESC':
|
|
|
|
arsort($newArray);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $newArray;
|
|
|
|
}
|
|
|
|
|
2022-04-06 09:38:58 +02:00
|
|
|
/**
|
|
|
|
* Compatibilité avec les anciens modules
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function arrayCollumn($array, $column, $sort = null)
|
|
|
|
{
|
2022-04-06 09:38:58 +02:00
|
|
|
return (helper::arrayColumn($array, $column, $sort));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-06 21:04:56 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-02 18:49:24 +02:00
|
|
|
* Génère un backup des données de site
|
2020-02-06 21:04:56 +01:00
|
|
|
* @param string $folder dossier de sauvegarde
|
|
|
|
* @param array $exclude dossier exclus
|
|
|
|
* @return string nom du fichier de sauvegarde
|
|
|
|
*/
|
|
|
|
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function autoBackup($folder, $filter = ['backup', 'tmp'])
|
|
|
|
{
|
2020-02-06 21:04:56 +01:00
|
|
|
// Creation du ZIP
|
2022-10-01 16:31:20 +02:00
|
|
|
$baseName = str_replace('/', '', helper::baseUrl(false, false));
|
2024-03-29 19:18:17 +01:00
|
|
|
$baseName = empty($baseName) ? 'ZwiiCMS' : $baseName;
|
2023-03-20 11:31:53 +01:00
|
|
|
$fileName = $baseName . '-backup-' . date('Y-m-d-H-i-s', time()) . '.zip';
|
2020-02-06 21:04:56 +01:00
|
|
|
$zip = new ZipArchive();
|
|
|
|
$zip->open($folder . $fileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
|
|
|
|
$directory = 'site/';
|
|
|
|
//$filter = array('backup','tmp','file');
|
2023-03-20 11:31:53 +01:00
|
|
|
$files = new RecursiveIteratorIterator(
|
2020-02-06 21:04:56 +01:00
|
|
|
new RecursiveCallbackFilterIterator(
|
2022-10-01 16:31:20 +02:00
|
|
|
new RecursiveDirectoryIterator(
|
|
|
|
$directory,
|
|
|
|
RecursiveDirectoryIterator::SKIP_DOTS
|
|
|
|
),
|
|
|
|
function ($fileInfo, $key, $iterator) use ($filter) {
|
|
|
|
return $fileInfo->isFile() || !in_array($fileInfo->getBaseName(), $filter);
|
|
|
|
}
|
2020-02-06 21:04:56 +01:00
|
|
|
)
|
2022-10-01 16:31:20 +02:00
|
|
|
);
|
|
|
|
foreach ($files as $name => $file) {
|
|
|
|
if (!$file->isDir()) {
|
2020-02-06 21:04:56 +01:00
|
|
|
$filePath = $file->getRealPath();
|
|
|
|
$relativePath = substr($filePath, strlen(realpath($directory)) + 1);
|
|
|
|
$zip->addFile($filePath, $relativePath);
|
2020-05-28 07:19:06 +02:00
|
|
|
}
|
2020-02-06 21:04:56 +01:00
|
|
|
}
|
|
|
|
$zip->close();
|
|
|
|
return ($fileName);
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:04:24 +01:00
|
|
|
|
|
|
|
|
2021-02-14 11:53:11 +01:00
|
|
|
/**
|
2021-02-09 18:04:24 +01:00
|
|
|
* Retourne la liste des modules installés dans un tableau composé
|
|
|
|
* du nom réel
|
|
|
|
* du numéro de version
|
|
|
|
*/
|
2023-03-20 11:31:53 +01:00
|
|
|
public static function getModules()
|
2022-10-01 16:31:20 +02:00
|
|
|
{
|
2021-02-14 11:53:11 +01:00
|
|
|
$modules = array();
|
2021-02-15 14:37:17 +01:00
|
|
|
$dirs = array_diff(scandir('module'), array('..', '.'));
|
2021-02-15 11:15:52 +01:00
|
|
|
foreach ($dirs as $key => $value) {
|
|
|
|
// Dossier non vide
|
2021-02-15 14:37:17 +01:00
|
|
|
if (file_exists('module/' . $value . '/' . $value . '.php')) {
|
2021-02-15 11:15:52 +01:00
|
|
|
// Lire les constantes en gérant les erreurs de nom de classe
|
2022-10-01 16:31:20 +02:00
|
|
|
try {
|
2021-02-15 11:15:52 +01:00
|
|
|
$class_reflex = new \ReflectionClass($value);
|
|
|
|
$class_constants = $class_reflex->getConstants();
|
|
|
|
// Constante REALNAME
|
|
|
|
if (array_key_exists('REALNAME', $class_constants)) {
|
2022-10-01 16:31:20 +02:00
|
|
|
$realName = $value::REALNAME;
|
2021-02-15 11:15:52 +01:00
|
|
|
} else {
|
2022-10-01 16:31:20 +02:00
|
|
|
$realName = ucfirst($value);
|
2021-02-11 10:31:58 +01:00
|
|
|
}
|
2021-02-15 11:15:52 +01:00
|
|
|
// Constante VERSION
|
|
|
|
if (array_key_exists('VERSION', $class_constants)) {
|
2022-10-01 16:31:20 +02:00
|
|
|
$version = $value::VERSION;
|
2021-02-15 11:15:52 +01:00
|
|
|
} else {
|
2022-10-01 16:31:20 +02:00
|
|
|
$version = '0.0';
|
2021-02-15 11:15:52 +01:00
|
|
|
}
|
2021-02-15 13:38:23 +01:00
|
|
|
// Constante UPDATE
|
|
|
|
if (array_key_exists('UPDATE', $class_constants)) {
|
2022-10-01 16:31:20 +02:00
|
|
|
$update = $value::UPDATE;
|
2021-02-15 13:38:23 +01:00
|
|
|
} else {
|
2022-10-01 16:31:20 +02:00
|
|
|
$update = '0.0';
|
2021-02-15 13:38:23 +01:00
|
|
|
}
|
|
|
|
// Constante DELETE
|
|
|
|
if (array_key_exists('DELETE', $class_constants)) {
|
2022-10-01 16:31:20 +02:00
|
|
|
$delete = $value::DELETE;
|
2021-02-15 13:38:23 +01:00
|
|
|
} else {
|
2022-10-01 16:31:20 +02:00
|
|
|
$delete = true;
|
2021-02-15 13:38:23 +01:00
|
|
|
}
|
|
|
|
// Constante DATADIRECTORY
|
2022-10-01 16:31:20 +02:00
|
|
|
if (array_key_exists('DATADIRECTORY', $class_constants)) {
|
|
|
|
$dataDirectory = $value::DATADIRECTORY;
|
2021-02-15 13:38:23 +01:00
|
|
|
} else {
|
2022-10-01 16:31:20 +02:00
|
|
|
$dataDirectory = '';
|
2021-02-15 13:38:23 +01:00
|
|
|
}
|
2021-02-15 11:15:52 +01:00
|
|
|
// Affection
|
2023-03-20 11:31:53 +01:00
|
|
|
$modules[$value] = [
|
2022-12-20 12:19:42 +01:00
|
|
|
'name' => $value,
|
2022-10-01 16:31:20 +02:00
|
|
|
'realName' => $realName,
|
|
|
|
'version' => $version,
|
|
|
|
'update' => $update,
|
|
|
|
'delete' => $delete,
|
|
|
|
'dataDirectory' => $dataDirectory
|
2021-02-15 11:15:52 +01:00
|
|
|
];
|
2022-10-01 16:31:20 +02:00
|
|
|
} catch (Exception $e) {
|
2021-02-15 11:15:52 +01:00
|
|
|
// on ne fait rien
|
2021-02-14 11:53:11 +01:00
|
|
|
}
|
2021-02-09 18:04:24 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-01 16:31:20 +02:00
|
|
|
return ($modules);
|
2021-02-09 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:59:17 +01:00
|
|
|
|
2021-02-09 18:04:24 +01:00
|
|
|
|
2019-12-15 10:47:18 +01:00
|
|
|
/**
|
2020-07-13 10:26:16 +02:00
|
|
|
* Retourne true si le protocole est en TLS
|
|
|
|
* @return bool
|
2019-12-15 10:47:18 +01:00
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function isHttps()
|
|
|
|
{
|
|
|
|
if (
|
2024-03-29 19:18:17 +01:00
|
|
|
(empty($_SERVER['HTTPS']) === false and $_SERVER['HTTPS'] !== 'off')
|
2022-10-01 16:31:20 +02:00
|
|
|
or $_SERVER['SERVER_PORT'] === 443
|
2019-12-15 10:47:18 +01:00
|
|
|
) {
|
2020-07-13 10:26:16 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
2020-07-13 10:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retourne l'URL de base du site
|
|
|
|
* @param bool $queryString Affiche ou non le point d'interrogation
|
|
|
|
* @param bool $host Affiche ou non l'host
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function baseUrl($queryString = true, $host = true)
|
|
|
|
{
|
2020-07-13 10:26:16 +02:00
|
|
|
// Protocole
|
|
|
|
$protocol = helper::isHttps() === true ? 'https://' : 'http://';
|
2019-12-15 10:47:18 +01:00
|
|
|
// Host
|
2022-10-01 16:31:20 +02:00
|
|
|
if ($host) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$host = $protocol . $_SERVER['HTTP_HOST'];
|
|
|
|
}
|
|
|
|
// Pathinfo
|
|
|
|
$pathInfo = pathinfo($_SERVER['PHP_SELF']);
|
|
|
|
// Querystring
|
2022-10-01 16:31:20 +02:00
|
|
|
if ($queryString and helper::checkRewrite() === false) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$queryString = '?';
|
2022-10-01 16:31:20 +02:00
|
|
|
} else {
|
2019-12-15 10:47:18 +01:00
|
|
|
$queryString = '';
|
|
|
|
}
|
2020-05-22 16:06:34 +02:00
|
|
|
return $host . rtrim($pathInfo['dirname'], ' ' . DIRECTORY_SEPARATOR) . '/' . $queryString;
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check le statut de l'URL rewriting
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function checkRewrite()
|
|
|
|
{
|
2022-04-28 11:17:51 +02:00
|
|
|
// N'interroge que le serveur Apache
|
|
|
|
if (strpos($_SERVER["SERVER_SOFTWARE"], 'Apache') > 0) {
|
2024-06-09 02:51:52 +02:00
|
|
|
self::$rewriteStatus = false;
|
|
|
|
} else {
|
2019-12-15 10:47:18 +01:00
|
|
|
// Ouvre et scinde le fichier .htaccess
|
|
|
|
$htaccess = explode('# URL rewriting', file_get_contents('.htaccess'));
|
|
|
|
// Retourne un boolean en fonction du contenu de la partie réservée à l'URL rewriting
|
2024-06-09 02:51:52 +02:00
|
|
|
self::$rewriteStatus = (strpos($htaccess[1], 'RewriteEngine on') !== false);
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
|
|
|
return self::$rewriteStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renvoie le numéro de version de Zwii est en ligne
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-02-06 21:43:40 +01:00
|
|
|
public static function getOnlineVersion($channel)
|
2022-10-01 16:31:20 +02:00
|
|
|
{
|
2023-02-06 21:43:40 +01:00
|
|
|
return (helper::getUrlContents(common::ZWII_UPDATE_URL . $channel . '/version'));
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check si une nouvelle version de Zwii est disponible
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-02-06 21:43:40 +01:00
|
|
|
public static function checkNewVersion($channel)
|
2022-10-01 16:31:20 +02:00
|
|
|
{
|
2023-02-06 21:43:40 +01:00
|
|
|
$version = helper::getOnlineVersion($channel);
|
2023-09-22 18:06:58 +02:00
|
|
|
$update = false;
|
2024-03-29 19:18:17 +01:00
|
|
|
if (!empty($version)) {
|
|
|
|
$update = version_compare(common::ZWII_VERSION, $version) == -1;
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
2023-09-22 18:06:58 +02:00
|
|
|
return $update;
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Génère des variations d'une couleur
|
|
|
|
* @param string $rgba Code rgba de la couleur
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function colorVariants($rgba)
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
preg_match('#\(+(.*)\)+#', $rgba, $matches);
|
|
|
|
$rgba = explode(', ', $matches[1]);
|
|
|
|
return [
|
|
|
|
'normal' => 'rgba(' . $rgba[0] . ',' . $rgba[1] . ',' . $rgba[2] . ',' . $rgba[3] . ')',
|
|
|
|
'darken' => 'rgba(' . max(0, $rgba[0] - 15) . ',' . max(0, $rgba[1] - 15) . ',' . max(0, $rgba[2] - 15) . ',' . $rgba[3] . ')',
|
|
|
|
'veryDarken' => 'rgba(' . max(0, $rgba[0] - 20) . ',' . max(0, $rgba[1] - 20) . ',' . max(0, $rgba[2] - 20) . ',' . $rgba[3] . ')',
|
2020-11-01 19:00:24 +01:00
|
|
|
'text' => self::relativeLuminanceW3C($rgba) > .22 ? "#222" : "#DDD",
|
2023-03-20 11:31:53 +01:00
|
|
|
'rgb' => 'rgb(' . $rgba[0] . ',' . $rgba[1] . ',' . $rgba[2] . ')',
|
2022-10-01 16:31:20 +02:00
|
|
|
'invert' => 'rgba (' .
|
2024-03-29 19:18:17 +01:00
|
|
|
($rgba[0] < 128 ? 255 : 0) . ',' .
|
|
|
|
($rgba[1] < 128 ? 255 : 0) . ',' .
|
|
|
|
($rgba[1] < 128 ? 255 : 0) . ',' .
|
|
|
|
($rgba[0] < 128 ? 255 : 0) . ')'
|
2019-12-15 10:47:18 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Supprime un cookie
|
|
|
|
* @param string $cookieKey Clé du cookie à supprimer
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function deleteCookie($cookieKey)
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
unset($_COOKIE[$cookieKey]);
|
2020-07-12 17:50:09 +02:00
|
|
|
setcookie($cookieKey, '', time() - 3600, helper::baseUrl(false, false), '', false, true);
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filtre une chaîne en fonction d'un tableau de données
|
|
|
|
* @param string $text Chaîne à filtrer
|
|
|
|
* @param int $filter Type de filtre à appliquer
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function filter($text, $filter)
|
|
|
|
{
|
2022-10-05 10:36:13 +02:00
|
|
|
$text = is_null($text) ? $text : trim($text);
|
2022-10-01 16:31:20 +02:00
|
|
|
switch ($filter) {
|
2019-12-15 10:47:18 +01:00
|
|
|
case self::FILTER_BOOLEAN:
|
|
|
|
$text = (bool) $text;
|
|
|
|
break;
|
|
|
|
case self::FILTER_DATETIME:
|
|
|
|
$timezone = new DateTimeZone(core::$timezone);
|
|
|
|
$date = new DateTime($text);
|
|
|
|
$date->setTimezone($timezone);
|
|
|
|
$text = (int) $date->format('U');
|
|
|
|
break;
|
|
|
|
case self::FILTER_FLOAT:
|
|
|
|
$text = filter_var($text, FILTER_SANITIZE_NUMBER_FLOAT);
|
|
|
|
$text = (float) $text;
|
|
|
|
break;
|
|
|
|
case self::FILTER_ID:
|
|
|
|
$text = mb_strtolower($text, 'UTF-8');
|
2023-03-20 11:31:53 +01:00
|
|
|
$text = strip_tags(
|
|
|
|
str_replace(
|
2024-03-29 19:18:17 +01:00
|
|
|
explode(',', 'á,à,â,ä,ã,å,ç,é,è,ê,ë,í,ì,î,ï,ñ,ó,ò,ô,ö,õ,ú,ù,û,ü,ý,ÿ,\',", '),
|
|
|
|
explode(',', 'a,a,a,a,a,a,c,e,e,e,e,i,i,i,i,n,o,o,o,o,o,u,u,u,u,y,y,-,-,-'),
|
2023-03-20 11:31:53 +01:00
|
|
|
$text
|
|
|
|
)
|
|
|
|
);
|
2019-12-15 10:47:18 +01:00
|
|
|
$text = preg_replace('/([^a-z0-9-])/', '', $text);
|
2020-04-26 19:06:10 +02:00
|
|
|
// Supprime les emoji
|
|
|
|
$text = preg_replace('/[[:^print:]]/', '', $text);
|
|
|
|
// Supprime les tirets en fin de chaine (emoji en fin de nom)
|
2022-10-01 16:31:20 +02:00
|
|
|
$text = rtrim($text, '-');
|
2019-12-15 10:47:18 +01:00
|
|
|
// Cas où un identifiant est vide
|
2024-03-29 19:18:17 +01:00
|
|
|
if (empty($text)) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$text = uniqid('');
|
|
|
|
}
|
|
|
|
// Un ID ne peut pas être un entier, pour éviter les conflits avec le système de pagination
|
2022-10-01 16:31:20 +02:00
|
|
|
if (intval($text) !== 0) {
|
2021-01-03 18:52:36 +01:00
|
|
|
$text = '_' . $text;
|
2020-05-28 07:19:06 +02:00
|
|
|
}
|
2019-12-15 10:47:18 +01:00
|
|
|
break;
|
|
|
|
case self::FILTER_INT:
|
|
|
|
$text = (int) filter_var($text, FILTER_SANITIZE_NUMBER_INT);
|
|
|
|
break;
|
|
|
|
case self::FILTER_MAIL:
|
|
|
|
$text = filter_var($text, FILTER_SANITIZE_EMAIL);
|
|
|
|
break;
|
|
|
|
case self::FILTER_PASSWORD:
|
|
|
|
$text = password_hash($text, PASSWORD_BCRYPT);
|
|
|
|
break;
|
|
|
|
case self::FILTER_STRING_LONG:
|
2022-10-04 08:40:55 +02:00
|
|
|
$text = mb_substr(filter_var($text, FILTER_SANITIZE_FULL_SPECIAL_CHARS), 0, 500000);
|
2019-12-15 10:47:18 +01:00
|
|
|
break;
|
|
|
|
case self::FILTER_STRING_SHORT:
|
2022-10-04 08:40:55 +02:00
|
|
|
$text = mb_substr(filter_var($text, FILTER_SANITIZE_FULL_SPECIAL_CHARS), 0, 500);
|
2019-12-15 10:47:18 +01:00
|
|
|
break;
|
|
|
|
case self::FILTER_TIMESTAMP:
|
|
|
|
$text = date('Y-m-d H:i:s', $text);
|
|
|
|
break;
|
|
|
|
case self::FILTER_URL:
|
|
|
|
$text = filter_var($text, FILTER_SANITIZE_URL);
|
|
|
|
break;
|
|
|
|
}
|
2020-04-29 13:38:01 +02:00
|
|
|
return $text;
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Incrémente une clé en fonction des clés ou des valeurs d'un tableau
|
|
|
|
* @param mixed $key Clé à incrémenter
|
|
|
|
* @param array $array Tableau à vérifier
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function increment($key, $array = [])
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
// Pas besoin d'incrémenter si la clef n'existe pas
|
2022-10-01 16:31:20 +02:00
|
|
|
if ($array === []) {
|
2019-12-15 10:47:18 +01:00
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
// Incrémente la clef
|
|
|
|
else {
|
|
|
|
// Si la clef est numérique elle est incrémentée
|
2022-10-01 16:31:20 +02:00
|
|
|
if (is_numeric($key)) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$newKey = $key;
|
2022-10-01 16:31:20 +02:00
|
|
|
while (array_key_exists($newKey, $array) or in_array($newKey, $array)) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$newKey++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Sinon l'incrémentation est ajoutée après la clef
|
|
|
|
else {
|
|
|
|
$i = 2;
|
|
|
|
$newKey = $key;
|
2022-10-01 16:31:20 +02:00
|
|
|
while (array_key_exists($newKey, $array) or in_array($newKey, $array)) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$newKey = $key . '-' . $i;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $newKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimise du css
|
|
|
|
* @param string $css Css à minimiser
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function minifyCss($css)
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
// Supprime les commentaires
|
|
|
|
$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
|
|
|
|
// Supprime les tabulations, espaces, nouvelles lignes, etc...
|
2022-10-01 16:31:20 +02:00
|
|
|
$css = str_replace(["\r\n", "\r", "\n", "\t", ' ', ' ', ' '], '', $css);
|
2019-12-15 10:47:18 +01:00
|
|
|
$css = preg_replace(['(( )+{)', '({( )+)'], '{', $css);
|
|
|
|
$css = preg_replace(['(( )+})', '(}( )+)', '(;( )*})'], '}', $css);
|
|
|
|
$css = preg_replace(['(;( )+)', '(( )+;)'], ';', $css);
|
2023-03-20 11:31:53 +01:00
|
|
|
// Convertir les codes entités
|
|
|
|
$css = htmlspecialchars_decode($css);
|
|
|
|
// Supprime les balises HTML
|
|
|
|
$css = strip_tags($css);
|
2019-12-15 10:47:18 +01:00
|
|
|
// Retourne le css minifié
|
|
|
|
return $css;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimise du js
|
|
|
|
* @param string $js Js à minimiser
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function minifyJs($js)
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
// Supprime les commentaires
|
|
|
|
$js = preg_replace('/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/|\s*(?<![\:\=])\/\/.*/', '', $js);
|
|
|
|
// Supprime les tabulations, espaces, nouvelles lignes, etc...
|
|
|
|
$js = str_replace(["\r\n", "\r", "\t", "\n", ' ', ' ', ' '], '', $js);
|
|
|
|
$js = preg_replace(['(( )+\))', '(\)( )+)'], ')', $js);
|
|
|
|
// Retourne le js minifié
|
|
|
|
return $js;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crée un système de pagination (retourne un tableau contenant les informations sur la pagination (first, last, pages))
|
|
|
|
* @param array $array Tableau de donnée à utiliser
|
|
|
|
* @param string $url URL à utiliser, la dernière partie doit correspondre au numéro de page, par défaut utiliser $this->getUrl()
|
|
|
|
* @param string $item pagination nombre d'éléments par page
|
|
|
|
* @param null|int $sufix Suffixe de l'url
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-05-29 18:49:50 +02:00
|
|
|
public static function pagination($array, $url, $item, $suffix = null)
|
2022-10-01 16:31:20 +02:00
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
// Scinde l'url
|
|
|
|
$url = explode('/', $url);
|
|
|
|
// Url de pagination
|
|
|
|
$urlPagination = is_numeric($url[count($url) - 1]) ? array_pop($url) : 1;
|
|
|
|
// Url de la page courante
|
|
|
|
$urlCurrent = implode('/', $url);
|
|
|
|
// Nombre d'éléments à afficher
|
|
|
|
$nbElements = count($array);
|
|
|
|
// Nombre de page
|
|
|
|
$nbPage = ceil($nbElements / $item);
|
|
|
|
// Page courante
|
|
|
|
$currentPage = is_numeric($urlPagination) ? self::filter($urlPagination, self::FILTER_INT) : 1;
|
|
|
|
// Premier élément de la page
|
|
|
|
$firstElement = ($currentPage - 1) * $item;
|
|
|
|
// Dernier élément de la page
|
|
|
|
$lastElement = $firstElement + $item;
|
|
|
|
$lastElement = ($lastElement > $nbElements) ? $nbElements : $lastElement;
|
|
|
|
// Mise en forme de la liste des pages
|
|
|
|
$pages = '';
|
2022-10-01 16:31:20 +02:00
|
|
|
if ($nbPage > 1) {
|
|
|
|
for ($i = 1; $i <= $nbPage; $i++) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$disabled = ($i === $currentPage) ? ' class="disabled"' : false;
|
2023-05-29 18:49:50 +02:00
|
|
|
$pages .= '<a href="' . helper::baseUrl() . $urlCurrent . '/' . $i . $suffix . '"' . $disabled . '>' . $i . '</a>';
|
2019-12-15 10:47:18 +01:00
|
|
|
}
|
|
|
|
$pages = '<div class="pagination">' . $pages . '</div>';
|
|
|
|
}
|
|
|
|
// Retourne un tableau contenant les informations sur la pagination
|
|
|
|
return [
|
|
|
|
'first' => $firstElement,
|
|
|
|
'last' => $lastElement,
|
|
|
|
'pages' => $pages
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calcul de la luminance relative d'une couleur
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function relativeLuminanceW3C($rgba)
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
// Conversion en sRGB
|
|
|
|
$RsRGB = $rgba[0] / 255;
|
|
|
|
$GsRGB = $rgba[1] / 255;
|
|
|
|
$BsRGB = $rgba[2] / 255;
|
|
|
|
// Ajout de la transparence
|
|
|
|
$RsRGBA = $rgba[3] * $RsRGB + (1 - $rgba[3]);
|
|
|
|
$GsRGBA = $rgba[3] * $GsRGB + (1 - $rgba[3]);
|
|
|
|
$BsRGBA = $rgba[3] * $BsRGB + (1 - $rgba[3]);
|
|
|
|
// Calcul de la luminance
|
|
|
|
$R = ($RsRGBA <= .03928) ? $RsRGBA / 12.92 : pow(($RsRGBA + .055) / 1.055, 2.4);
|
|
|
|
$G = ($GsRGBA <= .03928) ? $GsRGBA / 12.92 : pow(($GsRGBA + .055) / 1.055, 2.4);
|
|
|
|
$B = ($BsRGBA <= .03928) ? $BsRGBA / 12.92 : pow(($BsRGBA + .055) / 1.055, 2.4);
|
|
|
|
return .2126 * $R + .7152 * $G + .0722 * $B;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retourne les attributs d'une balise au bon format
|
|
|
|
* @param array $array Liste des attributs ($key => $value)
|
|
|
|
* @param array $exclude Clés à ignorer ($key)
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function sprintAttributes(array $array = [], array $exclude = [])
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
$exclude = array_merge(
|
|
|
|
[
|
|
|
|
'before',
|
|
|
|
'classWrapper',
|
|
|
|
'help',
|
|
|
|
'label'
|
|
|
|
],
|
|
|
|
$exclude
|
|
|
|
);
|
|
|
|
$attributes = [];
|
2022-10-01 16:31:20 +02:00
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
if (($value or $value === 0) and in_array($key, $exclude) === false) {
|
2019-12-15 10:47:18 +01:00
|
|
|
// Désactive le message de modifications non enregistrées pour le champ
|
2022-10-01 16:31:20 +02:00
|
|
|
if ($key === 'noDirty') {
|
2019-12-15 10:47:18 +01:00
|
|
|
$attributes[] = 'data-no-dirty';
|
|
|
|
}
|
|
|
|
// Disabled
|
|
|
|
// Readonly
|
2022-10-01 16:31:20 +02:00
|
|
|
elseif (in_array($key, ['disabled', 'readonly'])) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$attributes[] = sprintf('%s', $key);
|
|
|
|
}
|
|
|
|
// Autres
|
|
|
|
else {
|
|
|
|
$attributes[] = sprintf('%s="%s"', $key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return implode(' ', $attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retourne un segment de chaîne sans couper de mot
|
|
|
|
* @param string $text Texte à scinder
|
|
|
|
* @param int $start (voir substr de PHP pour fonctionnement)
|
|
|
|
* @param int $length (voir substr de PHP pour fonctionnement)
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-10-01 16:31:20 +02:00
|
|
|
public static function subword($text, $start, $length)
|
|
|
|
{
|
2019-12-15 10:47:18 +01:00
|
|
|
$text = trim($text);
|
2022-10-01 16:31:20 +02:00
|
|
|
if (strlen($text) > $length) {
|
2019-12-15 10:47:18 +01:00
|
|
|
$text = mb_substr($text, $start, $length);
|
|
|
|
$text = mb_substr($text, 0, min(mb_strlen($text), mb_strrpos($text, ' ')));
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:02:32 +02:00
|
|
|
/**
|
2020-06-02 18:49:24 +02:00
|
|
|
* Cryptage
|
2020-05-11 17:02:32 +02:00
|
|
|
* @param string $key la clé d'encryptage
|
2023-03-27 11:34:22 +02:00
|
|
|
* @param string $string la chaine à coder
|
2020-05-11 17:02:32 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2023-07-19 10:30:41 +02:00
|
|
|
public static function encrypt($string, $key)
|
|
|
|
{
|
2023-03-27 11:34:22 +02:00
|
|
|
$encrypted = openssl_encrypt($string, "AES-256-CBC", $key, 0, substr(md5($key), 0, 16));
|
|
|
|
return base64_encode($encrypted);
|
2023-07-19 10:30:41 +02:00
|
|
|
}
|
2020-05-28 07:19:06 +02:00
|
|
|
|
2020-05-11 17:02:32 +02:00
|
|
|
/**
|
2020-06-02 18:49:24 +02:00
|
|
|
* Décryptage
|
2020-05-11 17:02:32 +02:00
|
|
|
* @param string $key la clé d'encryptage
|
2023-03-27 11:34:22 +02:00
|
|
|
* @param string $string la chaine à décoder
|
2020-05-11 17:02:32 +02:00
|
|
|
* @return string
|
2020-05-28 07:19:06 +02:00
|
|
|
*/
|
2023-07-19 10:30:41 +02:00
|
|
|
public static function decrypt($string, $key)
|
|
|
|
{
|
2023-03-27 11:34:22 +02:00
|
|
|
$decrypted = openssl_decrypt(base64_decode($string), "AES-256-CBC", $key, 0, substr(md5($key), 0, 16));
|
|
|
|
return $decrypted;
|
2023-07-19 10:30:41 +02:00
|
|
|
}
|
2023-03-27 11:34:22 +02:00
|
|
|
|
2023-07-19 10:30:41 +02:00
|
|
|
}
|