public function updateProfileImage(Request $request, Response $response, array $args) { /* Directory to move the file to once processed */ $destination = __DIR__ . "../../../../../images/"; /* The uploaded file */ /** @var $file UploadedFile */ $file = $request->getUploadedFiles()['image']; /* If there is an error in the file, stop upload */ if ($file->getError() != UPLOAD_ERR_OK) { return "Upload failed"; } $currentLocation = $file->file; /* If the file is not a jpg, png, or gif, stop upload */ $finfo = new \finfo(FILEINFO_MIME_TYPE); if (false === ($ext = array_search($finfo->file($currentLocation), array('jpg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif'), true))) { return "Invalid File Format. Only .jpg, .gif, and .png accepted."; } $size = getimagesize($currentLocation); /* If the file is greater than 2MB in size, stop upload. */ if ($size > 1024 * 1024 * 2) { return "Upload failed. Image is greater than 2MB."; } $givenName = $file->getClientFilename(); /* Generate a unique name for this file from its SHA1 hash. */ $fileHashName = sha1_file($currentLocation); /* Build the full path and extension of the file */ $fullFilePath = $destination . $fileHashName . $ext; /* Replace uploaded file with recreated image and save */ switch ($ext) { case 'jpg': $image = imagecreatefromjpeg($currentLocation); imagejpeg($image, $fullFilePath); break; case 'gif': $image = imagecreatefromgif($currentLocation); imagegif($image, $fullFilePath); break; case 'png': $image = imagecreatefrompng($currentLocation); imagealphablending($image, true); imagesavealpha($image, true); imagepng($image, $fullFilePath); break; } /* Remove original upload */ unlink($currentLocation); /* Get user from route args */ $user = $args['user']; /* Make change in Database */ if ($this->dbService->updateUserImage($user['username'], $fullFilePath, $givenName, $size)) { return "Upload successful"; } else { return "Upload failed"; } }
/** * @param Request $request * @param Response $response * @param array $args * @return mixed */ public function verifyUser(Request $request, Response $response, array $args) { $post = $request->getParsedBody(); $ret = array(); if ($this->dbService->verifyUser($post['id'])) { $ret['success'] = true; $ret['message'] = "Verified."; } else { $ret['success'] = false; $ret['message'] = "Verification Unsuccessful."; } return $ret; }
/** * Authenticates a user if given the correct username and password. * * @param Request $request The HTTP Request object. * @param Response $response The HTTP Response object. * @param array $args The array containing arguments provided. * * @return string The message from the authentication process. */ public function authenticate(Request $request, Response $response, array $args) { //get post variables from request body $post = $request->getParams(); //validate post variables (exist, and as expected) /** @var Validator $v */ $v = new Validator($post); $v->rule('required', ['username', 'password']); $ret = array(); //if validation fails, exit, else authenticate if ($v->validate()) { if (password_verify($post['password'], $this->dbService->getPassword($post['username']))) { $user = $this->dbService->getUser($post['username']); if ($user) { if ($this->dbService->hasVerified($post['username'])) { $remember = $post['remember']; $this->startSession($user, $remember); $ret['success'] = true; $ret['message'] = "authenticated"; } else { $ret['success'] = false; $ret['message'] = "This account has not yet been verified."; } } else { $ret['success'] = false; $ret['message'] = "Incorrect username and/or password"; } } else { $ret['success'] = false; $ret['message'] = "Incorrect username and/or password"; } } else { $ret['success'] = true; $ret['message'] = "Please enter your username and password."; } return json_encode($ret); }
/** * @depends testUserIsNotVerifiedOnInsert * @param $data array * @return mixed */ public function testCanVerifyUser($data) { $this->dbService->verifyUser($data['key']); $this->assertTrue($this->dbService->hasVerified($data['username'])); return $data; }