-
Notifications
You must be signed in to change notification settings - Fork 1
/
qlib.php3
168 lines (142 loc) · 3.54 KB
/
qlib.php3
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
<?php
$QLIB_INCLUDED = TRUE;
function qlib_error($errorMessage,$msgType)
{
/* global $tpl;
$tpl->assign(MESSAGE, $errorMessage);
$tpl->parse(MAIN, ".message");
$tpl->FastPrint();
*/
echo($errorMessage);
exit;
}
// qlib_fopen
// opens a file $fileName and locks it
function qlib_fopen($fileName, $openMode, $sharing)
{
global $langOpenError;
$fp=fopen($fileName, $openMode);
if ($fp<0)
qlib_error("Error opening file ".$fileName."!<br/>Make sure writing is allowed in this directory.",2);
if ($sharing==true)
$op=1;
else
$op=2;
if (!flock($fp,$op))
{
flock($fp,3);
fclose($fp);
$fp = -1;
qlib_error("<i>flock</i> timeout",2);
exit();
}
return $fp;
}
// qlib_fclose
// unlocks the file $fp and closes it
function qlib_fclose($fp)
{
flock($fp,3);
fclose($fp);
}
//
// DB funcs
//
// qlib_DB_read
// open a DB, and read it into an array of arrays.
// Specify a list of keynames in the array $keynames.
function qlib_DB_read($filename, $separator = ",")
{
$fp = qlib_fopen($filename, "r", true);
while (feof($fp) == 0)
{
$line = chop(fgets($fp,1000));
// Ignore blank lines, and lines starting with '#' (comments)
if( $line == "" or $line[0] == '#' )
continue;
$arr = explode($separator, $line);
$keyArrayOfValueArrays[$arr[0]] = $arr;
}
qlib_fclose($fp);
return $keyArrayOfValueArrays;
}
function qlib_DB_findKey($filename, $key, $separator = ",")
{
$fp = qlib_fopen($filename, "r", true);
while (feof($fp) == 0)
{
$line = chop(fgets($fp,1000));
// Ignore blank lines, and lines starting with '#' (comments)
if( $line == "" or $line[0] == '#' )
continue;
$arr = explode($separator, $line);
if( $arr[0] == $key )
{
qlib_fclose($fp);
return $arr;
}
}
qlib_fclose($fp);
return array("");
}
function qlib_DB_updateKey($filename, $dataArray, $splitSeparator = ",", $joinSeparator = ",")
{
$fp = qlib_fopen($filename, "r", false);
while (feof($fp) == 0)
{
$line = chop(fgets($fp,1000));
// Ignore blank lines, and lines starting with '#' (comments)
if( $line == "" or $line[0] == '#' )
continue;
$arr = explode($splitSeparator, $line);
$keyArrayOfValueArrays[$arr[0]] = $arr;
}
fclose($fp); // Close the file, but keep the lock!
$keyArrayOfValueArrays[$dataArray[0]] = $dataArray;
// Reopen the file, which is still locked, so no changes could
// have happened in the meantime
$fp = fopen($filename, "w+");
foreach ($keyArrayOfValueArrays as $key => $valueArray)
{
// Write it out entry by entry
fputs($fp, join($joinSeparator, $valueArray) . "\n");
}
qlib_fclose($fp);
}
function qlib_DB_removeKey($filename, $key, $separator = ",")
{
$fp = qlib_fopen($filename, "r", false);
while (feof($fp) == 0)
{
$line = chop(fgets($fp,1000));
// Ignore blank lines, and lines starting with '#' (comments)
if( $line == "" or $line[0] == '#' )
continue;
$arr = explode($separator, $line);
$keyArrayOfValueArrays[$arr[0]] = $arr;
}
fclose($fp); // Close the file, but keep the lock!
// Remove the key
unset($keyArrayOfValueArrays[$key]);
// Reopen the file, which is still locked, so no changes could
// have happened in the meantime
$fp = fopen($filename, "w+");
foreach ($keyArrayOfValueArrays as $key => $valueArray)
{
// Write it out entry by entry
fputs($fp, join($separator, $valueArray) . "\n");
}
qlib_fclose($fp);
}
//
//
function qlib_filterUserText($str)
{
$str = str_replace("\\'", "'", $str);
$str = str_replace("\\\"", "\"", $str);
$str = htmlentities($str);
$str = str_replace("\n", "<br/>", $str);
$str = str_replace("$", "$", $str);
return $str;
}
?>