forked from intfocus/iLearn_iSearch_API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigFunction.php
executable file
·152 lines (139 loc) · 4.71 KB
/
configFunction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
////////////////////////////////////
// 20130624, created by Odie
// Some functions used to get and set config values.
// The config value is stored in "customer" table with column named "conf".
// it contains 100 digit, each digit reprensents some config value.
// The meaning of each bit is stored in "conf" table.
// "conf_name" stands for the name,
// and "conf_index" stands for the index of the config in the 100 digit string.
////////////////////////////////////
// check if the guid valid, return true if valid, false if not valid
function check_guid($link, $guid){
if(gettype($link) != "object" || gettype($guid) != "string")
return false;
$query_cmd = "select * from customer where GUID='$guid'";
$result = mysqli_query($link, $query_cmd);
if($result && mysqli_num_rows($result) > 0){
$row = mysqli_fetch_assoc($result);
return $row["name"];
}
else
return false;
}
// used for other functions only, do not call directly in other programs
// return an array with format {"config_name" => "index"(conf_index) } if success
// return false if fail
function get_config_name_and_index($link){
// check parameter
if(gettype($link) != "object")
return false;
$query_cmd = "select * from conf";
$result = mysqli_query($link, $query_cmd);
if($result){
while($row = mysqli_fetch_assoc($result)){
$conf_name = $row["conf_name"];
$index = $row["conf_index"];
$config[$conf_name] = $index;
}
return $config;
}
else
return false;
}
// used for other functions only, do not call directly in other programs
// return a string with conf value (100 digits) if success
// return false if fail
function get_config_string($link, $guid){
// check parameter
if(gettype($link) != "object" || gettype($guid) != "string")
return false;
$query_cmd = "select conf from customer where GUID='$guid'";
$result = mysqli_query($link, $query_cmd);
if($result){
$row = mysqli_fetch_assoc($result);
$conf_value = $row["conf"];
return $conf_value;
}
else
return false;
}
// return an array with format {"config_name" => "value"} if success
// return false if fail
function get_all_config_name_and_value($link, $guid){
if(gettype($link) != "object" || gettype($guid) != "string")
return false;
$conf_string = get_config_string($link, $guid);
$conf_array = get_config_name_and_index($link, $guid);
$conf_return = array();
if($conf_string && $conf_array){
foreach($conf_array as $name => $value){
$conf_return[$name] = $conf_string[$value];
}
return $conf_return;
}
return false;
}
// get the config value by config_name
// return 0 if success
// return -1 if fail
function get_config_by_name($link, $guid, $config_name){
if(gettype($link) != "object" || gettype($guid) != "string")
return -1;
$config = get_config_name_and_index($link);
if($config && array_key_exists($config_name, $config)){
$index = $config[$config_name];
$conf_value = get_config_string($link, $guid);
if($conf_value){
return $conf_value[$index];
}
}
return -1;
}
// set the config value by config_name, value and update DB
// return 0 if success
// return -1 if fail
function set_config_by_name($link, $guid, $config_name, $value){
if(gettype($link) != "object" || gettype($guid) != "string")
return -1;
$value_str = (string)$value;
if(strlen($value_str) != 1)
return -1;
$config = get_config_name_and_index($link);
if($config && array_key_exists($config_name, $config)){
$index = $config[$config_name];
$conf_value = get_config_string($link, $guid);
if($conf_value){
$conf_value[$index] = $value_str;
$update_cmd = "update customer set conf='$conf_value' where GUID='$guid'";
if(mysqli_query($link, $update_cmd)){
return 0;
}
}
}
return -1;
}
// set the config value by array and update DB
// return 0 if success
// return -1 if fail
function set_all_config_by_name($link, $guid, $config_pair){
if(gettype($link) != "object" || gettype($guid) != "string" || count($config_pair) == 0)
return -1;
$config = get_config_name_and_index($link);
$conf_value = get_config_string($link, $guid);
if($config && $conf_value){
foreach($config_pair as $key => $value){
if(strlen($value) != 1 || !array_key_exists($key, $config)){
return -1;
}
$index = $config[$key];
$conf_value[$index] = (string)$value;
}
$update_cmd = "update customer set conf='$conf_value' where GUID='$guid'";
if(mysqli_query($link, $update_cmd)){
return 0;
}
}
return -1;
}
?>