-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.php
106 lines (95 loc) · 3.66 KB
/
storage.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
<?php
/**
*
*
*/
class Storage
{
// ファイル名一覧を抽出して、ファイル名からスレッド名を取り出す
public static function getThreads()
{
$items = array();
$files = glob("data/thread_*");
if ($files) foreach ($files as $filename) {
$encode_name = str_replace('thread_', '', basename($filename));
$timestamp = filemtime($filename);
$items[$timestamp] = array(
'title' => pack('H*', $encode_name),
'timestamp' => $timestamp,
'datetime' => date('Y-m-d H:i:s', $timestamp),
);
}
krsort($items);
return $items;
}
// タイトルを元にファイルを取り出しパースして返す
public static function getThread($title)
{
$filename = 'data/thread_' . strtoupper(bin2hex($title));
if (!file_exists($filename)) {
throw new RuntimeException('Contents not found.');
}
// 20M以上のファイルの場合false
$filesize = filesize($filename);
if ($filesize > (1024 * 1024 * 20)) {
throw new RuntimeException('Contents over size.');
}
$raw_data = file_get_contents($filename);
$threads = self::parse($raw_data);
return $threads;
}
// スレッドデータをパースして返す
private static function parse($raw_data)
{
$threads = array();
$remove_ids = array();
$lines = explode("\n", $raw_data);
foreach ($lines as $line) {
$parts = explode('<>', $line);
$thread = array(
'name' => '',
'timestamp' => array_shift($parts),
'id' => array_shift($parts),
'body' => '',
);
$thread['datetime'] = date('Y-m-d H:i:s', $thread['timestamp']);
while (count($parts) > 0) {
if (strpos($parts[0], 'name:') === 0) {
$thread['name'] = array_shift($parts);
} else if (strpos($parts[0], 'mail:') === 0) {
$thread['mail'] = array_shift($parts);
} else if (strpos($parts[0], 'body:') === 0) {
$thread['body'] = substr(array_shift($parts), 5);
} else if (strpos($parts[0], 'remove_stamp:') === 0) {
array_shift($parts);
} else if (strpos($parts[0], 'remove_id:') === 0) {
$remove_ids[] = str_replace('remove_id:', '', array_shift($parts));
} else if (strpos($parts[0], 'attach:') === 0) {
$thread['attach'] = substr(array_shift($parts), 7);
} else if (strpos($parts[0], 'suffix:') === 0) {
$thread['suffix'] = substr(array_shift($parts), 7);
} else if (strpos($parts[0], 'sign:') === 0) {
// implement yet
array_shift($parts);
} else if (strpos($parts[0], 'pubkey:') === 0) {
// implement yet
array_shift($parts);
} else if (strpos($parts[0], 'target:') === 0) {
// implement yet
array_shift($parts);
} else {
var_dump(__LINE__,$parts);exit;
// throw new Exception();
}
}
$threads[$thread['id']] = $thread;
}
// remove_id指定されたレスの削除
foreach ($remove_ids as $remove_id) {
if (isset($threads[$remove_id])) {
unset($threads[$remove_id]);
}
}
return $threads;
}
}