/8 '; static public function getBaseUrl() { if (empty($_SERVER['HTTP_HOST'])) return 'http://localhost/'; return 'http' . (!empty($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . self::getBaseUrlPath(); } static public function getBaseUrlPath() { if (!empty(self::$_base_url_path)) return self::$_base_url_path; $uri = dirname($_SERVER['SCRIPT_NAME']); if (substr($uri, -6) == '/admin') { $uri = dirname($uri); } if (substr($uri, -1) != '/') { $uri .= '/'; } self::$_base_url_path = $uri; return $uri; } static public function makeUriFromTitle($str) { $str = utf8_decode($str); $str = strtr($str, "ÀÁÂÃÄÅàáâãäåÇçÒÓÔÕÖØòóôõöøÈÉÊËèéêëÌÍÎÏìíîïÙÚÛÜùúûü¾ÝÿýÑñ", "AAAAAAaaaaaaCcOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuYYyyNn"); $str = str_replace('Æ','AE',$str); $str = str_replace('æ','ae',$str); $str = str_replace('¼','OE',$str); $str = str_replace('½','oe',$str); $str = preg_replace('/[^A-Za-z0-9_\s\'\:\/\[\]-]/', '', $str); $str = preg_replace('/[\s\'\:\/\[\]-]+/',' ',trim($str)); if (isset($str[64])) { $str = substr($str, 0, 64); $str = substr($str, 0, strrpos($str, ' ')); } $str = str_replace(' ','-',$str); return $str; } static public function baseConv($num, $base=62, $index=false) { if (!$index) $index = substr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base); $out = ""; for ($t = floor(log10($num) / log10($base)); $t >= 0; $t--) { $a = floor($num / pow($base, $t)); $out = $out . substr($index, $a, 1); $num = $num - ($a * pow($base, $t)); } return $out; } static public function baseConvReverse($num, $base=62, $index=false) { if (!$index) $index = substr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 0, $base); $out = 0; $len = strlen($num) - 1; for ($t = 0; $t <= $len; $t++) { $out = $out + strpos($index, substr($num, $t, 1)) * pow($base, $len - $t); } return $out; } static public function id2url($id) { return self::baseConv($id); } static public function url2id($url) { return self::baseConvReverse($url); } public static function CSRF_create($id) { if (!isset($_SESSION)) { @session_start(); } if (!isset($_SESSION['csrf_protect'])) { $_SESSION['csrf_protect'] = array(); } $_SESSION['csrf_protect'][$id] = array( 'created' => time(), 'check' => md5(self::$secret_key . $id . time()) ); return $_SESSION['csrf_protect'][$id]['check']; } public static function CSRF_check($id, $hash) { if (!isset($_SESSION) && isset($_COOKIE[session_name()])) { @session_start(); } if (!isset($_SESSION['csrf_protect'][$id])) { return false; } if ($_SESSION['csrf_protect'][$id]['check'] == $hash) { unset($_SESSION['csrf_protect'][$id]); return true; } return false; } /** * 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 */ public static function getGenericPagination($current, $total, $bypage, $listLength=11, $showLast = true) { if ($total <= $bypage) return false; $total = ceil($total / $bypage); if ($total < $current) return false; $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 = array(); if ($current > 1) { $out[] = array('id' => $current - 1, 'label' => '« ' . 'Page précédente', 'class' => 'prev', 'accesskey' => 'a'); } if ($begin > 1) { $out[] = array('id' => 1, 'label' => '1 ...', 'class' => 'first'); } for ($i = $begin; $i <= $end; $i++) { $out[] = array('id' => $i, 'label' => $i, 'class' => ($i == $current) ? 'current' : ''); } if ($showLast && $end < $total) { $out[] = array('id' => $total, 'label' => '... ' . $total, 'class' => 'last'); } if ($current < $total) { $out[] = array('id' => $current + 1, 'label' => 'Page suivante' . ' »', 'class' => 'next', 'accesskey' => 'z'); } return $out; } static public function checkLangCode($code) { return preg_match('!^[a-z]{2,3}$!', $code); } static public function getHttpLanguages() { if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) return false; $http_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; $http_langs = explode(',', $http_lang); $langs = array(); // On établit la liste des langues par priorité $qr = 100; foreach ($http_langs as $lang) { if (preg_match('!;q=(.*)!', $lang, $match)) { $q = intval($match[1] * 100); $lang = str_replace($match[0], '', $lang); } else { $q = $qr--; } $lang = trim($lang); $langs[$q] = substr($lang, 0, 2); } krsort($langs); return $langs; } static public function redirect($url, $code=false, $exit=true) { if (empty($url) || !preg_match('!^https?://!', $destination)) { $host = $_SERVER['HTTP_HOST']; if (empty($url)) $url = 'http://' . $host . '/'; else $url = 'http://' . $host . '/' . preg_replace('!^/!', '', $url); } if (headers_sent()) { echo ''. ' ' . ' ' . ' '. ' '. '
'. ' Cliquez ici pour continuer...'. '
'. ' '. ''; } else { if ($code == 302) header('Status: 302 Moved Temporarily', true, 302); header('Location: '.$url); } if ($exit) exit; return true; } static public function parseContentPluginParams($str) { $attributes = array(); $last_value = ''; $state = 0; // matches double quoted strings: // "foobar" // "foo\"bar" // "foobar" . "foo\"bar" $_db_qstr_regexp = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"'; // matches single quoted strings: // 'foobar' // 'foo\'bar' $_si_qstr_regexp = '\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\''; // matches single or double quoted strings $_qstr_regexp = '(?:' . $_db_qstr_regexp . '|' . $_si_qstr_regexp . ')'; preg_match_all('/(?:' . $_qstr_regexp . ' | (?>[^"\'=\s]+))+|[=]/x', $str, $match); foreach ($match[0] as $value) { if ($state == 0) { if (!is_string($value)) throw new contentPluginException("Invalid attribute name '".$value."'."); $attr_name = $value; $state = 1; } elseif ($state == 1) { if ($value != '=') throw new contentPluginException("Expecting '=' after '".$last_value."'"); $state = 2; } elseif ($state == 2) { if ($value == '=') throw new contentPluginException("Unexpected '=' after '".$last_value."'"); if ($value == 'yes' || $value == 'on' || $value == 'true') $value = true; elseif ($value == 'no' || $value == 'off' || $value == 'false') $value = false; elseif ($value == 'null') $value = null; elseif ($value[0] == '"' || $value[0] == "'") $value = trim(substr($value, 1, -1)); $attributes[$attr_name] = $value; $state = 0; } $last_value = $value; } if ($state == 1) throw new contentPluginException("Expecting '=' after '".$last_value."'"); elseif ($state == 2) throw new contentPluginException("Missing attribute value after '".$last_value."'"); return $attributes; } } ?>