From 355535fa933c0fc2c1d63f0d4df3b1fc6e60d05c Mon Sep 17 00:00:00 2001 From: Calin Burloiu Date: Fri, 23 Sep 2011 18:08:00 +0300 Subject: [PATCH] account activation almost ready --- application/config/form_validation.php | 14 +++ application/controllers/user.php | 113 +++++++++++++++++- .../language/english/form_validation_lang.php | 13 +- application/language/english/user_lang.php | 12 ++ application/models/users_model.php | 11 +- application/models/videos_model.php | 13 +- application/views/header.php | 2 +- application/views/user/activate_view.php | 33 +++++ .../views/user/activated_account_view.php | 2 + .../views/video/ns-html5_plugin_view.php | 2 + .../views/video/ns-vlc_plugin_view.php | 2 + application/views/video/watch_view.php | 2 +- css/default.css | 10 ++ 13 files changed, 208 insertions(+), 21 deletions(-) create mode 100644 application/views/user/activate_view.php create mode 100644 application/views/user/activated_account_view.php diff --git a/application/config/form_validation.php b/application/config/form_validation.php index 1e7c787..55e0212 100644 --- a/application/config/form_validation.php +++ b/application/config/form_validation.php @@ -72,6 +72,20 @@ $config = array( 'label'=>'lang:user_locality', 'rules'=>'trim|ucwords|xss_clean|prep_for_form' ) + ), + 'activate'=> array( + array( + 'field'=>'activation-code', + 'label'=>'lang:user_activation_code', + 'rules'=>'trim|required|strtolower|callback__valid_activation_code|callback__do_activate' + ) + ), + 'resend_activation'=> array( + array( + 'field'=>'email', + 'label'=>'lang:user_email', + 'rules'=>'trim|required|xss_clean|valid_email|callback__do_resend_activation' + ) ) ); diff --git a/application/controllers/user.php b/application/controllers/user.php index 793d7f4..8c43cce 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -10,6 +10,7 @@ class User extends CI_Controller { private $import = FALSE; private $activated_account = TRUE; + private $user_id = NULL; public function __construct() { @@ -64,7 +65,8 @@ class User extends CI_Controller { else { if (! $this->activated_account) - header('Location: '. site_url('catalog/test')); + header('Location: ' + . site_url("user/activate/{$this->user_id}")); else if (! $this->import) { // Redirect to last page before login. @@ -167,6 +169,9 @@ class User extends CI_Controller { $data['password'] = $this->input->post('new-password'); $this->users_model->set_userdata($user_id, $data); + + // Redirect to last page before login. + header('Location: '. site_url(urldecode_segments($redirect))); } // Registration else @@ -175,10 +180,13 @@ class User extends CI_Controller { $data['password'] = $this->input->post('password'); $this->users_model->register($data); + $user_id = $this->users_model->get_userdata($data['username'], + "id"); + $user_id = $user_id['id']; + + // Redirect account activation page. + header('Location: '. site_url("user/activate/$user_id")); } - - // Redirect to last page before login. - header('Location: '. site_url(urldecode_segments($redirect))); } } @@ -263,10 +271,81 @@ class User extends CI_Controller { $this->load->view('html_end'); } - public function activate($user_id, $activation_code) + public function activate($user_id, $method='', $activation_code='') { $user_id = intval($user_id); - echo ''. $this->users_model->activate_account($user_id, $activation_code); + $userdata = $this->users_model->get_userdata($user_id, + 'email, a.activation_code'); + $email = $userdata['email']; + //print_r($userdata['activation_code']); + $activated_account = ($userdata['activation_code'] == NULL); + + $this->load->library('form_validation'); + + $this->form_validation->set_error_delimiters('', + ''); + + $res_form_validation = FALSE; + if ($method == 'code') + { + $res_form_validation = $this->form_validation->run('activate'); + } + else if ($method == 'resend') + { + $res_form_validation = + $this->form_validation->run('resend_activation'); + } + + if ($res_form_validation === FALSE) + { + $params = array( + 'title'=> $this->lang->line('user_title_activation') + .' – ' + . $this->config->item('site_name'), + //'metas' => array('description'=>'') + ); + $this->load->library('html_head_params', $params); + + // ** + // ** LOADING VIEWS + // ** + $this->load->view('html_begin', $this->html_head_params); + $this->load->view('header', array()); + + if (! $activated_account) + { + $main_params['content'] = + $this->load->view('user/activate_view', + array('user_id'=> $user_id, 'email'=> $userdata['email']), + TRUE); + } + else + { + $main_params['content'] = + $this->load->view('user/activated_account_view', + NULL, TRUE); + } + + $main_params['side'] = $this->load->view('side_default', NULL, TRUE); + $this->load->view('main', $main_params); + + $this->load->view('footer'); + $this->load->view('html_end'); + } + else + { + if ($method == 'code') + { + // Redirect to a message which tells the user that the + // activation was successful. + header('Location: '. site_url("user/activate/$user_id")); + } + else if ($method == 'resend') + { + // Redirect to home page + header('Location: '. site_url()); + } + } } public function _update_session_userdata($data) @@ -330,6 +409,11 @@ class User extends CI_Controller { return TRUE; } + + public function _valid_activation_code($activation_code) + { + return (preg_match('/^[a-fA-F0-9]{16}$/', $activation_code) == 1); + } public function _do_login($username, $field_password) { @@ -345,6 +429,7 @@ class User extends CI_Controller { if ($user['activation_code'] !== NULL) { $this->activated_account = FALSE; + $this->user_id = $user['id']; return TRUE; } @@ -358,6 +443,22 @@ class User extends CI_Controller { $this->import = (isset($user['import']) ? $user['import'] : FALSE); return TRUE; } + + public function _do_activate($activation_code) + { + $user_id = $this->input->post('user-id'); + if ($user_id === FALSE) + return FALSE; + $user_id = intval($user_id); + + return $this->users_model->activate_account($user_id, + $activation_code); + } + + public function _do_resend_activation($email) + { + return FALSE; + } } /* End of file user.php */ diff --git a/application/language/english/form_validation_lang.php b/application/language/english/form_validation_lang.php index 95d38d8..ce86d15 100644 --- a/application/language/english/form_validation_lang.php +++ b/application/language/english/form_validation_lang.php @@ -1,15 +1,22 @@ activation e-mail on your e-mail address you provided. In order to confirm that you are the owner of that e-mail address, follow the link there or enter the activation code also provided in that e-mail in the field below.'; +$lang['user_activation_code'] = 'Activation Code'; +$lang['user_submit_activate'] = 'Activate Account'; +$lang['user_legend_resend_activation'] = 'Resend the activation code'; +$lang['user_instruction_resend_activation'] = 'The activation e-mail can take up to a few minutes until it arrives in your inbox, so please be patient. If you entered a wrong e-mail address, you can change it by entering another one in the field below. Pressing Resend will cause us to retransmit you the activation e-mail on the address entered in this field.'; +$lang['user_submit_resend_activation'] = 'Resend Activation E-mail'; +$lang['user_msg_activated_account'] = 'Your account is active so you can login. You successfully validated your e-mail address through the activation e-mail.'; + $lang['user_no_videos_uploaded'] = 'The user uploaded no videos.'; $lang['user_role_standard'] = 'Standard User'; diff --git a/application/models/users_model.php b/application/models/users_model.php index 862229c..6faa6fa 100644 --- a/application/models/users_model.php +++ b/application/models/users_model.php @@ -277,15 +277,22 @@ class Users_model extends CI_Model { * id, if it is string it is used as an username. * * @param mixed $user + * @param string $table_cols (optional) string with comma separated + * `users` table column names. Use a.activation_code to check user's + * account activation_code. If this value is NULL than the account is + * active. + * @return array associative array with userdata from DB */ - public function get_userdata($user) + public function get_userdata($user, $table_cols = '*') { if (is_int($user)) $cond = "id = $user"; else $cond = "username = '$user'"; - $query = $this->db->query("SELECT * from `users` + $query = $this->db->query("SELECT $table_cols + FROM `users` u LEFT JOIN `users_unactivated` a + ON (u.id = a.user_id) WHERE $cond"); if ($query->num_rows() === 0) diff --git a/application/models/videos_model.php b/application/models/videos_model.php index 6a06e1c..0250038 100644 --- a/application/models/videos_model.php +++ b/application/models/videos_model.php @@ -44,7 +44,7 @@ class Videos_model extends CI_Model { *
  • id, name, title, duration, thumbs_count, default_thumb, views => from DB
  • *
  • shorted_title => ellipsized title
  • *
  • video_url => P2P-Tube video URl
  • - *
  • TODO: user_id, user_name
  • + *
  • user_id, user_name
  • *
  • thumbs => thumbnail images' URLs
  • * */ @@ -179,7 +179,7 @@ class Videos_model extends CI_Model { * video asset having keys: "src", "res", "par" and "ext". Value of key * "src" is the video torrent formated as * {name}_{format}.{video_ext}.{default_torrent_ext} - *
  • user_name => TODO: user name from `users` table
  • + *
  • username => user name from `users` table
  • *
  • category_title => a human-friendly category name
  • *
  • tags => associative list of "tag => score"
  • *
  • date => date and time when the video was created
  • @@ -190,9 +190,9 @@ class Videos_model extends CI_Model { { $this->load->helper('video'); - $query = $this->db->query('SELECT * - FROM `videos` - WHERE id = ?', $id); + $query = $this->db->query("SELECT v.*, u.username + FROM `videos` v, `users` u + WHERE v.user_id = u.id AND v.id = $id"); $video = array(); if ($query->num_rows() > 0) @@ -239,9 +239,6 @@ class Videos_model extends CI_Model { // Thumbnails $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']); - // TODO: user information - $video['user_name'] = 'TODO'; - return $video; } diff --git a/application/views/header.php b/application/views/header.php index 0988aaf..8a07dd5 100644 --- a/application/views/header.php +++ b/application/views/header.php @@ -64,7 +64,7 @@ - diff --git a/application/views/user/activate_view.php b/application/views/user/activate_view.php new file mode 100644 index 0000000..2cc994f --- /dev/null +++ b/application/views/user/activate_view.php @@ -0,0 +1,33 @@ + +
    +lang->line('user_legend_activation') ?> + +

    lang->line('user_instruction_activation') ?>

    + + + + + + + + +

    +
    + + + +
    +lang->line('user_legend_resend_activation') ?> + +

    lang->line('user_instruction_resend_activation') ?>

    + + + + + + + + +

    +
    + \ No newline at end of file diff --git a/application/views/user/activated_account_view.php b/application/views/user/activated_account_view.php new file mode 100644 index 0000000..9049a54 --- /dev/null +++ b/application/views/user/activated_account_view.php @@ -0,0 +1,2 @@ +

    lang->line('user_msg_activated_account'), + site_url('user/login')) ?>

    \ No newline at end of file diff --git a/application/views/video/ns-html5_plugin_view.php b/application/views/video/ns-html5_plugin_view.php index 4275b85..83ad390 100644 --- a/application/views/video/ns-html5_plugin_view.php +++ b/application/views/video/ns-html5_plugin_view.php @@ -1,3 +1,5 @@ + +