aboutsummaryrefslogtreecommitdiff
path: root/lib/Favicon/DataAccess.php
diff options
context:
space:
mode:
authorGravatar Clément <clement@selfhost.fr> 2017-02-15 14:14:03 +0100
committerGravatar Clément <clement@selfhost.fr> 2017-02-15 14:14:03 +0100
commit5a1bb1393b4496eb35a2ffb3cc63d41c9dc1e2e5 (patch)
tree67028e45792c575c25c92616633f64cc7a4a13eb /lib/Favicon/DataAccess.php
parent7e949d50320317b5c3b5a2da2bdaf324e794b2f7 (diff)
parent5f637bd816b7323885bfe1751a1724ee59a822f6 (diff)
Merge remote-tracking branch 'FreshRSS/master'
Diffstat (limited to 'lib/Favicon/DataAccess.php')
-rw-r--r--lib/Favicon/DataAccess.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/Favicon/DataAccess.php b/lib/Favicon/DataAccess.php
new file mode 100644
index 000000000..ae7509881
--- /dev/null
+++ b/lib/Favicon/DataAccess.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Favicon;
+
+/**
+ * DataAccess is a wrapper used to read/write data locally or remotly
+ * Aside from SOLID principles, this wrapper is also useful to mock remote resources in unit tests
+ * Note: remote access warning are silenced because we don't care if a website is unreachable
+ **/
+class DataAccess {
+ public function retrieveUrl($url) {
+ $this->set_context();
+ return @file_get_contents($url);
+ }
+
+ public function retrieveHeader($url) {
+ $this->set_context();
+ $headers = @get_headers($url, 1);
+ return $headers ? array_change_key_case($headers) : array();
+ }
+
+ public function saveCache($file, $data) {
+ file_put_contents($file, $data);
+ }
+
+ public function readCache($file) {
+ return file_get_contents($file);
+ }
+
+ private function set_context() {
+ stream_context_set_default(
+ array(
+ 'http' => array(
+ 'method' => 'GET',
+ 'timeout' => 10,
+ 'header' => "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:20.0; Favicon; +https://github.com/ArthurHoaro/favicon) Gecko/20100101 Firefox/32.0\r\n",
+ )
+ )
+ );
+ }
+} \ No newline at end of file