diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/Tools.php')
-rwxr-xr-x | src/Wallabag/CoreBundle/Helper/Tools.php | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/Tools.php b/src/Wallabag/CoreBundle/Helper/Tools.php index c932f880..0fd5f259 100755 --- a/src/Wallabag/CoreBundle/Helper/Tools.php +++ b/src/Wallabag/CoreBundle/Helper/Tools.php | |||
@@ -2,9 +2,6 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | 3 | namespace Wallabag\CoreBundle\Helper; |
4 | 4 | ||
5 | use \RecursiveIteratorIterator; | ||
6 | use \RecursiveDirectoryIterator; | ||
7 | |||
8 | final class Tools | 5 | final class Tools |
9 | { | 6 | { |
10 | /** | 7 | /** |
@@ -18,7 +15,7 @@ final class Tools | |||
18 | $timeout = 15; | 15 | $timeout = 15; |
19 | $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; | 16 | $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0"; |
20 | 17 | ||
21 | if (in_array ('curl', get_loaded_extensions())) { | 18 | if (in_array('curl', get_loaded_extensions())) { |
22 | # Fetch feed from URL | 19 | # Fetch feed from URL |
23 | $curl = curl_init(); | 20 | $curl = curl_init(); |
24 | curl_setopt($curl, CURLOPT_URL, $url); | 21 | curl_setopt($curl, CURLOPT_URL, $url); |
@@ -30,8 +27,8 @@ final class Tools | |||
30 | curl_setopt($curl, CURLOPT_HEADER, false); | 27 | curl_setopt($curl, CURLOPT_HEADER, false); |
31 | 28 | ||
32 | # for ssl, do not verified certificate | 29 | # for ssl, do not verified certificate |
33 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); | 30 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
34 | curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE ); | 31 | curl_setopt($curl, CURLOPT_AUTOREFERER, true); |
35 | 32 | ||
36 | # FeedBurner requires a proper USER-AGENT... | 33 | # FeedBurner requires a proper USER-AGENT... |
37 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); | 34 | curl_setopt($curl, CURL_HTTP_VERSION_1_1, true); |
@@ -48,13 +45,13 @@ final class Tools | |||
48 | array( | 45 | array( |
49 | 'http' => array( | 46 | 'http' => array( |
50 | 'timeout' => $timeout, | 47 | 'timeout' => $timeout, |
51 | 'header' => "User-Agent: " . $useragent, | 48 | 'header' => "User-Agent: ".$useragent, |
52 | 'follow_location' => true | 49 | 'follow_location' => true, |
53 | ), | 50 | ), |
54 | 'ssl' => array( | 51 | 'ssl' => array( |
55 | 'verify_peer' => false, | 52 | 'verify_peer' => false, |
56 | 'allow_self_signed' => true | 53 | 'allow_self_signed' => true, |
57 | ) | 54 | ), |
58 | ) | 55 | ) |
59 | ); | 56 | ); |
60 | 57 | ||
@@ -62,13 +59,12 @@ final class Tools | |||
62 | $data = @file_get_contents($url, false, $context, -1, 4000000); | 59 | $data = @file_get_contents($url, false, $context, -1, 4000000); |
63 | 60 | ||
64 | if (isset($http_response_header) and isset($http_response_header[0])) { | 61 | 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)); | 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 | } | 63 | } |
67 | } | 64 | } |
68 | 65 | ||
69 | # if response is not empty and response is OK | 66 | # if response is not empty and response is OK |
70 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { | 67 | if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { |
71 | |||
72 | # take charset of page and get it | 68 | # take charset of page and get it |
73 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); | 69 | preg_match('#<meta .*charset=.*>#Usi', $data, $meta); |
74 | 70 | ||
@@ -77,19 +73,20 @@ final class Tools | |||
77 | preg_match('#charset="?(.*)"#si', $meta[0], $encoding); | 73 | preg_match('#charset="?(.*)"#si', $meta[0], $encoding); |
78 | # if charset is found set it otherwise, set it to utf-8 | 74 | # if charset is found set it otherwise, set it to utf-8 |
79 | $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; | 75 | $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; |
80 | if (empty($encoding[1])) $encoding[1] = 'utf-8'; | 76 | if (empty($encoding[1])) { |
77 | $encoding[1] = 'utf-8'; | ||
78 | } | ||
81 | } else { | 79 | } else { |
82 | $html_charset = 'utf-8'; | 80 | $html_charset = 'utf-8'; |
83 | $encoding[1] = ''; | 81 | $encoding[1] = ''; |
84 | } | 82 | } |
85 | 83 | ||
86 | # replace charset of url to charset of page | 84 | # replace charset of url to charset of page |
87 | $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data); | 85 | $data = str_replace('charset='.$encoding[1], 'charset='.$html_charset, $data); |
88 | 86 | ||
89 | return $data; | 87 | return $data; |
90 | } | 88 | } else { |
91 | else { | 89 | return false; |
92 | return FALSE; | ||
93 | } | 90 | } |
94 | } | 91 | } |
95 | 92 | ||
@@ -101,7 +98,7 @@ final class Tools | |||
101 | */ | 98 | */ |
102 | public static function encodeString($string) | 99 | public static function encodeString($string) |
103 | { | 100 | { |
104 | return sha1($string . SALT); | 101 | return sha1($string.SALT); |
105 | } | 102 | } |
106 | 103 | ||
107 | public static function generateToken() | 104 | public static function generateToken() |
@@ -113,12 +110,10 @@ final class Tools | |||
113 | } else { | 110 | } else { |
114 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); | 111 | $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); |
115 | } | 112 | } |
116 | } | 113 | } else { |
117 | else { | ||
118 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); | 114 | $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); |
119 | } | 115 | } |
120 | 116 | ||
121 | return str_replace('+', '', $token); | 117 | return str_replace('+', '', $token); |
122 | } | 118 | } |
123 | |||
124 | } | 119 | } |