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.php265
1 files changed, 0 insertions, 265 deletions
diff --git a/inc/MyTool.class.php b/inc/MyTool.class.php
deleted file mode 100644
index 1f5051a4..00000000
--- a/inc/MyTool.class.php
+++ /dev/null
@@ -1,265 +0,0 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas LÅ“uillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11class MyTool
12{
13 public static function initPhp()
14 {
15 define('START_TIME', microtime(true));
16
17 if (phpversion() < 5) {
18 die("Argh you don't have PHP 5 !");
19 }
20
21 error_reporting(E_ALL);
22
23 function stripslashesDeep($value) {
24 return is_array($value)
25 ? array_map('stripslashesDeep', $value)
26 : stripslashes($value);
27 }
28
29 if (get_magic_quotes_gpc()) {
30 $_POST = array_map('stripslashesDeep', $_POST);
31 $_GET = array_map('stripslashesDeep', $_GET);
32 $_COOKIE = array_map('stripslashesDeep', $_COOKIE);
33 }
34
35 ob_start();
36 register_shutdown_function('ob_end_flush');
37 }
38
39 public static function isUrl($url)
40 {
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';
43
44 return preg_match($pattern, $url);
45 }
46
47 public static function isEmail($email)
48 {
49 $pattern = "/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2, 4}$/i";
50
51 return (preg_match($pattern, $email));
52 }
53
54 public static function formatBBCode($text)
55 {
56 $replace = array(
57 '/\[m\](.+?)\[\/m\]/is'
58 => '/* moderate */',
59 '/\[b\](.+?)\[\/b\]/is'
60 => '<strong>$1</strong>',
61 '/\[i\](.+?)\[\/i\]/is'
62 => '<em>$1</em>',
63 '/\[s\](.+?)\[\/s\]/is'
64 => '<del>$1</del>',
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'
74 => '<code>$1</code>',
75 '/\[([^[]+)\|([^[]+)\]/is'
76 => '<a href="$2">$1</a>'
77 );
78 $text = preg_replace(
79 array_keys($replace),
80 array_values($replace),
81 $text
82 );
83
84 return $text;
85 }
86
87 public static function formatText($text)
88 {
89 $text = preg_replace_callback(
90 '/<code_html>(.*?)<\/code_html>/is',
91 create_function(
92 '$matches',
93 'return htmlspecialchars($matches[1]);'
94 ),
95 $text
96 );
97 $text = preg_replace_callback(
98 '/<code_php>(.*?)<\/code_php>/is',
99 create_function(
100 '$matches',
101 'return highlight_string("<?php $matches[1] ?>", true);'
102 ),
103 $text
104 );
105 $text = preg_replace('/<br \/>/is', '', $text);
106
107 $text = preg_replace(
108 '#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)(\s|$)#im',
109 '\\1<a href="\\2">\\2</a>\\4',
110 $text
111 );
112 $text = preg_replace(
113 '#(^|\s)wp:?([a-z]{2}|):([\w]+)#im',
114 '\\1<a href="http://\\2.wikipedia.org/wiki/\\3">\\3</a>',
115 $text
116 );
117 $text = str_replace(
118 'http://.wikipedia.org/wiki/',
119 'http://www.wikipedia.org/wiki/',
120 $text
121 );
122 $text = str_replace('\wp:', 'wp:', $text);
123 $text = str_replace('\http:', 'http:', $text);
124 $text = MyTool::formatBBCode($text);
125 $text = nl2br($text);
126
127 return $text;
128 }
129
130 public static function getUrl()
131 {
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')
139 ? ''
140 : ':' . $_SERVER["SERVER_PORT"]);
141
142 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
143
144 if (!isset($_SERVER["SERVER_NAME"])) {
145 return $scriptname;
146 }
147
148 return 'http' . ($https ? 's' : '') . '://'
149 . $_SERVER["SERVER_NAME"] . $serverport . $scriptname;
150 }
151
152 public static function rrmdir($dir)
153 {
154 if (is_dir($dir) && ($d = @opendir($dir))) {
155 while (($file = @readdir($d)) !== false) {
156 if ( $file == '.' || $file == '..' ) {
157 continue;
158 } else {
159 unlink($dir . '/' . $file);
160 }
161 }
162 }
163 }
164
165 public static function humanBytes($bytes)
166 {
167 $siPrefix = array( 'bytes', 'KB', 'MB', 'GB', 'TB', 'EB', 'ZB', 'YB' );
168 $base = 1024;
169 $class = min((int) log($bytes, $base), count($siPrefix) - 1);
170 $val = sprintf('%1.2f', $bytes / pow($base, $class));
171
172 return $val . ' ' . $siPrefix[$class];
173 }
174
175 public static function returnBytes($val)
176 {
177 $val = trim($val);
178 $last = strtolower($val[strlen($val)-1]);
179 switch($last)
180 {
181 case 'g': $val *= 1024;
182 case 'm': $val *= 1024;
183 case 'k': $val *= 1024;
184 }
185
186 return $val;
187 }
188
189 public static function getMaxFileSize()
190 {
191 $sizePostMax = MyTool::returnBytes(ini_get('post_max_size'));
192 $sizeUploadMax = MyTool::returnBytes(ini_get('upload_max_filesize'));
193
194 // Return the smaller of two:
195 return min($sizePostMax, $sizeUploadMax);
196 }
197
198 public static function smallHash($text)
199 {
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);
205
206 return $t;
207 }
208
209 public static function renderJson($data)
210 {
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');
214
215 echo json_encode($data);
216 exit();
217 }
218
219 public static function grabToLocal($url, $file, $force = false)
220 {
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');
229 fwrite($fp, $raw);
230 fclose($fp);
231 }
232 curl_close ($ch);
233 }
234 }
235
236 public static function redirect($rurl = '')
237 {
238 if ($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'];
243 }
244 }
245
246 // prevent loop
247 if (empty($rurl) || parse_url($rurl, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
248 $rurl = MyTool::getUrl();
249 }
250
251 if (substr($rurl, 0, 1) !== '?') {
252 $ref = MyTool::getUrl();
253 if (substr($rurl, 0, strlen($ref)) !== $ref) {
254 $rurl = $ref;
255 }
256 }
257 header('Location: '.$rurl);
258 exit();
259 }
260
261 public static function silence_errors($num, $str)
262 {
263// No-op
264 }
265} \ No newline at end of file