function MSD_mysql_connect() { global $config, $databases; $port = isset($config['dbport']) && !empty($config['dbport']) ? ":" . $config['dbport'] : ""; $socket = isset($config['dbsocket']) && !empty($config['dbsocket']) ? ":" . $config['dbsocket'] : ""; $config['dbconnection'] = @mysql_connect($config['dbhost'] . $port . $socket, $config['dbuser'], $config['dbpass']) or die(SQLError("Datenbankverbindung", mysql_error())); if (!defined('MSD_MYSQL_VERSION')) { GetMySQLVersion(); } return true; }
function getDBInfos() { global $databases, $dump, $config, $tbl_sel, $flipped; for ($ii = 0; $ii < count($databases['multi']); $ii++) { $dump['dbindex'] = $flipped[$databases['multi'][$ii]]; $tabellen = mysql_query('SHOW TABLE STATUS FROM `' . $databases['Name'][$dump['dbindex']] . '`', $config['dbconnection']) or die('getDBInfos: ' . mysql_error()); $num_tables = mysql_num_rows($tabellen); // Array mit den gewünschten Tabellen zusammenstellen... wenn Präfix angegeben, werden die anderen einfach nicht übernommen if ($num_tables > 0) { for ($i = 0; $i < $num_tables; $i++) { $row = mysql_fetch_array($tabellen); if (isset($row['Type'])) { $row['Engine'] = $row['Type']; } if (isset($row['Comment']) && substr(strtoupper($row['Comment']), 0, 4) == 'VIEW') { $dump['table_types'][] = 'VIEW'; } else { $dump['table_types'][] = strtoupper($row['Engine']); } // check if data needs to be backed up if (strtoupper($row['Comment']) == 'VIEW' || isset($row['Engine']) && in_array(strtoupper($row['Engine']), array('MEMORY'))) { $dump['skip_data'][] = $databases['Name'][$dump['dbindex']] . '|' . $row['Name']; } if ($config['optimize_tables_beforedump'] == 1 && $dump['table_offset'] == -1) { mysql_query('OPTIMIZE `' . $row['Name'] . '`'); } if (isset($tbl_sel)) { if (in_array($row['Name'], $dump['tblArray'])) { $dump['tables'][] = $databases['Name'][$dump['dbindex']] . '|' . $row['Name']; $dump['records'][] = $databases['Name'][$dump['dbindex']] . '|' . $row['Rows']; $dump['totalrecords'] += $row['Rows']; } } elseif ($databases['praefix'][$dump['dbindex']] != '' && !isset($tbl_sel)) { if (substr($row['Name'], 0, strlen($databases['praefix'][$dump['dbindex']])) == $databases['praefix'][$dump['dbindex']]) { $dump['tables'][] = $databases['Name'][$dump['dbindex']] . '|' . $row['Name']; $dump['records'][] = $databases['Name'][$dump['dbindex']] . '|' . $row['Rows']; $dump['totalrecords'] += $row['Rows']; } } else { $dump['tables'][] = $databases['Name'][$dump['dbindex']] . '|' . $row['Name']; $dump['records'][] = $databases['Name'][$dump['dbindex']] . '|' . $row['Rows']; // Get nr of records -> need to do it this way because of incorrect returns when using InnoDBs $sql_2 = "SELECT count(*) as `count_records` FROM `" . $databases['Name'][$dump['dbindex']] . "`.`" . $row['Name'] . "`"; $res2 = @mysql_query($sql_2); if ($res2 === false) { $read_error = '(' . mysql_errno() . ') ' . mysql_error(); SQLError($read_error, $sql_2); WriteLog($read_error); if ($config['stop_with_error'] > 0) { die($read_error); } } else { $row2 = @mysql_fetch_array($res2); $row['Rows'] = $row2['count_records']; $dump['totalrecords'] += $row['Rows']; } } } // Correct total number of records; substract skipped data foreach ($dump['skip_data'] as $skip_data) { $index = false; $records_to_skip = 0; //find index of table to get the nr of records $count = sizeof($dump['tables']); for ($a = 0; $a < $count; $a++) { if ($dump['tables'][$a] == $skip_data) { $index = $a; $t = explode('|', $dump['records'][$a]); $rekords_to_skip = $t[1]; break; } } if ($index) { $dump['totalrecords'] -= $rekords_to_skip; } } } } }
function get_insert_syntax($table) { $insert = ''; $sql = 'SHOW COLUMNS FROM `' . $table . '`'; $res = mysqli_query($GLOBALS["___mysqli_ston"], $sql); if ($res) { $insert = 'INSERT INTO `' . $table . '` ('; while ($row = mysqli_fetch_object($res)) { $insert .= '`' . $row->Field . '`,'; } $insert = substr($insert, 0, strlen($insert) - 1) . ') '; } else { global $restore; v($restore); SQLError($sql, is_object($GLOBALS["___mysqli_ston"]) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)); } return $insert; }
function ExportHTML() { global $sql, $config, $lang; $header = implode("", file($config['paths']['config'] . 'html_template')); $header = str_replace("{TITLE}", 'MSD HTML-Export', $header); $footer = "\n\n</body>\n</html>"; $content = ""; $content .= '<h1>Datenbank ' . $sql['export']['db'] . '</h1>'; $time_start = time(); if (!isset($config['dbconnection'])) { MSD_mysql_connect(); } for ($table = 0; $table < count($sql['export']['tables']); $table++) { $content .= '<h2>Tabelle ' . $sql['export']['tables'][$table] . '</h2>' . "\n"; $fsql = "show fields from `" . $sql['export']['tables'][$table] . "`"; $dsql = "select * from `" . $sql['export']['tables'][$table] . "`"; //Struktur $res = MSD_query($fsql) or die(SQLError($fsql, mysql_error())); if ($res) { $field = $fieldname = $fieldtyp = array(); $structure = "<table class=\"Table\">\n"; $numfields = mysql_numrows($res); for ($feld = 0; $feld < $numfields; $feld++) { $row = mysql_fetch_row($res); $field[$feld] = $row[0]; if ($feld == 0) { $structure .= "<tr class=\"Header\">\n"; for ($i = 0; $i < count($row); $i++) { $str = mysql_fetch_field($res, $i); $fieldname[$i] = $str->name; $fieldtyp[$i] = $str->type; $structure .= "<th>" . $str->name . "</th>\n"; } $structure .= "</tr>\n<tr>\n"; } for ($i = 0; $i < count($row); $i++) { $structure .= "<td class=\"Object\">" . ($row[$i] != "" ? $row[$i] : " ") . "</td>\n"; } $structure .= "</tr>\n"; } $structure .= "</table>\n"; } if ($sql['export']['htmlstructure'] == 1) { $content .= "<h3>Struktur</h3>\n" . $structure; } //Daten $res = MSD_query($dsql) or die(SQLError($dsql, mysql_error())); if ($res) { $anz = mysql_num_rows($res); $content .= "<h3>Daten ({$anz} Datensätze)</h3>\n"; $content .= "<table class=\"Table\">\n"; for ($feld = 0; $feld < count($field); $feld++) { if ($feld == 0) { $content .= "<tr class=\"Header\">\n"; for ($i = 0; $i < count($field); $i++) { $content .= "<th>" . $field[$i] . "</th>\n"; } $content .= "</tr>\n"; } } for ($d = 0; $d < $anz; $d++) { $row = mysql_fetch_row($res); $content .= "<tr>\n"; for ($i = 0; $i < count($row); $i++) { $content .= '<td class="Object">' . ($row[$i] != "" ? $row[$i] : " ") . "</td>\n"; } $content .= "</tr>\n"; } } $content .= "</table>"; } CSVOutput($header . $content . $footer); }
$config['dbport'] = (int) $_POST['dbport']; } else { $config['dbport'] = 3306; } $config['dbsocket'] = $_POST['dbsocket']; include './inc/classes/db/MsdDbFactory.php'; $dbo = MsdDbFactory::getAdapter($config['dbhost'], $config['dbuser'], $config['dbpass'], $config['dbport'], $config['dbsocket']); $connectMsg = $dbo->dbConnect(); if ($connectMsg === true) { if ($config['dbmanual'] > '') { $dbDetect = searchDatabases(1, $config['dbmanual']); } else { $dbDetect = searchDatabases(1); } } else { $tpl->assign_block_vars('CONNECTION_ERROR', array('MSG' => SQLError('', $connectMsg, true))); $dbDetect = false; } if (false !== $dbDetect) { $saveButton = str_replace('\'', '\\\'', $lang['L_SAVEANDCONTINUE']); $tpl->assign_block_vars('CONTINUE', array('SAVE_AND_CONTINUE' => $saveButton)); $tpl->assign_block_vars('OK', array()); $tpl->assign_block_vars('CONNECTION_OK', array('RESULT' => $dbDetect)); if (count($databases) == 0) { $tpl->assign_block_vars('CONNECTION_OK_BUT_NO_DB', array()); } } } $tpl->assign_vars(array('SESSION_ID' => session_id(), 'DB_HOST' => $config['dbhost'], 'DB_USER' => $config['dbuser'], 'DB_PASS' => $config['dbpass'], 'DB_MANUAL' => $config['dbmanual'], 'ICON_OK' => $icon['ok'])); $tpl->pparse('show'); }
} } } // Bei MySQL-Fehlern sofort abbrechen und Info ausgeben $meldung = mysql_error($config['dbconnection']); if ($meldung != '') { if (strtolower(substr($meldung, 0, 15)) == 'duplicate entry') { ErrorLog('RESTORE', $databases['db_actual'], $sql_command, $meldung, 1); $restore['notices']++; } else { if ($config['stop_with_error'] == 0) { Errorlog("RESTORE", $databases['db_actual'], $sql_command, $meldung); $restore['errors']++; } else { Errorlog("RESTORE", $databases['db_actual'], $sql_command, 'Restore failed: ' . $meldung, 0); SQLError($sql_command, $meldung); die; } } } } $a++; $dauer = time() - $restore['startzeit']; } $eingetragen = $a - 1; } $restore['offset'] = $restore['compressed'] ? gztell($restore['filehandle']) : ftell($restore['filehandle']); if ($restore['compressed']) { gzclose($restore['filehandle']); } else { fclose($restore['filehandle']);
function ResetModule($id) { /* get module name */ $sqlstring = "select module_name from modules where module_id = {$id}"; $result = mysql_query($sqlstring) or die(SQLError(__FILE__, __LINE__, mysql_error(), $sqlstring)); $row = mysql_fetch_array($result, MYSQL_ASSOC); $scriptname = $row['module_name']; /* delete all lock files */ $path = $GLOBALS['cfg']['scriptdir'] . "/lock/{$scriptname}*"; //echo "$path<br>"; $files = glob($path); //print_r($files); foreach ($files as $file) { if (stripos($file, $scriptname) !== false) { ?> <div class="message">Deleting lock file [<?php echo $file; ?> ]</div><?php unlink($file); } } /* update DB to have 0 instances, status=stopped and lastfinish=now() */ $sqlstring = "update modules set module_status = 'stopped', module_numrunning = 0, module_laststop = now() where module_id = {$id}"; //echo "$sqlstring<br>"; $result = mysql_query($sqlstring) or die(SQLError(__FILE__, __LINE__, mysql_error(), $sqlstring)); }
/** * Receive all possible MySQL character sets and save standard to $config['mysql_standard_charset'] */ function get_sql_encodings() { global $config; unset($config['mysql_possible_character_sets']); if (!isset($config['dbconnection'])) { MSD_mysql_connect(); } $erg = false; $config['mysql_standard_character_set'] = ''; $config['mysql_possible_character_sets'] = array(); if (!defined('MSD_MYSQL_VERSION')) { GetMySQLVersion(); } $v = explode('.', MSD_MYSQL_VERSION); $config['mysql_can_change_encoding'] = false; if ($v[0] <= 4 && $v[1] < 1 || $v[0] <= 3) { // MySQL < 4.1 $config['mysql_can_change_encoding'] = false; $sqlt = 'SHOW VARIABLES LIKE \'character_set%\''; $res = MSD_query($sqlt) or die(SQLError($sqlt, mysql_error())); if ($res) { while ($row = mysql_fetch_row($res)) { if ($row[0] == 'character_set') { $config['mysql_standard_character_set'] = $row[1]; if ($v[0] == 3) { $config['mysql_possible_character_sets'][0] = $row[1]; } } if ($row[0] == 'character_sets' && $v[0] > 3) { $config['mysql_possible_character_sets'] = explode(' ', $row[1]); sort($config['mysql_possible_character_sets']); } } } } else { // MySQL-Version >= 4.1 $config['mysql_can_change_encoding'] = true; $sqlt = 'SHOW CHARACTER SET'; $res = MSD_query($sqlt) or die(SQLError($sqlt, mysql_error())); if ($res) { while ($row = mysql_fetch_row($res)) { $config['mysql_possible_character_sets'][] = $row[0] . ' - ' . $row[1]; } sort($config['mysql_possible_character_sets']); } $sqlt = 'SHOW VARIABLES LIKE \'character_set_connection\''; $res = MSD_query($sqlt) or die(SQLError($sqlt, mysql_error())); if ($res) { while ($row = mysql_fetch_row($res)) { $config['mysql_standard_character_set'] = $row[1]; } } } }
function ImportCreateTable() { global $sql, $lang, $db, $config; $tbl = array(); $tabellen = mysql_list_tables($db, $config['dbconnection']); $num_tables = mysql_num_rows($tabellen); for ($i = 0; $i < $num_tables; $i++) { $tbl[] = strtolower(mysql_tablename($tabellen, $i)); } $i = 0; $sql['import']['table'] = $sql['import']['table'] . $i; while (in_array($sql['import']['table'], $tbl)) { $sql['import']['table'] = substr($sql['import']['table'], 0, strlen($sql['import']['table']) - 1) . ++$i; } $create = "CREATE TABLE `" . $sql['import']['table'] . "` (" . ($sql['import']['createindex'] == 1 ? '`import_id` int(11) unsigned NOT NULL auto_increment, ' : ''); if ($sql['import']['namefirstline']) { for ($i = 0; $i < count($sql['import']['first_zeile']); $i++) { $create .= '`' . $sql['import']['first_zeile'][$i] . '` VARCHAR(250) NOT NULL, '; } } else { for ($i = 0; $i < count($sql['import']['first_zeile']); $i++) { $create .= '`FIELD_' . $i . '` VARCHAR(250) NOT NULL, '; } } if ($sql['import']['createindex'] == 1) { $create .= 'PRIMARY KEY (`import_id`) '; } else { $create = substr($create, 0, strlen($create) - 2); } $create .= ') ' . (MSD_NEW_VERSION ? 'ENGINE' : 'TYPE') . "=MyISAM COMMENT='imported at " . date("l dS of F Y H:i:s A") . "'"; $res = mysql_query($create, $config['dbconnection']) || die(SQLError($create, mysql_error())); return 1; }
// we've got a mysql error $sqlError = $e->getMessage(); if ($sqlError != '') { if (strtolower(substr($sqlError, 0, 15)) == 'duplicate entry') { writeToErrorLog($config['db_actual'], $sqlCommand, $sqlError, 1); $restore['notices']++; } else { if ($config['stop_with_error'] == 0) { // according to the config we continue restoring // but log the error writeToErrorLog($config['db_actual'], $sqlCommand, $sqlError, 0); $restore['errors']++; } else { // we should die if errors occur -> print error message writeToErrorLog($config['db_actual'], $sqlCommand, 'Fatal error, restore failed: ' . $sqlError, 0); SQLError($sqlCommand, $sqlError); $restore['restore_in_progress'] = 0; die; // TODO clean end of process - last message is not // logged on restore screen and // not sent back to client. Flag missing here that // should be handled via JSON } } } } } $timeElapsed = time() - $restore['page_start_time']; } if ($restore['compressed']) { $restore['offset'] = gztell($restore['filehandle']);
function get_insert_syntax($table) { $insert = ''; $sql = 'SHOW COLUMNS FROM `' . $table . '`'; $res = mysql_query($sql); if ($res) { $insert = 'INSERT INTO `' . $table . '` ('; while ($row = mysql_fetch_object($res)) { $insert .= '`' . $row->Field . '`,'; } $insert = substr($insert, 0, strlen($insert) - 1) . ') '; } else { global $restore; v($restore); SQLError($sql, mysql_error()); } return $insert; }
$sql['export']['namefirstline'] = isset($_POST['f_export_namefirstline' . $format]) ? $_POST['f_export_namefirstline' . $format] : 0; $sql['export']['sendfile'] = $_POST['f_export_sendresult']; $sql['export']['compressed'] = isset($_POST['f_export_compressed']) ? $_POST['f_export_compressed'] : 0; $sql['export']['exportfile'] = ""; $sql['export']['xmlstructure'] = isset($_POST['f_export_xmlstructure']) ? $_POST['f_export_xmlstructure'] : 0; $sql['export']['htmlstructure'] = isset($_POST['f_export_htmlstructure']) ? $_POST['f_export_htmlstructure'] : 0; //ausgewählte Tabellen if (isset($_POST['f_export_tables'])) { $sql['export']['tables'] = $_POST['f_export_tables']; } } else { CheckcsvOptions(); } //Tabellenliste $sqlt = "SHOW TABLE STATUS FROM `{$db}`"; $res = MSD_query($sqlt) or die(SQLError($sqlt, mysql_error())); if ($res) { $sql['export']['tablecount'] = mysql_numrows($res); $sql['export']['recordcount'] = 0; for ($i = 0; $i < $sql['export']['tablecount']; $i++) { $row = mysql_fetch_array($res); $tblstr .= '<option value="' . $row['Name'] . '" ' . (isset($sql['export']['tables']) && in_array($row['Name'], $sql['export']['tables']) ? "selected" : "") . '>' . $row['Name'] . ' (' . $row['Rows'] . ')</option>' . "\n"; $sql['export']['recordcount'] += $row['Rows']; } } $exaus = $aus . '<h4>' . sprintf($lang['sql_export'], $databases['Name'][$dbid]) . '</h4>'; $exaus .= '<form action="sql.php?db=' . $db . '&dbid=' . $dbid . '&context=4" method="post">' . $nl; $exaus .= '<a href="sql.php?db=' . $db . '&dbid=' . $dbid . '&context=4&import=1">' . $lang['import'] . '</a>'; $exaus .= '<h6>' . sprintf($lang['sql_export'], $databases['Name'][$dbid]) . '</h6>'; $exaus .= '<table class="bordersmall"><tr class="thead"><th>' . $lang['tables'] . '</th>' . $nl; $exaus .= '<th>' . $lang['exportoptions'] . '</th>';
function ImportCreateTable() { global $sql, $lang, $db, $config; $tbl = array(); $tabellen = mysqli_query($config['dbconnection'], "SHOW TABLES FROM {$db}"); $num_tables = mysqli_num_rows($tabellen); for ($i = 0; $i < $num_tables; $i++) { $tbl[] = strtolower(mysqli_data_seek($tabellen, $i) && ($___mysqli_tmp = mysqli_fetch_row($tabellen)) !== NULL ? array_shift($___mysqli_tmp) : false); } $i = 0; $sql['import']['table'] = $sql['import']['table'] . $i; while (in_array($sql['import']['table'], $tbl)) { $sql['import']['table'] = substr($sql['import']['table'], 0, strlen($sql['import']['table']) - 1) . ++$i; } $create = "CREATE TABLE `" . $sql['import']['table'] . "` (" . ($sql['import']['createindex'] == 1 ? '`import_id` int(11) unsigned NOT NULL auto_increment, ' : ''); if ($sql['import']['namefirstline']) { for ($i = 0; $i < count($sql['import']['first_zeile']); $i++) { $create .= '`' . $sql['import']['first_zeile'][$i] . '` VARCHAR(250) NOT NULL, '; } } else { for ($i = 0; $i < count($sql['import']['first_zeile']); $i++) { $create .= '`FIELD_' . $i . '` VARCHAR(250) NOT NULL, '; } } if ($sql['import']['createindex'] == 1) { $create .= 'PRIMARY KEY (`import_id`) '; } else { $create = substr($create, 0, strlen($create) - 2); } $create .= ') ' . (MSD_NEW_VERSION ? 'ENGINE' : 'TYPE') . "=MyISAM COMMENT='imported at " . date("l dS of F Y H:i:s A") . "'"; $res = mysqli_query($config['dbconnection'], $create) || die(SQLError($create, is_object($GLOBALS["___mysqli_ston"]) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); return 1; }
function lock_table() { global $config, $restore; if ($config["lock_tables"] == 1 && isset($restore["actual_table"]) && $restore["actual_table"] != "unbekannt") { $sql = "LOCK TABLES `" . $restore["actual_table"] . "` WRITE"; $res = MSD_query($sql) || die(SQLError("Kein Lock ausgeführt!", mysql_error())); } }