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 plugins!
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) === 0
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 = mb_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 = mb_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) {
100 foreach ($sub_dirs as $sd => $prefix) {
101 $handle = opendir('libraries/plugins/transformations/' . $sd);
104 $stack[$prefix . 'transformation'] = array();
105 $stack[$prefix . 'transformation_file'] = array();
109 $filestack = array();
110 while ($file = readdir($handle)) {
111 // Ignore hidden files
112 if ($file[0] == '.') {
115 // Ignore old plugins (.class in filename)
116 if (strpos($file, '.class') !== false) {
119 $filestack[] = $file;
125 foreach ($filestack as $file) {
126 if (preg_match('|^[^.].*_.*_.*\.php$|', $file)) {
127 // File contains transformation functions.
128 $parts = explode('_', str_replace('.php', '', $file));
129 $mimetype = $parts[0] . "/" . $parts[1];
130 $stack['mimetype'][$mimetype] = $mimetype;
132 $stack[$prefix . 'transformation'][] = $mimetype . ': ' . $parts[2];
133 $stack[$prefix . 'transformation_file'][] = $sd . $file;
135 $stack['input_transformation'][] = $mimetype . ': ' . $parts[2];
136 $stack['input_transformation_file'][] = $sd . $file;
139 } elseif (preg_match('|^[^.].*\.php$|', $file)) {
140 // File is a plain mimetype, no functions.
141 $base = str_replace('.php', '', $file);
143 if ($base != 'global') {
144 $mimetype = str_replace('_', '/', $base);
145 $stack['mimetype'][$mimetype] = $mimetype;
146 $stack['empty_mimetype'][$mimetype] = $mimetype;
155 * Returns the class name of the transformation
157 * @param string $filename transformation file name
159 * @return string the class name of transformation
161 function PMA_getTransformationClassName($filename)
163 // get the transformation class name
164 $class_name = explode(".php", $filename);
165 $class_name = 'PMA\\' . str_replace('/', '\\', $class_name[0]);
171 * Returns the description of the transformation
173 * @param string $file transformation file
175 * @return String the description of the transformation
177 function PMA_getTransformationDescription($file)
179 $include_file = 'libraries/plugins/transformations/' . $file;
180 /* @var $class_name PMA\libraries\plugins\TransformationsInterface */
181 $class_name = PMA_getTransformationClassName($include_file);
182 // include and instantiate the class
183 include_once $include_file;
184 return $class_name::getInfo();
188 * Returns the name of the transformation
190 * @param string $file transformation file
192 * @return String the name of the transformation
194 function PMA_getTransformationName($file)
196 $include_file = 'libraries/plugins/transformations/' . $file;
197 /* @var $class_name PMA\libraries\plugins\TransformationsInterface */
198 $class_name = PMA_getTransformationClassName($include_file);
199 // include and instantiate the class
200 include_once $include_file;
201 return $class_name::getName();
205 * Fixups old MIME or tranformation name to new one
207 * - applies some hardcoded fixups
208 * - adds spaces after _ and numbers
209 * - capitalizes words
210 * - removes back spaces
212 * @param string $value Value to fixup
216 function PMA_fixupMIME($value)
218 $value = str_replace(
219 array("jpeg", "png"), array("JPEG", "PNG"), $value
225 preg_replace('/([0-9_]+)/', '$1 ', $value)
231 * Gets the mimetypes for all columns of a table
233 * @param string $db the name of the db to check for
234 * @param string $table the name of the table to check for
235 * @param boolean $strict whether to include only results having a mimetype set
236 * @param boolean $fullName whether to use full column names as the key
240 * @return array [field_name][field_key] = field_value
242 function PMA_getMIME($db, $table, $strict = false, $fullName = false)
244 $cfgRelation = PMA_getRelationsParam();
246 if (! $cfgRelation['commwork']) {
252 $com_qry .= "SELECT CONCAT("
253 . "`db_name`, '.', `table_name`, '.', `column_name`"
254 . ") AS column_name, ";
256 $com_qry = "SELECT `column_name`, ";
258 $com_qry .= '`mimetype`,
260 `transformation_options`,
261 `input_transformation`,
262 `input_transformation_options`
263 FROM ' . PMA\libraries\Util
::backquote($cfgRelation['db']) . '.'
264 . PMA\libraries\Util
::backquote($cfgRelation['column_info']) . '
265 WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
266 AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table) . '\'
267 AND ( `mimetype` != \'\'' . (!$strict ?
'
268 OR `transformation` != \'\'
269 OR `transformation_options` != \'\'
270 OR `input_transformation` != \'\'
271 OR `input_transformation_options` != \'\'' : '') . ')';
272 $result = $GLOBALS['dbi']->fetchResult(
273 $com_qry, 'column_name', null, $GLOBALS['controllink']
276 foreach ($result as $column => $values) {
277 // replacements in mimetype and transformation
278 $values = str_replace("jpeg", "JPEG", $values);
279 $values = str_replace("png", "PNG", $values);
281 // convert mimetype to new format (f.e. Text_Plain, etc)
282 $values['mimetype'] = PMA_fixupMIME($values['mimetype']);
284 // For transformation of form
285 // output/image_jpeg__inline.inc.php
287 $dir = explode('/', $values['transformation']);
289 if (count($dir) === 2) {
290 $subdir = $dir[0] . '/';
291 $values['transformation'] = $dir[1];
294 $values['transformation'] = PMA_fixupMIME($values['transformation']);
295 $values['transformation'] = $subdir . $values['transformation'];
296 $result[$column] = $values;
300 } // end of the 'PMA_getMIME()' function
303 * Set a single mimetype to a certain value.
305 * @param string $db the name of the db
306 * @param string $table the name of the table
307 * @param string $key the name of the column
308 * @param string $mimetype the mimetype of the column
309 * @param string $transformation the transformation of the column
310 * @param string $transformationOpts the transformation options of the column
311 * @param string $inputTransform the input transformation of the column
312 * @param string $inputTransformOpts the input transformation options of the column
313 * @param boolean $forcedelete force delete, will erase any existing
314 * comments for this column
318 * @return boolean true, if comment-query was made.
320 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
321 $transformationOpts, $inputTransform, $inputTransformOpts, $forcedelete = false
323 $cfgRelation = PMA_getRelationsParam();
325 if (! $cfgRelation['commwork']) {
329 // lowercase mimetype & transformation
330 $mimetype = mb_strtolower($mimetype);
331 $transformation = mb_strtolower($transformation);
333 // Do we have any parameter to set?
335 strlen($mimetype) > 0 ||
336 strlen($transformation) > 0 ||
337 strlen($transformationOpts) > 0 ||
338 strlen($inputTransform) > 0 ||
339 strlen($inputTransformOpts) > 0
345 FROM ' . PMA\libraries\Util
::backquote($cfgRelation['db']) . '.'
346 . PMA\libraries\Util
::backquote($cfgRelation['column_info']) . '
347 WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
348 AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table) . '\'
349 AND `column_name` = \'' . $GLOBALS['dbi']->escapeString($key) . '\'';
351 $test_rs = PMA_queryAsControlUser(
352 $test_qry, true, PMA\libraries\DatabaseInterface
::QUERY_STORE
355 if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
356 $row = @$GLOBALS['dbi']->fetchAssoc($test_rs);
357 $GLOBALS['dbi']->freeResult($test_rs);
359 if (! $forcedelete && ($has_value ||
strlen($row['comment']) > 0)) {
360 $upd_query = 'UPDATE '
361 . PMA\libraries\Util
::backquote($cfgRelation['db']) . '.'
362 . PMA\libraries\Util
::backquote($cfgRelation['column_info'])
365 . $GLOBALS['dbi']->escapeString($mimetype) . '\', '
366 . '`transformation` = \''
367 . $GLOBALS['dbi']->escapeString($transformation) . '\', '
368 . '`transformation_options` = \''
369 . $GLOBALS['dbi']->escapeString($transformationOpts) . '\', '
370 . '`input_transformation` = \''
371 . $GLOBALS['dbi']->escapeString($inputTransform) . '\', '
372 . '`input_transformation_options` = \''
373 . $GLOBALS['dbi']->escapeString($inputTransformOpts) . '\'';
375 $upd_query = 'DELETE FROM '
376 . PMA\libraries\Util
::backquote($cfgRelation['db'])
377 . '.' . PMA\libraries\Util
::backquote($cfgRelation['column_info']);
380 WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
381 AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table)
383 AND `column_name` = \'' . $GLOBALS['dbi']->escapeString($key)
385 } elseif ($has_value) {
387 $upd_query = 'INSERT INTO '
388 . PMA\libraries\Util
::backquote($cfgRelation['db'])
389 . '.' . PMA\libraries\Util
::backquote($cfgRelation['column_info'])
390 . ' (db_name, table_name, column_name, mimetype, '
391 . 'transformation, transformation_options, '
392 . 'input_transformation, input_transformation_options) '
394 . '\'' . $GLOBALS['dbi']->escapeString($db) . '\','
395 . '\'' . $GLOBALS['dbi']->escapeString($table) . '\','
396 . '\'' . $GLOBALS['dbi']->escapeString($key) . '\','
397 . '\'' . $GLOBALS['dbi']->escapeString($mimetype) . '\','
398 . '\'' . $GLOBALS['dbi']->escapeString($transformation) . '\','
399 . '\'' . $GLOBALS['dbi']->escapeString($transformationOpts) . '\','
400 . '\'' . $GLOBALS['dbi']->escapeString($inputTransform) . '\','
401 . '\'' . $GLOBALS['dbi']->escapeString($inputTransformOpts) . '\')';
404 if (isset($upd_query)) {
405 return PMA_queryAsControlUser($upd_query);
409 } // end of 'PMA_setMIME()' function
413 * GLOBAL Plugin functions
417 * Delete related transformation details
418 * after deleting database. table or column
420 * @param string $db Database name
421 * @param string $table Table name
422 * @param string $column Column name
424 * @return boolean State of the query execution
426 function PMA_clearTransformations($db, $table = '', $column = '')
428 $cfgRelation = PMA_getRelationsParam();
430 if (! isset($cfgRelation['column_info'])) {
434 $delete_sql = 'DELETE FROM '
435 . PMA\libraries\Util
::backquote($cfgRelation['db']) . '.'
436 . PMA\libraries\Util
::backquote($cfgRelation['column_info'])
439 if (($column != '') && ($table != '')) {
441 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
442 . '`table_name` = \'' . $table . '\' AND '
443 . '`column_name` = \'' . $column . '\' ';
445 } else if ($table != '') {
447 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
448 . '`table_name` = \'' . $table . '\' ';
451 $delete_sql .= '`db_name` = \'' . $db . '\' ';
454 return $GLOBALS['dbi']->tryQuery($delete_sql);