prepare("SELECT COUNT(*) AS num, sect FROM note WHERE id = :id"); if (!$noteStmt) { return false; } if (!$noteStmt->execute(['id' => $id])) { return false; } if (false === $noteResult = $noteStmt->fetch(PDO::FETCH_ASSOC)) { return false; } if ($noteResult['sect'] !== $_POST['sect']) { return false; } // Validate remote IP has not exceeded voting limits $remoteStmt = $dbh->prepare("SELECT COUNT(*) AS num FROM votes WHERE ip = :ip AND ts >= (NOW() - INTERVAL 1 DAY) AND note_id = :id"); if (!$remoteStmt) { return false; } if (!$remoteStmt->execute(['ip' => $ip, 'id' => $id])) { return false; } if (false === $remoteResult = $remoteStmt->fetch(PDO::FETCH_ASSOC)) { return false; } if ($remoteResult['num'] >= 1) { // Limit of 1 vote, per note, per remote IP, per day. return false; } // Validate host IP has not exceeded voting limits $hostStmt = $dbh->prepare("SELECT COUNT(*) AS num FROM votes WHERE hostip = :ip AND ts >= (NOW() - INTERVAL 1 HOUR) AND note_id = :id"); if (!$hostStmt) { return false; } if (!$hostStmt->execute(['ip' => $ip, 'id' => $id])) { return false; } if (false === $hostResult = $hostStmt->fetch(PDO::FETCH_ASSOC)) { return false; } if ($hostResult['num'] >= 100) { // Limit of 100 votes, per note, per host IP, per hour. return false; } // Inser the new vote $voteStmt = $dbh->prepare("INSERT INTO votes(note_id,ip,hostip,ts,vote) VALUES(:id,:ip,:host,:ts,:vote)"); if (!$voteStmt) { return false; } if (!$voteStmt->execute(['id' => $id, 'ip' => $ip, 'host' => $hostip, 'ts' => $ts, 'vote' => $vote])) { return false; } // Get latest vote tallies for this note $voteStmt = $dbh->prepare("SELECT SUM(votes.vote) AS up, (COUNT(votes.vote) - SUM(votes.vote)) AS down FROM votes WHERE votes.note_id = :id"); if (!$voteStmt) { return false; } if (!$voteStmt->execute(['id' => $id])) { return false; } if (false === $voteResult = $voteStmt->fetch(PDO::FETCH_ASSOC)) { return false; } // Return the new vote tally for this note return $voteResult['up'] - $voteResult['down']; } // Initialize global JSON response object $jsonResponse = new stdclass; $jsonResponse->status = false; // Validate the request if (!isset($_SERVER['REQUEST_METHOD']) || strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST') { $jsonResponse->message = "Invalid request..."; echo json_encode($jsonResponse); exit; } // Initialize global PDO database handle try { $dbh = new PDO('mysql:host=localhost;dbname=phpmasterdb', 'nobody', ''); } catch(PDOException $e) { $jsonResponse->message = "The server could not complete this request. Please try again later..."; echo json_encode($jsonResponse); exit; } // Check master DB for hostip and clientip limits and other validations if (($jsonResponse->votes = vote_validate_request($dbh)) === false) { $jsonResponse->message = "Unable to complete your request at this time. Please try again later..."; echo json_encode($jsonResponse); exit; } // If everything passes the response should be the new jsonResponse object with updated votes and success status $jsonResponse->status = true; echo json_encode($jsonResponse);