-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatfile.inc.php
331 lines (277 loc) · 15.6 KB
/
datfile.inc.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
<?php
function write_dat_file() {
global $locations, $config, $insert_id, $message_author, $message_subject, $debug_strings, $mysqli_link, $t;
// Check for existing $datfile.lock
if (file_exists($locations['datfile'] . '.lock')) {
for ($i = 0; $i < 3; $i++) {
sleep(1);
if (!file_exists($locations['datfile'] . '.lock'))
break;
}
}
// don't allow user to abort
ignore_user_abort(true);
// create a lock
touch($locations['datfile'] . '.lock');
// increment the counter
$count = 0;
if (file_exists($locations['counter'])) {
// read old count
$fp = fopen($locations['counter'],'r');
$count = fread($fp, filesize($locations['counter']));
fclose($fp);
}
// reset the counter if it's mtime is not today (new day)
if (file_exists($locations['counter']) && date('m d y') != date('m d y', filemtime($locations['counter'])))
$count = 0;
// write count + 1
$fp = fopen($locations['counter'],'w');
fwrite($fp,$count + 1);
fclose($fp);
// update the last post
$fp = fopen($locations['lastpost'],'w');
fwrite($fp,"$insert_id\n$t\n".alter_username(deescape($message_author))."\n".deescape($message_subject)."\n");
fclose($fp);
// create the forum file
$fp = fopen($locations['datfile'],'w') or error('Unable to open ' . $locations['datfile'] . ' for writing',$config['admin_email'],'Unable to open ' . $locations['datfile'] . ' for writing');
// create the forum_lite file
$fp_lite = fopen($locations['datfile_lite'],'w') or error('Unable to open ' . $locations['datfile_lite'] . ' for writing',$config['admin_email'],'Unable to open ' . $locations['datfile_lite'] . ' for writing');
// create the forum_json file
//$fp_json = fopen($locations['jsonfile'],'w') or error('Unable to open ' . $locations['jsonfile'] . ' for writing',$config['admin_email'],'Unable to open ' . $locations['jsonfile'] . ' for writing');
$fp_banned = fopen($locations['datfile_banned'], 'w') or error('Unable to open ' . $locations['datfile_banned'] . ' for writing', $config['admin_email'], 'Unable to open ' . $locations['datfile_banned'] . ' for writing');
$fp_lite_banned = fopen($locations['datfile_lite_banned'],'w') or error('Unable to open ' . $locations['datfile_lite_banned'] . ' for writing',$config['admin_email'],'Unable to open ' . $locations['datfile_lite_banned'] . ' for writing');
// re-setup the table rotation scheme
if ($config['rotate_tables'] == 'daily')
$t = date('mdy');
elseif ($config['rotate_tables'] == 'weekly')
$t = strftime('%y%W');
elseif ($config['rotate_tables'] == 'monthly')
$t = date('my');
elseif ($config['rotate_tables'] == 'yearly')
$t = date('Y');
else
$t = date('mdy');
// reset the table name
$tablename = ($config['rotate_tables'] ? $locations['posts_table'].'_'.$t : $locations['posts_table']);
// query for the rows to output
$query = 'select ' . $tablename . '.id, ' . $tablename . '.parent, ' . $tablename . '.thread, ' . $tablename . '.message_author, ' . $tablename . '.message_subject, ' .
'date_format(' . $tablename . '.date,"%m/%d/%Y - %l:%i:%s %p") as date, date_format(' . $tablename . '.date, "%l:%i:%s %p") as date_sm, "' . $t . '" as t, ' .
$tablename . '.link, ' . $tablename . '.image, ' . $tablename . '.video, ifnull(' . $tablename . '.score, "null") as score, ifnull(' . $tablename . '.type, "null") as type, ' . $tablename . '.banned, ' .
'case when ' . $tablename . '.message_body = "" then "n" else "y" end as body, ' . $tablename . '.message_body ' .
'from ' . $tablename . ' ' .
'where unix_timestamp(' . $tablename . '.date) > (unix_timestamp(now()) - ' . $config['displaytime'] . ') ' .
(!$config['rotate_tables'] ? ' and t = ' . $t . ' ' : '') .
'order by ' . $tablename . '.parent desc, ' . $tablename . '.thread asc limit ' . $config['maxrows'];
// see if we need to bond another table
if (($config['rotate_tables'] == 'daily' && date('mdy',time() - $config['displaytime']) != date('mdy')) ||
($config['rotate_tables'] == 'weekly' && strftime('%y%W',time() - $config['displaytime']) != strftime('%y%W')) ||
($config['rotate_tables'] == 'monthly' && date('my',time() - $config['displaytime']) != date('my')) ||
($config['rotate_tables'] == 'yearly' && date('Y',time() - $config['displaytime']) != date('Y')) ||
(date('mdy',time() - $config['displaytime']) != date('mdy'))) {
if ($config['rotate_tables'] == 'daily')
$t = date('mdy', time() - $config['displaytime']);
elseif ($config['rotate_tables'] == 'weekly')
$t = strftime('%y%W', time() - $config['displaytime']);
elseif ($config['rotate_tables'] == 'monthly')
$t = date('my', time() - $config['displaytime']);
elseif ($config['rotate_tables'] == 'yearly')
$t = date('Y', time() - $config['displaytime']);
else
$t = date('mdy', time() - $config['displaytime']);
$tablename = ($config['rotate_tables'] ? $locations['posts_table'].'_'.$t : $locations['posts_table']);
$query = '(' . $query . ') union (' .
'select ' . $tablename . '.id,' . $tablename . '.parent,' . $tablename . '.thread,' . $tablename . '.message_author,' . $tablename . '.message_subject, ' .
'date_format(' . $tablename . '.date,"%m/%d/%Y - %l:%i:%s %p") as date, date_format(' . $tablename . '.date, "%l:%i:%s %p") as date_sm, "' . $t . '" as t, ' .
$tablename . '.link, ' . $tablename . '.image, ' . $tablename . '.video, ifnull(' . $tablename . '.score, "null") as score, ifnull(' . $tablename . '.type, "null") as type, ' . $tablename . '.banned, ' .
'case when ' . $tablename . '.message_body = "" then "n" else "y" end as body, ' . $tablename . '.message_body ' .
'from ' . $tablename . ' ' .
'where unix_timestamp(' . $tablename . '.date) > (unix_timestamp(now()) - ' . $config['displaytime'] . ') ' .
(!$config['rotate_tables'] ? ' and t = ' . $t . ' ' : '') .
'order by ' . $tablename . '.parent desc,' . $tablename . '.thread asc limit ' . $config['maxrows'] . ' ' .
') order by t desc, parent desc, thread asc limit ' . $config['maxrows'];
}
array_push($debug_strings, $query);
$msc = microtime(true);
$results = mysqli_query($mysqli_link, $query) or error($config['db_errstr'],$config['admin_email'],$query."\n".mysqli_error());
$msc = microtime(true) - $msc;
array_push($debug_strings, " took " . ($msc * 1000) . " ms<br />");
if (mysqli_num_rows($results) == 0)
error('',$config['admin_email'],'ERROR: No rows to output');
$lastthread = array();
$lastnormalthread = null;
$lastlitethread = null;
// preset thread count
$threads = 0;
//$json = array();
$sizes = array();
$sizes[0] = 'xx-small';
$sizes[1] = 'x-small';
$sizes[2] = 'small';
$sizes[3] = 'medium';
$sizes[4] = 'large';
$sizes[5] = 'x-large';
$sizes[6] = 'xx-large';
$msc = microtime(true);
while ($posts = mysqli_fetch_array($results)) {
// test to see if current row is a new thread, increment $threads if so
if (array_shift(explode('.',$posts['thread'])) != $lastthread[0]) {
// ignore rows that are new threads and are not the parent
if (count(explode('.',$posts['thread'])) != 1)
continue;
else
$threads++;
// don't feed the trolls
$parent_author = null;
}
// find difference between these arrays, returns an array
if ($threads <= $config['maxthreads']) {
$thread_count = count(array_diff($lastthread,explode('.',$posts['thread'])));
fputs($fp,str_repeat("</li></ul>",$thread_count));
fputs($fp_banned,str_repeat("</li></ul>",$thread_count));
}
if ($threads <= $config['maxthreadslite']) {
fputs($fp_lite,str_repeat("</li></ul>",count(array_diff($lastthread,explode('.',$posts['thread'])))));
fputs($fp_lite_banned,str_repeat("</li></ul>",count(array_diff($lastthread,explode('.',$posts['thread'])))));
}
$lastthread = explode('.',$posts['thread']);
if ($threads == $config['maxthreadslite'])
$lastlitethread = $lastthread;
if ($threads == $config['maxthreads'])
$lastnormalthread = $lastthread;
// build the rate string (i.e. "Warning - Gross")
$display_rate = null;
if ($posts['score'] != 'null' || ($posts['type'] != 'null' && $posts['type'] != '')) {
switch ($posts['type']) {
case 'warn-g':
$posts['type'] = "<b style='color: red; font-size: larger'>Warning</b> - Gross";
break;
case 'warn-n':
$posts['type'] = "<b style='color: red; font-size: larger'>Warning</b> - Nudity";
break;
case 'nsfw':
$posts['type'] = "<b style='color: red; font-size: larger'>NSFW</b>";
break;
}
$display_rate = " - <span style='font-size: smaller'>( ";
if ($posts['score'] != 'null') $display_rate .= $posts['score'];
if ($posts['score'] != 'null' && $posts['type'] != 'null' && $posts['type'] != '') $display_rate .= ', ' . ucfirst($posts['type']);
if ($posts['score'] == 'null') $display_rate .= ucfirst($posts['type']);
$display_rate .= ' )</span>';
}
if ($config['always_display_date_full'])
$display_date = ' - ' . $posts['date'];
elseif ($config['always_display_date_small'])
$display_date = ' - ' . $posts['date_sm'];
else
{
// only show the date for the first post of the thread
if ($posts['id'] == $posts['parent']) $display_date = ' - ' . $posts['date_sm'];
else $display_date = null;
}
if ($threads <= $config['maxthreads']) {
if ($posts['banned'] == 'y')
{
fputs($fp, '<ul style="display: none"><li>');
fputs($fp_banned,
'<ul><li><a href="?d=' . $posts['id'] . '&t=' . $posts['t'] . '" title="' . $posts['date'] . '">' . $posts['message_subject'] . '</a> ' .
options($posts['link'],$posts['video'],$posts['image'],$posts['body'],$posts['message_author']) .
' - <b>' . $posts['message_author'] . '</b>' . $display_date . $display_rate
);
}
else
{
fputs($fp,
'<ul><li><a href="?d=' . $posts['id'] . '&t=' . $posts['t'] . '" title="' . $posts['date'] . '">' . $posts['message_subject'] . '</a> ' .
options($posts['link'],$posts['video'],$posts['image'],$posts['body'],$posts['message_author']) .
' - <b>' . $posts['message_author'] . '</b>' . $display_date . $display_rate
);
fputs($fp_banned,
'<ul><li><a href="?d=' . $posts['id'] . '&t=' . $posts['t'] . '" title="' . $posts['date'] . '">' . $posts['message_subject'] . '</a> ' .
options($posts['link'],$posts['video'],$posts['image'],$posts['body'],$posts['message_author']) .
' - <b>' . $posts['message_author'] . '</b>' . $display_date . $display_rate
);
}
$thispost = array(
't' => $posts['t'],
'id' => $posts['id'],
'thread' => $posts['thread'],
'parent' => $posts['parent'],
'message_author' => $posts['message_author'],
'message_author_email' => $posts['message_author_email'],
'message_subject' => $posts['message_subject'],
'date' => $posts['date'],
'link' => $posts['link'],
'video' => $posts['video'],
'image' => $posts['image'],
'message_body' => $posts['message_body'],
);
/* if ($posts['link'] == 'y') {
$query = 'select link_url, link_title from ' . $locations['links_table'] . ' where t = "' . $posts['t'] . '" and id = "' . $posts['id'] . '"';
$jsonlinkresult = mysqli_query($mysqli_link, $query);
$thislinks = array();
while ($thislink = mysqli_fetch_array($jsonlinkresult)) {
array_push($thislinks, array('link_url' => $thislink['link_url'], 'link_title' => $thislink['link_title']));
}
array_push($thispost, array('links' => $thislinks));
}
if ($posts['image'] == 'y') {
$query = 'select image_url from ' . $locations['images_table'] . ' where t = "' . $posts['t'] . '" and id = "' . $posts['id'] . '"';
$jsonimageresult = mysqli_query($mysqli_link, $query);
$thisimages = array();
while ($thisimage = mysqli_fetch_array($jsonimageresult)) {
array_push($thisimages, array('image_url' => $thisimage['image_url']));
}
array_push($thispost, array('images' => $thisimages));
}
array_push($json, $thispost);
*/
}
if ($threads <= $config['maxthreadslite']) {
if ($posts['banned'] == 'y')
{
fputs($fp_lite, '<ul style="display: none"><li>');
fputs($fp_lite_banned,
'<ul><li><a href="?d=' . $posts['id'] . '&t=' . $posts['t'] . '">' . $posts['message_subject'] . '</a> ' .
options($posts['link'],$posts['video'],$posts['image'],$posts['body'],$posts['message_author']) .
' - <b>' . $posts['message_author'] . '</b>' . $display_date . $display_rate
);
}
else
{
fputs($fp_lite,
'<ul><li><a href="?d=' . $posts['id'] . '&t=' . $posts['t'] . '">' . $posts['message_subject'] . '</a> ' .
options($posts['link'],$posts['video'],$posts['image'],$posts['body'],$posts['message_author']) .
' - <b>' . $posts['message_author'] . '</b>' . $display_date . $display_rate
);
fputs($fp_lite_banned,
'<ul><li><a href="?d=' . $posts['id'] . '&t=' . $posts['t'] . '">' . $posts['message_subject'] . '</a> ' .
options($posts['link'],$posts['video'],$posts['image'],$posts['body'],$posts['message_author']) .
' - <b>' . $posts['message_author'] . '</b>' . $display_date . $display_rate
);
}
}
}
//fputs($fp_json, json_encode($json));
if (is_array($lastnormalthread)) {
fputs($fp,str_repeat('</li></ul>',count($lastnormalthread)));
fputs($fp_banned,str_repeat('</li></ul>',count($lastnormalthread)));
} else {
fputs($fp,str_repeat('</li></ul>',count($lastthread)));
fputs($fp_banned,str_repeat('</li></ul>',count($lastthread)));
}
if (is_array($lastlitethread)) {
fputs($fp_lite,str_repeat('</li></ul>',count($lastlitethread)));
fputs($fp_lite_banned,str_repeat('</li></ul>',count($lastlitethread)));
} else {
fputs($fp_lite,str_repeat('</li></ul>',count($lastthread)));
fputs($fp_lite_banned,str_repeat('</li></ul>',count($lastthread)));
}
// close the forum file
fclose($fp);
fclose($fp_banned);
fclose($fp_lite);
fclose($fp_lite_banned);
//fclose($fp_json);
// remove the lock
unlink($locations['datfile'] . '.lock');
}