Completed
Push — master ( 64741e...904a1b )
by Maurício
09:11
created

db_qbe.php (2 issues)

1
<?php
2
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
/**
4
 * query by example the whole database
5
 *
6
 * @package PhpMyAdmin
7
 */
8
declare(strict_types=1);
9
10
use PhpMyAdmin\Database\Qbe;
11
use PhpMyAdmin\Message;
12
use PhpMyAdmin\Relation;
13
use PhpMyAdmin\Response;
14
use PhpMyAdmin\SavedSearches;
15
use PhpMyAdmin\Sql;
16
use PhpMyAdmin\Template;
17
use PhpMyAdmin\Url;
18
use PhpMyAdmin\Util;
19
20
/**
21
 * requirements
22
 */
23
require_once 'libraries/common.inc.php';
24
25
$response = Response::getInstance();
26
$relation = new Relation($GLOBALS['dbi']);
27
$template = new Template();
28
29
// Gets the relation settings
30
$cfgRelation = $relation->getRelationsParam();
31
32
$savedSearchList = [];
33
$savedSearch = null;
34
$currentSearchId = null;
35
if ($cfgRelation['savedsearcheswork']) {
36
    $header = $response->getHeader();
37
    $scripts = $header->getScripts();
38
    $scripts->addFile('db_qbe.js');
39
40
    //Get saved search list.
41
    $savedSearch = new SavedSearches($GLOBALS);
42
    $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
43
        ->setDbname($_REQUEST['db']);
44
45
    if (!empty($_REQUEST['searchId'])) {
46
        $savedSearch->setId($_REQUEST['searchId']);
47
    }
48
49
    //Action field is sent.
50
    if (isset($_REQUEST['action'])) {
51
        $savedSearch->setSearchName($_REQUEST['searchName']);
52
        if ('create' === $_REQUEST['action']) {
53
            $saveResult = $savedSearch->setId(null)
54
                ->setCriterias($_REQUEST)
55
                ->save();
56
        } elseif ('update' === $_REQUEST['action']) {
57
            $saveResult = $savedSearch->setCriterias($_REQUEST)
58
                ->save();
59
        } elseif ('delete' === $_REQUEST['action']) {
60
            $deleteResult = $savedSearch->delete();
61
            //After deletion, reset search.
62
            $savedSearch = new SavedSearches($GLOBALS);
63
            $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
64
                ->setDbname($_REQUEST['db']);
65
            $_REQUEST = [];
66
        } elseif ('load' === $_REQUEST['action']) {
67
            if (empty($_REQUEST['searchId'])) {
68
                //when not loading a search, reset the object.
69
                $savedSearch = new SavedSearches($GLOBALS);
70
                $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
71
                    ->setDbname($_REQUEST['db']);
72
                $_REQUEST = [];
73
            } else {
74
                $loadResult = $savedSearch->load();
75
            }
76
        }
77
        //Else, it's an "update query"
78
    }
79
80
    $savedSearchList = $savedSearch->getList();
81
    $currentSearchId = $savedSearch->getId();
82
}
83
84
/**
85
 * A query has been submitted -> (maybe) execute it
86
 */
87
$message_to_display = false;
88
if (isset($_REQUEST['submit_sql']) && ! empty($sql_query)) {
89
    if (! preg_match('@^SELECT@i', $sql_query)) {
90
        $message_to_display = true;
91
    } else {
92
        $goto = 'db_sql.php';
93
        $sql = new Sql();
94
        $sql->executeQueryAndSendQueryResponse(
95
            null, // analyzed_sql_results
96
            false, // is_gotofile
97
            $_REQUEST['db'], // db
98
            null, // table
99
            false, // find_real_end
100
            null, // sql_query_for_bookmark
101
            null, // extra_data
102
            null, // message_to_show
103
            null, // message
104
            null, // sql_data
105
            $goto, // goto
106
            $pmaThemeImage, // pmaThemeImage
107
            null, // disp_query
108
            null, // disp_message
109
            null, // query_type
110
            $sql_query, // sql_query
111
            null, // selectedTables
112
            null // complete_query
113
        );
114
    }
115
}
116
117
$sub_part  = '_qbe';
118
require 'libraries/db_common.inc.php';
119
$url_query .= '&amp;goto=db_qbe.php';
120
$url_params['goto'] = 'db_qbe.php';
121
122
list(
0 ignored issues
show
Comprehensibility Best Practice introduced by
This list assign is not used and could be removed.
Loading history...
123
    $tables,
124
    $num_tables,
125
    $total_num_tables,
126
    $sub_part,
127
    $is_show_stats,
128
    $db_is_system_schema,
129
    $tooltip_truename,
130
    $tooltip_aliasname,
131
    $pos
132
) = Util::getDbInfo($db, is_null($sub_part) ? '' : $sub_part);
0 ignored issues
show
The condition is_null($sub_part) is always false.
Loading history...
133
134
if ($message_to_display) {
135
    Message::error(
136
        __('You have to choose at least one column to display!')
137
    )
138
        ->display();
139
}
140
unset($message_to_display);
141
142
// create new qbe search instance
143
$db_qbe = new Qbe($GLOBALS['dbi'], $GLOBALS['db'], $savedSearchList, $savedSearch);
144
145
$secondaryTabs = [
146
    'multi' => [
147
        'link' => 'db_multi_table_query.php',
148
        'text' => __('Multi-table query'),
149
    ],
150
    'qbe' => [
151
        'link' => 'db_qbe.php',
152
        'text' => __('Query by example'),
153
    ],
154
];
155
$response->addHTML(
156
    $template->render('secondary_tabs', [
157
        'url_params' => $url_params,
158
        'sub_tabs' => $secondaryTabs,
159
    ])
160
);
161
162
$url = 'db_designer.php' . Url::getCommon(
163
    array_merge(
164
        $url_params,
165
        ['query' => 1]
166
    )
167
);
168
$response->addHTML(
169
    Message::notice(
170
        sprintf(
171
            __('Switch to %svisual builder%s'),
172
            '<a href="' . $url . '">',
173
            '</a>'
174
        )
175
    )
176
);
177
178
/**
179
 * Displays the Query by example form
180
 */
181
$response->addHTML($db_qbe->getSelectionForm());
182