Translated using Weblate (Polish)
[phpmyadmin.git] / libraries / transformations.lib.php
blobb37b7406e7894931e262b68936f27f74c4af5cb6
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 * 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.
16 * @package PhpMyAdmin
18 if (! defined('PHPMYADMIN')) {
19 exit;
22 /**
23 * Returns array of options from string with options separated by comma,
24 * removes quotes
26 * <code>
27 * PMA_Transformation_getOptions("'option ,, quoted',abd,'2,3',");
28 * // array {
29 * // 'option ,, quoted',
30 * // 'abc',
31 * // '2,3',
32 * // '',
33 * // }
34 * </code>
36 * @param string $option_string comma separated options
38 * @return array options
40 function PMA_Transformation_getOptions($option_string)
42 $result = array();
44 if (! strlen($option_string)
45 || ! $transform_options = preg_split('/,/', $option_string)
46 ) {
47 return $result;
50 while (($option = array_shift($transform_options)) !== null) {
51 $trimmed = trim($option);
52 if (strlen($trimmed) > 1
53 && $trimmed[0] == "'"
54 && $trimmed[strlen($trimmed) - 1] == "'"
55 ) {
56 // '...'
57 $option = substr($trimmed, 1, -1);
58 } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
59 // '...,
60 $trimmed = ltrim($option);
61 while (($option = array_shift($transform_options)) !== null) {
62 // ...,
63 $trimmed .= ',' . $option;
64 $rtrimmed = rtrim($trimmed);
65 if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
66 // ,...'
67 break;
70 $option = substr($rtrimmed, 1, -1);
72 $result[] = stripslashes($option);
75 return $result;
78 /**
79 * Gets all available MIME-types
81 * @access public
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/plugins/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('|^.*_.*_.*\.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;
130 return $stack;
134 * Returns the description of the transformation
136 * @param string $file transformation file
137 * @param string $html_formatted whether the description should be formatted
138 * as HTML
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
160 * @access public
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']) {
169 return false;
172 $com_qry = '
173 SELECT `column_name`,
174 `mimetype`,
175 `transformation`,
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 = '- ';
196 $delimiter = "_";
197 $values['mimetype'] = str_replace(
198 $delimiter_space,
199 $delimiter,
200 ucwords(
201 str_replace(
202 $delimiter,
203 $delimiter_space,
204 $values['mimetype']
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(
215 $delimiter_space,
216 $delimiter,
217 ucwords(
218 str_replace(
219 $delimiter,
220 $delimiter_space,
221 $values['transformation']
226 $result[$column] = $values;
229 return $result;
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
244 * @access public
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']) {
254 return false;
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);
271 $test_qry = '
272 SELECT `mimetype`,
273 `comment`
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);
288 if (! $forcedelete
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'])
294 . ' SET '
295 . '`mimetype` = \''
296 . PMA_Util::sqlAddSlashes($mimetype) . '\', '
297 . '`transformation` = \''
298 . PMA_Util::sqlAddSlashes($transformation) . '\', '
299 . '`transformation_options` = \''
300 . PMA_Util::sqlAddSlashes($transformation_options) . '\'';
301 } else {
302 $upd_query = 'DELETE FROM ' . PMA_Util::backquote($cfgRelation['db'])
303 . '.' . PMA_Util::backquote($cfgRelation['column_info']);
305 $upd_query .= '
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) '
318 . ' VALUES('
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);
329 } else {
330 return false;
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
348 * = array (
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
352 * // with
353 * );
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'],
367 $buffer
371 // Replace occurences of [__BUFFER__] with actual text
372 $return = str_replace("[__BUFFER__]", $buffer, $options['string']);
373 return $return;
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'])) {
392 return false;
395 $delete_sql = 'DELETE FROM '
396 . PMA_Util::backquote($cfgRelation['db']) . '.'
397 . PMA_Util::backquote($cfgRelation['column_info'])
398 . ' WHERE ';
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 . '\' ';
411 } else {
412 $delete_sql .= '`db_name` = \'' . $db . '\' ';
415 return $GLOBALS['dbi']->tryQuery($delete_sql);