<commit_before><?php
require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim(array(
    'debug' => true
));

$app->post('/login', 'checkAuthentication');
$app->get('/authenticate', 'generateAPIKey');
$app->get('/friends', 'authenticate','getFriends');
$app->get('/friend/:id',  'getFriend');
$app->post('/friends', 'addFriend');
$app->put('/friend/:id', 'updateFriend');
$app->delete('/friend/:id', 'deleteFriend');
$app->get('/friend/search/:query', 'findByName');

$app->run();

//curl -i -X POST -H 'Content-Type: application/json' -d '{"username":"ntkthoa","password":"<PASSWORD>"}' http://127.0.0.1:8080/php-service/login
function checkAuthentication() {
    $request = \Slim\Slim::getInstance()->request();
    $body = $request->getBody();
    $data = json_decode($body);
    $sql = "SELECT * FROM user_tb WHERE user_name=:username and pass_word=:password";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("username", $data->username);
        $stmt->bindParam("password", $data->password);
        $stmt->execute();
        if($hello = $stmt->fetchObject()) {
            echo json_encode(true);
        } else {
            echo json_encode(false);
        }
        $db = null;
        //echo '{"friends": ' . json_encode($hello) . '}';
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }<commit_msg>completed check session(existing/add new) with username,password<commit_after><?php
require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new \Slim\Slim(array(
    'debug' => true
));

$app->get('/session/:id', 'checkSession');
$app->post('/login', 'checkAuthentication');
$app->get('/authenticate', 'generateAPIKey');
$app->get('/friends', 'authenticate','getFriends');
$app->get('/friend/:id',  'getFriend');
$app->post('/friends', 'addFriend');
$app->put('/friend/:id', 'updateFriend');
$app->delete('/friend/:id', 'deleteFriend');
$app->get('/friend/search/:query', 'findByName');

$app->run();

//curl -i -X POST -H 'Content-Type: application/json' -d '{"username":"ntkthoa","password":"<PASSWORD>"}' http://127.0.0.1:8080/php-service/login
function checkAuthentication() {
    $request = \Slim\Slim::getInstance()->request();
    $body = $request->getBody();
    $data = json_decode($body);
    $sql = "SELECT * FROM user_tb WHERE user_name=:username and pass_word=:password";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("username", $data->username);
        $stmt->bindParam("password", $<PASSWORD>->password);
        $stmt->execute();
        $hello = $stmt->fetchObject();
        $db = null;
        if($hello) {
            $status = checkSession($hello->id);
            if ($status == null) {
                generateSession($hello->id, $data->username . $data->password);
            } else {
                echo json_encode("existing: ".$status);
            }
        } else {
            echo json_encode(false);
        }
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

function generateSession($user_id, $session_id) {
    $sql = "INSERT INTO last_login_tb (user_id, session_id) VALUES (:user_id, :session_id)";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("user_id", $user_id);
        $stmt->bindParam("session_id", $session_id);
        $stmt->execute();
        if($db->lastInsertId()) {
            echo json_encode("new: ".$session_id);
        } else {
            echo json_encode(false);
        }
        $db = null;
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

function checkSession($user_id){
    $sql = "SELECT * FROM last_login_tb WHERE user_id=:user_id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("user_id", $user_id);
        $stmt->execute();
        if ($hello = $stmt->fetchObject()) {
            $db = null;
            return $hello->session_id;
        } else {
            return null;
        }
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
{'fraction_non_alphanumeric': 0.21502347417840376, 'fraction_numerical': 0.004694835680751174, 'mean_word_length': 2.952690166975881, 'pattern_counts': {'":': 13, '<': 8, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2719547', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1381, 'n_words': 240}