piler/webui/controller/user/add.php

147 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2012-02-08 23:14:28 +01:00
<?php
class ControllerUserAdd extends Controller {
private $error = array();
private $domains = array();
public function index(){
$this->id = "content";
$this->template = "user/add.tpl";
$this->layout = "common/layout";
$request = Registry::get('request');
$db = Registry::get('db');
$this->load->model('user/user');
2012-06-22 12:30:55 +02:00
$this->load->model('group/group');
2012-09-06 15:27:20 +02:00
$this->load->model('folder/folder');
2012-02-08 23:14:28 +01:00
2012-06-22 15:22:02 +02:00
$this->document->title = $this->data['text_add_new_user_alias'];
2012-02-08 23:14:28 +01:00
$this->data['domains'] = array();
/* check if we are admin */
if(Registry::get('admin_user') == 1) {
/* query available domains */
$this->data['domains'] = $this->model_user_user->get_domains();
$this->domains = $this->model_user_user->get_email_domains();
if($this->request->server['REQUEST_METHOD'] == 'POST') {
$ret = 0;
if($this->validate() == true){
2012-06-22 15:22:02 +02:00
$ret = $this->model_user_user->add_user($this->request->post);
2012-02-08 23:14:28 +01:00
$_SESSION['last_domain'] = $this->request->post['domain'];
if($ret == 1){
$this->data['x'] = $this->data['text_successfully_added'];
2012-12-10 13:08:41 +01:00
$this->data['next_user_id'] = $this->model_user_user->get_next_uid();
2012-02-08 23:14:28 +01:00
} else {
$this->data['errorstring'] = $this->data['text_failed_to_add'] . ": " . $ret;
}
}
else {
$this->data['errorstring'] = $this->data['text_error_message'];
$this->data['errors'] = $this->error;
2012-02-08 23:14:28 +01:00
}
if($ret == 0) {
$this->data['post'] = $this->request->post;
2012-06-22 15:22:02 +02:00
$this->data['next_user_id'] = $this->model_user_user->get_next_uid();
2012-02-08 23:14:28 +01:00
}
}
else {
2012-06-22 15:22:02 +02:00
$this->data['next_user_id'] = $this->model_user_user->get_next_uid();
// not sure these are needed
2012-06-22 12:30:55 +02:00
$this->data['groups'] = $this->model_group_group->get_groups();
2012-09-06 15:27:20 +02:00
$this->data['folders'] = $this->model_folder_folder->get_folders();
2012-02-08 23:14:28 +01:00
}
}
else {
$this->template = "common/error.tpl";
$this->data['errorstring'] = $this->data['text_you_are_not_admin'];
}
$this->render();
}
private function validate() {
//password is required and must be greater than the MIN_PASSWORD_LENGTH
if(!isset($this->request->post['password'])) {
2012-02-08 23:14:28 +01:00
$this->error['password'] = $this->data['text_missing_password'];
} elseif (strlen(@$this->request->post['password']) < MIN_PASSWORD_LENGTH) {
2012-02-08 23:14:28 +01:00
$this->error['password'] = $this->data['text_too_short_password'];
}
//password2 is required and must be greater than the MIN_PASSWORD_LENGTH
if(!isset($this->request->post['password2'])) {
$this->error['password2'] = $this->data['text_missing_password'];
} elseif (strlen(@$this->request->post['password2']) < MIN_PASSWORD_LENGTH) {
$this->error['password2'] = $this->data['text_too_short_password'];
}
//passwords must match (put here to override the password2 missing message, if also present)
2012-02-08 23:14:28 +01:00
if($this->request->post['password'] != $this->request->post['password2']) {
$this->error['password2'] = $this->data['text_password_mismatch'];
2012-02-08 23:14:28 +01:00
}
//uid is required and must be numeric & 0 or greater
2012-02-08 23:14:28 +01:00
if(!isset($this->request->post['uid']) || !is_numeric($this->request->post['uid']) || $this->request->post['uid'] < 0) {
$this->error['uid'] = $this->data['text_invalid_uid'];
}
//email address is required and must be in the proper format
2012-02-08 23:14:28 +01:00
if(!isset($this->request->post['email']) || strlen($this->request->post['email']) < 3) {
$this->error['email'] = $this->data['text_invalid_email'];
}
else {
$emails = explode("\n", $this->request->post['email']);
foreach ($emails as $email) {
$email = rtrim($email);
if($email == '') { continue; }
2012-02-08 23:14:28 +01:00
$ret = checkemail($email, $this->domains);
if($ret == 0) {
$this->error['email'] = $this->data['text_invalid_email'] . ": *$email*";
2012-02-08 23:14:28 +01:00
}
else if($ret == -1) {
$this->error['email'] = $this->data['text_email_in_unknown_domain'] . ": *$email*";
2012-02-08 23:14:28 +01:00
}
}
}
//username is required and must be greater than 2 chars
2012-02-08 23:14:28 +01:00
if(!isset($this->request->post['username']) || strlen($this->request->post['username']) < 2) {
$this->error['username'] = $this->data['text_invalid_username'];
}
//username is required and must be unique
2012-06-22 15:22:02 +02:00
if(isset($this->request->post['username']) && $this->model_user_user->get_uid_by_name($this->request->post['username']) > 0) {
2012-02-08 23:14:28 +01:00
$this->error['username'] = $this->data['text_existing_user'];
}
//primary domain is required
2012-02-08 23:14:28 +01:00
if(!isset($this->request->post['domain'])) {
$this->error['domain'] = $this->data['text_missing_data'];
}
if (!$this->error) {
return true;
} else {
return false;
}
}
}
?>