Revert initial commit
[phpmyadmin/blinky.git] / libraries / transformations.lib.php
blobc3249f18eae83e30538c4efd532e989d37b96a96
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 * @version $Id$
7 * @package phpMyAdmin
8 */
10 /**
11 * returns array of options from string with options separated by comma, removes quotes
13 * <code>
14 * PMA_transformation_getOptions("'option ,, quoted',abd,'2,3',");
15 * // array {
16 * // 'option ,, quoted',
17 * // 'abc',
18 * // '2,3',
19 * // '',
20 * // }
21 * </code>
23 * @uses preg_split()
24 * @uses array_shift()
25 * @uses trim()
26 * @uses rtrim()
27 * @uses ltrim()
28 * @uses strlen()
29 * @uses substr()
30 * @uses stripslashes()
31 * @param string $option_string comma separated options
32 * @return array options
34 function PMA_transformation_getOptions($option_string)
36 $result = array();
38 if (! strlen($option_string)
39 || ! $transform_options = preg_split('/,/', $option_string)) {
40 return $result;
43 while (($option = array_shift($transform_options)) !== null) {
44 $trimmed = trim($option);
45 if (strlen($trimmed) > 1
46 && $trimmed[0] == "'"
47 && $trimmed[strlen($trimmed) - 1] == "'") {
48 // '...'
49 $option = substr($trimmed, 1, -1);
50 } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
51 // '...,
52 $trimmed = ltrim($option);
53 while (($option = array_shift($transform_options)) !== null) {
54 // ...,
55 $trimmed .= ',' . $option;
56 $rtrimmed = rtrim($trimmed);
57 if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
58 // ,...'
59 break;
62 $option = substr($rtrimmed, 1, -1);
64 $result[] = stripslashes($option);
67 return $result;
70 /**
71 * Gets all available MIME-types
73 * @access public
74 * @uses opendir()
75 * @uses readdir()
76 * @uses closedir()
77 * @uses sort()
78 * @uses preg_match()
79 * @uses explode()
80 * @uses str_replace()
81 * @staticvar array mimetypes
82 * @return array array[mimetype], array[transformation]
84 function PMA_getAvailableMIMEtypes()
86 static $stack = null;
88 if (null !== $stack) {
89 return $stack;
92 $stack = array();
93 $filestack = array();
95 $handle = opendir('./libraries/transformations');
97 if (! $handle) {
98 return $stack;
101 while ($file = readdir($handle)) {
102 $filestack[] = $file;
105 closedir($handle);
106 sort($filestack);
108 foreach ($filestack as $file) {
109 if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
110 // File contains transformation functions.
111 $base = explode('__', str_replace('.inc.php', '', $file));
112 $mimetype = str_replace('_', '/', $base[0]);
113 $stack['mimetype'][$mimetype] = $mimetype;
115 $stack['transformation'][] = $mimetype . ': ' . $base[1];
116 $stack['transformation_file'][] = $file;
118 } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
119 // File is a plain mimetype, no functions.
120 $base = str_replace('.inc.php', '', $file);
122 if ($base != 'global') {
123 $mimetype = str_replace('_', '/', $base);
124 $stack['mimetype'][$mimetype] = $mimetype;
125 $stack['empty_mimetype'][$mimetype] = $mimetype;
130 return $stack;
134 * Gets the mimetypes for all columns of a table
136 * @uses $GLOBALS['controllink']
137 * @uses PMA_getRelationsParam()
138 * @uses PMA_backquote()
139 * @uses PMA_sqlAddslashes()
140 * @uses PMA_DBI_fetch_result()
141 * @access public
142 * @param string $db the name of the db to check for
143 * @param string $table the name of the table to check for
144 * @param string $strict whether to include only results having a mimetype set
145 * @return array [field_name][field_key] = field_value
147 function PMA_getMIME($db, $table, $strict = false)
149 $cfgRelation = PMA_getRelationsParam();
151 if (! $cfgRelation['commwork']) {
152 return false;
155 $com_qry = '
156 SELECT `column_name`,
157 `mimetype`,
158 `transformation`,
159 `transformation_options`
160 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
161 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
162 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
163 AND ( `mimetype` != \'\'' . (!$strict ? '
164 OR `transformation` != \'\'
165 OR `transformation_options` != \'\'' : '') . ')';
166 return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
167 } // end of the 'PMA_getMIME()' function
170 * Set a single mimetype to a certain value.
172 * @uses PMA_DBI_QUERY_STORE
173 * @uses PMA_getRelationsParam()
174 * @uses PMA_backquote()
175 * @uses PMA_sqlAddslashes()
176 * @uses PMA_query_as_controluser()
177 * @uses PMA_DBI_num_rows()
178 * @uses PMA_DBI_fetch_assoc()
179 * @uses PMA_DBI_free_result()
180 * @uses strlen()
181 * @access public
182 * @param string $db the name of the db
183 * @param string $table the name of the table
184 * @param string $key the name of the column
185 * @param string $mimetype the mimetype of the column
186 * @param string $transformation the transformation of the column
187 * @param string $transformation_options the transformation options of the column
188 * @param string $forcedelete force delete, will erase any existing comments for this column
189 * @return boolean true, if comment-query was made.
191 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
192 $transformation_options, $forcedelete = false)
194 $cfgRelation = PMA_getRelationsParam();
196 if (! $cfgRelation['commwork']) {
197 return false;
200 $test_qry = '
201 SELECT `mimetype`,
202 `comment`
203 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
204 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
205 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
206 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
207 $test_rs = PMA_query_as_controluser($test_qry, true, PMA_DBI_QUERY_STORE);
209 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
210 $row = @PMA_DBI_fetch_assoc($test_rs);
211 PMA_DBI_free_result($test_rs);
213 if (! $forcedelete
214 && (strlen($mimetype) || strlen($transformation)
215 || strlen($transformation_options) || strlen($row['comment']))) {
216 $upd_query = '
217 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
218 SET `mimetype` = \'' . PMA_sqlAddslashes($mimetype) . '\',
219 `transformation` = \'' . PMA_sqlAddslashes($transformation) . '\',
220 `transformation_options` = \'' . PMA_sqlAddslashes($transformation_options) . '\'';
221 } else {
222 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
224 $upd_query .= '
225 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
226 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
227 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
228 } elseif (strlen($mimetype) || strlen($transformation)
229 || strlen($transformation_options)) {
230 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
231 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
232 . ' VALUES('
233 . '\'' . PMA_sqlAddslashes($db) . '\','
234 . '\'' . PMA_sqlAddslashes($table) . '\','
235 . '\'' . PMA_sqlAddslashes($key) . '\','
236 . '\'' . PMA_sqlAddslashes($mimetype) . '\','
237 . '\'' . PMA_sqlAddslashes($transformation) . '\','
238 . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
241 if (isset($upd_query)){
242 return PMA_query_as_controluser($upd_query);
243 } else {
244 return false;
246 } // end of 'PMA_setMIME()' function