3.4.9
[phpmyadmin/alexukf.git] / libraries / transformations.lib.php
blob7148003b531e2b2f8df3f7e0c10b74012f5a65d7
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 * @uses preg_split()
23 * @uses array_shift()
24 * @uses trim()
25 * @uses rtrim()
26 * @uses ltrim()
27 * @uses strlen()
28 * @uses substr()
29 * @uses stripslashes()
30 * @param string $option_string comma separated options
31 * @return array options
33 function PMA_transformation_getOptions($option_string)
35 $result = array();
37 if (! strlen($option_string)
38 || ! $transform_options = preg_split('/,/', $option_string)) {
39 return $result;
42 while (($option = array_shift($transform_options)) !== null) {
43 $trimmed = trim($option);
44 if (strlen($trimmed) > 1
45 && $trimmed[0] == "'"
46 && $trimmed[strlen($trimmed) - 1] == "'") {
47 // '...'
48 $option = substr($trimmed, 1, -1);
49 } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
50 // '...,
51 $trimmed = ltrim($option);
52 while (($option = array_shift($transform_options)) !== null) {
53 // ...,
54 $trimmed .= ',' . $option;
55 $rtrimmed = rtrim($trimmed);
56 if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
57 // ,...'
58 break;
61 $option = substr($rtrimmed, 1, -1);
63 $result[] = stripslashes($option);
66 return $result;
69 /**
70 * Gets all available MIME-types
72 * @access public
73 * @uses opendir()
74 * @uses readdir()
75 * @uses closedir()
76 * @uses sort()
77 * @uses preg_match()
78 * @uses explode()
79 * @uses str_replace()
80 * @staticvar array mimetypes
81 * @return array array[mimetype], array[transformation]
83 function PMA_getAvailableMIMEtypes()
85 static $stack = null;
87 if (null !== $stack) {
88 return $stack;
91 $stack = array();
92 $filestack = array();
94 $handle = opendir('./libraries/transformations');
96 if (! $handle) {
97 return $stack;
100 while ($file = readdir($handle)) {
101 $filestack[] = $file;
104 closedir($handle);
105 sort($filestack);
107 foreach ($filestack as $file) {
108 if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
109 // File contains transformation functions.
110 $base = explode('__', str_replace('.inc.php', '', $file));
111 $mimetype = str_replace('_', '/', $base[0]);
112 $stack['mimetype'][$mimetype] = $mimetype;
114 $stack['transformation'][] = $mimetype . ': ' . $base[1];
115 $stack['transformation_file'][] = $file;
117 } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
118 // File is a plain mimetype, no functions.
119 $base = str_replace('.inc.php', '', $file);
121 if ($base != 'global') {
122 $mimetype = str_replace('_', '/', $base);
123 $stack['mimetype'][$mimetype] = $mimetype;
124 $stack['empty_mimetype'][$mimetype] = $mimetype;
129 return $stack;
133 * Gets the mimetypes for all columns of a table
135 * @uses $GLOBALS['controllink']
136 * @uses PMA_getRelationsParam()
137 * @uses PMA_backquote()
138 * @uses PMA_sqlAddslashes()
139 * @uses PMA_DBI_fetch_result()
140 * @access public
141 * @param string $db the name of the db to check for
142 * @param string $table the name of the table to check for
143 * @param string $strict whether to include only results having a mimetype set
144 * @return array [field_name][field_key] = field_value
146 function PMA_getMIME($db, $table, $strict = false)
148 $cfgRelation = PMA_getRelationsParam();
150 if (! $cfgRelation['commwork']) {
151 return false;
154 $com_qry = '
155 SELECT `column_name`,
156 `mimetype`,
157 `transformation`,
158 `transformation_options`
159 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
160 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
161 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
162 AND ( `mimetype` != \'\'' . (!$strict ? '
163 OR `transformation` != \'\'
164 OR `transformation_options` != \'\'' : '') . ')';
165 return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
166 } // end of the 'PMA_getMIME()' function
169 * Set a single mimetype to a certain value.
171 * @uses PMA_DBI_QUERY_STORE
172 * @uses PMA_getRelationsParam()
173 * @uses PMA_backquote()
174 * @uses PMA_sqlAddslashes()
175 * @uses PMA_query_as_controluser()
176 * @uses PMA_DBI_num_rows()
177 * @uses PMA_DBI_fetch_assoc()
178 * @uses PMA_DBI_free_result()
179 * @uses strlen()
180 * @access public
181 * @param string $db the name of the db
182 * @param string $table the name of the table
183 * @param string $key the name of the column
184 * @param string $mimetype the mimetype of the column
185 * @param string $transformation the transformation of the column
186 * @param string $transformation_options the transformation options of the column
187 * @param string $forcedelete force delete, will erase any existing comments for this column
188 * @return boolean true, if comment-query was made.
190 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
191 $transformation_options, $forcedelete = false)
193 $cfgRelation = PMA_getRelationsParam();
195 if (! $cfgRelation['commwork']) {
196 return false;
199 $test_qry = '
200 SELECT `mimetype`,
201 `comment`
202 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
203 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
204 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
205 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
206 $test_rs = PMA_query_as_controluser($test_qry, true, PMA_DBI_QUERY_STORE);
208 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
209 $row = @PMA_DBI_fetch_assoc($test_rs);
210 PMA_DBI_free_result($test_rs);
212 if (! $forcedelete
213 && (strlen($mimetype) || strlen($transformation)
214 || strlen($transformation_options) || strlen($row['comment']))) {
215 $upd_query = '
216 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
217 SET `mimetype` = \'' . PMA_sqlAddslashes($mimetype) . '\',
218 `transformation` = \'' . PMA_sqlAddslashes($transformation) . '\',
219 `transformation_options` = \'' . PMA_sqlAddslashes($transformation_options) . '\'';
220 } else {
221 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
223 $upd_query .= '
224 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
225 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
226 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
227 } elseif (strlen($mimetype) || strlen($transformation)
228 || strlen($transformation_options)) {
229 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
230 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
231 . ' VALUES('
232 . '\'' . PMA_sqlAddslashes($db) . '\','
233 . '\'' . PMA_sqlAddslashes($table) . '\','
234 . '\'' . PMA_sqlAddslashes($key) . '\','
235 . '\'' . PMA_sqlAddslashes($mimetype) . '\','
236 . '\'' . PMA_sqlAddslashes($transformation) . '\','
237 . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
240 if (isset($upd_query)){
241 return PMA_query_as_controluser($upd_query);
242 } else {
243 return false;
245 } // end of 'PMA_setMIME()' function