]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/FlattrItem.class.php
merge fix 776
[github/wallabag/wallabag.git] / inc / 3rdparty / FlattrItem.class.php
CommitLineData
12d9cfbc 1<?php
3602405e
NL
2/**
3 * wallabag, self hostable application allowing you to not miss any content anymore
4 *
5 * @category wallabag
6 * @author Nicolas Lœuillet <nicolas@loeuillet.org>
7 * @copyright 2013
8 * @license http://opensource.org/licenses/MIT see COPYING file
9 */
12d9cfbc 10
3602405e
NL
11class FlattrItem
12{
12d9cfbc 13 public $status;
d259f736 14 public $urlToFlattr;
12d9cfbc 15 public $flattrItemURL;
d259f736 16 public $numFlattrs;
12d9cfbc 17
d259f736 18 public function checkItem($urlToFlattr, $id)
3602405e 19 {
ccd0b381 20 $this->_cacheFlattrFile($urlToFlattr, $id);
4e5b0411 21 $flattrResponse = file_get_contents(CACHE . "/flattr/".$id.".cache");
12d9cfbc
NL
22 if($flattrResponse != FALSE) {
23 $result = json_decode($flattrResponse);
3602405e 24 if (isset($result->message)) {
12d9cfbc
NL
25 if ($result->message == "flattrable") {
26 $this->status = FLATTRABLE;
27 }
28 }
3602405e 29 elseif (is_object($result) && $result->link) {
12d9cfbc
NL
30 $this->status = FLATTRED;
31 $this->flattrItemURL = $result->link;
d259f736 32 $this->numFlattrs = $result->flattrs;
12d9cfbc
NL
33 }
34 else {
35 $this->status = NOT_FLATTRABLE;
36 }
37 }
38 else {
39 $this->status = "FLATTR_ERR_CONNECTION";
40 }
41 }
42
d259f736 43 private function _cacheFlattrFile($urlToFlattr, $id)
3602405e 44 {
12d9cfbc
NL
45 if (!is_dir(CACHE . '/flattr')) {
46 mkdir(CACHE . '/flattr', 0777);
47 }
48
49 // 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
4e5b0411 50 if ((!file_exists(CACHE . "/flattr/".$id.".cache")) || (time() - filemtime(CACHE . "/flattr/".$id.".cache") > 86400)) {
d259f736 51 $askForFlattr = Tools::getFile(FLATTR_API . $urlToFlattr);
4e5b0411 52 $flattrCacheFile = fopen(CACHE . "/flattr/".$id.".cache", 'w+');
12d9cfbc
NL
53 fwrite($flattrCacheFile, $askForFlattr);
54 fclose($flattrCacheFile);
55 }
56 }
d259f736 57}