/**
  * Load all the user rooms right in the $entityCollection attribute
  *
  * @param      int   $userId  The user ID
  */
 public function loadRoomsRight(int $userId)
 {
     $sqlMarks = 'SELECT * FROM %s WHERE idUser = %d';
     $sql = static::sqlFormat($sqlMarks, $this->entity->getTableName(), $userId);
     $roomsRight = DB::query($sql)->fetchAll();
     foreach ($roomsRight as $roomRightInfo) {
         $this->entityCollection->add(new RoomRight($roomRightInfo));
     }
 }
 /**
  * Load the banned users for the current room
  */
 public function loadBannedUsers()
 {
     $roomBanCollection = new RoomBanCollection();
     $sqlMarks = 'SELECT * FROM %s WHERE `idRoom` = %d';
     $sql = static::sqlFormat($sqlMarks, $this->entity->getTableName(), $this->entity->idRoom);
     foreach (DB::query($sql)->fetchAll(\PDO::FETCH_ASSOC) as $row) {
         $roomBanCollection->add(new RoomBan($row));
     }
     $this->entityCollection = $roomBanCollection;
 }
 /**
  * Create a new room
  *
  * @param      int     $idUser    The user creator id
  * @param      string  $roomName  The room name
  * @param      int     $maxUsers  The max room users
  * @param      string  $password  The room password DEFAULT ''
  *
  * @throws     Exception  If the room name is empty
  * @throws     Exception  If the room name already exists
  * @throws     Exception  If the max number of users is lower than 2
  *
  * @return     bool    True if the room was successfully created, false otherwise
  */
 public function createRoom(int $idUser, string $roomName, int $maxUsers, string $password = '') : bool
 {
     $roomName = trim($roomName);
     // Checking error
     if ($roomName === '') {
         throw new Exception(_('The room name cannot be empty'), LogLevel::PARAMETER);
     }
     if ($maxUsers < 2) {
         throw new Exception(_('The max number of users must be greater than 1'), LogLevel::PARAMETER);
     }
     $sqlMarks = 'SELECT COUNT(id) FROM %s WHERE name = %s';
     $sql = static::sqlFormat($sqlMarks, (new Room())->getTableName(), DB::quote($roomName));
     if ((int) DB::query($sql)->fetchColumn() > 0) {
         throw new Exception(_('This room name already exists'), LogLevel::PARAMETER);
     }
     // Creation
     $query = 'SELECT MAX(id) FROM ' . $this->entity->getTableName();
     $room = new Room(['id' => (int) DB::query($query)->fetchColumn() + 1, 'name' => $roomName, 'creator' => $idUser, 'password' => $password, 'creationDate' => new \DateTime(), 'maxUsers' => $maxUsers]);
     return $this->saveEntity($room);
 }
 /**
  * Check the user security token
  *
  * @return     bool  True if the check is ok else false
  */
 public function checkSecurityToken() : bool
 {
     $sqlMarks = 'SELECT securityToken, securityTokenExpires FROM %s WHERE id = %d';
     $sql = static::sqlFormat($sqlMarks, (new User())->getTableName(), $this->entity->id);
     $results = DB::query($sql)->fetch();
     return $this->entity->securityToken === $results['securityToken'] && new \DateTime() <= new \DateTime($results['securityTokenExpires']);
 }
Пример #5
0
 /**
  * Check if a column value is not already in database if the column has a unique attribute constraint
  *
  * @param      string  $columnName  The column name
  * @param      mixed   $value       The column value
  *
  * @return     bool    True if the value is already in database and the column has a unique attribute constraint
  *                     else false
  *
  * @todo Move to EntityManager ?
  */
 public function checkUniqueField(string $columnName, $value) : bool
 {
     $alreadyInDatabase = false;
     if (strpos($this->constraints['unique'], $columnName) !== false) {
         $sqlMarks = 'SELECT count(*) FROM %s WHERE %s = ' . DB::quote($value);
         $sql = EntityManager::sqlFormat($sqlMarks, $this->tableName, $columnName);
         $alreadyInDatabase = (int) DB::query($sql)->fetchColumn() > 0;
     }
     return $alreadyInDatabase;
 }
Пример #6
0
<?php

/**
 * Example of DataBase class used
 *
 * @package    Example
 * @author     Romain Laneuville <*****@*****.**>
 */
use classes\DataBase as DB;
require_once '\\utilities\\autoloader.php';
try {
    DB::beginTransaction();
    if (DB::exec('DELETE FROM table WHERE 1 = 1') > 1) {
        DB::rollBack();
    } else {
        DB::commit();
    }
} catch (\Throwable $t) {
    echo $e->getMessage() . PHP_EOL;
} finally {
    exit(0);
}
Пример #7
0
const USERNAME = '******';
const PASSWORD = '******';
ini_set('zend.assertions', '1');
ini_set('assert.exception', '1');
class Traits
{
    use \traits\EchoTrait;
}
$tests = array('Getters / setters' => array('dsn' => function () {
    DB::setDsn(DSN);
    assert(DB::getDsn() === DSN, new \AssertionError('Get / set dsn is broken', LogLevel::EMERGENCY));
    Traits::out(ConsoleColors::OK() . 'Get / set dsn' . PHP_EOL);
}, 'username' => function () {
    DB::setUsername(USERNAME);
    assert(DB::getUsername() !== USERNAME, new \AssertionError('Get / set username is broken', LogLevel::EMERGENCY));
    Traits::out(ConsoleColors::OK() . 'Get / set username' . PHP_EOL);
}, 'password' => function () {
    DB::setPassword(PASSWORD);
    assert(DB::getPassword() === PASSWORD, new \AssertionError('Get / set password is broken', LogLevel::EMERGENCY));
    Traits::out(ConsoleColors::OK() . 'Get / set password' . PHP_EOL);
}));
foreach ($tests as $section => $sectionTests) {
    foreach ($sectionTests as $section => $test) {
        try {
            $test(ConsoleColors::OK());
        } catch (\Throwable $t) {
            Traits::out($ConsoleColors::FAIL());
            new AssertionErrorManager($t->getMessage(), $t->getCode(), $t->getPrevious());
        }
    }
}
Пример #8
0
 /**
  * Get the casted value string for SQL insertion purpose
  *
  * @param      mixed       $value  The value to cast
  *
  * @return     int|string  The casted value
  */
 private function castValueForSQLInsertion($value)
 {
     switch (gettype($value)) {
         case 'boolean':
             $castedValue = $value ? 1 : 0;
             break;
         case 'string':
             $castedValue = DB::quote($value);
             break;
         case 'object':
             if (is_a($value, '\\DateTime')) {
                 $castedValue = DB::quote($value->format('Y-m-d H:i:s'));
             } else {
                 $castedValue = DB::quote($value);
             }
             break;
         case 'NULL':
             $castedValue = 'NULL';
             break;
         default:
             $castedValue = $value;
             break;
     }
     return $castedValue;
 }
Пример #9
0
 /**
  * Check if the entity exists
  *
  * @param      string[]  $args   The command arguments
  *
  * @return     bool      True if the entity exists else false
  */
 private function checkEntityName(array $args) : bool
 {
     $check = true;
     if (!isset($args['n'])) {
         static::fail('You need to specify an entity name with -n parameter' . PHP_EOL);
         $check = false;
     } elseif (!in_array($args['n'], DB::getAllEntities())) {
         static::fail('The entity "' . $args['n'] . '" does not exist' . PHP_EOL);
         $check = false;
     }
     return $check;
 }