/
controller.php
2210 lines (2096 loc) · 59.3 KB
/
controller.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class controller
{
/************************************
*
* 初期化
*
************************************/
function controller()
{
global $ini_data,$post_data,$get_data,$cookie_data;
$post_name = array("number","name","mail","url","title","com","key","cookie","pre","page","action");
$get_name = array("mode","page","no","quote","type","srch");
$cookie_name = array("name","mail","url","key","cookie");
$ini_data = array();
$post_data = array();
$get_data = array();
$cookie_data = array();
$ini_data = parse_ini_file("config.ini");
foreach($ini_data as $key => $val){define($key,$val);}
foreach($post_name as $key)
{
if(isset($_POST[$key]))
{
$post_data[$key] = $_POST[$key];
}else{
$post_data[$key] = NULL;
}
}
foreach($get_name as $key)
{
if(isset($_GET[$key]))
{
$get_data[$key] = $_GET[$key];
}else{
$get_data[$key] = NULL;
}
}
foreach($cookie_name as $key)
{
if(isset($_COOKIE[$key]))
{
$cookie_data[$key] = $_COOKIE[$key];
}else{
$cookie_data[$key] = NULL;
}
}
if(!is_file(topic_log))
{
touch(topic_log);
}
if(!is_file(res_log))
{
touch(res_log);
}
if(!is_file(count_log))
{
touch(count_log);
}
if(!is_file(lock_file))
{
touch(lock_file);
}
}
/************************************
*
* トピック表示
*
************************************/
function topic_view()
{
// 初期値
global $ini_data,$get_data;
$topic = array();
$total = 0;
$j = 0;
// チェック
if(!preg_match("/^[0-9]*$/", $get_data["page"]))
{
$this -> html_error("番号が不明です");
return;
}
// ページの取得
if(!$get_data["page"])
{
$point = 0;
}else{
$point = $get_data["page"] * $ini_data["topic_num"];
}
// ログファイル及びログ最大数の取得
$topic = file($ini_data["topic_log"]);
$total = $topic ? count($topic) : 0;
// トピックの表示
$this -> html_start();
if ($total === 0) {
echo "トピックがありません\n";
return;
}
echo "<table class=\"style\">\n";
for($i=$point; $i < $total; ++$i)
{
if($j++ >= $ini_data["topic_num"])
{
break;
}
if(($i-$point)%10 == 0)
{
$this -> html_topic_head();
}
$data = explode(",", $topic[$i]);
$this -> html_topic_body($data);
}
echo "</table>\n";
// ページリンク
if($total > $ini_data["topic_num"])
{
echo "<p>";
if($get_data["page"])
{
$num = $get_data["page"]-1;
echo "<a href=\"./".bbs_name."?page={$num}\" title=\"前のページ\"><<</a> ";
}
//ダイレクトリンク
$j = $total / $ini_data["topic_num"];
for($i=0; $i <= $j; ++$i)
{
if($i != $get_data["page"])
{
echo "<a href=\"./".bbs_name."?page={$i}\" title=\"ダイレクトリンク\">[$i]</a> ";
}else{
echo "[$i] ";
}
}
if($total-$ini_data["topic_num"] > $get_data["page"]*$ini_data["topic_num"])
{
$num = $get_data["page"]+1;
echo " <a href=\"./".bbs_name."?page={$num}\" title=\"次のページ\">>></a>";
}
echo "</p>\n";
}
}
// HTML表記 テーブルヘッダー
function html_topic_head()
{
echo "<thead>\n";
echo "<tr>\n";
echo "<th></th>\n";
echo "<th>トピックタイトル</th>\n";
echo "<th>最終更新</th>\n";
echo "<th>記事数</th>\n";
echo "<th>トピック作成者</th>\n";
echo "<th>最終投稿者</th>\n";
echo "<th></th>\n";
echo "</tr>\n";
echo "</thead>\n";
}
// HTML表記 テーブルボディー
function html_topic_body($data)
{
echo "<tbody>\n";
echo "<tr>\n";
echo "<td>{$this->new_flag($data[13])}</td>\n";
echo "<td style=\"text-align:left;\"><a href=\"./".bbs_name."?mode=res&no={$data[0]}\" style=\"font-size:140%;font-weight:bold;\">{$data[3]}</a><div>{$this->substr($data[7])}</div></td>\n";
echo "<td nowrap align=\"center\">{$this->days($data[13])}</td>\n";
echo "<td>{$this->res_count($data[12])}</td>\n";
echo "<td>$data[4]</td>\n";
echo "<td>{$data[11]}</td>\n";
echo "<td></td>\n";
echo "</tr>\n";
echo "</tbody>\n";
}
// 時間の変換( 年/月/日(曜日)時:分 )
function days($time)
{
$week = array("日","月","火","水","木","金","土");
$days = date("Y/m/d", $time);
$days.= "(".$week[date("w", $time)].") ";
$days.= date("H:i", $time);
return $days;
}
// 新着の表記チェック
function new_flag($time)
{
global $ini_data;
if($time + (60*60*$ini_data["new_time"]) > time())
{
return $ini_data["new_type"];
}
return NULL;
}
// 引用コメント(75文字カット)
function substr($str)
{
$str = str_replace("<br>", "", $str);
if(strlen($str) > 75)
{
return substr($str, 0 , 75)."....";
}
return $str;
}
// 記事数の取得
function res_count($num)
{
return count(explode("<>", $num)) - 1;
}
/************************************
*
* 記事表示
*
************************************/
function res_view()
{
// 初期値
global $get_data;
$view_data = array();
$name_list = array();
$take = 0;
$take = 0;
$quote = null;
// 番号の正当性をチェック
if(!preg_match("/^[0-9]+$/", $get_data["no"]))
{
$this -> html_error("番号が不明です");
return;
}
// 指定のトピックを読み込む
$fp = fopen(topic_log, "r");
while($str = fgets($fp))
{
$data = explode(",", $str);
if($data[0] == $get_data["no"])
{
$view_data[] = $data;
$name_list[] = "<a href=\"#{$data[0]}\" title=\"記事No:{$data[0]}\">[{$data[0]}] {$data[4]}</a><br>\n";
if($data[0] == $get_data["quote"])
{
$data[7] = preg_replace("/<br>/i","\n> ", $data[7]);
$quote = "> ".$data[7];
}
break;
}
}
fclose($fp);
// トピックの記事を読み込む
$fp = fopen(res_log, "r");
while($str = fgets($fp))
{
$data = explode(",", $str);
if($data[1] == $get_data["no"] || $data[0] == $get_data["no"] )
{
if(!$take)
{
$take = 1; // 記事列を見つけた事を示す
}
if($data[0] == $get_data["quote"])
{
$data[7] = preg_replace("/<br>/i","\n> ", $data[7]);
$quote = "> ".$data[7];
}
$view_data[] = $data;
$name_list[] = "・<a href=\"#{$data[0]}\" title=\"記事No:{$data[0]}\">[{$data[0]}] {$data[4]}</a><br>\n";
}
// 早々に閉じる
else if($take)
{
break;
}
}
fclose($fp);
// 記事が見つからなかったらエラーを出し終了
if(!$view_data)
{
$this -> html_error("記事がみつかりませんでした");
return;
}
/* 記事の表示 */
$this -> html_start();
echo "<table class=\"topic\">\n";
echo "<thead>\n";
echo "<tr>\n";
echo "<th colspan=\"2\">{$view_data[0][3]}</th>\n";
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
echo "<tr>\n";
echo "<td class=\"td1\">\n";
//記事リンク
foreach($name_list as $val)
{
echo $val;
}
echo "</td>\n";
echo "<td class=\"td2\">\n";
echo "<table class=\"style\" style=\"width:100%;\">\n";
// トピック
$this -> html_topic($view_data[0]);
echo "</table>\n";
echo "<br />\n";
echo "<br />\n";
echo "<table class=\"style\" style=\"width:100%;\">\n";
// トピックの記事
$total = count($view_data);
for($i=1; $i < $total; ++$i)
{
$this -> html_res($view_data[$i]);
}
echo "</table>\n";
echo "</td>\n";
echo "</tr>\n";
echo "</tbody>\n";
echo "</table>\n";
// 返信フォーム
if(!$view_data[0][1] && max_num > $total)
{
$this -> html_form("Re:".$view_data[0][3], $quote);
}
}
// 記事HTMLスタイル
function html_topic($data)
{
$this -> sub_object($data);
echo "<thead>\n";
echo "<tr>\n";
echo "<th style=\"text-align:left;border-right-width:0pt;\"><a name=\"{$data[0]}\"></a>No:{$data[0]} Name: <span style=\"font-size:110%;\">{$data[4]}</span>{$data[5]}</th>\n";
echo "<th style=\"text-align:right;border-left-width:0pt;\">\n";
echo "[<a href=\"#form\">返信</a>]\n";
echo "[<a href=\"./".bbs_name."?mode=res&no={$data[0]}"e={$data[0]}#form\">引用</a>]\n";
echo "[<a href=\"./".bbs_name."?mode=key&no={$data[0]}\">編集</a>]</th>\n";
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
echo "<tr>\n";
echo "<td colspan=\"2\" style=\"text-align:left;\">\n";
echo "<h2>Title: {$data[3]}</h2>\n";
echo "<p>\n";
echo "{$data[7]}";
echo "</p>\n";
echo "<div style=\"text-align:right;\">\n";
echo "{$this->days($data[2])}";
echo "</div>\n";
echo "</td>\n";
echo "</tr>\n";
echo "</tbody>\n";
}
function html_res($data)
{
$this -> sub_object($data);
echo "<thead>\n";
echo "<tr>\n";
echo "<th style=\"text-align:left;border-right-width:0pt;\"><a name=\"{$data[0]}\"></a>No:{$data[0]} Name: <span style=\"font-size:110%;\">{$data[4]}</span>{$data[5]}</th>\n";
echo "<th style=\"text-align:right;border-left-width:0pt;\">\n";
echo "[<a href=\"./".bbs_name."?mode=res&no={$data[1]}"e={$data[0]}#form\">引用</a>]\n";
echo "[<a href=\"./".bbs_name."?mode=key&no={$data[0]}\">編集</a>]</th>\n";
echo "</tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
echo "<tr>\n";
echo "<td colspan=\"2\" style=\"text-align:left;border-width:0pt;\">\n";
echo "<h2>Title: {$data[3]}</h2>\n";
echo "<p>\n";
echo "{$data[7]}";
echo "</p>\n";
echo "<div style=\"text-align:right;\">\n";
echo "{$this->days($data[2])}";
echo "</div>\n";
echo "</td>\n";
echo "</tr>\n";
echo "</tbody>\n";
}
// HTML表記 エラー
function html_error($str)
{
$this -> html_start();
echo "<b style=\"color:red\">{$str}</b><br>\n";
}
// URL・Mailがあればリンク
function sub_object(&$data)
{
if($data[5])
{
$data[5] = " [<a href=\"http://{$data[5]}\">URL</a>]";
}
if($data[6])
{
$data[4] = "<a href=\"mailto:{$data[6]}\">{$data[4]}</a>";
}
}
/************************************
*
* 投稿フォーム
*
************************************/
function html_form($title = NULL, $quote = NULL)
{
global $ini_data,$get_data,$cookie_data;
if(!$get_data["no"])
{
$get_data["no"] = 0;
}
echo "<a name=\"form\"></a>\n";
echo "<form method=\"POST\" action=\"./".bbs_name."?mode=write\" onsubmit=\"return submitForm()\">\n";
echo "<table class=\"form\">\n";
echo "<tbody>\n";
echo "<input type=\"hidden\" name=\"number\" value=\"{$get_data["no"]}\">\n";
echo "<tr>\n";
echo "<th>名前</th>\n";
echo "<td><input type=\"text\" name=\"name\" size=\"50\" maxlength=\"{$ini_data["name_len"]}\" value=\"{$cookie_data["name"]}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>E-Mail</th>\n";
echo "<td><input type=\"text\" name=\"mail\" size=\"50\" maxlength=\"{$ini_data["mail_len"]}\" value=\"{$cookie_data["mail"]}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>URL</th>\n";
echo "<td><input type=\"text\" name=\"url\" size=\"50\" maxlength=\"{$ini_data["url_len"]}\" value=\"http://{$cookie_data["url"]}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>タイトル</th>\n";
echo "<td><input type=\"text\" name=\"title\" size=\"50\" maxlength=\"{$ini_data["title_len"]}\" value=\"{$title}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>コメント</th>\n";
echo "<td><textarea id=\"com\" name=\"com\" cols=60 rows=12>$quote</textarea></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>認証キー</th>\n";
echo "<td><input type=\"password\" name=\"key\" size=\"10\" maxlength=\"8\" value=\"{$cookie_data["key"]}\"> <small>(最大8文字)</small></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th></th>\n";
echo "<td><input type=\"checkbox\" name=\"pre\" value=\"on\"><small>プレビュー</small>\n";
echo "<input type=\"checkbox\" name=\"cookie\" value=\"checked\" {$cookie_data["cookie"]}><small>クッキー保存</small>\n";
echo " <input type=\"submit\" value=\"書き込む\"></td>\n";
echo "</tr>\n";
echo "</tbody>\n";
echo "</table>\n";
echo "</form>\n";
}
//プレビュー用
function html_form_post()
{
global $ini_data,$post_data;
echo "<form method=\"POST\" action=\"./".bbs_name."?mode=write\" onsubmit=\"return submitForm()\">\n";
echo "<table class=\"form\">\n";
echo "<tbody>\n";
echo "<input type=\"hidden\" name=\"number\" value=\"{$post_data["number"]}\">\n";
echo "<tr>\n";
echo "<th>名前</th>\n";
echo "<td><input type=\"text\" name=\"name\" size=\"50\" maxlength=\"{$ini_data["name_len"]}\" value=\"{$post_data["name"]}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>E-Mail</th>\n";
echo "<td><input type=\"text\" name=\"mail\" size=\"50\" maxlength=\"{$ini_data["mail_len"]}\" value=\"{$post_data["mail"]}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>URL</th>\n";
echo "<td><input type=\"text\" name=\"url\" size=\"50\" maxlength=\"{$ini_data["url_len"]}\" value=\"{$post_data["url"]}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>タイトル</th>\n";
echo "<td><input type=\"text\" name=\"title\" size=\"50\" maxlength=\"{$ini_data["title_len"]}\" value=\"{$post_data["title"]}\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>コメント</th>\n";
echo "<td><textarea id=\"com\" name=\"com\" cols=60 rows=12>{$post_data["com"]}</textarea></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th>認証キー</th>\n";
echo "<td><input type=\"password\" name=\"key\" size=\"10\" maxlength=\"8\" value=\"{$post_data["key"]}\"> <small>(最大8文字)</small></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<th></th>\n";
echo "<td><input type=\"checkbox\" name=\"pre\" value=\"on\"><small>プレビュー</small>\n";
echo "<input type=\"checkbox\" name=\"cookie\" value=\"checked\" {$post_data["cookie"]}><small>クッキー保存</small>\n";
echo " <input type=\"submit\" value=\"書き込む\"></td>\n";
echo "</tr>\n";
echo "</tbody>\n";
echo "</table>\n";
echo "</form>\n";
}
/************************************
*
* ログ書き込み
*
************************************/
function write_controll()
{
// 初期化
global $post_data;
// フォームデータのチェックおよび加工
$err_mge = $this -> form_check();
$str = $this -> from_convert($post_data);
$data = explode(",", $str);
// エラーもしくはプレビューにチェックがあれば終了
if($err_mge)
{
$data[0] = "エラー";
$this -> html_error($err_mge);
echo "<table class=\"style\">\n";
$this -> html_topic($data);
echo "</table>\n";
$this -> html_form_post();
return;
}else if($post_data["pre"]){
$this -> html_start();
$data[0] = "プレビュー";
echo "<table class=\"style\">\n";
$this -> html_topic($data);
echo "</table>\n";
$this -> html_form_post();
return;
}
/* 記事の書き込み */
if($post_data["number"])
{
$this -> write_res($str);
if($post_data["cookie"])
{
$this -> set_cookie();
}
}else{
$this -> write_topic($str);
if($post_data["cookie"])
{
$this -> set_cookie();
}
}
$this -> article_number_up(0); //記事番号のカウントアップ
header("Location:".bbs_dir.bbs_name);
}
// フォームデータの正当性のチェック
function form_check()
{
global $post_data;
if(!$post_data["title"])
return "タイトル未記入";
else if(!$post_data["name"])
return "名前未記入";
else if(!$post_data["com"])
return "コメント未記入";
else if(strlen($post_data["title"]) > title_len)
return "タイトルが長すぎます";
else if(strlen($post_data["name"]) > name_len)
return "名前が長すぎます";
else if(strlen($post_data["url"]) > url_len)
return "URLが長すぎます";
else if(strlen($post_data["mail"]) > mail_len)
return "E-mailが長すぎます";
else if(strlen($post_data["com"]) > comment_len)
return "コメントが長すぎます";
//else if($post_data["name"] === master_name && $post_data["key"] !== master_pass)
// return "管理人名は使えません";
else if(!preg_match("/^[0-9]+$/", $post_data["number"]))
return "記事番号が不正です";
else if(!preg_match("/^([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})$|^$|^$/i", $post_data["mail"]))
return "E-Mailが不正です";
else if(!preg_match("/^http:\/\/|^$/i", $post_data["url"]))
return "URLが不正です";
return NULL;
}
// フォームデータの加工
function from_convert($post_data)
{
foreach($post_data as $key => $val){$post_data[$key] = $this->str_convert($val);}
$this -> convert($post_data);
$num = $this->article_number_get(0);
return "{$num},{$post_data["number"]},".time().",{$post_data["title"]},{$post_data["name"]},{$post_data["url"]},{$post_data["mail"]},{$post_data["com"]},{$post_data["key"]},".gethostbyaddr($_SERVER["REMOTE_ADDR"]).",{$_SERVER["REMOTE_ADDR"]},-,<>,,,,\n";
}
// 変換
function convert(&$post_data)
{
$post_data["url"] = str_replace( "http://", "", $post_data["url"]);
$post_data["mail"] = str_replace( "@", "@", $post_data["mail"]);
if(url_flag)
{
$post_data["com"] = preg_replace( "/<a .+>(.+)<\/a>/i", "$1", $post_data["com"]);
$post_data["com"] = preg_replace( "/(http:\/\/[\w\.\/\-=&%?;#]+)/i", "<a href=\"$1\" target=\"_blank\">$1</a>", $post_data["com"]);
}
/*if(tag_member)
{
$post_data["com"] = preg_replace( "/<(".tag_member.")>(.*?)<(\/".tag_member.")>/i","<$1>$2<$3>",$post_data["com"]);
}*/
$post_data["key"] = crypt($post_data["key"]);
}
// フォームデータの加工2
function str_convert($str)
{
$str = str_replace("\\", "\\\\", $str);
$str = str_replace( ",", ",", $str);
$str = stripslashes($str);
$str = htmlspecialchars($str, ENT_QUOTES);
$str = preg_replace("/(\n|\r|\r\n)/","<br>", $str);
return $str;
}
// 記事番号の取得
function article_number_get($i)
{
$fp = fopen(count_log, "r") or die($this->html_error("ファイルが開けません"));
flock($fp,LOCK_EX);
$str = fgets($fp);
fclose($fp);
$num = explode(",", $str);
++$num[$i];
return $num[$i];
}
// 記事番号カウントアップ
function article_number_up($i)
{
$fp = fopen(count_log, "r") or die($this->html_error("ファイルが開けません"));
flock($fp,LOCK_EX);
$str = fgets($fp);
fclose($fp);
$num = explode(",", $str);
++$num[$i];
$fp = fopen(count_log, "w") or die($this->html_error("ファイルが開けません"));
flock($fp,LOCK_EX);
fwrite($fp,"{$num[0]},{$num[1]},{$num[2]},{$num[3]},{$num[4]},\n");
fclose($fp);
}
// トピックログの書き込み
function write_topic($str)
{
$writ_data = explode(",", $str);
$writ_data[13] = time(); // 最終更新日の追加
$str = implode(",", $writ_data);
$fl = fopen(lock_file, "w") or die($this->html_error("ファイルが開けません"));
flock($fl,LOCK_EX);
$topic_data = file(topic_log);
array_unshift($topic_data, $str);
if(count($topic_data) > max_log)
{
$this -> topic_make($topic_data);
print "実行完了";
}
$fp = fopen(topic_log, "w");
foreach($topic_data as $val)
{
fwrite($fp, $val);
}
fclose($fp);
fclose($fl);
}
// 過去ログ作成
function topic_make(&$topic_data)
{
$topic_log_data = array();
$res_log_data = array();
$data = array();
$article = $this->article_number_get(1);
for($i=0; $i<max_memory; ++$i)
{
$topic_log_data[$i] = array_pop($topic_data);
preg_match("/^([0-9]+),/",$topic_log_data[$i],$num);
$data[$num[1]] = $num[1];
}
$fp = fopen(res_log, "r"); //レスの切り抜き
while($buf = fgets($fp))
{
$str = explode(",", $buf);
if(isset($data[$str[1]]) && $data[$str[1]] == $str[1])
{
$res_log_data[] = $buf; //過去ログへ
}else{
$res_data[] = $buf;
}
}
fclose($fp);
$fp = fopen(res_log, "w"); //レスログ
foreach($res_data as $val)
{
fwrite($fp, $val);
}
fclose($fp);
$fp = fopen(kako_dir."/res_{$article}.csv", "w"); //過去レスログ
foreach($res_log_data as $val)
{
fwrite($fp, $val);
}
fclose($fp);
$fp = fopen(kako_dir."/topic_{$article}.csv", "w"); //過去トピックログ
foreach($topic_log_data as $val)
{
fwrite($fp, $val);
}
fclose($fp);
$this -> article_number_up(1);
}
// 返信ログ書き込み
function write_res($str)
{
//初期化
global $post_data;
$topic_data = array();
$res_data = array();
$writ_data = explode(",", $str);
$take = 0;
$fl = fopen(lock_file, "w") or die($this->html_error("ファイルが開けません"));
flock($fl,LOCK_EX);
/* トピックの処理 */
$fp = fopen(topic_log, "r");
while($buf = fgets($fp))
{
$data = explode(",", $buf);
if($data[0] != $post_data["number"])
{
$topic_data[] = $buf;
}else{
if($this->res_count($data[12]) >= max_num)
{
fclose($fp);
$this->html_error("記事が最大数".max_num."を超えています");
return;
}
$take = 1; // トピックを見つけた意図
$data[11] = $post_data["name"];
$data[12].= "{$writ_data[0]}<>";
$data[13] = time();
$watch_data = implode(",", $data);
}
}
fclose($fp);
if(!$take)
{
$this -> html_error("トピックが存在しません");
return;
}
array_unshift($topic_data, $watch_data); //記事をトップに移動
$fp = fopen(topic_log, "w");
foreach($topic_data as $val)
{
fwrite($fp, $val);
}
fclose($fp);
/* 記事の処理 */
$take = 0;
$fp = fopen(res_log, "r");
while($buf = fgets($fp))
{
$match = preg_match("/^[0-9]+,".$post_data["number"].",/", $buf);
if(!$take && $match)
{
$take = 1; // 記事列を見つけた事を示す
}
if($take && !$match) // 記事列の最後尾に追加
{
$res_data[] = $str;
$str = NULL;
$take = 0;
}
$res_data[] = $buf;
}
if($str && $take) //記事列が見つからなかった場合
{
$res_data[] = $str;
$str = NULL;
$take = 0;
}
if($str)
{
array_unshift($res_data, $str);
}
fclose($fp);
$fp = fopen(res_log, "w");
foreach($res_data as $val)
{
fwrite($fp, $val);
}
fclose($fp);
fclose($fl);
}
// クッキーの作成
function set_cookie()
{
global $post_data,$cookie_data;
$post_data["url"] = str_replace( "http://", "", $post_data["url"]);
foreach($cookie_data as $key => $val)
{
if($post_data[$key])
{
setcookie($key ,$post_data[$key] ,time() + 60*60*24*30);
}
}
}
/************************************
*
* 記事編集
*
************************************/
function html_edit_key()
{
global $get_data,$cookie_data;
echo "<form method=\"POST\" action=\"./".bbs_name."?mode=edit\" name=\"form\">\n";
echo "<table class=\"form\">\n";
echo "<tbody>\n";
echo "<input type=\"hidden\" name=\"number\" value=\"{$get_data["no"]}\">\n";
echo "<tr>\n";
echo "<th style=\"text-align:center;\">記事No:{$get_data["no"]}</th>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td style=\"text-align:center;\">パスワード入力 \n";
echo "<select name=\"action\">\n";
echo "<option value=\"edit\" selected>編集</option>\n";
echo "<option value=\"del\">削除</option>\n";
echo "</select>\n";
echo "<input type=\"password\" name=\"key\" value=\"{$cookie_data["key"]}\">\n";
echo "<input type=\"submit\" value=\"送信\"></td>\n";
echo "</tr>\n";
echo "</tbody>\n";
echo "</table>\n";
echo "</form>\n";
}
function edit_controll()
{
global $post_data;
if($err_mge = $this->check_edit_form())
{
$this -> html_error($err_mge);
}else{
$edit_log = NULL;
/* 編集する記事の検索 */
$fp = fopen(topic_log, "r");
while($str = fgets($fp))
{
$data = explode(",", $str);
if($data[0] == $post_data["number"])
{
$edit_log = topic_log;
break;
}
}
fclose($fp);
if(!$edit_log) // topic_logで見つからければres_logを探す
{
$fp = fopen(res_log, "r");
while($str = fgets($fp))
{
$data = explode(",", $str);
if($data[0] == $post_data["number"])
{
$edit_log = res_log;
break;
}
}
fclose($fp);
}
// 記事が見つからなかったら
if(!$edit_log)
{
$this -> html_error("指定された記事が存在ません");
return;
}
if(crypt($post_data["key"], $data[8]) != $data[8] && $post_data["key"] != master_pass )
{
$this -> html_error("パスワードが違います");
return;
}
/* 編集 */
if($post_data["action"] == "edit")
{
$this -> encode($data);
$this -> html_start();
$this -> html_form_edit($data);
}
// 削除
else if($post_data["action"] == "del")
{
$fl = fopen(lock_file, "w") or die($this->html_error("ファイルが開けません"));
$fp = fopen($edit_log, "r");
while($str = fgets($fp))
{
$data = explode(",", $str);
if($data[0] != $post_data["number"])
{
$log_data[] = $str;
}else{
$data[7] = del_mge;
$data[8] = NULL;
$log_data[] = implode(",", $data);
}
}
fclose($fp);
$fp = fopen($edit_log, "w");
foreach($log_data as $val)
{
fwrite($fp, $val);
}
fclose($fp);
fclose($fl);
header("Location:".bbs_dir.bbs_name);
}
}
}
function edit_write()
{
global $post_data;
if($err_mge = $this->check_edit_form())
{
$this -> html_error($err_mge);
}else{
$edit_log = NULL;
/* 編集する記事の検索 */
$fp = fopen(topic_log, "r");
while($str = fgets($fp))
{
$data = explode(",", $str);
if($data[0] == $post_data["number"])
{
$edit_log = topic_log;
break;
}
}
fclose($fp);
if(!$edit_log)
{
$fp = fopen(res_log, "r");
while($str = fgets($fp))
{
$data = explode(",", $str);
if($data[0] == $post_data["number"])
{
$edit_log = res_log;
break;
}
}
fclose($fp);
}
if(!$edit_log)
{
$this -> html_error("指定された記事が存在ません");
return;
}
if(crypt($post_data["key"], $data[8]) != $data[8] && $post_data["key"] != master_pass )
{
$this -> html_error("パスワードが違います");