Exemple #1
0
 /**
  * @return array
  */
 public static function loadAll()
 {
     $dbWrapper = new DatabaseWrapper();
     $connection = $dbWrapper->getConnection();
     $query = "SELECT * FROM temos;";
     $temos = [];
     foreach ($connection->query($query) as $row) {
         $tema = new Tema($connection);
         $tema->setId($row['id']);
         $tema->setDate($row['subject_date']);
         $tema->setName($row['name']);
         $query = 'SELECT * FROM comments INNER JOIN temos ON comments.subjectId = ' . $row['id'] . " AND temos.id = " . $row['id'] . ";";
         $comments = [];
         foreach ($connection->query($query) as $i) {
             $comment = new Comment($connection);
             $comment->setId($i['id']);
             $comment->setsubjectId($i['subjectId']);
             $comment->setText($i['text']);
             $comment->setDate($i['date']);
             $comment->setAuthor($i['author']);
             $comments[] = $comment;
         }
         foreach ($comments as $comment) {
             $tema->setComments($comment);
         }
         $temos[] = $tema;
     }
     return $temos;
 }
 function testAddDeleteAccount()
 {
     $expected = new UserAccount('new_acc', 'pass', 'ADMIN');
     $this->db->addAccount($expected);
     $result = $this->db->queryUser('new_acc');
     $this->db->deleteUser('new_acc');
     // clean up after ourselves
     $this->assertIdentical($expected, $result);
 }
