~sit-developers/sit/master

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
// qbe.php - Very simple query by example
//
// SiT (Support Incident Tracker) - Support call tracking system
// Copyright (C) 2010-2014 The Support Incident Tracker Project
// Copyright (C) 2000-2009 Salford Software Ltd. and Contributors
//
// This software may be used and distributed according to the terms
// of the GNU General Public License, incorporated herein by reference.
//

// Author: Ivan Lucas <ivanlucas[at]users.sourceforge.net>

require ('core.php');
$permission = array(PERM_ADMIN, PERM_REPORT_RUN); // Administrate / Run Reports
require (APPLICATION_LIBPATH . 'functions.inc.php');

// This page requires authentication
require (APPLICATION_LIBPATH . 'auth.inc.php');

$title = $strQueryByExample;

$mode = clean_fixed_list($_REQUEST['mode'], array('', 'selectfields', 'report'));

$result = mysql_list_tables($CONFIG['db_database']);
while ($row = mysql_fetch_row($result))
{
    $tables[] = $row[0];
}

$table1 = clean_fixed_list($_POST['table1'], $tables);

if (empty($mode))
{
    include (APPLICATION_INCPATH . 'htmlheader.inc.php');
    echo "<h2>".icon('reports', 32)." {$title}</h2>";
    echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>";
    echo "<table class='maintable'>";
    echo "<tr><th>{$strTable}:</th>";
    echo "<td>";
    $result = mysql_list_tables($CONFIG['db_database']);
    echo array_drop_down($tables, 'table1');
    echo "</td></tr>\n";
    /*
    echo "<tr><td align='right' width='200' class='shade1'><b>Table 2</b>:</td>";
    echo "<td width=400 class='shade2'>";
    $result = mysql_list_tables($db_database);
    echo "<select name='table1'>";
    while ($row = mysql_fetch_row($result))
    {
        echo "<option value='{$row[0]}'>{$row[0]}</option>\n";
    }
    echo "</select>";
    echo "</td></tr>\n";
    */
    echo "</table>";
    echo "<p class='formbuttons'>";
    echo "<input type='hidden' name='mode' value='selectfields' />";
    echo "<input type='submit' value='{$strContinue}' />";
    echo "</p>";
    echo "</form>";
    include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
}
elseif ($mode == 'selectfields')
{
    include (APPLICATION_INCPATH . 'htmlheader.inc.php');
    echo "<h2>".icon('reports', 32)." {$title}</h2>";
    echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>";
    echo "<table class='maintable'>";
    echo "<tr><th>{$strTable}:</th>";
    echo "<td class='shade2'>{$table1}</td></tr>";

    echo "<tr><th valign='top'>{$strFields}:</th>";
    echo "<td width='400' class='shade2'>";
    $result = mysql_list_fields($CONFIG['db_database'],$table1);
    $columns = mysql_num_fields($result);
    echo "<select name='fields[]' multiple='multiple' size='10'>";
    for ($i = 0; $i < $columns; $i++)
    {
        $fieldname = mysql_field_name($result, $i);
        // We explicity filter out password columns (see Mantis 1565)
        if ($fieldname != 'password')
        {
            echo "<option value='$fieldname'>$fieldname</option>\n";
        }
    }
    echo "</select>";
    echo "</td></tr>\n";
    echo "<tr><th>{$strSort}:</th>";
    echo "<td class='shade2'>";
    echo "<select name='sortby'>";
    for ($i = 0; $i < $columns; $i++)
    {
        $fieldname = mysql_field_name($result, $i);
        echo "<option value='$fieldname'>$fieldname</option>\n";
    }
    echo "</select>";
    echo "<select name='sortorder'>";
    echo "<option value='none' selected='selected'>{$strNone}</option>";
    echo "<option value='ASC'>{$strSortAscending}</option>";
    echo "<option value='DESC'>{$strSortDescending}</option>";
    echo "</select>";
    echo "</td></tr>";

    echo "<tr><th>{$strCriteria}:</th>";
    echo "<td class='shade2'>";
    echo "<select name='criteriafield'>";
    for ($i = 0; $i < $columns; $i++)
    {
        $fieldname = mysql_field_name($result, $i);
        if ($fieldname != 'password')
        {
            echo "<option value='$fieldname'>$fieldname</option>\n";
        }
    }
    echo "</select>";
    echo "<select name='criteriaop'>";
    echo "<option value='eq' selected>=</option>";
    echo "<option value='lt'>&lt;</option>";
    echo "<option value='gt'>&gt;</option>";
    echo "<option value='LIKE'>LIKE</option>";
    echo "</select>";
    echo "<input type='text' name='criteriaval' />";
    echo "</td></tr>";

    echo "<tr><th>{$strLimitTo}:</th>";
    echo "<td><input type='text' name='limit' value='1000' size='4' /> {$strResults}</td></tr>";

    echo "<tr><th>{$strOutput}:</th>";
    echo "<td>";
    echo "<select name='output'>";
    echo "<option value='screen'>{$strScreen}</option>";
    // echo "<option value='printer'>Printer</option>";
    echo "<option value='csv'>{$strCSVfile}</option>";
    echo "</select>";
    echo "</td></tr>";
    echo "</table>";
    echo "<p class='formbuttons'>";
    echo "<input type='hidden' name='table1' value='".cleanvar($_POST['table1'])."' />";
    echo "<input type='hidden' name='mode' value='report' />";
    echo "<input type='reset' value=\"{$strReset}\" /> ";
    echo "<input type='submit' value='{$strRunReport}' />";
    echo "</p>";
    echo "</form>";
    include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
}
elseif ($mode == 'report')
{
    // External variables
    $table = clean_fixed_list($_POST['table1'], $tables);
    $criteriafield = clean_dbstring($_POST['criteriafield']);
    $criteriaop = clean_fixed_list($_POST['criteriaop'], array('eq', 'lt', 'gt'));
    $criteriaval = clean_dbstring($_POST['criteriaval']);
    $sortby = clean_dbstring($_POST['sortby']);
    $sortorder = clean_dbstring($_POST['sortorder']);
    $limit = clean_int($_POST['limit']);
    $columns = count($_POST[fields]);

    switch ($criteriaop)
    {
    	case 'eq': $criteriaop = "=";
            break;
        case 'lt': $criteriaop = "<";
            break;
        case 'gt' : $criteriaop = ">";
            break;
    }

    if ($columns >= 1)
    {
        $htmlfieldheaders = "<tr>";
        for ($i = 0; $i < $columns; $i++)
        {
            $fieldname = clean_dbstring($_POST[fields][$i]);
            if ($fieldname != 'password')
            {
                $fieldlist .= $fieldname;
            }
            if ($i < ($columns-1)) $fieldlist .= "`,`";
            $htmlfieldheaders .= "<th>{$fieldname}</th>";
            $csvfieldheaders .= $fieldname;
            if ($i < ($columns-1)) $csvfieldheaders .= '","';
        }
        $fieldlist = "`{$fieldlist}`";
        $fieldheaders.="</tr>\n";
        $csvfieldheaders.="\"\r\n";
    }
    else
    {
        $fieldlist = "*";
    }

    $sql = "SELECT {$fieldlist} FROM {$table} ";
    if (!empty($criteriaval)) $sql .= "WHERE `{$criteriafield}` {$criteriaop} '{$criteriaval}' ";
    if ($sortorder != 'none') $sql .= "ORDER BY `{$sortby}` {$sortorder} ";
    if ($limit >= 1) $sql .= "LIMIT {$limit} ";

    $result = mysql_query($sql);
    if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
    $html .= "<p align='center'><code>$sql</code></p>\n";
    $html .= "<table width='100%'>";
    $html .= $htmlfieldheaders;
    $shade = 'shade1';
    while ($row = mysql_fetch_row($result))
    {
        $columns = count($row);
        $html .= "<tr class='$shade'>";
        $csv .= "\"";
        for ($i = 0; $i < $columns; $i++)
        {
            $html .= "<td>{$row[$i]}</td>";
            $csv .= strip_comma($row[$i]);
            if ($i < ($columns-1)) $csv .= '","';
        }
        $html .= "</tr>\n";
        if ($shade == 'shade1') $shade = 'shade2';
        else $shade = 'shade1';
        $csv.="\"\r\n";
    }
    $html .= "</table>";
    if ($_POST['output'] == 'screen')
    {
        include (APPLICATION_INCPATH . 'htmlheader.inc.php');
        echo $html;
        include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
    }
    elseif ($_POST['output'] == 'csv')
    {
        // --- CSV File HTTP Header
        header("Content-type: text/csv\r\n");
        header("Content-disposition-type: attachment\r\n");
        header("Content-disposition: filename=qbe_report.csv");
        echo $csvfieldheaders;
        echo $csv;
    }
}
?>