From f16854859addf714887a5c24695621f3f3e9a70f Mon Sep 17 00:00:00 2001 From: Garvin Hicking Date: Wed, 8 Feb 2012 15:14:58 +0100 Subject: [PATCH] improved karma plugin --- docs/NEWS | 6 +- .../UTF-8/lang_bg.inc.php | 4 +- .../UTF-8/lang_de.inc.php | 16 +- .../UTF-8/lang_es.inc.php | 4 +- .../UTF-8/lang_fr.inc.php | 2 +- .../UTF-8/lang_ja.inc.php | 4 +- .../UTF-8/lang_ko.inc.php | 2 +- .../UTF-8/lang_pl.inc.php | 4 +- .../UTF-8/lang_tn.inc.php | 2 +- .../UTF-8/lang_zh.inc.php | 2 +- .../serendipity_event_karma/lang_bg.inc.php | 4 +- .../serendipity_event_karma/lang_de.inc.php | 16 +- .../serendipity_event_karma/lang_en.inc.php | 10 +- .../serendipity_event_karma/lang_es.inc.php | 4 +- .../serendipity_event_karma/lang_fa.inc.php | 4 +- .../serendipity_event_karma/lang_fr.inc.php | 2 +- .../serendipity_event_karma/lang_ja.inc.php | 4 +- .../serendipity_event_karma/lang_ko.inc.php | 2 +- .../serendipity_event_karma/lang_pl.inc.php | 4 +- .../serendipity_event_karma/lang_tn.inc.php | 2 +- .../serendipity_event_karma.php | 541 +++++++++++------- 21 files changed, 381 insertions(+), 258 deletions(-) diff --git a/docs/NEWS b/docs/NEWS index b0399ec0..41b67c3a 100644 --- a/docs/NEWS +++ b/docs/NEWS @@ -6,6 +6,9 @@ Version 1.7 () * fixed draft & future entries preview link in backend + * Improved karmarating plugin to be able to use AJAX calls + (gregman) + * Allow Smarty to fetch .tpl files from all directories so that s9y plugin can use the fetch() call for their .tpl files no matter which (symlinked) directory the plugin resides in. @@ -15,7 +18,8 @@ Version 1.7 () {include} calls seperately to smarty->fetch() calls, we'll also add that to .tpl files. (garvinhicking) - + + * Patch by Markus Brükner: Properly handle files that have no extension in media database diff --git a/plugins/serendipity_event_karma/UTF-8/lang_bg.inc.php b/plugins/serendipity_event_karma/UTF-8/lang_bg.inc.php index 13aec362..2f0667d2 100644 --- a/plugins/serendipity_event_karma/UTF-8/lang_bg.inc.php +++ b/plugins/serendipity_event_karma/UTF-8/lang_bg.inc.php @@ -1,7 +1,7 @@ - * DE-Revision: Revision of lang_de.inc.php * @author Konrad Bauckmeier * @translated 2009/06/03 - * @author Konrad Bauckmeier - * @revisionDate 2011/11/22 */ @define('PLUGIN_KARMA_NAME', 'Karma'); @@ -46,7 +44,7 @@ @define('PLUGIN_KARMA_VOTETEXT_1', '-2'); @define('PLUGIN_KARMA_VOTETEXT_1_NAME', 'Benennung: Schlechtes Ergebnis'); @define('PLUGIN_KARMA_VOTETEXT_1_BLAHBLAH', 'Text, der dargestellt wird, wenn der Benutzer für "schlecht" im Textmode stimmen möchte. (Standard: ' . PLUGIN_KARMA_VOTETEXT_1 . ')'); -@define('PLUGIN_KARMA_VOTED', 'Punktzahl "%s" gespeichert.'); +@define('PLUGIN_KARMA_VOTED', 'Bewertung "%s" gespeichert.'); @define('PLUGIN_KARMA_VOTES', '%3$s Stimme(n)'); @define('PLUGIN_KARMA_INVALID', 'Bewertung ungültig oder manipuliert!'); @define('PLUGIN_KARMA_ALREADYVOTED', 'Bewertung bereits gespeichert.'); @@ -73,7 +71,6 @@ //--JAM:@define('PLUGIN_KARMA_ALIGNMENT', 'Voting Bar Alignment'); //--JAM:@define('PLUGIN_KARMA_ALIGNMENT_BLAHBLAH', 'Alignment of voting bar in the entry footer. Templates can override this setting with !important (Default: right)'); //--JAM: @define('PLUGIN_KARMA_ALIGNMENT_DETECT', 'from style'); - @define('PLUGIN_KARMA_VISITS', 'Aufrufstatisik aktivieren?'); @define('PLUGIN_KARMA_VISITS_BLAHBLAH', 'Soll jeder Klick auf einen Artikel gezählt und dargestellt werden?'); @define('PLUGIN_KARMA_VISITSCOUNT', ' %4$s Klicks'); @@ -124,5 +121,8 @@ @define('PLUGIN_KARMA_DELETE_VOTES', 'Lösche ausgewählte Stimmen'); @define('PLUGIN_KARMA_APPROVE_VOTES', 'Bestätige ausgewählte Stimmen'); -// Next lines were translated on 2011/11/22 -@define('PLUGIN_KARMA_ACTIVE_REGISTERED', 'Nur eingeloggte Authoren können abstimmen'); \ No newline at end of file +//New on ajax feature +@define('PLUGIN_KARMA_AJAX', 'AJAX Funktionalität'); +@define('PLUGIN_KARMA_AJAX_BLAHBLAH', 'Soll die Stimmabgabe mittels asynchronem Javascript erfolgen? Wenn ja, wird die Bewertung im Hintergrund an den Server gesendet und das Ergebnis dargestellt, ohne dass die Seite neu geladen werden muss.'); +@define('PLUGIN_KARMA_ALIGNMENT', 'Ausrichtung des Plugins'); +@define('PLUGIN_KARMA_ALIGNMENT_BLAHBLAH', 'Soll das Plugin links, mittig oder rechts angezeigt werden?'); \ No newline at end of file diff --git a/plugins/serendipity_event_karma/UTF-8/lang_es.inc.php b/plugins/serendipity_event_karma/UTF-8/lang_es.inc.php index 0d8ab8ea..afbd4515 100644 --- a/plugins/serendipity_event_karma/UTF-8/lang_es.inc.php +++ b/plugins/serendipity_event_karma/UTF-8/lang_es.inc.php @@ -1,6 +1,6 @@ - * EN-Revision: 690 */ diff --git a/plugins/serendipity_event_karma/UTF-8/lang_fr.inc.php b/plugins/serendipity_event_karma/UTF-8/lang_fr.inc.php index 471dba70..c4ec1874 100644 --- a/plugins/serendipity_event_karma/UTF-8/lang_fr.inc.php +++ b/plugins/serendipity_event_karma/UTF-8/lang_fr.inc.php @@ -1,4 +1,4 @@ - * EN-Revision: 690 */ diff --git a/plugins/serendipity_event_karma/UTF-8/lang_ko.inc.php b/plugins/serendipity_event_karma/UTF-8/lang_ko.inc.php index d0264abd..0a03229d 100644 --- a/plugins/serendipity_event_karma/UTF-8/lang_ko.inc.php +++ b/plugins/serendipity_event_karma/UTF-8/lang_ko.inc.php @@ -1,4 +1,4 @@ - # (c) 2005 http://www.tool-box.info/ diff --git a/plugins/serendipity_event_karma/UTF-8/lang_pl.inc.php b/plugins/serendipity_event_karma/UTF-8/lang_pl.inc.php index d95996de..40dd85e2 100644 --- a/plugins/serendipity_event_karma/UTF-8/lang_pl.inc.php +++ b/plugins/serendipity_event_karma/UTF-8/lang_pl.inc.php @@ -1,7 +1,7 @@ - * EN-Revision: Revision of lang_en.inc.php */ diff --git a/plugins/serendipity_event_karma/UTF-8/lang_tn.inc.php b/plugins/serendipity_event_karma/UTF-8/lang_tn.inc.php index 0aeaab00..aeeb1571 100644 --- a/plugins/serendipity_event_karma/UTF-8/lang_tn.inc.php +++ b/plugins/serendipity_event_karma/UTF-8/lang_tn.inc.php @@ -1,4 +1,4 @@ - * DE-Revision: Revision of lang_de.inc.php * @author Konrad Bauckmeier * @translated 2009/06/03 - * @author Konrad Bauckmeier - * @revisionDate 2011/11/22 */ @define('PLUGIN_KARMA_NAME', 'Karma'); @@ -46,7 +44,7 @@ @define('PLUGIN_KARMA_VOTETEXT_1', '-2'); @define('PLUGIN_KARMA_VOTETEXT_1_NAME', 'Benennung: Schlechtes Ergebnis'); @define('PLUGIN_KARMA_VOTETEXT_1_BLAHBLAH', 'Text, der dargestellt wird, wenn der Benutzer für "schlecht" im Textmode stimmen möchte. (Standard: ' . PLUGIN_KARMA_VOTETEXT_1 . ')'); -@define('PLUGIN_KARMA_VOTED', 'Punktzahl "%s" gespeichert.'); +@define('PLUGIN_KARMA_VOTED', 'Bewertung "%s" gespeichert.'); @define('PLUGIN_KARMA_VOTES', '%3$s Stimme(n)'); @define('PLUGIN_KARMA_INVALID', 'Bewertung ungültig oder manipuliert!'); @define('PLUGIN_KARMA_ALREADYVOTED', 'Bewertung bereits gespeichert.'); @@ -73,7 +71,6 @@ //--JAM:@define('PLUGIN_KARMA_ALIGNMENT', 'Voting Bar Alignment'); //--JAM:@define('PLUGIN_KARMA_ALIGNMENT_BLAHBLAH', 'Alignment of voting bar in the entry footer. Templates can override this setting with !important (Default: right)'); //--JAM: @define('PLUGIN_KARMA_ALIGNMENT_DETECT', 'from style'); - @define('PLUGIN_KARMA_VISITS', 'Aufrufstatisik aktivieren?'); @define('PLUGIN_KARMA_VISITS_BLAHBLAH', 'Soll jeder Klick auf einen Artikel gezählt und dargestellt werden?'); @define('PLUGIN_KARMA_VISITSCOUNT', ' %4$s Klicks'); @@ -124,5 +121,8 @@ @define('PLUGIN_KARMA_DELETE_VOTES', 'Lösche ausgewählte Stimmen'); @define('PLUGIN_KARMA_APPROVE_VOTES', 'Bestätige ausgewählte Stimmen'); -// Next lines were translated on 2011/11/22 -@define('PLUGIN_KARMA_ACTIVE_REGISTERED', 'Nur eingeloggte Authoren können abstimmen'); \ No newline at end of file +//New on ajax feature +@define('PLUGIN_KARMA_AJAX', 'AJAX Funktionalität'); +@define('PLUGIN_KARMA_AJAX_BLAHBLAH', 'Soll die Stimmabgabe mittels asynchronem Javascript erfolgen? Wenn ja, wird die Bewertung im Hintergrund an den Server gesendet und das Ergebnis dargestellt, ohne dass die Seite neu geladen werden muss.'); +@define('PLUGIN_KARMA_ALIGNMENT', 'Ausrichtung des Plugins'); +@define('PLUGIN_KARMA_ALIGNMENT_BLAHBLAH', 'Soll das Plugin links, mittig oder rechts angezeigt werden?'); \ No newline at end of file diff --git a/plugins/serendipity_event_karma/lang_en.inc.php b/plugins/serendipity_event_karma/lang_en.inc.php index afd23c60..9d2b13e0 100644 --- a/plugins/serendipity_event_karma/lang_en.inc.php +++ b/plugins/serendipity_event_karma/lang_en.inc.php @@ -1,7 +1,7 @@ - * EN-Revision: Revision of lang_en.inc.php */ @@ -117,3 +117,9 @@ @define('PLUGIN_KARMA_DELETE_VOTES', 'Delete selected votes'); @define('PLUGIN_KARMA_APPROVE_VOTES', 'Approve selected votes'); @define('PLUGIN_KARMA_ACTIVE_REGISTERED', 'Only logged-in authors can vote'); + +//New on ajax feature +@define('PLUGIN_KARMA_AJAX', 'AJAX funcionality'); +@define('PLUGIN_KARMA_AJAX_BLAHBLAH', 'Shall rating be performed by asynchronous javascript? If set to "yes", your rating will be send to the server in the background and the result will be displayed without refreshing the website.'); +@define('PLUGIN_KARMA_ALIGNMENT', 'Alignmnet of the plugin'); +@define('PLUGIN_KARMA_ALIGNMENT_BLAHBLAH', 'Shall the plugin be displayed left, center or right'); \ No newline at end of file diff --git a/plugins/serendipity_event_karma/lang_es.inc.php b/plugins/serendipity_event_karma/lang_es.inc.php index 3eb29d53..3168b82f 100644 --- a/plugins/serendipity_event_karma/lang_es.inc.php +++ b/plugins/serendipity_event_karma/lang_es.inc.php @@ -1,6 +1,6 @@ - * EN-Revision: 690 */ diff --git a/plugins/serendipity_event_karma/lang_fa.inc.php b/plugins/serendipity_event_karma/lang_fa.inc.php index 60432cb0..fec746cc 100644 --- a/plugins/serendipity_event_karma/lang_fa.inc.php +++ b/plugins/serendipity_event_karma/lang_fa.inc.php @@ -1,7 +1,7 @@ - * EN-Revision: Revision of lang_fa.inc.php */ diff --git a/plugins/serendipity_event_karma/lang_fr.inc.php b/plugins/serendipity_event_karma/lang_fr.inc.php index 656808f8..33754c27 100644 --- a/plugins/serendipity_event_karma/lang_fr.inc.php +++ b/plugins/serendipity_event_karma/lang_fr.inc.php @@ -1,4 +1,4 @@ - * EN-Revision: 690 */ diff --git a/plugins/serendipity_event_karma/lang_ko.inc.php b/plugins/serendipity_event_karma/lang_ko.inc.php index 39fe6d8e..80b160e2 100644 --- a/plugins/serendipity_event_karma/lang_ko.inc.php +++ b/plugins/serendipity_event_karma/lang_ko.inc.php @@ -1,4 +1,4 @@ - # (c) 2005 http://www.tool-box.info/ diff --git a/plugins/serendipity_event_karma/lang_pl.inc.php b/plugins/serendipity_event_karma/lang_pl.inc.php index 12f5a480..e5ea1dcd 100644 --- a/plugins/serendipity_event_karma/lang_pl.inc.php +++ b/plugins/serendipity_event_karma/lang_pl.inc.php @@ -1,7 +1,7 @@ - * EN-Revision: Revision of lang_en.inc.php */ diff --git a/plugins/serendipity_event_karma/lang_tn.inc.php b/plugins/serendipity_event_karma/lang_tn.inc.php index 2bf1e5cc..f33c3e0b 100644 --- a/plugins/serendipity_event_karma/lang_tn.inc.php +++ b/plugins/serendipity_event_karma/lang_tn.inc.php @@ -1,4 +1,4 @@ -add('name', PLUGIN_KARMA_NAME); $propbag->add('description', PLUGIN_KARMA_BLAHBLAH); $propbag->add('stackable', false); - $propbag->add('author', 'Garvin Hicking, Grischa Brockhaus, Gregor Völtz, Judebert'); - $propbag->add('version', '2.6'); + $propbag->add('author', 'Garvin Hicking, Grischa Brockhaus, Judebert, Gregor Voeltz'); + $propbag->add('version', '2.7'); $propbag->add('requirements', array( 'serendipity' => '0.8', 'smarty' => '2.6.7', 'php' => '4.1.0' )); $propbag->add('event_hooks', array( + 'external_plugin' => true, + 'frontend_footer' => true, 'frontend_configure' => true, 'entry_display' => true, 'css' => true, @@ -73,10 +75,11 @@ class serendipity_event_karma extends serendipity_event 'track_visits_of_loggedin_users', 'min_disp_visits', 'exits_active', - 'logging', + 'logging', + 'ajax', // Appearance options 'appearance_tab', - //--JAM:'alignment', + 'alignment', 'rate_with_words', 'textual_msg', 'textual_current', @@ -238,11 +241,17 @@ class serendipity_event_karma extends serendipity_event $propbag->add('default', 'false'); break; - /*--JAM: + // Log karma votes? + case 'ajax': + $propbag->add('type', 'boolean'); + $propbag->add('name', PLUGIN_KARMA_AJAX); + $propbag->add('description', PLUGIN_KARMA_AJAX_BLAHBLAH); + $propbag->add('default', 'false'); + break; + // Alignment to use for rating bars case 'alignment': $select = array( - //--JAM: 'detect' => PLUGIN_KARMA_ALIGNMENT_DETECT, 'left' => LEFT, 'center' => CENTER, 'right' => RIGHT); @@ -252,7 +261,7 @@ class serendipity_event_karma extends serendipity_event $propbag->add('select_values', $select); $propbag->add('default', 'right'); break; - */ + // Use words for ratings? case 'rate_with_words': $propbag->add('type', 'boolean'); @@ -488,6 +497,207 @@ class serendipity_event_karma extends serendipity_event return sprintf($karma_exits, $points); } + + function performkarmaVote() { + global $serendipity; + // Make sure the karmaVote cookie is set, even if empty + if (!isset($serendipity['COOKIE']['karmaVote'])) { + serendipity_setCookie('karmaVote', serialize(array())); + } + + // If user didn't vote, we're done. + // Do we realy need this? + if (!isset($_POST['karmaId'])) { + if (!isset($serendipity['GET']['karmaId']) || !isset($serendipity['GET']['karmaVote'])) { + return; + } + } + + // Get URL vote data + if (isset($_POST['karmaId'])) { + $this->karmaId = (int)$_POST['karmaId']; + $this->karmaVoting = (int)$_POST['karmaVote']; + } else { + $this->karmaId = (int)$serendipity['GET']['karmaId']; + $this->karmaVoting = (int)$serendipity['GET']['karmaVote']; + } + + // karmaVote cookie was just set (see name="#1"); this boils down to + // "if check cookie isn't 1, there's no real cookie". + // The check cookie gets set when a rater is displayed, + // so you've got no business voting if you haven't even + // seen the rater yet. + if (!isset($serendipity['COOKIE']['karmaVote']) OR $serendipity['COOKIE']['check'] != '1') { + $this->karmaVote = 'nocookie'; + return; + } + + // Everything is ready. Get the cookie vote data. + $karma = unserialize($serendipity['COOKIE']['karmaVote']); + + // Stop on invalid votes (cookie invalid, or URL data incorrect) + if (!is_array($karma) || !is_numeric($this->karmaVoting) || !is_numeric($this->karmaId) || $this->karmaVoting > 2 || $this->karmaVoting < -2) { + $this->karmaVote = 'invalid1'; + return; + } + + // Stop if the cookie says we already voted + if (!empty($karma[$this->karmaId])) { + $this->karmaVote = 'alreadyvoted'; + return ; + } + + // We don't want bots hitting the karma-voting + $agent = $_SERVER['HTTP_USER_AGENT']; + if (stristr($agent, 'google') + || stristr($agent, 'LinkWalker') + || stristr($agent, 'zermelo') + || stristr($agent, 'NimbleCrawler')) { + $this->karmaVote = 'invalid1'; + return ; + } + + // Voting takes place here. + // + // Get voting data from the database (keeps all entries, + // even if no karma match) + $q = 'SELECT * + FROM ' . $serendipity['dbPrefix'] . 'entries AS e + LEFT OUTER JOIN ' . $serendipity['dbPrefix'] . 'karma AS k + ON e.id = k.entryid + WHERE e.id = ' . serendipity_db_escape_string($this->karmaId) . ' LIMIT 1'; + $row = serendipity_db_query($q, true); + + // If there's no entry with this ID, we're done + // + // --TODO: Modify the plugin to allow arbitrary voting with generated IDs + if (!isset($row) || !is_array($row)) { + $this->karmaVote = 'invalid2'; + return; + } + + $now = time(); + if ($row['votes'] === '0' || $row['votes'] > 0) { + // Votes for this entry already exist. Do some checking. + $max_entrytime = $this->get_config('max_entrytime', 1440) * 60; + $max_votetime = $this->get_config('max_votetime', 5) * 60; + $max_karmatime = $this->get_config('max_karmatime', 7) * 24 * 60 * 60; + // Allow infinite voting when 0 or negative + if ($max_karmatime <= 0) { + $max_karmatime = $now; + } + + // If the entry's timestamp is too old for voting, + // we're done. + if ($row['timestamp'] < ($now - $max_karmatime)) { + $this->karmaVote = 'timeout2'; + return; + } + + // If the entry is in the grace period, or votes + // aren't too close together, record the vote. + if (($row['timestamp'] > ($now - $max_entrytime)) || ($row['lastvote'] + $max_votetime < $now) || $row['lastvote'] == 0) { + // Update votes + $q = sprintf( + "UPDATE {$serendipity['dbPrefix']}karma + SET points = %s, + votes = %s, + lastvote = %s + WHERE entryid = %s", + $row['points'] + $this->karmaVoting, + $row['votes'] + 1, + $now, + $this->karmaId + ); + + serendipity_db_query($q); + } else { + // Entry was too recently voted upon. Figure out + // how long until voting will be allowed (in minutes). + $this->karmaVote = 'timeout'; + $this->karmaTimeOut = abs(ceil(($now - ($row['lastvote'] + $max_votetime)) / 60)); + return; + } + } else { + // No row. Use INSERT instead of UPDATE. + $q = sprintf( + "INSERT INTO {$serendipity['dbPrefix']}karma + (entryid, points, votes, lastvote, visits) + VALUES (%s, %s, %s, %s, %s)", + $this->karmaId, + $this->karmaVoting, + 1, + $now, + 0 + ); + + $sql = serendipity_db_query($q); + } + + // Log the vote + if (serendipity_db_bool($this->get_config('logging', false))) { + $q = sprintf( + "INSERT INTO {$serendipity['dbPrefix']}karmalog + (entryid, points, ip, user_agent, votetime) + VALUES (%s, %s, '%s', '%s', %s)", + $this->karmaId, + $this->karmaVoting, + serendipity_db_escape_string($_SERVER['REMOTE_ADDR']), + substr(serendipity_db_escape_string($_SERVER['HTTP_USER_AGENT']), 0, 255), + $now + ); + $sql = serendipity_db_query($q); + if (is_string($sql)) { + mail($serendipity['serendipityEmail'] , 'KARMA ERROR', $q . '
' . $sql . '
'); + } + } + + // Set the cookie that we already voted for this entry + $karma[$this->karmaId] = $this->karmaVoting; + $this->karmaVote = 'voted'; + serendipity_setCookie('karmaVote', serialize($karma)); + } + + function karmaVoted($myvote,$points,$votes) { + $msg = '
' . PLUGIN_KARMA_VOTED . '
'; + if ($this->get_config('rate_with_words', false)) { + $myvote = $this->wordRating($myvote, 1); + } + elseif ($this->image_name != '0') { + $myvote = $this->imageRating($myvote, 1); + } + // Just a current rating bar, if any + $bar = $this->createRatingBar(null, $points, $votes); + return array("myvote" => $myvote, "msg" => $msg, "bar" => $bar); + } + + function createkarmaBlock($entryid, $textual_msg, $msg, $bar, $enough_votes, $textual_current, $enough_visits, $textual_visits, $points, $votes) { + $image_class = ''; + if ($this->image_name != '0') { + $image_class = ' serendipity_karmaVoting_images'; + } + $karma_block = "
"; + if ($textual_msg) { + $karma_block .= $msg; + } + $karma_block .= $bar; + if ($enough_votes && $textual_current) { + $curr_msg = $this->get_config('curr_msg', PLUGIN_KARMA_CURRENT); + $karma_block .= '' . $curr_msg . ''; + } + if ($enough_visits && $textual_visits) { + $karma_block .= '' . PLUGIN_KARMA_VISITSCOUNT . ''; + } + $karma_block .= "\n
\n"; + // Adjust rating points + if ($this->get_config('rate_with_words', false)) { + $points = $this->wordRating($points, $votes); + } + elseif ($this->image_name != '0') { + $points = $this->imageRating($points, $votes); + } + return array("karma_block" => $karma_block, "points" => $points); + } function event_hook($event, &$bag, &$eventData, $addData = null) { global $serendipity; @@ -515,157 +725,88 @@ class serendipity_event_karma extends serendipity_event } switch($event) { + + //Javascript for ajax functionality + case 'frontend_footer': + if ($this->get_config('ajax') == true) { +?> + + $req) { + $val = explode('=', $req); + if (empty($_REQUEST[$val[0]])) { + $_REQUEST[$val[0]] = $val[1]; + } + } + } + + $parts = explode('_', $uri_parts[0]); + + switch($parts[0]) { + case 'karma-ajaxquery': + $this->performkarmaVote(); + $q = "SELECT SUM(votes) AS votes, SUM(points) AS points, SUM(visits) AS visits + FROM " . $serendipity['dbPrefix'] . "karma + WHERE entryid = '" . (int)$_POST['karmaId'] . "';"; + $sql = serendipity_db_query($q); + $track_clicks = serendipity_db_bool($this->get_config('visits_active', true)); + $track_karma = serendipity_db_bool($this->get_config('karma_active', true)); + $enough_votes = $track_karma && ($sql[0]['votes'] >= $this->get_config('min_disp_votes', 0)); + $enough_visits = $track_clicks && ($sql[0]['visits'] >= $this->get_config('min_disp_visits', 0)); + $textual_msg = true; + $textual_current = true; + $textual_visits = true; + if ($this->image_name != '0') { + $textual_msg = $this->get_config('textual_msg', 'true'); + $textual_current = $this->get_config('textual_current', 'true'); + $textual_visits = $this->get_config('textual_visits', 'true'); + } + $temp = $this->karmaVoted((int)$_POST['karmaVote'],$sql[0]['points'],$sql[0]['votes']); + $myvote = $temp['myvote']; + $msg = $temp['msg']; + $bar = $temp['bar']; + $temp = $this->createkarmaBlock((int)$_POST['karmaId'], $textual_msg, $msg, $bar, $enough_votes, $textual_current, $enough_visits, $textual_visits, $sql[0]['points'], $sql[0]['votes']); + $karma_block = $temp['karma_block']; + $points = $temp['points']; + echo sprintf($karma_block, $myvote, $points, $sql[0]['votes'], $sql[0]['visits'], ''); + break; + } + + return true; + break; + // Early hook, before any page is displayed case 'frontend_configure': - - // Make sure the karmaVote cookie is set, even if empty - if (!isset($serendipity['COOKIE']['karmaVote'])) { - serendipity_setCookie('karmaVote', serialize(array())); - } - - // If user didn't vote, we're done. - if (!isset($serendipity['GET']['karmaId']) || !isset($serendipity['GET']['karmaVote'])) { - return; - } - - // Get URL vote data - $this->karmaId = (int)$serendipity['GET']['karmaId']; - $this->karmaVoting = (int)$serendipity['GET']['karmaVote']; - - // karmaVote cookie was just set (see name="#1"); this boils down to - // "if check cookie isn't 1, there's no real cookie". - // The check cookie gets set when a rater is displayed, - // so you've got no business voting if you haven't even - // seen the rater yet. - if (!isset($serendipity['COOKIE']['karmaVote']) OR $serendipity['COOKIE']['check'] != '1') { - $this->karmaVote = 'nocookie'; - return; - } - - // Everything is ready. Get the cookie vote data. - $karma = unserialize($serendipity['COOKIE']['karmaVote']); - - // Stop on invalid votes (cookie invalid, or URL data incorrect) - if (!is_array($karma) || !is_numeric($this->karmaVoting) || !is_numeric($this->karmaId) || $this->karmaVoting > 2 || $this->karmaVoting < -2) { - $this->karmaVote = 'invalid1'; - return; - } - - // Stop if the cookie says we already voted - if (!empty($karma[$this->karmaId])) { - $this->karmaVote = 'alreadyvoted'; - return ; - } - - // We don't want bots hitting the karma-voting - $agent = $_SERVER['HTTP_USER_AGENT']; - if (stristr($agent, 'google') - || stristr($agent, 'LinkWalker') - || stristr($agent, 'zermelo') - || stristr($agent, 'NimbleCrawler')) { - $this->karmaVote = 'invalid1'; - return ; - } - - // Voting takes place here. - // - // Get voting data from the database (keeps all entries, - // even if no karma match) - $q = 'SELECT * - FROM ' . $serendipity['dbPrefix'] . 'entries AS e - LEFT OUTER JOIN ' . $serendipity['dbPrefix'] . 'karma AS k - ON e.id = k.entryid - WHERE e.id = ' . serendipity_db_escape_string($this->karmaId) . ' LIMIT 1'; - $row = serendipity_db_query($q, true); - - // If there's no entry with this ID, we're done - // - // --TODO: Modify the plugin to allow arbitrary voting with generated IDs - if (!isset($row) || !is_array($row)) { - $this->karmaVote = 'invalid2'; - return; - } - - $now = time(); - if ($row['votes'] === '0' || $row['votes'] > 0) { - // Votes for this entry already exist. Do some checking. - $max_entrytime = $this->get_config('max_entrytime', 1440) * 60; - $max_votetime = $this->get_config('max_votetime', 5) * 60; - $max_karmatime = $this->get_config('max_karmatime', 7) * 24 * 60 * 60; - // Allow infinite voting when 0 or negative - if ($max_karmatime <= 0) { - $max_karmatime = $now; - } - - // If the entry's timestamp is too old for voting, - // we're done. - if ($row['timestamp'] < ($now - $max_karmatime)) { - $this->karmaVote = 'timeout2'; - return; - } - - // If the entry is in the grace period, or votes - // aren't too close together, record the vote. - if (($row['timestamp'] > ($now - $max_entrytime)) || ($row['lastvote'] + $max_votetime < $now) || $row['lastvote'] == 0) { - // Update votes - $q = sprintf( - "UPDATE {$serendipity['dbPrefix']}karma - SET points = %s, - votes = %s, - lastvote = %s - WHERE entryid = %s", - $row['points'] + $this->karmaVoting, - $row['votes'] + 1, - $now, - $this->karmaId - ); - - serendipity_db_query($q); - } else { - // Entry was too recently voted upon. Figure out - // how long until voting will be allowed (in minutes). - $this->karmaVote = 'timeout'; - $this->karmaTimeOut = abs(ceil(($now - ($row['lastvote'] + $max_votetime)) / 60)); - return; - } - } else { - // No row. Use INSERT instead of UPDATE. - $q = sprintf( - "INSERT INTO {$serendipity['dbPrefix']}karma - (entryid, points, votes, lastvote, visits) - VALUES (%s, %s, %s, %s, %s)", - $this->karmaId, - $this->karmaVoting, - 1, - $now, - 0 - ); - - $sql = serendipity_db_query($q); - } - - // Log the vote - if (serendipity_db_bool($this->get_config('logging', false))) { - $q = sprintf( - "INSERT INTO {$serendipity['dbPrefix']}karmalog - (entryid, points, ip, user_agent, votetime) - VALUES (%s, %s, '%s', '%s', %s)", - $this->karmaId, - $this->karmaVoting, - serendipity_db_escape_string($_SERVER['REMOTE_ADDR']), - substr(serendipity_db_escape_string($_SERVER['HTTP_USER_AGENT']), 0, 255), - $now - ); - $sql = serendipity_db_query($q); - if (is_string($sql)) { - mail($serendipity['serendipityEmail'] , 'KARMA ERROR', $q . '
' . $sql . '
'); - } - } - - // Set the cookie that we already voted for this entry - $karma[$this->karmaId] = $this->karmaVoting; - $this->karmaVote = 'voted'; - serendipity_setCookie('karmaVote', serialize($karma)); + $this->performkarmaVote(); return true; break; @@ -737,7 +878,7 @@ class serendipity_event_karma extends serendipity_event } if ($align == 'detect') { */ - $align = $this->get_config('alignment', 'center'); + $align = $this->get_config('alignment'); // Try to let the template take care of it if ($this->image_name == '0') { // Text-only rating bar is used @@ -842,6 +983,9 @@ EOS; background: url($img) left; font-size: 0; } +.ajaxloader { + background-image: none; +} .serendipity_karmaVoting_links { position: relative; width: {$w}px; @@ -1210,17 +1354,10 @@ END_IMG_CSS; if ($track_karma) { if (isset($karma[$entryid])) { // We already voted for this one - $msg = - '
' . PLUGIN_KARMA_VOTED . '
'; - $myvote = $karma[$entryid]; - if ($this->get_config('rate_with_words', false)) { - $myvote = $this->wordRating($myvote, 1); - } - elseif ($this->image_name != '0') { - $myvote = $this->imageRating($myvote, 1); - } - // Just a current rating bar, if any - $bar = $this->createRatingBar(null, $points, $votes); + $temp = $this->karmaVoted($karma[$entryid],$points,$votes); + $myvote = $temp['myvote']; + $msg = $temp['msg']; + $bar = $temp['bar']; } elseif ($eventData[$i]['timestamp'] < ($now - $max_karmatime)) { // Too late to vote for this one $msg = @@ -1237,36 +1374,9 @@ END_IMG_CSS; } } // Create the karma block - $image_class = ''; - if ($this->image_name != '0') { - $image_class = ' serendipity_karmaVoting_images'; - } - $karma_block = -"
"; - if ($textual_msg) { - $karma_block .= - $msg; - } - $karma_block .= - $bar; - if ($enough_votes && $textual_current) { - $curr_msg = $this->get_config('curr_msg', PLUGIN_KARMA_CURRENT); - $karma_block .= - '' . $curr_msg . ''; - } - if ($enough_visits && $textual_visits) { - $karma_block .= - '' . PLUGIN_KARMA_VISITSCOUNT . ''; - } - $karma_block .= -"\n
\n"; - // Adjust rating points - if ($this->get_config('rate_with_words', false)) { - $points = $this->wordRating($points, $votes); - } - elseif ($this->image_name != '0') { - $points = $this->imageRating($points, $votes); - } + $temp = $this->createkarmaBlock($entryid, $textual_msg, $msg, $bar, $enough_votes, $textual_current, $enough_visits, $textual_visits, $points, $votes); + $karma_block = $temp['karma_block']; + $points = $temp['points']; /* print("

