File: system_table.inc.php

package info (click to toggle)
ibwebadmin 0.98-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,916 kB
  • ctags: 1,950
  • sloc: php: 12,454; makefile: 7
file content (248 lines) | stat: -rw-r--r-- 11,169 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
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
238
239
240
241
242
243
244
245
246
247
248
<?php
// File           system_table.inc.php / ibWebAdmin
// Purpose        functions concerning $s_system_table, the table to display
//                on the System Table panel
// Author         Lutz Brueckner <irie@gmx.de>
// Copyright      (c) 2001, 2002, 2003, 2004 by Lutz Brueckner,
//                published under the terms of the GNU General Public Licence v.2,
//                see file LICENCE for details
// Created        <01/09/06 21:48:07 lb>
//
// $Id: system_table.inc.php,v 1.16 2004/02/07 15:30:22 lbrueckner Exp $

//
// return an array to display an the System Table panel
//
function get_systable($s_systable) {
    global $dbhandle;

    // get the field names and types
    $sql  = 'SELECT RDB$RELATION_FIELDS.RDB$FIELD_NAME AS FNAME,'
                 .' RDB$RELATION_FIELDS.RDB$FIELD_POSITION,'
                 .' RDB$FIELD_TYPE AS FTYPE,'
                 .' RDB$FIELD_SUB_TYPE AS STYPE'
            .' FROM RDB$RELATION_FIELDS, RDB$FIELDS'
           .' WHERE RDB$RELATION_NAME=\''.$s_systable['table'].'\''
             .' AND RDB$FIELD_SOURCE=RDB$FIELDS.RDB$FIELD_NAME'
       .' ORDER BY RDB$FIELD_POSITION';
    
    $res = ibase_query($dbhandle, $sql) or ib_error(__FILE__, __LINE__, $sql);
    
    $table = array();
    while ($row = ibase_fetch_object($res)) {
        $type = (isset($row->FTYPE)) ? $row->FTYPE : NULL;
        $stype= (isset($row->STYPE)) ? $row->STYPE : NULL;
        $table[trim($row->FNAME)]['type'] = get_datatype($type, $stype);
    }
    ibase_free_result($res);

    // get the table content
    $sql = 'SELECT *' //.implode(',', array_keys($table))
           .' FROM '.$s_systable['table'];
    if ($s_systable['sysdata'] == FALSE) {
        $sql .= ' WHERE '.pos(array_keys($table))." NOT LIKE 'RDB\$%'"
                 .' AND '.pos(array_keys($table))." NOT LIKE 'TMP\$%'";
    }

    // handle the filter
    if (!empty($s_systable['ffield'])  &&  in_array($s_systable['ffield'], array_keys($table))) {
        $sql .= $s_systable['sysdata'] == TRUE ? ' WHERE ' : ' AND ';
        switch ($s_systable['fvalue']) {
            case '':
                $sql .= $s_systable['ffield'] . ' IS NULL';
                break;
            case 'BLOB':
                if ($table[$s_systable['ffield']]['type'] == 'BLOB') {
                    $sql .= $s_systable['ffield'] . " IS NOT NULL";
                    break;
                }
            default:
                $sql .= $s_systable['ffield']."='".$s_systable['fvalue']."'";
        }
    }

    if (!empty($s_systable['order'])) {
        $sql .= ' ORDER BY '.$s_systable['order'].' '.$s_systable['dir'];
    }
    $res = ibase_query($dbhandle, $sql) or ib_error(__FILE__, __LINE__, $sql);

    while ($row = ibase_fetch_object($res)) {
        foreach (array_keys($table) as $fname) {
            if ($row->$fname === 0) {
                $table[$fname]['col'][] = '0';
            }
            elseif (!isset($row->$fname)  || empty($row->$fname)) {
                $table[$fname]['col'][] = '&nbsp;';
            }
            elseif ($table[$fname]['type'] == 'BLOB') {
                $table[$fname]['col'][] = '<i>BLOB</i>';
            }
            else {
                $table[$fname]['col'][] = trim($row->$fname);
            }
        }
    }
    ibase_free_result($res);

    return $table;
}


