Few tweaks to the plugins system
[PHPCredLocker.git] / lib / db / Credentials.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
/** Credential related Database functions
*
* Copyright (C) 2012 B Tasker
* Released under GNU GPL V2
* See LICENSE
*
*/ 
defined('_CREDLOCK') or die;
 
 
class CredDB extends BTDB{
 
 
/** Check Cred Types are specified
*
* @return boolean
*/
function checkCredTypesDefined(){
 
$sql = "SELECT * FROM #__CredTypes";
$this->setQuery($sql);
return $this->loadResult();
}
 
 
 
/** Delete the credential specified
*
* @arg id INT
*
* @return boolena
*/
function DelCredential($id){
// Log the request
$log = new Logging;
$log->logEntry($id,10);
$ACL = BTMain::buildACLQuery();
$id = $this->stringEscape($id);
$sql = "DELETE FROM #__Cred WHERE id='$id' AND ($ACL)";
$this->setQuery($sql);
return $this->runQuery();
 
}
 
 
 
/** Retrieve the credential specified
*
* @arg id INT
*
* @return object
*/
function FetchCredential($id){
// Log the request
$log = new Logging;
$log->logEntry($id,9);
 
$ACL = BTMain::buildACLQuery();
 
$id = $this->stringEscape($id);
 
$sql = "SELECT Hash, Clicky, Address, UName, CredType, `Group` FROM #__Cred WHERE id='$id' AND ($ACL)";
$this->setQuery($sql);
 
 
return $this->loadResult();
}
 
 
/** Fetch All Credentials for a given type
*
* @arg type id
*
* @return object
*/
function getCredsbyType($id){
$id = $this->stringEscape($id);
$ACL = BTMain::buildACLQuery('a');
$CustACL = BTMain::buildACLQuery('b');
 
$sql = "SELECT a.id, b.Name FROM #__Cred as a LEFT JOIN #__Cust as b on a.cust=b.id WHERE CredType='$id' AND ($ACL) AND ($CustACL)";
$this->setQuery($sql);
 
return $this->loadResults();
 
 
 
}
 
 
 
 
/** Add a new Credential Type
*
* @arg Name string
*
* @return boolean
*/
function AddCredType($name){
$crypt = new Crypto;
$name = $crypt->encrypt($name,'CredType');
 
$name = $this->stringEscape($name);
 
$sql = "INSERT INTO #__CredTypes (`Name`) VALUES ('$name')";
$this->setQuery($sql);
 
$id = $this->insertID();
 
if ($id){
$log = new Logging;
$log->logEntry($id,15);
return $id;
}else{
return false;
}
}
 
 
 
/** Retrieve the available credential types
*
*/
function getCredTypes(){
 
$sql = "SELECT * FROM #__CredTypes";
$this->setQuery($sql);
return $this->loadResults();
}
 
 
/** Retrieve credential type
*
* @arg id INT
*
* @return object
*/
function getCredType($id){
$id = $this->stringEscape($id);
$sql = "SELECT * FROM #__CredTypes WHERE id='$id'";
$this->setQuery($sql);
return $this->loadResult();
}
 
 
 
/** Edit Credential Type
*
* @arg id - INT
* @arg name - string
*
* @return boolean
*/
function editCredType($id,$name){
$crypt = new Crypto;
$id = $this->stringEscape($id);
 
$name = $crypt->encrypt($name,'CredType');
$name = $this->stringEscape($name);
 
$sql = "UPDATE #__CredTypes SET `Name`='$name' WHERE id='$id'";
$this->setQuery($sql);
 
return $this->runQuery();
 
}
 
 
 
/** Delete credential type and all associated creds
*
* @arg id INT
*
* @return boolean
*/
function DelCredentialType($id){
$id = $this->stringEscape($id);
 
$sql = "DELETE FROM #__Cred WHERE `CredType`='$id'";
$this->setQuery($sql);
$this->runQuery();
 
$sql = "DELETE FROM #__CredTypes WHERE id='$id'";
$this->setQuery($sql);
if ($this->runQuery()){
$log = new Logging;
$log->logEntry($id,16);
return true;
}else{
return false;
}
 
 
 
 
 
 
}
 
 
/** Insert a new Credential into the database
*
* @arg cust - INT
* @arg credtype - INT
* @arg cred - string
* @arg clicky - tinyint
* @arg group - INT
* @arg address - string
* @arg uname - string
*
* @return object
*/
function addCred($cust,$credtype,$cred,$clicky,$group = 1,$address = '', $uname = '')
{
 
 
// Encrypt the relevant parts
$crypt = new Crypto;
 
 
if (!empty($address)){
$address = $crypt->encrypt($address,'Cre'.$credtype);
}
 
if (!empty($uname)){
$uname = $crypt->encrypt($uname,'Cre'.$credtype);
}
 
if (!empty($cred)){
$cred = $crypt->encrypt($cred,'Cre'.$credtype);
}
 
$address = $this->stringEscape($address);
$uname = $this->stringEscape($uname);
$credtype = $this->stringEscape($credtype);
$cred = $this->stringEscape($cred);
$cust = $this->stringEscape($cust);
$clicky = $this->stringEscape($clicky);
$date = date('Y-m-d H:i:s');
$group = $this->stringEscape($group);
 
 
$sql = "INSERT INTO #__Cred (`cust`,`Added`,`Group`,`Hash`,`CredType`,`Clicky`,`Address`,`UName`) ".
"VALUES ('$cust','$date','$group','$cred','$credtype','$clicky','$address','$uname')";
$this->setQuery($sql);
 
$id = $this->insertID();
 
    if ($id){
      $log = new Logging;
      $log->logEntry($id,7);
      return $id;
      }else{
      return false;
      }
 
}
 
 
 
 
/** Edit Specified Credential
*
* @arg id - INT
* @arg credtype - INT
* @arg cred - string
* @arg clicky - tinyint
* @arg group - INT
* @arg address - string
* @arg uname - string
*
* @return object
*/
function editCred($id,$credtype,$cred,$clicky,$group = 1,$address = '', $uname = '')
{
 
 
// Initialise some vars
$crypt = new Crypto;
$ACL = BTMain::buildACLQuery();
$credtype = $this->stringEscape($credtype);
$id = $this->stringEscape($id);
$date = date('Y-m-d H:i:s');
$group = $this->stringEscape($group);
 
 
// build the SQL
 
$sql = "UPDATE #__Cred SET `Added`='$date', `Group`='$group',";
 
if ($cred){
$cred = $crypt->encrypt($cred,'Cre'.$credtype);
$cred = $this->stringEscape($cred);
$sql .= "`Hash`='$cred',";
}
 
 
if ($clicky){
$clicky = $this->stringEscape($clicky);
$sql .= "`Clicky`='$clicky',";
}
 
if ($address){
$address = $crypt->encrypt($address,'Cre'.$credtype);
$address = $this->stringEscape($address);
$sql .= "`Address`='$address',";
}
 
if ($uname){
$uname = $crypt->encrypt($uname,'Cre'.$credtype);
$uname = $this->stringEscape($uname);
$sql .= "`UName`='$uname',";
}
 
// Get rid of the last comma to prevent a syntax error
$sql = rtrim($sql,",");
 
$sql .= " WHERE id='$id' AND ($ACL)";
 
$this->setQuery($sql);
 
      if ($this->runQuery()){
 
      $log = new Logging;
      $log->logEntry($id,8);
      return true;
      }else{
      return false;
      }
 
}
 
 
 
 
}
 
 
 
 
?>