File: DbTableExists.php

package info (click to toggle)
phpmyadmin 4%3A5.2.1%2Bdfsg-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 131,332 kB
  • sloc: javascript: 212,681; php: 168,094; xml: 18,098; sql: 504; sh: 274; makefile: 205; python: 199
file content (118 lines) | stat: -rw-r--r-- 2,928 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

declare(strict_types=1);

namespace PhpMyAdmin;

use PhpMyAdmin\Controllers\Database\SqlController;

use function __;
use function defined;
use function strlen;

final class DbTableExists
{
    /**
     * Ensure the database and the table exist (else move to the "parent" script)
     * and display headers
     */
    public static function check(): void
    {
        self::checkDatabase();
        self::checkTable();
    }

    private static function checkDatabase(): void
    {
        global $db, $dbi, $is_db, $message, $show_as_php, $sql_query;

        if (! empty($is_db)) {
            return;
        }

        $is_db = false;
        if (strlen($db) > 0) {
            $is_db = @$dbi->selectDb($db);
        }

        if ($is_db || defined('IS_TRANSFORMATION_WRAPPER')) {
            return;
        }

        $response = ResponseRenderer::getInstance();
        if ($response->isAjax()) {
            $response->setRequestStatus(false);
            $response->addJSON(
                'message',
                Message::error(__('No databases selected.'))
            );

            exit;
        }

        $urlParams = ['reload' => 1];

        if (isset($message)) {
            $urlParams['message'] = $message;
        }

        if (! empty($sql_query)) {
            $urlParams['sql_query'] = $sql_query;
        }

        if (isset($show_as_php)) {
            $urlParams['show_as_php'] = $show_as_php;
        }

        Core::sendHeaderLocation('./index.php?route=/' . Url::getCommonRaw($urlParams, '&'));

        exit;
    }

    private static function checkTable(): void
    {
        global $containerBuilder, $db, $table, $dbi, $is_table;

        if (! empty($is_table) || defined('PMA_SUBMIT_MULT') || defined('TABLE_MAY_BE_ABSENT')) {
            return;
        }

        $is_table = false;
        if (strlen($table) > 0) {
            $is_table = $dbi->getCache()->getCachedTableContent([$db, $table], false);
            if ($is_table) {
                return;
            }

            $result = $dbi->tryQuery('SHOW TABLES LIKE \'' . $dbi->escapeString($table) . '\';');
            $is_table = $result && $result->numRows();
        }

        if ($is_table) {
            return;
        }

        if (defined('IS_TRANSFORMATION_WRAPPER')) {
            exit;
        }

        if (strlen($table) > 0) {
            /**
             * SHOW TABLES doesn't show temporary tables, so try select
             * (as it can happen just in case temporary table, it should be fast):
             */
            $result = $dbi->tryQuery('SELECT COUNT(*) FROM ' . Util::backquote($table) . ';');
            $is_table = $result && $result->numRows();
        }

        if ($is_table) {
            return;
        }

        /** @var SqlController $controller */
        $controller = $containerBuilder->get(SqlController::class);
        $controller();

        exit;
    }
}