Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- /**
- * User class.
- *
- * @extends CI_Controller
- */
- class User extends CI_Controller {
- /**
- * __construct function.
- *
- * @access public
- * @return void
- */
- public function __construct() {
- parent::__construct();
- $this->load->library(array('session'));
- $this->load->helper(array('url'));
- $this->load->model('user_model');
- }
- public function index($username = false) {
- if ($username === false) {
- redirect(base_url());
- return;
- }
- // create the data object
- $data = new stdClass();
- $data->title = "$username Profile";
- // load the forum model
- $this->load->model('forum_model');
- // get user id from username
- $user_id = $this->user_model->get_user_id_from_username($username);
- // create the user object
- $user = $this->user_model->get_user($user_id);
- $user->count_topics = $this->user_model->count_user_topics($user_id);
- $user->count_posts = $this->user_model->count_user_posts($user_id);
- $user->latest_post = $this->user_model->get_user_last_post($user_id);
- if ($user->latest_post !== null) {
- $user->latest_post->topic = $this->forum_model->get_topic($user->latest_post->topic_id);
- $user->latest_post->topic->forum = $this->forum_model->get_forum($user->latest_post->topic->forum_id);
- $user->latest_post->topic->permalink = base_url($user->latest_post->topic->forum->slug . '/' . $user->latest_post->topic->slug);
- } else {
- $user->latest_post = new stdClass();
- $user->latest_post->created_at = $user->username . ' has not posted yet';
- }
- $user->latest_topic = $this->user_model->get_user_last_topic($user_id);
- if ($user->latest_topic !== null) {
- $user->latest_topic->forum = $this->forum_model->get_forum($user->latest_topic->forum_id);
- $user->latest_topic->permalink = base_url($user->latest_topic->forum->slug . '/' . $user->latest_topic->slug);
- } else {
- $user->latest_topic = new stdClass();
- $user->latest_topic->title = $user->username . ' has not started a topic yet';
- }
- // create breadcrumb
- $breadcrumb = '<ol class="breadcrumb">';
- $breadcrumb .= '<li><a href="' . base_url() . '">Home</a></li>';
- $breadcrumb .= '<li class="active">' . $username . '</li>';
- $breadcrumb .= '</ol>';
- // create a button to permit profile edition
- $edit_button = '<a href="' . base_url('user/' . $user->username . '/edit') . '" class="btn btn-xs btn-success">Edit your profile</a>';
- // assign created objects to the data object
- $data->user = $user;
- $data->breadcrumb = $breadcrumb;
- if (isset($_SESSION['username']) && $_SESSION['username'] === $username) {
- // user is on his own profile
- $data->edit_button = $edit_button;
- } else {
- // user is not on his own profile
- $data->edit_button = null;
- }
- $this->template->load('user/profile/profile', $data);
- }
- /**
- * register function.
- *
- * @access public
- * @return void
- */
- public function register() {
- // create the data object
- $data=array(
- 'title' => 'Register'
- );
- // load form helper and validation library
- $this->load->helper('form');
- $this->load->library('form_validation');
- // set validation rules
- $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[4]|is_unique[users.username]', array('is_unique' => 'This username already exists. Please choose another one.'));
- $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
- $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
- $this->form_validation->set_rules('password_confirm', 'Confirm Password', 'trim|required|min_length[6]|matches[password]');
- if ($this->form_validation->run() === false) {
- // validation not ok, send validation errors to the view
- $this->template->load('register',$data);
- } else {
- // set variables from the form
- $username = $this->input->post('username');
- $email = $this->input->post('email');
- $password = $this->input->post('password');
- if ($this->user_model->create_user($username, $email, $password)) {
- // user creation ok
- $this->template->load('register_success',$data);
- } else {
- // user creation failed, this should never happen
- $data['error'] = 'There was a problem creating your new account. Please try again.';
- // send error to the view
- $this->template->load('register',$data);
- }
- }
- }
- /**
- * login function.
- *
- * @access public
- * @return void
- */
- public function login() {
- // create the data object
- $data=array(
- 'title' => 'Login'
- );
- // load form helper and validation library
- $this->load->helper('form');
- $this->load->library('form_validation');
- // set validation rules
- $this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
- $this->form_validation->set_rules('password', 'Password', 'required');
- if ($this->form_validation->run() == false) {
- // validation not ok, send validation errors to the view
- $this->template->load('login',$data);
- } else {
- // set variables from the form
- $username = $this->input->post('username');
- $password = $this->input->post('password');
- if ($this->user_model->resolve_user_login($username, $password)) {
- $user_id = $this->user_model->get_user_id_from_username($username);
- $user = $this->user_model->get_user($user_id);
- // set session user datas
- $_SESSION['user_id'] = (int)$user->id;
- $_SESSION['username'] = (string)$user->username;
- $_SESSION['logged_in'] = (bool)true;
- // user login ok
- $this->template->load('login_success',$data);
- } else {
- // login failed
- $data['error'] = 'Wrong username or password.';
- // send error to the view
- $this->template->load('login',$data);
- }
- }
- }
- /**
- * logout function.
- *
- * @access public
- * @return void
- */
- public function logout() {
- // create the data object
- $data=array(
- 'title' => 'Logout'
- );
- if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
- // remove session datas
- foreach ($_SESSION as $key => $value) {
- unset($_SESSION[$key]);
- }
- // user logout ok
- $this->template->load('logout_success',$data);
- } else {
- // there user was not logged in, we cannot logged him out,
- // redirect him to site root
- redirect('/');
- }
- }
- public function forget()
- {
- $data=array(
- 'title' => 'Forgot'
- );
- if (isset($_GET['info'])) {
- $data['info'] = $_GET['info'];
- }
- if (isset($_GET['error'])) {
- $data['error'] = $_GET['error'];
- }
- $this->template->load('login_forget',$data);
- }
- public function doforget()
- {
- $this->load->helper('url');
- $email= $_POST['email'];
- $q = $this->db->query("select * from users where email=?",Array($email));
- $r = $q->result();
- if ($q->num_rows() > 0) {
- $user=$r[0];
- $this->resetpassword($user);
- $info= "Password has been reset and has been sent to email id: ". $email;
- redirect('/Login/forget?info=' . $info, 'refresh');
- } else {
- $error= "The email id you entered not found on our database ";
- redirect('/Login/forget?error=' . $error, 'refresh');
- }
- }
- /**
- * edit function.
- *
- * @access public
- * @param mixed $username (default: false)
- * @return void
- */
- public function edit($username = false) {
- // a user cann only edit his own profile
- if ($username === false || $username !== $_SESSION['username']) {
- redirect(base_url());
- return;
- }
- // create the data object
- $data = new stdClass();
- $data->title = "Edit Profile";
- // load form helper and form validation library
- $this->load->helper('form');
- $this->load->library('form_validation');
- // form validation
- $password_required_if = $this->input->post('password') ? '|required' : ''; // if there is something on password input, current password is required
- $this->form_validation->set_rules('username', 'Username', 'trim|min_length[4]|max_length[20]|alpha_numeric|is_unique[users.username]', array('is_unique' => 'This username already exists. Please choose another username.'));
- $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|is_unique[users.email]', array('is_unique' => 'The email you entered already exists in our database.'));
- if($this->input->post('password') AND $this->input->post('current_password') AND $this->input->post('password_confirm')){
- $this->form_validation->set_rules('current_password', 'Current Password', 'trim' . $password_required_if . '|callback_verify_current_password');
- $this->form_validation->set_rules('password', 'New Password', 'trim|min_length[6]|matches[password_confirm]');
- $this->form_validation->set_rules('password_confirm', 'Password Confirmation', 'trim|min_length[6]');
- }
- // get the user object
- $user_id = $this->user_model->get_user_id_from_username($username);
- $user = $this->user_model->get_user($user_id);
- // create breadcrumb
- $breadcrumb = '<ol class="breadcrumb">';
- $breadcrumb .= '<li><a href="' . base_url() . '">Home</a></li>';
- $breadcrumb .= '<li><a href="' . base_url('user/' . $username) . '">' . $username . '</a></li>';
- $breadcrumb .= '<li class="active">Edit</li>';
- $breadcrumb .= '</ol>';
- // assign objects to the data object
- $data->user = $user;
- $data->breadcrumb = $breadcrumb;
- if ($this->form_validation->run() === false) {
- // validation not ok, send validation errors to the view
- $this->template->load('user/profile/edit', $data);
- } else {
- $user_id = $_SESSION['user_id'];
- $update_data = [];
- if ($this->input->post('username') != '') {
- $update_data['username'] = $this->input->post('username');
- }
- if ($this->input->post('email') != '') {
- $update_data['email'] = $this->input->post('email');
- }
- if ($this->input->post('password') != '') {
- $update_data['password'] = $this->input->post('password');
- }
- // avatar upload
- if (isset($_FILES['userfile']['name']) && !empty($_FILES['userfile']['name'])) {
- // setup upload configuration and load upload library
- $config['upload_path'] = './uploads/avatars/';
- $config['allowed_types'] = 'gif|jpg|png';
- $config['max_size'] = 2048;
- $config['max_width'] = 1024;
- $config['max_height'] = 1024;
- $config['file_ext_tolower'] = true;
- $config['encrypt_name'] = true;
- //get_instance()->load->library('upload', $config);
- $this->load->library('upload');
- $this->upload->initialize($config);
- if (!$this->upload->do_upload()) {
- // upload NOT ok
- $error = array('error' => $this->upload->display_errors());
- $this->load->view('upload_form', $error);
- } else {
- // Upload ok send name to $updated_data
- $update_data['avatar'] = $this->upload->data('file_name');
- }
- }
- // if everything is ok
- if ($this->user_model->update_user($user_id, $update_data)) {
- // if username change, update session
- if(isset($update_data['username'])) {
- $_SESSION['username'] = $update_data['username'];
- if ($this->input->post('username') != '') {
- // a little hook to send success message the new profil edit url if the username was updated
- $_SESSION['flash'] = 'Your profile has been successfully updated!';
- }
- }
- // fix the fact that a new avatar was not shown until page refresh
- if(isset($update_data['avatar'])) {
- $data->user->avatar = $update_data['avatar'];
- }
- if ($this->input->post('username') != '') {
- // redirect to the new profile edit url
- redirect(base_url('user/' . $update_data['username'] . '/edit'));
- } else {
- // create a success message
- $data->success = 'Your profile has been successfully updated!';
- // send success message to the views
- $this->template->load('user/profile/edit', $data);
- }
- } else {
- // update user not ok : this should never happen
- $data->error = 'There was a problem updating your account. Please try again.';
- //send errors to the views
- $this->template->load('user/profile/edit', $data);
- }
- }
- }
- /**
- * delete function.
- *
- * @access public
- * @param mixed $username (default: false)
- * @return void
- */
- public function delete($username = false) {
- // a user cann only delete his own profile and must be logged in
- if ($username == false || !isset($_SESSION['username']) || $username !== $_SESSION['username']) {
- redirect(base_url());
- return;
- }
- // create the data object
- $data = new stdClass();
- $data->title = "Delete Account";
- if ($_SESSION['username'] === $username) {
- // create breadcrumb
- $breadcrumb = '<ol class="breadcrumb">';
- $breadcrumb .= '<li><a href="' . base_url() . '">Home</a></li>';
- $breadcrumb .= '<li><a href="' . base_url('user/' . $username) . '">' . $username . '</a></li>';
- $breadcrumb .= '<li class="active">Delete</li>';
- $breadcrumb .= '</ol>';
- $user_id = $this->user_model->get_user_id_from_username($username);
- $data->user = $this->user_model->get_user($user_id);
- $data->breadcrumb = $breadcrumb;
- if ($this->user_model->delete_user($user_id)) {
- $data->success = 'Your user account has been successfully deleted. Bye bye :(';
- // user delete ok, load views
- $this->template->load('user/profile/delete', $data);
- } else {
- // user delete not ok, this should never happen
- $data->error = 'There was a problem deleting your user account. Please contact an administrator.';
- // send errors to the views
- $this->template->load('user/profile/edit', $data);
- }
- } else {
- // a user cann only delete his own profile and must be logged in
- redirect(base_url());
- return;
- }
- }
- private function resetpassword($user)
- {
- date_default_timezone_set('GMT');
- $this->load->helper('string');
- $password= random_string('alnum', 16);
- $this->db->where('id', $user->id);
- $this->db->update('users',array('password' => $this->user_model->hash_password($password)));
- $this->load->library('email');
- $this->email->from('withussocialize@gmail.com', 'Socializewithus');
- $this->email->to($user->email);
- $this->email->subject('Password reset');
- $this->email->message('You have requested the new password, Here is you new password:'. $password);
- $this->email->send();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement