3 namespace Wallabag\CoreBundle\Helper
;
5 use \RecursiveIteratorIterator
;
6 use \RecursiveDirectoryIterator
;
11 * Download a file (typically, for downloading pictures on web server)
14 * @return bool|mixed|string
16 public static function getFile($url)
19 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
21 if (in_array ('curl', get_loaded_extensions())) {
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);
29 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
30 curl_setopt($curl, CURLOPT_HEADER
, false);
32 # for ssl, do not verified certificate
33 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, FALSE);
34 curl_setopt($curl, CURLOPT_AUTOREFERER
, TRUE );
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);
41 $data = curl_exec($curl);
42 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
43 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
46 # create http context and add timeout and user-agent
47 $context = stream_context_create(
50 'timeout' => $timeout,
51 'header' => "User-Agent: " . $useragent,
52 'follow_location' => true
55 'verify_peer' => false,
56 'allow_self_signed' => true
61 # only download page lesser than 4MB
62 $data = @file_get_contents($url, false, $context, -1, 4000000);
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));
69 # if response is not empty and response is OK
70 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
72 # take charset of page and get it
73 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
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';
82 $html_charset = 'utf-8';
86 # replace charset of url to charset of page
87 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
97 * Encode a URL by using a salt
102 public static function encodeString($string)
104 return sha1($string . SALT
);
108 * Returns the correct header for a status code
110 * @param $status_code
112 private static function _status($status_code)
114 if (strpos(php_sapi_name(), 'apache') !== false) {
116 header('HTTP/1.0 '.$status_code);
120 header('Status: '.$status_code);
124 public static function generateToken()
126 if (ini_get('open_basedir') === '') {
127 if (strtoupper(substr(PHP_OS
, 0, 3)) === 'WIN') {
128 // alternative to /dev/urandom for Windows
129 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
131 $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
135 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
138 return str_replace('+', '', $token);