Example #1
0
/**
* This method is used to store the ionformation about a Tweety game session. This information can later be
* used to find the top 10 scores, find game info about a particular session etc.
*
* @param player_name :: Name of the Player
* @param time_remaining :: Total time remaining in seconds
* @param score :: Total score at the end of the game
* @param num_correct :: Number of correct pairs selected
* @param num_incorrect :: Number of incorrect pairs selected
* @param profile_pic :: Profile Picture link of the player
*/
function storeGameInfo($player_name, $time_remaining, $score, $num_correct, $num_incorrect, $profile_pic)
{
    // Get connection to the DB
    $link = getConnection();
    // Generate global unique Game ID
    $guid = getGUID();
    // Insert Game Data for the Player
    $query = "INSERT INTO Scores(Game_ID, Player, Score, Time_Remaining, Num_Correct, Num_Incorrect, Profile_Pic) VALUES('" . (string) $guid . "'," . $player_name . ", " . $score . ", " . $time_remaining . ", " . $num_correct . ", " . $num_incorrect . ", " . $profile_pic . ");";
    $res = mysqli_query($link, $query);
    $affectedRows = mysqli_affected_rows($link);
    // log information based on result
    if ($affectedRows == 1) {
        if ($player_name == NULL) {
            $player_name = 'NOT_SPECIFIED';
        }
        logSuccess('gameSelectionLogs.html', 'Player <b>' . $player_name . '</b>\'s game data has been stored. Game ID: <b>' . $guid . '</b>.');
    } else {
        if ($player_name == NULL) {
            $player_name = 'NOT_SPECIFIED';
        }
        logError('gameSelectionLogs.html', 'Unable to store game data for Player <b>' . $player_name . '</b>. <b> ERROR: </b>' . (string) mysqli_error($link));
    }
}
Example #2
0
$user = "******";
if (notNull($_POST['auth_login']) && notNull($_POST['auth_pass'])) {
    $myDB = new DBFactory();
    $sql = $myDB->getMysqlConnexionWithPDO();
    $query = $sql->prepare("SELECT * FROM users WHERE login = :login;");
    $query->execute(array('login' => $_POST['auth_login']));
    $result = $query->fetch();
    if (notNull($result)) {
        $sent_pass = sha1($_POST['auth_pass'] . $result['salt']);
        $stored_pass = $result['pass'];
        if ($sent_pass == $stored_pass) {
            $_SESSION['logged'] = "LOGGED";
            $_SESSION['user'] = ucfirst($result['login']);
            $_SESSION['role'] = $result['role'];
            $_SESSION['id'] = $result['id'];
            logSuccess($result['login'], 0, "User " . $result['login'] . " successfully logged in from IP " . get_client_ip());
        } else {
            $_SESSION['logged'] = "NOT_LOGGED";
            $_SESSION['user'] = "******";
            $_SESSION['role'] = 0;
            logWarning($result['login'], 0, "User " . $result['login'] . " failed to log from IP " . get_client_ip());
        }
    }
}
if ($logged) {
    if (notNull($_POST['auth_from'])) {
        header('Location:' . $_POST['auth_from']);
    } else {
        header('Location:/');
    }
} else {
Example #3
0
/**
* This is the method that inserts 200 recent Twitter response objects with their Twitter handles in 
* the DB (Table: Tweets). 
*/
function insertTweetInDB()
{
    //$users = array("result" => ["@katyperry"]);
    $users = getAllTwitterUsers();
    $twitterApiCallCount = 0;
    foreach ($users['result'] as $user) {
        if ($twitterApiCallCount % 180 == 0 && $twitterApiCallCount != 0) {
            // Sleep for 15mins and 30 seconds
            break;
        }
        // strip the initial character '@' and get 200 Twitter Responses for that screen-name.
        $twitterResp = getTweet(substr($user, 1), 200);
        $twitterApiCallCount += 1;
        $count = 1;
        mysqli_query(getConnection(), "START TRANSACTION;");
        foreach ($twitterResp as $response) {
            $response["source"] = str_replace('"', '\\"', $response["source"]);
            $response["text"] = str_replace('"', '\\"', $response["text"]);
            $object = json_encode($response);
            // Escaping all the ' character from the Tweet Data
            $object = str_replace("'", "\\'", $object);
            $query = "REPLACE INTO Tweets(Number, TwitterHandle, TwitterResp) VALUES('" . (string) $count . "', '" . $user . "', '" . $object . "');";
            $count += 1;
            $res = mysqli_query(getConnection(), $query);
            if (false === $res) {
                logWarning('tweetylogs.txt', "Insertion for Tweet #" . $count . " for Twitter User " . $user . " failed. Insertion error: " . mysqli_error($link));
                logWarning('warning.txt', "Insertion for Tweet #" . $count . " for Twitter User " . $user . " failed. Insertion error: " . mysqli_error($link));
                logWarning('tweetylogs.html', "Insertion for <b>Tweet #" . $count . "</b> for <b>Twitter User " . $user . "</b> failed. Insertion error: " . mysqli_error($link));
            }
        }
        if ($count >= 200) {
            logSuccess('tweetylogs.txt', "Insertion for 200 Tweets for Twitter User " . $user . " succeded.");
            logSuccess('warning.txt', "Insertion for 200 Tweets for Twitter User " . $user . " succeded.");
            logSuccess('tweetylogs.html', "Insertion for 200 Tweets for <b>Twitter User " . $user . "</b> succeded.");
        }
        mysqli_query(getConnection(), "COMMIT;");
    }
}
Example #4
0
/**
* This method creates a game object that is good for one game session. Everytime a game object is created, it is all random.
* The twitter users, their tweet, all random. 
*
* NOTE: This method logs a bunch of stuff in a new live log html file called "gameSelectionLogs.html". This is done 
*       because even though the game object is created there is a minor glitch in creation of the game object. Some 
*       Tweets do not work when doing json_decode ("enigma-bug"). Sometiumes it works sometimes it doesnt. 
*
* @return gameObject :: A json form string that could be converted into json in JS easily.
*/
function createGameObject()
{
    // Get random 10 Twitter Users
    $twitterUsers = getRandomTwitterUsers(10);
    // Get Connection Link
    $link = getConnection();
    $correct = array();
    $incorrect = array();
    $userKeys = array();
    foreach ($twitterUsers as $user) {
        // Select a random number between 1 and 200
        $rand = mt_rand(1, 200);
        logInfo("gameSelectionLogs.html", "Grabbing Tweet Number: " . $rand . " for Twitter User: "******" from the DB.");
        $query = "SELECT TwitterResp FROM Tweets WHERE Number = " . (string) $rand . " AND TwitterHandle = \"" . $user . "\";";
        $res = mysqli_query($link, $query);
        $row = $res->fetch_array();
        // Get the textual tweet response form the DB
        $twitterResp = $row[0];
        // Convert that text to associative array
        $twitterRespJson = json_decode($twitterResp, true, 200000);
        // Creating a new user variable jsut in case the last one fails
        $newUser = $user;
        // If it fails...
        while ($twitterRespJson == null) {
            logError("gameSelectionLogs.html", "Conversion of Tweet Response text from DB to JSON in PHP failed.");
            logInfo("gameSelectionLogs.html", "Finding a new random Twitter User for the game...");
            // Get new random twitter user
            $newUser = getRandomTwitterUsers(1);
            $newUser = $newUser[0];
            // Select a random number between 1 and 200
            $rand = mt_rand(1, 200);
            logInfo("gameSelectionLogs.html", "Grabbing Tweet Number: " . $rand . " for Twitter User: "******" from the DB.");
            $query = "SELECT TwitterResp FROM Tweets WHERE Number = " . (string) $rand . " AND TwitterHandle = \"" . $newUser . "\";";
            $res = mysqli_query($link, $query);
            $row = $res->fetch_array();
            // Grab the Twitter Response as text from the DB
            $twitterResp = $row[0];
            $twitterRespJson = json_decode($twitterResp, true, 20000);
        }
        logSuccess("gameSelectionLogs.html", "User: "******" with Tweet Number: " . $rand . " has been selected for the game.");
        // If Everything went OK
        if ($twitterResp != "") {
            $response = $twitterRespJson;
            $userObj = array('name' => $response['user']['name'], 'handle' => '@' . $response['user']['screen_name'], 'profilePicURL' => str_replace("_normal", "", $response['user']['profile_image_url']), 'followURL' => "https://twitter.com/intent/follow?screen_name=" . '@' . $response['user']['screen_name']);
            //var_dump($userObj);
            $tweetObj = array('tweetID' => $response['id'], 'tweetDate' => $response['created_at'], 'tweetHTML' => getTweetHTML($response), 'tweetText' => $response['text'], 'numOfRetweets' => $response['retweet_count'], 'numOfFavorites' => $response['favorite_count']);
            $unit = array('userInfo' => $userObj, 'tweetInfo' => $tweetObj);
            array_push($userKeys, $response['user']['screen_name']);
            $correct[$response['user']['screen_name']] = $unit;
        }
    }
    logInfo("gameSelectionLogs.html", "'Correct' part of the game object has been COMPLETED. Starting the construction of 'incorrect' part of the game object.");
    $incorrect = array();
    // Variable to log the final Game Layout.
    $gameObjectLog = "";
    foreach ($correct as $unit) {
        $rand = $rand = mt_rand(0, count($userKeys) - 1);
        // Swap that random number with the last user in the userKeys array
        if (count($userKeys) != 0) {
            $temp = $userKeys[count($userKeys) - 1];
            $userKeys[count($userKeys) - 1] = $userKeys[$rand];
            $userKeys[$rand] = $temp;
        }
        // get random tweet user
        $randomTwitterUser = array_pop($userKeys);
        $gameObjectLog = $gameObjectLog . '<b>' . substr($unit['userInfo']['handle'], 1) . '</b> has <b>' . $randomTwitterUser . '\'s</b> tweet infront of him/her in the game. <br>';
        // Select that random tweet from the correct part of game object and add in current incorrect unit
        $incorrect[substr($unit['userInfo']['handle'], 1)]['userInfo'] = $correct[substr($unit['userInfo']['handle'], 1)]['userInfo'];
        $incorrect[substr($unit['userInfo']['handle'], 1)]['tweetInfo'] = $correct[$randomTwitterUser]['tweetInfo'];
    }
    // Game object construction
    $gameObject = array('correct' => $correct, 'incorrect' => $incorrect);
    logSuccess("gameSelectionLogs.html", "Game Object Creation Successful. <br><u>GAME INFO:</u><br>" . $gameObjectLog);
    logInfo("gameSessionObjects.txt", json_encode($gameObject));
    return json_encode($gameObject);
}
Example #5
0
function deleteArticle($id)
{
    $sql = initDB();
    $sql->beginTransaction();
    echo 'deleting...';
    try {
        $query = $sql->prepare('DELETE FROM news WHERE id = :id;');
        $query->execute(array('id' => $id));
        $sql->commit();
        logSuccess($_SESSION['user'], 3, $_SESSION['user'] . " deleted article n°" . $id . ".");
        echo 'ok';
    } catch (Exception $e) {
        $sql->rollBack();
        logError($_SESSION['user'], 3, $_SESSION['user'] . " tried to delete article n°" . $id . ". Server returned : <br />" . $e->getMessage());
        echo 'nop';
    }
}