user_db.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3. $( "#db_id" ).change(function() {
  4. this.form.submit();
  5. });
  6. });
  7. </script>
  8. <?php
  9. /*
  10. *
  11. * OGP - Open Game Panel
  12. * Copyright (C) 2008 - 2018 The OGP Development Team
  13. *
  14. * http://www.opengamepanel.org/
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version 2
  19. * of the License, or any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. *
  30. */
  31. require_once("modules/mysql/functions.php");
  32. require_once('includes/form_table_class.php');
  33. require_once('includes/lib_remote.php');
  34. if ( function_exists('mysqli_connect') )
  35. require_once("modules/mysql/mysqli_database.php");
  36. else
  37. require_once("modules/mysql/mysql_database.php");
  38. function exec_ogp_module() {
  39. $modDb = new MySQLModuleDatabase();
  40. require("includes/config.inc.php");
  41. $modDb->connect($db_host,$db_user,$db_pass,$db_name,$table_prefix);
  42. global $view,$db;
  43. $isAdmin = $db->isAdmin( $_SESSION['user_id'] );
  44. if( $isAdmin )
  45. $game_home = $db->getGameHome($_GET['home_id']);
  46. else
  47. $game_home = $db->getUserGameHome($_SESSION['user_id'],$_GET['home_id']);
  48. if ( ! $game_home and ! $isAdmin )
  49. return;
  50. echo "<h2>".get_lang_f('mysql_dbs_for',htmlentities($game_home['home_name']))."</h2>";
  51. $home_dbs = $modDb->getMysqlDBsbyHomeId($game_home['home_id']);
  52. if(empty($home_dbs))
  53. {
  54. print_failure(get_lang_f('there_are_no_databases_assigned_for',htmlentities($game_home['home_name'])));
  55. return;
  56. }
  57. $db_array["0"] = get_lang('select_db');
  58. foreach ( $home_dbs as $home_db )
  59. {
  60. $db_array[$home_db['db_id']] = $home_db['db_name'];
  61. }
  62. $ft = new FormTable();
  63. $ft->start_form('');
  64. $ft->start_table();
  65. $ft->add_custom_field('select_db',
  66. create_drop_box_from_array($db_array,"db_id",isset($_REQUEST['db_id'])?$_REQUEST['db_id']:"0",false));
  67. $ft->end_table();
  68. $ft->end_form();
  69. $database_exists = FALSE;
  70. $server_online = FALSE;
  71. if(isset($_REQUEST['db_id']) AND $_REQUEST['db_id'] != "0")
  72. {
  73. $db_id = $_REQUEST['db_id'];
  74. $mysql_db = $modDb->getMysqlHomeDBbyId($game_home['home_id'],$db_id);
  75. if(!$mysql_db)
  76. return;
  77. if($mysql_db['remote_server_id'] != "0")
  78. {
  79. $remote_server = $db->getRemoteServer($mysql_db['remote_server_id']);
  80. $remote = new OGPRemoteLibrary($remote_server['agent_ip'],$remote_server['agent_port'],$remote_server['encryption_key'],$remote_server['timeout']);
  81. $host_stat = $remote->status_chk();
  82. if($host_stat === 1 )
  83. {
  84. $command = "mysql -h localhost -P ".$mysql_db['mysql_port']." -u root -p".$mysql_db['mysql_root_passwd'].' -e exit; echo $?';
  85. $test_mysql_conn = $remote->exec($command);
  86. if($test_mysql_conn == 0)
  87. {
  88. $user_db = $remote->exec('mysqlshow --user='.$mysql_db['db_user'].' --password='.$mysql_db['db_passwd'].' '.$mysql_db['db_name']);
  89. if($user_db != "")
  90. $database_exists = TRUE;
  91. $server_online = TRUE;
  92. }
  93. }
  94. }
  95. else
  96. {
  97. if( function_exists('mysqli_connect') )
  98. {
  99. $link = mysqli_connect($mysql_db['mysql_ip'], $mysql_db['db_user'], $mysql_db['db_passwd'], $mysql_db['db_name'], $mysql_db['mysql_port']);
  100. if ( $link !== FALSE )
  101. {
  102. $server_online = TRUE;
  103. $database_exists = TRUE;
  104. $databases = mysqli_query($link, "SHOW TABLES;");
  105. $user_db = "Database: ".$mysql_db['db_name']."\nTables:\n";
  106. while ( $table = mysqli_fetch_array($databases) ) {
  107. $user_db .= $table[0] . "\n";
  108. }
  109. mysqli_close($link);
  110. }
  111. }
  112. else
  113. {
  114. @$link = mysql_connect($mysql_db['mysql_ip'].':'.$mysql_db['mysql_port'], $mysql_db['db_user'], $mysql_db['db_passwd']);
  115. if ( $link !== FALSE )
  116. {
  117. $server_online = TRUE;
  118. if ( mysql_select_db($mysql_db['db_name'],$link) !== FALSE )
  119. {
  120. $databases = mysql_query("SHOW TABLES;");
  121. $user_db = "Database: ".$mysql_db['db_name']."\nTables:\n";
  122. while ( $table = mysql_fetch_array($databases) ) {
  123. $user_db .= $table[0] . "\n";
  124. }
  125. $database_exists = TRUE;
  126. }
  127. mysql_close($link);
  128. }
  129. }
  130. }
  131. if(isset($_POST['restore']))
  132. {
  133. $command = 'mysql --host='.$mysql_db['mysql_ip'].' --port='.$mysql_db['mysql_port'].' --user='.$mysql_db['db_user'].
  134. ' --password='.$mysql_db['db_passwd'].' '.$mysql_db['db_name'].' < ';
  135. $local_tmp = sys_get_temp_dir()."/".$_FILES["file"]["name"];
  136. move_uploaded_file($_FILES["file"]["tmp_name"], $local_tmp);
  137. if($mysql_db['remote_server_id'] != "0")
  138. {
  139. $temp_dir = trim($remote->exec('mktemp -d'));
  140. $writefile = $temp_dir."/".$_FILES["file"]["name"];
  141. $content = file_get_contents($local_tmp);
  142. $command .= $writefile;
  143. if($remote->remote_writefile($writefile, $content) === 1)
  144. $remote->exec($command);
  145. }
  146. else
  147. {
  148. $command .= $local_tmp;
  149. system($command);
  150. }
  151. unlink($local_tmp);
  152. $view->refresh('?m=mysql&p=user_db&home_id='.$game_home['home_id'].'&db_id='.$db_id,0);
  153. }
  154. if($server_online and $database_exists)
  155. {
  156. echo "<table class='database' ><tr><td>\n<div class='dragbox bloc rounded' ><h4>".get_lang('db_info')."</h4>\n".
  157. "<table class='database_info' ><tr>".
  158. "<td><b>".get_lang('mysql_ip')." :</b></td><td>".$mysql_db['mysql_ip']."</td></tr>\n".
  159. "<td><b>".get_lang('mysql_port')." :</b></td><td>".$mysql_db['mysql_port']."</td></tr>\n".
  160. "<td><b>".get_lang('db_name')." :</b></td><td>".$mysql_db['db_name']."</td></tr>\n".
  161. "<td><b>".get_lang('db_user')." :</b></td><td>".$mysql_db['db_user']."</td></tr>\n".
  162. "<td><b>".get_lang('db_passwd')." :</b></td><td>".$mysql_db['db_passwd']."</td></tr>\n".
  163. "<td><b>".get_lang('privilegies')." :</b></td><td>".$mysql_db['privilegies_str']."</td></tr></table></div>\n".
  164. "<td><div class='dragbox bloc rounded' style='background:black;' ><h4>".get_lang('db_tables')."</h4>".
  165. "<pre><xmp>".$user_db."</xmp></pre></div></td></tr></table>";
  166. if(suhosin_function_exists('system') or $mysql_db['remote_server_id'] != "0")
  167. {
  168. echo "<h2>".get_lang('db_backup')."</h2>";
  169. ?>
  170. <table class='administration-table'>
  171. <tr>
  172. <td>
  173. <form method="POST" action="?m=mysql&p=get_dump&home_id=<?php echo $game_home['home_id']; ?>&db_id=<?php echo $db_id; ?>&type=cleared" >
  174. <button name="download"><?php print_lang('download_db_backup'); ?></button>
  175. </form>
  176. <br>
  177. <form method="POST" action="?m=mysql&p=user_db&home_id=<?php echo $game_home['home_id']; ?>&db_id=<?php echo $db_id; ?>" enctype="multipart/form-data">
  178. <label for="file"><?php print_lang('sql_file'); ?>:</label>
  179. <input type="file" name="file" id="file" />
  180. <button name="restore"><?php print_lang('restore_db_backup'); ?></button>
  181. </form>
  182. </td>
  183. </tr>
  184. </table>
  185. <?php
  186. }
  187. }
  188. }
  189. }