]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/MyTool.class.php
3 * poche, a read it later open source system
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
8 * @license http://www.wtfpl.net/ see COPYING file
13 public static function initPhp()
15 define('START_TIME', microtime(true));
17 if (phpversion() < 5) {
18 die("Argh you don't have PHP 5 !");
21 error_reporting(E_ALL
);
23 function stripslashesDeep($value) {
24 return is_array($value)
25 ? array_map('stripslashesDeep', $value)
26 : stripslashes($value);
29 if (get_magic_quotes_gpc()) {
30 $_POST = array_map('stripslashesDeep', $_POST);
31 $_GET = array_map('stripslashesDeep', $_GET);
32 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
36 register_shutdown_function('ob_end_flush');
39 public static function isUrl($url)
41 // http://neo22s.com/check-if-url-exists-and-is-online-php/
42 $pattern='|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
44 return preg_match($pattern, $url);
47 public static function isEmail($email)
49 $pattern = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2, 4}$/i";
51 return (preg_match($pattern, $email));
54 public static function formatBBCode($text)
57 '/\
[m\
](.+
?)\
[\
/m\
]/is
'
59 '/\
[b\
](.+
?)\
[\
/b\
]/is
'
60 => '<strong
>$1</strong
>',
61 '/\
[i\
](.+
?)\
[\
/i\
]/is
'
63 '/\
[s\
](.+
?)\
[\
/s\
]/is
'
65 '/\
[u\
](.+
?)\
[\
/u\
]/is
'
66 => '<span style
="text-decoration: underline;">$1</span
>',
67 '/\
[url\
](.+
?)\
[\
/url
]/is
'
68 => '<a href
="$1">$1</a>',
69 '/\[url=(\w+:\/\/[^\]]+)\](.+?)\[\/url]/is'
70 => '<a href="$1">$2</a
>',
71 '/\
[quote\
](.+
?)\
[\
/quote\
]/is
'
72 => '<blockquote
>$1</blockquote
>',
73 '/\
[code\
](.+
?)\
[\
/code\
]/is
'
75 '/\
[([^
[]+
)\
|([^
[]+
)\
]/is
'
76 => '<a href
="$2">$1</a>'
80 array_values($replace),
87 public static function formatText($text)
89 $text = preg_replace_callback(
90 '/<code_html>(.*?)<\/code_html>/is',
93 'return htmlspecialchars($matches[1]);'
97 $text = preg_replace_callback(
98 '/<code_php>(.*?)<\/code_php>/is',
101 'return highlight_string("<?php
$matches[1] ?>", true);'
105 $text = preg_replace('/<br \/>/is', '', $text);
107 $text = preg_replace(
108 '#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)(\s|$)#im',
109 '\\1<a href="\\
2">\\2</a>\\4',
112 $text = preg_replace(
113 '#(^|\s)wp:?([a-z]{2}|):([\w]+)#im',
114 '\\1<a href="http
://\\2.wikipedia.org/wiki/\\3">\\3</a>',
118 'http://.wikipedia.org/wiki/',
119 'http://www.wikipedia.org/wiki/',
122 $text = str_replace('\wp:', 'wp:', $text);
123 $text = str_replace('\http:', 'http:', $text);
124 $text = MyTool
::formatBBCode($text);
125 $text = nl2br($text);
130 public static function getUrl()
132 $https = (!empty($_SERVER['HTTPS'])
133 && (strtolower($_SERVER['HTTPS']) == 'on'))
134 || (isset($_SERVER["SERVER_PORT"])
135 && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection.
136 $serverport = (!isset($_SERVER["SERVER_PORT"])
137 || $_SERVER["SERVER_PORT"] == '80'
138 || ($https && $_SERVER["SERVER_PORT"] == '443')
140 : ':' . $_SERVER["SERVER_PORT"]);
142 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
144 if (!isset($_SERVER["SERVER_NAME"])) {
148 return 'http' . ($https ? 's' : '') . '://'
149 . $_SERVER["SERVER_NAME"] . $serverport . $scriptname;
152 public static function rrmdir($dir)
154 if (is_dir($dir) && ($d = @opendir($dir))) {
155 while (($file = @readdir($d)) !== false) {
156 if ( $file == '.' || $file == '..' ) {
159 unlink($dir . '/' . $file);
165 public static function humanBytes($bytes)
167 $siPrefix = array( 'bytes', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
169 $class = min((int) log($bytes, $base), count($siPrefix) - 1);
170 $val = sprintf('%1.2f', $bytes / pow($base, $class));
172 return $val . ' ' . $siPrefix[$class];
175 public static function returnBytes($val)
178 $last = strtolower($val[strlen($val)-1]);
181 case 'g': $val *= 1024;
182 case 'm': $val *= 1024;
183 case 'k': $val *= 1024;
189 public static function getMaxFileSize()
191 $sizePostMax = MyTool
::returnBytes(ini_get('post_max_size'));
192 $sizeUploadMax = MyTool
::returnBytes(ini_get('upload_max_filesize'));
194 // Return the smaller of two:
195 return min($sizePostMax, $sizeUploadMax);
198 public static function smallHash($text)
200 $t = rtrim(base64_encode(hash('crc32', $text, true)), '=');
201 // Get rid of characters which need encoding in URLs.
202 $t = str_replace('+', '-', $t);
203 $t = str_replace('/', '_', $t);
204 $t = str_replace('=', '@', $t);
209 public static function renderJson($data)
211 header('Cache-Control: no-cache, must-revalidate');
212 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
213 header('Content-type: application/json; charset=UTF-8');
215 echo json_encode($data);
219 public static function grabToLocal($url, $file, $force = false)
221 if ((!file_exists($file) || $force) && in_array('curl', get_loaded_extensions())){
222 $ch = curl_init ($url);
223 curl_setopt($ch, CURLOPT_HEADER
, false);
224 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
225 curl_setopt($ch, CURLOPT_BINARYTRANSFER
, true);
226 $raw = curl_exec($ch);
227 if (curl_getinfo($ch, CURLINFO_HTTP_CODE
) == 200) {
228 $fp = fopen($file, 'x');
236 public static function redirect($rurl = '')
239 // if (!empty($_SERVER['HTTP_REFERER']) && strcmp(parse_url($_SERVER['HTTP_REFERER'],PHP_URL_HOST),$_SERVER['SERVER_NAME'])==0)
240 $rurl = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
241 if (isset($_POST['returnurl'])) {
242 $rurl = $_POST['returnurl'];
247 if (empty($rurl) || parse_url($rurl, PHP_URL_QUERY
) === $_SERVER['QUERY_STRING']) {
248 $rurl = MyTool
::getUrl();
251 if (substr($rurl, 0, 1) !== '?') {
252 $ref = MyTool
::getUrl();
253 if (substr($rurl, 0, strlen($ref)) !== $ref) {
257 header('Location: '.$rurl);
261 public static function silence_errors($num, $str)