Guest User

Untitled

a guest
Feb 13th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. //DB connection values
  2. $sHost = "localhost";
  3. $sName = "test";
  4. $sUser = "";
  5. $sPass = "";
  6. $sPort = 3307;
  7.  
  8.  
  9. //The following could be retrieved using file_get_contents, or a file streamer
  10. $sFileContents = <<<EOT
  11. -- This is the first comment
  12. SELECT * FROM dl_bookmarks WHERE iID=3;
  13.  
  14. /* This is the second comment */
  15. SELECT * FROM dl_bookmarks WHERE sTitle=""Paragon" Initiative Enterprises Software consulting and web development for businesses \\ 'smes'";
  16.  
  17. # This is the third comment
  18. SELECT * FROM dl_bookmarks WHERE sTitle LIKE '"xDEEP" Diving Equipment; Scuba Gear; Single tank BC; Side Mount; Double tank BCD; Diving computer 'equipment'';
  19. EOT;
  20.  
  21. //Variant 1: Run a local SQL file. Since we stored our SQL contents in a
  22. //variable (could have been retrieved before using eg. file_get_contents),
  23. //we need to temporarily create a file for this
  24.  
  25. $sTempFile = tempnam(sys_get_temp_dir(), 'Sql');
  26.  
  27. //Create the temp file
  28. if(!file_put_contents($sTempFile, $sFileContents)) {
  29. trigger_error("Failed to create temporary file", E_USER_ERROR);
  30. }
  31.  
  32. //Assemble the command
  33. $sCommand = 'mysql'
  34. . ' --host=' . $sHost
  35. . ' --port=' . $sPort
  36. . ' --user=' . $sUser
  37. . ' --password=' . $sPass
  38. . ' --database=' . $sName
  39. . ' --execute="SOURCE ' . $sTempFile . '"'
  40. ;
  41. $sOutput = shell_exec($sCommand);
  42.  
  43. //Cleanup: remove the temp file
  44. if(!unlink($sTempFile)) {
  45. trigger_error("Failed to remove temporary file", E_USER_ERROR);
  46. }
  47.  
  48. //Variant 2: Run a parser
  49.  
  50. //Connect to the database
  51. $rMysqlI = new mysqli("localhost", "", "", "test", $sPort);
  52. if ($rMysqlI->connect_errno) {
  53. trigger_error("Failed to connect to MySQL: (" . $rMysqlI->connect_errno . ") " . $rMysqlI->connect_error, E_USER_ERROR);
  54. }
  55.  
  56.  
  57.  
  58. //START_OF_PARSER
  59.  
  60. $iCur = 0; //Current character pointer inside the SQL content
  61. $iInside = 0; //The context, in which the pointer is currently located (is the pointer inside a
  62. //comment, an SQL query, or deeper into an SQL query value?)
  63. $sBuffer = ""; //The buffer of the next individual query
  64. $aQueries = array(); //The list of queries
  65. while($iCur < strlen($sFileContents)) {
  66.  
  67. switch ($iInside) {
  68. case 0: //Inside query-context
  69. //Change context: Comments beginning with --
  70. if(substr($sFileContents, $iCur, 2) === "--") {
  71. $iCur++;
  72. $iInside = 2;
  73.  
  74. //Change context: Comments beginning with /*
  75. } elseif(substr($sFileContents, $iCur, 2) === "/*") {
  76. $iCur++;
  77. $iInside = 3;
  78.  
  79. //Change context: Comments beginning with #
  80. } elseif(substr($sFileContents, $iCur, 1) === "#") {
  81. $iInside = 2;
  82.  
  83. //Separator for a new query
  84. } elseif(substr($sFileContents, $iCur, 1) === ";") {
  85. $aQueries[] = trim($sBuffer); //$sBuffer; //Add current buffer to a unique array query item
  86. $sBuffer = ""; //Start a new buffer
  87.  
  88. //Change context: query values opened with '
  89. } elseif(substr($sFileContents, $iCur, 1) === "'") {
  90. $sBuffer .= substr($sFileContents, $iCur, 1);
  91. $iInside = 1;
  92.  
  93. //Change context: query values opened with "
  94. } elseif(substr($sFileContents, $iCur, 1) === '"') {
  95. $sBuffer .= substr($sFileContents, $iCur, 1);
  96. $iInside = 4;
  97.  
  98. //Not a special character
  99. } else {
  100. $sBuffer .= substr($sFileContents, $iCur, 1);
  101. }
  102. break;
  103.  
  104. case 1: //Inside value-context, ending with '
  105.  
  106. //Escaping character found within the query-value
  107. if(substr($sFileContents, $iCur, 1) === "\") {
  108. $sBuffer .= substr($sFileContents, $iCur, 2);
  109. $iCur++; //Skip next char
  110.  
  111. //The ending character for the query-value is found
  112. } elseif(substr($sFileContents, $iCur, 1) === "'") {
  113. $sBuffer .= substr($sFileContents, $iCur, 1);
  114. $iInside = 0;
  115.  
  116. //Not a special character
  117. } else {
  118. $sBuffer .= substr($sFileContents, $iCur, 1);
  119. }
  120. break;
  121.  
  122. case 4: //Inside value-context, ending with "
  123.  
  124. //Escaping character found within the query-value
  125. if(substr($sFileContents, $iCur, 1) === "\") {
  126. $sBuffer .= substr($sFileContents, $iCur, 2);
  127. $iCur = $iCur + 1; //Skip next char
  128.  
  129. //The ending character for the query-value is found
  130. } elseif(substr($sFileContents, $iCur, 1) === '"') {
  131. $sBuffer .= substr($sFileContents, $iCur, 1);
  132. $iInside = 0;
  133.  
  134. //Not a special character
  135. } else {
  136. $sBuffer .= substr($sFileContents, $iCur, 1);
  137. }
  138. break;
  139.  
  140. case 2: //Inside comment-context, ending with newline
  141.  
  142. //A two-character newline is found, signalling the end of the comment
  143. if(substr($sFileContents, $iCur, 2) === "rn") {
  144. $iCur++;
  145. $iInside = 0;
  146.  
  147. //A single-character newline is found, signalling the end of the comment
  148. } elseif(substr($sFileContents, $iCur, 1) === "n" || substr($sFileContents, $iCur, 1) === "r") {
  149. $iInside = 0;
  150. }
  151. break;
  152.  
  153. case 3: //Inside comment-context, ending with */
  154.  
  155. //A two-character */ is found, signalling the end of the comment
  156. if(substr($sFileContents, $iCur, 2) === "*/") {
  157. $iCur++;
  158. $iInside = 0;
  159. }
  160. break;
  161.  
  162. default:
  163. break;
  164. }
  165. $iCur++;
  166. }
  167.  
  168. //END_OF_PARSER
  169.  
  170. //Preview our results
  171. foreach($aQueries as $sQuery) {
  172. if (!$rMysqlI->query($sQuery)) {
  173. echo "ERROR "{$sQuery}": (" . $rMysqlI->errno . ") " . $rMysqlI->error . "<br />", E_USER_ERROR;
  174. } else {
  175. echo "SUCCESS "{$sQuery}"<br />", E_USER_ERROR;
  176. }
  177. }
Add Comment
Please, Sign In to add comment