Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / transformations.lib.php
blobc653a9ae32d3adc28c7ab032a6e953dd470801ae
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 /* Temporary workaround for bug #3783 :
151 Calling a method from a variable class is not possible before PHP 5.3. */
152 $functionGetInfo = $class_name . "_getInfo";
153 return $functionGetInfo();
157 * Gets the mimetypes for all columns of a table
159 * @param string $db the name of the db to check for
160 * @param string $table the name of the table to check for
161 * @param string $strict whether to include only results having a mimetype set
163 * @access public
165 * @return array [field_name][field_key] = field_value
167 function PMA_getMIME($db, $table, $strict = false)
169 $cfgRelation = PMA_getRelationsParam();
171 if (! $cfgRelation['commwork']) {
172 return false;
175 $com_qry = '
176 SELECT `column_name`,
177 `mimetype`,
178 `transformation`,
179 `transformation_options`
180 FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
181 . PMA_Util::backquote($cfgRelation['column_info']) . '
182 WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
183 AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
184 AND ( `mimetype` != \'\'' . (!$strict ? '
185 OR `transformation` != \'\'
186 OR `transformation_options` != \'\'' : '') . ')';
187 $result = PMA_DBI_fetch_result(
188 $com_qry, 'column_name', null, $GLOBALS['controllink']
191 foreach ($result as $column => $values) {
192 // replacements in mimetype and transformation
193 $values = str_replace("jpeg", "JPEG", $values);
194 $values = str_replace("png", "PNG", $values);
195 $values = str_replace("octet-stream", "Octetstream", $values);
197 // convert mimetype to new format (f.e. Text_Plain, etc)
198 $delimiter_space = '- ';
199 $delimiter = "_";
200 $values['mimetype'] = str_replace(
201 $delimiter_space,
202 $delimiter,
203 ucwords(
204 str_replace(
205 $delimiter,
206 $delimiter_space,
207 $values['mimetype']
212 // convert transformation to new format (class name)
213 // f.e. Text_Plain_Substring.class.php
214 $values = str_replace("__", "_", $values);
215 $values = str_replace(".inc.php", ".class.php", $values);
217 $values['transformation'] = str_replace(
218 $delimiter_space,
219 $delimiter,
220 ucwords(
221 str_replace(
222 $delimiter,
223 $delimiter_space,
224 $values['transformation']
229 $result[$column] = $values;
232 return $result;
233 } // end of the 'PMA_getMIME()' function
236 * Set a single mimetype to a certain value.
238 * @param string $db the name of the db
239 * @param string $table the name of the table
240 * @param string $key the name of the column
241 * @param string $mimetype the mimetype of the column
242 * @param string $transformation the transformation of the column
243 * @param string $transformation_options the transformation options of the column
244 * @param string $forcedelete force delete, will erase any existing
245 * comments for this column
247 * @access public
249 * @return boolean true, if comment-query was made.
251 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
252 $transformation_options, $forcedelete = false
254 $cfgRelation = PMA_getRelationsParam();
256 if (! $cfgRelation['commwork']) {
257 return false;
260 // convert mimetype to old format (f.e. text_plain)
261 $mimetype = strtolower($mimetype);
262 // old format has octet-stream instead of octetstream for mimetype
263 if (strstr($mimetype, "octetstream")) {
264 $mimetype = "application_octet-stream";
267 // convert transformation to old format (f.e. text_plain__substring.inc.php)
268 $transformation = strtolower($transformation);
269 $transformation = str_replace(".class.php", ".inc.php", $transformation);
270 $last_pos = strrpos($transformation, "_");
271 $transformation = substr($transformation, 0, $last_pos) . "_"
272 . substr($transformation, $last_pos);
274 $test_qry = '
275 SELECT `mimetype`,
276 `comment`
277 FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
278 . PMA_Util::backquote($cfgRelation['column_info']) . '
279 WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
280 AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
281 AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
283 $test_rs = PMA_queryAsControlUser($test_qry, true, PMA_DBI_QUERY_STORE);
285 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
286 $row = @PMA_DBI_fetch_assoc($test_rs);
287 PMA_DBI_free_result($test_rs);
289 if (! $forcedelete
290 && (strlen($mimetype) || strlen($transformation)
291 || strlen($transformation_options) || strlen($row['comment']))
293 $upd_query = '
294 UPDATE ' . PMA_Util::backquote($cfgRelation['db']) . '.'
295 . PMA_Util::backquote($cfgRelation['column_info']) . '
296 SET `mimetype` = \'' . PMA_Util::sqlAddSlashes($mimetype) . '\',
297 `transformation` = \'' . PMA_Util::sqlAddSlashes($transformation) . '\',
298 `transformation_options` = \'' . PMA_Util::sqlAddSlashes($transformation_options) . '\'';
299 } else {
300 $upd_query = 'DELETE FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['column_info']);
302 $upd_query .= '
303 WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
304 AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
305 AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
306 } elseif (strlen($mimetype) || strlen($transformation)
307 || strlen($transformation_options)) {
309 $upd_query = 'INSERT INTO ' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['column_info'])
310 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
311 . ' VALUES('
312 . '\'' . PMA_Util::sqlAddSlashes($db) . '\','
313 . '\'' . PMA_Util::sqlAddSlashes($table) . '\','
314 . '\'' . PMA_Util::sqlAddSlashes($key) . '\','
315 . '\'' . PMA_Util::sqlAddSlashes($mimetype) . '\','
316 . '\'' . PMA_Util::sqlAddSlashes($transformation) . '\','
317 . '\'' . PMA_Util::sqlAddSlashes($transformation_options) . '\')';
320 if (isset($upd_query)) {
321 return PMA_queryAsControlUser($upd_query);
322 } else {
323 return false;
325 } // end of 'PMA_setMIME()' function
329 * GLOBAL Plugin functions
334 * Replaces "[__BUFFER__]" occurences found in $options['string'] with the text
335 * in $buffer, after performing a regular expression search and replace on
336 * $buffer using $options['regex'] and $options['regex_replace'].
338 * @param string $buffer text that will be replaced in $options['string'],
339 * after being formatted
340 * @param array $options the options required to format $buffer
341 * = array (
342 * 'string' => 'string', // text containing "[__BUFFER__]"
343 * 'regex' => 'mixed', // the pattern to search for
344 * 'regex_replace' => 'mixed', // string or array of strings to replace
345 * // with
346 * );
348 * @return string containing the text with all the replacements
350 function PMA_transformation_global_html_replace($buffer, $options = array())
352 if ( ! isset($options['string']) ) {
353 $options['string'] = '';
356 if (isset($options['regex']) && isset($options['regex_replace'])) {
357 $buffer = preg_replace(
358 '@' . str_replace('@', '\@', $options['regex']) . '@si',
359 $options['regex_replace'],
360 $buffer
364 // Replace occurences of [__BUFFER__] with actual text
365 $return = str_replace("[__BUFFER__]", $buffer, $options['string']);
366 return $return;
371 * Delete related transformation details
372 * after deleting database. table or column
374 * @param string $db Database name
375 * @param string $table Table name
376 * @param string $column Column name
378 * @return boolean State of the query execution
380 function PMA_clearTransformations($db, $table = '', $column = '')
382 $cfgRelation = PMA_getRelationsParam();
384 if (! isset($cfgRelation['column_info'])) {
385 return false;
388 $delete_sql = 'DELETE FROM '
389 . PMA_Util::backquote($cfgRelation['db']) . '.'
390 . PMA_Util::backquote($cfgRelation['column_info'])
391 . ' WHERE ';
393 if (($column != '') && ($table != '')) {
395 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
396 . '`table_name` = \'' . $table . '\' AND '
397 . '`column_name` = \'' . $column . '\' ';
399 } else if ($table != '') {
401 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
402 . '`table_name` = \'' . $table . '\' ';
404 } else {
405 $delete_sql .= '`db_name` = \'' . $db . '\' ';
408 return PMA_DBI_try_query($delete_sql);