|
1 | 1 | <?php
|
| 2 | + |
| 3 | +const debugOn = false; |
| 4 | + |
2 | 5 | set_error_handler(function($n, $msg, $file, $line){
|
3 | 6 | header("Content-Type: text/plain");
|
4 | 7 | echo "Error: $msg at $file#$line";
|
|
7 | 10 | });
|
8 | 11 | if(isset($_FILES["phar"])){
|
9 | 12 | $newApi = "2.0.0";
|
10 |
| - if(isset($_REQUEST["api"])) $newApi = $_REQUEST["api"]; |
| 13 | + if(isset($_REQUEST["diffapi"]) and $_REQUEST["diffapi"] === "on" and isset($_REQUEST["api"])) $newApi = $_REQUEST["api"]; |
11 | 14 | $path = $_FILES["phar"]["tmp_name"];
|
12 | 15 | $extended = $path . ".phar";
|
13 | 16 | move_uploaded_File($path, $extended);
|
|
29 | 32 | $yaml["api"][] = "2.0.0";
|
30 | 33 | $contents = yaml_emit($yaml);
|
31 | 34 | $phar->addFromString("plugin.yml", $contents);
|
| 35 | + |
| 36 | + if(debugOn) header("Content-Type: text/plain"); |
| 37 | + |
| 38 | + $replaceUse = ($_REQUEST["nbt-use"] ?? "off") === "on"; |
| 39 | + $replaceFQN = ($_REQUEST["nbt-fqn"] ?? "off") === "on"; |
| 40 | + $replaceNew = ($_REQUEST["nbt-new"] ?? "off") === "on"; |
| 41 | + $replaceInstanceOf = ($_REQUEST["nbt-instanceof"] ?? "off") === "on"; |
| 42 | + if($replaceUse or $replaceFQN or $replaceNew or $replaceInstanceOf){ |
| 43 | + $refactorTable = [ |
| 44 | + "ByteArray" => "ByteArrayTag", |
| 45 | + "Byte" => "ByteTag", |
| 46 | + "Compound" => "CompoundTag", |
| 47 | + "Double" => "DoubleTag", |
| 48 | + "End" => "EndTag", |
| 49 | + "Float" => "FloatTag", |
| 50 | + "IntArray" => "IntArrayTag", |
| 51 | + "Int" => "IntTag", |
| 52 | + "Enum" => "ListTag", |
| 53 | + "Long" => "LongTag", |
| 54 | + "Short" => "ShortTag", |
| 55 | + "String" => "StringTag" |
| 56 | + ]; |
| 57 | + foreach(new \RecursiveIteratorIterator($phar) as $localName => $file){ |
| 58 | + $fn = $file->getFileName(); |
| 59 | + $localName = substr($localName, 8 + strlen($extended)); |
| 60 | + if(substr($fn, -4) === ".php"){ |
| 61 | + $c = $o = file_get_contents($file->getPathName()); |
| 62 | + if($replaceUse){ |
| 63 | + $ns = "pocketmine\\nbt\\tag\\"; |
| 64 | + $quotedNs = preg_quote($ns); |
| 65 | + $names = implode("|", array_map("preg_quote", array_keys($refactorTable))); |
| 66 | + $c = preg_replace_callback("/use[ \t\n\r]+$quotedNs($names)[ \t\n\r]*;/i", function($match) use($ns, $refactorTable){ |
| 67 | + return "use $ns" . $refactorTable[$match[1]] . ";"; |
| 68 | + }, $c); |
| 69 | + } |
| 70 | + if($replaceFQN){ |
| 71 | + foreach($refactorTable as $from => $to){ |
| 72 | + $from = "\\pocketmine\\nbt\\tag\\$from"; |
| 73 | + $to = "\\pocketmine\\nbt\\tag\\$to"; |
| 74 | + $c = str_replace($from, $to, $c); |
| 75 | + } |
| 76 | + } |
| 77 | + if($replaceNew){ |
| 78 | + $ns = "pocketmine\\nbt\\tag\\"; |
| 79 | + $quotedNs = preg_quote($ns); |
| 80 | + $names = implode("|", array_map("preg_quote", array_keys($refactorTable))); |
| 81 | + $c = preg_replace_callback("/new[ \t\n\r]+($names)[ \t\n\r]*([\(\);,])/i", function($match) use($ns, $refactorTable){ |
| 82 | + return "new " . $refactorTable[$match[1]] . $match[2]; |
| 83 | + }, $c); |
| 84 | + } |
| 85 | + if($replaceInstanceOf){ |
| 86 | + $ns = "pocketmine\\nbt\\tag\\"; |
| 87 | + $quotedNs = preg_quote($ns); |
| 88 | + $names = implode("|", array_map("preg_quote", array_keys($refactorTable))); |
| 89 | + $c = preg_replace_callback("/instanceof[ \t\n\r]+$quotedNs($names)/i", function($match) use($ns, $refactorTable){ |
| 90 | + return "instanceof " . $refactorTable[$match[1]]; |
| 91 | + }, $c); |
| 92 | + } |
| 93 | + if(debugOn){ |
| 94 | + echo $localName, " ($fn):\n", $c, "\n\n"; |
| 95 | + } |
| 96 | + if($c !== $o){ |
| 97 | + $phar->addFromString($localName, $c); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
32 | 103 | $phar->stopBuffering();
|
| 104 | + if(debugOn){ |
| 105 | + echo base64_encode(file_get_contents($extended)); |
| 106 | + die; |
| 107 | + } |
33 | 108 | header("Content-Type: application/octet-stream");
|
34 | 109 | echo file_get_contents($extended);
|
35 | 110 | exit;
|
|
41 | 116 | </head>
|
42 | 117 | <body>
|
43 | 118 | <h1>API 2.0.0 injection</h1>
|
44 |
| - <p>Before using this tool, please carefully read the following cautions:</p> |
| 119 | + <p>Before using this tool, please carefully read the following caution:</p> |
45 | 120 | <ul>
|
46 |
| - <li>This tool only <em>forces</em> the plugin to say that it supports API 2.0.0 (PHP 7 update). It will not fix the actual incompatibility issues.</li> |
| 121 | + <li>This tool only <em>forces</em> the plugin to say that it supports API 2.0.0 (PHP 7 update), and optionally, blindly replaces some specific backwards-incompatible changes in the phar. It will not fix the actual incompatibility issues.</li> |
47 | 122 | <li>If errors happen after using phars downloaded from this page, unintsall it immediately.</li>
|
48 |
| - <li>Click <button onclick='document.getElementById("upload").disabled = false;'>this button</button> if you have read the above.</li> |
| 123 | + <li>Click <span onclick='alert("Thanks for carefully reading the caution!"); document.getElementById("upload").disabled = false;'>these three words</span> if you have read the above.</li> |
49 | 124 | </ul>
|
50 | 125 | <hr>
|
51 | 126 | <form action="updated.phar" method="post" enctype="multipart/form-data">
|
52 |
| - <input type="file" name="phar"><br> |
53 |
| - No, I don't want API <code>2.0.0</code>. I want something else: <input type="text" name="api" value="2.0.0"><br> |
54 |
| - <input type="submit" id="upload" value="Inject" disabled> |
| 127 | + <p><input type="file" name="phar"></p> |
| 128 | + <ul> |
| 129 | + <li> |
| 130 | + <input type="checkbox" name="diffapi" onclick='document.getElementById("apiInput").disabled = false;'> No, I don't want API <code>2.0.0</code>. |
| 131 | + I want something else: <input id="apiInput" type="text" name="api" value="2.0.0" disabled> |
| 132 | + </li> |
| 133 | + <li> |
| 134 | + Replace the following usage of NBT tags: |
| 135 | + <ul> |
| 136 | + <li><input type="checkbox" name="nbt-use" checked> <code>use pocketmine\nbt\tag\****Tag</code></li> |
| 137 | + <li><input type="checkbox" name="nbt-fqn" checked> fully-qualified <code>\pocketmine\nbt\tag\****Tag</code></li> |
| 138 | + <li><input type="checkbox" name="nbt-new" checked> <code>new ****Tag</code></li> |
| 139 | + <li><input type="checkbox" name="nbt-instanceof" checked> <code>instanceof ****Tag</code></li> |
| 140 | + </ul> |
| 141 | + </li> |
| 142 | + </ul> |
| 143 | + <p><input type="submit" id="upload" value="Inject" disabled></p> |
55 | 144 | </form>
|
56 | 145 | </body>
|
57 | 146 | </html>
|
|
0 commit comments