bug #1233349 db search in MySQL 5.0.x on fields without a charset
[phpmyadmin/crack.git] / libraries / transformations.lib.php
blobf2706edde34617e18cadf8b4ca0d2ccd2381ee42
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Set of functions used with the relation and pdf feature
7 */
9 function PMA_transformation_getOptions($string) {
10 $transform_options = array();
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);
20 // strip possible slashes to behave like documentation says
21 $result = array();
22 foreach($transform_options as $val) {
23 $result[] = stripslashes($val);
25 return $result;
28 /**
29 * Gets all available MIME-types
31 * @return array array[mimetype], array[transformation]
33 * @access public
35 * @author Garvin Hicking <me@supergarv.de>
37 function PMA_getAvailableMIMEtypes() {
38 $handle = opendir('./libraries/transformations');
40 $stack = array();
41 $filestack = array();
43 while (($file = readdir($handle)) != false) {
44 $filestack[$file] = $file;
47 closedir($handle);
49 if (is_array($filestack)) {
50 @ksort($filestack);
51 foreach ($filestack AS $key => $file) {
53 if (preg_match('|^.*__.*\.inc\.php(3?)$|', trim($file), $match)) {
54 // File contains transformation functions.
55 $base = explode('__', str_replace('.inc.php' . $match[1], '', $file));
57 $mimetype = str_replace('_', '/', $base[0]);
58 $stack['mimetype'][$mimetype] = $mimetype;
60 $stack['transformation'][] = $mimetype . ': ' . $base[1];
61 $stack['transformation_file'][] = $file;
63 } else if (preg_match('|^.*\.inc\.php(3?)$|', trim($file), $match)) {
64 // File is a plain mimetype, no functions.
65 $base = str_replace('.inc.php' . $match[1], '', $file);
67 if ($base != 'global') {
68 $mimetype = str_replace('_', '/', $base);
69 $stack['mimetype'][$mimetype] = $mimetype;
70 $stack['empty_mimetype'][$mimetype] = $mimetype;
77 return $stack;
80 /**
81 * Gets the mimetypes for all rows of a table
83 * @param string the name of the db to check for
84 * @param string the name of the table to check for
85 * @param string whether to include only results having a mimetype set
87 * @return array [field_name][field_key] = field_value
89 * @global array the list of relations settings
91 * @access public
93 * @author Mike Beck <mikebeck@users.sourceforge.net> / Garvin Hicking <me@supergarv.de>
95 function PMA_getMIME($db, $table, $strict = false) {
96 global $cfgRelation;
98 $com_qry = 'SELECT column_name, mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info'])
99 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
100 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
101 . ' AND (mimetype != \'\'' . (!$strict ? ' OR transformation != \'\' OR transformation_options != \'\'' : '') . ')';
102 $com_rs = PMA_query_as_cu($com_qry);
104 while ($row = @PMA_DBI_fetch_assoc($com_rs)) {
105 $col = $row['column_name'];
106 $mime[$col]['mimetype'] = $row['mimetype'];
107 $mime[$col]['transformation'] = $row['transformation'];
108 $mime[$col]['transformation_options'] = $row['transformation_options'];
109 } // end while
110 PMA_DBI_free_result($com_rs);
111 unset($com_rs);
113 if (isset($mime) && is_array($mime)) {
114 return $mime;
115 } else {
116 return FALSE;
118 } // end of the 'PMA_getMIME()' function
121 * Set a single mimetype to a certain value.
123 * @param string the name of the db
124 * @param string the name of the table
125 * @param string the name of the column
126 * @param string the mimetype of the column
127 * @param string the transformation of the column
128 * @param string the transformation options of the column
129 * @param string (optional) force delete, will erase any existing comments for this column
131 * @return boolean true, if comment-query was made.
133 * @global array the list of relations settings
135 * @access public
137 function PMA_setMIME($db, $table, $key, $mimetype, $transformation, $transformation_options, $forcedelete = false) {
138 global $cfgRelation;
140 $test_qry = 'SELECT mimetype, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info'])
141 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
142 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
143 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
144 $test_rs = PMA_query_as_cu($test_qry, TRUE, PMA_DBI_QUERY_STORE);
146 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
147 $row = @PMA_DBI_fetch_assoc($test_rs);
148 PMA_DBI_free_result($test_rs);
149 unset($test_rs);
151 if (!$forcedelete && (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0 || strlen($row['comment']) > 0)) {
152 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
153 . ' SET mimetype = \'' . PMA_sqlAddslashes($mimetype) . '\','
154 . ' transformation = \'' . PMA_sqlAddslashes($transformation) . '\','
155 . ' transformation_options = \'' . PMA_sqlAddslashes($transformation_options) . '\''
156 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
157 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
158 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
159 } else {
160 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
161 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
162 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
163 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
165 } else if (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0) {
166 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info'])
167 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
168 . ' VALUES('
169 . '\'' . PMA_sqlAddslashes($db) . '\','
170 . '\'' . PMA_sqlAddslashes($table) . '\','
171 . '\'' . PMA_sqlAddslashes($key) . '\','
172 . '\'' . PMA_sqlAddslashes($mimetype) . '\','
173 . '\'' . PMA_sqlAddslashes($transformation) . '\','
174 . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
177 if (isset($upd_query)){
178 $upd_rs = PMA_query_as_cu($upd_query);
179 PMA_DBI_free_result($upd_rs);
180 unset($upd_rs);
181 return true;
182 } else {
183 return false;
185 } // end of 'PMA_setMIME()' function
188 * Returns the real filename of a configured transformation
190 * @param string the current filename
192 * @return string the new filename
194 * @access public
196 function PMA_sanitizeTransformationFile(&$filename) {
197 // garvin: for security, never allow to break out from transformations directory
199 $include_file = PMA_securePath($filename);
201 // This value can also contain a 'php3' value, in which case we map this filename to our new 'php' variant
202 $testfile = preg_replace('@\.inc\.php3$@', '.inc.php', $include_file);
203 if ($include_file{strlen($include_file)-1} == '3' && file_exists('./libraries/transformations/' . $testfile)) {
204 $include_file = $testfile;
205 $filename = $testfile; // Corrects the referenced variable for further actions on the filename;
208 return $include_file;
209 } // end of 'PMA_sanitizeTransformationFile()' function