aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas@loeuillet.org>2015-01-27 13:08:02 +0100
committerNicolas LÅ“uillet <nicolas@loeuillet.org>2015-01-27 13:08:02 +0100
commitb9ec99e25b5972bb3d0702919519382ebc37dacc (patch)
treeba70a157b17cb0b64f52634296fdf6dd03c4edc5
parentd692b3b08d26d3f945d52f6cf5e9f90335d74cdd (diff)
downloadwallabag-b9ec99e25b5972bb3d0702919519382ebc37dacc.tar.gz
wallabag-b9ec99e25b5972bb3d0702919519382ebc37dacc.tar.zst
wallabag-b9ec99e25b5972bb3d0702919519382ebc37dacc.zip
replace legacy calls with new one
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php4
-rwxr-xr-xsrc/Wallabag/CoreBundle/Helper/Tools.php141
-rw-r--r--src/Wallabag/CoreBundle/Helper/Url.php25
-rw-r--r--src/Wallabag/CoreBundle/Service/Extractor.php2
4 files changed, 169 insertions, 3 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php
index 48d19307..c6c94462 100644
--- a/src/Wallabag/CoreBundle/Controller/EntryController.php
+++ b/src/Wallabag/CoreBundle/Controller/EntryController.php
@@ -8,8 +8,8 @@ use Symfony\Component\HttpFoundation\Request;
8use Wallabag\CoreBundle\Repository; 8use Wallabag\CoreBundle\Repository;
9use Wallabag\CoreBundle\Entity\Entries; 9use Wallabag\CoreBundle\Entity\Entries;
10use Wallabag\CoreBundle\Service\Extractor; 10use Wallabag\CoreBundle\Service\Extractor;
11use Wallabag\Wallabag\Tools; 11use Wallabag\CoreBundle\Helper\Tools;
12use Wallabag\Wallabag\Url; 12use Wallabag\CoreBundle\Helper\Url;
13 13
14class EntryController extends Controller 14class EntryController extends Controller
15{ 15{
diff --git a/src/Wallabag/CoreBundle/Helper/Tools.php b/src/Wallabag/CoreBundle/Helper/Tools.php
new file mode 100755
index 00000000..c773af37
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/Tools.php
@@ -0,0 +1,141 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use \RecursiveIteratorIterator;
6use \RecursiveDirectoryIterator;
7
8final class Tools
9{
10 /**
11 * Download a file (typically, for downloading pictures on web server)
12 *
13 * @param $url
14 * @return bool|mixed|string
15 */
16 public static function getFile($url)
17 {
18 $timeout = 15;
19 $useragent = "Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
20
21 if (in_array ('curl', get_loaded_extensions())) {
22 # Fetch feed from URL
23 $curl = curl_init();
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);
28 }
29 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
30 curl_setopt($curl, CURLOPT_HEADER, false);
31
32 # for ssl, do not verified certificate
33 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
34 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE );
35
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);
40
41 $data = curl_exec($curl);
42 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
43 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
44 curl_close($curl);
45 } else {
46 # create http context and add timeout and user-agent
47 $context = stream_context_create(
48 array(
49 'http' => array(
50 'timeout' => $timeout,
51 'header' => "User-Agent: " . $useragent,
52 'follow_location' => true
53 ),
54 'ssl' => array(
55 'verify_peer' => false,
56 'allow_self_signed' => true
57 )
58 )
59 );
60
61 # only download page lesser than 4MB
62 $data = @file_get_contents($url, false, $context, -1, 4000000);
63
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));
66 }
67 }
68
69 # if response is not empty and response is OK
70 if (isset($data) and isset($httpcodeOK) and $httpcodeOK) {
71
72 # take charset of page and get it
73 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
74
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';
81 } else {
82 $html_charset = 'utf-8';
83 $encoding[1] = '';
84 }
85
86 # replace charset of url to charset of page
87 $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data);
88
89 return $data;
90 }
91 else {
92 return FALSE;
93 }
94 }
95
96 /**
97 * Encode a URL by using a salt
98 *
99 * @param $string
100 * @return string
101 */
102 public static function encodeString($string)
103 {
104 return sha1($string . SALT);
105 }
106
107 /**
108 * Returns the correct header for a status code
109 *
110 * @param $status_code
111 */
112 private static function _status($status_code)
113 {
114 if (strpos(php_sapi_name(), 'apache') !== false) {
115
116 header('HTTP/1.0 '.$status_code);
117 }
118 else {
119
120 header('Status: '.$status_code);
121 }
122 }
123
124 public static function generateToken()
125 {
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);
130 } else {
131 $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15);
132 }
133 }
134 else {
135 $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20);
136 }
137
138 return str_replace('+', '', $token);
139 }
140
141}
diff --git a/src/Wallabag/CoreBundle/Helper/Url.php b/src/Wallabag/CoreBundle/Helper/Url.php
new file mode 100644
index 00000000..b076e7c7
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/Url.php
@@ -0,0 +1,25 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5class Url
6{
7 public $url;
8
9 function __construct($url)
10 {
11 $this->url = base64_decode($url);
12 }
13
14 public function getUrl() {
15 return $this->url;
16 }
17
18 public function setUrl($url) {
19 $this->url = $url;
20 }
21
22 public function isCorrect() {
23 return filter_var($this->url, FILTER_VALIDATE_URL) !== FALSE;
24 }
25} \ No newline at end of file
diff --git a/src/Wallabag/CoreBundle/Service/Extractor.php b/src/Wallabag/CoreBundle/Service/Extractor.php
index 790386d4..1c6ff0ae 100644
--- a/src/Wallabag/CoreBundle/Service/Extractor.php
+++ b/src/Wallabag/CoreBundle/Service/Extractor.php
@@ -3,7 +3,7 @@
3namespace Wallabag\CoreBundle\Service; 3namespace Wallabag\CoreBundle\Service;
4 4
5use Wallabag\CoreBundle\Helper\Content; 5use Wallabag\CoreBundle\Helper\Content;
6use Wallabag\Wallabag\Url; 6use Wallabag\CoreBundle\Helper\Url;
7 7
8final class Extractor 8final class Extractor
9{ 9{