| These modes are used when working with fopen()/popen()
|
*/
-
define('FOPEN_READ', 'rb');
define('FOPEN_READ_WRITE', 'r+b');
define('FOPEN_WRITE_CREATE_DESTRUCTIVE', 'wb'); // truncates existing file data, use with care
define('FOPEN_WRITE_CREATE_STRICT', 'xb');
define('FOPEN_READ_WRITE_CREATE_STRICT', 'x+b');
+/*
+|--------------------------------------------------------------------------
+| User Roles
+|--------------------------------------------------------------------------
+|
+| Define bits significance in user roles bit mask.
+|
+*/
+define('USER_ROLE_STANDARD', 0);
+define('USER_ROLE_ADMIN', 1);
/* End of file constants.php */
/* Location: ./application/config/constants.php */
\ No newline at end of file
{
// Videos
$vs_data['videos'] = $this->videos_model->get_videos_summary(
- $id, 0, $this->config->item('videos_per_row'));
+ $id, NULL, 0, $this->config->item('videos_per_row'));
// Category
$vs_data['category_name'] = $name;
- $vs_data['category_title'] = $name ?
- $this->lang->line("ui_categ_$name") : $name;
$vs_data['category_id'] = $id;
+ $videos_summary['category_name'] = $name;
+ $videos_summary['category_title'] = $name ?
+ $this->lang->line("ui_categ_$name") : $name;
// Pagination (not required)
$vs_data['pagination'] = '';
- $data['videos_summaries'][] =
- $this->load->view('catalog/videos_summary_view',
- $vs_data, TRUE);
+ $videos_summary['content'] = $this->load->view(
+ 'catalog/videos_summary_view', $vs_data, TRUE);
+ $data['videos_summaries'][] = $videos_summary;
}
$params = array( 'title' => $this->config->item('site_name'),
public function test($page = 0)
{
- echo $this->uri->segment(1);
+ $this->load->model('users_model');
+ echo Users_model::gen_activation_code('calin.burloiu');
}
public function category($category_name, $ordering = 'hottest', $offset = 0)
// ** LOADING MODEL
// **
// Video Category
- $vs_data = $this->_get_category_data($category_name);
+ $category_data = Catalog::_get_category_data($category_name);
// Retrieve videos summary.
$this->load->model('videos_model');
$vs_data['videos'] = $this->videos_model->get_videos_summary(
- $vs_data['category_id'], intval($offset),
+ $category_data['category_id'], NULL, intval($offset),
$this->config->item('videos_per_page'), $ordering);
$vs_data['ordering'] = $ordering;
$pg_config['base_url'] = site_url("catalog/category/$category_name/$ordering/");
$pg_config['uri_segment'] = 5;
$pg_config['total_rows'] = $this->videos_model->get_videos_count(
- $vs_data['category_id']);
+ $category_data['category_id']);
$pg_config['per_page'] = $this->config->item('videos_per_page');
$this->pagination->initialize($pg_config);
$vs_data['pagination'] = $this->pagination->create_links();
-
- // Video Summary
-// $data['video_summary'] = $this->load->view('catalog/videos_summary_view',
-// $vs_data, TRUE);
+ $vs_data['category_name'] = $category_data['category_name'];
+ $vs_data['title'] = $category_data['category_title'];
$params = array( 'title' =>
- $vs_data['category_title'].' – '
+ $category_data['category_title'].' – '
. $this->config->item('site_name'),
'css' => array(
'catalog.css'
- ),
- //'js' => array(),
+ )
//'metas' => array('description'=>'','keywords'=>'')
);
$this->load->library('html_head_params', $params);
'search_category_name'=> $vs_data['category_name']
));
-// $main_params['content'] = $this->load->view('catalog/category_view', $data, TRUE);
$main_params['content'] =
$this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
$main_params['side'] = $this->load->view('side_default', NULL, TRUE);
$results_data['search_query'] = $search_query;
// Category
- $results_data = $this->_get_category_data($category_name);
+ $results_data = Catalog::_get_category_data($category_name);
if ($results_data === NULL)
$results_data = array('category_id'=>NULL);
$this->load->view('html_end');
}
- public function _get_category_data($category_name)
+ public static function _get_category_data($category_name)
{
+ $ci =& get_instance();
+
if ($category_name === NULL)
return NULL;
- $categories = $this->config->item('categories');
+ $categories = $ci->config->item('categories');
$category_id = array_search($category_name, $categories);
$results_data['category_name'] = $category_name;
$results_data['category_id'] = $category_id;
$results_data['category_title'] = $category_name ?
- $this->lang->line("ui_categ_$category_name") : $category_name;
+ $ci->lang->line("ui_categ_$category_name") : $category_name;
return $results_data;
}
class User extends CI_Controller {
private $import = FALSE;
+ private $activated_account = TRUE;
public function __construct()
{
}
else
{
- if (! $this->import)
+ if (! $this->activated_account)
+ header('Location: '. site_url('catalog/test'));
+ else if (! $this->import)
{
// Redirect to last page before login.
header('Location: '. site_url(urldecode_segments($redirect)));
{
$this->session->unset_userdata('user_id');
$this->session->unset_userdata('username');
+ $this->session->unset_userdata('auth_src');
+ $this->session->unset_userdata('time_zone');
header('Location: '. site_url(urldecode_segments($redirect)));
}
// Edit account data if logged in, otherwise register.
if ($user_id = $this->session->userdata('user_id'))
{
- $userdata = $this->users_model->get_userdata($user_id);
+ $userdata = $this->users_model->get_userdata(intval($user_id));
+ $selected_menu = 'account';
}
else
{
$userdata = FALSE;
+ $selected_menu = 'register';
}
$params = array('title' =>
// ** LOADING VIEWS
// **
$this->load->view('html_begin', $this->html_head_params);
- $this->load->view('header', array('selected_menu' => 'register'));
+ $this->load->view('header',
+ array('selected_menu' => $selected_menu));
$main_params['content'] = $this->load->view('user/register_view',
array('userdata'=> $userdata, 'redirect'=> $redirect),
$data['ui_lang'] = $this->input->post('ui-lang');
$data['time_zone'] = $this->input->post('time-zone');
+ // Update session user data.
+ $this->_update_session_userdata($data);
+
// Edit account data
if ($user_id)
{
$this->register($redirect);
}
+ public function profile($username, $videos_offset = 0)
+ {
+ // TODO handle user not found
+
+ $this->load->config('localization');
+ $this->load->helper('date');
+ $this->lang->load('date');
+
+ // **
+ // ** LOADING MODEL
+ // **
+ // Logged in user time zone
+ $time_zone = $this->session->userdata('time_zone');
+ if (! $time_zone)
+ $time_zone = 'UTC';
+
+ // User data
+ $userdata = $this->users_model->get_userdata($username);
+ $userdata['roles'] = Users_model::roles_to_string($userdata['roles']);
+ $country_list = $this->config->item('country_list');
+ $userdata['country_name'] = $country_list[ $userdata['country'] ];
+ $userdata['last_login'] = date('Y-m-d H:i:s',
+ gmt_to_local(
+ strtotime($userdata['last_login']),
+ $time_zone,
+ TRUE)) . ($time_zone == 'UTC' ? ' (UTC)' : '');
+ $userdata['time_zone'] = $this->lang->line($userdata['time_zone']);
+
+ // User's videos
+ $this->load->model('videos_model');
+ $vs_data['videos'] = $this->videos_model->get_videos_summary(
+ NULL, $username, intval($videos_offset),
+ $this->config->item('videos_per_page'));
+
+ // Pagination
+ $this->load->library('pagination');
+ $pg_config['base_url'] = site_url("user/profile/$username/");
+ $pg_config['uri_segment'] = 4;
+ $pg_config['total_rows'] = $this->videos_model->get_videos_count(
+ NULL, $username);
+ $pg_config['per_page'] = $this->config->item('videos_per_page');
+ $this->pagination->initialize($pg_config);
+ $vs_data['pagination'] = $this->pagination->create_links();
+ $vs_data['title'] = NULL;
+ $vs_data['category_name'] = ''; // TODO videos_summary with AJAX
+
+ $params = array(
+ 'title'=> $this->lang->line('user_appelation').' '.$username
+ .' – '
+ . $this->config->item('site_name'),
+ 'css'=> array('catalog.css')
+ //'metas' => array('description'=>'')
+ );
+ $this->load->library('html_head_params', $params);
+
+ // Current user profile tab
+ $tab = (! $videos_offset ? 0 : 1);
+
+ // **
+ // ** LOADING VIEWS
+ // **
+ $this->load->view('html_begin', $this->html_head_params);
+ $this->load->view('header', array());
+
+ $vs = $this->load->view('catalog/videos_summary_view', $vs_data, TRUE);
+
+ $main_params['content'] = $this->load->view('user/profile_view',
+ array('userdata'=> $userdata, 'videos_summary'=> $vs, 'tab'=>$tab),
+ 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');
+ }
+
+ public function activate($user_id, $activation_code)
+ {
+ $user_id = intval($user_id);
+ echo ''. $this->users_model->activate_account($user_id, $activation_code);
+ }
+
+ public function _update_session_userdata($data)
+ {
+ foreach ($data as $key=> $val)
+ $this->session->set_userdata($key, $val);
+ }
+
public function _valid_username($username)
{
return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
if ($user === FALSE)
return FALSE;
+ // User has not activated the account.
+ if ($user['activation_code'] !== NULL)
+ {
+ $this->activated_account = FALSE;
+ return TRUE;
+ }
+
// Authentication successful: set session with user data.
$this->session->set_userdata(array(
'user_id'=> $user['id'],
'username'=> $user['username'],
- 'auth_src'=> $user['auth_src']
+ 'auth_src'=> $user['auth_src'],
+ 'time_zone'=> $user['time_zone']
));
- $this->import = $user['import'];
+ $this->import = (isset($user['import']) ? $user['import'] : FALSE);
return TRUE;
}
}
<?php
+$lang['user_appelation'] = 'User';
+
$lang['user_username'] = 'User Name';
$lang['user_username_or_email'] = 'User Name or E-mail';
$lang['user_password'] = 'Password';
$lang['user_locality'] = 'City / Town';
$lang['user_ui_lang'] = 'Site Interface Language';
$lang['user_time_zone'] = 'Time Zone';
-$lang['user_role'] = 'Roles';
+$lang['user_roles'] = 'Roles';
$lang['user_auth_src'] = 'Authentication Source';
$lang['user_video_prefs'] = 'Video Preferences';
+$lang['user_registration_date'] = 'Registration Date';
+$lang['user_last_login'] = 'Last Login Time';
+
$lang['user_note_required_fields'] = '* Required fields!';
$lang['user_submit_register'] = 'Register';
$lang['user_submit_save'] = 'Save';
+$lang['user_no_videos_uploaded'] = 'The user uploaded no videos.';
+
+$lang['user_role_standard'] = 'Standard User';
+$lang['user_role_admin'] = 'Administrator';
+
/* End of file user_lang.php */
/* Location: ./application/language/english/user_lang.php */
\ No newline at end of file
$enc_password = sha1($password);
// TODO select only required fields.
- $query = $this->db->query("SELECT * FROM `users`
+ $query = $this->db->query("SELECT u.*, a.activation_code
+ FROM `users` u LEFT JOIN `users_unactivated` a ON (u.id = a.user_id)
WHERE $cond_user
AND (auth_src = 'ldap' OR password = '$enc_password')");
// It is possible that the user has a LDAP account but he's
// authenticating here for the first time so it does not have an entry
// in `users` table.
- if ($query->num_rows() !== 1)
+ if ($query->num_rows() === 0)
{
$ldap_userdata = $this->ldap_login($username, $password);
+ if ($ldap_userdata === FALSE)
+ return FALSE;
$userdata = $this->convert_ldap_userdata($ldap_userdata);
$this->register($userdata);
$user = $this->login($username, $password);
$user['import'] = TRUE;
return $user;
-
- /* foreach ($ldap_userdata as $k => $v)
- {
- echo "<h1>$k</h1>";
- print_r($v);
- }
- die(); */
}
$user = $query->row_array();
// Authenticate with LDAP.
- if ($user['auth_src'] == 'ldap')
- return ($this->ldap_login($username, $password) !== FALSE
- ? $user : FALSE);
+ if ($user['auth_src'] == 'ldap'
+ && ! $this->ldap_login($username, $password))
+ return FALSE;
+
+ // Update last login time.
+ $this->db->query("UPDATE `users`
+ SET last_login = UTC_TIMESTAMP()
+ WHERE username = '$username'");
// If we are here internal authentication has successful.
return $user;
/**
* Adds a new user to DB.
+ * Do not add join_date and last_login column, they will be automatically
+ * added.
*
* @param array $data corresponds to DB columns
*/
$vals = substr($vals, 0, -2);
$query = $this->db->query("INSERT INTO `users`
- ($cols)
- VALUES ($vals)");
+ ($cols, registration_date, last_login)
+ VALUES ($vals, utc_timestamp(), utc_timestamp())");
+
+ if ($query === FALSE)
+ return FALSE;
+
+ // If the registered with internal authentication it needs to activate
+ // the account.
+ $activation_code = Users_model::gen_activation_code();
+ $user_id = $this->get_user_id($data['username']);
+ $query = $this->db->query("INSERT INTO `users_unactivated`
+ (user_id, activation_code)
+ VALUES ($user_id, '$activation_code')");
// TODO exception on failure
return $query;
}
+ public function get_user_id($username)
+ {
+ $query = $this->db->query("SELECT id FROM `users`
+ WHERE username = '$username'");
+
+ if ($query->num_rows() === 0)
+ return FALSE;
+
+ return $query->row()->id;
+ }
+
+ // TODO cleanup account activation
+ public function cleanup_account_activation()
+ {
+
+ }
+
/**
- * Returns data from `users` table for user with $user_id.
+ * Activated an account for an user having $user_id with $activation_code.
*
* @param int $user_id
+ * @param string $activation_code hexa 16 characters string
+ * @return returns TRUE if activation was successful and FALSE otherwise
+ */
+ public function activate_account($user_id, $activation_code)
+ {
+ $query = $this->db->query("SELECT * FROM `users_unactivated`
+ WHERE user_id = $user_id
+ AND activation_code = '$activation_code'");
+
+ if ($query->num_rows() === 0)
+ return FALSE;
+
+ $this->db->query("DELETE FROM `users_unactivated`
+ WHERE user_id = $user_id");
+
+ return TRUE;
+ }
+
+ /**
+ * Returns data from `users` table. If $user is int it is used as an
+ * id, if it is string it is used as an username.
+ *
+ * @param mixed $user
*/
- public function get_userdata($user_id)
+ public function get_userdata($user)
{
+ if (is_int($user))
+ $cond = "id = $user";
+ else
+ $cond = "username = '$user'";
+
$query = $this->db->query("SELECT * from `users`
- WHERE id = $user_id");
+ WHERE $cond");
if ($query->num_rows() === 0)
return FALSE;
// TODO exception
return $query;
}
+
+ public static function gen_activation_code($str = '')
+ {
+ $ci =& get_instance();
+
+ $activation_code = substr(
+ sha1(''. $str. $ci->config->item('encryption_key')
+ . mt_rand()),
+ 0,
+ 16);
+
+ return $activation_code;
+ }
+
+ public static function roles_to_string($roles)
+ {
+ $ci =& get_instance();
+ $ci->lang->load('user');
+
+ if ($roles == USER_ROLE_STANDARD)
+ return $ci->lang->line('user_role_standard');
+ else
+ {
+ $str_roles = '';
+
+ if ($roles & USER_ROLE_ADMIN)
+ $str_roles .= $ci->lang->line('user_role_admin') . '; ';
+ }
+
+ return $str_roles;
+ }
}
/* End of file users_model.php */
}
/**
- * Retrieves information about a set of videos which are going to be
- * displayed in the catalog.
+ * Retrieves a set of videos information which can be used for displaying
+ * that videos as a list with few details.
*
- * @param int $category_id DB category ID
+ * @param int $category_id DB category ID; pass NULL for all
+ * categories
+ * @param mixed $user an user_id (as int) or an username
+ * (as string); pass NULL for all users
* @param int $offset
* @param int $count
* @param string $ordering control videos ording by these
* <li>thumbs => thumbnail images' URLs</li>
* </ul>
*/
- public function get_videos_summary($category_id, $offset, $count, $ordering = 'hottest')
+ public function get_videos_summary($category_id, $user, $offset, $count,
+ $ordering = 'hottest')
{
$this->load->helper('text');
$order_statement = "";
}
+ // Category filtering
+ if ($category_id === NULL)
+ $cond_category = "1";
+ else
+ {
+ $category_id = intval($category_id);
+ $cond_category = "category_id = $category_id";
+ }
+
+ // User filtering
+ if ($user === NULL)
+ $cond_user = "1";
+ else
+ {
+ if (is_int($user))
+ $cond_user = "v.user_id = $user";
+ else if (is_string($user))
+ $cond_user = "u.username = '$user'";
+ }
+
$query = $this->db->query(
- "SELECT id, name, title, duration, user_id, views, thumbs_count,
- default_thumb, (views + likes - dislikes) AS score
- FROM `videos`
- WHERE category_id = ?
+ "SELECT v.id, name, title, duration, user_id, u.username, views,
+ thumbs_count, default_thumb,
+ (views + likes - dislikes) AS score
+ FROM `videos` v, `users` u
+ WHERE v.user_id = u.id AND $cond_category AND $cond_user
$order_statement
- LIMIT ?, ?",
- array(intval($category_id), $offset, $count));
+ LIMIT $offset, $count");
if ($query->num_rows() > 0)
$videos = $query->result_array();
else
- return NULL;
+ return array();
foreach ($videos as & $video)
{
// Ellipsized title
//$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
$video['shorted_title'] = character_limiter($video['title'], 50);
-
- // TODO: user information
- $video['user_name'] = 'TODO';
}
return $videos;
}
- public function get_videos_count($category_id)
+ /**
+ * Returns the number of videos from database from a specific category or
+ * user.
+ * NULL parameters count videos from all categories and / or all users.
+ *
+ * @param int $category_id
+ * @param mixed $user an user_id (as int) or an username (as string)
+ * @return int number of videos or NULL if an error occured
+ */
+ public function get_videos_count($category_id = NULL, $user = NULL)
{
+ if ($category_id === NULL)
+ $cond_category = "1";
+ else
+ $cond_category = "category_id = $category_id";
+
+ if ($user === NULL)
+ $cond_user = "1";
+ else
+ {
+ if (is_int($user))
+ $cond_user = "v.user_id = $user";
+ else if(is_string($user))
+ $cond_user = "u.username = '$user'";
+ }
+
$query = $this->db->query(
- 'SELECT COUNT(*) count
- FROM `videos`
- WHERE category_id = ?',
- $category_id);
+ "SELECT COUNT(*) count
+ FROM `videos` v, `users` u
+ WHERE v.user_id = u.id AND $cond_category AND $cond_user");
if ($query->num_rows() > 0)
return $query->row()->count;
+++ /dev/null
-<?php echo $video_summary ?>
\ No newline at end of file
-<?php foreach ($videos_summaries as $videos_summary):
- echo $videos_summary;
-endforeach ?>
\ No newline at end of file
+<?php foreach ($videos_summaries as $videos_summary): ?>
+<h1 class="category-title">
+ <a href="<?php echo site_url("catalog/category/{$videos_summary['category_name']}") ?>">
+ <?php echo $videos_summary['category_title'] ?>
+ </a>
+</h1>
+<?php echo $videos_summary['content']; ?>
+<?php endforeach ?>
\ No newline at end of file
<div class="videos-summary">
- <h1 class="category-title">
- <a href="<?php echo site_url("catalog/category/$category_name") ?>">
- <?php echo $category_title ?>
- </a>
+ <?php if (isset($title) && $title): ?>
+ <h1>
+ <?php echo $title ?>
</h1>
+ <?php endif ?>
<?php
if ( isset($ordering))
<?php echo $pagination ?>
+ <?php if (count($videos) === 0): ?>
+ <p><?php echo $this->lang->line('user_no_videos_uploaded') ?></p>
+ <?php else: ?>
<?php foreach($videos as $video):
$thumb_src = $video['thumbs'][ $video['default_thumb'] ];
?>
?>
</div>
<div class="video-username">
- <?php echo $this->lang->line('ui_from') . ' TODO' //TODO ?>
+ <?php echo $this->lang->line('ui_from') ?> <a href="<?php echo site_url("user/profile/{$video['username']}") ?>"><?php echo $video['username'] ?></a>
</div>
</div>
<?php endforeach ?>
+ <?php endif ?>
<?php echo $pagination ?>
</div>
+<?php // TODO change ordering via AJAX ?>
<script type="text/javascript">
$(function() {
$('#ordering').change(function(e) {
--- /dev/null
+<?php
+ if (! isset($tab))
+ $tab = 0;
+?>
+<h1><?php echo $this->lang->line('user_appelation')
+ . ' <em>'. $userdata['username']. '</em>' ?></h1>
+<div id="profile-tabs">
+ <ul>
+ <li><a href="#tab-profile">Profile</a></li>
+ <li><a href="#tab-videos">Videos</a></li>
+ </ul>
+ <div id="tab-profile">
+ <table class="form">
+ <tr>
+ <th><?php echo $this->lang->line('user_username'). ': ' ?></th>
+ <td><?php echo $userdata['username'] ?></td>
+ </tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_roles'). ': ' ?></th>
+ <td><?php echo $userdata['roles'] ?></td>
+ </tr>
+
+ <tr><td></td><td> </td></tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_first_name'). ': ' ?></th>
+ <td><?php echo $userdata['first_name'] ?></td>
+ </tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_last_name'). ': ' ?></th>
+ <td><?php echo $userdata['last_name'] ?></td>
+ </tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_birth_date'). ': ' ?></th>
+ <td><?php echo $userdata['birth_date'] ?></td>
+ </tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_country'). ': ' ?></th>
+ <td><?php echo $userdata['country_name'] ?></td>
+ </tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_locality'). ' : ' ?></th>
+ <td><?php echo $userdata['locality'] ?></td>
+ </tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_time_zone'). ' : ' ?></th>
+ <td><?php echo $userdata['time_zone'] ?></td>
+ </tr>
+
+ <tr><td></td><td> </td></tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_registration_date'). ' : ' ?></th>
+ <td><?php echo $userdata['registration_date'] ?></td>
+ </tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_last_login'). ' : ' ?></th>
+ <td><?php echo $userdata['last_login'] ?></td>
+ </tr>
+ </table>
+ </div>
+ <div id="tab-videos">
+ <?php echo $videos_summary ?>
+ </div>
+</div>
+
+<script type="text/javascript">
+ $(function() {
+ $('#profile-tabs').tabs({
+ "selected": <?php echo $tab ?>
+ });
+ });
+
+</script>
\ No newline at end of file
<?php else: ?>
<th><?php echo $this->lang->line('user_username'). ': ' ?></th>
<td>
- <strong><?php echo $userdata['username'] ?></strong>
+ <em><?php echo $userdata['username'] ?></em>
</td>`
<?php endif ?>
</tr>
margin-left: 8px;
}
-.category-title
-{
- /* border-bottom: 1px solid rgb(108,162,222); */
-}
-
.video-icon
{
position: relative;
float: left;
width: 146px;
height: 170px;
- margin-right: 4px;
+ padding-right: 4px;
}
.video-icon .video-thumb
{
margin-top: 2px;
font-size: 1em;
+ text-align: left;
line-height: 100%;
}
.video-icon .video-views
}
table.form td
{
+ padding-left: 0.5em;
vertical-align: top;
}