forked from sivann/itdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
242 lines (207 loc) · 6.38 KB
/
functions.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
<?php
function sec2ymd($secs)
{
if (strlen($secs))
return date("Ymd",$secs);
else
return "";
}
//convert Y/M/D dates to unix timestamp
function ymd2sec($d)
{
global $settings;
if (!strlen($d))
$purchasedate2="NULL";
elseif ($settings['dateformat']=="dmy"){
$x=explode("/",$d);
if ((count($x)==1) && strlen(trim($d))==4) { //only year
$d2= mktime(0, 0, 0, 1, 1, $d);
}
else {
$d2= mktime(0, 0, 0, $x[1], $x[0], $x[2]);
}
//echo "$d -> $d2<br>";
return $d2;
}
elseif ($settings['dateformat']=="mdy"){
$x=explode("/",$d);
if ((count($x)==1) && strlen(trim($d))==4) { //only year
$d2= mktime(0, 0, 0, 1, 1, $d);
}
else {
$d2= mktime(0, 0, 0, $x[0], $x[1], $x[2]);
}
return $d2;
}
return "";
}
//remove invalid filename characters
function validfn($s) {
$f =preg_split('//u', 'ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΣΤΥΦΧΨΩΪΫΌΎΏΆΈΰαβγδεζηθικλμνξοπρςστυφχψωίϊΐϋόύώάέΰ');
$t =preg_split('//u', 'ABGDEZHUIKLMNJOPRSSTYFXCVIUOUVAEUabgdezhuiklmnjoprsstyfxcviiiuouvaeu');
$s=str_replace($f,$t,$s);
$reserved = preg_quote('\/:*?"<>|', '/');
$s=preg_replace("/([-\\x00-\\x20\\x7f-\\xff{$reserved}])/e", "", $s);
$s=strtolower($s);
return $s;
}
//encode string for sql/html
function strenc($s)
{
$s=htmlspecialchars($s,ENT_QUOTES,"UTF-8");
return $s;
}
//////////////////// Database functions /////
// check permissions, log errors and transactions
//
//encode string for sql/html
//for insert, update, delete
function db_exec($dbh,$sql,$skipauth=0,$skiphist=0,&$wantlastid=0)
{
global $authstatus,$userdata, $remaddr, $dblogsize,$errorstr,$errorbt;
if (!$skipauth && !$authstatus) {echo "<big><b>Not logged in</b></big><br>";return 0;}
if (stristr($sql,"insert ")) $skiphist=1; //for lastid function to work.
//find user access
$usr=$userdata[0]['username'];
$sqlt="SELECT usertype FROM users where username='$usr'";
$sth=$dbh->prepare($sqlt);
$sth->execute();
$ut=$sth->fetch(PDO::FETCH_ASSOC);
$usertype=($ut['usertype']);
$sth->closeCursor();
if (!$skipauth && $usertype && (stristr($sql,"DELETE") || stristr($sql,"UPDATE") || stristr($sql,"INSERT"))
&& !stristr($sql," tt ")) { /*tt:temporary table used for complex queries*/
echo "<big><b>Access Denied, user '$usr' is read-only</b></big><br>";
return 0;
}
$r=$dbh->exec($sql);
$error = $dbh->errorInfo();
if($error[0] && isset($error[2])) {
$errorstr= "<br><b>db_exec:DB Error: ($sql): ".$error[2]."<br></b>";
$errorbt = debug_backtrace();
echo "</table></table></div>\n<pre>".$errorstr;
print_r ($errorbt);
return 0;
}
$wantlastid=$dbh->lastInsertId();
if (!$skiphist) {
$hist="";
$t=time();
$escsql=str_replace("'","''",$sql);
$histsql="INSERT into history (date,sql,ip,authuser) VALUES ($t,'$escsql','$remaddr','".$_COOKIE["itdbuser"]."')";
//update history table
$rh=$dbh->exec($histsql);
$lasthistid=$dbh->lastInsertId();
$error = $dbh->errorInfo();
if($error[0] && isset($error[2])) {
$errorstr= "<br><b>HIST DB Error: ($histsql): ".$error[2]."<br></b>";
$errorbt = debug_backtrace();
echo $errorstr;
print_r ($errorbt);
return 0;
}
else { /* remove old history entries */
$lastkeep=(int)($lasthistid)-$dblogsize;
$sql="DELETE from history where id<$lastkeep";
$sth=$dbh->exec($sql);
}
}
return $r;
} //db_exec
//for select
function db_execute($dbh,$sql,$skipauth=0)
{
global $authstatus,$errorstr,$errorbt;
if (!$skipauth && !$authstatus) {echo "<big><b>Not logged in</b></big><br>";return 0;}
$sth = $dbh->prepare($sql);
$error = $dbh->errorInfo();
if($error[0] && isset($error[2])) {
$errorstr= "\n<br><b>db_execute:DB Error: ($sql): ".$error[2]."<br></b>\n";
$errorbt= debug_backtrace();
echo "</table></table></div>\n<pre>".$errorstr;
print_r ($errorbt);
echo "</pre>";
return 0;
}
$sth->execute();
return $sth;
}
function opendb($dbfile) {
global $dbh;
//open db
try {
$dbh = new PDO("sqlite:$dbfile");
}
catch (PDOException $e) {
print "Open database Error!: " . $e->getMessage() . "<br>";
die();
}
return $dbh;
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
//$ret = $dbh->exec("PRAGMA case_sensitive_like = 0;");
}
function ckdberr($resource) {
global $errorstr;
$error = $resource->errorInfo();
if($error[0] && isset($error[2])) {
$errorstr= $error[2];
$errorbt = debug_backtrace();
logerr($errorstr." BACKTRACE: ".$errorbt);
return 1;
}
return 0;
}
/*execute with prepared statements
Example:
$sql="SELECT * from tablename where id=:id order by date";
$stmt=db_execute($dbh,$sql,array('id'=>$items['id']));
$res=$stmt->fetch(PDO::FETCH_ASSOC);
*/
function db_execute2($dbh,$sql,$params=NULL) {
global $errorstr,$errorbt,$errorno;
$sth = $dbh->prepare($sql);
$error = $dbh->errorInfo();
if(((int)$error[0]||(int)$error[1]) && isset($error[2])) {
$errorstr= "DB Error: ($sql): <br>\n".
$error[2]."<br>\nParameters:"."params\n";
//implode(",",$params);
$errorbt= debug_backtrace();
$errorno=$error[1]+$error[0];
logerr("$errorstr BACKTRACE:".$errorbt);
return 0;
}
if (is_array($params))
$sth->execute($params);
else
$sth->execute();
$error = $sth->errorInfo();
if(((int)$error[0]||(int)$error[1]) && isset($error[2])) {
$errorstr= "DB Error: ($sql): <br>\n".$error[2]."<br>\nParameters:".implode(",",$params);
$errorbt= debug_backtrace();
$errorno=$error[1]+$error[0];
logerr("$errorstr BACKTRACE:".$errorbt);
}
return $sth;
}
function connect_to_ldap_server($ldap_server,$username,$passwd,$ldap_dn) {
global $gen_error,$gen_errorstr;
$ds=ldap_connect($ldap_server); // must be a valid LDAP server!
//echo "connect result is " . $ds . "<br />\n";
if($ds){
$dn="uid=".$username.",".$ldap_dn;
echo $dn;
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
$r=ldap_bind($ds,$dn, $passwd);
if(!$r){
$gen_errorstr="ldap_bind: ".ldap_error($ds);
$gen_error=100;
ldap_close($ds);
return FALSE;
}
return $ds;
}
else {
return FALSE;
}
}
?>