diff options
author | Nicolas Lœuillet <nicolas.loeuillet@gmail.com> | 2013-08-07 10:41:26 -0700 |
---|---|---|
committer | Nicolas Lœuillet <nicolas.loeuillet@gmail.com> | 2013-08-07 10:41:26 -0700 |
commit | 01c0e050ad8eca54f115dfa21db99e4f61ab7ca7 (patch) | |
tree | e1bdacb68b3a56644f4525974844dd954d6e3c6b /inc/poche/Tools.class.php | |
parent | da2c5d6fc33587c775a7d8a738c2c18de41f83b2 (diff) | |
parent | 339d510fda0a43b08981309f7540acedf3a4976c (diff) | |
download | wallabag-01c0e050ad8eca54f115dfa21db99e4f61ab7ca7.tar.gz wallabag-01c0e050ad8eca54f115dfa21db99e4f61ab7ca7.tar.zst wallabag-01c0e050ad8eca54f115dfa21db99e4f61ab7ca7.zip |
Merge pull request #104 from inthepoche/twig
Twig version on dev branch
Diffstat (limited to 'inc/poche/Tools.class.php')
-rw-r--r-- | inc/poche/Tools.class.php | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/inc/poche/Tools.class.php b/inc/poche/Tools.class.php new file mode 100644 index 00000000..d0e43166 --- /dev/null +++ b/inc/poche/Tools.class.php | |||
@@ -0,0 +1,226 @@ | |||
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 | |||
11 | class Tools | ||
12 | { | ||
13 | public static function initPhp() | ||
14 | { | ||
15 | define('START_TIME', microtime(true)); | ||
16 | |||
17 | if (phpversion() < 5) { | ||
18 | die(_('Oops, it seems 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 getPocheUrl() | ||
40 | { | ||
41 | $https = (!empty($_SERVER['HTTPS']) | ||
42 | && (strtolower($_SERVER['HTTPS']) == 'on')) | ||
43 | || (isset($_SERVER["SERVER_PORT"]) | ||
44 | && $_SERVER["SERVER_PORT"] == '443'); // HTTPS detection. | ||
45 | $serverport = (!isset($_SERVER["SERVER_PORT"]) | ||
46 | || $_SERVER["SERVER_PORT"] == '80' | ||
47 | || ($https && $_SERVER["SERVER_PORT"] == '443') | ||
48 | ? '' : ':' . $_SERVER["SERVER_PORT"]); | ||
49 | |||
50 | $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]); | ||
51 | |||
52 | if (!isset($_SERVER["SERVER_NAME"])) { | ||
53 | return $scriptname; | ||
54 | } | ||
55 | |||
56 | return 'http' . ($https ? 's' : '') . '://' | ||
57 | . $_SERVER["SERVER_NAME"] . $serverport . $scriptname; | ||
58 | } | ||
59 | |||
60 | public static function redirect($url = '') | ||
61 | { | ||
62 | if ($url === '') { | ||
63 | $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']); | ||
64 | if (isset($_POST['returnurl'])) { | ||
65 | $url = $_POST['returnurl']; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | # prevent loop | ||
70 | if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) { | ||
71 | $url = Tools::getPocheUrl(); | ||
72 | } | ||
73 | |||
74 | if (substr($url, 0, 1) !== '?') { | ||
75 | $ref = Tools::getPocheUrl(); | ||
76 | if (substr($url, 0, strlen($ref)) !== $ref) { | ||
77 | $url = $ref; | ||
78 | } | ||
79 | } | ||
80 | self::logm('redirect to ' . $url); | ||
81 | header('Location: '.$url); | ||
82 | exit(); | ||
83 | } | ||
84 | |||
85 | public static function getTplFile($view) | ||
86 | { | ||
87 | $tpl_file = 'home.twig'; | ||
88 | switch ($view) | ||
89 | { | ||
90 | case 'install': | ||
91 | $tpl_file = 'install.twig'; | ||
92 | break; | ||
93 | case 'import'; | ||
94 | $tpl_file = 'import.twig'; | ||
95 | break; | ||
96 | case 'export': | ||
97 | $tpl_file = 'export.twig'; | ||
98 | break; | ||
99 | case 'config': | ||
100 | $tpl_file = 'config.twig'; | ||
101 | break; | ||
102 | case 'view': | ||
103 | $tpl_file = 'view.twig'; | ||
104 | break; | ||
105 | default: | ||
106 | break; | ||
107 | } | ||
108 | return $tpl_file; | ||
109 | } | ||
110 | |||
111 | public static function getFile($url) | ||
112 | { | ||
113 | $timeout = 15; | ||
114 | $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | ||
115 | |||
116 | if (in_array ('curl', get_loaded_extensions())) { | ||
117 | # Fetch feed from URL | ||
118 | $curl = curl_init(); | ||
119 | curl_setopt($curl, CURLOPT_URL, $url); | ||
120 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | ||
121 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | ||
122 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | ||
123 | curl_setopt($curl, CURLOPT_HEADER, false); | ||
124 | |||
125 | # for ssl, do not verified certificate | ||
126 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | ||
127 | curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE ); | ||
128 | |||
129 | # FeedBurner requires a proper USER-AGENT... | ||
130 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | ||
131 | curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate"); | ||
132 | curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | ||
133 | |||
134 | $data = curl_exec($curl); | ||
135 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | ||
136 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | ||
137 | curl_close($curl); | ||
138 | } else { | ||
139 | # create http context and add timeout and user-agent | ||
140 | $context = stream_context_create( | ||
141 | array( | ||
142 | 'http' => array( | ||
143 | 'timeout' => $timeout, | ||
144 | 'header' => "User-Agent: " . $useragent, | ||
145 | 'follow_location' => true | ||
146 | ), | ||
147 | 'ssl' => array( | ||
148 | 'verify_peer' => false, | ||
149 | 'allow_self_signed' => true | ||
150 | ) | ||
151 | ) | ||
152 | ); | ||
153 | |||
154 | # only download page lesser than 4MB | ||
155 | $data = @file_get_contents($url, false, $context, -1, 4000000); | ||
156 | |||
157 | if (isset($http_response_header) and isset($http_response_header[0])) { | ||
158 | $httpcodeOK = isset($http_response_header) and isset($http_response_header[0]) and ((strpos($http_response_header[0], '200 OK') !== FALSE) or (strpos($http_response_header[0], '301 Moved Permanently') !== FALSE)); | ||
159 | } | ||
160 | } | ||
161 | |||
162 | # if response is not empty and response is OK | ||
163 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { | ||
164 | |||
165 | # take charset of page and get it | ||
166 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | ||
167 | |||
168 | # if meta tag is found | ||
169 | if (!empty($meta[0])) { | ||
170 | preg_match('#charset="?(.*)"#si', $meta[0], $encoding); | ||
171 | # if charset is found set it otherwise, set it to utf-8 | ||
172 | $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; | ||
173 | } else { | ||
174 | $html_charset = 'utf-8'; | ||
175 | $encoding[1] = ''; | ||
176 | } | ||
177 | |||
178 | # replace charset of url to charset of page | ||
179 | $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data); | ||
180 | |||
181 | return $data; | ||
182 | } | ||
183 | else { | ||
184 | return FALSE; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | public static function renderJson($data) | ||
189 | { | ||
190 | header('Cache-Control: no-cache, must-revalidate'); | ||
191 | header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); | ||
192 | header('Content-type: application/json; charset=UTF-8'); | ||
193 | echo json_encode($data); | ||
194 | exit(); | ||
195 | } | ||
196 | |||
197 | public static function logm($message) | ||
198 | { | ||
199 | if (DEBUG_POCHE) { | ||
200 | $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n"; | ||
201 | file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND); | ||
202 | error_log('DEBUG POCHE : ' . $message); | ||
203 | } | ||
204 | } | ||
205 | |||
206 | public static function encodeString($string) | ||
207 | { | ||
208 | return sha1($string . SALT); | ||
209 | } | ||
210 | |||
211 | public static function checkVar($var, $default = '') | ||
212 | { | ||
213 | return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default); | ||
214 | } | ||
215 | |||
216 | public static function getDomain($url) | ||
217 | { | ||
218 | $pieces = parse_url($url); | ||
219 | $domain = isset($pieces['host']) ? $pieces['host'] : ''; | ||
220 | if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { | ||
221 | return $regs['domain']; | ||
222 | } | ||
223 | |||
224 | return FALSE; | ||
225 | } | ||
226 | } \ No newline at end of file | ||