Пример #1
0
 function export_rows($rows)
 {
     foreach ($rows as $row) {
         echo $this->charset->utf2local('"' . implode('","', $this->_escape(removeBom($row))) . '"', 'zh') . "\r\n";
     }
     flush();
 }
Пример #2
0
 function get_area_select($path, $params, $selected_id = null)
 {
     $params['depth'] = $params['depth'] ? $params['depth'] : 1;
     $html = '<select onchange="selectArea(this,this.value,' . ($params['depth'] + 1) . ')">';
     $html .= __('<option value="_NULL_">请选择...</option>');
     $package = $params['package'] ? $params['package'] : $this->get_default();
     $sql = 'select region_id,p_region_id,local_name,region_grade from sdb_regions where p_region_id' . ($path ? '=' . intval($path) : ' is null') . ' and package="' . $package . '" order by ordernum asc,region_id asc';
     if ($rows = $this->db->select($sql)) {
         foreach ($rows as $item) {
             if ($item['region_grade'] <= $this->system->getConf('system.area_depth')) {
                 $selected = $selected_id == $item['region_id'] ? 'selected="selected"' : '';
                 if ($params['depth'] < $this->system->getConf('system.area_depth')) {
                     $html .= '<option has_c="true" value="' . $item['region_id'] . '" ' . $selected . '>' . removeBom($item['local_name']) . '</option>';
                 } else {
                     $html .= '<option value="' . $item['region_id'] . '" ' . $selected . '>' . removeBom($item['local_name']) . '</option>';
                 }
             } else {
                 $no = true;
             }
         }
         $html .= '</select>';
         if ($no) {
             $html = "";
         }
         return $html;
     } else {
         return false;
     }
 }
Пример #3
0
 function export_rows($rows)
 {
     include CORE_DIR . '/lib/charset/char_local.php';
     include CORE_DIR . '/lib/charset/char_utf.php';
     foreach ($rows as $row) {
         $row = removeBom($row);
         $char = implode('","', $this->_escape($row));
         foreach ($char_utf as $k => $v) {
             $char = str_replace($v, $char_replace[$k], $char);
         }
         $char = $this->charset->utf2local('"' . $char . '"', 'zh');
         foreach ($char_local as $k => $v) {
             $char = str_replace($char_replace[$k], $v, $char);
         }
         echo $char . "\r\n";
     }
     flush();
 }
/**
 * Analyzes the first line of a MSD-Backup. Expects it as string parameter.
 *
 * @param string $line The statusline from the backup to analyze
 *
 * @return array Extracted information as array
 */
function readStatusline($filename)
{
    global $config;
    /*AUFBAU der Statuszeile:
      -- Status:nr of tables:records:Multipart:Databasename:script:scriptversion:Comment:
      MySQL-Version:flags (unused):SQLCommandBeforeBackup:SQLCommandAfterBackup:Charset:EXTINFO
      */
    $gz = substr($filename, -3) == '.gz' ? true : false;
    $fh = $gz ? gzopen($config['paths']['backup'] . $filename, 'r') : fopen($config['paths']['backup'] . $filename, 'r');
    if (!$fh) {
        v(debug_backtrace());
        die;
    }
    $line = $gz ? gzgets($fh) : fgets($fh);
    $gz ? gzclose($fh) : fclose($fh);
    $statusline = array();
    $line = removeBom($line);
    if (substr($line, 0, 8) != "# Status" && substr($line, 0, 9) != "-- Status" || substr($line, 0, 10) == '-- StatusC') {
        //Fremdfile
        $statusline['tables'] = -1;
        $statusline['records'] = -1;
        $statusline['part'] = 'MP_0';
        $statusline['dbname'] = 'unknown';
        $statusline['script'] = '';
        $statusline['scriptversion'] = '';
        $statusline['comment'] = '';
        $statusline['mysqlversion'] = 'unknown';
        $statusline['flags'] = '2222222';
        $statusline['sqlbefore'] = '';
        $statusline['sqlafter'] = '';
        $statusline['charset'] = '?';
    } else {
        // MySQLDumper-File - Informationen extrahieren
        $s = explode(':', $line);
        if (count($s) < 12) {
            //fehlenden Elemente auffüllen
            $c = count($s);
            array_pop($s);
            for ($i = $c - 1; $i < 12; $i++) {
                $s[] = '';
            }
        }
        $statusline['tables'] = $s[1];
        $statusline['records'] = $s[2];
        $statusline['part'] = $s[3] == '' || $s[3] == 'MP_0' ? 'MP_0' : $s[3];
        $statusline['dbname'] = $s[4];
        $statusline['script'] = $s[5];
        $statusline['scriptversion'] = $s[6];
        $statusline['comment'] = $s[7];
        $statusline['mysqlversion'] = $s[8];
        if (isset($s[12]) && trim($s[12]) != 'EXTINFO') {
            $statusline['charset'] = $s[12];
        } else {
            $statusline['charset'] = '?';
        }
    }
    return $statusline;
}
/**
 * Reads next lines from file and extracts a complete SQL-Command
 *
 * @return string $sql_command The complete Query
 */
