'Janvier', 'February'=>'Février', 'March'=>'Mars', 'April'=>'Avril', 'May'=>'Mai', 'June'=>'Juin', 'July'=>'Juillet', 'August'=>'Août', 'September'=>'Septembre', 'October'=>'Octobre', 'November'=>'Novembre', 'December'=>'Décembre', 'Monday'=>'Lundi', 'Tuesday'=>'Mardi', 'Wednesday'=>'Mercredi', 'Thursday'=>'Jeudi','Friday'=>'Vendredi','Saturday'=>'Samedi','Sunday'=>'Dimanche', 'Feb'=>'Fév','Apr'=>'Avr','Jun'=>'Juin', 'Jul'=>'Juil','Aug'=>'Aout','Dec'=>'Déc', 'Mon'=>'Lun','Tue'=>'Mar','Wed'=>'Mer','Thu'=>'Jeu','Fri'=>'Ven','Sat'=>'Sam','Sun'=>'Dim']; static public function get_datetime($ts) { if (is_object($ts) && $ts instanceof \DateTimeInterface) { return $ts; } elseif (is_numeric($ts)) { return new \DateTime('@' . $ts); } elseif (strlen($ts) == 10) { return \DateTime::createFromFormat('!Y-m-d', $ts); } elseif (strlen($ts) == 19) { return \DateTime::createFromFormat('Y-m-d H:i:s', $ts); } else { return null; } } static public function strftime_fr($ts, $format) { $ts = self::get_datetime($ts); if (null === $ts) { return $ts; } $date = strftime($format, $ts->getTimestamp()); $date = strtr($date, self::FRENCH_DATE_NAMES); $date = strtolower($date); return $date; } static public function date_fr($ts, $format = null) { $ts = self::get_datetime($ts); if (null === $ts) { return $ts; } if (is_null($format)) { $format = 'd/m/Y à H:i'; } $date = $ts->format($format); $date = strtr($date, self::FRENCH_DATE_NAMES); $date = strtolower($date); return $date; } /** * @deprecated */ static public function checkDate($str) { if (!preg_match('!^(\d{4})-(\d{2})-(\d{2})$!', $str, $match)) return false; if (!checkdate($match[2], $match[3], $match[1])) return false; return true; } /** * @deprecated */ static public function checkDateTime($str) { if (!preg_match('!^(\d{4}-\d{2}-\d{2})[T ](\d{2}):(\d{2})!', $str, $match)) return false; if (!self::checkDate($match[1])) return false; if ((int) $match[2] < 0 || (int) $match[2] > 23) return false; if ((int) $match[3] < 0 || (int) $match[3] > 59) return false; if (isset($match[4]) && ((int) $match[4] < 0 || (int) $match[4] > 59)) return false; return true; } static public function moneyToInteger($value) { if (trim($value) === '') { return 0; } if (!preg_match('/^-?(\d+)(?:[,.](\d{1,2}))?$/', $value, $match)) { throw new UserException(sprintf('Le montant est invalide : %s. Exemple de format accepté : 142,02', $value)); } $value = $match[1] . str_pad(@$match[2], 2, '0', STR_PAD_RIGHT); $value = (int) $value; return $value; } static public function money_format($number, string $dec_point = ',', string $thousands_sep = ' ', $zero_if_empty = true): string { if ($number == 0) { return $zero_if_empty ? '0' : '0,00'; } $sign = $number < 0 ? '-' : ''; $number = abs((int) $number); $decimals = substr('0' . $number, -2); $number = (int) substr($number, 0, -2); return sprintf('%s%s%s%s', $sign, number_format($number, 0, $dec_point, $thousands_sep), $dec_point, $decimals); } static public function getRequestURI() { if (!empty($_SERVER['REQUEST_URI'])) return $_SERVER['REQUEST_URI']; else return false; } static public function getSelfURL($qs = true) { $uri = self::getSelfURI($qs); // Make absolute URI relative to parent URI if (strpos($uri, WWW_URI) === 0) { $uri = substr($uri, strlen(WWW_URI)); } return WWW_URL . $uri; } static public function getSelfURI($qs = true) { $uri = self::getRequestURI(); if ($qs !== true && (strpos($uri, '?') !== false)) { $uri = substr($uri, 0, strpos($uri, '?')); } if (is_array($qs)) { $uri .= '?' . http_build_query($qs); } return $uri; } static public function getModifiedURL(string $new) { return HTTP::mergeURLs(self::getSelfURL(), $new); } public static function redirect($destination=false, $exit=true) { if (empty($destination) || !preg_match('/^https?:\/\//', $destination)) { if (empty($destination)) $destination = WWW_URL; else $destination = WWW_URL . preg_replace('/^\//', '', $destination); } if (PHP_SAPI == 'cli') { echo 'Please visit ' . $destination . PHP_EOL; exit; } if (headers_sent()) { echo ''. ' ' . ' ' . ' '. ' '. '
'. ' Cliquez ici pour continuer...'. '
'. ' '. ''; if ($exit) exit(); return true; } header("Location: " . $destination); if ($exit) exit(); } static public function getIP() { if (!empty($_SERVER['REMOTE_ADDR'])) return $_SERVER['REMOTE_ADDR']; return ''; } static public function getCountryList() { return Translate::getCountriesList('fr'); } static public function getCountryName($code) { $list = self::getCountryList(); if (!isset($list[$code])) return false; return $list[$code]; } /** * Génération pagination à partir de la page courante ($current), * du nombre d'items total ($total), et du nombre d'items par page ($bypage). * $listLength représente la longueur d'items de la pagination à génerer * * @param int $current * @param int $total * @param int $bypage * @param int $listLength * @param bool $showLast Toggle l'affichage du dernier élément de la pagination * @return array|null */ public static function getGenericPagination($current, $total, $bypage, $listLength=11, $showLast = true) { if ($total <= $bypage) return null; $total = ceil($total / $bypage); if ($total < $current) return null; $length = ($listLength / 2); $begin = $current - ceil($length); if ($begin < 1) { $begin = 1; } $end = $begin + $listLength; if($end > $total) { $begin -= ($end - $total); $end = $total; } if ($begin < 1) { $begin = 1; } if($end==($total-1)) { $end = $total; } if($begin == 2) { $begin = 1; } $out = []; if ($current > 1) { $out[] = ['id' => $current - 1, 'label' => '« ' . 'Page précédente', 'class' => 'prev', 'accesskey' => 'a']; } if ($begin > 1) { $out[] = ['id' => 1, 'label' => '1 ...', 'class' => 'first']; } for ($i = $begin; $i <= $end; $i++) { $out[] = ['id' => $i, 'label' => $i, 'class' => ($i == $current) ? 'current' : '']; } if ($showLast && $end < $total) { $out[] = ['id' => $total, 'label' => '... ' . $total, 'class' => 'last']; } if ($current < $total) { $out[] = ['id' => $current + 1, 'label' => 'Page suivante' . ' »', 'class' => 'next', 'accesskey' => 'z']; } return $out; } static public function transliterateToAscii($str, $charset='UTF-8') { // Don't process empty strings if (!trim($str)) return $str; // We only process non-ascii strings if (preg_match('!^[[:ascii:]]+$!', $str)) return $str; $str = htmlentities($str, ENT_NOQUOTES, $charset); $str = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str); $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str); // pour les ligatures e.g. 'œ' $str = preg_replace('#&[^;]+;#', '', $str); // supprime les autres caractères $str = preg_replace('![^[:ascii:]]+!', '', $str); return $str; } /** * Transforme un texte SkrivML en HTML * @param string $str Texte SkrivML * @return string Texte HTML */ static public function SkrivToHTML($str) { if (!self::$skriv) { self::$skriv = new \KD2\SkrivLite; self::$skriv->registerExtension('fichier', ['\\Garradin\\Fichiers', 'SkrivFichier']); self::$skriv->registerExtension('image', ['\\Garradin\\Fichiers', 'SkrivImage']); // Enregistrer d'autres extensions éventuellement Plugin::fireSignal('skriv.init', ['skriv' => self::$skriv]); } $skriv =& self::$skriv; $str = preg_replace_callback('/(fichier|image):\/\/(\d+)/', function ($match) use ($skriv) { try { $file = new Fichiers((int)$match[2]); } catch (\InvalidArgumentException $e) { return $skriv->parseError('/!\ Lien fichier : ' . $e->getMessage()); } return $file->getURL(); }, $str); $str = self::$skriv->render($str); return $str; } /** * Transforme les tags de base SPIP en tags SkrivML * @param string $str Texte d'entrée * @return string Texte transformé */ static public function SpipToSkriv($str) { $str = preg_replace('/(?(.+?)\]/', '[[$1 | $2]]', $str); $str = preg_replace('/(?(\V*?)<\/h3>/', '=== $1 ===', $str); $str = preg_replace('/(\V*)<\/b>/', '**$1**', $str); $str = preg_replace('/(\V*?)<\/strong>/', '**$1**', $str); $str = preg_replace('/(\V*?)<\/i>/', '\'\'$1\'\'', $str); $str = preg_replace('/(\V*?)<\/em>/', '\'\'$1\'\'', $str); $str = preg_replace('/
  • (\V*?)<\/li>/', '* $1', $str); $str = preg_replace('/