Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. <?php
  2. //exit(); //Comment this line to have this scripta actually DO anything
  3. //Obviously you'll have to secure access to this script (USING IIS or Apache HTTP authentication)
  4. $host = $_SERVER['argv'][1];
  5. $user = $_SERVER['argv'][2];
  6. $pass = $_SERVER['argv'][3];
  7. $dbname = $_SERVER['argv'][4];
  8.  
  9. connect($host,$user,$pass,$dbname);
  10. echo "Connected\n";
  11. $sql = array();
  12. $sql['list tables'] = '
  13. SELECT TABLE_SCHEMA,TABLE_NAME, OBJECTPROPERTY(object_id(TABLE_NAME), N\'IsUserTable\') AS type
  14. FROM INFORMATION_SCHEMA.TABLES';
  15.  
  16. echo "GETTINGOBJECT\n";
  17. $tables = getObjects($sql['list tables']);
  18. var_dump($tables);
  19. echo "GETTINGOBJECT DONE\n";
  20. header('content-type: text/plain');
  21.  
  22.  
  23. echo "SIMPLEXML DONE\n";
  24. if ($_SERVER['argv']['5'] == 'LISTALL') {
  25. foreach ($tables as $table) {
  26. echo $table->TABLE_NAME."\n";
  27. }
  28. exit;
  29. }
  30.  
  31. if ($_SERVER['argv'][5] == 'QUERY') {
  32. $res = mssql_query($_SERVER['argv'][6]);
  33. if(!$res || mssql_num_rows($res) == 0) { //geen resultaten
  34. return array();
  35. }
  36. $rows = array();
  37. while($row = mssql_fetch_assoc($res)) {
  38. $rows[] = $row;
  39. }
  40. print_r($rows);
  41. exit;
  42.  
  43. }
  44.  
  45. foreach($tables as $table) {
  46. if ($_SERVER['argv'][5] && $_SERVER['argv'][5] != $table->TABLE_NAME)
  47. continue;
  48. $document = new SimpleXMLElement('<dump><tables></tables></dump>');
  49. echo "GETARRAY\n";
  50. $dump = getArray('SELECT '.$_SERVER['argv'][7].' * FROM '.$table->TABLE_SCHEMA.'.'.$table->TABLE_NAME.' '.$_SERVER['argv'][6]);
  51. echo "GETARRAY DONE\n";
  52.  
  53. $newTable = $document->tables[0]->addChild('table');
  54. $newTable->addAttribute('name',$table->TABLE_NAME);
  55. foreach($dump as $dumpRow) {
  56. $newRow = $newTable->addChild('row');
  57. foreach($dumpRow as $field=>$value) {
  58. $safe_value = preg_replace('/&(?!\w+;)/', '&', $value);
  59. $newRow->addChild($field,$safe_value);
  60. }
  61. }
  62. echo "$filename\n";
  63. $filename = 'bdd/'.$dbname.'-'.$table->TABLE_NAME;
  64. file_put_contents($filename, $document->asXML());
  65. echo "done\n";
  66. }
  67. echo "ASXML DONE\n";
  68.  
  69.  
  70. function connect($host,$user,$pass,$dbname)
  71. {
  72. mssql_connect($host,$user,$pass);
  73. mssql_select_db($dbname);
  74. var_dump(mssql_get_last_message());
  75. }
  76.  
  77.  
  78. /**
  79. * Get the database results as a object array
  80. * @param $query string SQL Query
  81. */
  82. function getObjects($query)
  83. {
  84. $res = mssql_query($query);
  85. if(!$res || mssql_num_rows($res) == 0) { //geen resultaten
  86. return array();
  87. }
  88. $rows = array();
  89. while($row = mssql_fetch_object($res)) {
  90. $rows[] = $row;
  91. }
  92. return $rows;
  93. }
  94.  
  95. /**
  96. * Get the database results as an array
  97. * @param $query string SQL Query
  98. */
  99. function getArray($query)
  100. {
  101. $res = mssql_query($query);
  102. if(!$res || mssql_num_rows($res) == 0) { //geen resultaten
  103. return array();
  104. }
  105. $rows = array();
  106. while($row = mssql_fetch_assoc($res)) {
  107. $rows[] = $row;
  108. }
  109. return $rows;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement