|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Game-Insight |
|
4
|
|
|
* |
|
5
|
|
|
* @package Game-Insight/torrent |
|
6
|
|
|
* @author Ilya Gusev <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2015 |
|
8
|
|
|
* @link https://github.com/Game-Insight/torrent |
|
9
|
|
|
* @created 15.03.2015 03:16 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace League\Torrent\Helper; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Decoder |
|
17
|
|
|
* |
|
18
|
|
|
* @todo: validations for decoders |
|
19
|
|
|
* @package League\Torrent\Helper |
|
20
|
|
|
*/ |
|
21
|
|
|
class Decoder |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param $data |
|
26
|
|
|
* |
|
27
|
|
|
* @return array|bool|int|string |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function decodeData(& $data) |
|
30
|
|
|
{ |
|
31
|
|
|
switch (FileSystem::char($data)) { |
|
32
|
|
|
case 'i': |
|
33
|
|
|
$data = substr($data, 1); |
|
34
|
|
|
return self::decodeInteger($data); |
|
35
|
|
|
case 'l': |
|
36
|
|
|
$data = substr($data, 1); |
|
37
|
|
|
return self::decodeList($data); |
|
38
|
|
|
case 'd': |
|
39
|
|
|
$data = substr($data, 1); |
|
40
|
|
|
return self::decodeDictionary($data); |
|
41
|
|
|
default: |
|
42
|
|
|
return self::decodeString($data); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $data |
|
48
|
|
|
* |
|
49
|
|
|
* @return array|bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function decodeDictionary(& $data) |
|
52
|
|
|
{ |
|
53
|
|
|
$dictionary = array(); |
|
54
|
|
|
while (($char = FileSystem::char($data)) != 'e') { |
|
55
|
|
|
$key = self::decodeString($data); |
|
56
|
|
|
|
|
57
|
|
|
$dictionary[$key] = self::decodeData($data); |
|
58
|
|
|
} |
|
59
|
|
|
$data = substr($data, 1); |
|
60
|
|
|
return $dictionary; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $data |
|
65
|
|
|
* |
|
66
|
|
|
* @return array|bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function decodeList(& $data) |
|
69
|
|
|
{ |
|
70
|
|
|
$list = array(); |
|
71
|
|
|
while (($char = FileSystem::char($data)) != 'e') { |
|
72
|
|
|
$list[] = self::decodeData($data); |
|
73
|
|
|
} |
|
74
|
|
|
$data = substr($data, 1); |
|
75
|
|
|
return $list; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $data |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool|string |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function decodeString(& $data) |
|
84
|
|
|
{ |
|
85
|
|
|
$colon = strpos($data, ':'); |
|
86
|
|
|
$length = intval(substr($data, 0, $colon)); |
|
87
|
|
|
|
|
88
|
|
|
$string = substr($data, $colon + 1, $length); |
|
89
|
|
|
$data = substr($data, $colon + $length + 1); |
|
90
|
|
|
return $string; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $data |
|
95
|
|
|
* |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function decodeInteger(& $data) |
|
99
|
|
|
{ |
|
100
|
|
|
$start = 0; |
|
101
|
|
|
$end = strpos($data, 'e'); |
|
102
|
|
|
if (FileSystem::char($data) == '-') { |
|
103
|
|
|
$start++; |
|
104
|
|
|
} |
|
105
|
|
|
$integer = (int) substr($data, 0, $end); |
|
106
|
|
|
$data = substr($data, $end + 1); |
|
107
|
|
|
return 0 + $integer; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param $string |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function decode($string) |
|
116
|
|
|
{ |
|
117
|
|
|
if (is_file($string)) { |
|
118
|
|
|
$data = file_get_contents($string); |
|
119
|
|
|
} elseif (FileSystem::url_exists($string)) { |
|
120
|
|
|
$data = FileSystem::downloadTorrent($string); |
|
121
|
|
|
} else { |
|
122
|
|
|
$data = $string; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return (array) Decoder::decodeData($data); |
|
126
|
|
|
} |
|
127
|
|
|
} |