1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / transformations.lib.php
blob3983b7589ce34bdfa62575a59b41f91c412f5780
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 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.
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 (! /*overload*/mb_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 (/*overload*/mb_strlen($trimmed) > 1
53 && $trimmed[0] == "'"
54 && $trimmed[/*overload*/mb_strlen($trimmed) - 1] == "'"
55 ) {
56 // '...'
57 $option = /*overload*/mb_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[/*overload*/mb_strlen($rtrimmed) - 1] == "'") {
66 // ,...'
67 break;
70 $option = /*overload*/mb_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 $sub_dirs = array(
95 'input/' => 'input_',
96 'output/' => '',
97 '' => ''
99 foreach ($sub_dirs as $sd => $prefix) {
100 $handle = opendir('./libraries/plugins/transformations/' . $sd);
102 if (! $handle) {
103 $stack[$prefix . 'transformation'] = array();
104 $stack[$prefix . 'transformation_file'] = array();
105 continue;
108 $filestack = array();
109 while ($file = readdir($handle)) {
110 $filestack[] = $file;
113 closedir($handle);
114 sort($filestack);
116 foreach ($filestack as $file) {
117 if (preg_match('|^[^.].*_.*_.*\.class\.php$|', $file)) {
118 // File contains transformation functions.
119 $parts = explode('_', str_replace('.class.php', '', $file));
120 $mimetype = $parts[0] . "/" . $parts[1];
121 $stack['mimetype'][$mimetype] = $mimetype;
123 $stack[$prefix . 'transformation'][] = $mimetype . ': ' . $parts[2];
124 $stack[$prefix . 'transformation_file'][] = $sd . $file;
125 if ($sd === '') {
126 $stack['input_transformation'][] = $mimetype . ': ' . $parts[2];
127 $stack['input_transformation_file'][] = $sd . $file;
130 } elseif (preg_match('|^[^.].*\.class.php$|', $file)) {
131 // File is a plain mimetype, no functions.
132 $base = str_replace('.class.php', '', $file);
134 if ($base != 'global') {
135 $mimetype = str_replace('_', '/', $base);
136 $stack['mimetype'][$mimetype] = $mimetype;
137 $stack['empty_mimetype'][$mimetype] = $mimetype;
142 return $stack;
146 * Returns the class name of the transformation
148 * @param string $filename transformation file name
150 * @return string the class name of transformation
152 function PMA_getTransformationClassName($filename)
154 // get the transformation class name
155 $class_name = explode(".class.php", $filename);
156 $class_name = explode("/", $class_name[0]);
157 $class_name = count($class_name) === 1 ? $class_name[0] : $class_name[1];
159 return $class_name;
163 * Returns the description of the transformation
165 * @param string $file transformation file
167 * @return String the description of the transformation
169 function PMA_getTransformationDescription($file)
171 /* @var $class_name TransformationsInterface */
172 $class_name = PMA_getTransformationClassName($file);
173 // include and instantiate the class
174 include_once 'libraries/plugins/transformations/' . $file;
175 return $class_name::getInfo();
179 * Returns the name of the transformation
181 * @param string $file transformation file
183 * @return String the name of the transformation
185 function PMA_getTransformationName($file)
187 /* @var $class_name TransformationsInterface */
188 $class_name = PMA_getTransformationClassName($file);
189 // include and instantiate the class
190 include_once 'libraries/plugins/transformations/' . $file;
191 return $class_name::getName();
195 * Gets the mimetypes for all columns of a table
197 * @param string $db the name of the db to check for
198 * @param string $table the name of the table to check for
199 * @param boolean $strict whether to include only results having a mimetype set
200 * @param boolean $fullName whether to use full column names as the key
202 * @access public
204 * @return array [field_name][field_key] = field_value
206 function PMA_getMIME($db, $table, $strict = false, $fullName = false)
208 $cfgRelation = PMA_getRelationsParam();
210 if (! $cfgRelation['commwork']) {
211 return false;
214 $com_qry = '';
215 if ($fullName) {
216 $com_qry .= "SELECT CONCAT("
217 . "`db_name`, '.', `table_name`, '.', `column_name`"
218 . ") AS column_name, ";
219 } else {
220 $com_qry = "SELECT `column_name`, ";
222 $com_qry .= '`mimetype`,
223 `transformation`,
224 `transformation_options`,
225 `input_transformation`,
226 `input_transformation_options`
227 FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
228 . PMA_Util::backquote($cfgRelation['column_info']) . '
229 WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
230 AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
231 AND ( `mimetype` != \'\'' . (!$strict ? '
232 OR `transformation` != \'\'
233 OR `transformation_options` != \'\'
234 OR `input_transformation` != \'\'
235 OR `input_transformation_options` != \'\'' : '') . ')';
236 $result = $GLOBALS['dbi']->fetchResult(
237 $com_qry, 'column_name', null, $GLOBALS['controllink']
240 foreach ($result as $column => $values) {
241 // replacements in mimetype and transformation
242 $values = str_replace("jpeg", "JPEG", $values);
243 $values = str_replace("png", "PNG", $values);
245 // convert mimetype to new format (f.e. Text_Plain, etc)
246 $delimiter_space = '- ';
247 $delimiter = "_";
248 $values['mimetype'] = str_replace(
249 $delimiter_space,
250 $delimiter,
251 ucwords(
252 str_replace(
253 $delimiter,
254 $delimiter_space,
255 $values['mimetype']
260 // For transformation of form
261 // output/image_jpeg__inline.inc.php
262 // extract dir part.
263 $dir = explode('/', $values['transformation']);
264 $subdir = '';
265 if (count($dir) === 2) {
266 $subdir = $dir[0] . '/';
267 $values['transformation'] = $dir[1];
270 $values['transformation'] = str_replace(
271 $delimiter_space,
272 $delimiter,
273 ucwords(
274 str_replace(
275 $delimiter,
276 $delimiter_space,
277 $values['transformation']
281 $values['transformation'] = $subdir . $values['transformation'];
282 $result[$column] = $values;
285 return $result;
286 } // end of the 'PMA_getMIME()' function
289 * Set a single mimetype to a certain value.
291 * @param string $db the name of the db
292 * @param string $table the name of the table
293 * @param string $key the name of the column
294 * @param string $mimetype the mimetype of the column
295 * @param string $transformation the transformation of the column
296 * @param string $transformationOpts the transformation options of the column
297 * @param string $inputTransform the input transformation of the column
298 * @param string $inputTransformOpts the input transformation options of the column
299 * @param boolean $forcedelete force delete, will erase any existing
300 * comments for this column
302 * @access public
304 * @return boolean true, if comment-query was made.
306 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
307 $transformationOpts, $inputTransform, $inputTransformOpts, $forcedelete = false
309 $cfgRelation = PMA_getRelationsParam();
311 if (! $cfgRelation['commwork']) {
312 return false;
315 // lowercase mimetype & transformation
316 $mimetype = /*overload*/mb_strtolower($mimetype);
317 $transformation = /*overload*/mb_strtolower($transformation);
319 $test_qry = '
320 SELECT `mimetype`,
321 `comment`
322 FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
323 . PMA_Util::backquote($cfgRelation['column_info']) . '
324 WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
325 AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
326 AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
328 $test_rs = PMA_queryAsControlUser(
329 $test_qry, true, PMA_DatabaseInterface::QUERY_STORE
332 if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
333 $row = @$GLOBALS['dbi']->fetchAssoc($test_rs);
334 $GLOBALS['dbi']->freeResult($test_rs);
336 $transformationLength = /*overload*/mb_strlen($transformation);
337 if (! $forcedelete
338 && (/*overload*/mb_strlen($mimetype) || $transformationLength
339 || /*overload*/mb_strlen($transformationOpts)
340 || /*overload*/mb_strlen($row['comment']))
342 $upd_query = 'UPDATE ' . PMA_Util::backquote($cfgRelation['db']) . '.'
343 . PMA_Util::backquote($cfgRelation['column_info'])
344 . ' SET '
345 . '`mimetype` = \''
346 . PMA_Util::sqlAddSlashes($mimetype) . '\', '
347 . '`transformation` = \''
348 . PMA_Util::sqlAddSlashes($transformation) . '\', '
349 . '`transformation_options` = \''
350 . PMA_Util::sqlAddSlashes($transformationOpts) . '\', '
351 . '`input_transformation` = \''
352 . PMA_Util::sqlAddSlashes($inputTransform) . '\', '
353 . '`input_transformation_options` = \''
354 . PMA_Util::sqlAddSlashes($inputTransformOpts) . '\'';
355 } else {
356 $upd_query = 'DELETE FROM ' . PMA_Util::backquote($cfgRelation['db'])
357 . '.' . PMA_Util::backquote($cfgRelation['column_info']);
359 $upd_query .= '
360 WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
361 AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
362 AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
363 } elseif (/*overload*/mb_strlen($mimetype)
364 || /*overload*/mb_strlen($transformation)
365 || /*overload*/mb_strlen($transformationOpts)
368 $upd_query = 'INSERT INTO ' . PMA_Util::backquote($cfgRelation['db'])
369 . '.' . PMA_Util::backquote($cfgRelation['column_info'])
370 . ' (db_name, table_name, column_name, mimetype, '
371 . 'transformation, transformation_options, '
372 . 'input_transformation, input_transformation_options) '
373 . ' VALUES('
374 . '\'' . PMA_Util::sqlAddSlashes($db) . '\','
375 . '\'' . PMA_Util::sqlAddSlashes($table) . '\','
376 . '\'' . PMA_Util::sqlAddSlashes($key) . '\','
377 . '\'' . PMA_Util::sqlAddSlashes($mimetype) . '\','
378 . '\'' . PMA_Util::sqlAddSlashes($transformation) . '\','
379 . '\'' . PMA_Util::sqlAddSlashes($transformationOpts) . '\','
380 . '\'' . PMA_Util::sqlAddSlashes($inputTransform) . '\','
381 . '\'' . PMA_Util::sqlAddSlashes($inputTransformOpts) . '\')';
384 if (isset($upd_query)) {
385 return PMA_queryAsControlUser($upd_query);
386 } else {
387 return false;
389 } // end of 'PMA_setMIME()' function
393 * GLOBAL Plugin functions
398 * Replaces "[__BUFFER__]" occurrences found in $options['string'] with the text
399 * in $buffer, after performing a regular expression search and replace on
400 * $buffer using $options['regex'] and $options['regex_replace'].
402 * @param string $buffer text that will be replaced in $options['string'],
403 * after being formatted
404 * @param array $options the options required to format $buffer
405 * = array (
406 * 'string' => 'string', // text containing "[__BUFFER__]"
407 * 'regex' => 'mixed', // the pattern to search for
408 * 'regex_replace' => 'mixed', // string or array of strings to replace
409 * // with
410 * );
412 * @return string containing the text with all the replacements
414 function PMA_Transformation_globalHtmlReplace($buffer, $options = array())
416 if (! isset($options['string'])) {
417 $options['string'] = '';
420 if (isset($options['regex']) && isset($options['regex_replace'])) {
421 $buffer = preg_replace(
422 '@' . str_replace('@', '\@', $options['regex']) . '@si',
423 $options['regex_replace'],
424 $buffer
428 // Replace occurrences of [__BUFFER__] with actual text
429 $return = str_replace("[__BUFFER__]", $buffer, $options['string']);
430 return $return;
435 * Delete related transformation details
436 * after deleting database. table or column
438 * @param string $db Database name
439 * @param string $table Table name
440 * @param string $column Column name
442 * @return boolean State of the query execution
444 function PMA_clearTransformations($db, $table = '', $column = '')
446 $cfgRelation = PMA_getRelationsParam();
448 if (! isset($cfgRelation['column_info'])) {
449 return false;
452 $delete_sql = 'DELETE FROM '
453 . PMA_Util::backquote($cfgRelation['db']) . '.'
454 . PMA_Util::backquote($cfgRelation['column_info'])
455 . ' WHERE ';
457 if (($column != '') && ($table != '')) {
459 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
460 . '`table_name` = \'' . $table . '\' AND '
461 . '`column_name` = \'' . $column . '\' ';
463 } else if ($table != '') {
465 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
466 . '`table_name` = \'' . $table . '\' ';
468 } else {
469 $delete_sql .= '`db_name` = \'' . $db . '\' ';
472 return $GLOBALS['dbi']->tryQuery($delete_sql);