Пример #1
0
function save_data($SIDU, $txt, $eng)
{
    $txt = @trim($txt);
    if (@substr($txt, -1) == ')') {
        $txt = @trim(@substr($txt, 0, -1));
        if (@substr($txt, -1) == ',') {
            $txt = @substr($txt, 0, -1);
        }
        $txt .= ')';
    }
    $res = @tm("SQL", $txt);
    $err = @sidu_err(1);
    if ($err) {
        return $err;
    }
    echo @html_js("Goto('menu','menu.php?id={$SIDU['0']}',1);Goto('main','db.php?id={$SIDU['0']},{$SIDU['1']},{$SIDU['2']},{$SIDU['3']},{$SIDU['4']},{$SIDU['5']},{$SIDU['6']}',1);self.close()");
    exit;
}
Пример #2
0
function main_cout($SIDU, $exp, $mode)
{
    if ($mode == "SQL") {
        $file = "sidu-sql";
    } else {
        $file = @str_replace("/", "_", $SIDU[1]) . ($SIDU[2] ? "_{$SIDU['2']}" : "");
        if (!$exp['sql'][1]) {
            $file .= "_" . $exp['tabs'][0];
        }
    }
    $file .= "_" . @date("YmdHis") . "." . $exp['ext'];
    if ($exp['zip']) {
        $fp = @fopen("/tmp/{$file}", 'w');
    }
    if (!$exp['zip'] || $exp['ext'] == "html") {
        @main_cout_str("<html>\n<head>\n<title>SIDU Export: {$file}</title>\n<style>*{font-family:monospace}", $fp);
    }
    if ($exp['ext'] == "html") {
        @main_cout_str("\n.n{color:#888;font-style:italic}\n.th td{background:#ddd}\ntd{vertical-align:top;border:solid 1px #ccc}", $fp);
    }
    if (!$exp['zip'] || $exp['ext'] == "html") {
        @main_cout_str("\n</style>\n</head>\n<body><pre>\n", $fp);
    }
    @main_cout_str("/*SIDU Export Start-------------------" . @date("Y-m-d H:i:s") . "*/\n", $fp);
    if ($mode != "SQL") {
        if ($exp['db']) {
            if ($SIDU['eng'] == 'my') {
                @main_cout_str("\nUSE `{$SIDU['1']}`;\n", $fp);
            } elseif ($SIDU['eng'] == 'pg') {
                @main_cout_str("\nSET search_path to \"{$SIDU['2']}\";\n", $fp);
            }
        }
        if ($exp['drop']) {
            foreach ($exp['tabs'] as $v) {
                if ($SIDU['eng'] != 'sl' || $v != 'sqlite_master') {
                    @main_cout_str("\nDROP " . ($SIDU[3] == 'r' ? "TABLE " : "VIEW ") . @goodname($v) . ";", $fp);
                }
            }
            @main_cout_str("\n", $fp);
        }
        if ($exp['desc']) {
            $typ = $SIDU[3] == 'r' ? "TABLE" : "VIEW";
            foreach ($exp['tabs'] as $v) {
                @main_cout_desc($SIDU, $typ, $v, $fp);
            }
            @main_cout_str("\n", $fp);
        }
    }
    if (!$exp['data']) {
        @main_cout_str("\n/*SIDU Export End-------------------*/</pre>\n</body></html>", $fp);
        return;
    }
    if ($exp['ext'] == "html") {
        @main_cout_str("</pre>", $fp);
    }
    foreach ($exp['sql'] as $i => $v) {
        if ($exp['ext'] != 'sql') {
            @main_cout_str("\n\n" . ($exp['ext'] == 'html' ? "<br/>" : "/* ") . @nl2br($exp['zip'] && $exp['ext'] != 'html' ? $v : @html8($v)) . ($exp['ext'] == 'html' ? "" : " */") . "\n", $fp);
        }
        $res = @tm("SQL", $v);
        $err = @sidu_err(1);
        if ($err) {
            @main_cout_str("\n" . ($exp['ext'] == 'html' ? "" : "/* ") . "<font color='red'>{$err}</font>" . ($exp['ext'] == 'html' ? "" : " */") . "\n", $fp);
        } else {
            @main_cout_data($SIDU, $exp, $res, $exp['tabs'][$i], $fp);
        }
    }
    @main_cout_str("\n" . ($exp['ext'] == "html" ? "<p>" : "") . "/*SIDU Export End-------------------*/" . ($exp['ext'] == "html" ? "</p>" : ""), $fp);
    if ($exp['ext'] != "html" && !$exp['zip']) {
        @main_cout_str("\n</pre>", $fp);
    }
    if ($exp['ext'] == "html" || !$exp['zip']) {
        @main_cout_str("\n\n</body></html>", $fp);
    }
    if (!$fp) {
        return;
    }
    @fclose($fp);
    $zip = new ZipArchive();
    $zipFile = $file . ".zip";
    if ($zip->open("/tmp/{$zipFile}", ZIPARCHIVE::CREATE) !== true) {
        return;
    }
    $zip->addFile("/tmp/{$file}", $file);
    $zip->close();
    @header("Expires: 0");
    @header("Content-Description: File Transfer");
    @header("Content-Type: application/zip");
    @header("Content-Disposition: attachment; filename=\"{$zipFile}\"");
    $fp = @fopen("/tmp/{$zipFile}", "rb");
    if ($fp) {
        while (!feof($fp)) {
            print fread($fp, 1024 * 8);
            flush();
            if (connection_status() != 0) {
                @fclose($fp);
                die;
            }
        }
        @fclose($fp);
    }
}
Пример #3
0
function run_sqls()
{
    global $SIDU;
    $conn = $SIDU['conn'][$SIDU[0]];
    $eng = $conn[1];
    $cook = $SIDU['cook'][$SIDU[0]];
    @tm_use_db($cook[1], $cook[2]);
    if ($_GET['sql'] == 'show vars') {
        $_POST['sqlcur'] = $eng == 'pg' ? 'SHOW ALL' : 'SHOW VARIABLES';
    } elseif (@substr($_GET['sql'], 0, 6) == 'FLUSH ') {
        if ($_GET['sql'] == 'FLUSH ALL') {
            $_POST['sqlcur'] = "FLUSH LOGS;\nFLUSH HOSTS;\nFLUSH PRIVILEGES;\nFLUSH TABLES;\nFLUSH STATUS;\nFLUSH DES_KEY_FILE;\nFLUSH QUERY CACHE;\nFLUSH USER_RESOURCES;\nFLUSH TABLES WITH READ LOCK";
        } else {
            $_POST['sqlcur'] = $_GET['sql'];
        }
    } elseif (@substr($_GET['sql'], 0, 9) == 'STATScol:') {
        $_POST['sqlcur'] = "SELECT " . @goodname(@substr($_GET['sql'], 9)) . ",count(*) FROM " . @goodname($cook[4]) . " GROUP BY 1 ORDER BY 2 DESC,1 LIMIT 20";
    } elseif ($_GET['sql']) {
        $_POST['sqlcur'] = $_GET['sql'];
    }
    $arr = @explode(chr(10), @strip($_POST['sqlcur'], 1, 0, 1));
    foreach ($arr as $v) {
        $v = @trim($v);
        if ($v) {
            $arr2[] = $v;
        }
    }
    $txt = @implode(chr(10), $arr2);
    $arr = @explode(";" . chr(10), $txt);
    foreach ($arr as $i => $sql) {
        $time_start = @microtime(true);
        $res = $eng == 'my' ? @mysql_query($sql) : ($eng == 'pg' ? @pg_query($sql) : @sqlite_query($SIDU['dbL'], $sql));
        $time_end = @microtime(true);
        $time = @round(($time_end - $time_start) * 1000);
        $SIDU[5] += $time;
        $err = @sidu_err(1);
        $RES[$i][0] = $sql;
        if ($err) {
            $RES[$i][1] = $err;
            $SIDU[8]++;
        } else {
            $RES[$i][3] = $eng == 'my' ? @mysql_num_rows($res) : ($eng == 'pg' ? @pg_num_rows($res) : @sqlite_num_rows($res));
            if ($eng == 'pg' && !$RES[$i][3]) {
                $RES[$i][2] = @pg_affected_rows($res);
                if (!$RES[$i][2]) {
                    unset($RES[$i][2]);
                }
            }
            //there is no function: sqlite_affected_rows()
            if ($RES[$i][3] === false && $eng == 'my') {
                $RES[$i][2] = @mysql_affected_rows();
            }
            if (!isset($SIDU[6]) && $RES[$i][3] !== false) {
                $SIDU[6] = $i;
                $RES[$i][5] = @get_sql_col($res, $eng);
                $RES[$i][6] = @get_sql_data($res, $eng);
            } elseif ($_POST['sqlmore'] && $RES[$i][3] !== false) {
                $RES[$i][5] = @get_sql_col($res, $eng);
                $RES[$i][6] = @get_sql_data($res, $eng);
            }
            $RES[$i][4] = $time;
        }
        @tm_his_log('S', $sql, $time, $err);
    }
    $SIDU[7] = ++$i;
    $SIDU['RES'] = $RES;
    //reset cookie
    if ($eng == 'my') {
        $db = @mysql_fetch_row(@mysql_query("SELECT database()"));
        if ($db[0] != $cook[1]) {
            $ck = @array($conn[0], $db[0]);
        }
    } elseif ($eng == 'pg') {
        $db[0] = @pg_dbname();
        if ($db[0] != $cook[1]) {
            $ck = @array($conn[0], $db[0]);
        } else {
            //sch
            $sch = pg_fetch_row(pg_query("SHOW search_path"));
            if (@substr($sch[0], 0, 8) == '"$user",') {
                $sch[0] = @substr($sch[0], 8);
            }
            $sch[0] = @str_replace('"', '', $sch[0]);
            if ($sch[0] != $cook[2]) {
                $ck = @array($conn[0], $cook[1], $sch[0]);
            }
        }
    }
    if (isset($ck)) {
        @update_sidu_cook($ck);
    }
}
Пример #4
0
function save_data($SIDU, $eng, $cmd)
{
    foreach ($_POST as $k => $v) {
        if (@substr($k, 0, 5) == 'data_' || @substr($k, 0, 10) == 'cbox_data_') {
            $arr = @explode('_', $k, 3);
            $data[$arr[0]][$arr[1]][$arr[2]] = $v;
        }
    }
    foreach ($SIDU['col'] as $i => $v) {
        $col[] = $eng == 'pg' ? '"' . $v[0] . '"' : $v[0];
    }
    $tab = $eng == 'pg' ? '"' . $SIDU[2] . '"."' . $SIDU[4] . '"' : ($eng == 'my' ? "`{$SIDU['1']}`.`{$SIDU['4']}`" : $SIDU[4]);
    foreach ($data['cbox']['data'] as $i => $v) {
        //only need i
        unset($COL);
        unset($VAL);
        $where = '';
        $is_new = @substr($i, 0, 3) === 'new';
        if (!$is_new || $cmd == 'data_del') {
            foreach ($_POST['pkV'][$i] as $j => $v) {
                $where .= " and " . $col[$j] . (@strtoupper($v) === 'NULL' ? " IS NULL" : "='{$v}'");
            }
            $where = "WHERE " . @substr($where, 5);
        }
        if ($cmd == 'data_save') {
            foreach ($data['data'][$i] as $j => $v) {
                $v = @strip($v, 1, 0, 1);
                if (!$is_new || $eng != 'pg' || $v != '' || @substr($SIDU['col'][$j][3], 0, 8) != 'nextval(') {
                    if ($eng == 'pg' && $SIDU['page']['dataEasy']) {
                        if ($SIDU['col'][$j][1] == "smallint" || $SIDU['col'][$j][1] == "int") {
                            $v = @ceil($v);
                        } elseif ((@substr($SIDU['col'][$j][1], 0, 8) == "varchar(" || @substr($SIDU['col'][$j][1], 0, 5) == "char(") && @strtoupper($v) != 'NULL') {
                            $v = @trim(@substr($v, 0, $SIDU['col'][$j][4] - 4));
                        }
                    }
                    $COL[] = $col[$j];
                    $VAL[] = $v;
                }
                //above logic too complex - even myself forgot :D
            }
            if ($is_new && isset($COL)) {
                $res = @tm("insert", $tab, $COL, $VAL);
            } elseif (!$is_new) {
                $res = @tm("update", $tab, $COL, $VAL, $where);
            }
        } elseif ($cmd == 'data_del') {
            $res = @tm("delete", $tab, null, null, $where);
        }
        $errno = @sidu_err(1);
        if ($errno) {
            $err .= ($eng == 'pg' ? $errno : "Err {$errno}") . "\\n";
        } elseif ($cmd == "data_save") {
            echo @html_js("parent.document.dataTab.cbox_data_{$i}.checked=''");
        } elseif ($cmd == "data_del") {
            echo @html_js("parent.document.getElementById('tr_{$i}').style.display='none'");
        }
    }
    if ($err) {
        echo @html_js("alert('" . @strtr($err, @array("'" => "\\'", "\"" => "\\\"", "\n" => "\\n")) . "')");
    }
}
Пример #5
0
function tab_tool_run_sql($sql)
{
    @tm("SQL", $sql);
    echo "<br/>{$sql}";
    $err = @sidu_err(1);
    if ($err) {
        echo "<br/><span class='red'>{$err}</span>";
    } else {
        echo "<br/><span class='green'>OK</span>";
    }
}
Пример #6
0
function save_data_sql_run($i, $SQL, $imp)
{
    global $SIDU;
    if ($SIDU['eng'] == 'my') {
        @mysql_query($SQL);
    } elseif ($SIDU['eng'] == 'pg') {
        @pg_query($SQL);
    } else {
        @sqlite_query($SIDU['dbL'], $SQL);
    }
    $err = @sidu_err(1);
    if ($err) {
        echo "<p class='err'>", @lang(2228, $i), "<br/>{$err}</p><pre>{$SQL}</pre><br/>";
    }
    if ($err && $imp['stop']) {
        echo "<br/><p class='err'>", @lang(2229), "</p><p class='ok'>", @lang(2230), "</p>";
        exit;
    }
}