<?php require_once 'viewmodels/Profile_ViewModel.php'; require_once 'viewmodels/Error_ViewModel.php'; require_once 'data/UserSession.php'; require_once 'data/data.php'; require_once 'utils/http.php'; require_once 'utils/string.php'; $session = UserSession::getInstance(); $data = MorpheusPetsData::getInstance(); // Get logged in user $loggedInUser = $session->getLoggedInUser(); // Username searched $usernameSearched = StringUtils::sanitize($_GET['search']); //Get profile's user $profileUser = $data->getUserByUserName($usernameSearched); if (!is_null($profileUser)) { $profileId = $profileUser->getId(); HTTPUtils::my_http_redirect("my_profile.php?profileId={$profileId}"); } else { $ret = $data->getLikeUsers($usernameSearched); if (!empty($ret)) { $viewModel = new Profile_ViewModel($loggedInUser, $profileUser); $viewModel->renderSearchResults($ret, $usernameSearched); } else { //Setup view model $viewModel = new Error_ViewModel(); $viewModel->renderFailSearch($usernameSearched); } }
require_once 'viewmodels/PetEditor_ViewModel.php'; require_once 'viewmodels/Error_ViewModel.php'; require_once 'data/UserSession.php'; require_once 'data/data.php'; require_once 'utils/http.php'; require_once 'utils/string.php'; require_once 'utils/Pet.php'; $session = UserSession::getInstance(); // Redirect to home page if not logged in if (!$session->isUserLoggedIn()) { HTTPUtils::my_http_redirect('index.php'); } // POST keys $pet_id_key = 'pet_id'; // Form inputs $form_pet_id = empty($_POST[$pet_id_key]) ? null : intval(StringUtils::sanitize($_POST[$pet_id_key])); $data = MorpheusPetsData::getInstance(); // Get logged in user $logged_in_user = $session->getLoggedInUser(); // Get pet to edit $pet_to_edit = $form_pet_id === null ? null : $data->getPet($form_pet_id); // Check if pet was found if ($pet_to_edit === null) { // Pet does not exist $view_model = new Error_ViewModel(); $view_model->renderPetNotExist($form_pet_id); } elseif (!PetUtils::userCanEditPet($logged_in_user, $pet_to_edit)) { // Pet does not belong to logged in user $view_model = new Error_ViewModel(); $view_model->renderEditPetNotAllowed($pet_to_edit); } else {
<?php require_once 'viewmodels/Error_ViewModel.php'; require_once 'data/UserSession.php'; require_once 'data/data.php'; require_once 'utils/http.php'; require_once 'utils/string.php'; $session = UserSession::getInstance(); $data = MorpheusPetsData::getInstance(); // Get logged in user $loggedInUser = $session->getLoggedInUser(); $edited_description = StringUtils::sanitize($_POST['description-text']); $profileId = $_POST['profile-user']; $profileUser = $data->getUser($profileId); if ($loggedInUser->getId() == $profileUser->getId() && !is_null($loggedInUser)) { $data->updateUser($profileUser, $edited_description); HTTPUtils::my_http_redirect("my_profile.php?profileId={$profileId}"); } else { //Setup view model $viewModel = new Error_ViewModel(); $viewModel->renderUserNotExist($profileId); }
/** * View for registering a new user. * If a user is successfully registered, they are redirected to the home page. */ public function renderRegisterUser() { // POST keys $submit_key = 'submit'; $username_key = 'username'; $password_key = 'password'; $email_address_key = 'email_address'; $description_key = 'description'; // Form inputs $username = null; $password = null; $email_address = null; $description = null; $errors_found = false; // View Config $view_data['page_title'] = 'Register'; $view_data['js'] = '<script src="js/register.js"></script>'; // Fill in view data if (isset($_POST[$username_key])) { $view_data['username'] = $_POST[$username_key]; } if (isset($_POST[$password_key])) { $view_data['password'] = $_POST[$password_key]; } if (isset($_POST[$email_address_key])) { $view_data['email_address'] = $_POST[$email_address_key]; } if (isset($_POST[$description_key])) { $view_data['description'] = $_POST[$description_key]; } // Check form was submitted if (isset($_POST[$submit_key])) { // Check user name is set if (empty($_POST[$username_key]) || StringUtils::whitespaceOnly($_POST[$username_key])) { $view_data['err_username'] = "******"; $errors_found = true; } else { // Sanitize the user name $username = StringUtils::sanitize($_POST[$username_key]); // Check user name meets length requirements if (strlen($username) > 100) { $view_data['err_username'] = "******"; $errors_found = true; } } // Check password is set if (empty($_POST[$password_key]) || StringUtils::whitespaceOnly($_POST[$password_key])) { $view_data['err_password'] = "******"; $errors_found = true; } else { $password = StringUtils::sanitize($_POST[$password_key]); } // Check email address is set if (empty($_POST[$email_address_key]) || StringUtils::whitespaceOnly($_POST[$email_address_key])) { $view_data['err_email_address'] = "Please enter an email address."; $errors_found = true; } else { // Sanitize the email address $email_address = StringUtils::sanitize($_POST[$email_address_key]); // TODO: Check email address follows the right pattern } // Check description is set if (empty($_POST[$description_key]) || StringUtils::whitespaceOnly($_POST[$description_key])) { $view_data['err_description'] = "Please enter a brief description."; $errors_found = true; } else { // Sanitize the description $description = StringUtils::sanitize($_POST[$description_key]); } if ($errors_found) { $view_data['form_err'] = "Please see errors below."; } else { // Check if user with given username already exists if (!is_null($this->data->getUserByUserName($username))) { $view_data['err_username'] = "******"; } else { // Add user to database $new_user = new User($username, $email_address, $description); $new_user->setPassword($password); $new_user_id = $this->data->addUser($new_user); // Login user if they were successfully added if ($new_user_id) { $_SESSION['user_id'] = $new_user_id; HTTPUtils::my_http_redirect('index.php'); } else { $view_data['form_err'] = "Failed to add new user. Please try again."; } } } } $this->renderTemplate('templates/header.php', $view_data); $this->renderTemplate('templates/register_view.php', $view_data); $this->renderTemplate('templates/footer.php', $view_data); }
/** * View for registering a new user. * If a user is successfully registered, they are redirected to the home page. */ public function renderRegisterUser() { // POST keys $submit_key = 'submit'; $username_key = 'username'; $password_key = 'password'; $email_address_key = 'email_address'; $description_key = 'description'; // Form inputs $form_username = empty($_POST[$username_key]) ? null : StringUtils::sanitize($_POST[$username_key]); $form_password = empty($_POST[$password_key]) ? null : StringUtils::sanitize($_POST[$password_key]); $form_email_address = empty($_POST[$email_address_key]) ? null : StringUtils::sanitize($_POST[$email_address_key]); $form_description = empty($_POST[$description_key]) ? null : StringUtils::sanitize($_POST[$description_key]); $form_submit = isset($_POST[$submit_key]); $errors_found = false; // View Config $view_data['page_title'] = 'Register'; // Fill in view data if ($form_username !== null) { $view_data['username'] = $form_username; } if ($form_password !== null) { $view_data['password'] = $form_password; } if ($form_email_address !== null) { $view_data['email_address'] = $form_email_address; } if ($form_description !== null) { $view_data['description'] = $form_description; } // Check form was submitted if ($form_submit) { // Check user name is set if ($form_username === null || StringUtils::whitespaceOnly($form_username)) { $view_data['err_username'] = "******"; $errors_found = true; } elseif (strlen($form_username) > 100) { $view_data['err_username'] = "******"; $errors_found = true; } // Check password is set if ($form_password === null || StringUtils::whitespaceOnly($form_password)) { $view_data['err_password'] = "******"; $errors_found = true; } // Check email address is set if ($form_email_address === null || StringUtils::whitespaceOnly($form_email_address)) { $view_data['err_email_address'] = "Please enter an email address."; $errors_found = true; } elseif (!filter_var($form_email_address, FILTER_VALIDATE_EMAIL)) { $view_data['err_email_address'] = "Please enter a valid email address."; $errors_found = true; } // Check description is set if ($form_description === null || StringUtils::whitespaceOnly($form_description)) { $view_data['err_description'] = "Please enter a brief description."; $errors_found = true; } // Form was submitted without errors if (!$errors_found) { // Check if user with given username already exists if ($this->data->getUserByUserName($form_username) !== null) { $view_data['err_username'] = "******"; $errors_found = true; } else { // Add user to database $new_user = new User($form_username, $form_email_address, $form_description); $new_user->setPassword($form_password); $new_user_id = $this->data->addUser($new_user); // Login user if they were successfully added if ($new_user_id) { $_SESSION['user_id'] = $new_user_id; HTTPUtils::my_http_redirect('index.php'); } else { $view_data['form_err'] = "Failed to add new user. Please try again."; } } } if ($errors_found) { $view_data['form_err'] = "Please see errors below."; } } $this->renderTemplate('templates/header.php', $view_data); $this->renderTemplate('templates/register_view.php', $view_data); $this->renderTemplate('templates/footer.php', $view_data); }
/** * Create/Edit a pet * * @param $view_data * @param bool $create whether a pet is being created or edited * * @return mixed */ private function editPet($view_data, $create = false) { // POST keys $submit_key = 'submit'; $name_key = 'name'; $species_id_key = 'species_id'; $pet_id_key = 'pet_id'; $errors_found = false; // Gather sanitized form inputs $form_name = empty($_POST[$name_key]) ? null : StringUtils::sanitize($_POST[$name_key]); $form_species_id = empty($_POST[$species_id_key]) ? null : intval(StringUtils::sanitize($_POST[$species_id_key])); $form_submit = isset($_POST[$submit_key]); // Other data $species = null; // View config $view_data['species_list'] = $this->species_list; // Pet is being created if ($create) { // Fill in view data if ($form_name !== null) { $view_data[$name_key] = $form_name; } if ($form_species_id !== null) { $view_data[$species_id_key] = $form_species_id; } } else { // Set the flag that this pet is being edited // Species should not be modifiable $view_data['edit_mode'] = true; // Fill in view data $view_data[$pet_id_key] = $this->pet_to_edit->getId(); $view_data[$name_key] = $form_name === null ? $this->pet_to_edit->getName() : $form_name; $view_data[$species_id_key] = $this->pet_to_edit->getSpecies()->getId(); } // Check form was submitted without error if ($form_submit) { // Check name is set if ($form_name === null || StringUtils::whitespaceOnly($form_name)) { $view_data['err_name'] = "Please enter a name."; $errors_found = true; } elseif (strlen($form_name) > 64) { $view_data['err_name'] = "Please enter a name that is no greater than 64 characters."; $errors_found = true; } // Check species_id is set // Only using in create mode if ($create && $form_species_id === 0) { $view_data['err_species'] = "Please select a species."; $errors_found = true; } elseif ($create) { $species = $this->data->getSpecies($form_species_id); if ($species === null) { $view_data['err_species'] = "Could not find species. Please try again."; $errors_found = true; } } if ($errors_found) { $view_data['form_err'] = "Please see errors below."; } else { // Check if a pet is being created if ($create) { $new_pet = new Pet($this->logged_in_user, $species, $form_name); // Generate initial stats $new_pet->rollStats(); // Make pet active if user does not have a full active set $active_pets = $this->data->getActivePetsForUser($this->logged_in_user->getId()); if (count($active_pets) < 3) { $new_pet->setActive(true); } // Add pet to database $new_pet_id = $this->data->addPet($new_pet); // Redirect to view pet if added successfully if ($new_pet_id) { HTTPUtils::my_http_redirect('pet.php?pet_id=' . $new_pet_id); } else { $view_data['form_err'] = 'Failed to add pet. Please try again.'; } } else { // Update pet fields // Species cannot be modified $this->pet_to_edit->setName($form_name); // Update pet in database $updated = $this->data->updatePet($this->pet_to_edit); // Redirect to view pet if updated successfully if ($updated) { HTTPUtils::my_http_redirect('pet.php?pet_id=' . $this->pet_to_edit->getId()); } else { $view_data['form_err'] = 'Failed to update pet. Please try again.'; } } } } return $view_data; }