From e4d2565e05a517641de921c4c19a2c9d1beea2e7 Mon Sep 17 00:00:00 2001 From: nicosomb Date: Thu, 18 Apr 2013 15:39:34 +0200 Subject: =?UTF-8?q?#4=20-=20ajout=20syst=C3=A8me=20de=20connexion=20(login?= =?UTF-8?q?=20poche=20mot=20de=20passe=20poche=20pour=20l'instant)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/MyTool.class.php | 256 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 inc/MyTool.class.php (limited to 'inc/MyTool.class.php') diff --git a/inc/MyTool.class.php b/inc/MyTool.class.php new file mode 100644 index 00000000..8206f3f7 --- /dev/null +++ b/inc/MyTool.class.php @@ -0,0 +1,256 @@ + '/* moderate */', + '/\[b\](.+?)\[\/b\]/is' + => '$1', + '/\[i\](.+?)\[\/i\]/is' + => '$1', + '/\[s\](.+?)\[\/s\]/is' + => '$1', + '/\[u\](.+?)\[\/u\]/is' + => '$1', + '/\[url\](.+?)\[\/url]/is' + => '$1', + '/\[url=(\w+:\/\/[^\]]+)\](.+?)\[\/url]/is' + => '$2', + '/\[quote\](.+?)\[\/quote\]/is' + => '
$1
', + '/\[code\](.+?)\[\/code\]/is' + => '$1', + '/\[([^[]+)\|([^[]+)\]/is' + => '$1' + ); + $text = preg_replace( + array_keys($replace), + array_values($replace), + $text + ); + + return $text; + } + + public static function formatText($text) + { + $text = preg_replace_callback( + '/(.*?)<\/code_html>/is', + create_function( + '$matches', + 'return htmlspecialchars($matches[1]);' + ), + $text + ); + $text = preg_replace_callback( + '/(.*?)<\/code_php>/is', + create_function( + '$matches', + 'return highlight_string("", true);' + ), + $text + ); + $text = preg_replace('/
/is', '', $text); + + $text = preg_replace( + '#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)(\s|$)#im', + '\\1\\2\\4', + $text + ); + $text = preg_replace( + '#(^|\s)wp:?([a-z]{2}|):([\w]+)#im', + '\\1\\3', + $text + ); + $text = str_replace( + 'http://.wikipedia.org/wiki/', + 'http://www.wikipedia.org/wiki/', + $text + ); + $text = str_replace('\wp:', 'wp:', $text); + $text = str_replace('\http:', 'http:', $text); + $text = MyTool::formatBBCode($text); + $text = nl2br($text); + + return $text; + } + + public static function getUrl() + { + $https = (!empty($_SERVER['HTTPS']) + && (strtolower($_SERVER['HTTPS']) == 'on')) + || (isset($_SERVER["SERVER_PORT"]) + && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection. + $serverport = (!isset($_SERVER["SERVER_PORT"]) + || $_SERVER["SERVER_PORT"] == '80' + || ($https && $_SERVER["SERVER_PORT"] == '443') + ? '' + : ':' . $_SERVER["SERVER_PORT"]); + + $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); + + if (!isset($_SERVER["SERVER_NAME"])) { + return $scriptname; + } + + return 'http' . ($https ? 's' : '') . '://' + . $_SERVER["SERVER_NAME"] . $serverport . $scriptname; + } + + public static function rrmdir($dir) + { + if (is_dir($dir) && ($d = @opendir($dir))) { + while (($file = @readdir($d)) !== false) { + if ( $file == '.' || $file == '..' ) { + continue; + } else { + unlink($dir . '/' . $file); + } + } + } + } + + public static function humanBytes($bytes) + { + $siPrefix = array( 'bytes', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' ); + $base = 1024; + $class = min((int) log($bytes, $base), count($siPrefix) - 1); + $val = sprintf('%1.2f', $bytes / pow($base, $class)); + + return $val . ' ' . $siPrefix[$class]; + } + + public static function returnBytes($val) + { + $val = trim($val); + $last = strtolower($val[strlen($val)-1]); + switch($last) + { + case 'g': $val *= 1024; + case 'm': $val *= 1024; + case 'k': $val *= 1024; + } + + return $val; + } + + public static function getMaxFileSize() + { + $sizePostMax = MyTool::returnBytes(ini_get('post_max_size')); + $sizeUploadMax = MyTool::returnBytes(ini_get('upload_max_filesize')); + + // Return the smaller of two: + return min($sizePostMax, $sizeUploadMax); + } + + public static function smallHash($text) + { + $t = rtrim(base64_encode(hash('crc32', $text, true)), '='); + // Get rid of characters which need encoding in URLs. + $t = str_replace('+', '-', $t); + $t = str_replace('/', '_', $t); + $t = str_replace('=', '@', $t); + + return $t; + } + + public static function renderJson($data) + { + header('Cache-Control: no-cache, must-revalidate'); + header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); + header('Content-type: application/json; charset=UTF-8'); + + echo json_encode($data); + exit(); + } + + public static function grabToLocal($url, $file, $force = false) + { + if ((!file_exists($file) || $force) && in_array('curl', get_loaded_extensions())){ + $ch = curl_init ($url); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); + $raw = curl_exec($ch); + if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) { + $fp = fopen($file, 'x'); + fwrite($fp, $raw); + fclose($fp); + } + curl_close ($ch); + } + } + + public static function redirect($rurl = '') + { + if ($rurl === '') { + // if (!empty($_SERVER['HTTP_REFERER']) && strcmp(parse_url($_SERVER['HTTP_REFERER'],PHP_URL_HOST),$_SERVER['SERVER_NAME'])==0) + $rurl = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']); + if (isset($_POST['returnurl'])) { + $rurl = $_POST['returnurl']; + } + } + + // prevent loop + if (empty($rurl) || parse_url($rurl, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) { + $rurl = MyTool::getUrl(); + } + + if (substr($rurl, 0, 1) !== '?') { + $ref = MyTool::getUrl(); + if (substr($rurl, 0, strlen($ref)) !== $ref) { + $rurl = $ref; + } + } + header('Location: '.$rurl); + exit(); + } + + public static function silence_errors($num, $str) + { +// No-op + } +} \ No newline at end of file -- cgit v1.2.3