Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F54973155
D307.id782.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
D307.id782.diff
View Options
diff --git a/page.php b/page.php
--- a/page.php
+++ b/page.php
@@ -108,11 +108,11 @@
$this->get_data();
if ($this->data) {
$this->analyse();
- }
+ }
}
function get_data () {
- ini_set('user_agent', USER_AGENT);
+ ini_set('user_agent', USER_AGENT);
$data = file_get_contents($this->url);
if (!$data) {
ini_set('user_agent', USER_AGENT_FALLBACK);
@@ -121,11 +121,14 @@
return;
}
}
- $encoding = mb_detect_encoding($data, "ISO-8859-15, ISO-8859-1, UTF-8, ASCII, auto");
+ $this->data = $data;
+ $this->encodeData();
+ }
+
+ function encodeData () {
+ $encoding = mb_detect_encoding($this->data, "ISO-8859-15, ISO-8859-1, UTF-8, ASCII, auto");
if ($encoding && $encoding != 'UTF-8') {
- $this->data = Encoding::toUTF8($data);
- } else {
- $this->data = $data;
+ $this->data = Encoding::toUTF8($this->data);
}
}
diff --git a/pages/DownloadWithWget.php b/pages/DownloadWithWget.php
new file mode 100644
--- /dev/null
+++ b/pages/DownloadWithWget.php
@@ -0,0 +1,37 @@
+<?php
+
+trait DownloadWithWget {
+
+ /**
+ * @return string
+ */
+ private function getTemporaryFilename () {
+ $dir = sys_get_temp_dir();
+ return tempnam($dir, "http-client-wget-");
+ }
+
+ /**
+ * Gets the content of the specified URL, using wget to download it
+ *
+ * @return string
+ */
+ function getFileContents ($url) {
+ $file = $this->getTemporaryFilename();
+ $url = escapeshellarg($url);
+
+ system("wget -q -O $file $url");
+ $data = file_get_contents($file);
+ unlink($file);
+
+ return $data;
+ }
+
+ /**
+ * Downloads the URL through wget and fill data properties
+ */
+ function get_data () {
+ $this->data = $this->getFileContents($this->url);
+ $this->encodeData();
+ }
+
+}
diff --git a/tests/DownloadWithWgetTest.php b/tests/DownloadWithWgetTest.php
new file mode 100644
--- /dev/null
+++ b/tests/DownloadWithWgetTest.php
@@ -0,0 +1,34 @@
+<?php
+
+require 'pages/DownloadWithWget.php';
+
+class DownloadWithWgetTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * The object under test.
+ *
+ * @var object
+ */
+ private $instance;
+
+ /**
+ * Sets up the fixture.
+ *
+ * This method is called before a test is executed.
+ *
+ * @return void
+ */
+ public function setUp () {
+ $this->instance = $this->getObjectForTrait('DownloadWithWget');
+ }
+
+ /**
+ * Tests getFileContents method
+ */
+ public function testGetFileContents () {
+ $this->assertContains(
+ "* <----- vous êtes ici",
+ $this->instance->getFileContents("http://www.perdu.com")
+ );
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Oct 26, 09:02 (23 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4219957
Default Alt Text
D307.id782.diff (2 KB)
Attached To
Mode
D307: Allow to download with wget
Attached
Detach File
Event Timeline
Log In to Comment