]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/MyTool.class.php
4 public static function initPhp()
6 define('START_TIME', microtime(true));
8 if (phpversion() < 5) {
9 die("Argh you don't have PHP 5 !");
12 error_reporting(E_ALL
);
14 function stripslashesDeep($value) {
15 return is_array($value)
16 ? array_map('stripslashesDeep', $value)
17 : stripslashes($value);
20 if (get_magic_quotes_gpc()) {
21 $_POST = array_map('stripslashesDeep', $_POST);
22 $_GET = array_map('stripslashesDeep', $_GET);
23 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
27 register_shutdown_function('ob_end_flush');
30 public static function isUrl($url)
32 // http://neo22s.com/check-if-url-exists-and-is-online-php/
33 $pattern='|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
35 return preg_match($pattern, $url);
38 public static function isEmail($email)
40 $pattern = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2, 4}$/i";
42 return (preg_match($pattern, $email));
45 public static function formatBBCode($text)
48 '/\
[m\
](.+
?)\
[\
/m\
]/is
'
50 '/\
[b\
](.+
?)\
[\
/b\
]/is
'
51 => '<strong
>$1</strong
>',
52 '/\
[i\
](.+
?)\
[\
/i\
]/is
'
54 '/\
[s\
](.+
?)\
[\
/s\
]/is
'
56 '/\
[u\
](.+
?)\
[\
/u\
]/is
'
57 => '<span style
="text-decoration: underline;">$1</span
>',
58 '/\
[url\
](.+
?)\
[\
/url
]/is
'
59 => '<a href
="$1">$1</a>',
60 '/\[url=(\w+:\/\/[^\]]+)\](.+?)\[\/url]/is'
61 => '<a href="$1">$2</a
>',
62 '/\
[quote\
](.+
?)\
[\
/quote\
]/is
'
63 => '<blockquote
>$1</blockquote
>',
64 '/\
[code\
](.+
?)\
[\
/code\
]/is
'
66 '/\
[([^
[]+
)\
|([^
[]+
)\
]/is
'
67 => '<a href
="$2">$1</a>'
71 array_values($replace),
78 public static function formatText($text)
80 $text = preg_replace_callback(
81 '/<code_html>(.*?)<\/code_html>/is',
84 'return htmlspecialchars($matches[1]);'
88 $text = preg_replace_callback(
89 '/<code_php>(.*?)<\/code_php>/is',
92 'return highlight_string("<?php
$matches[1] ?>", true);'
96 $text = preg_replace('/<br \/>/is', '', $text);
99 '#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)(\s|$)#im',
100 '\\1<a href="\\
2">\\2</a>\\4',
103 $text = preg_replace(
104 '#(^|\s)wp:?([a-z]{2}|):([\w]+)#im',
105 '\\1<a href="http
://\\2.wikipedia.org/wiki/\\3">\\3</a>',
109 'http://.wikipedia.org/wiki/',
110 'http://www.wikipedia.org/wiki/',
113 $text = str_replace('\wp:', 'wp:', $text);
114 $text = str_replace('\http:', 'http:', $text);
115 $text = MyTool
::formatBBCode($text);
116 $text = nl2br($text);
121 public static function getUrl()
123 $https = (!empty($_SERVER['HTTPS'])
124 && (strtolower($_SERVER['HTTPS']) == 'on'))
125 || (isset($_SERVER["SERVER_PORT"])
126 && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection.
127 $serverport = (!isset($_SERVER["SERVER_PORT"])
128 || $_SERVER["SERVER_PORT"] == '80'
129 || ($https && $_SERVER["SERVER_PORT"] == '443')
131 : ':' . $_SERVER["SERVER_PORT"]);
133 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
135 if (!isset($_SERVER["SERVER_NAME"])) {
139 return 'http' . ($https ? 's' : '') . '://'
140 . $_SERVER["SERVER_NAME"] . $serverport . $scriptname;
143 public static function rrmdir($dir)
145 if (is_dir($dir) && ($d = @opendir($dir))) {
146 while (($file = @readdir($d)) !== false) {
147 if ( $file == '.' || $file == '..' ) {
150 unlink($dir . '/' . $file);
156 public static function humanBytes($bytes)
158 $siPrefix = array( 'bytes', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
160 $class = min((int) log($bytes, $base), count($siPrefix) - 1);
161 $val = sprintf('%1.2f', $bytes / pow($base, $class));
163 return $val . ' ' . $siPrefix[$class];
166 public static function returnBytes($val)
169 $last = strtolower($val[strlen($val)-1]);
172 case 'g': $val *= 1024;
173 case 'm': $val *= 1024;
174 case 'k': $val *= 1024;
180 public static function getMaxFileSize()
182 $sizePostMax = MyTool
::returnBytes(ini_get('post_max_size'));
183 $sizeUploadMax = MyTool
::returnBytes(ini_get('upload_max_filesize'));
185 // Return the smaller of two:
186 return min($sizePostMax, $sizeUploadMax);
189 public static function smallHash($text)
191 $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
192 // Get rid of characters which need encoding in URLs.
193 $t = str_replace('+', '-', $t);
194 $t = str_replace('/', '_', $t);
195 $t = str_replace('=', '@', $t);
200 public static function renderJson($data)
202 header('Cache-Control: no-cache, must-revalidate');
203 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
204 header('Content-type: application/json; charset=UTF-8');
206 echo json_encode($data);
210 public static function grabToLocal($url, $file, $force = false)
212 if ((!file_exists($file) || $force) && in_array('curl', get_loaded_extensions())){
213 $ch = curl_init ($url);
214 curl_setopt($ch, CURLOPT_HEADER
, false);
215 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
216 curl_setopt($ch, CURLOPT_BINARYTRANSFER
, true);
217 $raw = curl_exec($ch);
218 if (curl_getinfo($ch, CURLINFO_HTTP_CODE
) == 200) {
219 $fp = fopen($file, 'x');
227 public static function redirect($rurl = '')
230 // if (!empty($_SERVER['HTTP_REFERER']) && strcmp(parse_url($_SERVER['HTTP_REFERER'],PHP_URL_HOST),$_SERVER['SERVER_NAME'])==0)
231 $rurl = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
232 if (isset($_POST['returnurl'])) {
233 $rurl = $_POST['returnurl'];
238 if (empty($rurl) || parse_url($rurl, PHP_URL_QUERY
) === $_SERVER['QUERY_STRING']) {
239 $rurl = MyTool
::getUrl();
242 if (substr($rurl, 0, 1) !== '?') {
243 $ref = MyTool
::getUrl();
244 if (substr($rurl, 0, strlen($ref)) !== $ref) {
248 header('Location: '.$rurl);
252 public static function silence_errors($num, $str)