db-sync.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. session_start();
  3. date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date()
  4. if(file_exists(".config.php")) include(".config.php");
  5. SEF('DEBUG_S');
  6. SEF("ZAP_SQL");
  7. SEF("USERS_COLUMN_INIT");
  8. SEF("AUTHORIZE_USER");
  9. SEF("AUTHORIZE_USER_LOGIN");
  10. //SEF("AUTHORIZE_USER_BY_AUTH_MODULE");
  11. //SEF("AUTHORIZE_USER_LOGIN_BY_AUTH_MODULE");
  12. SEF("GETFORMITEM");
  13. SEF("T_WORKPOINTS_USER_SELECT");
  14. require_once dirname(__FILE__) . '/' . 'se-lib' . '/' . 'Lib.php';
  15. Lib::loadClass('V');
  16. Lib::loadClass('User');
  17. Lib::loadClass('App');
  18. Lib::loadClass('DB');
  19. $conn = DB::connect();
  20. $task = V::get('task', '', $_REQUEST);
  21. if ($task) {
  22. $con = new SyncDB();
  23. $task_fun = 'task_'.$task;
  24. if (method_exists($con, $task_fun)) {
  25. $con->$task_fun();
  26. } else {
  27. die('task not exists');
  28. }
  29. } else {
  30. die('no task');
  31. }
  32. // ==================== Sync DB ===================
  33. class SyncDB {
  34. var $output;
  35. function __construct() {
  36. $this->output = V::get('output', '', $_REQUEST);
  37. }
  38. function write( $str ) {
  39. if ($this->output) {
  40. error_log($str, 3, $this->output);
  41. } else {
  42. echo $str;
  43. }
  44. }
  45. function task_export_csv() {
  46. $tbl = V::get('tbl', '', $_REQUEST);
  47. if (!$tbl) return;
  48. $sql = new stdClass();
  49. $sql->fields = array();
  50. $sql->limit = V::get('limit', '', $_REQUEST);
  51. $sql->offset = V::get('offset', '', $_REQUEST);
  52. $sql->out_where = 'where 1=1';
  53. $sql->out_limit = '';
  54. //if (($sql->limit && $sql->offset)? ' limit '.$sql->limit : '');
  55. /*
  56. [FROM table_references
  57. [WHERE where_condition]
  58. [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]]
  59. [HAVING where_condition]
  60. [ORDER BY {col_name | expr | position} [ASC | DESC], ...]
  61. [LIMIT {[offset,] row_count | row_count OFFSET offset}]
  62. [PROCEDURE procedure_name(argument_list)]
  63. [INTO OUTFILE 'file_name' export_options
  64. | INTO DUMPFILE 'file_name'
  65. | INTO var_name [, var_name]]
  66. [FOR UPDATE | LOCK IN SHARE MODE]]
  67. *
  68. INTO OUTFILE '".$sql->out_file."'
  69. FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
  70. LINES TERMINATED BY '\\r\\n';
  71. */
  72. $this->output = dirname( __FILE__ ). '/db-sync-tmp/'.'sync_db_'.session_id().'-'.$tbl.'.csv';
  73. if (file_exists($this->output)) {
  74. unlink($this->output);
  75. }
  76. $query = "select t.*
  77. from `".$tbl."` as t
  78. ".$sql->out_where."
  79. ".$sql->out_limit."
  80. ";
  81. $res = DB::query( $query, false );
  82. $csv_sep = ',';
  83. $row_cols = array();
  84. while ($r = DB::fetch( $res )) {
  85. if (empty($row_cols)) {
  86. $row_cols = array_keys(get_object_vars($r));
  87. $this->write();
  88. $first = true;
  89. foreach ($row_cols as $field) {
  90. if ($first) {
  91. $first = false;
  92. } else {
  93. $this->write($csv_sep);
  94. }
  95. $this->write('"'.str_replace('"', '""', $field).'"');
  96. }//end foreachs
  97. $this->write("\n");
  98. }
  99. $first = true;
  100. foreach ($row_cols as $field) {
  101. if ($first) {
  102. $first = false;
  103. } else {
  104. $this->write($csv_sep);
  105. }
  106. $this->write('"'.str_replace('"', '""', $r->$field).'"');
  107. }//end foreach
  108. $this->write("\n");
  109. }
  110. }
  111. function task_show_tables() {
  112. $field = 'Tables_in_'."SES_USERS2";
  113. $sql = "show tables; ";
  114. $res = DB::query( $sql );
  115. while ($r = DB::fetch( $res )) {
  116. if (isset($r->$field)) {
  117. echo'<br />'. $r->$field;
  118. echo' <a href="'."?task=export_csv&tbl=".$r->$field.'">'."export cvs".'</a>';
  119. } else {
  120. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">Unknown row=';print_r($r);echo'</pre>';
  121. }
  122. }
  123. }
  124. }// class