import from HEAD:
[freeradius.git] / dialup_admin / lib / sql / create_user.php3
1 <?php
2 if (is_file("../lib/sql/drivers/$config[sql_type]/functions.php3"))
3         include_once("../lib/sql/drivers/$config[sql_type]/functions.php3");
4 else{
5         echo "<b>Could not include SQL library</b><br>\n";
6         exit();
7 }
8 include_once('../lib/functions.php3');
9 if ($config[sql_use_operators] == 'true'){
10         include("../lib/operators.php3");
11         $text = ',op';
12         $passwd_op = ",':='";
13 }
14 $da_abort=0;
15 $link = @da_sql_pconnect($config);
16 if ($link){
17         if (is_file("../lib/crypt/$config[general_encryption_method].php3")){
18                 include("../lib/crypt/$config[general_encryption_method].php3");
19                 $passwd = da_encrypt($passwd);
20                 $res = @da_sql_query($link,$config,
21                 "INSERT INTO $config[sql_check_table] (attribute,value,username $text)
22                 VALUES ('$config[sql_password_attribute]','$passwd','$login' $passwd_op);");
23                 if (!$res || !@da_sql_affected_rows($link,$res,$config)){
24                         echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
25                         $da_abort=1;
26                 }
27                 if ($config[sql_use_user_info_table] == 'true' && !$da_abort){
28                         $res = @da_sql_query($link,$config,
29                         "SELECT username FROM $config[sql_user_info_table] WHERE
30                         username = '$login';");
31                         if ($res){
32                                 if (!@da_sql_num_rows($res,$config)){
33                                         $Fcn = da_sql_escape_string($Fcn);
34                                         $Fmail = da_sql_escape_string($Fmail);
35                                         $Fou = da_sql_escape_string($Fou);
36                                         $Fhomephone = da_sql_escape_string($Fhomephone);
37                                         $Fworkphone = da_sql_escape_string($Fworkphone);
38                                         $Fmobile = da_sql_escape_string($Fmobile);      
39                                         $res = @da_sql_query($link,$config,
40                                         "INSERT INTO $config[sql_user_info_table]
41                                         (username,name,mail,department,homephone,workphone,mobile) VALUES
42                                         ('$login','$Fcn','$Fmail','$Fou','$Fhomephone','$Ftelephonenumber','$Fmobile');");
43                                         if (!$res || !@da_sql_affected_rows($link,$res,$config))
44                                                 echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
45                                 }
46                                 else
47                                         echo "<b>User already exists in user info table.</b><br>\n";
48                         }
49                         else
50                                 echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
51                 }
52                 if ($Fgroup != ''){
53                         $Fgroup = da_sql_escape_string($Fgroup);
54                         $res = @da_sql_query($link,$config,
55                         "SELECT username FROM $config[sql_usergroup_table]
56                         WHERE username = '$login' AND groupname = '$Fgroup';");
57                         if ($res){
58                                 if (!@da_sql_num_rows($res,$config)){
59                                         $res = @da_sql_query($link,$config,
60                                         "INSERT INTO $config[sql_usergroup_table]
61                                         (username,groupname) VALUES ('$login','$Fgroup');");
62                                         if (!$res || !@da_sql_affected_rows($link,$res,$config))
63                                                 echo "<b>Could not add user to group $Fgroup. SQL Error</b><br>\n";
64                                 }
65                                 else
66                                         echo "<b>User already is a member of group $Fgroup</b><br>\n";
67                         }
68                         else
69                                 echo "<b>Could not add user to group $Fgroup: " . da_sql_error($link,$config) . "</b><br>\n";
70                 }
71                 if (!$da_abort){
72                         if ($Fgroup != '')
73                                 require('../lib/defaults.php3');
74                         foreach($show_attrs as $key => $attr){
75                                 if ($attrmap["$key"] == 'none')
76                                         continue;
77                                 if ($attrmap["$key"] == ''){
78                                         $attrmap["$key"] = $key;
79                                         $attr_type["$key"] = 'replyItem';
80                                         $rev_attrmap["$key"] = $key;
81                                 }
82                                 if ($attr_type["$key"] == 'checkItem'){
83                                         $table = "$config[sql_check_table]";
84                                         $type = 1;
85                                 }
86                                 else if ($attr_type["$key"] == 'replyItem'){
87                                         $table = "$config[sql_reply_table]";
88                                         $type = 2;
89                                 }
90                                 $val = $$attrmap["$key"];
91                                 $val = da_sql_escape_string($val);
92                                 $op_name = $attrmap["$key"] . '_op';
93                                 $op_val = $$op_name;
94                                 if ($op_val != ''){
95                                         if (check_operator($op_val,$type) == -1){
96                                                 echo "<b>Invalid operator ($op_val) for attribute $key</b><br>\n";
97                                                 coninue;
98                                         }
99                                         $op_val2 = ",'$op_val'";
100                                 }
101                                 if ($val == '' || check_defaults($val,$op_val,$default_vals["$key"]))
102                                         continue;
103                                 $res = @da_sql_query($link,$config,
104                                 "INSERT INTO $table (attribute,value,username $text)
105                                 VALUES ('$attrmap[$key]','$val','$login' $op_val2);");
106                                 if (!$res || !@da_sql_affected_rows($link,$res,$config))
107                                         echo "<b>Query failed for attribute $key: " . da_sql_error($link,$config) . "</b><br>\n";
108                         }
109                 }
110                 echo "<b>User created successfully</b><br>\n";
111         }
112         else
113                 echo "<b>Could not open encryption library file</b><br>\n";
114 }
115 else
116         echo "<b>Could not connect to SQL database</b><br>\n";
117 ?>