<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Users extends CI_Controller {
var $homeurl;
function __construct() {
parent::__construct();
$this->homeurl = base_url();
}
function index() {
echo " <p><a href='{$this->homeurl}/Users/register'>Register</a></p>";
echo " <p><a href='{$this->homeurl}/Users/login'>Login</a></p>";
print_r($this->session->all_userdata());
}
function register() {
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[6]|max_length[32]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|alpha_numeric|min_length[6]|max_length[32]|xss_clean|md5');
$this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[10]|max_length[255]|xss_clean|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('/change_user/register_load');
//bad
} else {
//okey
$data['username'] = $this->input->post('username');
$data['password'] = $this->input->post('password');
$data['email'] = $this->input->post('email');
$this->User_model->add($data['username'], $data['password'], $data['email']);
}
}
//register
function login() {
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[3]|max_length[32]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|alpha_numeric|min_length[6]|max_length[32]|xss_clean|md5');
if ($this->form_validation->run() == FALSE) {
$this->load->view('/change_user/login_view');
//bad
} else {
//okey
// $this->load->model('User_model');
echo " okey login ";
$user_id = $this->User_model->lookingfor_user_login($this->input->post('username'), $this->input->post('password'));
if ($user_id != false) {
$session_data = array('loged' => true, 'username' => $this->input->post('username'), 'id' => $user_id);
$this->session->set_userdata($session_data);
echo "lognat<br/>";
print_r($this->session->all_userdata());
} else {
echo "problem , opitaite pak sled malko ";
}
}
}
//login
function logout() {
$this->session->sess_destroy();
redirect(base_url('/Users/login'), 'refresh');
}
//logout
function change_pass() {
$user_id = $this->session->userdata('id');
if ($this->User_model->is_loged() != false) {
$this->load->view('change_user/change_password'); // load view
$this->form_validation->set_rules('old_password', 'Password', 'required|alpha_numeric|min_length[6]|max_length[32]|xss_clean|md5');
$this->form_validation->set_rules('new_password', 'Password', 'required|alpha_numeric|min_length[6]|max_length[32]|xss_clean|md5');
$this->form_validation->set_rules('new_password_again', 'Password', 'required|alpha_numeric|min_length[6]|max_length[32]|xss_clean|matches[new_password]|md5');
// set rules
if ($this->form_validation->run() != FALSE) {
$old_pass = $this->input->post('old_password');
$new_pass = $this->input->post('new_password');
$new_pass_again = $this->input->post('new_password');
// get data from form
if ($this->User_model->set_user_password($user_id, $old_pass, $new_pass)) {
echo " fade ... okey , changed password ";
} else {
echo " fade - bad, cant change it ";
}
//okey
} else {
echo " fail valid";
}
} else {
redirect(base_url('/Users/login'), 'refresh');
}
}
//change pass
function change_mail() {
$user_id = $this->session->userdata('id');
if ($this->User_model->is_loged() != false) {
$mail_for_set = $this->User_model->get_user_data($user_id, 2); // get user mail
$this->form_validation->set_rules('mail', 'Email', 'trim|required|min_length[10]|max_length[255]|xss_clean|valid_email');
if ($this->form_validation->run() != FALSE) {
$new_mail = $this->input->post('mail');
if ($mail_for_set != $new_mail) {
$mail_for_set = $new_mail;
$this->User_model->set_user_data($user_id, $mail_for_set, 2);
echo " fade.... OKEY MSG";
}
//okey
} else {
}
if ($mail_for_set != false) {
$this->load->view('/change_user/change_mail', array('value_mail' => $mail_for_set));
}
} else {
$this->load->view("fail_page");
}
}
//change mail
function change_username() {
$user_id = $this->session->userdata('id');
if ($this->User_model->is_loged() != false) {
$username_for_set = $this->User_model->get_user_data($user_id, 1); // get user mail
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[6]|max_length[32]|xss_clean');
if ($this->form_validation->run() != FALSE) {
$new_username = $this->input->post('mail');
if ($username_for_set != $new_username) {
$username_for_set = $new_username;
$this->User_model->set_user_data($user_id, $username_for_set, 1);
echo " fade.... OKEY MSG";
}
//okey
} else {
}
if ($mail_for_set != false) {
$this->load->view('/change_user/change_mail', array('value_mail' => $username_for_set));
}
} else {
$this->load->view("fail_page");
}
}
//change username
}