[Help] FindRetros force vote not working, using main API from site.

Status
Not open for further replies.

BizarreDio

New Member
Mar 17, 2018
26
2
Currently trying to get the FindRetro's API from the site itself working on my retro. Testing the " " with different IP's returns values of 2 or 3, so the system IS detecting whether or not the player has voted. However, whether they have or not, the redirect doesn't work and players dont need to vote to enter the game.

Here's the config:
PHP:
<?php

$_CONFIG = array(
    /* What is "username" of your page on FindRetros? */
    'pagename' => 'silverfork',

    /* How many seconds should it take to try and reach FindRetros if it's down? */
    'timeout'  => 5,

    /* Are you using CloudFlare? If so, set this to true. */
    'cloudflare' => true,

    /* Incase FindRetros.com has a new domain, change it below. */
    'api' => 'https://findretros.com/'
);

The Findretros.php file:
PHP:
<?php

class FindRetros {

    private $pageName, $callTimeout, $usingCloudFlare, $apiPath;

    function __construct() {

        global $_CONFIG;

        $this->pageName        = $_CONFIG['pagename'];
        $this->requestTimeout  = $_CONFIG['timeout'];
        $this->usingCloudFlare = $_CONFIG['cloudflare'];
        $this->apiPath         = $_CONFIG['api'];

        if($this->usingCloudFlare) {

            if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {

                $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];

            }

        }

    }

    public function hasClientVoted() {
  
        if(!$this->_isVoteCookieSet()) {

            $urlRequest = $this->apiPath . 'validate.php?user=' . $this->pageName . '&ip=' . $_SERVER['REMOTE_ADDR'];

            $dataRequest = $this->_makeCurlRequest($urlRequest);

            if(in_array($dataRequest, array(1, 2))) {

                $this->_setVoteCookie();

                return true;

            }else if($dataRequest == 3) {

                return false;

            }else{

                /* There's something wrong with FindRetros, so we will mark the user as voted and have them proceed as if they voted. */

                $this->_setVoteCookie();

                return true;

            }

        }

        return true;

    }

    public function redirectClientToVote() {

        header('Location: ' . $this->apiPath . 'rankings/vote/' . $this->pageName);

        exit;

    }  

    private function _makeCurlRequest($url) {

        if(function_exists('curl_version')) {

            $curl = curl_init();

            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_HEADER, 0);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($curl, CURLOPT_TIMEOUT, $this->requestTimeout);
            curl_setopt($curl, CURLOPT_USERAGENT, 'FindRetros Vote Validator');

            $requestData = curl_exec($curl);

            curl_close($curl);

        }else{

            $requestData = stream_context_create(array('http' => array('timeout' => $this->requestTimeout)));

            return @file_get_contents($url, 0, $requestData);

        }

        return $requestData;

    }

    private function _setVoteCookie() {

        $rankingsResetTime = $this->_getRankingsResetTime();

        setcookie('voting_stamp', $rankingsResetTime, $rankingsResetTime);

    }

    private function _isVoteCookieSet() {

        if(isset($_COOKIE['voting_stamp'])) {

            if($_COOKIE['voting_stamp'] == $this->_getRankingsResetTime()) {

                return true;

            }else{

                setcookie('voting_stamp', '');

                return false;

            }

        }

        return false;

    }

    private function _getRankingsResetTime() {

        $serverDefaultTime = date_default_timezone_get();

        date_default_timezone_set('America/Chicago');

        $rankingsResetTime = mktime(0, 0, 0, date('n'), date('j') + 1);
  
        date_default_timezone_set($serverDefaultTime);
      
        return $rankingsResetTime;

    }

}

And the top of the /client .php file:
PHP:
<?php

require_once 'findretros_config.php';
require_once 'findretros.php';

$FindRetros = new FindRetros();

if($FindRetros->hasClientVoted()) {
}else{

    // echo 'You have yet to vote!';

    $FindRetros->redirectClientToVote();

};
?>
 
Bump.
 
Bump again.
 

Synapse

PogChamp
Mar 3, 2012
164
59
First of all, FindRetros api is slightly old. It hasn't been updated since the newest website has been live. Since now when you vote if you don't pass the return argument and set it equal to one, it may not redirect back to the destination you specified. To work around this modify the code to include the return parameter.
PHP:
public function redirectClientToVote() {

        header('Location: ' . $this->apiPath . 'rankings/vote/' . $this->pageName . '/?return=1');

        exit;

    }

As for your question about the meaning of 3 or 2; 3 means the user has not voted, and 2 means it has. Please refer to the API's documentation. .


Most hotels just use the redirect function built in, so going to /client will redirect to the api, if your user has voted it will go the location you specify, usually /client/voted. By doing this you pass a $_GET['voted'] parameter and use an if statement, should look something like this:
PHP:
<?php
$voted = $_GET['voted'];
if(!isset($voted)){
     header('Location: 'https://findretros.com/rankings/vote/<YOUR PAGE NAME>/?return=1');
}
?>
Then if you use RevCMS to get a pretty url (otherwise you'd have to use index.php?url=client&voted) you must set a urlrewrite to pass the voted argument with /client/voted. You must also change the target location after vote to be the one you specify with this code. There's better ways of doing this, but I see MOST hotels doing it this way. You could go further and do cookies to prevent loads of redirects, this makes it quicker for your users, and reduces load on FindRetros end as well.
 

BizarreDio

New Member
Mar 17, 2018
26
2
Solved; I just tried using a different skin that had voting presetup for it and edited to match my website. if anyone needs help.
 

Synapse

PogChamp
Mar 3, 2012
164
59
Lol. My post literally had everything you needed to know about it. Dunno why you didn't just read it instead of going thru those lengths. LUL
 
Status
Not open for further replies.

Users who are viewing this thread

Top