Project

General

Profile

« Previous | Next » 

Revision 1815

Added by Dietmar almost 12 years ago

! Complex code refactoring users management

View differences:

add.php
15 15
 *
16 16
 */
17 17

  
18
// Print admin header
19
require('../../config.php');
20
require_once(WB_PATH.'/framework/class.admin.php');
21
// suppress to print the header, so no new FTAN will be set
22
$admin = new admin('Access', 'users_add',false);
18
/* -------------------------------------------------------- */
19
// Must include code to stop this file being accessed directly
20
if(!defined('WB_URL')) {
21
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
22
	throw new IllegalFileException();
23
}
24
/* -------------------------------------------------------- */
23 25

  
24
// Create a javascript back link
25
$js_back = ADMIN_URL.'/users/index.php';
26
	function add_user($admin, &$aActionRequest)
27
	{
28
		global $MESSAGE,$TEXT, $HEADING;
29
		$database = WbDatabase::getInstance();
30
        $bRetVal = false;
31
        $iMinPassLength = 6;
26 32

  
27
if( !$admin->checkFTAN() )
28
{
29
	$admin->print_header();
30
	$admin->print_error($MESSAGE['GENERIC_SECURITY_ACCESS'], $js_back);
31
}
32
// After check print the header
33
$admin->print_header();
33
        if( !$admin->checkFTAN() )
34
        {
35
//        	$admin->print_header();
36
        	msgQueue::add($MESSAGE['GENERIC_SECURITY_ACCESS']);
37
            return $bRetVal;
38
        }
34 39

  
35
// Get details entered
36
$groups_id = (isset($_POST['groups'])) ? implode(",", $admin->add_slashes($_POST['groups'])) : ''; //should check permissions
37
$groups_id = trim($groups_id, ','); // there will be an additional ',' when "Please Choose" was selected, too
38
$active = $admin->add_slashes($_POST['active'][0]);
39
$username_fieldname = $admin->get_post_escaped('username_fieldname');
40
$username = strtolower($admin->get_post_escaped($username_fieldname));
41
$password = $admin->get_post('password');
42
$password2 = $admin->get_post('password2');
43
$display_name = $admin->get_post_escaped('display_name');
44
$email = $admin->get_post_escaped('email');
45
$home_folder = $admin->get_post_escaped('home_folder');
46
$default_language = DEFAULT_LANGUAGE;
40
        // Get details entered
41
        $groups_id = (isset($aActionRequest['groups'])) ? implode(",", $admin->add_slashes($aActionRequest['groups'])) : '';
42
        $groups_id = trim($groups_id, ','); // there will be an additional ',' when "Please Choose" was selected, too
43
        $active = intval(strip_tags($admin->StripCodeFromText($aActionRequest['active'][0])));
44
        $username_fieldname = strip_tags($admin->StripCodeFromText($aActionRequest['username_fieldname']));
45
        $username = strtolower(strip_tags($admin->StripCodeFromText($aActionRequest[$username_fieldname])));
46
        $password = strip_tags($admin->StripCodeFromText($aActionRequest['password']));
47
        $password2 = strip_tags($admin->StripCodeFromText($aActionRequest['password2']));
48
        $display_name = strip_tags($admin->StripCodeFromText($aActionRequest['display_name']));
49
        $email = strip_tags($admin->StripCodeFromText($aActionRequest['email']));
50
        $home_folder = strip_tags($admin->StripCodeFromText($aActionRequest['home_folder']));
47 51

  
48
// Check values
49
if($groups_id == '') {
50
	$admin->print_error($MESSAGE['USERS']['NO_GROUP'], $js_back);
51
}
52
if(!preg_match('/^[a-z]{1}[a-z0-9_-]{2,}$/i', $username)) {
53
	$admin->print_error( $MESSAGE['USERS_NAME_INVALID_CHARS'].' / '.
54
	                  $MESSAGE['USERS_USERNAME_TOO_SHORT'], $js_back);
55
}
56
if(strlen($password) < 2) {
57
	$admin->print_error($MESSAGE['USERS']['PASSWORD_TOO_SHORT'], $js_back);
58
}
59
if($password != $password2) {
60
	$admin->print_error($MESSAGE['USERS']['PASSWORD_MISMATCH'], $js_back);
61
}
62
if($email != '')
63
{
64
	if($admin->validate_email($email) == false)
65
    {
66
		$admin->print_error($MESSAGE['USERS']['INVALID_EMAIL'], $js_back);
67
	}
68
} else { // e-mail must be present
69
	$admin->print_error($MESSAGE['SIGNUP']['NO_EMAIL'], $js_back);
70
}
52
        $language = DEFAULT_LANGUAGE;
53
        $timezone = -72000;
54
        $date_format = DEFAULT_DATE_FORMAT;
55
        $time_format = DEFAULT_TIME_FORMAT;
56
        $confirm_code = '';
57
        $confirm_timeout = 0;
58
        $remember_key = '';
59
        $login_ip = '';
60
        $last_reset = 0;
61
        $login_when = 0;
71 62

  
72
// choose group_id from groups_id - workaround for still remaining calls to group_id (to be cleaned-up)
73
$gid_tmp = explode(',', $groups_id);
74
if(in_array('1', $gid_tmp)) $group_id = '1'; // if user is in administrator-group, get this group
75
else $group_id = $gid_tmp[0]; // else just get the first one
76
unset($gid_tmp);
63
        // Check values
64
        // Check values
65
        if($groups_id == "") {
66
        	msgQueue::add($MESSAGE['USERS_NO_GROUP']);
67
        } else {
68
            $aGroups_id = explode(',', $groups_id);
69
            //if user is in administrator-group, get this group else just get the first one
70
            if($admin->is_group_match($groups_id,'1')) { $group_id = 1; } else { $group_id = intval($aGroups_id[0]); }
71
        }
77 72

  
78
// Check if username already exists
79
$results = $database->query("SELECT user_id FROM ".TABLE_PREFIX."users WHERE username = '$username'");
80
if($results->numRows() > 0) {
81
	$admin->print_error($MESSAGE['USERS']['USERNAME_TAKEN'], $js_back);
82
}
73
        if(!preg_match('/^[a-z]{1}[a-z0-9_-]{2,}$/i', $username)) {
74
        	msgQueue::add( $MESSAGE['USERS_NAME_INVALID_CHARS']);
75
        }
83 76

  
84
// Check if the email already exists
85
$results = $database->query("SELECT user_id FROM ".TABLE_PREFIX."users WHERE email = '".$admin->add_slashes($_POST['email'])."'");
86
if($results->numRows() > 0)
87
{
88
	if(isset($MESSAGE['USERS']['EMAIL_TAKEN']))
89
    {
90
		$admin->print_error($MESSAGE['USERS']['EMAIL_TAKEN'], $js_back);
91
	} else {
92
		$admin->print_error($MESSAGE['USERS']['INVALID_EMAIL'], $js_back);
93
	}
94
}
77
		$sql  = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'users` '.
78
                'WHERE `username` LIKE \''.$username.'\' ';
79
        // Check if username already exists
80
        if( ($iFoundUser = $database->get_one($sql)) != null ) {
81
            if($iFoundUser) {
82
            	msgQueue::add($MESSAGE['USERS_USERNAME_TAKEN']);
83
            }
84
        }
95 85

  
96
// MD5 supplied password
97
$md5_password = md5($password);
86
    	if(strlen($password) < $iMinPassLength ) {
87
    		msgQueue::add($MESSAGE['USERS_PASSWORD_TOO_SHORT']);
88
    	}
98 89

  
99
// Inser the user into the database
100
$query = "INSERT INTO ".TABLE_PREFIX."users (group_id,groups_id,active,username,password,display_name,home_folder,email,timezone, language) VALUES ('$group_id', '$groups_id', '$active', '$username','$md5_password','$display_name','$home_folder','$email','-72000', '$default_language')";
101
$database->query($query);
102
if($database->is_error()) {
103
	$admin->print_error($database->get_error());
104
} else {
105
	$admin->print_success($MESSAGE['USERS']['ADDED']);
106
}
90
		$pattern = '/[^'.$admin->password_chars.']/';
91
		if (preg_match($pattern, $password)) {
92
			msgQueue::add($MESSAGE['PREFERENCES_INVALID_CHARS']);
93
    	}
107 94

  
108
// Print admin footer
109
$admin->print_footer();
95
    	if(($password != $password2) ) {
96
    		msgQueue::add($MESSAGE['USERS_PASSWORD_MISMATCH']);
97
    	}
98

  
99
//
100
// check that display_name is unique in whoole system (prevents from User-faking)
101
    	$sql  = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'users` ';
102
    	$sql .= 'WHERE `user_id` <> '.(int)$admin->get_user_id().' AND `display_name` LIKE "'.$display_name.'"';
103
    	if( ($iFoundUser = intval($database->get_one($sql))) > 0 ){
104
    	   msgQueue::add($MESSAGE['USERS_USERNAME_TAKEN'].' ('.$TEXT['DISPLAY_NAME'].')');
105
        } else {
106
            if($display_name == '') {
107
        	   msgQueue::add($MESSAGE['GENERIC_FILL_IN_ALL'].' ('.$TEXT['DISPLAY_NAME'].')');
108
            }
109
       }
110

  
111
        if(findStringInFileList($display_name, dirname(__FILE__).'/disallowedNames')) {
112
            msgQueue::add( $TEXT['ERROR'].' '.$TEXT['DISPLAY_NAME'].' ('.$display_name.')' );
113
        }
114

  
115
        if($email != "")
116
        {
117
        	if($admin->validate_email($email) == false)
118
            {
119
                msgQueue::add($MESSAGE['USERS_INVALID_EMAIL'].' ('.$email.')');
120
        	}
121
        } else { // e-mail must be present
122
        	msgQueue::add($MESSAGE['SIGNUP_NO_EMAIL']);
123
        }
124

  
125
		$sql  = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'users` '.
126
                'WHERE `email` LIKE \''.$email.'\' ';
127

  
128
        // Check if the email already exists
129
        if( ($iFoundUser = $database->get_one($sql)) != null ) {
130
            if($iFoundUser) {
131
            	if(isset($MESSAGE['USERS_EMAIL_TAKEN']))
132
                {
133
            		msgQueue::add($MESSAGE['USERS_EMAIL_TAKEN'].' ('.$email.')');
134
            	} else {
135
            		msgQueue::add($MESSAGE['USERS_INVALID_EMAIL'].' ('.$email.')');
136
            	}
137
            }
138
        }
139

  
140
		if( ($msg = msgQueue::getError()) == '')
141
		{
142
            //if user is in administrator-group, get this group else just get the first one
143
            if($admin->is_group_match($groups_id,'1')) { $group_id = 1; $groups_id = '1'; }
144
            // Inser the user into the database
145
			$sql  = 'INSERT INTO `'.TABLE_PREFIX.'users` SET '.
146
                    '`group_id`     = '.intval($group_id).', '.
147
                    '`groups_id`    = \''.mysql_real_escape_string($groups_id).'\', '.
148
                    '`active`       = '.intval($active).', '.
149
                    '`username`     = \''.mysql_real_escape_string($username).'\', '.
150
                    '`password`     = \''.md5($password).'\', '.
151
                    '`confirm_code` = \''.mysql_real_escape_string($confirm_code).'\', '.
152
                    '`confirm_timeout` = '.intval($confirm_timeout).', '.
153
                    '`remember_key` = \''.mysql_real_escape_string($remember_key).'\', '.
154
                    '`last_reset`   = '.intval($last_reset).', '.
155
                    '`display_name` = \''.mysql_real_escape_string($display_name).'\', '.
156
                    '`email`        = \''.mysql_real_escape_string($email).'\', '.
157
                    '`timezone`     = '.intval($timezone).', '.
158
                    '`date_format`  = \''.mysql_real_escape_string($date_format).'\', '.
159
                    '`time_format`  = \''.mysql_real_escape_string($time_format).'\', '.
160
                    '`language`     = \''.mysql_real_escape_string($language).'\', '.
161
                    '`home_folder`  = \''.mysql_real_escape_string($home_folder).'\', '.
162
                    '`login_when`   = '.intval($login_when).', '.
163
                    '`login_ip`     = \''.mysql_real_escape_string($login_ip).'\' '.
164
                    '';
165
            if($database->query($sql)) {
166
            	msgQueue::add($MESSAGE['USERS_ADDED'], true);
167
            }
168
            if($database->is_error()) {
169
                msgQueue::add( implode('<br />',explode(';',$database->get_error())) );
170
            }
171
        } else {
172
        	msgQueue::add($HEADING['ADD_USER'].' '.$MESSAGE['GENERIC_NOT_COMPARE']);
173

  
174
       }
175
    }
176
//

Also available in: Unified diff