From a32231274060ec2de453124117518458c4fa00df Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 8 Sep 2013 20:54:11 +0200 Subject: [PATCH] Implemented Flattr changes Added a button to say if the article is flattrable or not and how many people have flattred this object. --- inc/poche/Poche.class.php | 59 +++++++++++++++++++++++++++++++++++++-- tpl/view.twig | 1 + 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index a8f64151..5dab10df 100644 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php @@ -247,10 +247,15 @@ class Poche $tidy = tidy_parse_string($content, array('indent'=>true, 'show-body-only' => true), 'UTF8'); $tidy->cleanRepair(); $content = $tidy->value; - } + + // flattr checking + $flattr = new FlattrItem(); + $flattr->checkitem($entry['url']); + $tpl_vars = array( 'entry' => $entry, 'content' => $content, + 'flattr' => $flattr, ); } else { @@ -558,4 +563,54 @@ class Poche } return $version; } -} \ No newline at end of file +} + +/* class for Flattr querying. Should be put in a separate file +* Or maybe just create an array instead of a complete class... My mistake. :-° +*/ +class FlattrItem{ + public $status; + public $urltoflattr; + public $flattrItemURL; + public $numflattrs; + + public function checkitem($urltoflattr){ + $this->cacheflattrfile($urltoflattr); + $flattrResponse = file_get_contents("cache/flattr/".base64_encode($urltoflattr).".cache"); + var_dump($flattrResponse); + 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_user_count; + } + else{ + $this->status = "not flattrable"; + } + } + else + { + $this->status = "FLATTR_ERR_CONNECTION"; + } + } + + private function cacheflattrfile($urltoflattr){ + if (!is_dir('cache/flattr')){ + mkdir('./cache/flattr', 0700); + } + // 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/".base64_encode($urltoflattr).".cache")) || (time() - filemtime("cache/flattr/".base64_encode($urltoflattr).".cache") > 86400)) + { + $askForFlattr = Tools::getFile("https://api.flattr.com/rest/v2/things/lookup/?url=".$urltoflattr); + $flattrCacheFile = fopen("cache/flattr/".base64_encode($urltoflattr).".cache", 'w+'); + fwrite($flattrCacheFile, $askForFlattr); + fclose($flattrCacheFile); + } + } +} diff --git a/tpl/view.twig b/tpl/view.twig index 28508772..7b15c7c7 100644 --- a/tpl/view.twig +++ b/tpl/view.twig @@ -31,6 +31,7 @@ {% if constant('SHARE_TWITTER') == 1 %}
  • {% endif %} {% if constant('SHARE_MAIL') == 1 %}
  • {% endif %} {% if constant('SHARE_SHAARLI') == 1 %}
  • {% trans "shaarli" %}
  • {% endif %} +
  • {% if flattr.status == "flattrable" %} This thing is flattrable !{% elseif flattr.status == "flattred" %} This thing has already been flattred by {{ flattr.numflattrs }} users and can be flattred !{% else %}This article cannot be flattred{% endif %}
  • {% trans "this article appears wrong?" %} {% trans "create an issue" %} {% trans "or" %} {% trans "contact us by mail" %}

    -- 2.41.0