3.3.6-rc1
[phpmyadmin/dkf.git] / libraries / transformations.lib.php
blobdf97a2ec3272755c14ab2e6137e9ab13cbaa6c0d
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 * @author Garvin Hicking <me@supergarv.de>
75 * @uses opendir()
76 * @uses readdir()
77 * @uses closedir()
78 * @uses sort()
79 * @uses preg_match()
80 * @uses explode()
81 * @uses str_replace()
82 * @staticvar array mimetypes
83 * @return array array[mimetype], array[transformation]
85 function PMA_getAvailableMIMEtypes()
87 static $stack = null;
89 if (null !== $stack) {
90 return $stack;
93 $stack = array();
94 $filestack = array();
96 $handle = opendir('./libraries/transformations');
98 if (! $handle) {
99 return $stack;
102 while ($file = readdir($handle)) {
103 $filestack[] = $file;
106 closedir($handle);
107 sort($filestack);
109 foreach ($filestack as $file) {
110 if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
111 // File contains transformation functions.
112 $base = explode('__', str_replace('.inc.php', '', $file));
113 $mimetype = str_replace('_', '/', $base[0]);
114 $stack['mimetype'][$mimetype] = $mimetype;
116 $stack['transformation'][] = $mimetype . ': ' . $base[1];
117 $stack['transformation_file'][] = $file;
119 } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
120 // File is a plain mimetype, no functions.
121 $base = str_replace('.inc.php', '', $file);
123 if ($base != 'global') {
124 $mimetype = str_replace('_', '/', $base);
125 $stack['mimetype'][$mimetype] = $mimetype;
126 $stack['empty_mimetype'][$mimetype] = $mimetype;
131 return $stack;
135 * Gets the mimetypes for all columns of a table
137 * @uses $GLOBALS['controllink']
138 * @uses PMA_getRelationsParam()
139 * @uses PMA_backquote()
140 * @uses PMA_sqlAddslashes()
141 * @uses PMA_DBI_fetch_result()
142 * @author Mike Beck <mikebeck@users.sourceforge.net>
143 * @author Garvin Hicking <me@supergarv.de>
144 * @access public
145 * @param string $db the name of the db to check for
146 * @param string $table the name of the table to check for
147 * @param string $strict whether to include only results having a mimetype set
148 * @return array [field_name][field_key] = field_value
150 function PMA_getMIME($db, $table, $strict = false)
152 $cfgRelation = PMA_getRelationsParam();
154 if (! $cfgRelation['commwork']) {
155 return false;
158 $com_qry = '
159 SELECT `column_name`,
160 `mimetype`,
161 `transformation`,
162 `transformation_options`
163 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
164 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
165 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
166 AND ( `mimetype` != \'\'' . (!$strict ? '
167 OR `transformation` != \'\'
168 OR `transformation_options` != \'\'' : '') . ')';
169 return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
170 } // end of the 'PMA_getMIME()' function
173 * Set a single mimetype to a certain value.
175 * @uses PMA_DBI_QUERY_STORE
176 * @uses PMA_getRelationsParam()
177 * @uses PMA_backquote()
178 * @uses PMA_sqlAddslashes()
179 * @uses PMA_query_as_controluser()
180 * @uses PMA_DBI_num_rows()
181 * @uses PMA_DBI_fetch_assoc()
182 * @uses PMA_DBI_free_result()
183 * @uses strlen()
184 * @access public
185 * @param string $db the name of the db
186 * @param string $table the name of the table
187 * @param string $key the name of the column
188 * @param string $mimetype the mimetype of the column
189 * @param string $transformation the transformation of the column
190 * @param string $transformation_options the transformation options of the column
191 * @param string $forcedelete force delete, will erase any existing comments for this column
192 * @return boolean true, if comment-query was made.
194 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
195 $transformation_options, $forcedelete = false)
197 $cfgRelation = PMA_getRelationsParam();
199 if (! $cfgRelation['commwork']) {
200 return false;
203 $test_qry = '
204 SELECT `mimetype`,
205 `comment`
206 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
207 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
208 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
209 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
210 $test_rs = PMA_query_as_controluser($test_qry, true, PMA_DBI_QUERY_STORE);
212 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
213 $row = @PMA_DBI_fetch_assoc($test_rs);
214 PMA_DBI_free_result($test_rs);
216 if (! $forcedelete
217 && (strlen($mimetype) || strlen($transformation)
218 || strlen($transformation_options) || strlen($row['comment']))) {
219 $upd_query = '
220 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
221 SET `mimetype` = \'' . PMA_sqlAddslashes($mimetype) . '\',
222 `transformation` = \'' . PMA_sqlAddslashes($transformation) . '\',
223 `transformation_options` = \'' . PMA_sqlAddslashes($transformation_options) . '\'';
224 } else {
225 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
227 $upd_query .= '
228 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
229 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
230 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
231 } elseif (strlen($mimetype) || strlen($transformation)
232 || strlen($transformation_options)) {
233 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
234 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
235 . ' VALUES('
236 . '\'' . PMA_sqlAddslashes($db) . '\','
237 . '\'' . PMA_sqlAddslashes($table) . '\','
238 . '\'' . PMA_sqlAddslashes($key) . '\','
239 . '\'' . PMA_sqlAddslashes($mimetype) . '\','
240 . '\'' . PMA_sqlAddslashes($transformation) . '\','
241 . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
244 if (isset($upd_query)){
245 return PMA_query_as_controluser($upd_query);
246 } else {
247 return false;
249 } // end of 'PMA_setMIME()' function