public static function get_table_content($database, $table, $crlf, $complete = false, $echo = false, $compress = false) { global $db; $str = $fields = ''; $result = $db->sql_query("SELECT * FROM {$database}.{$table}"); $fieldcount = $db->sql_numfields($result); if ($complete) { $fields = array(); for ($j = 0; $j < $fieldcount; $j++) { $fields[] = $db->sql_fieldname($j, $result); } $fields = '(' . implode(', ', $fields) . ') '; } while ($row = $db->sql_fetchrow($result, SQL_NUM)) { $str .= "INSERT INTO {$table} {$fields} VALUES ("; for ($j = 0; $j < $fieldcount; $j++) { if ($j > 0) { $str .= ', '; } # Can't use addslashes() as we don't know the value of magic_quotes_sybase. if (!isset($row[$j])) { $str .= 'NULL'; } elseif ($row[$j] != '') { $str .= "'" . $db->escape_string($row[$j]) . "'"; } else { $str .= "''"; } } $str .= ");{$crlf}"; if ($echo) { DBCtrl::output($str, $compress); $str = ''; } } $db->sql_freeresult($result); return $str; }
public static function query_file($file, &$error, $replace_prefix = false) { $error = false; if (!is_array($file)) { $tmp['name'] = $tmp['tmp_name'] = $file; $tmp['type'] = preg_match("/\\.gz\$/is", $file) ? 'application/x-gzip' : 'text/plain'; $file = $tmp; } if (empty($file['tmp_name']) || empty($file['name'])) { cpg_error('ERROR no file specified!'); } // Most servers identify a .gz as x-tar if (preg_match("/^(text\\/[a-zA-Z]+)|(application\\/(x\\-)?(gzip|tar)(\\-compressed)?)|(application\\/octet-stream)\$/is", $file['type'])) { $filedata = ''; $open = 'gzopen'; $eof = 'gzeof'; $read = 'gzgets'; $close = 'gzclose'; if (!GZIPSUPPORT) { if (preg_match("/\\.gz\$/is", $file['name'])) { $error = "Can't decompress file"; return false; } $open = 'fopen'; $eof = 'feof'; $read = 'fread'; $close = 'fclose'; } $rc = $open($file['tmp_name'], 'rb'); if ($rc) { while (!$eof($rc)) { $filedata .= $read($rc, 100000); } $close($rc); } else { $error = 'Couldn\'t open ' . $file['tmp_name'] . ' for processing'; } } else { $error = "Invalid filename: {$file['type']} {$file['name']}"; } if ($error) { return false; } $filedata = DBCtrl::remove_remarks($filedata); $queries = DBCtrl::split_sql_file($filedata, ";\n"); if (count($queries) < 1) { $error = 'There are no queries in ' . $file['name']; return false; } global $db, $prefix; set_time_limit(0); foreach ($queries as $query) { if (!$replace_prefix) { $query = preg_replace('#(TABLE|INTO|EXISTS|ON) ([a-zA-Z]*?(_))#i', "\\1 {$prefix}" . '_', $query); } else { foreach ($replace_prefix as $oldprefix => $newprefix) { if ($oldprefix != $newprefix) { $query = preg_replace("/{$oldprefix}/", $newprefix, $query); } } } if (SQL_LAYER == 'mysql' && preg_match('#^CREATE TABLE #', $query) && false === stripos($query, 'ENGINE=MyISAM')) { $query .= ' ENGINE=MyISAM'; } $db->sql_query($query); } return true; }