22/*
33 This script acts as the backend communication API for the user notes vote feature.
44 Requests come in here from the php.net to update the database with new votes.
5- master .php.net should respond with a JSON object (with one required property [status] and two optional properties
5+ main .php.net should respond with a JSON object (with one required property [status] and two optional properties
66 [votes] and [message]).
77 The JSON object [status] property contains a status returned by the server for that request.
88 It's value may be either a boolean true or false. If the status is true the php.net will know the vote went through successfully.
@@ -32,31 +32,31 @@ function vote_validate_request(PDO $dbh) {
3232 FILTER_FLAG_NO_PRIV_RANGE |
3333 FILTER_FLAG_IPV4 ))
3434 {
35- $ ip = sprintf ("%u " , ip2long ($ _POST ['ip ' ]));
35+ $ ip = sprintf ("%u " , ip2long ($ _POST ['ip ' ]));
3636 } else {
3737 // If the IP can't be validated use a non routable IP for loose validation (i.e. IPv6 and clients that couldn't send back proper IPs)
38- $ ip = 0 ;
38+ $ ip = 0 ;
3939 }
40-
40+
4141 if (isset ($ _SERVER ['REMOTE_ADDR ' ]) &&
4242 filter_var ($ _SERVER ['REMOTE_ADDR ' ], FILTER_VALIDATE_IP , FILTER_FLAG_NO_RES_RANGE |
4343 FILTER_FLAG_NO_PRIV_RANGE |
4444 FILTER_FLAG_IPV4 ))
4545 {
46- $ hostip = sprintf ("%u " , ip2long ($ _SERVER ['REMOTE_ADDR ' ]));
46+ $ hostip = sprintf ("%u " , ip2long ($ _SERVER ['REMOTE_ADDR ' ]));
4747 } else {
4848 // If the IP can't be validated use a non routable IP for loose validation (i.e. IPv6 and clients that couldn't send back proper IPs)
4949 $ hostip = 0 ;
5050
5151 }
52-
52+
5353 if (!empty ($ _POST ['noteid ' ]) && filter_var ($ _POST ['noteid ' ], FILTER_VALIDATE_INT ))
5454 {
5555 $ id = filter_var ($ _POST ['noteid ' ], FILTER_VALIDATE_INT );
5656 } else {
5757 return false ;
5858 }
59-
59+
6060 if (!empty ($ _POST ['vote ' ]) && ($ _POST ['vote ' ] === 'up ' || $ _POST ['vote ' ] === 'down ' ))
6161 {
6262 $ vote = $ _POST ['vote ' ] === 'up ' ? 1 : 0 ;
@@ -81,7 +81,7 @@ function vote_validate_request(PDO $dbh) {
8181 if ($ noteResult ['sect ' ] !== $ _POST ['sect ' ]) {
8282 return false ;
8383 }
84-
84+
8585 // Validate remote IP has not exceeded voting limits
8686 $ remoteStmt = $ dbh ->prepare ("SELECT COUNT(*) AS num FROM votes WHERE ip = :ip AND ts >= (NOW() - INTERVAL 1 DAY) AND note_id = :id " );
8787 if (!$ remoteStmt ) {
@@ -96,7 +96,7 @@ function vote_validate_request(PDO $dbh) {
9696 if ($ remoteResult ['num ' ] >= 1 ) { // Limit of 1 vote, per note, per remote IP, per day.
9797 return false ;
9898 }
99-
99+
100100 // Validate host IP has not exceeded voting limits
101101 $ hostStmt = $ dbh ->prepare ("SELECT COUNT(*) AS num FROM votes WHERE hostip = :ip AND ts >= (NOW() - INTERVAL 1 HOUR) AND note_id = :id " );
102102 if (!$ hostStmt ) {
0 commit comments