Файл: adultscript-2.0.3-pro/files/admin/modules/user/components/add.php
Строк: 107
<?php
defined('_VALID') or die('Restricted Access!');
class VComponent_Admin_user_add extends VModule_Admin_user
{
public function __construct()
{
parent::__construct();
}
public function render()
{
$ucfg = VF::cfg('module.user');
$errors = array();
$messages = array();
$groups = $this->get_groups();
$user = array(
'username' => '', 'group_id' => '', 'email' => '', 'name' => '', 'status' => '1',
);
if (isset($_POST['submit_user_add'])) {
$filter = &VF::factory('filter');
$username = $filter->get('username');
$group_id = $filter->get('group', 'INTEGER');
$email = $filter->get('email');
$password = $_POST['password'];
$password_c = $_POST['password_confirm'];
$status = (int) trim($_POST['status']);
$name = $filter->get('name');
if ($username == '') {
$errors[] = 'Username field cannot be left blank!';
} elseif (!VValid::length($username, 3, 16)) {
$errors[] = 'Username can contain maximum 16 characters!';
} elseif (!VValid::aldash($username)) {
$errors[] = 'Username can contain only letters, numbers, dashes and underscores!';
} else {
$this->db->query("SELECT user_id FROM #__user WHERE username = '".$this->db->escape($username)."' LIMIT 1");
if ($this->db->affected_rows()) {
$errors[] = 'Username is already used for another account!';
} else {
$user['username'] = $username;
}
}
if ($group_id === 0) {
$errors[] = 'Please select a user group!';
} else {
$group_found = FALSE;
foreach ($groups as $group) {
if ($group['group_id'] == $group_id) {
$group_found = TRUE;
break;
}
}
if ($group_found === FALSE) {
$errors[] = 'Invalid user group! What exactly did you select!?';
} else {
$user['group_id'] = $group_id;
}
}
if ($email == '') {
$errors[] = 'Email field cannot be left blank!';
} elseif (!VValid::length($email, 5, 255)) {
$errors[] = 'Email can contain maximum 255 characters!';
} elseif (!VValid::email($email)) {
$errors[] = 'Email is not a valid email address!';
} else {
$this->db->query("SELECT user_id FROM #__user WHERE email = '".$this->db->escape($email)."' LIMIT 1");
if ($this->db->affected_rows()) {
$errors[] = 'Email is already used for another account!';
} else {
$user['email'] = $email;
}
}
if ($password == '' OR $password_c == '') {
$errors[] = 'Password or confirmation password are empty!';
} else {
if ($password != $password_c) {
$errors[] = 'Password and confirmation password do not match!';
} elseif (!VValid::length($password, $ucfg['pwd_min_length'])) {
$errors[] = 'Password must contain at least '.$ucfg['pwd_min_length'].' characters!';
}
}
if ($name != '') {
if (!VValid::length($name, 0, 99)) {
$errors[] = 'Name can contain maximum 99 characters!';
} else {
$user['name'] = $name;
}
}
if (!$errors) {
$this->db->query("SELECT user_id FROM #__user WHERE username = '".$username.$i."' LIMIT 1");
if ($this->db->affected_rows()) {
continue;
}
$this->db->query("INSERT INTO #__user
SET username = '".$this->db->escape($username)."',
password = '".$this->db->escape(VHash::encrypt($password))."',
group_id = ".$group_id.",
email = '".$this->db->escape($email)."',
name = '".$this->db->escape($name)."',
join_date = '".date('Y-m-d h:i:s')."',
join_ip = ".VServer::ip(TRUE).",
status = '".$status."'");
if ($this->db->affected_rows()) {
$user_id = $this->db->get_last_insert_id('#__user');
$this->db->query("INSERT INTO #__user_activity SET user_id = ".$user_id);
$this->db->query("INSERT INTO #__user_profile SET user_id = ".$user_id);
$this->db->query("INSERT INTO #__user_notifications SET user_id = ".$user_id);
$this->db->query("INSERT INTO #__user_preferences SET user_id = ".$user_id);
$messages[] = 'User added!';
} else {
$errors[] = 'Failed to add user!';
}
}
}
$tpl = &VF::factory('template');
$tpl->menu = 'user';
$tpl->submenu = 'user_add';
$tpl->meta_title = 'Admin::User::Add';
$tpl->errors = $errors;
$tpl->messages = $messages;
$tpl->ucfg = $ucfg;
$tpl->groups = $groups;
$tpl->user = $user;
$tpl->load(array('header', 'user_add', 'footer'));
$tpl->display();
}
private function get_groups()
{
$this->db->query("SELECT group_id, name FROM #__user_groups ORDER BY group_id ASC");
if ($this->db->affected_rows()) {
return $this->db->fetch_rows();
}
die('Failed to load the user groups table!');
}
}
?>