diff -r 6904847e956b -r 87ba86c9d5a6 karma.php --- a/karma.php Fri Jul 30 23:03:02 2010 -0400 +++ b/karma.php Fri Jul 30 23:03:33 2010 -0400 @@ -2,7 +2,7 @@ /**!info** { "Plugin Name" : "Karma", - "Plugin URI" : "http://example.com/", + "Plugin URI" : "http://enanocms.org/plugin/karma", "Description" : "Karma is a plugin that enables in the user page a voting system, to evaluate the popularity of each member.", "Author" : "Adriano Pereira", "Version" : "1.0", @@ -21,13 +21,15 @@ { // If the user votes, get the vote - $vote = @$_GET['vote']; + $vote = !empty($_GET['vote']) && in_array($_GET['vote'], array('Yes', 'No')) + ? $_GET['vote'] + : null; // Get the user_id from the user that is voting $user_voting_id = $session->user_id; // Find the page_id that is the username of the current user page and gets the user_id from database - $username = $paths->page_id; + $username = str_replace('_', ' ', dirtify_page_id($paths->page_id)); $q = $db->sql_query('SELECT user_id FROM '. table_prefix. "users WHERE username = '$username'"); if ( !$q ) @@ -36,7 +38,7 @@ $user_voted_id = $voted['user_id']; // Retrieves from database the total votes, yes votes, no votes and the karma from user - $q = $db->sql_query('SELECT karma, karma_total_votes, karma_yes_votes, karma_no_votes FROM '. table_prefix."users_extra WHERE user_id = '$user_voted_id'"); + $q = $db->sql_query('SELECT karma_yes_votes, karma_no_votes, (karma_yes_votes + karma_no_votes) AS karma_total_votes, (karma_yes_votes - karma_no_votes) AS karma FROM '. table_prefix."users_extra WHERE user_id = '$user_voted_id'"); if ( !$q ) $db->_die(); $karma_info = $db->fetchrow(); @@ -50,78 +52,86 @@ if ( !$q ) $db->_die(); $num_votes = $db->numrows(); + $db->free_result(); // If the user that votes and the user voted id is equal or the user has already voted, displays the commom page - if ($user_voting_id == $user_voted_id) goto commom_page_title; - if ($num_votes == 0 && empty($vote)) goto vote; + // If we're on our own user page, block voting + $same_user = $user_voting_id === $user_voted_id; - if ($num_votes != 0) goto commom_page_title; - - // Know if the vote is yes or no and do the respective action in database - if ($vote == 'Yes') - { - $karma = $karma + 1; - $total_votes = $total_votes + 1; - $yes_votes = $yes_votes + 1; - $q = $db->sql_query('INSERT INTO '. table_prefix."karma (user_voting_id, user_voted_id) VALUES ('$user_voting_id', '$user_voted_id')"); - if ( !$q ) - $db->_die(); - $q = $db->sql_query('UPDATE '. table_prefix."users_extra SET karma = '$karma', karma_total_votes = '$total_votes', karma_yes_votes = '$yes_votes' WHERE user_id = '$user_voted_id'"); - if ( !$q ) - $db->_die(); - } - elseif ($vote == 'No') + // If we have not yet voted on this user, allow that to take place below + $can_vote = $num_votes == 0 && !$same_user && $session->user_level >= USER_LEVEL_MEMBER; + + echo "