--DEBUG: Karma block code:

\n
\n");
@@ -1279,8 +1389,9 @@ END_IMG_CSS;
                                 $eventData[$i]['properties']['points'] = $points;
                                 $eventData[$i]['properties']['votes'] = $votes;
                                 $eventData[$i]['properties']['visits'] = $visits;
-
+                                
                                 $footer .= sprintf($karma_block, $myvote, $points, $votes, $visits, $url);
+
                             } // foreach key in entries
                     }// End switch on karma voting status
                     return true;
@@ -1783,11 +1894,13 @@ function invertSelection() {
         $images = array();
         $folder = opendir($path);
         while (false !== ($filename = readdir($folder))) {
-            $parts = serendipity_parseFileName($filename);
-            $img_data = serendipity_getimagesize($path . '/' . $filename);
-            if (!isset($img_data['noimage'])) {
-                // Curly braces are just a different syntax of associative array assignment
-                $images{$filename} = array('fname'=>$filename, 'width'=>$img_data[0], 'height'=>$img_data[1]);
+            if ($filename != "ajax-loader.gif") {
+                $parts = serendipity_parseFileName($filename);
+                $img_data = serendipity_getimagesize($path . '/' . $filename);
+                if (!isset($img_data['noimage'])) {
+                    // Curly braces are just a different syntax of associative array assignment
+                    $images{$filename} = array('fname'=>$filename, 'width'=>$img_data[0], 'height'=>$img_data[1]);
+                }
             }
         }
         closedir($folder);                               
@@ -1988,16 +2101,16 @@ function invertSelection() {
             $rating = ((float)$points) / ((float)$votes);
             // Put it into the language-specific string format, rounding up
             // I'm mapping invalid cases (rating > 5 and rating < -2) to the extremes
-            if ($rating <= -1.5) {
-                $rating = PLUGIN_KARMA_VOTEPOINT_1;
-            } elseif ($rating <= -0.5) {
-                $rating = PLUGIN_KARMA_VOTEPOINT_2;
-            } elseif ($rating <= 0.5) {
-                $rating = PLUGIN_KARMA_VOTEPOINT_3;
-            } elseif ($rating <= 1.5) {
-                $rating = PLUGIN_KARMA_VOTEPOINT_4;
+            if ($rating < -1.5) {
+                $rating = $this->get_config('rate_vile', PLUGIN_KARMA_VOTEPOINT_1);
+            } elseif ($rating < -0.5) {
+                $rating = $this->get_config('rate_poor', PLUGIN_KARMA_VOTEPOINT_2);
+            } elseif ($rating < 0.5) {
+                $rating = $this->get_config('rate_okay', PLUGIN_KARMA_VOTEPOINT_3);
+            } elseif ($rating < 1.5) {
+                $rating = $this->get_config('rate_good', PLUGIN_KARMA_VOTEPOINT_4);
             } else {
-                $rating = PLUGIN_KARMA_VOTEPOINT_5;
+                $rating = $this->get_config('rate_best', PLUGIN_KARMA_VOTEPOINT_5);
             }
         }
         return $rating;