<commit_before><?php

/*
 * This file is part of the Silex framework.
 *
 * Copyright (c) 2013 clover studio official account
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Silex\Application;

function abortManually($errMessage){
    $arr = array('message' => $errMessage, 'error' => 'logout');
    header("HTTP/1.0 403 Forbidden");
    echo json_encode($arr);
    die();
}

function makeBeforeTokenChecker()
{
    return function (Request $request, Application $app) {

        $headers = getallheaders();
        $tokenReceived = $headers['token'];
        $useridReceived = $headers['user_id'];
        $isCreateUserRequest = false;

        return;

        $app['monolog']->addDebug("token : {$tokenReceived}");
        $app['monolog']->addDebug("medhod : " . $request->getMethod());
        $app['monolog']->addDebug("user id : {$useridReceived}");
        $app['monolog']->addDebug(print_r($_SERVER,true));

        if($request->getMethod() == "POST" && $useridReceived == "create_user"){
            $isCreateUserRequest = true;
            return;
        }

        if(empty($tokenReceived) || empty($useridReceived)){
            abortManually("No token sent");
        }

        $query = "?key=" . urlencode('"' . $useridReceived . '"');
        $result = $app['spikadb']->doGetRequest("/_design/app/_view/find_user_by_id{$query}",false);
        $userData = json_decode($result, true);

        if(!isset($userData['rows'][0]['value']['_id']) || $userData['rows'][0]['value']['_id'] != $useridReceived){
            abortManually("No token sent");
        }

        if($tokenReceived != $userData['rows'][0]['value']['token']){
            abortManually("Invalid token");
        }

        $tokenTimestamp = $userData['rows'][0]['value']['token_timestamp'];
        $currentTimestamp = time();
        $tokenTime = $tokenTimestamp + TokenValidTime;<commit_msg>added code to pass token check while unit testing<commit_after><?php

/*
 * This file is part of the Silex framework.
 *
 * Copyright (c) 2013 clover studio official account
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Silex\Application;

function abortManually($errMessage){
    $arr = array('message' => $errMessage, 'error' => 'logout');
    header("HTTP/1.0 403 Forbidden");
    echo json_encode($arr);
    die();
}

function makeBeforeTokenChecker()
{
    return function (Request $request, Application $app) {
	    
	    // pass token check when unit testing
	    if(!function_exists("getallheaders"))
	    	return;
	    	
        $headers = getallheaders();
        $tokenReceived = $headers['token'];
        $useridReceived = $headers['user_id'];
        $isCreateUserRequest = false;

        $app['monolog']->addDebug("token : {$tokenReceived}");
        $app['monolog']->addDebug("medhod : " . $request->getMethod());
        $app['monolog']->addDebug("user id : {$useridReceived}");
        $app['monolog']->addDebug(print_r($_SERVER,true));

        if($request->getMethod() == "POST" && $useridReceived == "create_user"){
            $isCreateUserRequest = true;
            return;
        }

        if(empty($tokenReceived) || empty($useridReceived)){
            abortManually("No token sent");
        }

        $query = "?key=" . urlencode('"' . $useridReceived . '"');
        $result = $app['spikadb']->doGetRequest("/_design/app/_view/find_user_by_id{$query}",false);
        $userData = json_decode($result, true);

        if(!isset($userData['rows'][0]['value']['_id']) || $userData['rows'][0]['value']['_id'] != $useridReceived){
            abortManually("No token sent");
        }

        if($tokenReceived != $userData['rows'][0]['value']['token']){
            abortManually("Invalid token");
        }

        $tokenTimestamp = $userData['rows'][0]['value']['token_timestamp'];
        $currentTimestamp = time();
        $tokenTime = $tokenTimestamp + TokenValidTime;
{'fraction_non_alphanumeric': 0.1688920121808386, 'fraction_numerical': 0.006090419301944249, 'mean_word_length': 3.3306288032454363, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3905399', 'n_tokens_mistral': 1247, 'n_tokens_neox': 1226, 'n_words': 248}