public function fizzBuzz($number)
 {
     if ($number % 3 === 0 && $number % 5 === 0) {
         $this->outputHandler->write('FizzBuzz');
         return;
     }
     if ($number % 3 === 0) {
         $this->outputHandler->write('Fizz');
         return;
     }
     if ($number % 5 === 0) {
         $this->outputHandler->write('Buzz');
         return;
     }
     $this->outputHandler->write($number);
 }
Exemple #2
0
<?php

/*
New version - using classes
*/
require_once "init.php";
$outputHandler = new OutputHandler("output-external-auth.txt");
if (isset($_GET['provider'])) {
    $provider = $_GET['provider'];
    $outputHandler->write($provider);
    switch ($provider) {
        case "google":
            $code = $headersHandler->getHeader('code');
            $google_client = new Google_Client();
            if ($headersHandler->isAuthenticated()) {
                // Link accounts
                $user = new User($database, $headersHandler->getBearer());
                $googleProvider = new GoogleProvider($database, $user->getID(), $google_client);
                $accessToken = $googleProvider->getAccessToken($code);
                //$googleProvider->getDataFromDB();
                $retrievedData = $googleProvider->retrieveData();
                // check if anyone has the same GoogleID and different user_id
                $checkQuery = $database->prepare("SELECT id FROM google_users WHERE google_id = :googleID AND user_id != :userID LIMIT 1;");
                $checkQuery->bindParam(":googleID", $retrievedData['id'], PDO::PARAM_INT);
                $checkQuery->bindParam(":userID", $user->getID(), PDO::PARAM_INT);
                $checkQuery->execute();
                if ($checkQuery->rowCount() == 0) {
                    // First time this google ID appeared in the DB
                    $googleProvider->save();
                    $user->fetchProviders();
                    $jwt = $user->getJWT();
Exemple #3
0
<?php

/*
New version - with classes
*/
require_once "init.php";
$email = $headersHandler->getHeader('email');
$password = $headersHandler->getHeader('password');
$outputHandler = new OutputHandler("output-login2.txt");
$outputHandler->write($headersHandler->getHeaders(true));
if ($headersHandler->isAuthenticated()) {
    // if the user is already authenticated - do not login
    $headersHandler->sendHeaderCode(400);
    $headersHandler->sendJSONData(['error' => "User already authenticated"]);
    $outputHandler->write("user already authenticated");
    die;
}
if ($email && $password) {
    $user = new User($database, $email, $password);
    if ($user->isAuthenticated()) {
        $jwt = $user->getJWT();
        $preparedData = ['token' => $jwt];
        $headersHandler->sendJSONData($preparedData);
        $outputHandler->write("successful login");
        $outputHandler->write($preparedData);
        $outputHandler->write($jwt);
    } else {
        $outputHandler->write("email and password do not match");
        $headersHandler->sendHeaderCode(401);
        $headersHandler->sendJSONData(['error' => "Email and password do not match"]);
    }