-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShingetsu_Client.php
97 lines (83 loc) · 2.33 KB
/
Shingetsu_Client.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
<?php
// http://shingetsu.info/protocol/protocol-0.7.d1
/**
* Shingetsu_Client
*/
class Shingetsu_Client
{
private $server;
public function __construct($server)
{
$this->server = $server;
}
private function fetch($url)
{
$context = stream_context_create(
array('http' => array(
'method' => 'GET',
'header' => 'User-Agent: shinGETsu/0.7 (sgphp/0.1)',
))
);
return trim(file_get_contents('http://' . $url, false, $context));
}
public function ping()
{
return $this->fetch($this->server . '/ping');
}
public function node()
{
return $this->fetch($this->server . '/node');
}
public function recent()
{
$response = $this->fetch($this->server . '/recent');
$lines = explode("\n", $response);
$files = array();
foreach ($lines as $line) {
if (strpos($line, '<>') === false) {
continue;
}
list($timestamp, $id, $filename) = explode('<>', $line);
$name = str_replace('thread_', '', $filename);
$title = pack('H*', $name);
$id2 = md5('thread_' . $filename);
$files[] = compact('timestamp', 'id', 'filename', 'title', 'id2');
}
return $files;
}
public function head($filename, $timestamp = false)
{
$url = $this->server . "/head/{$filename}";
if ($timestamp) {
$url .= '/' . $timestamp;
}
return $this->fetch($url);
}
public function have($filename, $timestamp = false)
{
$url = $this->server . "/have/{$filename}";
$response = $this->fetch($url);
if ($response === "YES") {
return true;
} else if ($response === "NO") {
return false;
} else {
throw new Exception($response);
}
}
// timestampは必須
public function get($filename, $timestamp = false)
{
$url = $this->server . "/get/{$filename}";
if ($timestamp !== false) {
$url .= "/{$timestamp}";
}
$response = $this->fetch($url);
return $response;
}
public function join($my_node)
{
$url = $this->server . "/join/{$my_node}";
return $this->fetch($url);
}
}