Advertisement
TBotNik

Parse SQL Project

Nov 25th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.43 KB | None | 0 0
  1. CONFIG.PHP
  2.  
  3. <?php
  4. /***************************************************************************/
  5. /* Author: Nyle E. Davis Create Date: 12/10/05 */
  6. /* E-Mail: davisoftaec@gmail.com Revision: 1.0 */
  7. /* Purpose: This file contains the preset vars and definitions that */
  8. /* direct the calling and processing for the current process. */
  9. /* This file is written into by the parse_sql.php screen proc */
  10. /* to setup processing with the parse_sql_proc.php process so */
  11. /* the process can run from the command line via a cron job. */
  12. /***************************************************************************/
  13. /* Mod by: Nyle E. Davis Mod Date: 12/09/05 */
  14. /* Change: Added debug vars of: */
  15. /* DBUG_ME ==> Does array and var dumps, */
  16. /* INFO_ME ==> Allow run of phpinfo() statements, */
  17. /* PRUN_ME ==> Does other process run debugging. */
  18. /* SHOW_ME ==> Shows/echos all set vars, */
  19. /* STOP_ME ==> Stop processing with a die() statement. */
  20. /* Notes: The debug var PRUN_ME can be assigned values of 0-9 where 0 is */
  21. /* off and the other values can turn on upto 9 debugging tests. */
  22. /***************************************************************************/
  23.  
  24. // Define the debugging Constants
  25. define ( 'DBUG_ME', 1 ); // 0=off, 1=on
  26. define ( 'INFO_ME', 0 ); // 0=off, 1=on
  27. define ( 'PRUN_ME', 0 ); // 0=off, 1-9=on by test type
  28. define ( 'SHOW_ME', 1 ); // 0=off, 1=on
  29. define ( 'STOP_ME', 0 ); // 0=off, 1=on end where set in code.
  30. $et_ray = array(); // Add the module names to test in this array
  31. // End debugging vars
  32.  
  33. $crdir = getcwd().'/';
  34. // The values after this line will be written in by the parse_sql.php screen.
  35. // Set the DB constants
  36. define ( 'DB_HST', 'localhost' ); // DB Host
  37. define ( 'DB_PRT', 3306 ); // DB Port
  38. define ( 'DB_UID', 'ndavis' ); // DB User
  39. define ( 'DB_PWD', 'nomened1497' ); // DB Password
  40. define ( 'DB_DFD', '*.*' ); // DB Default DB
  41. define ( 'DB_DTB', true ); // DB Drop Tables enabled
  42. define ( 'DB_STR', false ); // DB Structure only enabled
  43. define ( 'DB_COM', true ); // DB Export Comments enabled
  44. define ( 'DB_SIZ', '12M' ); // DB Max File Size Setting
  45.  
  46. // Set Backup Directory
  47. define ('BUP_DIR', '/backups/DB Backups/Dumps');
  48.  
  49. // Set Other Constants
  50. define ('MAX_RWS', 10000);
  51.  
  52. // end definitions
  53.  
  54. ?>
  55.  
  56. PARSE_SQL.PHP
  57.  
  58. <?php
  59. /***************************************************************************/
  60. /* Author: Nyle E. Davis Create Date: 12/10/05 */
  61. /* E-Mail: davisoftaec@gmail.com Revision: 1.0 */
  62. /* Purpose: This file contains the preset vars and definitions that */
  63. /* direct the calling and processing for the current process. */
  64. /***************************************************************************/
  65. /* Mod by: Nyle E. Davis Mod Date: 12/09/05 */
  66. /* Notes: The debug var PRUN_ME can be assigned values of 0-9 where 0 is */
  67. /* off and the other values can turn on upto 9 debugging tests. */
  68. /***************************************************************************/
  69. // Parse .sql
  70. define ('SHOW_ME', 0);
  71. if (SHOW_ME==2) {
  72. foreach ($_POST as $key => $val) {
  73. echo "PK=> $key PV=> $val <br>";
  74. } // end foreach $_POST
  75. foreach ($_GET as $key => $val) {
  76. echo "GK=> $key GV=> $val <br>";
  77. } // end foreach $_POST
  78. } // end if SHOW_ME
  79. if (isset($_POST['pfile'])) {
  80. // $ffname = __FILE__;
  81. $ffname = $_POST['pfile'];
  82. $nfile = __FILE__;
  83. $fname = realpath($ffname);
  84. $rlpath = truepath($ffname);
  85. $realpt = realpath($ffname);
  86. echo "FN=> $ffname FNM=> $fname NF=> $nfile <br>";
  87. echo "R=> $rlpath RP=> $realpt <br>";
  88. var_dump($_FILES);
  89. // filePathParts($info);
  90. } // end if isset $_POST
  91. foreach ($_FILES as $name => $file) {
  92. echo "TN=> $name FN=> $file <br>";
  93. }
  94. //phpinfo();
  95.  
  96. function read_fas_array ($myfile) {
  97. echo "Found Array Loading! <br>";
  98. $ret_ray = array();
  99. $f_hand = fopen($myfile, "r");
  100. while (!feof($file_handle)) {
  101. $ret_ray[] = fgets($file_handle);
  102. }
  103. fclose($file_handle);
  104. return $ret_ray;
  105. } // end function
  106.  
  107. function parse_to_files ($myray) {
  108. echo "Found Parse Processing! <br>";
  109.  
  110.  
  111. } // end function
  112.  
  113. function filePathParts($arg1) {
  114. echo "Dir=>".$arg1['dirname']."<br>";
  115. echo "Bas=>".$arg1['basename']."<br>";
  116. echo "Ext=>".$arg1['extension']."<br>";
  117. echo "Fnm=>".$arg1['filename']."<br>";
  118. }
  119.  
  120. function truepath($path){
  121. // whether $path is unix or not
  122. $unipath=strlen($path)==0 || $path{0}!='/';
  123. // attempts to detect if path is relative in which case, add cwd
  124. if(strpos($path,':')===false && $unipath)
  125. $path=getcwd().DIRECTORY_SEPARATOR.$path;
  126. // resolve path parts (single dot, double dot and double delimiters)
  127. $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
  128. $parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
  129. $absolutes = array();
  130. foreach ($parts as $part) {
  131. if ('.' == $part) continue;
  132. if ('..' == $part) {
  133. array_pop($absolutes);
  134. } else {
  135. $absolutes[] = $part;
  136. }
  137. }
  138. $path=implode(DIRECTORY_SEPARATOR, $absolutes);
  139. // resolve any symlinks
  140. if(file_exists($path) && linkinfo($path)>0)$path=readlink($path);
  141. // put initial separator that could have been lost
  142. $path=!$unipath ? '/'.$path : $path;
  143. return $path;
  144. }
  145.  
  146. ?>
  147.  
  148. <html>
  149. <head>
  150. <title>SQL File Parsing</title>
  151. </head>
  152.  
  153. <body bgcolor='#ffbdaa'>
  154. <!-- FORM NAME='sqlp' METHOD='POST' enctype="multipart/form-data" -->
  155. <FORM NAME='sqlp' METHOD='POST' ACTION="<?php echo $_SERVER['PHP_SELF']; ?>">
  156. <table align='center' width='70%' border='0' cellspacing='0' cellpadding='0'>
  157. <tr>
  158. <td>&nbsp;</td>
  159. </tr>
  160.  
  161. <tr>
  162. <td colspan=3 align='center'><h2>SQL File Parsing</h2></td>
  163. </tr>
  164.  
  165. <tr>
  166. <td>&nbsp;</td>
  167. </tr>
  168.  
  169. <tr>
  170. <td width='38%' align='right'>File to Parse:</h2></td>
  171. <td>&nbsp;</td>
  172. <td width='58%' align='left'>
  173. <input type='file' id='pfile' name='pfile' value='Browse'>
  174. </td>
  175. </tr>
  176.  
  177. <tr>
  178. <td>&nbsp;</td>
  179. </tr>
  180.  
  181. <tr>
  182. <td colspan=3 width='45%' align='center'>
  183. <input type='submit' value='Parse File'>
  184. </td>
  185. </tr>
  186.  
  187. <tr>
  188. <td>&nbsp;</td>
  189. </tr>
  190. </table>
  191.  
  192. </form>
  193. </body>
  194. </html>
  195.  
  196.  
  197. PARSE_SQL_CLASS.PHP
  198.  
  199. <?php
  200. /***************************************************************************/
  201. /* Author: Nyle E. Davis Create Date: 12/10/05 */
  202. /* E-Mail: davisoftaec@gmail.com Revision: 1.0 */
  203. /* Purpose: This file contains the preset vars and definitions that */
  204. /* direct the calling and processing for the current process. */
  205. /***************************************************************************/
  206. /* Mod by: Nyle E. Davis Mod Date: 12/09/05 */
  207. /* Change: Added debug vars of: */
  208. /* DBUG_ME ==> Does array and var dumps, */
  209. /* INFO_ME ==> Allow run of phpinfo() statements, */
  210. /* PRUN_ME ==> Does other process run debugging. */
  211. /* SHOW_ME ==> Shows/echos all set vars, */
  212. /* STOP_ME ==> Stop processing with a die() statement. */
  213. /* Notes: The debug var PRUN_ME can be assigned values of 0-9 where 0 is */
  214. /* off and the other values can turn on upto 9 debugging tests. */
  215. /***************************************************************************/
  216.  
  217. define('MSB_VERSION', '1.0.0');
  218. define('MSB_NL', "\r\n");
  219.  
  220. class BackupMySQL {
  221. var $server = 'localhost';
  222. var $port = 3306;
  223. var $username = 'root';
  224. var $password = '';
  225. var $database = '';
  226. var $link_id = -1;
  227. var $connected = false;
  228. var $tables = array();
  229. var $drop_tables = true;
  230. var $struct_only = false;
  231. var $comments = true;
  232. var $backup_dir = '/backups/DB Backups/Dumps';
  233. var $fname_format = 'Y_m_d';
  234. var $error = '';
  235.  
  236. function Execute($file_name,$database,$table_name,$start,$end,$max_rows) {
  237. $this->_Connect();
  238. if (!($sql = $this->_Retrieve($database,$table_name,$start,$end,$max_rows)))return false;
  239. return $this->_SaveToFile($file_name, $sql);
  240. }
  241.  
  242. function _Connect() {
  243. $value = false;
  244. if (!$this->connected) {
  245. $host = $this->server . ':' . $this->port;
  246. $this->link_id = mysql_connect($host, $this->username, $this->password);
  247. }
  248. if ($this->link_id) {
  249. if (empty($this->database)) {
  250. $value = true;
  251. } elseif ($this->link_id !== -1) {
  252. $value = mysql_select_db($this->database, $this->link_id);
  253. } else {
  254. $value = mysql_select_db($this->database);
  255. } // end if empty
  256. } // end if $this->link_id
  257. if (!$value) {
  258. $this->error = mysql_error();
  259. } // end if !value
  260. return $value;
  261. } // end function _Connect
  262.  
  263. function _DisConnect() {
  264. $value = false;
  265. if ($this->connected) {
  266. $host = $this->server . ':' . $this->port;
  267. $this->link_id = mysql_connect($host, $this->username, $this->password);
  268. }
  269. } // end function _Connect
  270.  
  271. function _Query($sql) {
  272. if ($this->link_id !== -1) {
  273. $result = mysql_query($sql, $this->link_id);
  274. } else {
  275. $result = mysql_query($sql);
  276. } // end if $this->link_id
  277. if (!$result) {
  278. $this->error = mysql_error();
  279. } // end if !result
  280. return $result;
  281. } // end function _Query
  282.  
  283. function _GetDBSize($db_max) {
  284. $this->_Connect();
  285. $v_ray = array(); // Set output array
  286. $value = array(); // Set array for small DBs
  287. $large = array(); // Set array for large DBs
  288. $sqlvl = "SELECT `table_schema`, ".
  289. "sum( data_length + index_length ) / 1024 / 1024 ".
  290. "`db_size` FROM `information_schema`.`tables` ".
  291. "GROUP BY `table_schema`;"
  292. if (!($result = $this->_Query('$sqlvl'))) { return false; } // end if !result
  293. while ($row = mysql_fetch_row($result)) {
  294. if ($row['db_size']>$db_max) {
  295. $large[] = $row;
  296. } else {
  297. $value[] = $row;
  298. }
  299. } // end while $row
  300. $v_ray = array(0=>$value,1=>$large);
  301. return $v_ray;
  302. } // end function _GetDBSize
  303.  
  304. function _GetTBLSize($db_max,$tbl_array) {
  305. $this->_Connect();
  306. $v_ray = array(); // Set output array
  307. $value = array(); // Set array for small DBs
  308. $large = array(); // Set array for large DBs
  309. $sqlvs = "SELECT `table_schema`, , `table_name`".
  310. "sum( data_length + index_length ) / 1024 / 1024 ".
  311. "`db_size` FROM `information_schema`.`tables` ";
  312. $sqlve = "GROUP BY `table_name` ORDER BY `table_schema` , `table_name`;";
  313. foreach ($tbl_array as $key => $val) {
  314. echo "K=> $key V=> $val <br>";
  315. }
  316. //return;
  317. if (!($result = $this->_Query('$sqlvl'))) { return false; } // end if !result
  318. while ($row = mysql_fetch_row($result)) {
  319. if ($row['db_size']>$db_max) {
  320. $large[] = $row;
  321. } else {
  322. $value[] = $row;
  323. }
  324. } // end while $row
  325. $v_ray = array(0=>$value,1=>$large);
  326. return $v_ray;
  327. } // end function _GetTBLSize
  328.  
  329.  
  330.  
  331. // HERE HERE HERE HERE HERE HERE HERE
  332.  
  333. function _ExportList($list_ray) {
  334.  
  335.  
  336. } // end function _GetTBLSize
  337.  
  338. function _ImportList($list_ray) {
  339.  
  340.  
  341. } // end function _GetTBLSize
  342.  
  343. function _ParseLarge($table) {
  344.  
  345.  
  346. } // end function _GetTBLSize
  347.  
  348. function _GetTables($database) {
  349. $this->_Connect();
  350. $value = array();
  351. if (!($result = $this->_Query('SHOW TABLES'))) {
  352. return false;
  353. } // end if !(result
  354. while ($row = mysql_fetch_row($result)) {
  355. if (!($result1 = $this->_Query('SELECT count(*) FROM `'.$row[0].'`'))) {
  356. return false;
  357. }
  358. $row1 = mysql_fetch_row($result1);
  359. $value[] = $row[0].':'.$row1[0];
  360. } // end while $row
  361. if (!sizeof($value)) {
  362. $this->error = 'No tables found in database.';
  363. return false;
  364. } // end if !sizeof
  365. return $value;
  366. } // end function _GetTables
  367.  
  368. function GetTables($database) {
  369. return $this->_GetTables($database);
  370. } // end function GetTables
  371.  
  372. function _DumpTable($database,$table,$start,$end,$row_count) {
  373. $value = '';
  374. if ($start==0) {
  375. if ($this->comments) {
  376. $value .= '#' . MSB_NL;
  377. $value .= '# Table structure for table `'.$table. '`'. MSB_NL;
  378. $value .= '#' . MSB_NL . MSB_NL;
  379. } // end if $this->comments
  380. if ($this->drop_tables) {
  381. $value .= 'DROP TABLE IF EXISTS `'.$table. '`;'. MSB_NL;
  382. } // end if $this->drop_tables
  383. if (!($result = $this->_Query('SHOW CREATE TABLE `'.$table.'`'))) {
  384. return false;
  385. } // end if !$result
  386. $row = mysql_fetch_assoc($result);
  387. $value .= str_replace("\n", MSB_NL, $row['Create Table']) . ';';
  388. $value .= MSB_NL . MSB_NL;
  389. } // end if $start
  390. if (!$this->struct_only) {
  391. if ($start==0 && $this->comments) {
  392. $value .= '#' . MSB_NL;
  393. $value .= '# Dumping data for table `' . $table . '`' . MSB_NL;
  394. $value .= '#' . MSB_NL . MSB_NL;
  395. } // end if $start
  396. $value .= $this->_GetInserts($database,$table,$start,$end);
  397. } // end if !$this->struct_only
  398. if ($end>=$row_count) { $value .= MSB_NL . MSB_NL; }
  399. return $value;
  400. }
  401.  
  402. function _GetInserts($database,$table,$start,$end)
  403. {
  404. $value = '';
  405. if (!($result = $this->_Query('SELECT * FROM `'.$table.'` LIMIT '.$start.','.$end)))
  406. {
  407. return false;
  408. }
  409. while ($row = mysql_fetch_row($result))
  410. {
  411. $values = '';
  412. foreach ($row as $data)
  413. {
  414. $values .= '\'' . addslashes($data) . '\', ';
  415. }
  416. $values = substr($values, 0, -2);
  417. $value .= 'INSERT INTO `'.$table . '` VALUES (' . $values . ');' . MSB_NL;
  418. }
  419. return $value;
  420. }
  421.  
  422. function _Retrieve($database,$table_name,$start,$end,$row_count)
  423. {
  424. $value = '';
  425. if (!$this->_Connect())
  426. {
  427. return false;
  428. }
  429. if ($start==0 && $this->comments)
  430. {
  431. $value .= '#' . MSB_NL;
  432. $value .= '# MySQL database dump' . MSB_NL;
  433. $value .= '# Created by Database Backup class, ver. ' . MSB_VERSION . MSB_NL;
  434. $value .= '#' . MSB_NL;
  435. $value .= '# Host: ' . $this->server . MSB_NL;
  436. $value .= '# Generated: ' . date('M j, Y') . ' at ' . date('H:i') . MSB_NL;
  437. $value .= '# MySQL version: ' . mysql_get_server_info() . MSB_NL;
  438. $value .= '# PHP version: ' . phpversion() . MSB_NL;
  439. if (!empty($this->database))
  440. {
  441. $value .= '#' . MSB_NL;
  442. $value .= '# Database: `' . $this->database . '`' . MSB_NL;
  443. }
  444. $value .= '#' . MSB_NL . MSB_NL . MSB_NL;
  445. }
  446.  
  447. if (!($table_dump = $this->_DumpTable($database,$table_name,$start,$end,$row_count)))
  448. {
  449. $this->error = mysql_error();
  450. return false;
  451. }
  452. $value .= $table_dump;
  453.  
  454. return $value;
  455. }
  456.  
  457. function _SaveToFile($fname, $sql)
  458. {
  459. if (!($f = fopen($fname, 'a')))
  460. {
  461. $this->error = 'Can\'t create the output file.';
  462. return false;
  463. }
  464. fwrite($f, $sql);
  465. fclose($f);
  466. return true;
  467. } // end function _SaveToFile
  468.  
  469. } // end class
  470.  
  471. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement