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