//
// display the system table onto the System Table panel
//
function get_systable_html($table, $s_systable) {
    global $s_watch_blobas, $HTTP_SERVER_VARS;

    $html = "<table id=\"systable\" cellpadding=\"2\" border=\"0\" onselectstart=\"return false\" style=\"-moz-user-select: none\">\n<tr>\n";
    foreach (array_keys($table) as $colname) {
        if ($s_systable['order'] == $colname) {
            $headstr = ($s_systable['dir'] == 'ASC') ? '*&nbsp;'.$colname : $colname.'&nbsp;*';
        }
        else {
            $headstr = $colname;
        }
        $url = url_session('database.php?order='.$colname);
        $html .= '<th><a href="'.$url.'">'.$headstr."</a></th>\n";
    }
    $html .= "</tr>\n";

    $systable_textblobs = systable_textblobs();
    if (isset($systable_textblobs[$s_systable['table']])) {
        foreach ($systable_textblobs[$s_systable['table']]['columns'] as $col) {
            $s_watch_blobas[$col] = 'text';
        }
        $where_str = 'WHERE ';
        foreach ($systable_textblobs[$s_systable['table']]['indices'] as $idx) {
            $where_str .= $idx."='%s' AND ";
        }
        $where_str = substr($where_str, 0, -4);
    }

    if (isset($table[$colname]['col'])) {
        $rows = count($table[$colname]['col']);
        $class = 'wttr2';

        // loop the rows
        for ($i=0; $i<$rows; $i++) {
            $class = ($class == 'wttr1') ? 'wttr2' : 'wttr1';
            $html .= '<tr class="'.$class."\">\n";

            // loop the columns
            foreach ($table as $colname => $colarr) {

                $align = ($colarr['type'] == 'BLOB') ? 'center' : 'right';
                $val = $colarr['col'][$i];

                if (isset($systable_textblobs[$s_systable['table']])
                &&  $val != '&nbsp;'
                &&  in_array($colname, $systable_textblobs[$s_systable['table']]['columns'])) {
                    if (count($systable_textblobs[$s_systable['table']]['indices']) == 1) {
                        $where = urlencode(sprintf($where_str,
                                                   $table[$systable_textblobs[$s_systable['table']]['indices'][0]]['col'][$i]));
                    }
                    else {
                        $where = urlencode(sprintf($where_str,
                                                   $table[$systable_textblobs[$s_systable['table']]['indices'][0]]['col'][$i],
                                                   $table[$systable_textblobs[$s_systable['table']]['indices'][1]]['col'][$i]));
                    }
                    $blob_url = 'showblob.php?where='.$where.'&table='.$s_systable['table'].'&col='.$colname;
                    $val = '<a href="'.$blob_url.'" target="_blank">'.$val.'</a>';
                }

                $html .= '<td align="'.$align.'">'.$val."</td>\n";
            }
            $html .= "</tr>\n";
        }
    }
    $html .= "</table>\n";

    return $html;
}


//
// delivers the definitions for the blob links on the systemtables panel
//
function systable_textblobs() {

    return 
        array('RDB$CHARACTER_SETS'       => array('columns' => array('RDB$DESCRIPTON'),
                                                  'indices' => array('RDB$RDB$CHARACTER_SET_NAME')
                                                  ),
              'RDB$COLLATIONS'           => array('columns' => array('RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$COLLATION_NAME')
                                                  ),
              'RDB$DATABASE'             => array('columns' => array('RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$RELATION_ID')
                                                  ),                            
              'RDB$EXCEPTIONS'           => array('columns' => array('RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$EXCEPTION_NAME')
                                                  ),
              'RDB$FIELDS'               => array('columns' => array('RDB$VALIDATION_SOURCE', 'RDB$COMPUTED_SOURCE',
                                                                     'RDB$DEFAULT_SOURCE', 'RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$FIELD_NAME')
                                                  ),
              'RDB$FILTERS'              => array('columns' => array('RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$FUNCTION_NAME')
                                                  ),
              'RDB$FUNCTIONS'            => array('columns' => array('RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$FUNCTION_NAME')
                                                  ),
              'RDB$INDICES'              => array('columns' => array('RDB$DESCRIPTION', 'RDB$EXPRESSION_SOURCE'),
                                                  'indices' => array('RDB$INDEX_NAME')
                                                  ),
              'RDB$PROCEDURE_PARAMETERS' => array('columns' => array('RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$PROCEDURE_NAME', 'RDB$PARAMETER_NAME')
                                                  ),
              'RDB$PROCEDURES'           => array('columns' => array('RDB$DESCRIPTION', 'RDB$PROCEDURE_SOURCE'),
                                                  'indices' => array('RDB$PROCEDURE_NAME') 
                                                  ),
              'RDB$RELATION_FIELDS'      => array('columns' => array('RDB$DESCRIPTION', 'RDB$DEFAULT_SOURCE'),
                                                  'indices' => array('RDB$FIELD_SOURCE', 'RDB$RELATION_NAME')
                                                  ),
              'RDB$RELATIONS'            => array('columns' => array('RDB$VIEW_SOURCE', 'RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$RELATION_ID')
                                                  ),
              'RDB$SECURITY_CLASSES'     => array ('columns' => array('RDB$DESCRIPTION'),
                                                   'indices' => array('RDB$SECURITY_CLASS')
                                                   ),
              'RDB$TRIGGERS'             => array('columns' => array('RDB$DESCRIPTION', 'RDB$TRIGGER_SOURCE'),
                                                  'indices' => array('RDB$TRIGGER_NAME')
                                                  ),
              'RDB$TYPES'                => array('columns' => array('RDB$DESCRIPTION'),
                                                  'indices' => array('RDB$TYPE_NAME')
                                                  ),
              );
}


function systable_field_select($table, $field=NULL) {
    global $db_strings;

    $cols = get_table_fields($table);

    return '<b>'.$db_strings['FField']."</b><br>\n"
          .get_selectlist('db_sysfield', $cols, $field, TRUE,
                           array('onChange' => "getFilterValues('".$table."' ,selectedElement(this))",
                                 'id' => 'db_sysvalues_list')
                           );
}

function systable_value_select($table, $field, $value=NULL) {
    global $dbhandle, $db_strings;

    $sql = 'SELECT DISTINCT '.$field.' AS FNAME FROM '.$table;
    $res = ibase_query($dbhandle, $sql) or ib_error(__FILE__, __LINE__, $sql);

    $values = array();
    while ($row = ibase_fetch_object($res)) {
        $values[] = trim($row->FNAME);
    }
    ibase_free_result($res);

    return '<b>'.$db_strings['FValue']."</b><br>\n"
          .get_selectlist('db_sysvalue', $values, $value, TRUE);
}

?>