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