Advisor: mark that 'Rate of reading fixed position' may be wrong, requires further...
[phpmyadmin/thilanka.git] / libraries / transformations.lib.php
blob51da6ce993a9a88084341c5f56446b23c93bad28
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used with the relation and pdf feature
6 * @package phpMyAdmin
7 */
9 /**
10 * returns array of options from string with options separated by comma, removes quotes
12 * <code>
13 * PMA_transformation_getOptions("'option ,, quoted',abd,'2,3',");
14 * // array {
15 * // 'option ,, quoted',
16 * // 'abc',
17 * // '2,3',
18 * // '',
19 * // }
20 * </code>
22 * @param string $option_string comma separated options
24 * @return array options
26 function PMA_transformation_getOptions($option_string)
28 $result = array();
30 if (! strlen($option_string)
31 || ! $transform_options = preg_split('/,/', $option_string)
32 ) {
33 return $result;
36 while (($option = array_shift($transform_options)) !== null) {
37 $trimmed = trim($option);
38 if (strlen($trimmed) > 1
39 && $trimmed[0] == "'"
40 && $trimmed[strlen($trimmed) - 1] == "'"
41 ) {
42 // '...'
43 $option = substr($trimmed, 1, -1);
44 } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
45 // '...,
46 $trimmed = ltrim($option);
47 while (($option = array_shift($transform_options)) !== null) {
48 // ...,
49 $trimmed .= ',' . $option;
50 $rtrimmed = rtrim($trimmed);
51 if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
52 // ,...'
53 break;
56 $option = substr($rtrimmed, 1, -1);
58 $result[] = stripslashes($option);
61 return $result;
64 /**
65 * Gets all available MIME-types
67 * @access public
68 * @staticvar array mimetypes
69 * @return array array[mimetype], array[transformation]
71 function PMA_getAvailableMIMEtypes()
73 static $stack = null;
75 if (null !== $stack) {
76 return $stack;
79 $stack = array();
80 $filestack = array();
82 $handle = opendir('./libraries/transformations');
84 if (! $handle) {
85 return $stack;
88 while ($file = readdir($handle)) {
89 $filestack[] = $file;
92 closedir($handle);
93 sort($filestack);
95 foreach ($filestack as $file) {
96 if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
97 // File contains transformation functions.
98 $base = explode('__', str_replace('.inc.php', '', $file));
99 $mimetype = str_replace('_', '/', $base[0]);
100 $stack['mimetype'][$mimetype] = $mimetype;
102 $stack['transformation'][] = $mimetype . ': ' . $base[1];
103 $stack['transformation_file'][] = $file;
105 } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
106 // File is a plain mimetype, no functions.
107 $base = str_replace('.inc.php', '', $file);
109 if ($base != 'global') {
110 $mimetype = str_replace('_', '/', $base);
111 $stack['mimetype'][$mimetype] = $mimetype;
112 $stack['empty_mimetype'][$mimetype] = $mimetype;
117 return $stack;
121 * Gets the mimetypes for all columns of a table
123 * @param string $db the name of the db to check for
124 * @param string $table the name of the table to check for
125 * @param string $strict whether to include only results having a mimetype set
127 * @access public
129 * @return array [field_name][field_key] = field_value
131 function PMA_getMIME($db, $table, $strict = false)
133 $cfgRelation = PMA_getRelationsParam();
135 if (! $cfgRelation['commwork']) {
136 return false;
139 $com_qry = '
140 SELECT `column_name`,
141 `mimetype`,
142 `transformation`,
143 `transformation_options`
144 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
145 WHERE `db_name` = \'' . PMA_sqlAddSlashes($db) . '\'
146 AND `table_name` = \'' . PMA_sqlAddSlashes($table) . '\'
147 AND ( `mimetype` != \'\'' . (!$strict ? '
148 OR `transformation` != \'\'
149 OR `transformation_options` != \'\'' : '') . ')';
150 return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
151 } // end of the 'PMA_getMIME()' function
154 * Set a single mimetype to a certain value.
156 * @param string $db the name of the db
157 * @param string $table the name of the table
158 * @param string $key the name of the column
159 * @param string $mimetype the mimetype of the column
160 * @param string $transformation the transformation of the column
161 * @param string $transformation_options the transformation options of the column
162 * @param string $forcedelete force delete, will erase any existing
163 * comments for this column
165 * @access public
167 * @return boolean true, if comment-query was made.
169 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
170 $transformation_options, $forcedelete = false)
172 $cfgRelation = PMA_getRelationsParam();
174 if (! $cfgRelation['commwork']) {
175 return false;
178 $test_qry = '
179 SELECT `mimetype`,
180 `comment`
181 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
182 WHERE `db_name` = \'' . PMA_sqlAddSlashes($db) . '\'
183 AND `table_name` = \'' . PMA_sqlAddSlashes($table) . '\'
184 AND `column_name` = \'' . PMA_sqlAddSlashes($key) . '\'';
185 $test_rs = PMA_query_as_controluser($test_qry, true, PMA_DBI_QUERY_STORE);
187 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
188 $row = @PMA_DBI_fetch_assoc($test_rs);
189 PMA_DBI_free_result($test_rs);
191 if (! $forcedelete
192 && (strlen($mimetype) || strlen($transformation)
193 || strlen($transformation_options) || strlen($row['comment']))
195 $upd_query = '
196 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
197 SET `mimetype` = \'' . PMA_sqlAddSlashes($mimetype) . '\',
198 `transformation` = \'' . PMA_sqlAddSlashes($transformation) . '\',
199 `transformation_options` = \'' . PMA_sqlAddSlashes($transformation_options) . '\'';
200 } else {
201 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
203 $upd_query .= '
204 WHERE `db_name` = \'' . PMA_sqlAddSlashes($db) . '\'
205 AND `table_name` = \'' . PMA_sqlAddSlashes($table) . '\'
206 AND `column_name` = \'' . PMA_sqlAddSlashes($key) . '\'';
207 } elseif (strlen($mimetype) || strlen($transformation)
208 || strlen($transformation_options)) {
209 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
210 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
211 . ' VALUES('
212 . '\'' . PMA_sqlAddSlashes($db) . '\','
213 . '\'' . PMA_sqlAddSlashes($table) . '\','
214 . '\'' . PMA_sqlAddSlashes($key) . '\','
215 . '\'' . PMA_sqlAddSlashes($mimetype) . '\','
216 . '\'' . PMA_sqlAddSlashes($transformation) . '\','
217 . '\'' . PMA_sqlAddSlashes($transformation_options) . '\')';
220 if (isset($upd_query)) {
221 return PMA_query_as_controluser($upd_query);
222 } else {
223 return false;
225 } // end of 'PMA_setMIME()' function