forked from Kipjr/Ldap_Login
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions_sql.inc.php
More file actions
206 lines (186 loc) · 4.41 KB
/
functions_sql.inc.php
File metadata and controls
206 lines (186 loc) · 4.41 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
function ld_table_exist() {
/**
* Checks table in SQL-database Piwigo for plugin ldap_login
*
*
* - Return an boolean for table existance
*
* @since 2.10.1
*
*
* @return boolean
*/
$query = ('select 1 from `piwigo_ldap_login_config` LIMIT 1');
$r = pwg_query($query);
if(is_object($r) !== TRUE)
{
//table not found..
return false;
}
else
{
return true;
//I can find it...
}
}
function ld_sql($action='get',$type=null,$data=null){
/**
* Does actions on SQL-database Piwigo for plugin ldap_login
* Depending on $action and $type it can do for:
*
* ld_sql($action='get')
* - Return an associative array of a table values (key='...', value='...')
*
* ld_sql($action='create',$type='create_table')
* - Creates table piwigo_ldap_login_config
*
* ld_sql($action='create',$type='create_data','data')
* - Inserts data (array(array($k,$v),...)) in table
*
* ld_sql($action='update','reset_openldap')
* - Updates values corresponding with OpenLDAP values (classes and attributes)
*
* ld_sql($action='update','reset_ad')
* - Updates values corresponding with AD values (classes and attributes)
*
* ld_sql($action='update','update_value','data')
* - Updates data from array($k1=>$v1,$k2=>$v2,...) in table
*
* ld_sql($action='delete')
* - Deletes piwigo_ldap_login_config
*
*
* @since 2.10.1
*
* @param string $action
* @param string $type
* @param array $data
* @return array $result
*/
###
### GET
###
if ($action === 'get') {
if(ld_table_exist()){
$query ='SELECT param,value FROM piwigo_ldap_login_config';
$result = query2array($query,'param','value');
return $result;
}
};
###
### CREATE
### ENGINE = MyISAM CHARSET=utf8 COLLATE utf8_general_ci;
if ($action == 'create'){
if ($type == 'create_table') {
$query="CREATE TABLE IF NOT EXISTS `piwigo_ldap_login_config` (`param` varchar(40) CHARACTER SET utf8 NOT NULL,`value` text CHARACTER SET utf8,`comment` varchar(255) CHARACTER SET utf8 DEFAULT NULL,UNIQUE KEY `param` (`param`)) ENGINE = MyISAM CHARSET=utf8 COLLATE utf8_general_ci;";
pwg_query($query);
}
if ($type == 'create_data') {
if(isset($data)){
foreach($data as $k => $v){
$datas[]=array(
'param' => $k,
'value' => pwg_db_real_escape_string($v)
);
}
mass_inserts('piwigo_ldap_login_config', array('param','value'), $datas,array('ignore'=>true));
}
}
}
###
### Update
###
//maybe try this in future, but piwigo functions dont support it (yet)
//INSERT OR IGNORE INTO book(id) VALUES(1001);
//UPDATE book SET name = 'Programming' WHERE id = 1001;
if ($action == 'update'){
if(ld_table_exist()){
if ($type == 'reset_openldap') {
$updates = array(
array(
'param' => 'ld_user_attr',
'value' => 'cn'
),
array(
'param' => 'ld_user_class',
'value' => 'inetOrgPerson'
),
array(
'param' => 'ld_group_class',
'value' => 'groupOfNames'
)
);
mass_updates(
'piwigo_ldap_login_config',
array(
'primary' => array('param'),
'update' => array('value')
),
$updates
);
}
if ($type == 'reset_ad') {
$updates = array(
array(
'param' => 'ld_user_attr',
'value' => 'sAMAccountname'
),
array(
'param' => 'ld_user_class',
'value' => 'user'
),
array(
'param' => 'ld_group_class',
'value' => 'group'
)
);
mass_updates(
'piwigo_ldap_login_config',
array(
'primary' => array('param'),
'update' => array('value')
),
$updates
);
}
if ($type == 'update_value') {
if (isset($data)) {
foreach($data as $k => $v){
$updates[]=array(
'param' => $k,
'value' => pwg_db_real_escape_string($v)
);
}
mass_updates(
'piwigo_ldap_login_config',
array(
'primary' => array('param'),
'update' => array('value')
),
$updates
);
}
}
if ($type == 'clear_mail_address') {
$query="update piwigo_users SET mail_address = null WHERE id > 2";
pwg_query($query);
}
}
}
###
### DELETE
###
if ($action == 'delete'){
if(ld_table_exist()){
if ($type=='delete_table') {
$query="
DROP TABLE `piwigo_ldap_login_config`;
";
pwg_query($query);
}
}
}
}