/**
  * @dataProvider readDataProvider
  */
 public function test_readStdErrLines($stderr, $expected)
 {
     $this->handler->handle(null, $stderr);
     $result = $this->handler->readStdErrLines();
     foreach ($expected as $index => $line) {
         $this->assertArrayHasKey($index, $result);
         $this->assertEquals($line, $result[$index]);
     }
 }
 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);
 }
 /**
  * Handle the command output.
  *
  * @param $stdout
  * @param $stderr
  * @return void
  */
 public function handle($stdout, $stderr)
 {
     parent::handle($stdout, $stderr);
     if (trim($stdout)) {
         echo trim($stdout) . "\n";
     }
     if (trim($stderr)) {
         echo trim($stderr) . "\n";
     }
 }
Beispiel #4
0
 public static function fuckoff($desc = null)
 {
     OutputHandler::clear();
     header_remove();
     header("HTTP/1.1 471 F**k Off");
     echo 'Bad input';
     if ($desc !== null) {
         echo ': ' . $desc;
     }
     echo '. If you did things correctly but still see this, please use the "Support" section on the site and describe the exact steps you followed and what device/browser/extensions you use.';
     exit;
     // Bail out
 }
Beispiel #5
0
 /**
  * @return void
  */
 public function read()
 {
     $stdout = $this->readStream(static::STDOUT);
     $stderr = $this->readStream(static::STDERR);
     $this->outputHandler->handle($stdout, $stderr);
 }
Beispiel #6
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();
<?php

namespace Radical\CLI\Output;

class OutputHandler
{
    private static $handler;
    static function output($string)
    {
        if (self::$handler instanceof Handler\IOutputHandler) {
            self::$handler->Output($string);
        }
    }
    static function setHandler(Handler\IOutputHandler $handler)
    {
        static::$handler = $handler;
    }
    static function init()
    {
        self::$handler = new Handler\EchoOutput();
    }
}
OutputHandler::Init();
Beispiel #8
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"]);
    }
 /**
  * Function used to update the DB to show the current run status of the cron
  * Used for the CRON Manager UI for updating status fields
  * @param $cron array The cron for which the runstate needs to be set
  * @param $state int If 0, then cron is inactive, if 1 it is active and running
  * @param $dbh object The database object
  */
 private static function setRunState($cron, $state = 0, $dbh)
 {
     $return = false;
     $q = 'UPDATE admin_crons SET run_state = ' . $state . ' WHERE cron_id = ' . $cron . ';';
     try {
         // Set / Unset a lock
         switch ($state) {
             case 0:
                 $dbh->runQuery("SELECT RELEASE_LOCK('cron_" . $cron . "');");
                 $result = $dbh->runQuery($q);
                 $return = true;
                 break;
             case 1:
                 // First check if the lock is set
                 $lock = $dbh->getResults("SELECT IS_USED_LOCK('cron_" . $cron . "') as locked;");
                 if (GenConfig::DEBUG) {
                     OutputHandler::displayOutput("[%white%DEBUG%lightgray%] : Does lock cron_{$cron} exist? \n" . print_r($lock, true) . "\n");
                 }
                 // The lock exists
                 if (!empty($lock[0]["locked"]) && !is_null($lock[0]["locked"])) {
                     $result = 0;
                     $return = false;
                 } else {
                     $dbh->runQuery("SELECT GET_LOCK('cron_" . $cron . "', 10);");
                     $result = $dbh->runQuery($q);
                     $return = true;
                 }
                 break;
         }
     } catch (PDOException $e) {
         $result = 1;
         $return = false;
     }
     # Failed
     if ($result == 1) {
         if (GenConfig::DEBUG) {
             OutputHandler::displayOutput("%lightred%Could not update the run state of cronjob %red%{$cron_id}%lightred%! :(%lightgray%\n--------------------\n");
         }
         SR_Agent::Log(GenConfig::API, SystemReporter::MSG_ERROR, "Could not update the run state of cronjob {$cron_id}! :(");
     }
     return $return;
 }
 /**
  * Private Fucntion completeRun
  * @description
  * Function is called after all the databases have been backed up, in order to create a final MD5 checksum of the databases and then compress it all as a single file
  */
 private function completeRun()
 {
     // Check if it is the LIVE environment
     if (LIVE === true) {
         $backupDir = "/ts_backups/mysql_backups/";
     } else {
         $backupDir = "/srv_admin/srv_backups/mysql/";
     }
     // Check if it was a full or incremental backup run
     if (date("D") == "Fri") {
         $backupDir .= "full_backups/";
     } else {
         $backupDir .= "incremental_backups/";
     }
     // Setup paths and filenames
     $folder = $backupDir;
     $backupDir .= date("d_m_Y") . "/";
     $compressed_file = $folder . date("d_m_Y") . ".tar.gz";
     // Check if it is a simulated run
     if (!$this->simulation) {
         exec("md5sum {$backupDir}/*.tar.gz > {$backupDir}/databases.md5");
         if (DEBUG === true) {
             OutputHandler::displayOutput("[%lightgreen%DEBUG%lightgray%] Generated MD5 Hashes of DB Backups%lightgray%", $this->color);
         }
     }
     $command = "tar -zcvf {$compressed_file} {$backupDir}";
     if (DEBUG === true) {
         OutputHandler::displayOutput("[%lightgreen%DEBUG%lightgray%] Command : %lightblue%{$command}%lightgray%", $this->color);
     }
     // Check if it is a simulated run
     if (!$this->simulation) {
         exec($command, $command_output);
         // Generate MD5 checksum of the compressed file (usefull for integrity checks)
         exec("md5sum {$compressed_file} > {$compressed_file}.md5");
     } else {
         $command_output = array("Folder compressed");
     }
     // Check if debugging is enabled
     if (DEBUG === true) {
         // Display debugging information
         foreach ($command_output as $cmd_output) {
             OutputHandler::displayOutput("[%lightgreen%DEBUG%lightgray%] [%lightblue%{$backupDir}%lightgray%]\t" . $cmd_output, $this->color);
         }
     }
     // Check if it is a simulated run
     if (!$this->simulation) {
         exec("rm -rf {$backupDir}");
     }
     SR_Agent::Log(APPID, SystemReporter::MSG_SUCCESS, "Backup complete for " . date("d/m/Y"));
 }