3 namespace Wallabag\CoreBundle\Helper
;
8 * Download a file (typically, for downloading pictures on web server).
12 * @return bool|mixed|string
14 public static function getFile($url)
17 $useragent = 'Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0';
19 if (in_array('curl', get_loaded_extensions())) {
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);
27 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
28 curl_setopt($curl, CURLOPT_HEADER
, false);
30 # for ssl, do not verified certificate
31 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, false);
32 curl_setopt($curl, CURLOPT_AUTOREFERER
, true);
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);
39 $data = curl_exec($curl);
40 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
41 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
44 # create http context and add timeout and user-agent
45 $context = stream_context_create(
48 'timeout' => $timeout,
49 'header' => 'User-Agent: '.$useragent,
50 'follow_location' => true,
53 'verify_peer' => false,
54 'allow_self_signed' => true,
59 # only download page lesser than 4MB
60 $data = @file_get_contents($url, false, $context, -1, 4000000);
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));
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);
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';
81 $html_charset = 'utf-8';
85 # replace charset of url to charset of page
86 $data = str_replace('charset='.$encoding[1], 'charset='.$html_charset, $data);
95 * Encode a URL by using a salt.
101 public static function encodeString($string)
103 return sha1($string.SALT
);
106 public static function generateToken()
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);
113 $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
116 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
119 return str_replace('+', '', $token);
123 * For a given text, we calculate reading time for an article.
129 public static function getReadingTime($text)
131 return floor(str_word_count(strip_tags($text)) / 200);