function getQueryFromFile($showErrors = true)
{
    global $restore, $config, $databases, $lang, $dbo, $log;
    //Init
    $complete_sql = '';
    $sqlparser_status = 0;
    $query_found = false;
    //Parse
    while (!$query_found && !$restore['fileEOF'] && !$restore['EOB']) {
        //get next line from file
        $zeile = $restore['compressed'] ? gzgets($restore['filehandle']) : fgets($restore['filehandle']);
        // if we are at the beginning of a file look for BOM and remove it
        if ($restore['offset'] == 0) {
            $zeile = removeBom($zeile);
        }
        // what kind of command did we read from the file?
        if ($sqlparser_status == 0) {
            // build comparing line in uppercase
            $zeile2 = strtoupper(trim(substr($zeile, 0, 9)));
            // pre-build compare strings - so we need the CPU power only once :)
            $sub9 = substr($zeile2, 0, 9);
            $sub7 = substr($sub9, 0, 7);
            $sub6 = substr($sub7, 0, 6);
            $sub4 = substr($sub6, 0, 4);
            $sub3 = substr($sub4, 0, 3);
            $sub2 = substr($sub3, 0, 2);
            $sub1 = substr($sub2, 0, 1);
            if ($sub7 == 'INSERT ') {
                $sqlparser_status = 3;
                $restore['actual_table'] = getTablename($zeile, $restore['actual_table']);
            } elseif ($sub7 == 'REPLACE') {
                $sqlparser_status = 8;
                $restore['actual_table'] = getTablename($zeile, $restore['actual_table']);
            } elseif ($sub7 == 'LOCK TA') {
                $sqlparser_status = 4;
            } elseif ($sub6 == 'COMMIT') {
                $sqlparser_status = 7;
            } elseif (substr($sub6, 0, 5) == 'BEGIN') {
                $sqlparser_status = 7;
            } elseif ($sub9 == 'UNLOCK TA') {
                $sqlparser_status = 4;
            } elseif ($sub3 == 'SET') {
                $sqlparser_status = 4;
            } elseif ($sub6 == 'START ') {
                $sqlparser_status = 4;
            } elseif ($sub3 == '/*!') {
                $sqlparser_status = 5;
            } elseif ($sub9 == 'ALTER TAB') {
                $sqlparser_status = 4;
            } elseif ($sub9 == 'CREATE TA') {
                $sqlparser_status = 2;
            } elseif ($sub9 == 'CREATE AL') {
                $sqlparser_status = 2;
            } elseif ($sub9 == 'CREATE IN') {
                $sqlparser_status = 4;
            } elseif ($sub7 == 'UPDATE ') {
                $sqlparser_status = 4;
            } elseif ($sub7 == 'SELECT ') {
                $sqlparser_status = 4;
            } elseif ($sqlparser_status != 5 && $sub2 == '/*') {
                $sqlparser_status = 6;
            } elseif ($sub9 == 'DROP TABL') {
                $sqlparser_status = 1;
            } elseif ($sub9 == 'DROP VIEW') {
                $sqlparser_status = 1;
            } elseif ($sub7 == 'DELETE ') {
                $sqlparser_status = 1;
            } elseif ($sub9 == 'CREATE DA ') {
                $sqlparser_status = 7;
            } elseif ($sub9 == 'DROP DATA ') {
                $sqlparser_status = 7;
            } elseif ($sub3 == 'USE') {
                $sqlparser_status = 7;
            } elseif ($sub6 == '-- EOB' || $sub4 == '# EO') {
                $restore['EOB'] = true;
                $restore['fileEOF'] = true;
                $zeile = '';
                $zeile2 = '';
                $query_found = true;
            } elseif ($sub2 == '--' || $sub1 == '#') {
                $zeile = '';
                $zeile2 = '';
                $sqlparser_status = 0;
            }
            // continue extended Insert?
            if ($restore['extended_insert_flag'] == 1) {
                $sqlparser_status = 3;
            }
            if ($sqlparser_status == 0 && trim($complete_sql) > '' && $restore['extended_insert_flag'] == -1) {
                if ($showErrors) {
                    // unknown command -> output debug information
                    v($restore);
                    echo "<br />Sql: " . htmlspecialchars($complete_sql);
                    die('<br />' . $lang['L_UNKNOWN_SQLCOMMAND'] . ': ' . $zeile . '<br /><br />' . $complete_sql);
                } else {
                    return array(false, $complete_sql);
                }
            }
        }
        $last_char = substr(rtrim($zeile), -1);
        // retain new lines - otherwise keywords are glued together
        // e.g. 'null' and on next line 'check' would necome 'nullcheck'
        $complete_sql .= $zeile . "\n";
        if ($sqlparser_status == 3 || $sqlparser_status == 8) {
            //INSERT or REPLACE
            if (isCompleteQuery($complete_sql)) {
                $query_found = true;
                $complete_sql = trim($complete_sql);
                if (substr($complete_sql, -2) == '*/') {
                    $complete_sql = deleteInlineComments($complete_sql);
                }
                // end of extended insert found?
                if (substr($complete_sql, -2) == ');') {
                    $restore['extended_insert_flag'] = -1;
                } else {
                    if (substr($complete_sql, -2) == '),') {
                        // letztes Komme gegen Semikolon tauschen
                        $complete_sql = substr($complete_sql, 0, -1);
                        $restore['extended_inserts'] = 1;
                        $restore['extended_insert_flag'] = 1;
                    }
                }
                $compare = substr(strtoupper($complete_sql), 0, 7);
                if ($compare != 'INSERT ' && $compare != 'REPLACE') {
                    // we do have extended inserts here -> prepend insert syntax
                    // if we don't have it because of a page refresh -> get it
                    if (!isset($restore['insert_syntax'])) {
                        $restore['insert_syntax'] = Sql::getInsertSyntax($dbo, $restore['actual_table']);
                    }
                    $complete_sql = $restore['insert_syntax'] . ' VALUES ' . $complete_sql;
                } else {
                    // remember the INSERT syntax
                    $ipos = strpos(strtoupper($complete_sql), ' VALUES');
                    if (!$ipos === false) {
                        $restore['insert_syntax'] = substr($complete_sql, 0, $ipos);
                    } else {
                        if ($sqlparser_status == 3) {
                            $restore['insert_syntax'] = 'INSERT INTO `' . $restore['actual_table'] . '`';
                        } else {
                            $restore['insert_syntax'] = 'REPLACE INTO `' . $restore['actual_table'] . '`';
                        }
                    }
                }
            }
        } else {
            if ($sqlparser_status == 1) {
                // delete action
                if ($last_char == ';') {
                    $query_found = true;
                }
                $restore['actual_table'] = getTablename($complete_sql);
            } else {
                if ($sqlparser_status == 2) {
                    // Create-command is finished if there is a colon at the end of line
                    if ($last_char == ';') {
                        $restore['speed'] = $config['minspeed'];
                        // Restore this table?
                        $do_it = true;
                        if (is_array($restore['tables_to_restore'])) {
                            $do_it = false;
                            if (in_array($restore['actual_table'], $restore['tables_to_restore'])) {
                                $do_it = true;
                            } else {
                                // if we do a partial restore with selected tables and we already inserted all
                                // of them and we now have a table we don't need to restore
                                // -> we did all we need to do! Check and finish the process in that case
                                // (we don't need to further walk through the file if all needed tables are done)
                                if ($restore['table_ready'] == $restore['tables_total']) {
                                    $sqlparser_status = 0;
                                    $restore['EOB'] = true;
                                }
                            }
                        }
                        $tablename = getTablename($complete_sql);
                        if ($do_it) {
                            $complete_sql = getCorrectedCreateCommand($complete_sql);
                            $restore['table_ready']++;
                        } else {
                            $complete_sql = '';
                        }
                        $restore['actual_table'] = $tablename;
                        $query_found = true;
                        $sqlparser_status = 0;
                    }
                } else {
                    if ($sqlparser_status == 4) {
                        if ($last_char == ';') {
                            $restore['speed'] = $config['minspeed'];
                            $complete_sql = deleteInlineComments($complete_sql);
                            $query_found = true;
                        }
                    } else {
                        if ($sqlparser_status == 5) {
                            $t = strrpos($zeile, '*/;');
                            if (!$t === false) {
                                $restore['speed'] = $config['minspeed'];
                                $query_found = true;
                            }
                        } else {
                            if ($sqlparser_status == 6) {
                                $t = strrpos($zeile, '*/');
                                if (!$t === false) {
                                    $complete_sql = '';
                                    $sqlparser_status = 0;
                                }
                            } else {
                                if ($sqlparser_status == 7) {
                                    if ($last_char == ';') {
                                        $restore['speed'] = $config['minspeed'];
                                        $complete_sql = '';
                                        $sqlparser_status = 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if ($restore['compressed'] && gzeof($restore['filehandle'])) {
            $restore['fileEOF'] = true;
        } elseif (!$restore['compressed'] && feof($restore['filehandle'])) {
            $restore['fileEOF'] = true;
        }
    }
    // if special tables are selected for restoring, check if this query belongs to them
    if (is_array($restore['tables_to_restore']) && !in_array($restore['actual_table'], $restore['tables_to_restore'])) {
        $complete_sql = '';
    }
    //detect if a table is finished and write log message
    if ($sqlparser_status != 3 && $sqlparser_status != 8 && in_array($restore['last_parser_status'], array(3, 8))) {
        if (isset($restore['records_inserted_table'][$restore['actual_table']])) {
            $message = sprintf($lang['L_RESTORE_TABLE'], $restore['actual_table']) . ': ';
            $message .= sprintf($lang['L_RECORDS_INSERTED'], String::formatNumber($restore['records_inserted_table'][$restore['actual_table']]));
            $log->write(Log::PHP, $message);
        }
    }
    $restore['last_parser_status'] = $sqlparser_status;
    $complete_sql = trim($complete_sql);
    return $complete_sql;
}