]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/poche/Tools.class.php
[add] create tags page
[github/wallabag/wallabag.git] / inc / poche / Tools.class.php
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 || (isset($_SERVER["SERVER_PORT"]) //Custom HTTPS port detection
46 && $_SERVER["SERVER_PORT"] == SSL_PORT);
47
48 $serverport = (!isset($_SERVER["SERVER_PORT"])
49 || $_SERVER["SERVER_PORT"] == '80'
50 || ($https && $_SERVER["SERVER_PORT"] == '443')
51 || ($https && $_SERVER["SERVER_PORT"]==SSL_PORT) //Custom HTTPS port detection
52 ? '' : ':' . $_SERVER["SERVER_PORT"]);
53
54 $scriptname = str_replace('/index.php', '/', $_SERVER["SCRIPT_NAME"]);
55
56 if (!isset($_SERVER["HTTP_HOST"])) {
57 return $scriptname;
58 }
59
60 return 'http' . ($https ? 's' : '') . '://'
61 . $_SERVER["HTTP_HOST"] . $serverport . $scriptname;
62 }
63
64 public static function redirect($url = '')
65 {
66 if ($url === '') {
67 $url = (empty($_SERVER['HTTP_REFERER'])?'?':$_SERVER['HTTP_REFERER']);
68 if (isset($_POST['returnurl'])) {
69 $url = $_POST['returnurl'];
70 }
71 }
72
73 # prevent loop
74 if (empty($url) || parse_url($url, PHP_URL_QUERY) === $_SERVER['QUERY_STRING']) {
75 $url = Tools::getPocheUrl();
76 }
77
78 if (substr($url, 0, 1) !== '?') {
79 $ref = Tools::getPocheUrl();
80 if (substr($url, 0, strlen($ref)) !== $ref) {
81 $url = $ref;
82 }
83 }
84 self::logm('redirect to ' . $url);
85 header('Location: '.$url);
86 exit();
87 }
88
89 public static function getTplFile($view)
90 {
91 $default_tpl = 'home.twig';
92
93 switch ($view) {
94 case 'install':
95 $tpl_file = 'install.twig';
96 break;
97 case 'import';
98 $tpl_file = 'import.twig';
99 break;
100 case 'export':
101 $tpl_file = 'export.twig';
102 break;
103 case 'config':
104 $tpl_file = 'config.twig';
105 break;
106 case 'tags':
107 $tpl_file = 'tags.twig';
108 break;
109 case 'view':
110 $tpl_file = 'view.twig';
111 break;
112 case 'login':
113 $tpl_file = 'login.twig';
114 break;
115 case 'error':
116 $tpl_file = 'error.twig';
117 break;
118
119 default:
120 $tpl_file = $default_tpl;
121 break;
122 }
123
124 return $tpl_file;
125 }
126
127 public static function getFile($url)
128 {
129 $timeout = 15;
130 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
131
132 if (in_array ('curl', get_loaded_extensions())) {
133 # Fetch feed from URL
134 $curl = curl_init();
135 curl_setopt($curl, CURLOPT_URL, $url);
136 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
137 if (!ini_get('open_basedir') && !ini_get('safe_mode')) {
138 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
139 }
140 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
141 curl_setopt($curl, CURLOPT_HEADER, false);
142
143 # for ssl, do not verified certificate
144 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
145 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE );
146
147 # FeedBurner requires a proper USER-AGENT...
148 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
149 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
150 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
151
152 $data = curl_exec($curl);
153 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
154 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
155 curl_close($curl);
156 } else {
157 # create http context and add timeout and user-agent
158 $context = stream_context_create(
159 array(
160 'http' => array(
161 'timeout' => $timeout,
162 'header' => "User-Agent: " . $useragent,
163 'follow_location' => true
164 ),
165 'ssl' => array(
166 'verify_peer' => false,
167 'allow_self_signed' => true
168 )
169 )
170 );
171
172 # only download page lesser than 4MB
173 $data = @file_get_contents($url, false, $context, -1, 4000000);
174
175 if (isset($http_response_header) and isset($http_response_header[0])) {
176 $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));
177 }
178 }
179
180 # if response is not empty and response is OK
181 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
182
183 # take charset of page and get it
184 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
185
186 # if meta tag is found
187 if (!empty($meta[0])) {
188 preg_match('#charset="?(.*)"#si', $meta[0], $encoding);
189 # if charset is found set it otherwise, set it to utf-8
190 $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8';
191 if (empty($encoding[1])) $encoding[1] = 'utf-8';
192 } else {
193 $html_charset = 'utf-8';
194 $encoding[1] = '';
195 }
196
197 # replace charset of url to charset of page
198 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
199
200 return $data;
201 }
202 else {
203 return FALSE;
204 }
205 }
206
207 public static function renderJson($data)
208 {
209 header('Cache-Control: no-cache, must-revalidate');
210 header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
211 header('Content-type: application/json; charset=UTF-8');
212 echo json_encode($data);
213 exit();
214 }
215
216 public static function logm($message)
217 {
218 if (DEBUG_POCHE) {
219 $t = strval(date('Y/m/d_H:i:s')) . ' - ' . $_SERVER["REMOTE_ADDR"] . ' - ' . strval($message) . "\n";
220 file_put_contents(CACHE . '/log.txt', $t, FILE_APPEND);
221 error_log('DEBUG POCHE : ' . $message);
222 }
223 }
224
225 public static function encodeString($string)
226 {
227 return sha1($string . SALT);
228 }
229
230 public static function checkVar($var, $default = '')
231 {
232 return ((isset ($_REQUEST["$var"])) ? htmlentities($_REQUEST["$var"]) : $default);
233 }
234
235 public static function getDomain($url)
236 {
237 return parse_url($url, PHP_URL_HOST);
238 }
239
240 public static function getReadingTime($text) {
241 $word = str_word_count(strip_tags($text));
242 $minutes = floor($word / 200);
243 $seconds = floor($word % 200 / (200 / 60));
244 $time = array('minutes' => $minutes, 'seconds' => $seconds);
245
246 return $minutes;
247 }
248
249 public static function getDocLanguage($userlanguage) {
250 $lang = explode('.', $userlanguage);
251 return str_replace('_', '-', $lang[0]);
252 }
253 }