-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault-db.php
188 lines (151 loc) · 5.1 KB
/
default-db.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
<?php
/*
* Giki - a flat-file PHP wiki
* Copyright (C) 2006 Gregor Richards
*
* This file is part of Giki.
*
* Giki is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Giki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Giki; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once("config.php");
/* returns true if the title is bad */
function blacklistTitle($title) {
// blacklist the title
if (strpos($title, "/") !== false ||
strpos($title, "\\") !== false ||
strpos($title, "..") !== false) return true;
return false;
}
function nodeExists($title) {
global $nodedir;
// blacklist the title
if (blacklistTitle($title)) return false;
return file_exists($nodedir.$title.".wiki");
}
/* returns an array:
* "text" => text of the node
* "revhistory" => revision history array:
* 0 => history string
* 1 => ...
*/
function getNodeContents($title) {
global $nodedir;
if (!nodeExists($title)) return false;
// open the file
$file = fopen($nodedir.$title.".wiki", "r");
if ($file === false) return false;
// and lock it
flock($file, LOCK_SH);
// first line is the content
$text = fgets($file, 65536);
$text = rtrim($text);
// it uses ^NEWLINE^ to signify newlines
$text = str_replace("^NEWLINE^", "\n", $text);
// the revision history is stored line-by-line
$revhistory = array();
while (!feof($file)) {
$ln = fgets($file, 65536);
if ($ln !== false) {
$ln = rtrim($ln);
$revhistory[] = $ln;
}
}
// finish
flock($file, LOCK_UN);
fclose($file);
return array(
"text" => $text,
"revhistory" => $revhistory
);
}
/* takes the same format as getNodeContents returns */
function putNodeContents($title, $contents, $user) {
global $nodedir;
// blacklist the title
if (blacklistTitle($title)) return false;
// open the files
$file = fopen($nodedir.$title.".wiki", "w");
$histfile = fopen($nodedir . $title . "." . time() . ".hist", "w");
if ($file === false || $histfile === false) return false;
// and lock them
flock($file, LOCK_EX);
flock($histfile, LOCK_EX);
// convert the content
$text = str_replace("\n", "^NEWLINE^", $contents["text"]);
// write it out
fputs($file, $text . "\n");
fputs($histfile, $text . "\n" . $user . "\n");
// also write revision history
foreach ($contents["revhistory"] as $hist) {
fputs($file, $hist . "\n");
}
// finish up
flock($file, LOCK_UN);
flock($histfile, LOCK_UN);
fclose($file);
fclose($histfile);
}
/* get a list of historical versions of a node
* returns an array:
* datestamp => array:
* "datestamp" => unix timestamp date
* "user" => the user who made the modification or false if
* unavailable
* datestamp => ...
*/
function getNodeHistoryList($title) {
global $nodedir;
// blacklist the title
if (blacklistTitle($title)) return false;
$hl = array();
$handle = opendir($nodedir);
while ($entry = readdir($handle)) {
if (substr($entry, -5) == ".hist" &&
substr($entry, 0, strlen($title) + 1) == "$title.") {
// Check for whodunit
$who = false;
$fhandle = fopen($nodedir.$entry, "r");
if ($fhandle !== false) {
$junk = fgets($fhandle, 65536); // first line is content
if (!feof($fhandle)) $who = rtrim(fgets($fhandle, 65536));
fclose($fhandle);
}
$elems = explode(".", $entry);
$datestamp = $elems[count($elems)-2];
$hl[intval($datestamp)] = array(
"datestamp" => $datestamp,
"user" => $who
);
}
}
closedir($handle);
return $hl;
}
/* get a history element for a node, returns the same format as
* getNodeContents */
function getNodeHistory($title, $stamp) {
global $nodedir;
// blacklist the title
if (blacklistTitle($title)) return false;
$fname = $nodedir . $title . "." . $stamp . ".hist";
if (!file_exists($fname)) return false;
$file = fopen($fname, "r");
if ($file === false) return false;
$text = fgets($file, 65536);
$text = str_replace("^NEWLINE^", "\n", $text);
fclose($file);
return array("text" => $text, "revhistory" => array());
}
?>