Support displaying in UTC (RFE #1440386).
[phpmyadmin/last10db.git] / libraries / transformations.lib.php
blob6b3f3af532ffc12b9121e89e5691da2488127917
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Set of functions used with the relation and pdf feature
7 */
9 function PMA_transformation_getOptions($string) {
10 $transform_options = array();
12 if ($string != '') {
13 if ($string{0} == "'" && $string{strlen($string)-1} == "'") {
14 $transform_options = explode('\',\'', substr($string, 1, strlen($string)-2));
15 } else {
16 $transform_options = array(0 => $string);
20 // strip possible slashes to behave like documentation says
21 $result = array();
22 foreach ($transform_options as $val) {
23 $result[] = stripslashes($val);
25 return $result;
28 /**
29 * Gets all available MIME-types
31 * @return array array[mimetype], array[transformation]
33 * @access public
35 * @author Garvin Hicking <me@supergarv.de>
37 function PMA_getAvailableMIMEtypes() {
38 $handle = opendir('./libraries/transformations');
40 $stack = array();
41 $filestack = array();
43 while (($file = readdir($handle)) != false) {
44 $filestack[$file] = $file;
47 closedir($handle);
49 if (is_array($filestack)) {
50 @ksort($filestack);
51 foreach ($filestack AS $key => $file) {
53 if (preg_match('|^.*__.*\.inc\.php$|', trim($file))) {
54 // File contains transformation functions.
55 $base = explode('__', str_replace('.inc.php', '', $file));
56 $mimetype = str_replace('_', '/', $base[0]);
57 $stack['mimetype'][$mimetype] = $mimetype;
59 $stack['transformation'][] = $mimetype . ': ' . $base[1];
60 $stack['transformation_file'][] = $file;
62 } elseif (preg_match('|^.*\.inc\.php$|', trim($file))) {
63 // File is a plain mimetype, no functions.
64 $base = str_replace('.inc.php', '', $file);
66 if ($base != 'global') {
67 $mimetype = str_replace('_', '/', $base);
68 $stack['mimetype'][$mimetype] = $mimetype;
69 $stack['empty_mimetype'][$mimetype] = $mimetype;
76 return $stack;
79 /**
80 * Gets the mimetypes for all rows of a table
82 * @param string the name of the db to check for
83 * @param string the name of the table to check for
84 * @param string whether to include only results having a mimetype set
86 * @return array [field_name][field_key] = field_value
88 * @global array the list of relations settings
90 * @access public
92 * @author Mike Beck <mikebeck@users.sourceforge.net> / Garvin Hicking <me@supergarv.de>
94 function PMA_getMIME($db, $table, $strict = false) {
95 global $cfgRelation;
97 $com_qry = 'SELECT column_name, mimetype, transformation, transformation_options FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
98 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
99 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
100 . ' AND (mimetype != \'\'' . (!$strict ? ' OR transformation != \'\' OR transformation_options != \'\'' : '') . ')';
101 $com_rs = PMA_query_as_cu($com_qry);
103 while ($row = @PMA_DBI_fetch_assoc($com_rs)) {
104 $col = $row['column_name'];
105 $mime[$col]['mimetype'] = $row['mimetype'];
106 $mime[$col]['transformation'] = $row['transformation'];
107 $mime[$col]['transformation_options'] = $row['transformation_options'];
108 } // end while
109 PMA_DBI_free_result($com_rs);
110 unset($com_rs);
112 if (isset($mime) && is_array($mime)) {
113 return $mime;
114 } else {
115 return FALSE;
117 } // end of the 'PMA_getMIME()' function
120 * Set a single mimetype to a certain value.
122 * @param string the name of the db
123 * @param string the name of the table
124 * @param string the name of the column
125 * @param string the mimetype of the column
126 * @param string the transformation of the column
127 * @param string the transformation options of the column
128 * @param string (optional) force delete, will erase any existing comments for this column
130 * @return boolean true, if comment-query was made.
132 * @global array the list of relations settings
134 * @access public
136 function PMA_setMIME($db, $table, $key, $mimetype, $transformation, $transformation_options, $forcedelete = false) {
137 global $cfgRelation;
139 $test_qry = 'SELECT mimetype, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
140 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
141 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
142 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
143 $test_rs = PMA_query_as_cu($test_qry, TRUE, PMA_DBI_QUERY_STORE);
145 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
146 $row = @PMA_DBI_fetch_assoc($test_rs);
147 PMA_DBI_free_result($test_rs);
148 unset($test_rs);
150 if (!$forcedelete && (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0 || strlen($row['comment']) > 0)) {
151 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
152 . ' SET mimetype = \'' . PMA_sqlAddslashes($mimetype) . '\','
153 . ' transformation = \'' . PMA_sqlAddslashes($transformation) . '\','
154 . ' transformation_options = \'' . PMA_sqlAddslashes($transformation_options) . '\''
155 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
156 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
157 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
158 } else {
159 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
160 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
161 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
162 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
164 } elseif (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0) {
165 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
166 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
167 . ' VALUES('
168 . '\'' . PMA_sqlAddslashes($db) . '\','
169 . '\'' . PMA_sqlAddslashes($table) . '\','
170 . '\'' . PMA_sqlAddslashes($key) . '\','
171 . '\'' . PMA_sqlAddslashes($mimetype) . '\','
172 . '\'' . PMA_sqlAddslashes($transformation) . '\','
173 . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
176 if (isset($upd_query)){
177 $upd_rs = PMA_query_as_cu($upd_query);
178 PMA_DBI_free_result($upd_rs);
179 unset($upd_rs);
180 return true;
181 } else {
182 return false;
184 } // end of 'PMA_setMIME()' function
187 * Returns the real filename of a configured transformation
189 * @param string the current filename
191 * @return string the new filename
193 * @access public
195 function PMA_sanitizeTransformationFile(&$filename) {
196 // garvin: for security, never allow to break out from transformations directory
198 $include_file = PMA_securePath($filename);
200 // This value can also contain a 'php3' value, in which case we map this filename to our new 'php' variant
201 $testfile = preg_replace('@\.inc\.php3$@', '.inc.php', $include_file);
202 if ($include_file{strlen($include_file)-1} == '3' && file_exists('./libraries/transformations/' . $testfile)) {
203 $include_file = $testfile;
204 $filename = $testfile; // Corrects the referenced variable for further actions on the filename;
207 return $include_file;
208 } // end of 'PMA_sanitizeTransformationFile()' function