/application/main.php
PHP | 422 lines | 253 code | 100 blank | 69 comment | 23 complexity | 5369833cb1bb1074e5593d76540966a3 MD5 | raw file
- <?php
- class Main extends Controller {
- function Main()
- {
- parent::Controller();
- $this->load->library('session');
- $this->load->model('directory_model');
- $this->load->helper(array('form', 'url'));
- $this->load->library('form_validation');
- }
-
- function index()
- {
- $this->load->model('directory_model');
- // get all faculty data to display
- $data = array();
- $data['directory'] = $this->directory_model->get_faculty();
- // get unique user names
- $users = $this->directory_model->get_users();
- // extract user names into numbered array
- $user = array();
- $i = 0;
- foreach ($users as $un){
- foreach ($un as $key => $val){
- $user[$i] = $val;
- $i = $i + 1;
- }
- }
- $data['users'] = $user;
- // load main directory page
- $this->load->view('header');
- $this->load->view('directory', $data);
- $this->load->view('footer');
- }
- function profile($id)
- {
- // get all data to be displayed on profile page
- $data = array();
- $data['profile'] = $this->directory_model->get_profile($id);
- // load main profile page
- $this->load->view('header');
- $this->load->view('profile', $data);
- $this->load->view('footer');
- }
- function login()
- {
- // run if login data has been submitted
- if (isset($_POST['un']) && isset($_POST['pw']))
- {
- // validate password
- $this->form_validation->set_rules('un', 'Username', 'strip_tags|required|max_length[25]|min_length[2]|alpha_dash');
- $this->form_validation->set_rules('pw', 'The Password', 'strip_tags|required|max_length[100]|min_length[2]');
- if ($this->form_validation->run() === TRUE)
- {
- // check login against database
- $result = $this->directory_model->check_login($_POST['un'], $_POST['pw']);
- if ($result === TRUE)
- {
- // set session data
- $this->session->set_userdata('loggedIn', TRUE);
- $this->session->set_userdata('user', $_POST['un']);
- // if this is users first login, make them change password and username
- if ($_POST['un'] === $_POST['pw'])
- $this->update_password();
- // if not first login load user's profile
- else
- $this->profile($_POST['un']);
- }
- // if incorrect login, reload website
- else
- {
- $this->load->view('header');
- $this->load->view('login_fail');
- $this->load->view('login');
- $this->load->view('footer');
- }
- }
- // if form doesn't validate, display error messages
- else
- {
- $this->load->view('header');
- $this->load->view('login');
- $this->load->view('footer');
- }
- }
- // if form has not been submitted, load main login page
- else
- {
- $this->load->view('header');
- $this->load->view('login');
- $this->load->view('footer');
- }
- }
- function logout()
- {
- // kill all user data and session, then load main directory page
- $this->session->unset_userdata('loggedIn');
- $this->session->unset_userdata('user');
- $this->session->sess_destroy();
- $this->index();
- }
- function register()
- {
- // run if form has been submitted
- if (isset($_POST['fname']) && isset($_POST['lname']))
- {
- // validation rules. makes sure good values are submitted
- $this->form_validation->set_rules('fname', 'First name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('lname', 'Last name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('title', 'Title', 'strip_tags|trim|required|max_length[100]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('phone', 'Phone Number', 'strip_tags|trim|required|max_length[15]|min_length[10]|xxs_clean');
- $this->form_validation->set_rules('email', 'Email', 'strip_tags|callback_good_email|required|max_length[50]|min_length[2]|valid_email');
- $this->form_validation->set_rules('location', 'Location', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('department', 'Department', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('bio', 'Biography', 'strip_tags|trim|max_length[1000]|xxs_clean');
- $this->form_validation->set_rules('cv', 'CV', 'strip_tags|trim|max_length[100]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('homepage', 'Home Page', 'strip_tags|trim|max_length[100]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('username', 'Username', 'strip_tags|callback_username_unique|trim|max_length[50]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('pw1', 'Password', 'strip_tags|required|max_length[50]|min_length[2]|matches[pw2]');
- $this->form_validation->set_rules('pw2', 'Confirm Password', 'strip_tags|required|max_length[50]|min_length[2]');
- // if form passes validation
- if ($this->form_validation->run() === TRUE)
- {
- // extracts post info into array for simple insertion to database
- $memberInfo = array(
- "fname" => $_POST['fname'],
- "lname" => $_POST['lname'],
- "title" => $_POST['title'],
- "phone" => $_POST['phone'],
- "email" => $_POST['email'],
- "location" => $_POST['location'],
- "department" => $_POST['department'],
- "bio" => $_POST['bio'],
- "homepage" => $_POST['homepage'],
- "cv" => $_POST['cv'],
- "user" => $_POST['username'],
- "password" => md5($_POST['pw1']));
- // attempt to insert values into database
- $result = $this->directory_model->register_new($memberInfo);
- // if insert is succesful, log user in and load profile
- if ($result === TRUE)
- {
- $this->session->set_userdata('user', $_POST['username']);
- $this->session->set_userdata('loggedIn', TRUE);
- $this->profile($_POST['username']);
- }
- // if there is a problem inserting load main directory page again
- else
- $this->index ();
-
- }
- // if the form does not validate, reload with validation errors
- else
- {
- $this->load->view('header');
- $this->load->view('register');
- $this->load->view('footer');
- }
- // end of post if
- }
-
-
-
- // if no form has been submitted, load form
- else
- {
- $this->load->view('header');
- $this->load->view('register');
- $this->load->view('footer');
- }
-
- }
- function update_password ()
- {
- // if there is no user logged in, load main directory page
- if (($this->session->userdata('loggedIn') != TRUE))
- $this->index ();
- else{
- // if there has been attempt to change password
- if (isset($_POST['pw']) && isset($_POST['pw2']))
- {
- // validate password
- $this->form_validation->set_rules('pw', 'Password', 'strip_tags|required|max_length[50]|min_length[2]|matches[pw2]');
- $this->form_validation->set_rules('pw2', 'Confirm Password', 'strip_tags|required|max_length[50]|min_length[2]');
- if ($this->form_validation->run() === TRUE)
- {
- // encrypt password and send to database
- $pw = md5($_POST['pw']);
- $result = $this->directory_model->update_password($this->session->userdata('user'), $pw);
- // if password update is succesful, load user's profile
- if ($result === TRUE)
- $this->profile($this->session->userdata('user'));
-
- // if there is a problem updating password load main directory page
- else
- $this->index();
- }
- // if form doesn't validate, reload form with validation errors
- else
- {
- $this->load->view('header');
- $this->load->view('update_password');
- $this->load->view('footer');
- }
- }
-
-
- // if the form hasn't been submitted, load form
- else
- {
- $this->load->view('header');
- $this->load->view('update_password');
- $this->load->view('footer');
- }
- }
- }
- function update_profile ()
- {
-
- // if there is no user logged in, load main directory page
- if (($this->session->userdata('loggedIn') != TRUE))
- $this->index ();
- else{
- // if form data has been submitted
- if (isset($_POST['fname']) && isset($_POST['lname']))
- {
- // validate form info
- $this->form_validation->set_rules('fname', 'First name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('lname', 'Last name', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean|');
- $this->form_validation->set_rules('title', 'Title', 'strip_tags|trim|required|max_length[100]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('phone', 'Phone Number', 'strip_tags|trim|required|max_length[15]|min_length[10]|xxs_clean');
- $this->form_validation->set_rules('email', 'Email', 'strip_tags|required|max_length[100]|min_length[2]|valid_email');
- $this->form_validation->set_rules('location', 'Location', 'strip_tags|trim|required|max_length[25]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('department', 'Department', 'strip_tags|trim|required|max_length[100]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('bio', 'Biography', 'strip_tags|trim|max_length[1000]|xxs_clean');
- $this->form_validation->set_rules('cv', 'CV', 'strip_tags|trim|max_length[75]|min_length[2]|xxs_clean');
- $this->form_validation->set_rules('homepage', 'Home Page', 'strip_tags|trim|max_length[75]|min_length[2]|xxs_clean');
- if ($this->form_validation->run() === TRUE)
- {
- // send data to database if validation is succesful
- $result = $this->directory_model->update_profile($_POST);
- // if query is succesful load user's profile
- if ($result === TRUE)
- $this->profile($_POST['user']);
- // if there is a database problem load main directory page
- else
- $this->index();
- }
- // if validation is not successful reload form with validation errors
- else
- {
- // convert post data to array to refill form values
- // the profile index name cannot change (matches same name when data is gotten from database
- $data = array();
- $profile = $_POST;
- $data['profile'] = $profile;
- $this->load->view('header');
- $this->load->view('update_profile', $data);
- $this->load->view('footer');
- }
- }
- // this will be ran if the form has not been submitted
- else
- {
- // get profile data and convert to array (loads here to be in scope throughout function)
- $rawProfile = $this->directory_model->get_profile($this->session->userdata('user'));
- $profile = array();
- foreach ($rawProfile as $rp)
- {
- foreach ($rp as $key => $val)
- {
- $profile[$key] = $val;
- }
- }
- $data['profile'] = $profile;
- // load form with database info as form values
- $this->load->view('header');
- $this->load->view('update_profile', $data);
- $this->load->view('footer');
- }
- }
- }
- /**
- *
- * Checks to see if the string passed to it is a unique value in the
- * database. If it is unique it returns true, otherwise it will
- * set the form_validation error message and return false
- *
- * @param string $username
- * @return bool
- */
- function username_unique($username)
- {
- $users = $this->directory_model->get_users();
- // Is there a row with this email?
- foreach ($users as $user)
- {
- foreach ($user as $key => $value)
- {
- // if this username exists return false and set error message
- if ($username === $value)
- {
- $this->form_validation->set_message('username_unique', 'That User Name is already used.');
- return FALSE;
- }
- }
- }
- // usename is unique, don't return an error.
- return TRUE;
- }
- /**
- * Checks that email address ends in @cci.fsu.edu. If it does it will
- * return true, if not it will set form_validation error message and return
- * false.
- *
- * @param string $email
- * @return bool
- */
- function good_email($email)
- {
- // if email does not end in @cci.fsu.edu set error and return false
- if (substr($email, -12, 12) !== "@cci.fsu.edu")
- {
- $this->form_validation->set_message('good_email', 'You must use a valid @cci.fsu.edu email address.');
- return FALSE;
- }
- else
- return TRUE;
- }
- }
- /* End of file welcome.php */
- /* Location: ./system/application/controllers/welcome.php */