]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/Wallabag/FlattrItem.php
new folders
[github/wallabag/wallabag.git] / src / Wallabag / Wallabag / FlattrItem.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
6ad93dff
NL
11namespace Wallabag\Wallabag;
12
3602405e
NL
13class FlattrItem
14{
12d9cfbc 15 public $status;
d259f736 16 public $urlToFlattr;
12d9cfbc 17 public $flattrItemURL;
d259f736 18 public $numFlattrs;
12d9cfbc 19
d259f736 20 public function checkItem($urlToFlattr, $id)
3602405e 21 {
ccd0b381 22 $this->_cacheFlattrFile($urlToFlattr, $id);
4e5b0411 23 $flattrResponse = file_get_contents(CACHE . "/flattr/".$id.".cache");
12d9cfbc
NL
24 if($flattrResponse != FALSE) {
25 $result = json_decode($flattrResponse);
3602405e 26 if (isset($result->message)) {
12d9cfbc
NL
27 if ($result->message == "flattrable") {
28 $this->status = FLATTRABLE;
29 }
30 }
3602405e 31 elseif (is_object($result) && $result->link) {
12d9cfbc
NL
32 $this->status = FLATTRED;
33 $this->flattrItemURL = $result->link;
d259f736 34 $this->numFlattrs = $result->flattrs;
12d9cfbc
NL
35 }
36 else {
37 $this->status = NOT_FLATTRABLE;
38 }
39 }
40 else {
41 $this->status = "FLATTR_ERR_CONNECTION";
42 }
43 }
44
d259f736 45 private function _cacheFlattrFile($urlToFlattr, $id)
3602405e 46 {
12d9cfbc
NL
47 if (!is_dir(CACHE . '/flattr')) {
48 mkdir(CACHE . '/flattr', 0777);
49 }
50
51 // 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 52 if ((!file_exists(CACHE . "/flattr/".$id.".cache")) || (time() - filemtime(CACHE . "/flattr/".$id.".cache") > 86400)) {
d259f736 53 $askForFlattr = Tools::getFile(FLATTR_API . $urlToFlattr);
4e5b0411 54 $flattrCacheFile = fopen(CACHE . "/flattr/".$id.".cache", 'w+');
12d9cfbc
NL
55 fwrite($flattrCacheFile, $askForFlattr);
56 fclose($flattrCacheFile);
57 }
58 }
d259f736 59}