]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | final class Tools | |
6 | { | |
7 | /** | |
8 | * Download a file (typically, for downloading pictures on web server). | |
9 | * | |
10 | * @param $url | |
11 | * | |
12 | * @return bool|mixed|string | |
13 | */ | |
14 | public static function getFile($url) | |
15 | { | |
16 | $timeout = 15; | |
17 | $useragent = 'Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0'; | |
18 | ||
19 | if (in_array('curl', get_loaded_extensions())) { | |
20 | # Fetch feed from URL | |
21 | $curl = curl_init(); | |
22 | curl_setopt($curl, CURLOPT_URL, $url); | |
23 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | |
24 | if (!ini_get('open_basedir') && !ini_get('safe_mode')) { | |
25 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
26 | } | |
27 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
28 | curl_setopt($curl, CURLOPT_HEADER, false); | |
29 | ||
30 | # for ssl, do not verified certificate | |
31 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
32 | curl_setopt($curl, CURLOPT_AUTOREFERER, true); | |
33 | ||
34 | # FeedBurner requires a proper USER-AGENT... | |
35 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | |
36 | curl_setopt($curl, CURLOPT_ENCODING, 'gzip, deflate'); | |
37 | curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
38 | ||
39 | $data = curl_exec($curl); | |
40 | $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
41 | $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301); | |
42 | curl_close($curl); | |
43 | } else { | |
44 | # create http context and add timeout and user-agent | |
45 | $context = stream_context_create( | |
46 | array( | |
47 | 'http' => array( | |
48 | 'timeout' => $timeout, | |
49 | 'header' => 'User-Agent: '.$useragent, | |
50 | 'follow_location' => true, | |
51 | ), | |
52 | 'ssl' => array( | |
53 | 'verify_peer' => false, | |
54 | 'allow_self_signed' => true, | |
55 | ), | |
56 | ) | |
57 | ); | |
58 | ||
59 | # only download page lesser than 4MB | |
60 | $data = @file_get_contents($url, false, $context, -1, 4000000); | |
61 | ||
62 | if (isset($http_response_header) and isset($http_response_header[0])) { | |
63 | $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)); | |
64 | } | |
65 | } | |
66 | ||
67 | # if response is not empty and response is OK | |
68 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { | |
69 | # take charset of page and get it | |
70 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | |
71 | ||
72 | # if meta tag is found | |
73 | if (!empty($meta[0])) { | |
74 | preg_match('#charset="?(.*)"#si', $meta[0], $encoding); | |
75 | # if charset is found set it otherwise, set it to utf-8 | |
76 | $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; | |
77 | if (empty($encoding[1])) { | |
78 | $encoding[1] = 'utf-8'; | |
79 | } | |
80 | } else { | |
81 | $html_charset = 'utf-8'; | |
82 | $encoding[1] = ''; | |
83 | } | |
84 | ||
85 | # replace charset of url to charset of page | |
86 | $data = str_replace('charset='.$encoding[1], 'charset='.$html_charset, $data); | |
87 | ||
88 | return $data; | |
89 | } else { | |
90 | return false; | |
91 | } | |
92 | } | |
93 | ||
94 | /** | |
95 | * Encode a URL by using a salt. | |
96 | * | |
97 | * @param $string | |
98 | * | |
99 | * @return string | |
100 | */ | |
101 | public static function encodeString($string) | |
102 | { | |
103 | return sha1($string.SALT); | |
104 | } | |
105 | ||
106 | public static function generateToken() | |
107 | { | |
108 | if (ini_get('open_basedir') === '') { | |
109 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { | |
110 | // alternative to /dev/urandom for Windows | |
111 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
112 | } else { | |
113 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | |
114 | } | |
115 | } else { | |
116 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | |
117 | } | |
118 | ||
119 | return str_replace('+', '', $token); | |
120 | } | |
121 | } |