[Laravel] FindRetros Voting Validator

Jerry

not rly active lol
Jul 8, 2013
1,956
522
Hi,

A few people have asked me to set this up for them since they were having issues implementing the voting validator to their Laravel application, so I'm going to release it here. I did not code it myself, all I did was make a new class and copy and pasted the code from .

Steps:
1. Make a new PHP file called FindRetros.php in the App\Http folder.

2. Paste this code to the file (replace 'pageName' with your FindRetros page name):
PHP:
<?php namespace App\Http;

class FindRetros {
    private $pageName, $callTimeout, $usingCloudFlare, $apiPath;

    function __construct() {
        $this->pageName        = 'RevivalRP';
        $this->requestTimeout  = 2;
        $this->usingCloudFlare = true;
        $this->apiPath         = 'https://findretros.com/';

        if ($this->usingCloudFlare) {

            if (isset($_SERVER['HTTP_CF_CONNECTING_IP']))
                $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
            else if (isset($_SERVER['HTTP_X_REAL_IP']))
                $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP'];
            else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
                $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];

        }

    }

    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 . 'servers/' . $this->pageName . '/vote?minimal=1');
        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;
    }
}

3. Then call the file whereever you want, I called mine in one of the files in the App\Http\Middleware\Session folder.

Example (it will only be called in production environment):
PHP:
if (App::environment('production', 'staging')) {
    if (!$request->exists('novote')) {
        $FindRetros = new App\Http\FindRetros();
      
        if (!$FindRetros->hasClientVoted()) {
            $FindRetros->redirectClientToVote();
        }
    }
}

Example of a full middleware file:
Code:
<?php

namespace App\Http\Middleware\Session;

use Illuminate\Support\Facades\Auth;

use App;
use Closure;

class Guest
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            return redirect('/me');
        }

        if (App::environment('production', 'staging')) {
            if (!$request->exists('novote')) {
                $FindRetros = new App\Http\FindRetros();

                if (!$FindRetros->hasClientVoted()) {
                    $FindRetros->redirectClientToVote();
                }
            }
        }

        return $next($request);
    }
}

Then that's it, you have the FindRetros Voting Validator setup on your Laravel application!
 

treebeard

Member
Jan 16, 2018
317
173
Good release; I see many asking what Laravel is and trying to use it with no knowledge what it is just because they saw that some retros use it.
This is sure to help many! Good job ^.^
 

Users who are viewing this thread

Top