Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / transformations.lib.php
blob39a0ff09d6e0a0365250ea9dd4e185451199beac
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 = $GLOBALS['dbi']->fetchResult(
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(
284 $test_qry, true, PMA_DatabaseInterface::QUERY_STORE
287 if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
288 $row = @$GLOBALS['dbi']->fetchAssoc($test_rs);
289 $GLOBALS['dbi']->freeResult($test_rs);
291 if (! $forcedelete
292 && (strlen($mimetype) || strlen($transformation)
293 || strlen($transformation_options) || strlen($row['comment']))
295 $upd_query = '
296 UPDATE ' . PMA_Util::backquote($cfgRelation['db']) . '.'
297 . PMA_Util::backquote($cfgRelation['column_info']) . '
298 SET `mimetype` = \'' . PMA_Util::sqlAddSlashes($mimetype) . '\',
299 `transformation` = \'' . PMA_Util::sqlAddSlashes($transformation) . '\',
300 `transformation_options` = \'' . PMA_Util::sqlAddSlashes($transformation_options) . '\'';
301 } else {
302 $upd_query = 'DELETE FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['column_info']);
304 $upd_query .= '
305 WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
306 AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
307 AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
308 } elseif (strlen($mimetype) || strlen($transformation)
309 || strlen($transformation_options)) {
311 $upd_query = 'INSERT INTO ' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['column_info'])
312 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
313 . ' VALUES('
314 . '\'' . PMA_Util::sqlAddSlashes($db) . '\','
315 . '\'' . PMA_Util::sqlAddSlashes($table) . '\','
316 . '\'' . PMA_Util::sqlAddSlashes($key) . '\','
317 . '\'' . PMA_Util::sqlAddSlashes($mimetype) . '\','
318 . '\'' . PMA_Util::sqlAddSlashes($transformation) . '\','
319 . '\'' . PMA_Util::sqlAddSlashes($transformation_options) . '\')';
322 if (isset($upd_query)) {
323 return PMA_queryAsControlUser($upd_query);
324 } else {
325 return false;
327 } // end of 'PMA_setMIME()' function
331 * GLOBAL Plugin functions
336 * Replaces "[__BUFFER__]" occurences found in $options['string'] with the text
337 * in $buffer, after performing a regular expression search and replace on
338 * $buffer using $options['regex'] and $options['regex_replace'].
340 * @param string $buffer text that will be replaced in $options['string'],
341 * after being formatted
342 * @param array $options the options required to format $buffer
343 * = array (
344 * 'string' => 'string', // text containing "[__BUFFER__]"
345 * 'regex' => 'mixed', // the pattern to search for
346 * 'regex_replace' => 'mixed', // string or array of strings to replace
347 * // with
348 * );
350 * @return string containing the text with all the replacements
352 function PMA_transformation_global_html_replace($buffer, $options = array())
354 if ( ! isset($options['string']) ) {
355 $options['string'] = '';
358 if (isset($options['regex']) && isset($options['regex_replace'])) {
359 $buffer = preg_replace(
360 '@' . str_replace('@', '\@', $options['regex']) . '@si',
361 $options['regex_replace'],
362 $buffer
366 // Replace occurences of [__BUFFER__] with actual text
367 $return = str_replace("[__BUFFER__]", $buffer, $options['string']);
368 return $return;
373 * Delete related transformation details
374 * after deleting database. table or column
376 * @param string $db Database name
377 * @param string $table Table name
378 * @param string $column Column name
380 * @return boolean State of the query execution
382 function PMA_clearTransformations($db, $table = '', $column = '')
384 $cfgRelation = PMA_getRelationsParam();
386 if (! isset($cfgRelation['column_info'])) {
387 return false;
390 $delete_sql = 'DELETE FROM '
391 . PMA_Util::backquote($cfgRelation['db']) . '.'
392 . PMA_Util::backquote($cfgRelation['column_info'])
393 . ' WHERE ';
395 if (($column != '') && ($table != '')) {
397 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
398 . '`table_name` = \'' . $table . '\' AND '
399 . '`column_name` = \'' . $column . '\' ';
401 } else if ($table != '') {
403 $delete_sql .= '`db_name` = \'' . $db . '\' AND '
404 . '`table_name` = \'' . $table . '\' ';
406 } else {
407 $delete_sql .= '`db_name` = \'' . $db . '\' ';
410 return $GLOBALS['dbi']->tryQuery($delete_sql);