]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/FlattrItem.class.php
Changed Flattr Caching System
[github/wallabag/wallabag.git] / inc / 3rdparty / FlattrItem.class.php
1 <?php
2 /*
3 * Class for Flattr querying
4 */
5 class FlattrItem {
6
7 public $status;
8 public $urltoflattr;
9 public $flattrItemURL;
10 public $numflattrs;
11
12 public function checkItem($urltoflattr,$id) {
13 $this->cacheflattrfile($urltoflattr, $id);
14 $flattrResponse = file_get_contents(CACHE . "/flattr/".$id.".cache");
15 print_r($flattrResponse);
16 if($flattrResponse != FALSE) {
17 $result = json_decode($flattrResponse);
18 if (isset($result->message)){
19 if ($result->message == "flattrable") {
20 $this->status = FLATTRABLE;
21 }
22 }
23 elseif ($result->link) {
24 $this->status = FLATTRED;
25 $this->flattrItemURL = $result->link;
26 $this->numflattrs = $result->flattrs;
27 }
28 else {
29 $this->status = NOT_FLATTRABLE;
30 }
31 }
32 else {
33 $this->status = "FLATTR_ERR_CONNECTION";
34 }
35 }
36
37 private function cacheflattrfile($urltoflattr, $id) {
38 if (!is_dir(CACHE . '/flattr')) {
39 mkdir(CACHE . '/flattr', 0777);
40 }
41
42 // if a cache flattr file for this url already exists and it's been less than one day than it have been updated, see in /cache
43 if ((!file_exists(CACHE . "/flattr/".$id.".cache")) || (time() - filemtime(CACHE . "/flattr/".$id.".cache") > 86400)) {
44 $askForFlattr = Tools::getFile(FLATTR_API . $urltoflattr);
45 $flattrCacheFile = fopen(CACHE . "/flattr/".$id.".cache", 'w+');
46 fwrite($flattrCacheFile, $askForFlattr);
47 fclose($flattrCacheFile);
48 }
49 }
50 }