Exemple #3
0
 public function __construct($connection = null)
 {
     if (!isset($connection)) {
         $dbWrapper = new DatabaseWrapper();
         $this->connection = $dbWrapper->getConnection();
     } else {
         $this->connection = $connection;
     }
 }
 /**
  * Checks if valid registration details have been submitted.
  * @throws Exception If an error occurs with registration.
  */
 public function checkValidRegistration()
 {
     if (empty($_POST['username']) && empty($_POST['password']) && empty($_POST['password2'])) {
         /* hasn't submitted any information */
         return;
     }
     $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
     $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
     $password2 = filter_var($_POST['password2'], FILTER_SANITIZE_STRING);
     $this->model->setUsername($username);
     if ($username === false) {
         $this->model->setError('Invalid username.');
     } else {
         if (strlen($username) < 3) {
             $this->model->setError('Username requires 3 characters.');
         } else {
             if (strlen($username) > 50) {
                 $this->model->setError('Username exceeds 50 characters.');
             } else {
                 if ($password === false) {
                     $this->model->setError('Invalid password.');
                 } else {
                     if (strlen($password) < 3) {
                         $this->model->setError('Password requires 3 characters.');
                     } else {
                         if (strlen($password) > 50) {
                             $this->model->setError('Password exceeds 50 characters.');
                         } else {
                             if ($password !== $password2) {
                                 $this->model->setError('Passwords do not match.');
                             } else {
                                 $database = new DatabaseWrapper();
                                 $database->connect(DB_NAME);
                                 try {
                                     /* if the queryUser method executes without any errors then it means an account does exist */
                                     $database->queryUser($username);
                                     throw new AlreadyRegisteredException();
                                 } catch (AlreadyRegisteredException $e) {
                                     throw $e;
                                     // throw the error upwards for it to be handled correctly
                                 } catch (Exception $e) {
                                     /* error thrown as the statement failed to find an account */
                                 }
                                 $database->addAccount(new UserAccount($username, password_hash($password, PASSWORD_BCRYPT), 'USER'));
                                 $this->model->registered();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Fetches the statuses of the circuit boards from the database.
  * @throws Exception If a query fails to execute.
  */
 public function fetchStatuses()
 {
     $database = new DatabaseWrapper();
     $database->connect(DB_NAME);
     $boards = $database->queryAllBoardInformation();
     foreach ($boards as $board) {
         $msisdn = $board->getMSISDN();
         $status = null;
         try {
             $status = $database->queryBoardStatus($msisdn);
         } catch (Exception $e) {
             /* failed to find its status */
         }
         $this->model->addBoard(new CircuitBoard($board, $status));
     }
 }
 /**
  * Fetches the statuses of the circuit boards from the database.
  * @throws Exception If a query fails to execute.
  */
 public function fetchUpdates()
 {
     $soap = new SoapClientWrapper();
     $messages = $soap->getNewMessages();
     $database = new DatabaseWrapper();
     $database->connect(DB_NAME);
     foreach ($messages as $message) {
         $xmlParser = new XMLParser($message);
         $xmlParser->parse();
         $parsedMessage = $xmlParser->getParsedData();
         $validator = new SMSValidator($parsedMessage);
         try {
             $msisdn = $validator->validateMSISDN();
             $status = $validator->validateStatus();
             $information = $database->queryBoardInformation($msisdn);
             $update = new CircuitBoard($information, $status);
             $database->updateBoardStatus($msisdn, $status);
             $this->model->addUpdate($update);
         } catch (Exception $e) {
             continue;
         }
     }
 }
 /**
  * Checks for valid login details.
  * @throws Exception If there was an error with the login procedure.
  */
 public function checkValidLogin()
 {
     if (empty($_POST['username']) && empty($_POST['password'])) {
         /* hasn't submitted any information */
         return;
     }
     $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
     $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
     $database = new DatabaseWrapper();
     $database->connect(DB_NAME);
     $account = null;
     try {
         $account = $database->queryUser($username);
     } catch (Exception $e) {
         throw new LoginDetailsException();
     }
     $passwordHash = $account->getPasswordHash();
     if (!password_verify($password, $passwordHash)) {
         throw new LoginDetailsException();
     }
     $_SESSION['username'] = $account->getUsername();
     $_SESSION['rank'] = $account->getRank();
     $this->model->loggedIn();
 }
<?php

require_once 'Comment.php';
require_once '../databaseWrapper.php';
$dbWrapper = new DatabaseWrapper();
$connection = $dbWrapper->getConnection();
$ids = [];
foreach ($connection->query('SELECT id FROM temos') as $row) {
    $ids[] = $row['id'];
}
$text = "Lambada lambada lambada";
$comment = new Comment($connection);
$comment->setAuthor('Anonymous');
$comment->setSubjectID($ids[rand(0, sizeof($ids) - 1)]);
$comment->setText($text);
$comment->save();
header('Location:  Controller.php');
Exemple #9
0
 /**
  * This is the real thing: run a given instance-deployment request. This means applying the script on the instance,
  * which in turn means executing script queries one by one.
  * The request must be in the 'not_started' state.
  * 
  * @param unknown $propagate_script_instance_deployment_id
  * @param unknown $submitter
  * @param unknown $user
  * @param unknown $password
  * @throws Exception
  */
 public function execute_propagate_script_instance_deployment($propagate_script_instance_deployment_id, $force_manual, $restart_script, $run_single_query, $submitter, $credentials)
 {
     if ($force_manual) {
         $submitter_mask = empty($submitter) ? '%' : $submitter;
         $this->get_database()->query("\n\t    \t\t\tUPDATE \n\t    \t\t\t\tpropagate_script_instance_deployment \n\t    \t\t\tSET \n\t    \t\t\t\tmanual_approved=1 \n\t    \t\t\tWHERE \n\t    \t\t\t\tpropagate_script_instance_deployment_id = " . $this->get_database()->quote($propagate_script_instance_deployment_id) . " \n\t    \t\t\t\tAND submitted_by LIKE " . $this->get_database()->quote($submitter_mask) . "\n\t    \t\t\t");
     }
     $datas = $this->get_database()->query("\n    \t\tSELECT\n    \t\t\t*\n    \t\tFROM\n    \t\t\tpropagate_script_instance_deployment \n    \t\t\tJOIN database_instance USING (database_instance_id)\n   \t\t\tWHERE\n    \t\t\tpropagate_script_instance_deployment_id = " . $this->get_database()->quote($propagate_script_instance_deployment_id) . "\n    \t\t\tAND deployment_status IN ('not_started', 'awaiting_guinea_pig', 'paused', 'failed', 'awaiting_dba_approval')\n    \t\t")->fetchAll();
     if (empty($datas)) {
         throw new Exception("Internal error: cannot read instance deployment info: propagate_script_instance_deployment_id=" . $propagate_script_instance_deployment_id);
     }
     $propagate_script_instance_deployment = $datas[0];
     if (array_key_exists('two_step_approval_environments', $this->conf) && !empty($this->conf['two_step_approval_environments']) && in_array($propagate_script_instance_deployment["environment"], $this->conf['two_step_approval_environments'])) {
         // This deployment has to further get approval of dba.
         if ($submitter != '') {
             // normal user.
             $this->update_propagate_script_instance_deployment_status($propagate_script_instance_deployment_id, "awaiting_dba_approval", "A DBA must approve this deployment", $submitter);
             return;
         }
     }
     if ($propagate_script_instance_deployment['deployment_type'] == 'manual' && !$propagate_script_instance_deployment['manual_approved']) {
         // Do nothing: this is a manual deployment, and force_manual was not provided.
         return;
     }
     // Check for guinea pigs:
     // - A guinea pig can start off right away
     // - A non-guinea pig must wait on at least one guinea pig to succeed
     // + - unless there is no guinea pig, in which case it is free to go
     if (!$propagate_script_instance_deployment['is_guinea_pig']) {
         $guinea_pig_deployment_status = $this->get_propagate_script_guinea_pig_deployment_status($propagate_script_instance_deployment['propagate_script_id'], $submitter);
         if ($guinea_pig_deployment_status['count_guinea_pigs'] > 0) {
             if ($guinea_pig_deployment_status['count_guinea_pigs'] == $guinea_pig_deployment_status['count_failed_guinea_pigs']) {
                 $this->update_propagate_script_instance_deployment_status($propagate_script_instance_deployment_id, "awaiting_approval", "All guinea pigs failed. Will not deploy", $submitter);
                 // No point in polling anymore... Wait for manual intervention.
                 return;
             }
             if ($guinea_pig_deployment_status['count_complete_guinea_pigs'] == 0) {
                 $this->update_propagate_script_instance_deployment_status($propagate_script_instance_deployment_id, "awaiting_guinea_pig", "No deployed guinea pigs yet. Awaiting", $submitter);
                 // Will not continue. Wait for next poll.
                 return;
             }
         }
     }
     // Begin with status updates
     $this->get_database()->query("UPDATE propagate_script_instance_deployment SET processing_start_time=NOW(), processing_end_time=NULL WHERE propagate_script_instance_deployment_id = " . $this->get_database()->quote($propagate_script_instance_deployment_id));
     $this->update_propagate_script_instance_deployment_status($propagate_script_instance_deployment_id, "deploying", "", $submitter);
     // Grab host connection and start issuing queries
     try {
         $propagate_script = $this->get_propagate_script($propagate_script_instance_deployment['propagate_script_id'], $submitter);
         $general_query_mapping = $this->get_general_query_mapping();
         $database_role_query_mapping = $this->get_database_role_query_mapping($propagate_script["database_role_id"]);
         $database_instance_query_mapping = $this->get_database_instance_query_mapping($propagate_script_instance_deployment["database_instance_id"]);
         $deploy_schema = empty($propagate_script_instance_deployment["deploy_schema"]) ? "information_schema" : $propagate_script_instance_deployment["deploy_schema"];
         $database_role = $this->get_database_role($propagate_script['database_role_id']);
         if ($credentials->is_empty() && $this->instance_credentials) {
             $stored_credentials = $this->get_instance_credentials($propagate_script_instance_deployment['host'], $propagate_script_instance_deployment['port']);
             if ($stored_credentials) {
                 $credentials = $stored_credentials;
             }
         }
         $database_wrapper = new DatabaseWrapper(array('database_type' => $database_role['database_type'], 'default_schema' => $deploy_schema, 'host' => $propagate_script_instance_deployment['host'], 'port' => $propagate_script_instance_deployment['port'], 'user' => $credentials->get_username(), 'password' => $credentials->get_password()));
         if ($restart_script) {
             $start_from = 0;
         } else {
             $start_from = intval($propagate_script_instance_deployment["current_propagate_script_query_id"]);
         }
         $queries = $this->get_propagate_script_query($propagate_script_instance_deployment['propagate_script_id'], $submitter, $start_from);
         $query_counter = 0;
         foreach ($queries as $query) {
             $query_counter++;
             $this->update_propagate_script_instance_deployment_current_query_id($propagate_script_instance_deployment_id, $submitter, $query['propagate_script_query_id']);
             if ($run_single_query && $query_counter > 1) {
                 break;
             }
             $sql_code = $query['sql_code'];
             $sql_code = rewrite_query($sql_code, $general_query_mapping);
             $sql_code = rewrite_query($sql_code, $database_role_query_mapping);
             $sql_code = rewrite_query($sql_code, $database_instance_query_mapping);
             $database_wrapper->execute($sql_code);
         }
         if ($run_single_query && $query_counter > 1) {
             $this->update_propagate_script_instance_deployment_status($propagate_script_instance_deployment_id, "paused", "User initiated a step-single-query", $submitter);
         } else {
             // Weepee!
             $this->update_propagate_script_instance_deployment_status($propagate_script_instance_deployment_id, "passed", "Script executed successfully", $submitter);
         }
     } catch (Exception $e) {
         // Bummer
         $this->update_propagate_script_instance_deployment_status($propagate_script_instance_deployment_id, "failed", $e->getMessage(), $submitter);
     }
     $this->get_database()->query("UPDATE propagate_script_instance_deployment SET processing_end_time=NOW() WHERE propagate_script_instance_deployment_id = " . $this->get_database()->quote($propagate_script_instance_deployment_id));
 }