PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Admin/ntunnel_mysql.php

https://gitlab.com/hoanghung.dev/phunuvadoisong.com
PHP | 440 lines | 403 code | 31 blank | 6 comment | 77 complexity | d32d1d2d841faff5de02b7967524849b MD5 | raw file
  1. <?php //version my202
  2. //set allowTestMenu to false to disable System/Server test page
  3. $allowTestMenu = true;
  4. header("Content-Type: text/plain; charset=x-user-defined");
  5. error_reporting(0);
  6. set_time_limit(0);
  7. function phpversion_int()
  8. {
  9. list($maVer, $miVer, $edVer) = preg_split("(/|\.|-)", phpversion());
  10. return $maVer*10000 + $miVer*100 + $edVer;
  11. }
  12. if (phpversion_int() < 50300)
  13. {
  14. set_magic_quotes_runtime(0);
  15. }
  16. function GetLongBinary($num)
  17. {
  18. return pack("N",$num);
  19. }
  20. function GetShortBinary($num)
  21. {
  22. return pack("n",$num);
  23. }
  24. function GetDummy($count)
  25. {
  26. $str = "";
  27. for($i=0;$i<$count;$i++)
  28. $str .= "\x00";
  29. return $str;
  30. }
  31. function GetBlock($val)
  32. {
  33. $len = strlen($val);
  34. if( $len < 254 )
  35. return chr($len).$val;
  36. else
  37. return "\xFE".GetLongBinary($len).$val;
  38. }
  39. function EchoHeader($errno)
  40. {
  41. $str = GetLongBinary(1111);
  42. $str .= GetShortBinary(202);
  43. $str .= GetLongBinary($errno);
  44. $str .= GetDummy(6);
  45. echo $str;
  46. }
  47. function EchoConnInfo($conn)
  48. {
  49. $str = GetBlock(mysql_get_host_info($conn));
  50. $str .= GetBlock(mysql_get_proto_info($conn));
  51. $str .= GetBlock(mysql_get_server_info($conn));
  52. echo $str;
  53. }
  54. function EchoResultSetHeader($errno, $affectrows, $insertid, $numfields, $numrows)
  55. {
  56. $str = GetLongBinary($errno);
  57. $str .= GetLongBinary($affectrows);
  58. $str .= GetLongBinary($insertid);
  59. $str .= GetLongBinary($numfields);
  60. $str .= GetLongBinary($numrows);
  61. $str .= GetDummy(12);
  62. echo $str;
  63. }
  64. function EchoFieldsHeader($res, $numfields)
  65. {
  66. $str = "";
  67. for( $i = 0; $i < $numfields; $i++ ) {
  68. $str .= GetBlock(mysql_field_name($res, $i));
  69. $str .= GetBlock(mysql_field_table($res, $i));
  70. $type = mysql_field_type($res, $i);
  71. $length = mysql_field_len($res, $i);
  72. switch ($type) {
  73. case "int":
  74. if( $length > 11 ) $type = 8;
  75. else $type = 3;
  76. break;
  77. case "real":
  78. if( $length == 12 ) $type = 4;
  79. elseif( $length == 22 ) $type = 5;
  80. else $type = 0;
  81. break;
  82. case "null":
  83. $type = 6;
  84. break;
  85. case "timestamp":
  86. $type = 7;
  87. break;
  88. case "date":
  89. $type = 10;
  90. break;
  91. case "time":
  92. $type = 11;
  93. break;
  94. case "datetime":
  95. $type = 12;
  96. break;
  97. case "year":
  98. $type = 13;
  99. break;
  100. case "blob":
  101. if( $length > 16777215 ) $type = 251;
  102. elseif( $length > 65535 ) $type = 250;
  103. elseif( $length > 255 ) $type = 252;
  104. else $type = 249;
  105. break;
  106. default:
  107. $type = 253;
  108. }
  109. $str .= GetLongBinary($type);
  110. $flags = explode( " ", mysql_field_flags ( $res, $i ) );
  111. $intflag = 0;
  112. if(in_array( "not_null", $flags )) $intflag += 1;
  113. if(in_array( "primary_key", $flags )) $intflag += 2;
  114. if(in_array( "unique_key", $flags )) $intflag += 4;
  115. if(in_array( "multiple_key", $flags )) $intflag += 8;
  116. if(in_array( "blob", $flags )) $intflag += 16;
  117. if(in_array( "unsigned", $flags )) $intflag += 32;
  118. if(in_array( "zerofill", $flags )) $intflag += 64;
  119. if(in_array( "binary", $flags)) $intflag += 128;
  120. if(in_array( "enum", $flags )) $intflag += 256;
  121. if(in_array( "auto_increment", $flags )) $intflag += 512;
  122. if(in_array( "timestamp", $flags )) $intflag += 1024;
  123. if(in_array( "set", $flags )) $intflag += 2048;
  124. $str .= GetLongBinary($intflag);
  125. $str .= GetLongBinary($length);
  126. }
  127. echo $str;
  128. }
  129. function EchoData($res, $numfields, $numrows)
  130. {
  131. for( $i = 0; $i < $numrows; $i++ ) {
  132. $str = "";
  133. $row = mysql_fetch_row( $res );
  134. for( $j = 0; $j < $numfields; $j++ ){
  135. if( is_null($row[$j]) )
  136. $str .= "\xFF";
  137. else
  138. $str .= GetBlock($row[$j]);
  139. }
  140. echo $str;
  141. }
  142. }
  143. if (phpversion_int() < 40005) {
  144. EchoHeader(201);
  145. echo GetBlock("unsupported php version");
  146. exit();
  147. }
  148. if (phpversion_int() < 40010) {
  149. global $HTTP_POST_VARS;
  150. $_POST = &$HTTP_POST_VARS;
  151. }
  152. if (!isset($_POST["actn"]) || !isset($_POST["host"]) || !isset($_POST["port"]) || !isset($_POST["login"])) {
  153. $testMenu = $allowTestMenu;
  154. if (!$testMenu){
  155. EchoHeader(202);
  156. echo GetBlock("invalid parameters");
  157. exit();
  158. }
  159. }
  160. if (!$testMenu){
  161. if ($_POST["encodeBase64"] == '1') {
  162. for($i=0;$i<count($_POST["q"]);$i++)
  163. $_POST["q"][$i] = base64_decode($_POST["q"][$i]);
  164. }
  165. if (!function_exists("mysql_connect")) {
  166. EchoHeader(203);
  167. echo GetBlock("MySQL not supported on the server");
  168. exit();
  169. }
  170. $errno_c = 0;
  171. $hs = $_POST["host"];
  172. if( $_POST["port"] ) $hs .= ":".$_POST["port"];
  173. $conn = mysql_connect($hs, $_POST["login"], $_POST["password"]);
  174. $errno_c = mysql_errno();
  175. if(($errno_c <= 0) && ( $_POST["db"] != "" )) {
  176. $res = mysql_select_db( $_POST["db"], $conn);
  177. $errno_c = mysql_errno();
  178. }
  179. EchoHeader($errno_c);
  180. if($errno_c > 0) {
  181. echo GetBlock(mysql_error());
  182. } elseif($_POST["actn"] == "C") {
  183. EchoConnInfo($conn);
  184. } elseif($_POST["actn"] == "Q") {
  185. for($i=0;$i<count($_POST["q"]);$i++) {
  186. $query = $_POST["q"][$i];
  187. if($query == "") continue;
  188. if (phpversion_int() < 50400){
  189. if(get_magic_quotes_gpc())
  190. $query = stripslashes($query);
  191. }
  192. $res = mysql_query($query, $conn);
  193. $errno = mysql_errno();
  194. $affectedrows = mysql_affected_rows($conn);
  195. $insertid = mysql_insert_id($conn);
  196. $numfields = mysql_num_fields($res);
  197. $numrows = mysql_num_rows($res);
  198. EchoResultSetHeader($errno, $affectedrows, $insertid, $numfields, $numrows);
  199. if($errno > 0)
  200. echo GetBlock(mysql_error());
  201. else {
  202. if($numfields > 0) {
  203. EchoFieldsHeader($res, $numfields);
  204. EchoData($res, $numfields, $numrows);
  205. } else {
  206. if(phpversion_int() >= 40300)
  207. echo GetBlock(mysql_info($conn));
  208. else
  209. echo GetBlock("");
  210. }
  211. }
  212. if($i<(count($_POST["q"])-1))
  213. echo "\x01";
  214. else
  215. echo "\x00";
  216. mysql_free_result($res);
  217. }
  218. }
  219. exit();
  220. }
  221. function doSystemTest()
  222. {
  223. function output($description, $succ, $resStr) {
  224. echo "<tr><td class=\"TestDesc\">$description</td><td ";
  225. echo ($succ)? "class=\"TestSucc\">$resStr[0]</td></tr>" : "class=\"TestFail\">$resStr[1]</td></tr>";
  226. }
  227. output("PHP version >= 4.0.5", phpversion_int() >= 40005, array("Yes", "No"));
  228. output("mysql_connect() available", function_exists("mysql_connect"), array("Yes", "No"));
  229. if (phpversion_int() >= 40302 && substr($_SERVER["SERVER_SOFTWARE"], 0, 6) == "Apache" && function_exists("apache_get_modules")){
  230. if (in_array("mod_security2", apache_get_modules()))
  231. output("Mod Security 2 installed", false, array("No", "Yes"));
  232. }
  233. }
  234. header("Content-Type: text/html");
  235. ?>
  236. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  237. <html>
  238. <head>
  239. <title>Navicat HTTP Tunnel Tester</title>
  240. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  241. <style type="text/css">
  242. body{
  243. margin: 30px;
  244. font-family: Tahoma;
  245. font-weight: normal;
  246. font-size: 14px;
  247. color: #222222;
  248. }
  249. table{
  250. width: 100%;
  251. border: 0px;
  252. }
  253. input{
  254. font-family:Tahoma,sans-serif;
  255. border-style:solid;
  256. border-color:#666666;
  257. border-width:1px;
  258. }
  259. fieldset{
  260. border-style:solid;
  261. border-color:#666666;
  262. border-width:1px;
  263. }
  264. .Title1{
  265. font-size: 30px;
  266. color: #003366;
  267. }
  268. .Title2{
  269. font-size: 10px;
  270. color: #999966;
  271. }
  272. .TestDesc{
  273. width:70%
  274. }
  275. .TestSucc{
  276. color: #00BB00;
  277. }
  278. .TestFail{
  279. color: #DD0000;
  280. }
  281. .mysql{
  282. }
  283. .pgsql{
  284. display:none;
  285. }
  286. .sqlite{
  287. display:none;
  288. }
  289. #page{
  290. max-width: 42em;
  291. min-width: 36em;
  292. border-width: 0px;
  293. margin: auto auto;
  294. }
  295. #host, #dbfile{
  296. width: 300px;
  297. }
  298. #port{
  299. width: 75px;
  300. }
  301. #login, #password, #db{
  302. width: 150px;
  303. }
  304. #Copyright{
  305. text-align: right;
  306. font-size: 10px;
  307. color: #888888;
  308. }
  309. </style>
  310. <script type="text/javascript">
  311. function getInternetExplorerVersion(){
  312. var ver = -1;
  313. if (navigator.appName == "Microsoft Internet Explorer"){
  314. var regex = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  315. if (regex.exec(navigator.userAgent))
  316. ver = parseFloat(RegExp.$1);
  317. }
  318. return ver;
  319. }
  320. function setText(element, text, succ){
  321. element.className = (succ)?"TestSucc":"TestFail";
  322. element.innerHTML = text;
  323. }
  324. function getByteAt(str, offset){
  325. return str.charCodeAt(offset) & 0xff;
  326. }
  327. function getIntAt(binStr, offset){
  328. return (getByteAt(binStr, offset) << 24)+
  329. (getByteAt(binStr, offset+1) << 16)+
  330. (getByteAt(binStr, offset+2) << 8)+
  331. (getByteAt(binStr, offset+3) >>> 0);
  332. }
  333. function getBlockStr(binStr, offset){
  334. if (getByteAt(binStr, offset) < 254)
  335. return binStr.substring(offset+1, offset+1+binStr.charCodeAt(offset));
  336. else
  337. return binStr.substring(offset+5, offset+5+getIntAt(binStr, offset+1));
  338. }
  339. function doServerTest(){
  340. var version = getInternetExplorerVersion();
  341. if (version==-1 || version>=9.0){
  342. var xmlhttp = (window.XMLHttpRequest)? new XMLHttpRequest() : xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  343. xmlhttp.onreadystatechange=function(){
  344. var outputDiv = document.getElementById("ServerTest");
  345. if (xmlhttp.readyState == 4){
  346. if (xmlhttp.status == 200){
  347. var errno = getIntAt(xmlhttp.responseText, 6);
  348. if (errno == 0)
  349. setText(outputDiv, "Connection Success!", true);
  350. else
  351. setText(outputDiv, parseInt(errno)+" - "+getBlockStr(xmlhttp.responseText, 16), false);
  352. }else
  353. setText(outputDiv, "HTTP Error - "+xmlhttp.status, false);
  354. }
  355. }
  356. var params = "";
  357. var form = document.getElementById("TestServerForm");
  358. for (var i=0; i<form.elements.length; i++){
  359. if (i>0) params += "&";
  360. params += form.elements[i].id+"="+form.elements[i].value.replace("&", "%26");
  361. }
  362. document.getElementById("ServerTest").className = "";
  363. document.getElementById("ServerTest").innerHTML = "Connecting...";
  364. xmlhttp.open("POST", "", true);
  365. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  366. xmlhttp.setRequestHeader("Content-length", params.length);
  367. xmlhttp.setRequestHeader("Connection", "close");
  368. xmlhttp.send(params);
  369. }else{
  370. document.getElementById("ServerTest").className = "";
  371. document.getElementById("ServerTest").innerHTML = "Internet Explorer "+version+" is not supported, please use Internet explorer 9.0 or above, firefox, chrome or safari";
  372. }
  373. }
  374. </script>
  375. </head>
  376. <body>
  377. <div id="page">
  378. <p>
  379. <font class="Title1">Navicat&trade;</font><br>
  380. <font class="Title2">The gateway to your database!</font>
  381. </p>
  382. <fieldset>
  383. <legend>System Environment Test</legend>
  384. <table>
  385. <tr style="<?php echo "display:none"; ?>"><td width=70%>PHP installed properly</td><td class="TestFail">No</td></tr>
  386. <?php echo doSystemTest();?>
  387. </table>
  388. </fieldset>
  389. <br>
  390. <fieldset>
  391. <legend>Server Test</legend>
  392. <form id="TestServerForm" action="#" onSubmit="return false;">
  393. <input type=hidden id="actn" value="C">
  394. <table>
  395. <tr class="mysql"><td width="35%">Hostname/IP Address:</td><td><input type=text id="host" placeholder="localhost"></td></tr>
  396. <tr class="mysql"><td>Port:</td><td><input type=text id="port" placeholder="3306"></td></tr>
  397. <tr class="pgsql"><td>Initial Database:</td><td><input type=text id="db" placeholder="template1"></td></tr>
  398. <tr class="mysql"><td>Username:</td><td><input type=text id="login" placeholder="root"></td></tr>
  399. <tr class="mysql"><td>Password:</td><td><input type=password id="password" placeholder=""></td></tr>
  400. <tr class="sqlite"><td>Database File:</td><td><input type=text id="dbfile" placeholder="sqlite.db"></td></tr>
  401. <tr><td></td><td><br><input id="TestButton" type="submit" value="Test Connection" onClick="doServerTest()"></td></tr>
  402. </table>
  403. </form>
  404. <div id="ServerTest"><br></div>
  405. </fieldset>
  406. <p id="Copyright">Copyright &copy; PremiumSoft &trade; CyberTech Ltd. All Rights Reserved.</p>
  407. </div>
  408. </body>
  409. </html>