250 |
kaklik |
1 |
<?php |
|
|
2 |
/* $Id: transformations.lib.php,v 2.14 2006/01/19 15:39:29 cybot_tm Exp $ */ |
|
|
3 |
// vim: expandtab sw=4 ts=4 sts=4: |
|
|
4 |
|
|
|
5 |
/** |
|
|
6 |
* Set of functions used with the relation and pdf feature |
|
|
7 |
*/ |
|
|
8 |
|
|
|
9 |
function PMA_transformation_getOptions($string) { |
|
|
10 |
$transform_options = array(); |
|
|
11 |
|
|
|
12 |
if ($string != '') { |
|
|
13 |
if ($string{0} == "'" && $string{strlen($string)-1} == "'") { |
|
|
14 |
$transform_options = explode('\',\'', substr($string, 1, strlen($string)-2)); |
|
|
15 |
} else { |
|
|
16 |
$transform_options = array(0 => $string); |
|
|
17 |
} |
|
|
18 |
} |
|
|
19 |
|
|
|
20 |
// strip possible slashes to behave like documentation says |
|
|
21 |
$result = array(); |
|
|
22 |
foreach ($transform_options as $val) { |
|
|
23 |
$result[] = stripslashes($val); |
|
|
24 |
} |
|
|
25 |
return $result; |
|
|
26 |
} |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* Gets all available MIME-types |
|
|
30 |
* |
|
|
31 |
* @return array array[mimetype], array[transformation] |
|
|
32 |
* |
|
|
33 |
* @access public |
|
|
34 |
* |
|
|
35 |
* @author Garvin Hicking <me@supergarv.de> |
|
|
36 |
*/ |
|
|
37 |
function PMA_getAvailableMIMEtypes() { |
|
|
38 |
$handle = opendir('./libraries/transformations'); |
|
|
39 |
|
|
|
40 |
$stack = array(); |
|
|
41 |
$filestack = array(); |
|
|
42 |
|
|
|
43 |
while (($file = readdir($handle)) != false) { |
|
|
44 |
$filestack[$file] = $file; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
closedir($handle); |
|
|
48 |
|
|
|
49 |
if (is_array($filestack)) { |
|
|
50 |
@ksort($filestack); |
|
|
51 |
foreach ($filestack AS $key => $file) { |
|
|
52 |
|
|
|
53 |
if (preg_match('|^.*__.*\.inc\.php$|', trim($file))) { |
|
|
54 |
// File contains transformation functions. |
|
|
55 |
$base = explode('__', str_replace('.inc.php', '', $file)); |
|
|
56 |
$mimetype = str_replace('_', '/', $base[0]); |
|
|
57 |
$stack['mimetype'][$mimetype] = $mimetype; |
|
|
58 |
|
|
|
59 |
$stack['transformation'][] = $mimetype . ': ' . $base[1]; |
|
|
60 |
$stack['transformation_file'][] = $file; |
|
|
61 |
|
|
|
62 |
} elseif (preg_match('|^.*\.inc\.php$|', trim($file))) { |
|
|
63 |
// File is a plain mimetype, no functions. |
|
|
64 |
$base = str_replace('.inc.php', '', $file); |
|
|
65 |
|
|
|
66 |
if ($base != 'global') { |
|
|
67 |
$mimetype = str_replace('_', '/', $base); |
|
|
68 |
$stack['mimetype'][$mimetype] = $mimetype; |
|
|
69 |
$stack['empty_mimetype'][$mimetype] = $mimetype; |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
} |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
return $stack; |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
/** |
|
|
80 |
* Gets the mimetypes for all rows of a table |
|
|
81 |
* |
|
|
82 |
* @param string the name of the db to check for |
|
|
83 |
* @param string the name of the table to check for |
|
|
84 |
* @param string whether to include only results having a mimetype set |
|
|
85 |
* |
|
|
86 |
* @return array [field_name][field_key] = field_value |
|
|
87 |
* |
|
|
88 |
* @global array the list of relations settings |
|
|
89 |
* |
|
|
90 |
* @access public |
|
|
91 |
* |
|
|
92 |
* @author Mike Beck <mikebeck@users.sourceforge.net> / Garvin Hicking <me@supergarv.de> |
|
|
93 |
*/ |
|
|
94 |
function PMA_getMIME($db, $table, $strict = false) { |
|
|
95 |
global $cfgRelation; |
|
|
96 |
|
|
|
97 |
$com_qry = 'SELECT column_name, mimetype, transformation, transformation_options FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info']) |
|
|
98 |
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' |
|
|
99 |
. ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'' |
|
|
100 |
. ' AND (mimetype != \'\'' . (!$strict ? ' OR transformation != \'\' OR transformation_options != \'\'' : '') . ')'; |
|
|
101 |
$com_rs = PMA_query_as_cu($com_qry); |
|
|
102 |
|
|
|
103 |
while ($row = @PMA_DBI_fetch_assoc($com_rs)) { |
|
|
104 |
$col = $row['column_name']; |
|
|
105 |
$mime[$col]['mimetype'] = $row['mimetype']; |
|
|
106 |
$mime[$col]['transformation'] = $row['transformation']; |
|
|
107 |
$mime[$col]['transformation_options'] = $row['transformation_options']; |
|
|
108 |
} // end while |
|
|
109 |
PMA_DBI_free_result($com_rs); |
|
|
110 |
unset($com_rs); |
|
|
111 |
|
|
|
112 |
if (isset($mime) && is_array($mime)) { |
|
|
113 |
return $mime; |
|
|
114 |
} else { |
|
|
115 |
return FALSE; |
|
|
116 |
} |
|
|
117 |
} // end of the 'PMA_getMIME()' function |
|
|
118 |
|
|
|
119 |
/** |
|
|
120 |
* Set a single mimetype to a certain value. |
|
|
121 |
* |
|
|
122 |
* @param string the name of the db |
|
|
123 |
* @param string the name of the table |
|
|
124 |
* @param string the name of the column |
|
|
125 |
* @param string the mimetype of the column |
|
|
126 |
* @param string the transformation of the column |
|
|
127 |
* @param string the transformation options of the column |
|
|
128 |
* @param string (optional) force delete, will erase any existing comments for this column |
|
|
129 |
* |
|
|
130 |
* @return boolean true, if comment-query was made. |
|
|
131 |
* |
|
|
132 |
* @global array the list of relations settings |
|
|
133 |
* |
|
|
134 |
* @access public |
|
|
135 |
*/ |
|
|
136 |
function PMA_setMIME($db, $table, $key, $mimetype, $transformation, $transformation_options, $forcedelete = false) { |
|
|
137 |
global $cfgRelation; |
|
|
138 |
|
|
|
139 |
$test_qry = 'SELECT mimetype, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info']) |
|
|
140 |
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' |
|
|
141 |
. ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'' |
|
|
142 |
. ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\''; |
|
|
143 |
$test_rs = PMA_query_as_cu($test_qry, TRUE, PMA_DBI_QUERY_STORE); |
|
|
144 |
|
|
|
145 |
if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { |
|
|
146 |
$row = @PMA_DBI_fetch_assoc($test_rs); |
|
|
147 |
PMA_DBI_free_result($test_rs); |
|
|
148 |
unset($test_rs); |
|
|
149 |
|
|
|
150 |
if (!$forcedelete && (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0 || strlen($row['comment']) > 0)) { |
|
|
151 |
$upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info']) |
|
|
152 |
. ' SET mimetype = \'' . PMA_sqlAddslashes($mimetype) . '\',' |
|
|
153 |
. ' transformation = \'' . PMA_sqlAddslashes($transformation) . '\',' |
|
|
154 |
. ' transformation_options = \'' . PMA_sqlAddslashes($transformation_options) . '\'' |
|
|
155 |
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' |
|
|
156 |
. ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'' |
|
|
157 |
. ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\''; |
|
|
158 |
} else { |
|
|
159 |
$upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info']) |
|
|
160 |
. ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' |
|
|
161 |
. ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'' |
|
|
162 |
. ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\''; |
|
|
163 |
} |
|
|
164 |
} elseif (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0) { |
|
|
165 |
$upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info']) |
|
|
166 |
. ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) ' |
|
|
167 |
. ' VALUES(' |
|
|
168 |
. '\'' . PMA_sqlAddslashes($db) . '\',' |
|
|
169 |
. '\'' . PMA_sqlAddslashes($table) . '\',' |
|
|
170 |
. '\'' . PMA_sqlAddslashes($key) . '\',' |
|
|
171 |
. '\'' . PMA_sqlAddslashes($mimetype) . '\',' |
|
|
172 |
. '\'' . PMA_sqlAddslashes($transformation) . '\',' |
|
|
173 |
. '\'' . PMA_sqlAddslashes($transformation_options) . '\')'; |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
if (isset($upd_query)){ |
|
|
177 |
$upd_rs = PMA_query_as_cu($upd_query); |
|
|
178 |
PMA_DBI_free_result($upd_rs); |
|
|
179 |
unset($upd_rs); |
|
|
180 |
return true; |
|
|
181 |
} else { |
|
|
182 |
return false; |
|
|
183 |
} |
|
|
184 |
} // end of 'PMA_setMIME()' function |
|
|
185 |
|
|
|
186 |
/** |
|
|
187 |
* Returns the real filename of a configured transformation |
|
|
188 |
* |
|
|
189 |
* @param string the current filename |
|
|
190 |
* |
|
|
191 |
* @return string the new filename |
|
|
192 |
* |
|
|
193 |
* @access public |
|
|
194 |
*/ |
|
|
195 |
function PMA_sanitizeTransformationFile(&$filename) { |
|
|
196 |
// garvin: for security, never allow to break out from transformations directory |
|
|
197 |
|
|
|
198 |
$include_file = PMA_securePath($filename); |
|
|
199 |
|
|
|
200 |
// This value can also contain a 'php3' value, in which case we map this filename to our new 'php' variant |
|
|
201 |
$testfile = preg_replace('@\.inc\.php3$@', '.inc.php', $include_file); |
|
|
202 |
if ($include_file{strlen($include_file)-1} == '3' && file_exists('./libraries/transformations/' . $testfile)) { |
|
|
203 |
$include_file = $testfile; |
|
|
204 |
$filename = $testfile; // Corrects the referenced variable for further actions on the filename; |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
return $include_file; |
|
|
208 |
} // end of 'PMA_sanitizeTransformationFile()' function |
|
|
209 |
?> |