3 namespace Wallabag\CoreBundle\Helper
;
8 * Download a file (typically, for downloading pictures on web server)
11 * @return bool|mixed|string
13 public static function getFile($url)
16 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
18 if (in_array('curl', get_loaded_extensions())) {
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);
26 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
27 curl_setopt($curl, CURLOPT_HEADER
, false);
29 # for ssl, do not verified certificate
30 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, false);
31 curl_setopt($curl, CURLOPT_AUTOREFERER
, true);
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);
38 $data = curl_exec($curl);
39 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
40 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
43 # create http context and add timeout and user-agent
44 $context = stream_context_create(
47 'timeout' => $timeout,
48 'header' => "User-Agent: ".$useragent,
49 'follow_location' => true,
52 'verify_peer' => false,
53 'allow_self_signed' => true,
58 # only download page lesser than 4MB
59 $data = @file_get_contents($url, false, $context, -1, 4000000);
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));
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);
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';
80 $html_charset = 'utf-8';
84 # replace charset of url to charset of page
85 $data = str_replace('charset='.$encoding[1], 'charset='.$html_charset, $data);
94 * Encode a URL by using a salt
99 public static function encodeString($string)
101 return sha1($string.SALT
);
104 public static function generateToken()
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);
111 $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
114 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
117 return str_replace('+', '', $token);