Fix for the Open in New Window in Patient/Client->Patients search gui, take 2.
[openemr.git] / phpmyadmin / libraries / transformations.lib.php
blob86a0f3a14a87c59b70742996f110bcecfcd4ca0a
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 * @version $Id$
7 */
9 /**
10 * returns array of options from string with options separated by comma, removes quotes
12 * <code>
13 * PMA_transformation_getOptions("'option ,, quoted',abd,'2,3',");
14 * // array {
15 * // 'option ,, quoted',
16 * // 'abc',
17 * // '2,3',
18 * // '',
19 * // }
20 * </code>
22 * @uses preg_split()
23 * @uses array_shift()
24 * @uses trim()
25 * @uses rtrim()
26 * @uses ltrim()
27 * @uses strlen()
28 * @uses substr()
29 * @uses stripslashes()
30 * @param string $option_string comma separated options
31 * @return array options
33 function PMA_transformation_getOptions($option_string)
35 $result = array();
37 if (! strlen($option_string)
38 || ! $transform_options = preg_split('/,/', $option_string)) {
39 return $result;
42 while (($option = array_shift($transform_options)) !== null) {
43 $trimmed = trim($option);
44 if (strlen($trimmed) > 1
45 && $trimmed[0] == "'"
46 && $trimmed[strlen($trimmed) - 1] == "'") {
47 // '...'
48 $option = substr($trimmed, 1, -1);
49 } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
50 // '...,
51 $trimmed = ltrim($option);
52 while (($option = array_shift($transform_options)) !== null) {
53 // ...,
54 $trimmed .= ',' . $option;
55 $rtrimmed = rtrim($trimmed);
56 if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
57 // ,...'
58 break;
61 $option = substr($rtrimmed, 1, -1);
63 $result[] = stripslashes($option);
66 return $result;
69 /**
70 * Gets all available MIME-types
72 * @access public
73 * @author Garvin Hicking <me@supergarv.de>
74 * @uses opendir()
75 * @uses readdir()
76 * @uses closedir()
77 * @uses sort()
78 * @uses preg_match()
79 * @uses explode()
80 * @uses str_replace()
81 * @staticvar array mimetypes
82 * @return array array[mimetype], array[transformation]
84 function PMA_getAvailableMIMEtypes()
86 static $stack = null;
88 if (null !== $stack) {
89 return $stack;
92 $stack = array();
93 $filestack = array();
95 $handle = opendir('./libraries/transformations');
97 if (! $handle) {
98 return $stack;
101 while ($file = readdir($handle)) {
102 $filestack[] = $file;
105 closedir($handle);
106 sort($filestack);
108 foreach ($filestack as $file) {
109 if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
110 // File contains transformation functions.
111 $base = explode('__', str_replace('.inc.php', '', $file));
112 $mimetype = str_replace('_', '/', $base[0]);
113 $stack['mimetype'][$mimetype] = $mimetype;
115 $stack['transformation'][] = $mimetype . ': ' . $base[1];
116 $stack['transformation_file'][] = $file;
118 } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
119 // File is a plain mimetype, no functions.
120 $base = str_replace('.inc.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 * Gets the mimetypes for all rows of a table
136 * @uses $GLOBALS['controllink']
137 * @uses PMA_getRelationsParam()
138 * @uses PMA_backquote()
139 * @uses PMA_sqlAddslashes()
140 * @uses PMA_DBI_fetch_result()
141 * @author Mike Beck <mikebeck@users.sourceforge.net>
142 * @author Garvin Hicking <me@supergarv.de>
143 * @access public
144 * @param string $db the name of the db to check for
145 * @param string $table the name of the table to check for
146 * @param string $strict whether to include only results having a mimetype set
147 * @return array [field_name][field_key] = field_value
149 function PMA_getMIME($db, $table, $strict = false)
151 $cfgRelation = PMA_getRelationsParam();
153 if (! $cfgRelation['commwork']) {
154 return false;
157 $com_qry = '
158 SELECT `column_name`,
159 `mimetype`,
160 `transformation`,
161 `transformation_options`
162 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
163 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
164 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
165 AND ( `mimetype` != \'\'' . (!$strict ? '
166 OR `transformation` != \'\'
167 OR `transformation_options` != \'\'' : '') . ')';
168 return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
169 } // end of the 'PMA_getMIME()' function
172 * Set a single mimetype to a certain value.
174 * @uses PMA_DBI_QUERY_STORE
175 * @uses PMA_getRelationsParam()
176 * @uses PMA_backquote()
177 * @uses PMA_sqlAddslashes()
178 * @uses PMA_query_as_cu()
179 * @uses PMA_DBI_num_rows()
180 * @uses PMA_DBI_fetch_assoc()
181 * @uses PMA_DBI_free_result()
182 * @uses strlen()
183 * @access public
184 * @param string $db the name of the db
185 * @param string $table the name of the table
186 * @param string $key the name of the column
187 * @param string $mimetype the mimetype of the column
188 * @param string $transformation the transformation of the column
189 * @param string $transformation_options the transformation options of the column
190 * @param string $forcedelete force delete, will erase any existing comments for this column
191 * @return boolean true, if comment-query was made.
193 function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
194 $transformation_options, $forcedelete = false)
196 $cfgRelation = PMA_getRelationsParam();
198 if (! $cfgRelation['commwork']) {
199 return false;
202 $test_qry = '
203 SELECT `mimetype`,
204 `comment`
205 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
206 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
207 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
208 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
209 $test_rs = PMA_query_as_cu($test_qry, true, PMA_DBI_QUERY_STORE);
211 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
212 $row = @PMA_DBI_fetch_assoc($test_rs);
213 PMA_DBI_free_result($test_rs);
215 if (! $forcedelete
216 && (strlen($mimetype) || strlen($transformation)
217 || strlen($transformation_options) || strlen($row['comment']))) {
218 $upd_query = '
219 UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
220 SET `mimetype` = \'' . PMA_sqlAddslashes($mimetype) . '\',
221 `transformation` = \'' . PMA_sqlAddslashes($transformation) . '\',
222 `transformation_options` = \'' . PMA_sqlAddslashes($transformation_options) . '\'';
223 } else {
224 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
226 $upd_query .= '
227 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
228 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
229 AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
230 } elseif (strlen($mimetype) || strlen($transformation)
231 || strlen($transformation_options)) {
232 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
233 . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
234 . ' VALUES('
235 . '\'' . PMA_sqlAddslashes($db) . '\','
236 . '\'' . PMA_sqlAddslashes($table) . '\','
237 . '\'' . PMA_sqlAddslashes($key) . '\','
238 . '\'' . PMA_sqlAddslashes($mimetype) . '\','
239 . '\'' . PMA_sqlAddslashes($transformation) . '\','
240 . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
243 if (isset($upd_query)){
244 return PMA_query_as_cu($upd_query);
245 } else {
246 return false;
248 } // end of 'PMA_setMIME()' function
251 * Returns the real filename of a configured transformation
253 * in fact: it just replaces old php3 with php extension
255 * garvin: for security, never allow to break out from transformations directory
257 * @uses PMA_securePath()
258 * @uses preg_replace()
259 * @uses strlen()
260 * @uses file_exists()
261 * @access public
262 * @param string $filename the current filename
263 * @return string the new filename
265 function PMA_sanitizeTransformationFile(&$filename)
267 $include_file = PMA_securePath($filename);
269 // This value can also contain a 'php3' value, in which case we map this filename to our new 'php' variant
270 $testfile = preg_replace('@\.inc\.php3$@', '.inc.php', $include_file);
271 if ($include_file{strlen($include_file)-1} == '3'
272 && file_exists('./libraries/transformations/' . $testfile)) {
273 $include_file = $testfile;
274 $filename = $testfile; // Corrects the referenced variable for further actions on the filename;
277 return $include_file;
278 } // end of 'PMA_sanitizeTransformationFile()' function