1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Localdisk\Monar; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
6
|
|
|
use GuzzleHttp\Cookie\SetCookie; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Localdisk\Monar\Exceptions\MonarException; |
9
|
|
|
|
10
|
|
|
class TwoChanDriver extends AbstractDriver |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $encoding = 'Shift_JIS'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* get threads. |
19
|
|
|
* |
20
|
|
|
* @return \Illuminate\Support\Collection |
21
|
|
|
* @throws MonarException |
22
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
23
|
|
|
*/ |
24
|
|
|
public function threads(): Collection |
25
|
|
|
{ |
26
|
|
|
$body = $this->request('GET', $this->threadsUrl()); |
27
|
|
|
|
28
|
|
|
return $this->parseThreadsCollection($body); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* get messages. |
33
|
|
|
* |
34
|
|
|
* @param int|null $start |
35
|
|
|
* @param int|null $end |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Support\Collection |
38
|
|
|
* @throws MonarException |
39
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
40
|
|
|
*/ |
41
|
|
|
public function messages(?int $start = null, ?int $end = null): Collection |
42
|
|
|
{ |
43
|
|
|
$body = $this->request('GET', $this->messagesUrl($start, $end)); |
44
|
|
|
|
45
|
|
|
return $this->parseDatCollection($body, $end); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* post message. |
50
|
|
|
* |
51
|
|
|
* @param string $name |
52
|
|
|
* @param string $email |
53
|
|
|
* @param string|null $text |
54
|
|
|
* |
55
|
|
|
* @return mixed|string |
56
|
|
|
* @throws MonarException |
57
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
58
|
|
|
*/ |
59
|
|
|
public function post(string $name = '', string $email = 'sage', ?string $text = null) |
60
|
|
|
{ |
61
|
|
|
mb_convert_variables('Shift_JIS', 'UTF-8', $name, $email, $text); |
62
|
|
|
$params = [ |
63
|
|
|
'bbs' => $this->board, |
64
|
|
|
'key' => $this->thread, |
65
|
|
|
'time' => time(), |
66
|
|
|
'FROM' => $name, |
67
|
|
|
'mail' => $email, |
68
|
|
|
'MESSAGE' => $text, |
69
|
|
|
'submit' => $this->encode('書き込む', 'Shift_JIS', 'UTF-8'), |
70
|
|
|
]; |
71
|
|
|
$headers = [ |
72
|
|
|
'Host' => parse_url($this->url, PHP_URL_HOST), |
73
|
|
|
'Referer' => $this->url, |
74
|
|
|
'User-Agent' => 'Monazilla/1.00', |
75
|
|
|
]; |
76
|
|
|
$cookie = new CookieJar(); |
77
|
|
|
$response = $this->request('POST', $this->postUrl(), [ |
78
|
|
|
'headers' => $headers, |
79
|
|
|
'form_params' => $params, |
80
|
|
|
'cookies' => $cookie, |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
if ($this->confirm($response)) { |
84
|
|
|
$cookie->setCookie(SetCookie::fromString('IS_COOKIE=1')); |
85
|
|
|
$response = $this->request('POST', $this->postUrl(), [ |
86
|
|
|
'headers' => $headers, |
87
|
|
|
'form_params' => $params, |
88
|
|
|
'cookies' => $cookie, |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* parse url. |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function parse(): void |
101
|
|
|
{ |
102
|
|
|
$parsed = parse_url($this->url); |
103
|
|
|
$paths = $this->renewArray(explode('/', parse_url($this->url, PHP_URL_PATH))); |
104
|
|
|
|
105
|
|
|
$this->baseUrl = $parsed['scheme'].'://'.$parsed['host']; |
106
|
|
|
$this->category = ''; |
107
|
|
|
|
108
|
|
|
if (\count($paths) === 1) { |
109
|
|
|
$this->board = $paths[0]; |
110
|
|
|
$this->thread = ''; |
111
|
|
|
} else { |
112
|
|
|
$this->board = $paths[2]; |
113
|
|
|
$this->thread = $paths[3]; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* parse dat collection. |
119
|
|
|
* |
120
|
|
|
* @param string $body |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Support\Collection |
123
|
|
|
*/ |
124
|
|
|
protected function parseDatCollection(string $body, ?int $end = null): Collection |
125
|
|
|
{ |
126
|
|
|
$lines = array_filter(explode("\n", $body), '\strlen'); |
127
|
|
|
$number = 0; |
|
|
|
|
128
|
|
|
|
129
|
|
|
$lineCount = count($lines); |
130
|
|
|
|
131
|
|
|
if (null === $end || $end > $lineCount) { |
132
|
|
|
$end = $lineCount; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$collection = collect(); |
136
|
|
|
|
137
|
|
|
for ($number = 1; $number <= $end; $number++) { |
138
|
|
|
$line = $lines[$number - 1]; |
139
|
|
|
|
140
|
|
|
[$name, $email, $date, $body] = explode('<>', $line); |
|
|
|
|
141
|
|
|
$name = trim(strip_tags($name)); |
142
|
|
|
$body = strip_tags($body, '<br>'); |
143
|
|
|
$resid = mb_substr($date, strpos($date, ' ID:') + 2); |
144
|
|
|
$date = mb_substr($date, 0, strpos($date, ' ID:') - 2); |
145
|
|
|
|
146
|
|
|
$collection->push(compact('number', 'name', 'email', 'date', 'body', 'resid')); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $collection; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* parse threads collection. |
154
|
|
|
* |
155
|
|
|
* @param string $body |
156
|
|
|
* |
157
|
|
|
* @return \Illuminate\Support\Collection |
158
|
|
|
*/ |
159
|
|
|
protected function parseThreadsCollection(string $body): Collection |
160
|
|
|
{ |
161
|
|
|
$threads = array_filter(explode("\n", $body), '\strlen'); |
162
|
|
|
|
163
|
|
|
return collect(array_map(function($elem) { |
164
|
|
|
[$id, $tmp] = explode('.dat<>', $elem); |
|
|
|
|
165
|
|
|
preg_match('/^(.*)\((\d+)\)\z/', $tmp, $matches); |
166
|
|
|
|
167
|
|
|
return [ |
168
|
|
|
'url' => vsprintf('http://%s/test/read.cgi/%s/%d', [ |
169
|
|
|
parse_url($this->url, PHP_URL_HOST), |
170
|
|
|
$this->board, |
171
|
|
|
$id, |
172
|
|
|
]), |
173
|
|
|
'id' => $id, |
174
|
|
|
'title' => trim($matches[1]), |
175
|
|
|
'count' => $matches[2], |
176
|
|
|
]; |
177
|
|
|
}, $threads)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* build message url. |
182
|
|
|
* |
183
|
|
|
* @param int $start |
184
|
|
|
* @param int|null $end |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
protected function messagesUrl(int $start = 1, ?int $end = null): string |
189
|
|
|
{ |
190
|
|
|
return "{$this->baseUrl}/{$this->board}/dat/{$this->thread}.dat"; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* build thread url. |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
protected function threadsUrl(): string |
199
|
|
|
{ |
200
|
|
|
return "{$this->baseUrl}/{$this->board}/subject.txt"; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* build post url. |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
protected function postUrl(): string |
209
|
|
|
{ |
210
|
|
|
return "{$this->baseUrl}/test/bbs.cgi"; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* 書き込み確認かどうか. |
215
|
|
|
* |
216
|
|
|
* @param string $html |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
private function confirm(string $html): bool |
221
|
|
|
{ |
222
|
|
|
return strpos($html, '書き込み確認') !== false; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.