account activation almost ready
authorCalin Burloiu <calin.burloiu@gmail.com>
Fri, 23 Sep 2011 15:08:00 +0000 (18:08 +0300)
committerCalin Burloiu <calin.burloiu@gmail.com>
Fri, 23 Sep 2011 15:08:00 +0000 (18:08 +0300)
13 files changed:
application/config/form_validation.php
application/controllers/user.php
application/language/english/form_validation_lang.php
application/language/english/user_lang.php
application/models/users_model.php
application/models/videos_model.php
application/views/header.php
application/views/user/activate_view.php [new file with mode: 0644]
application/views/user/activated_account_view.php [new file with mode: 0644]
application/views/video/ns-html5_plugin_view.php
application/views/video/ns-vlc_plugin_view.php
application/views/video/watch_view.php
css/default.css

index 1e7c787..55e0212 100644 (file)
@@ -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'
+               )
        )
 );
 
index 793d7f4..8c43cce 100644 (file)
@@ -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('<span class="error">',
+                                       '</span>');
+               
+               $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')
+                                       .' &ndash; '
+                                       . $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 */
index 95d38d8..ce86d15 100644 (file)
@@ -1,15 +1,22 @@
 <?php
 
+// Merge with standard language entries.
 include('system/language/english/form_validation_lang.php');
 
+// Login / Register/ Account / Profile
 $lang['_valid_username']                       = 'You must enter a valid username with letters, numbers, . (dots) or _ (underscores).';
 $lang['_valid_username_or_email']      = "You must enter an e-mail address or a valid username.";
-$lang['_do_login']                                     = "Wrong %s, or wrong %s.";
-$lang['_valid_date']                           = "Invalid %s! Use the specified format or leave the field blank if you don't want to specify it.";
-$lang['_valid_old_password']           = "Wrong %s.";
+$lang['_do_login']                                     = 'Wrong %s, or wrong %s.';
+$lang['_valid_date']                           = 'Invalid %s! Use the specified format or leave the field blank if you don\'t want to specify it.';
+$lang['_valid_old_password']           = 'Wrong %s.';
 $lang['_change_password_cond']         = 'If you want to change your password complete all three password related fields.';
 $lang['_required_by_register']         = 'The %s field is required.';
 
+// Account Activation
+$lang['_valid_activation_code']                = 'Invalid activation code. You must provide 16 hexa characters.';
+$lang['_do_activate']                          = 'Wrong activation code.';
+$lang['_do_resend_activation']         = 'An error occurred while resending your activation e-mail. This is not a permanent error. Please try again later.';
+
 
 /* End of file form_validation_lang.php */
 /* Location: ./system/language/english/form_validation_lang.php */
\ No newline at end of file
index 84cdd79..03ca5f2 100644 (file)
@@ -2,6 +2,7 @@
 
 $lang['user_appelation'] = 'User';
 
+// Login / Register/ Account / Profile
 $lang['user_username'] = 'User Name';
 $lang['user_username_or_email'] = 'User Name or E-mail';
 $lang['user_password'] = 'Password';
@@ -28,6 +29,17 @@ $lang['user_note_required_fields'] = '* Required fields!';
 $lang['user_submit_register'] = 'Register';
 $lang['user_submit_save'] = 'Save';
 
+// Account Activation
+$lang['user_title_activation'] = 'Account Activation';
+$lang['user_legend_activation'] = 'Activate your account';
+$lang['user_instruction_activation'] = 'Shortly after the registration you made you will receive an <em>activation e-mail</em> 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 <em>activation code</em> 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 <em>activation e-mail</em> 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 <em>Resend</em> will cause us to retransmit you the <em>activation e-mail</em> 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 <a href="%s">login</a>. You successfully validated your e-mail address through the <em>activation e-mail</em>.';
+
 $lang['user_no_videos_uploaded'] = 'The user uploaded no videos.';
 
 $lang['user_role_standard'] = 'Standard User';
index 862229c..6faa6fa 100644 (file)
@@ -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)
index 6a06e1c..0250038 100644 (file)
@@ -44,7 +44,7 @@ class Videos_model extends CI_Model {
         *   <li>id, name, title, duration, thumbs_count, default_thumb, views => from DB</li>
         *   <li>shorted_title => ellipsized title</li>
         *   <li>video_url => P2P-Tube video URl</li>
-        *   <li>TODO: user_id, user_name</li>
+        *   <li>user_id, user_name</li>
         *   <li>thumbs => thumbnail images' URLs</li>
         * </ul>
         */
@@ -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}</li>
-        *   <li>user_name => TODO: user name from `users` table</li>
+        *   <li>username => user name from `users` table</li>
         *   <li>category_title => a human-friendly category name</li>
         *   <li>tags => associative list of "tag => score"</li>
         *   <li>date => date and time when the video was created</li>
