Exemplo n.º 1
0
 /**
  * Add new games to db and game catalog
  *
  * @param \model\Game $credential
  * @param \model\IListener $listener
  * @return bool
  */
 public function add(\model\Game $credential, \model\IListener $listener)
 {
     try {
         $random = rand(0, pow(10, 5)) . '-';
         // 5 digit random number to prefix game and img name
         $gameDirectory = "../public/games/";
         // path to game directory
         $imgDirectory = "../public/images/";
         // path to image directory
         $title = $credential->getTitle();
         $gameFile = $random . $credential->getGameFile()["name"];
         $imgFile = $random . $credential->getImage()["name"];
         $targetFile = $gameDirectory . $gameFile;
         $targetImg = $imgDirectory . $imgFile;
         // Move img and game file to the given directory
         move_uploaded_file($credential->getGameFile()["tmp_name"], $targetFile);
         move_uploaded_file($credential->getImage()["tmp_name"], $targetImg);
         $records = $this->db;
         $records->query('INSERT INTO game (title, game, img) VALUES (:title, :game, :img)');
         $records->bind(':title', $title);
         $records->bind(':game', $gameFile);
         $records->bind(':img', $imgFile);
         $records->execute();
         return true;
     } catch (\Exception $e) {
         $listener->errorListener("AddGameDal::CouldNotAddGameException");
     }
 }
Exemplo n.º 2
0
 /**
  * Register a new user
  *
  * @param \model\User $credential
  * @param \model\IListener $listener
  * @return bool
  */
 public function doRegister(\model\User $credential, \model\IListener $listener)
 {
     $username = $credential->getUsername();
     $records = new \Db();
     $records->query('SELECT username,password FROM users WHERE username = :username');
     $records->bind(':username', $username);
     $records->resultset();
     if ($records->rowCount() > 0) {
         $listener->errorListener("Register::UserAlreadyExistException");
     } else {
         $password = password_hash($credential->getPassword(), PASSWORD_BCRYPT);
         $records->query('INSERT INTO users (username, password) VALUES (:username, :password)');
         $records->bind(':username', $username);
         $records->bind(':password', $password);
         $records->execute();
         $this->sessionStorage->set(SessionStorage::$auth, $username);
         return true;
     }
 }