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