Exemplo n.º 1
0
if (!check_security_key($_REQUEST['key'])) {
    error(_('clave de control incorrecta'));
}
if (empty($_REQUEST['value']) || !is_numeric($_REQUEST['value'])) {
    error(_('falta valor del voto'));
}
if ($current_user->user_karma < $globals['min_karma_for_post_votes']) {
    error(_('karma bajo para votar comentarios'));
}
$value = intval($_REQUEST['value']);
if ($value != -1 && $value != 1) {
    error(_('valor del voto incorrecto'));
}
$vote = new Vote('posts', $id, $current_user->user_id);
$vote->link = $id;
if ($vote->exists()) {
    error(_('ya se votó antes con el mismo usuario o IP'));
}
$votes_freq = intval($db->get_var("select count(*) from votes where vote_type='posts' and vote_user_id={$current_user->user_id} and vote_date > subtime(now(), '0:0:30') and vote_ip_int = " . $globals['user_ip_int']));
$freq = 6;
if ($votes_freq > $freq) {
    if ($current_user->user_id > 0 && $current_user->user_karma > 4) {
        // Crazy votes attack, decrease karma
        // she does not deserve it :-)
        $user = new User();
        $user->id = $current_user->user_id;
        $user->read();
        $user->karma = $user->karma - 0.1;
        $user->store();
        error(_('¡tranquilo cowboy!, tu karma ha bajado: ') . $user->karma);
    } else {
Exemplo n.º 2
0
  /**
   * return "OK" only delete
   *        > 0 - usual behavior: first delete then insert
   *        0  error
   */
	function insert_vote($value = 0) {
		global $current_user, $db;

		if (!$value) $value = $current_user->user_karma;

		$vote = new Vote('comments', $this->id, $current_user->user_id);

    $result = 'CREATE'; // vote doesn't exits?

		if ($old_value = $vote->exists(false)) { // save old vote value
      $result = 'REPLACE';
      $vote->delete_comment_vote($old_value); // always destroy current vote

      // check if they have the same sign
      if ($value * $old_value > 0) {
        return Array('DELETE',$old_value); // equal => only delete
      }
		}

		// Affinity
		if ($current_user->user_id != $this->author
				&& ($affinity = User::get_affinity($this->author, $current_user->user_id)) ) {
			if ($value < -1 && $affinity < 0) {
					$value = round(min(-1, $value *  abs($affinity/100)));
			} elseif ($value > 1 && $affinity > 0) {
					$value = round(max($value * $affinity/100, 1));
			}
		}

    $value>0?$svalue='+'.$value:$svalue=$value;

		$vote->value = $value;
		$db->transaction();
		if($vote->insert()) {
			if ($current_user->user_id != $this->author) {
        //echo "update comments set comment_votes=comment_votes+1, comment_karma=comment_karma$svalue, comment_date=comment_date where comment_id=$this->id";
				$db->query("update comments set comment_votes=comment_votes+1, comment_karma=comment_karma$svalue, comment_date=comment_date where comment_id=$this->id");
			}
		} else {
			$vote->value = false;
		}
		$db->commit();

    if ($result == 'CREATE') {
      return Array($result, $vote->value);
    } else { // replace
      return Array($result, $old_value);
    }
	}
Exemplo n.º 3
0
 function insert_vote($user_id = false, $value = 0)
 {
     global $current_user, $db;
     if (!$user_id) {
         $user_id = $current_user->user_id;
     }
     if (!$value && $current_user->user_karma) {
         $value = $current_user->user_karma;
     }
     $vote = new Vote('posts', $this->id, $user_id);
     $vote->link = $this->id;
     if ($vote->exists(true)) {
         return false;
     }
     $vote->value = $value;
     $db->transaction();
     if ($r = $vote->insert()) {
         if ($current_user->user_id != $this->author) {
             $r = $db->query("update posts set post_votes=post_votes+1, post_karma=post_karma+{$value}, post_date=post_date where post_id={$this->id}");
         }
     }
     $c = $db->commit();
     if ($r && $c) {
         return $vote->value;
     }
     syslog(LOG_INFO, "failed insert post vote for {$this->id}");
     return false;
 }
Exemplo n.º 4
0
	function insert_vote($user_id = false, $value = 0) {
		global $current_user, $db;

		if (! $user_id) $user_id = $current_user->user_id;
		if (! $value && $current_user->user_karma) {
			$value = $current_user->user_karma;
		}

		$vote = new Vote('posts', $this->id, $user_id);
		$vote->link=$this->id;
		if ($vote->exists(true)) {
			return false;
		}
		$vote->value = $value;
		$db->transaction();
		if($vote->insert()) {
			if ($current_user->user_id != $this->author) {
				$db->query("update posts set post_votes=post_votes+1, post_karma=post_karma+$value, post_date=post_date where post_id=$this->id");
			}
		} else {
			$vote->value = false;
		}
		$db->commit();
		return $vote->value;
	}
Exemplo n.º 5
0
	function insert_vote($value, $former_value = null) {
		global $db, $current_user, $globals;

		$vote = new Vote('links', $this->id, $current_user->user_id);
		if ($vote->exists(true)) return false;
		// For karma calculation
		if ($this->status != 'published') {
			if($value < 0 && $current_user->user_id > 0) {
				if ($current_user->user_id != $this->author && 
						($affinity = User::get_affinity($this->author, $current_user->user_id)) <  0 ) {
					$karma_value = round(min(-5, $current_user->user_karma *  ($affinity/100)));
				} else {
					$karma_value = round(-$current_user->user_karma);
				}
			} else {
				if ($current_user->user_id  > 0 && $current_user->user_id != $this->author && 
						($affinity = User::get_affinity($this->author, $current_user->user_id)) > 0 ) {
					$karma_value = $value = round(max($current_user->user_karma * ($affinity/100), 5));
				} else {
					$karma_value=round($value);
				}
			}
		} else {
			$karma_value = 0;
		}


    // former_value only for negatives
    if ($former_value != null) {
      $vote->value=$former_value;
      $karma_value = $karma_value /3;
    } else {
      $vote->value=$value;
    }

		$db->transaction();
		if($vote->insert()) {
			// For published links we update counter fields
			if ($this->status == 'published') {
				if ($vote->user > 0) {
					if ($value > 0) {
						$db->query("update links set link_votes=link_votes+1 where link_id = $this->id");
					} else {
						$db->query("update links set link_negatives=link_negatives+1 where link_id = $this->id");
					}
				} else {
					$db->query("update links set link_anonymous=link_anonymous+1 where link_id = $this->id");
				}
			} else {
				// If not published we update karma and count all votes
				$db->query("update links set link_karma=link_karma+$karma_value where link_id = $this->id");
				$this->update_votes();
			}
			$db->commit();
			$this->read_basic();
		} else {
			$db->commit();
			$value = false;
		}
		return $value;
	}
Exemplo n.º 6
0
	function insert_vote($user=0) {
		global $anon_karma;
		require_once(mnminclude.'votes.php');

		$vote = new Vote;
		$vote->user=$user;
		$vote->link=$this->id;
		if ($vote->exists()) return false;
		$vote->value=$anon_karma;
		if($user>0) {
			require_once(mnminclude.'user.php');
			$dbuser = new User($user);
			if($dbuser->id>0) {
				$vote->value = round($dbuser->karma);
			}
		}
		if($vote->insert()) {
			$vote->user=-1;
			$this->votes=$vote->count();
			$this->store_basic();
			return true;
		}
		return false;
	}
Exemplo n.º 7
0
 function insert_vote($value = 0)
 {
     global $current_user, $db;
     if (!$value) {
         $value = $current_user->user_karma;
     }
     $vote = new Vote('comments', $this->id, $current_user->user_id);
     // Affinity
     if ($current_user->user_id != $this->author && ($affinity = User::get_affinity($this->author, $current_user->user_id))) {
         if ($value < -1 && $affinity < 0) {
             $value = round(min(-1, $value * abs($affinity / 100)));
         } elseif ($value > 1 && $affinity > 0) {
             $value = round(max($value * $affinity / 100, 1));
         }
     }
     if ($vote->exists(true)) {
         return false;
     }
     $vote->value = $value;
     $db->transaction();
     if ($r = $vote->insert()) {
         if ($current_user->user_id != $this->author) {
             $r = $db->query("update comments set comment_votes=comment_votes+1, comment_karma=comment_karma+{$value}, comment_date=comment_date where comment_id={$this->id}");
         }
     }
     if ($r && $db->commit()) {
         return $vote->value;
     }
     syslog(LOG_INFO, "failed insert comment vote for {$this->id}");
     return false;
 }
Exemplo n.º 8
0
 function insert_vote($user, $value)
 {
     global $db, $current_user;
     require_once mnminclude . 'votes.php';
     $vote = new Vote();
     $vote->user = $user;
     $vote->link = $this->id;
     if ($vote->exists()) {
         return false;
     }
     $vote->value = $value;
     // For karma calculation
     if ($this->status != 'published') {
         if ($value < 0 && $user > 0) {
             $karma_value = round(($value - $current_user->user_karma) / 2);
         } else {
             $karma_value = round($value);
         }
     } else {
         $karma_value = 0;
     }
     if ($vote->insert()) {
         if ($value < 0) {
             $db->query("update links set link_negatives=link_negatives+1, link_karma=link_karma+{$karma_value} where link_id = {$this->id}");
         } else {
             $db->query("update links set link_votes = link_votes+1, link_karma=link_karma+{$karma_value} where link_id = {$this->id}");
         }
         $new = $db->get_row("select link_votes, link_negatives, link_karma from links where link_id = {$this->id}");
         $this->votes = $new->link_votes;
         $this->negatives = $new->link_negatives;
         $this->karma = $new->link_karma;
         return true;
     }
     return false;
 }
Exemplo n.º 9
0
 function insert_vote($value)
 {
     global $db, $current_user, $globals;
     $vote = new Vote('links', $this->id, $current_user->user_id);
     if ($vote->exists(true)) {
         return false;
     }
     // For karma calculation
     $status = !empty($this->sub_status) ? $this->sub_status : $this->status;
     $vote_value = $value > 0 ? $value : -$current_user->user_karma;
     $karma_value = round($vote_value);
     /******* Simplified, it doesn't make much sense with subs
     		if ($status != 'published') {
     			if($value < 0 && $current_user->user_id > 0) {
     				if ($globals['karma_user_affinity'] && $current_user->user_id != $this->author &&
     						($affinity = User::get_affinity($this->author, $current_user->user_id)) <  0 ) {
     					$karma_value = round(min(-5, $current_user->user_karma *  $affinity/100));
     				} else {
     					$karma_value = round($vote_value);
     				}
     			} else {
     				if ($globals['karma_user_affinity'] && $current_user->user_id  > 0 && $current_user->user_id != $this->author &&
     						($affinity = User::get_affinity($this->author, $current_user->user_id)) > 0 ) {
     					$karma_value = $value = round(max($current_user->user_karma * $affinity/100, 5));
     				} else {
     					$karma_value=round($vote_value);
     				}
     			}
     		} else {
     			$karma_value=round($vote_value);
     		}
     */
     $vote->value = $value;
     $db->transaction();
     if ($vote->insert()) {
         if ($vote->user > 0) {
             if ($value > 0) {
                 $what = 'link_votes=link_votes+1';
             } else {
                 $what = 'link_negatives=link_negatives+1';
             }
         } else {
             $what = 'link_anonymous=link_anonymous+1';
         }
         $r = $db->query("update links set {$what}, link_karma=link_karma+{$karma_value} where link_id = {$this->id}");
         if (!$r) {
             syslog(LOG_INFO, "failed transaction in Link::insert_vote: {$this->id} ({$r})");
             $value = false;
         } else {
             // Update in memory object
             if ($vote->user > 0) {
                 if ($value > 0) {
                     $this->votes += 1;
                 } else {
                     $this->negatives += 1;
                 }
             } else {
                 $this->anonymous += 1;
             }
             // Update karma and check votes
             if ($status != 'published') {
                 $this->karma += $karma_value;
                 $this->update_votes();
             }
         }
         $db->commit();
     } else {
         $db->rollback();
         $value = false;
     }
     return $value;
 }
Exemplo n.º 10
0
 function vote_exists($user)
 {
     $vote = new Vote('links', $this->id, $user);
     return $vote->exists(false);
 }
Exemplo n.º 11
0
 function insert_vote()
 {
     global $current_user;
     require_once mnminclude . 'votes.php';
     $vote = new Vote();
     $vote->user = $current_user->user_id;
     $vote->type = 'posts';
     $vote->link = $this->id;
     if ($vote->exists()) {
         return false;
     }
     $vote->value = $current_user->user_karma;
     if ($vote->insert()) {
         return true;
     }
     return false;
 }
Exemplo n.º 12
0
 function insert_vote($value)
 {
     global $db, $current_user, $globals;
     $vote = new Vote('links', $this->id, $current_user->user_id);
     if ($vote->exists(true)) {
         return false;
     }
     // For karma calculation
     if ($this->status != 'published') {
         if ($value < 0 && $current_user->user_id > 0) {
             if ($globals['karma_user_affinity'] && $current_user->user_id != $this->author && ($affinity = User::get_affinity($this->author, $current_user->user_id)) < 0) {
                 $karma_value = round(min(-5, $current_user->user_karma * $affinity / 100));
             } else {
                 $karma_value = round(-$current_user->user_karma);
             }
         } else {
             if ($globals['karma_user_affinity'] && $current_user->user_id > 0 && $current_user->user_id != $this->author && ($affinity = User::get_affinity($this->author, $current_user->user_id)) > 0) {
                 $karma_value = $value = round(max($current_user->user_karma * $affinity / 100, 5));
             } else {
                 $karma_value = round($value);
             }
         }
     } else {
         $karma_value = 0;
     }
     $vote->value = $value;
     $db->transaction();
     if ($vote->insert()) {
         if ($vote->user > 0) {
             if ($value > 0) {
                 $r = $db->query("update links set link_votes=link_votes+1 where link_id = {$this->id}");
             } else {
                 $r = $db->query("update links set link_negatives=link_negatives+1 where link_id = {$this->id}");
             }
         } else {
             $r = $db->query("update links set link_anonymous=link_anonymous+1 where link_id = {$this->id}");
         }
         // For published links we update counter fields
         if ($r && $this->status != 'published') {
             // If not published we update karma and count all votes
             $r = $db->query("update links set link_karma=link_karma+{$karma_value} where link_id = {$this->id}");
         }
         if (!$r) {
             syslog(LOG_INFO, "failed transaction in Link::insert_vote: {$this->id} ({$r})");
             $value = false;
         } else {
             // Update in memory object
             if ($vote->user > 0) {
                 if ($value > 0) {
                     $this->votes += 1;
                 } else {
                     $this->negatives += 1;
                 }
             } else {
                 $this->anonymous += 1;
             }
             // Update karma and check votes
             if ($this->status != 'published') {
                 $this->karma += $karma_value;
                 $this->update_votes();
             }
         }
         $db->commit();
     } else {
         $db->rollback();
         $value = false;
     }
     return $value;
 }