Skip to content

Instantly share code, notes, and snippets.

@joelmandell
Created November 2, 2015 21:10
Show Gist options
  • Save joelmandell/1b20fa5bf61bfb580480 to your computer and use it in GitHub Desktop.
Save joelmandell/1b20fa5bf61bfb580480 to your computer and use it in GitHub Desktop.
CodeIgniter - UserController kodsnutt
<?php
/*TODO Make comment about the goal with this class and quick examples!!!!!*/
class User extends Controller {
var $pages;
function User()
{
/*Initiate the User class/controller and load the session and database helpers.*/
parent::Controller();
$this->load->library('session');
$this->load->database();
$this->load->library('auth');
}
function index()
{
/*Loads the module for this controller*/
$this->load->model('UserModel');
$data['text'] = "";
$this->load->library('auth');
//USER VALIDATION. Needs major revamp.
if($this->auth->get_session_user_status()=="true")
{
//Validation succesful, then show the admin links and the modul selection combo box.
$data['text'] .= $this->UserModel->content();
$data['text_sidebar'] = $this->UserModel->links();
$data['text_sidebar'] .= $this->UserModel->list_modules();
} else {
//If NOT LOGGED IN then show the login form again.
$data['text'] .= $this->UserModel->form();
}
//Present the model data appended to the $data array on the view "mandell_view2"
$this->load->view('mandell_view2', $data);
}
function register()
{
$this->load->model('UserModel');
$data['text']=$this->UserModel->register();
$this->load->view('mandell_view2', $data);
}
function activate($id)
{
$this->load->model('UserModel');
$this->load->database();
$query = $this->db->query("select * from users where id=$id and active=1");
if ($query->num_rows() > 0)
{
$data['text']=$this->UserModel->activation_already_done();
} else {
$query = $this->db->query("UPDATE users SET active=1 WHERE id=$id");
$data['text']=$this->UserModel->activation_finished();
}
$this->load->view('mandell_view2', $data);
}
function choose_module()
{
//301 direction is the shit! This is like a lilbit ugly, could maybe be done better?
if($this->auth->get_session_user_status()=="true") redirect("".$this->get_url_prefix()."/module/".$this->input->post('Modul'), "301");
}
function get_url_prefix()
{
/*Function to generate a url prefix for form actions and append it.
For example: $uri=$this->get_url_prefix."/module/".
*/
return str_replace("model","",strtolower(get_class($this)));
}
function account_settings($param="overview") //The standard param is to show the form overview to make changements of your settings.
{
$this->load->model('UserModel');
$data['text'] = "";
$this->load->library('auth');
if($this->auth->get_session_user_status()=="true")
{
//Validation succesful, then show the admin links and the modul selection combo box.
switch($param)
{
case 'overview':
$data['text'] .= $this->UserModel->account_settings();
$data['text_sidebar'] = $this->UserModel->links();
$data['text_sidebar'] .= $this->UserModel->list_modules();
break;
case 'update':
$data['text'] .= $this->UserModel->update_account_settings();
$data['text_sidebar'] = $this->UserModel->links();
$data['text_sidebar'] .= $this->UserModel->list_modules();
break;
}
} else {
//If NOT LOGGED IN then show the login form again.
$data['text'] .= $this->UserModel->form();
}
$this->load->view('mandell_view2', $data);
}
function module($module, $func="false", $id=0)
{
/*
IF We are authenticated and the standard $func argument is not changed...
Then we are going to show the links for editing the selected($module) model.
*/
if($this->auth->get_session_user_status()=="true" && $func=="false")
{
$model=$module."Model";
$this->load->model('UserModel');
$this->load->model($model);
$data['text_sidebar'] = $this->UserModel->links();
$data['text_sidebar'] .= $this->UserModel->list_modules();
$data['text']=$this->$model->create_edit_item_links();
$this->load->view('mandell_view2', $data);
}
/*If the func argument is not set to false, then the argument is to show a function
for example edit function for the selected module IE blog or news!*/
if(($this->auth->get_session_user_status()=="true") && ($func!="false"))
{
$this->load->model($module);
$func.="_form";
$data['text']=$this->$module->$func($id);
$data['edit']=$this->$module->create_form_editor();
$this->load->view('mandell_view2', $data);
}
}
function login()
{
$this->load->model("AuthModel");
$status=$this->auth->login($this->input->post('user'),$this->input->post('pass')); //Get the returned status from the auth class.
$data['text']=$this->AuthModel->read_status($status); //Read in the status message and show it later on in the view.
$this->load->view("mandell_view2",$data);
}
function logout()
{
$this->auth->logout();
}
function not_active()
{
$this->load->model('UserModel');
$data['text']=$this->UserModel->not_active();
$this->load->view('mandell_view2',$data);
}
function reg_account()
{
$this->load->helper(array('form', 'url'));
$this->load->database();
$this->load->library('form_validation');
$this->load->library('session');
$this->form_validation->set_error_delimiters('<div class="error"><div class="space">', '</div></div><br />');
$this->form_validation->set_rules('captcha', 'security code', 'callback_captcha_check');
$this->form_validation->set_rules('user', 'användarnamn', 'callback_username_check');
$this->form_validation->set_rules('email', 'felaktig eller ingen email-adress angiven', 'callback_email_check');
$this->form_validation->set_rules('pass', 'lösenord', 'callback_password_check');
if ($this->form_validation->run() == FALSE)
{
$this->load->model('UserModel');
$data['text'] = validation_errors();
$data['text'] .= $this->UserModel->register();
// $data['right_text'] = $this->Start->member_benefits();
$this->load->view('mandell_view2',$data);
}
else
{
$pass=$this->input->post('pass');
$user=$this->input->post('user');
$email=$this->input->post('email');
$salt_str="6fc9205039ece914f0db009bb5fd321b"; //MD5 of my FOOBAR
$salt = sha1(md5($salt_str.$pass)); //Create a salt algorithm.
$pass = md5($pass.$salt); //Scramble it together with a bit of md5:ing..and voila we have b33f with eggs!
/*Creates a user and add it to the standard group (id=3) that is 'users'.*/
$sql = "INSERT INTO users (username, password, active, groups) VALUES (".$this->db->escape($user).", ".$this->db->escape($pass).", '0','3')";
$this->db->query($sql);
$query = $this->db->query("select id from users where password like ".$this->db->escape($pass)." and username like ".$this->db->escape($user)."");
$userId="";
if ($query->num_rows() > 0)
{
$userId = $query->row()->id;
}
$sql = "INSERT INTO userInformation (userId, email)
VALUES (".$this->db->escape($userId).", ".$this->db->escape($email).")";
$this->db->query($sql);
$this->activation_mail($email, $userId);
redirect('/../user/activate_pending', 'location', 301);
}
}
function activate_pending()
{
$this->load->model('UserModel');
$data['text']=$this->UserModel->activation_pending();
$this->load->view('mandell_view2',$data);
}
function activation_mail($email, $id)
{
$this->load->library('email');
$this->email->from('reg@openzource.se', 'Joel Mandell');
$this->email->to($email);
$this->email->subject('Bekräfta erat användarkonto');
$text="Adress för att bekräfta:\n\nhttp://www.openzource.org/user/activate/$id";
$this->email->message($text);
$this->email->send();
}
function captcha_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('captcha_check', 'Du glömmde fylla i %s.');
return FALSE;
} else {
if($this->session->userdata('security_code') == strtolower($this->input->post('captcha')))
{
return TRUE;
} else {
$this->form_validation->set_message('captcha_check', 'Felaktig %s.');
return FALSE;
}
}
$this->session->sess_destroy();
}
function personalnumber_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('personalnumber_check', 'Pamirše užpilditi jūsu %s.');
return FALSE;
} else {
$query = $this->db->query("select * from userInformation where personalNr like '$str'");
if ($query->num_rows() > 0)
{
$this->form_validation->set_message('personalnumber_check', 'Jau yra tokie %s.');
return FALSE;
} else {
return TRUE;
}
}
}
function username_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('username_check', 'Du glömde ange användarnamn %s.');
return FALSE;
}
else
{
$user=$this->input->post("user");
$email = $user;
$query = $this->db->query("select * from users where id like (select userId from userInformation where email like '$user')");
if ($query->num_rows() > 0)
{
$this->form_validation->set_message('username_check', 'En användare med det namnet finns redan %s.');
return FALSE;
} else {
return TRUE;
}
}
}
function email_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('email_check', 'Du glömde ange epost %s.');
return FALSE;
}
else
{
$email=$this->input->post("email");
if(filter_var($email, FILTER_VALIDATE_EMAIL))
{
$query = $this->db->query("select * from users where id like (select userId from userInformation where email like '$email')");
if ($query->num_rows() > 0)
{
$this->form_validation->set_message('email_check', 'Ett konto med den adressen är redan registrerad. %s.');
return FALSE;
} else {
return TRUE;
}
} else {
$this->form_validation->set_message('email_check', 'Felaktig email-adress format %s.');
return FALSE;
}
}
}
function password_check($str)
{
if ($str == '')
{
$this->form_validation->set_message('password_check', 'Du glömmde fylla i %s.');
return FALSE;
}
else
{
return TRUE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment