PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/transformations.lib.php

https://github.com/md-tech/openemr
PHP | 279 lines | 148 code | 28 blank | 103 comment | 34 complexity | 5483bc7f4ff5679f4ca9baa967d8e9b3 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Set of functions used with the relation and pdf feature
  5. *
  6. * @version $Id$
  7. */
  8. /**
  9. * returns array of options from string with options separated by comma, removes quotes
  10. *
  11. * <code>
  12. * PMA_transformation_getOptions("'option ,, quoted',abd,'2,3',");
  13. * // array {
  14. * // 'option ,, quoted',
  15. * // 'abc',
  16. * // '2,3',
  17. * // '',
  18. * // }
  19. * </code>
  20. *
  21. * @uses preg_split()
  22. * @uses array_shift()
  23. * @uses trim()
  24. * @uses rtrim()
  25. * @uses ltrim()
  26. * @uses strlen()
  27. * @uses substr()
  28. * @uses stripslashes()
  29. * @param string $option_string comma separated options
  30. * @return array options
  31. */
  32. function PMA_transformation_getOptions($option_string)
  33. {
  34. $result = array();
  35. if (! strlen($option_string)
  36. || ! $transform_options = preg_split('/,/', $option_string)) {
  37. return $result;
  38. }
  39. while (($option = array_shift($transform_options)) !== null) {
  40. $trimmed = trim($option);
  41. if (strlen($trimmed) > 1
  42. && $trimmed[0] == "'"
  43. && $trimmed[strlen($trimmed) - 1] == "'") {
  44. // '...'
  45. $option = substr($trimmed, 1, -1);
  46. } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
  47. // '...,
  48. $trimmed = ltrim($option);
  49. while (($option = array_shift($transform_options)) !== null) {
  50. // ...,
  51. $trimmed .= ',' . $option;
  52. $rtrimmed = rtrim($trimmed);
  53. if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
  54. // ,...'
  55. break;
  56. }
  57. }
  58. $option = substr($rtrimmed, 1, -1);
  59. }
  60. $result[] = stripslashes($option);
  61. }
  62. return $result;
  63. }
  64. /**
  65. * Gets all available MIME-types
  66. *
  67. * @access public
  68. * @author Garvin Hicking <me@supergarv.de>
  69. * @uses opendir()
  70. * @uses readdir()
  71. * @uses closedir()
  72. * @uses sort()
  73. * @uses preg_match()
  74. * @uses explode()
  75. * @uses str_replace()
  76. * @staticvar array mimetypes
  77. * @return array array[mimetype], array[transformation]
  78. */
  79. function PMA_getAvailableMIMEtypes()
  80. {
  81. static $stack = null;
  82. if (null !== $stack) {
  83. return $stack;
  84. }
  85. $stack = array();
  86. $filestack = array();
  87. $handle = opendir('./libraries/transformations');
  88. if (! $handle) {
  89. return $stack;
  90. }
  91. while ($file = readdir($handle)) {
  92. $filestack[] = $file;
  93. }
  94. closedir($handle);
  95. sort($filestack);
  96. foreach ($filestack as $file) {
  97. if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
  98. // File contains transformation functions.
  99. $base = explode('__', str_replace('.inc.php', '', $file));
  100. $mimetype = str_replace('_', '/', $base[0]);
  101. $stack['mimetype'][$mimetype] = $mimetype;
  102. $stack['transformation'][] = $mimetype . ': ' . $base[1];
  103. $stack['transformation_file'][] = $file;
  104. } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
  105. // File is a plain mimetype, no functions.
  106. $base = str_replace('.inc.php', '', $file);
  107. if ($base != 'global') {
  108. $mimetype = str_replace('_', '/', $base);
  109. $stack['mimetype'][$mimetype] = $mimetype;
  110. $stack['empty_mimetype'][$mimetype] = $mimetype;
  111. }
  112. }
  113. }
  114. return $stack;
  115. }
  116. /**
  117. * Gets the mimetypes for all rows of a table
  118. *
  119. * @uses $GLOBALS['controllink']
  120. * @uses PMA_getRelationsParam()
  121. * @uses PMA_backquote()
  122. * @uses PMA_sqlAddslashes()
  123. * @uses PMA_DBI_fetch_result()
  124. * @author Mike Beck <mikebeck@users.sourceforge.net>
  125. * @author Garvin Hicking <me@supergarv.de>
  126. * @access public
  127. * @param string $db the name of the db to check for
  128. * @param string $table the name of the table to check for
  129. * @param string $strict whether to include only results having a mimetype set
  130. * @return array [field_name][field_key] = field_value
  131. */
  132. function PMA_getMIME($db, $table, $strict = false)
  133. {
  134. $cfgRelation = PMA_getRelationsParam();
  135. if (! $cfgRelation['commwork']) {
  136. return false;
  137. }
  138. $com_qry = '
  139. SELECT `column_name`,
  140. `mimetype`,
  141. `transformation`,
  142. `transformation_options`
  143. FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
  144. WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
  145. AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
  146. AND ( `mimetype` != \'\'' . (!$strict ? '
  147. OR `transformation` != \'\'
  148. OR `transformation_options` != \'\'' : '') . ')';
  149. return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
  150. } // end of the 'PMA_getMIME()' function
  151. /**
  152. * Set a single mimetype to a certain value.
  153. *
  154. * @uses PMA_DBI_QUERY_STORE
  155. * @uses PMA_getRelationsParam()
  156. * @uses PMA_backquote()
  157. * @uses PMA_sqlAddslashes()
  158. * @uses PMA_query_as_cu()
  159. * @uses PMA_DBI_num_rows()
  160. * @uses PMA_DBI_fetch_assoc()
  161. * @uses PMA_DBI_free_result()
  162. * @uses strlen()
  163. * @access public
  164. * @param string $db the name of the db
  165. * @param string $table the name of the table
  166. * @param string $key the name of the column
  167. * @param string $mimetype the mimetype of the column
  168. * @param string $transformation the transformation of the column
  169. * @param string $transformation_options the transformation options of the column
  170. * @param string $forcedelete force delete, will erase any existing comments for this column
  171. * @return boolean true, if comment-query was made.
  172. */
  173. function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
  174. $transformation_options, $forcedelete = false)
  175. {
  176. $cfgRelation = PMA_getRelationsParam();
  177. if (! $cfgRelation['commwork']) {
  178. return false;
  179. }
  180. $test_qry = '
  181. SELECT `mimetype`,
  182. `comment`
  183. FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
  184. WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
  185. AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
  186. AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
  187. $test_rs = PMA_query_as_cu($test_qry, true, PMA_DBI_QUERY_STORE);
  188. if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
  189. $row = @PMA_DBI_fetch_assoc($test_rs);
  190. PMA_DBI_free_result($test_rs);
  191. if (! $forcedelete
  192. && (strlen($mimetype) || strlen($transformation)
  193. || strlen($transformation_options) || strlen($row['comment']))) {
  194. $upd_query = '
  195. UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
  196. SET `mimetype` = \'' . PMA_sqlAddslashes($mimetype) . '\',
  197. `transformation` = \'' . PMA_sqlAddslashes($transformation) . '\',
  198. `transformation_options` = \'' . PMA_sqlAddslashes($transformation_options) . '\'';
  199. } else {
  200. $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
  201. }
  202. $upd_query .= '
  203. WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
  204. AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
  205. AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
  206. } elseif (strlen($mimetype) || strlen($transformation)
  207. || strlen($transformation_options)) {
  208. $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
  209. . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
  210. . ' VALUES('
  211. . '\'' . PMA_sqlAddslashes($db) . '\','
  212. . '\'' . PMA_sqlAddslashes($table) . '\','
  213. . '\'' . PMA_sqlAddslashes($key) . '\','
  214. . '\'' . PMA_sqlAddslashes($mimetype) . '\','
  215. . '\'' . PMA_sqlAddslashes($transformation) . '\','
  216. . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
  217. }
  218. if (isset($upd_query)){
  219. return PMA_query_as_cu($upd_query);
  220. } else {
  221. return false;
  222. }
  223. } // end of 'PMA_setMIME()' function
  224. /**
  225. * Returns the real filename of a configured transformation
  226. *
  227. * in fact: it just replaces old php3 with php extension
  228. *
  229. * garvin: for security, never allow to break out from transformations directory
  230. *
  231. * @uses PMA_securePath()
  232. * @uses preg_replace()
  233. * @uses strlen()
  234. * @uses file_exists()
  235. * @access public
  236. * @param string $filename the current filename
  237. * @return string the new filename
  238. */
  239. function PMA_sanitizeTransformationFile(&$filename)
  240. {
  241. $include_file = PMA_securePath($filename);
  242. // This value can also contain a 'php3' value, in which case we map this filename to our new 'php' variant
  243. $testfile = preg_replace('@\.inc\.php3$@', '.inc.php', $include_file);
  244. if ($include_file{strlen($include_file)-1} == '3'
  245. && file_exists('./libraries/transformations/' . $testfile)) {
  246. $include_file = $testfile;
  247. $filename = $testfile; // Corrects the referenced variable for further actions on the filename;
  248. }
  249. return $include_file;
  250. } // end of 'PMA_sanitizeTransformationFile()' function
  251. ?>