2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the relation and pdf feature
6 * This file also provides basic functions to use in other plungins!
7 * These are declared in the 'GLOBAL Plugin functions' section
9 * Please use short and expressive names.
10 * For now, special characters which aren't allowed in
11 * filenames or functions should not be used.
13 * Please provide a comment for your function,
14 * what it does and what parameters are available.
18 if (! defined('PHPMYADMIN')) {
23 * Returns array of options from string with options separated by comma,
27 * PMA_Transformation_getOptions("'option ,, quoted',abd,'2,3',");
29 * // 'option ,, quoted',
36 * @param string $option_string comma separated options
38 * @return array options
40 function PMA_Transformation_getOptions($option_string)
44 if (! strlen($option_string)
45 ||
! $transform_options = preg_split('/,/', $option_string)
50 while (($option = array_shift($transform_options)) !== null) {
51 $trimmed = trim($option);
52 if (strlen($trimmed) > 1
54 && $trimmed[strlen($trimmed) - 1] == "'"
57 $option = substr($trimmed, 1, -1);
58 } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
60 $trimmed = ltrim($option);
61 while (($option = array_shift($transform_options)) !== null) {
63 $trimmed .= ',' . $option;
64 $rtrimmed = rtrim($trimmed);
65 if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
70 $option = substr($rtrimmed, 1, -1);
72 $result[] = stripslashes($option);
79 * Gets all available MIME-types
82 * @staticvar array mimetypes
83 * @return array array[mimetype], array[transformation]
85 function PMA_getAvailableMIMEtypes()
89 if (null !== $stack) {
96 $handle = opendir('./libraries/plugins/transformations');
102 while ($file = readdir($handle)) {
103 $filestack[] = $file;
109 foreach ($filestack as $file) {
110 if (preg_match('|^.*_.*_.*\.class\.php$|', $file)) {
111 // File contains transformation functions.
112 $parts = explode('_', str_replace('.class.php', '', $file));
113 $mimetype = $parts[0] . "/" . $parts[1];
114 $stack['mimetype'][$mimetype] = $mimetype;
115 $stack['transformation'][] = $mimetype . ': ' . $parts[2];
116 $stack['transformation_file'][] = $file;
118 } elseif (preg_match('|^.*\.class.php$|', $file)) {
119 // File is a plain mimetype, no functions.
120 $base = str_replace('.class.php', '', $file);
122 if ($base != 'global') {
123 $mimetype = str_replace('_', '/', $base);
124 $stack['mimetype'][$mimetype] = $mimetype;
125 $stack['empty_mimetype'][$mimetype] = $mimetype;
134 * Returns the description of the transformation
136 * @param string $file transformation file
137 * @param string $html_formatted whether the description should be formatted
140 * @return the description of the transformation
142 function PMA_getTransformationDescription($file, $html_formatted = true)
144 // get the transformation class name
145 $class_name = explode(".class.php", $file);
146 $class_name = $class_name[0];
148 // include and instantiate the class
149 include_once 'libraries/plugins/transformations/' . $file;
150 return $class_name::getInfo();
154 * Gets the mimetypes for all columns of a table
156 * @param string $db the name of the db to check for
157 * @param string $table the name of the table to check for
158 * @param string $strict whether to include only results having a mimetype set
162 * @return array [field_name][field_key] = field_value
164 function PMA_getMIME($db, $table, $strict = false)
166 $cfgRelation = PMA_getRelationsParam();
168 if (! $cfgRelation['commwork']) {
173 SELECT `column_name`,
176 `transformation_options`
177 FROM ' . PMA_Util
::backquote($cfgRelation['db']) . '.'
178 . PMA_Util
::backquote($cfgRelation['column_info']) . '
179 WHERE `db_name` = \'' . PMA_Util
::sqlAddSlashes($db) . '\'
180 AND `table_name` = \'' . PMA_Util
::sqlAddSlashes($table) . '\'
181 AND ( `mimetype` != \'\'' . (!$strict ?
'
182 OR `transformation` != \'\'
183 OR `transformation_options` != \'\'' : '') . ')';
184 $result = $GLOBALS['dbi']->fetchResult(
185 $com_qry, 'column_name', null, $GLOBALS['controllink']
188 foreach ($result as $column => $values) {
189 // replacements in mimetype and transformation
190 $values = str_replace("jpeg", "JPEG", $values);
191 $values = str_replace("png", "PNG", $values);
192 $values = str_replace("octet-stream", "Octetstream", $values);
194 // convert mimetype to new format (f.e. Text_Plain, etc)
195 $delimiter_space = '- ';
197 $values['mimetype'] = str_replace(
209 // convert transformation to new format (class name)
210 // f.e. Text_Plain_Substring.class.php
211 $values = str_replace("__", "_", $values);
212 $values = str_replace(".inc.php", ".class.php", $values);
214 $values['transformation'] = str_replace(
221 $values['transformation']
226 $result[$column] = $values;
230 } // end of the 'PMA_getMIME()' function
233 * Set a single mimetype to a certain value.
235 * @param string $db the name of the db
236 * @param string $table the name of the table
237 * @param string $key the name of the column
238 * @param string $mimetype the mimetype of the column
239 * @param string $transformation the transformation of the column
240 * @param string $transformation_options the transformation options of the column
241 * @param string $forcedelete force delete, will erase any existing
242 * comments for this column
246 * @return boolean true, if comment-query was made.
248 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
249 $transformation_options, $forcedelete = false
251 $cfgRelation = PMA_getRelationsParam();
253 if (! $cfgRelation['commwork']) {
257 // convert mimetype to old format (f.e. text_plain)
258 $mimetype = strtolower($mimetype);
259 // old format has octet-stream instead of octetstream for mimetype
260 if (strstr($mimetype, "octetstream")) {
261 $mimetype = "application_octet-stream";
264 // convert transformation to old format (f.e. text_plain__substring.inc.php)
265 $transformation = strtolower($transformation);
266 $transformation = str_replace(".class.php", ".inc.php", $transformation);
267 $last_pos = strrpos($transformation, "_");
268 $transformation = substr($transformation, 0, $last_pos) . "_"
269 . substr($transformation, $last_pos);
274 FROM ' . PMA_Util
::backquote($cfgRelation['db']) . '.'
275 . PMA_Util
::backquote($cfgRelation['column_info']) . '
276 WHERE `db_name` = \'' . PMA_Util
::sqlAddSlashes($db) . '\'
277 AND `table_name` = \'' . PMA_Util
::sqlAddSlashes($table) . '\'
278 AND `column_name` = \'' . PMA_Util
::sqlAddSlashes($key) . '\'';
280 $test_rs = PMA_queryAsControlUser(
281 $test_qry, true, PMA_DatabaseInterface
::QUERY_STORE
284 if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
285 $row = @$GLOBALS['dbi']->fetchAssoc($test_rs);
286 $GLOBALS['dbi']->freeResult($test_rs);
289 && (strlen($mimetype) ||
strlen($transformation)
290 ||
strlen($transformation_options) ||
strlen($row['comment']))
292 $upd_query = 'UPDATE ' . PMA_Util
::backquote($cfgRelation['db']) . '.'
293 . PMA_Util
::backquote($cfgRelation['column_info'])
296 . PMA_Util
::sqlAddSlashes($mimetype) . '\', '
297 . '`transformation` = \''
298 . PMA_Util
::sqlAddSlashes($transformation) . '\', '
299 . '`transformation_options` = \''
300 . PMA_Util
::sqlAddSlashes($transformation_options) . '\'';
302 $upd_query = 'DELETE FROM ' . PMA_Util
::backquote($cfgRelation['db'])
303 . '.' . PMA_Util
::backquote($cfgRelation['column_info']);
306 WHERE `db_name` = \'' . PMA_Util
::sqlAddSlashes($db) . '\'
307 AND `table_name` = \'' . PMA_Util
::sqlAddSlashes($table) . '\'
308 AND `column_name` = \'' . PMA_Util
::sqlAddSlashes($key) . '\'';
309 } elseif (strlen($mimetype)
310 ||
strlen($transformation)
311 ||
strlen($transformation_options)
314 $upd_query = 'INSERT INTO ' . PMA_Util
::backquote($cfgRelation['db'])
315 . '.' . PMA_Util
::backquote($cfgRelation['column_info'])
316 . ' (db_name, table_name, column_name, mimetype, '
317 . 'transformation, transformation_options) '
319 . '\'' . PMA_Util
::sqlAddSlashes($db) . '\','
320 . '\'' . PMA_Util
::sqlAddSlashes($table) . '\','
321 . '\'' . PMA_Util
::sqlAddSlashes($key) . '\','
322 . '\'' . PMA_Util
::sqlAddSlashes($mimetype) . '\','
323 . '\'' . PMA_Util
::sqlAddSlashes($transformation) . '\','
324 . '\'' . PMA_Util
::sqlAddSlashes($transformation_options) . '\')';
327 if (isset($upd_query)) {
328 return PMA_queryAsControlUser($upd_query);
332 } // end of 'PMA_setMIME()' function
336 * GLOBAL Plugin functions
341 * Replaces "[__BUFFER__]" occurences found in $options['string'] with the text
342 * in $buffer, after performing a regular expression search and replace on
343 * $buffer using $options['regex'] and $options['regex_replace'].
345 * @param string $buffer text that will be replaced in $options['string'],
346 * after being formatted
347 * @param array $options the options required to format $buffer
349 * 'string' => 'string', // text containing "[__BUFFER__]"
350 * 'regex' => 'mixed', // the pattern to search for
351 * 'regex_replace' => 'mixed', // string or array of strings to replace
355 * @return string containing the text with all the replacements
357 function PMA_Transformation_globalHtmlReplace($buffer, $options = array())
359 if ( ! isset($options['string']) ) {
360 $options['string'] = '';
363 if (isset($options['regex']) && isset($options['regex_replace'])) {
364 $buffer = preg_replace(
365 '@' . str_replace('@', '\@', $options['regex']) . '@si',
366 $options['regex_replace'],
371 // Replace occurences of [__BUFFER__] with actual text
372 $return = str_replace("[__BUFFER__]", $buffer, $options['string']);
378 * Delete related transformation details
379 * after deleting database. table or column
381 * @param string $db Database name
382 * @param string $table Table name
383 * @param string $column Column name
385 * @return boolean State of the query execution
387 function PMA_clearTransformations($db, $table = '', $column = '')
389 $cfgRelation = PMA_getRelationsParam();
391 if (! isset($cfgRelation['column_info'])) {
395 $delete_sql = 'DELETE FROM '
396 . PMA_Util
::backquote($cfgRelation['db']) . '.'
397 . PMA_Util
::backquote($cfgRelation['column_info'])
400 if (($column != '') && ($table != '')) {
402 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
403 . '`table_name` = \'' . $table . '\' AND '
404 . '`column_name` = \'' . $column . '\' ';
406 } else if ($table != '') {
408 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
409 . '`table_name` = \'' . $table . '\' ';
412 $delete_sql .= '`db_name` = \'' . $db . '\' ';
415 return $GLOBALS['dbi']->tryQuery($delete_sql);