@@ -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;
        }
        
index 0988aaf..8a07dd5 100644 (file)
@@ -64,7 +64,7 @@
        </li> 
        
        <?php else: ?>
-       <li class="menu-right"><a href="<?php echo site_url('user/register/'. urlencode_segments(uri_string(), 'user/register')) ?>"
+       <li class="menu-right"><a href="<?php echo site_url('user/register') ?>"
                <?php echo ($selected_menu == 'register' ? 'class="selected"' : '') ?>><?php echo $this->lang->line('ui_nav_menu_register') ?></a>
        </li>
                
diff --git a/application/views/user/activate_view.php b/application/views/user/activate_view.php
new file mode 100644 (file)
index 0000000..2cc994f
--- /dev/null
@@ -0,0 +1,33 @@
+<?php echo form_open("user/activate/$user_id/code") ?>
+<fieldset>
+<legend><?php echo $this->lang->line('user_legend_activation') ?></legend>
+
+<p><?php echo $this->lang->line('user_instruction_activation') ?></p>
+
+<input type="hidden" name="user-id" value="<?php echo $user_id ?>" />
+
+<label for="activation-code" class="strong"><?php echo $this->lang->line('user_activation_code') ?>:</label>
+<input type="text" name="activation-code" id="activation-code" value="<?php echo set_value('activation-code') ?>" size="24" />
+
+<input type="submit" value="<?php echo $this->lang->line('user_submit_activate') ?>" />
+
+<p><?php echo form_error('activation-code') ?></p>
+</fieldset>
+</form>
+
+<?php echo form_open("user/activate/$user_id/resend") ?>
+<fieldset>
+<legend><?php echo $this->lang->line('user_legend_resend_activation') ?></legend>
+
+<p><?php echo $this->lang->line('user_instruction_resend_activation') ?></p>
+
+<input type="hidden" name="user-id" value="<?php echo $user_id ?>" />
+
+<label for="email" class="strong"><?php echo $this->lang->line('user_email') ?>:</label>
+<input type="text" name="email" id="email" value="<?php echo set_value('email', $email) ?>" size="24" />
+
+<input type="submit" value="<?php echo $this->lang->line('user_submit_resend_activation') ?>" />
+
+<p><?php echo form_error('email') ?></p>
+</fieldset>
+</form>
\ 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 (file)
index 0000000..9049a54
--- /dev/null
@@ -0,0 +1,2 @@
+<p><?php echo sprintf($this->lang->line('user_msg_activated_account'), 
+               site_url('user/login')) ?></p>
\ No newline at end of file
index 4275b85..83ad390 100644 (file)
@@ -1,3 +1,5 @@
+<!-- OBSOLETE -->
+
 <video controls="controls" width="800" height="600" preload="auto">
        <source src="<?php echo $url ?>" type="video/ogg" />
     Error: Your browser does not support HTML5 or the video format!
index adcd415..5fe1aec 100644 (file)
@@ -1,3 +1,5 @@
+<!-- OBSOLETE -->
+
 <div id="vlc_container">No VLC</div>
 <table id="nsTable">
        <tr><td id="nsPlaybackCell"><input type=button value="Play" onClick="play();" />
index 11662fc..7486a78 100644 (file)
@@ -37,7 +37,7 @@
        <div id="video-info" style="clear: both">
        <div id="video-upload-info">
                <?php echo $this->lang->line('ui_uploaded_by') ?>
-                       <span id="video-date"><?php echo $video['user_name'] ?></span>
+                       <span id="video-date"><a href="<?php echo site_url("user/profile/{$video['username']}") ?>"><?php echo $video['username'] ?></a></span>
                        <?php echo $this->lang->line('ui_on_date') ?>
                        <span id="video-date"><?php echo $video['date'] ?></span>
        </div>
index 16073af..4448c97 100644 (file)
@@ -37,6 +37,16 @@ table.form td
        vertical-align: top;
 }
 
+fieldset
+{
+       margin-bottom: 1em;
+}
+
+label.strong
+{
+       font-weight: bold;
+}
+
 .error
 {
        color: red;