import from branch_1_1:
[freeradius.git] / dialup_admin / lib / sql / change_attrs.php3
1 <?php
2 require_once('../lib/functions.php3');
3 if (is_file("../lib/sql/drivers/$config[sql_type]/functions.php3"))
4         include_once("../lib/sql/drivers/$config[sql_type]/functions.php3");
5 else{
6         echo "<b>Could not include SQL library</b><br>\n";
7         exit();
8 }
9 if ($config[sql_use_operators] == 'true'){
10         $use_ops=1;
11         $text1 = 'AND op =';
12         $text2 = ',op';
13 }
14 $link = @da_sql_pconnect($config);
15 if ($link){
16         foreach($show_attrs as $key => $desc){
17                 if ($attrmap["$key"] == 'none')
18                         continue;
19                 if ($attrmap["$key"] == ''){
20                         $attrmap["$key"] = $key;
21                         $attr_type["key"] = 'replyItem';
22                         $rev_attrmap["$key"] = $key;
23                 }
24                 $i = 0;
25                 $j = -1;
26                 $name = $attrmap["$key"] . $i;
27
28                 while(isset($$name)){
29                         $val=$$name;
30                         $val = da_sql_escape_string($val);
31                         $op_name = $name . '_op';
32                         $i++;
33                         $j++;
34                         $name = $attrmap["$key"] . $i;
35
36                         $sql_attr=$attrmap["$key"];
37                         $query_key = ($user_type == 'group') ? 'groupname' : 'username';
38                         if ($attr_type["$key"] == 'checkItem'){
39                                 $table = ($user_type == 'group') ? $config[sql_groupcheck_table] : $config[sql_check_table];
40                                 $type = 1;
41                         }
42                         else if ($attr_type["$key"] == 'replyItem'){
43                                 $table = ($user_type == 'group') ? $config[sql_groupreply_table] : $config[sql_reply_table];
44                                 $type = 2;
45                         }
46                         if ($use_ops){
47                                 $op_val = $$op_name;
48                                 if ($op_val != ''){
49                                         $op_val = da_sql_escape_string($op_val);
50                                         if (check_operator($op_val,$type) == -1){
51                                                 echo "<b>Invalid operator ($op_val) for attribute $key</b><br>\n";
52                                                 continue;
53                                         }
54                                         $op_val2 = ",'$op_val'";
55                                 }
56                         }
57                         $sql_attr = da_sql_escape_string($sql_attr);
58                         $val = da_sql_escape_string($val);
59         // if we have operators, the operator has changed and the corresponding value exists then update
60                         if ($use_ops && isset($item_vals["$key"][operator][$j]) &&
61                                 $op_val != $item_vals["$key"][operator][$j] ){
62                                 $res = @da_sql_query($link,$config,
63                                 "UPDATE $table SET op = '$op_val' WHERE $query_key = '$login'
64                                 AND attribute = '$sql_attr' AND value = '$val';");
65                                 if (!$res || !@da_sql_affected_rows($link,$res,$config))
66                                         echo "<b>Operator change failed for attribute $key: " . da_sql_error($link,$config) . "</b><br>\n";
67                         }
68
69         //      if value is the same as that in the sql database do nothing
70                         if ($val == $item_vals["$key"][$j])
71                                 continue;
72         //      if value is null or equals the default value and corresponding value exists then delete
73                         else if ((check_defaults($val,$op_val,$default_vals["$key"]) || $val == '') && isset($item_vals["$key"][$j])){
74                                 $res = @da_sql_query($link,$config,
75                                 "DELETE FROM $table WHERE $query_key = '$login' AND attribute = '$sql_attr';");
76                                 if (!$res || !@da_sql_affected_rows($link,$res,$config))
77                                         echo "<b>Delete failed for attribute $key: " . da_sql_error($link,$config) . "</b><br>\n";
78                         }
79         //      if value is null or equals the default value then don't add it 
80                         else if ($val == '' || check_defaults($val,$op_val,$default_vals["$key"]))
81                                 continue;
82         //      if value differs from the sql value then update
83                         else{
84                                 if (isset($item_vals["$key"][$j])){
85                                         $old_val = $item_vals["$key"][$j];
86                                         $old_val = da_sql_escape_string($old_val);
87                                         $res = @da_sql_query($link,$config,
88                                         "UPDATE $table SET value = '$val' WHERE $query_key = '$login' AND
89                                         attribute = '$sql_attr' AND value = '$old_val';");
90                                 }
91                                 else
92                                         $res = @da_sql_query($link,$config,
93                                         "INSERT INTO $table ($query_key,attribute,value $text2)
94                                         VALUES ('$login','$sql_attr','$val' $op_val2);");
95                                 if (!$res || !@da_sql_affected_rows($link,$res,$config))
96                                         echo "<b>Change failed for attribute $key: " . da_sql_error($link,$config) . "</b><br>\n";
97                         }
98                 }
99         }
100 }
101 else
102         echo "<b>Could not connect to SQL database</b><br>\n";