1<?php
2/* vim: set expandtab sw=4 ts=4 sts=4: */
3/**
4 * Ensure the database and the table exist (else move to the "parent" script)
5 * and display headers
6 *
7 * @package PhpMyAdmin
8 */
9
10use PhpMyAdmin\Core;
11use PhpMyAdmin\Message;
12use PhpMyAdmin\Response;
13use PhpMyAdmin\Url;
14
15if (! defined('PHPMYADMIN')) {
16    exit;
17}
18
19if (empty($is_db)) {
20    if (strlen($db) > 0) {
21        $is_db = @$GLOBALS['dbi']->selectDb($db);
22    } else {
23        $is_db = false;
24    }
25
26    if (! $is_db) {
27        // not a valid db name -> back to the welcome page
28        if (! defined('IS_TRANSFORMATION_WRAPPER')) {
29            $response = Response::getInstance();
30            if ($response->isAjax()) {
31                $response->setRequestStatus(false);
32                $response->addJSON(
33                    'message',
34                    Message::error(__('No databases selected.'))
35                );
36            } else {
37                $url_params = array('reload' => 1);
38                if (isset($message)) {
39                    $url_params['message'] = $message;
40                }
41                if (! empty($sql_query)) {
42                    $url_params['sql_query'] = $sql_query;
43                }
44                if (isset($show_as_php)) {
45                    $url_params['show_as_php'] = $show_as_php;
46                }
47                Core::sendHeaderLocation(
48                    './index.php'
49                    . Url::getCommonRaw($url_params)
50                );
51            }
52            exit;
53        }
54    }
55} // end if (ensures db exists)
56
57if (empty($is_table)
58    && !defined('PMA_SUBMIT_MULT')
59    && !defined('TABLE_MAY_BE_ABSENT')
60) {
61    // Not a valid table name -> back to the db_sql.php
62
63    if (strlen($table) > 0) {
64        $is_table = $GLOBALS['dbi']->getCachedTableContent(array($db, $table), false);
65
66        if (! $is_table) {
67            $_result = $GLOBALS['dbi']->tryQuery(
68                'SHOW TABLES LIKE \''
69                . $GLOBALS['dbi']->escapeString($table) . '\';',
70                PhpMyAdmin\DatabaseInterface::CONNECT_USER,
71                PhpMyAdmin\DatabaseInterface::QUERY_STORE
72            );
73            $is_table = @$GLOBALS['dbi']->numRows($_result);
74            $GLOBALS['dbi']->freeResult($_result);
75        }
76    } else {
77        $is_table = false;
78    }
79
80    if (! $is_table) {
81        if (!defined('IS_TRANSFORMATION_WRAPPER')) {
82            if (strlen($table) > 0) {
83                // SHOW TABLES doesn't show temporary tables, so try select
84                // (as it can happen just in case temporary table, it should be
85                // fast):
86
87                /**
88                 * @todo should this check really
89                 * only happen if IS_TRANSFORMATION_WRAPPER?
90                 */
91                $_result = $GLOBALS['dbi']->tryQuery(
92                    'SELECT COUNT(*) FROM ' . PhpMyAdmin\Util::backquote($table)
93                    . ';',
94                    PhpMyAdmin\DatabaseInterface::CONNECT_USER,
95                    PhpMyAdmin\DatabaseInterface::QUERY_STORE
96                );
97                $is_table = ($_result && @$GLOBALS['dbi']->numRows($_result));
98                $GLOBALS['dbi']->freeResult($_result);
99            }
100
101            if (! $is_table) {
102                include './db_sql.php';
103                exit;
104            }
105        }
106
107        if (! $is_table) {
108            exit;
109        }
110    }
111} // end if (ensures table exists)
112