Browse code

Import from the old rivettracker git repository at sourceforge (amisaph/amisapphire branch)

Clarissa Walker (ami-sapphire) authored on 2014/01/24 14:02:23
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,122 @@
1
+<?php
2
+
3
+// Woohoo! Who needs mhash or PHP 4.3?
4
+// Don't require it. Still recommended, but not mandatory.
5
+if (!function_exists("sha1"))
6
+	@include_once("sha1lib.php");
7
+
8
+
9
+// We'll protect the namespace of our code
10
+// using a class
11
+class BEncode
12
+{
13
+
14
+// Dictionary keys must be sorted. foreach tends to iterate over the order
15
+// the array was made, so we make a new one in sorted order. :)
16
+/*
17
+function makeSorted($array)
18
+{
19
+	$i = 0;
20
+
21
+	// Shouldn't happen!
22
+	if (empty($array))
23
+		return $array;
24
+
25
+	foreach($array as $key => $value)
26
+		$keys[$i++] = stripslashes($key);
27
+	sort($keys);
28
+	for ($i=0 ; isset($keys[$i]); $i++)
29
+		$return[addslashes($keys[$i])] = $array[addslashes($keys[$i])];
30
+	return $return;
31
+}
32
+*/
33
+// Encodes strings, integers and empty dictionaries.
34
+// $unstrip is set to true when decoding dictionary keys
35
+function encodeEntry($entry, &$fd, $unstrip = false)
36
+{
37
+	if (is_bool($entry))
38
+	{
39
+		$fd .= "de";
40
+		return;
41
+	}
42
+	if (is_int($entry) || is_float($entry))
43
+	{
44
+		$fd .= "i".$entry."e";
45
+		return;
46
+	}
47
+	if ($unstrip)
48
+		$myentry = stripslashes($entry);
49
+	else
50
+		$myentry = $entry;
51
+	$length = strlen($myentry);
52
+	$fd .= $length.":".$myentry;
53
+	return;
54
+}
55
+
56
+// Encodes lists
57
+function encodeList($array, &$fd)
58
+{
59
+	$fd .= "l";
60
+
61
+	// The empty list is defined as array();
62
+	if (empty($array))
63
+	{
64
+		$fd .= "e";
65
+		return;
66
+	}
67
+	for ($i = 0; isset($array[$i]); $i++)
68
+		$this->decideEncode($array[$i], $fd);
69
+	$fd .= "e";
70
+}
71
+
72
+// Passes lists and dictionaries accordingly, and has encodeEntry handle
73
+// the strings and integers.
74
+function decideEncode($unknown, &$fd)
75
+{
76
+	if (is_array($unknown))
77
+	{
78
+		if (isset($unknown[0]) || empty($unknown))
79
+			return $this->encodeList($unknown, $fd);
80
+		else
81
+			return $this->encodeDict($unknown, $fd);
82
+	}
83
+	$this->encodeEntry($unknown, $fd);
84
+}
85
+
86
+// Encodes dictionaries
87
+function encodeDict($array, &$fd)
88
+{
89
+	$fd .= "d";
90
+	if (is_bool($array))
91
+	{
92
+		$fd .= "e";
93
+		return;
94
+	}
95
+	// NEED TO SORT!
96
+	//$newarray = $this->makeSorted($array);
97
+	ksort($array, SORT_STRING);
98
+
99
+	foreach($array as $left => $right)
100
+	{
101
+		$this->encodeEntry($left, $fd, true);
102
+		$this->decideEncode($right, $fd);
103
+	}
104
+	$fd .= "e";
105
+	return;
106
+}
107
+
108
+
109
+
110
+} // End of class declaration.
111
+
112
+// Use this function in your own code.
113
+function BEncode($array)
114
+{
115
+	$string = "";
116
+	$encoder = new BEncode;
117
+	$encoder->decideEncode($array, $string);
118
+	return $string;
119
+}
120
+
121
+
122
+?>
0 123
\ No newline at end of file