/home/mjc1/public_html/ej_sql/libraries/transformations.lib.php


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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Set of functions used with the relation and pdf feature
 *
 * @version $Id$
 */

/**
 * returns array of options from string with options separated by comma, removes quotes
 *
 * <code>
 * PMA_transformation_getOptions("'option ,, quoted',abd,'2,3',");
 * // array {
 * //     'option ,, quoted',
 * //     'abc',
 * //     '2,3',
 * //     '',
 * // }
 * </code>
 *
 * @uses    preg_split()
 * @uses    array_shift()
 * @uses    trim()
 * @uses    rtrim()
 * @uses    ltrim()
 * @uses    strlen()
 * @uses    substr()
 * @uses    stripslashes()
 * @param   string  $option_string  comma separated options
 * @return  array   options
 */
function PMA_transformation_getOptions($option_string)
{
    
$result = array();

    if (! 
strlen($option_string)
     || ! 
$transform_options preg_split('/,/'$option_string)) {
        return 
$result;
    }

    while ((
$option array_shift($transform_options)) !== null) {
        
$trimmed trim($option);
        if (
strlen($trimmed) > 1
         
&& $trimmed[0] == "'"
         
&& $trimmed[strlen($trimmed) - 1] == "'") {
            
// '...'
            
$option substr($trimmed1, -1);
        } elseif (isset(
$trimmed[0]) && $trimmed[0] == "'") {
            
// '...,
            
$trimmed ltrim($option);
            while ((
$option array_shift($transform_options)) !== null) {
                
// ...,
                
$trimmed .= ',' $option;
                
$rtrimmed rtrim($trimmed);
                if (
$rtrimmed[strlen($rtrimmed) - 1] == "'") {
                    
// ,...'
                    
break;
                }
            }
            
$option substr($rtrimmed1, -1);
        }
        
$result[] = stripslashes($option);
    }

    return 
$result;
}

/**
 * Gets all available MIME-types
 *
 * @access  public
 * @author  Garvin Hicking <me@supergarv.de>
 * @uses    opendir()
 * @uses    readdir()
 * @uses    closedir()
 * @uses    sort()
 * @uses    preg_match()
 * @uses    explode()
 * @uses    str_replace()
 * @staticvar   array   mimetypes
 * @return  array    array[mimetype], array[transformation]
 */
function PMA_getAvailableMIMEtypes()
{
    static 
$stack null;

    if (
null !== $stack) {
        return 
$stack;
    }

    
$stack = array();
    
$filestack = array();

    
$handle opendir('./libraries/transformations');

    if (! 
$handle) {
        return 
$stack;
    }

    while (
$file readdir($handle)) {
        
$filestack[] = $file;
    }

    
closedir($handle);
    
sort($filestack);

    foreach (
$filestack as $file) {
        if (
preg_match('|^.*__.*\.inc\.php$|'$file)) {
            
// File contains transformation functions.
            
$base explode('__'str_replace('.inc.php'''$file));
            
$mimetype str_replace('_''/'$base[0]);
            
$stack['mimetype'][$mimetype] = $mimetype;

            
$stack['transformation'][] = $mimetype ': ' $base[1];
            
$stack['transformation_file'][] = $file;

        } elseif (
preg_match('|^.*\.inc\.php$|'$file)) {
            
// File is a plain mimetype, no functions.
            
$base str_replace('.inc.php'''$file);

            if (
$base != 'global') {
                
$mimetype str_replace('_''/'$base);
                
$stack['mimetype'][$mimetype] = $mimetype;
                
$stack['empty_mimetype'][$mimetype] = $mimetype;
            }
        }
    }

    return 
$stack;
}

/**
 * Gets the mimetypes for all rows of a table
 *
 * @uses    $GLOBALS['controllink']
 * @uses    PMA_getRelationsParam()
 * @uses    PMA_backquote()
 * @uses    PMA_sqlAddslashes()
 * @uses    PMA_DBI_fetch_result()
 * @author  Mike Beck <mikebeck@users.sourceforge.net>
 * @author  Garvin Hicking <me@supergarv.de>
 * @access  public
 * @param   string   $db        the name of the db to check for
 * @param   string   $table     the name of the table to check for
 * @param   string   $strict    whether to include only results having a mimetype set
 * @return  array    [field_name][field_key] = field_value
 */
