aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/FlattrItem.class.php
blob: ab5ed81d894163d00f5b4b682e892249040469e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/* 
* Class for Flattr querying
*/
class FlattrItem {

    public $status;
    public $urltoflattr;
    public $flattrItemURL;
    public $numflattrs;

    public function checkItem($urltoflattr) {
        $this->cacheflattrfile($urltoflattr);
        $flattrResponse = file_get_contents(CACHE . "/flattr/".md5($urltoflattr).".cache");
        if($flattrResponse != FALSE) {
            $result = json_decode($flattrResponse);
            if (isset($result->message)){
                if ($result->message == "flattrable") {
                    $this->status = FLATTRABLE;
                }
            } 
            elseif ($result->link) {
                $this->status = FLATTRED;
                $this->flattrItemURL = $result->link;
                $this->numflattrs = $result->flattrs;
            }
            else {
                $this->status = NOT_FLATTRABLE;
            }
        }
        else {
            $this->status = "FLATTR_ERR_CONNECTION";
        }
    }

    private function cacheflattrfile($urltoflattr) {
        if (!is_dir(CACHE . '/flattr')) {
            mkdir(CACHE . '/flattr', 0777);
        }

        // 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
        if ((!file_exists(CACHE . "/flattr/".md5($urltoflattr).".cache")) || (time() - filemtime(CACHE . "/flattr/".md5($urltoflattr).".cache") > 86400)) {
            $askForFlattr = Tools::getFile(FLATTR_API . $urltoflattr);
            $flattrCacheFile = fopen(CACHE . "/flattr/".md5($urltoflattr).".cache", 'w+');
            fwrite($flattrCacheFile, $askForFlattr);
            fclose($flattrCacheFile);
        }
    }
}