better-staridc-MNBT
1<?php
2class hieroglyphy{
3 private $characters;
4 private $numbers;
5 private $unescape;
6 private $functionConstructor;
7
8 public function __construct(){
9 $this->precharacters();
10 }
11
12 private function precharacters(){
13 $this->numbers = array(
14 "+[]",
15 "+!![]",
16 "!+[]+!![]",
17 "!+[]+!![]+!![]",
18 "!+[]+!![]+!![]+!![]",
19 "!+[]+!![]+!![]+!![]+!![]",
20 "!+[]+!![]+!![]+!![]+!![]+!![]",
21 "!+[]+!![]+!![]+!![]+!![]+!![]+!![]",
22 "!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]",
23 "!+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]"
24 );
25
26 $this->characters = array(
27 "0" => "(" . $this->numbers[0] . "+[])",
28 "1" => "(" . $this->numbers[1] . "+[])",
29 "2" => "(" . $this->numbers[2] . "+[])",
30 "3" => "(" . $this->numbers[3] . "+[])",
31 "4" => "(" . $this->numbers[4] . "+[])",
32 "5" => "(" . $this->numbers[5] . "+[])",
33 "6" => "(" . $this->numbers[6] . "+[])",
34 "7" => "(" . $this->numbers[7] . "+[])",
35 "8" => "(" . $this->numbers[8] . "+[])",
36 "9" => "(" . $this->numbers[9] . "+[])"
37 );
38
39 $_object_Object = "[]+{}";
40 $_NaN = "+{}+[]";
41 $_true = "!![]+[]";
42 $_false = "![]+[]";
43 $_undefined = "[][[]]+[]";
44
45 $this->characters[" "] = "(" . $_object_Object . ")[" . $this->numbers[7] . "]";
46 $this->characters["["] = "(" . $_object_Object . ")[" . $this->numbers[0] . "]";
47 $this->characters["]"] = "(" . $_object_Object . ")[" + $this->characters[1] . "+" . $this->characters[4] . "]";
48 $this->characters["a"] = "(" . $_NaN . ")[" . $this->numbers[1] . "]";
49 $this->characters["b"] = "(" . $_object_Object . ")[" . $this->numbers[2] . "]";
50 $this->characters["c"] = "(" . $_object_Object . ")[" . $this->numbers[5] . "]";
51 $this->characters["d"] = "(" . $_undefined . ")[" . $this->numbers[2] . "]";
52 $this->characters["e"] = "(" . $_undefined . ")[" . $this->numbers[3] . "]";
53 $this->characters["f"] = "(" . $_false . ")[" . $this->numbers[0] . "]";
54 $this->characters["i"] = "(" . $_undefined . ")[" . $this->numbers[5] . "]";
55 $this->characters["j"] = "(" . $_object_Object . ")[" . $this->numbers[3] . "]";
56 $this->characters["l"] = "(" . $_false . ")[" . $this->numbers[2] . "]";
57 $this->characters["n"] = "(" . $_undefined . ")[" . $this->numbers[1] . "]";
58 $this->characters["o"] = "(" . $_object_Object . ")[" . $this->numbers[1] . "]";
59 $this->characters["r"] = "(" . $_true . ")[" . $this->numbers[1] . "]";
60 $this->characters["s"] = "(" . $_false . ")[" . $this->numbers[3] . "]";
61 $this->characters["t"] = "(" . $_true . ")[" . $this->numbers[0] . "]";
62 $this->characters["u"] = "(" . $_undefined . ")[" . $this->numbers[0] ."]";
63 $this->characters["N"] = "(" . $_NaN . ")[" . $this->numbers[0] . "]";
64 $this->characters["O"] = "(" . $_object_Object . ")[" . $this->numbers[8] . "]";
65
66 $_Infinity = "+(" . $this->numbers[1] . "+" . $this->characters["e"] . "+" . $this->characters[1] . "+" . $this->characters[0] . "+" . $this->characters[0] . "+" . $this->characters[0] . ")+[]";
67
68 $this->characters["y"] = "(" . $_Infinity . ")[" . $this->numbers[7] . "]";
69 $this->characters["I"] = "(" . $_Infinity . ")[" . $this->numbers[0] . "]";
70
71 $_1e100 = "+(" . $this->numbers[1] . "+" . $this->characters["e"] . "+" . $this->characters[1] . "+" . $this->characters[0] . "+" . $this->characters[0] . ")+[]";
72
73 $this->characters["+"] = "(" . $_1e100 . ")[" . $this->numbers[2] . "]";
74
75 $this->functionConstructor = "[][" . $this->hieroglyphyString("sort") . "][" . $this->hieroglyphyString("constructor") . "]";
76
77 //Below $this->characters need target http(s) pages
78 $locationString = "[]+" . $this->hieroglyphyScript("return location");
79 $this->characters["h"] = "(" . $locationString . ")" . "[" . $this->numbers[0] . "]";
80 $this->characters["p"] = "(" . $locationString . ")" . "[" . $this->numbers[3] . "]";
81 $this->characters["/"] = "(" . $locationString . ")" . "[" . $this->numbers[6] . "]";
82
83 $this->unescape = $this->hieroglyphyScript("return unescape");
84 $escape = $this->hieroglyphyScript("return escape");
85
86 $this->characters["%"] = $escape . "(" . $this->hieroglyphyString("[") . ")[" . $this->numbers[0] . "]";
87 }
88
89 private function getHexaString ($number, $digits) {
90 $string = bin2hex($number);
91
92 while (strlen($string) < $digits) {
93 $string = "0" . $string;
94 }
95
96 return $string;
97 }
98
99 private function getUnescapeSequence ($charCode) {
100 return $this->unescape . "(" .
101 $this->hieroglyphyString("%" . $this->getHexaString($charCode, 2)) . ")";
102 }
103
104 private function getHexaSequence ($charCode) {
105 return $this->hieroglyphyString("\\x" . $this->getHexaString($charCode, 2));
106 }
107
108 private function getUnicodeSequence ($charCode) {
109 return $this->hieroglyphyString("\\u" . $this->getHexaString($charCode, 4));
110 }
111
112 private function hieroglyphyCharacter ($char) {
113 $charCode = ord($char);
114
115 if (isset($this->characters[$char])) {
116 return $this->characters[$char];
117 }
118
119 if (($char == "\\") || ($char == "x")) {
120 //These chars must be handled appart becuase the others need them
121 $this->characters[$char] = $this->getUnescapeSequence($charCode);
122 return $this->characters[$char];
123 }
124
125 $shortestSequence = $this->getUnicodeSequence($charCode);
126
127 //ASCII $characters can be obtained with hexa and unscape sequences
128 if ($charCode < 128) {
129 $unescapeSequence = $this->getUnescapeSequence($charCode);
130 if (strlen($shortestSequence) > strlen($unescapeSequence)) {
131 $shortestSequence = $unescapeSequence;
132 }
133
134 $hexaSequence = $this->getHexaSequence($charCode);
135 if (strlen($shortestSequence) > strlen($hexaSequence)) {
136 $shortestSequence = $hexaSequence;
137 }
138 }
139
140 $this->characters[$char] = $shortestSequence;
141 return $shortestSequence;
142 }
143
144 public function hieroglyphyString ($str) {
145 $hieroglyphiedStr = "";
146
147 for ($i = 0; $i < strlen($str); $i++) {
148
149 $hieroglyphiedStr .= ($i > 0) ? "+" : "";
150 $hieroglyphiedStr .= $this->hieroglyphyCharacter($str[$i]);
151 }
152
153 return $hieroglyphiedStr;
154 }
155
156 public function hieroglyphyNumber ($n) {
157 $n = +$n;
158
159 if ($n <= 9) {
160 return $this->numbers[$n];
161 }
162
163 return "+(" . $this->hieroglyphyString(ord($n[10])) . ")";
164 }
165
166 public function hieroglyphyScript ($src) {
167 return $this->functionConstructor . "(" . $this->hieroglyphyString($src) . ")()";
168 }
169}