function PMA_getMIME($db$table$strict false)
{
    
$cfgRelation PMA_getRelationsParam();

    if (! 
$cfgRelation['commwork']) {
        return 
false;
    }

    
$com_qry  '
         SELECT `column_name`,
                `mimetype`,
                `transformation`,
                `transformation_options`
          FROM ' 
PMA_backquote($cfgRelation['db']) . '.' PMA_backquote($cfgRelation['column_info']) . '
         WHERE `db_name`    = \'' 
PMA_sqlAddslashes($db) . '\'
           AND `table_name` = \'' 
PMA_sqlAddslashes($table) . '\'
           AND ( `mimetype` != \'\'' 
. (!$strict '
              OR `transformation` != \'\'
              OR `transformation_options` != \'\'' 
'') . ')';
    return 
PMA_DBI_fetch_result($com_qry'column_name'null$GLOBALS['controllink']);
// end of the 'PMA_getMIME()' function

/**
 * Set a single mimetype to a certain value.
 *
 * @uses    PMA_DBI_QUERY_STORE
 * @uses    PMA_getRelationsParam()
 * @uses    PMA_backquote()
 * @uses    PMA_sqlAddslashes()
 * @uses    PMA_query_as_cu()
 * @uses    PMA_DBI_num_rows()
 * @uses    PMA_DBI_fetch_assoc()
 * @uses    PMA_DBI_free_result()
 * @uses    strlen()
 * @access  public
 * @param   string   $db        the name of the db
 * @param   string   $table     the name of the table
 * @param   string   $key       the name of the column
 * @param   string   $mimetype  the mimetype of the column
 * @param   string   $transformation    the transformation of the column
 * @param   string   $transformation_options    the transformation options of the column
 * @param   string   $forcedelete   force delete, will erase any existing comments for this column
 * @return  boolean  true, if comment-query was made.
 */
function PMA_setMIME($db$table$key$mimetype$transformation,
    
$transformation_options$forcedelete false)
{
    
$cfgRelation PMA_getRelationsParam();

    if (! 
$cfgRelation['commwork']) {
        return 
false;
    }

    
$test_qry  '
         SELECT `mimetype`,
                `comment`
           FROM ' 
PMA_backquote($cfgRelation['db']) . '.' PMA_backquote($cfgRelation['column_info']) . '
          WHERE `db_name`     = \'' 
PMA_sqlAddslashes($db) . '\'
            AND `table_name`  = \'' 
PMA_sqlAddslashes($table) . '\'
            AND `column_name` = \'' 
PMA_sqlAddslashes($key) . '\'';
    
$test_rs   PMA_query_as_cu($test_qrytruePMA_DBI_QUERY_STORE);

    if (
$test_rs && PMA_DBI_num_rows($test_rs) > 0) {
        
$row = @PMA_DBI_fetch_assoc($test_rs);
        
PMA_DBI_free_result($test_rs);

        if (! 
$forcedelete
         
&& (strlen($mimetype) || strlen($transformation)
          || 
strlen($transformation_options) || strlen($row['comment']))) {
            
$upd_query '
                UPDATE ' 
PMA_backquote($cfgRelation['db']) . '.' PMA_backquote($cfgRelation['column_info']) . '
                   SET `mimetype`               = \'' 
PMA_sqlAddslashes($mimetype) . '\',
                       `transformation`         = \'' 
PMA_sqlAddslashes($transformation) . '\',
                       `transformation_options` = \'' 
PMA_sqlAddslashes($transformation_options) . '\'';
        } else {
            
$upd_query 'DELETE FROM ' PMA_backquote($cfgRelation['db']) . '.' PMA_backquote($cfgRelation['column_info']);
        }
        
$upd_query .= '
            WHERE `db_name`     = \'' 
PMA_sqlAddslashes($db) . '\'
              AND `table_name`  = \'' 
PMA_sqlAddslashes($table) . '\'
              AND `column_name` = \'' 
PMA_sqlAddslashes($key) . '\'';
    } elseif (
strlen($mimetype) || strlen($transformation)
     || 
strlen($transformation_options)) {
        
$upd_query 'INSERT INTO ' PMA_backquote($cfgRelation['db']) . '.' PMA_backquote($cfgRelation['column_info'])
                   . 
' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
                   
' VALUES('
                   
'\'' PMA_sqlAddslashes($db) . '\','
                   
'\'' PMA_sqlAddslashes($table) . '\','
                   
'\'' PMA_sqlAddslashes($key) . '\','
                   
'\'' PMA_sqlAddslashes($mimetype) . '\','
                   
'\'' PMA_sqlAddslashes($transformation) . '\','
                   
'\'' PMA_sqlAddslashes($transformation_options) . '\')';
    }

    if (isset(
$upd_query)){
        return 
PMA_query_as_cu($upd_query);
    } else {
        return 
false;
    }
// end of 'PMA_setMIME()' function

/**
 * Returns the real filename of a configured transformation
 *
 * in fact: it just replaces old php3 with php extension
 *
 * garvin: for security, never allow to break out from transformations directory
 *
 * @uses    PMA_securePath()
 * @uses    preg_replace()
 * @uses    strlen()
 * @uses    file_exists()
 * @access  public
 * @param   string   $filename   the current filename
 * @return  string   the new filename
 */
function PMA_sanitizeTransformationFile(&$filename)
{
    
$include_file PMA_securePath($filename);

    
// This value can also contain a 'php3' value, in which case we map this filename to our new 'php' variant
    
$testfile preg_replace('@\.inc\.php3$@''.inc.php'$include_file);
    if (
$include_file{strlen($include_file)-1} == '3'
     
&& file_exists('./libraries/transformations/' $testfile)) {
        
$include_file $testfile;
        
$filename     $testfile// Corrects the referenced variable for further actions on the filename;
    
}

    return 
$include_file;
// end of 'PMA_sanitizeTransformationFile()' function
?>