aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/MyTool.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/MyTool.class.php')
-rw-r--r--inc/MyTool.class.php256
1 files changed, 256 insertions, 0 deletions
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 @@
1<?php
2class MyTool
3{
4 public static function initPhp()
5 {
6 define('START_TIME', microtime(true));
7
8 if (phpversion() < 5) {
9 die("Argh you don't have PHP 5 !");
10 }
11
12 error_reporting(E_ALL);
13
14 function stripslashesDeep($value) {
15 return is_array($value)
16 ? array_map('stripslashesDeep', $value)
17 : stripslashes($value);
18 }
19
20 if (get_magic_quotes_gpc()) {
21 $_POST = array_map('stripslashesDeep', $_POST);
22 $_GET = array_map('stripslashesDeep', $_GET);
23 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
24 }
25
26 ob_start();
27 register_shutdown_function('ob_end_flush');
28 }
29
30 public static function isUrl($url)
31 {
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';
34
35 return preg_match($pattern, $url);
36 }
37
38 public static function isEmail($email)
39 {
40 $pattern = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2, 4}$/i";
41
42 return (preg_match($pattern, $email));
43 }
44
45 public static function formatBBCode($text)
46 {
47 $replace = array(
48 '/\[m\](.+?)\[\/m\]/is'
49 => '/* moderate */',
50 '/\[b\](.+?)\[\/b\]/is'
51 => '<strong>$1</strong>',
52 '/\[i\](.+?)\[\/i\]/is'
53 => '<em>$1</em>',
54 '/\[s\](.+?)\[\/s\]/is'
55 => '<del>$1</del>',
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'
65 => '<code>$1</code>',
66 '/\[([^[]+)\|([^[]+)\]/is'
67 => '<a href="$2">$1</a>'
68 );
69 $text = preg_replace(
70 array_keys($replace),
71 array_values($replace),
72 $text
73 );
74
75 return $text;
76 }
77
78 public static function formatText($text)
79 {
80 $text = preg_replace_callback(
81 '/<code_html>(.*?)<\/code_html>/is',
82 create_function(
83 '$matches',
84 'return htmlspecialchars($matches[1]);'
85 ),
86 $text
87 );
88 $text = preg_replace_callback(
89 '/<code_php>(.*?)<\/code_php>/is',
90 create_function(
91 '$matches',
92 'return highlight_string("<?php $matches[1] ?>", true);'
93 ),
94 $text
95 );
96 $text = preg_replace('/<br \/>/is', '', $text);
97
98 $text = preg_replace(
99 '#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)(\s|$)#im',
100 '\\1<a href="\\2">\\2</a>\\4',
101 $text
102 );
103 $text = preg_replace(
104 '#(^|\s)wp:?([a-z]{2}|):([\w]+)#im',
105 '\\1<a href="http://\\2.wikipedia.org/wiki/\\3">\\3</a>',
106 $text
107 );
108 $text = str_replace(
109 'http://.wikipedia.org/wiki/',
110 'http://www.wikipedia.org/wiki/',
111 $text
112 );
113 $text = str_replace('\wp:', 'wp:', $text);
114 $text = str_replace('\http:', 'http:', $text);
115 $text = MyTool::formatBBCode($text);
116 $text = nl2br($text);
117
118 return $text;
119 }
120
121 public static function getUrl()
122 {
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')
130 ? ''
131 : ':' . $_SERVER["SERVER_PORT"]);
132
133 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
134
135 if (!isset($_SERVER["SERVER_NAME"])) {
136 return $scriptname;
137 }
138
139 return 'http' . ($https ? 's' : '') . '://'
140 . $_SERVER["SERVER_NAME"] . $serverport . $scriptname;
141 }
142
143 public static function rrmdir($dir)
144 {
145 if (is_dir($dir) && ($d = @opendir($dir))) {
146 while (($file = @readdir($d)) !== false) {
147 if ( $file == '.' || $file == '..' ) {
148 continue;
149 } else {
150 unlink($dir . '/' . $file);
151 }
152 }
153 }
154 }
155
156 public static function humanBytes($bytes)
157 {
158 $siPrefix = array( 'bytes', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
159 $base = 1024;
160 $class = min((int) log($bytes, $base), count($siPrefix) - 1);
161 $val = sprintf('%1.2f', $bytes / pow($base, $class));
162
163 return $val . ' ' . $siPrefix[$class];
164 }
165
166 public static function returnBytes($val)
167 {
168 $val = trim($val);
169 $last = strtolower($val[strlen($val)-1]);
170 switch($last)
171 {
172 case 'g': $val *= 1024;
173 case 'm': $val *= 1024;
174 case 'k': $val *= 1024;
175 }
176
177 return $val;
178 }
179
180 public static function getMaxFileSize()
181 {
182 $sizePostMax = MyTool::returnBytes(ini_get('post_max_size'));
183 $sizeUploadMax = MyTool::returnBytes(ini_get('upload_max_filesize'));
184
185 // Return the smaller of two:
186 return min($sizePostMax, $sizeUploadMax);
187 }
188
189 public static function smallHash($text)
190 {
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);
196
197 return $t;
198 }
199
200 public static function renderJson($data)
201 {
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');
205
206 echo json_encode($data);
207 exit();
208 }
209
210 public static function grabToLocal($url, $file, $force = false)
211 {
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');
220 fwrite($fp, $raw);
221 fclose($fp);
222 }
223 curl_close ($ch);
224 }
225 }
226
227 public static function redirect($rurl = '')
228 {
229 if ($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'];
234 }
235 }
236
237 // prevent loop
238 if (empty($rurl) || parse_url($rurl, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
239 $rurl = MyTool::getUrl();
240 }
241
242 if (substr($rurl, 0, 1) !== '?') {
243 $ref = MyTool::getUrl();
244 if (substr($rurl, 0, strlen($ref)) !== $ref) {
245 $rurl = $ref;
246 }
247 }
248 header('Location: '.$rurl);
249 exit();
250 }
251
252 public static function silence_errors($num, $str)
253 {
254// No-op
255 }
256} \ No newline at end of file