/gui/tools/pma/libraries/transformations.lib.php

https://github.com/BenBE/ispCP · PHP · 246 lines · 137 code · 25 blank · 84 comment · 31 complexity · f8e8c01d98e16019313c28366da2289c 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. * @package phpMyAdmin
  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. * @uses opendir()
  69. * @uses readdir()
  70. * @uses closedir()
  71. * @uses sort()
  72. * @uses preg_match()
  73. * @uses explode()
  74. * @uses str_replace()
  75. * @staticvar array mimetypes
  76. * @return array array[mimetype], array[transformation]
  77. */
  78. function PMA_getAvailableMIMEtypes()
  79. {
  80. static $stack = null;
  81. if (null !== $stack) {
  82. return $stack;
  83. }
  84. $stack = array();
  85. $filestack = array();
  86. $handle = opendir('./libraries/transformations');
  87. if (! $handle) {
  88. return $stack;
  89. }
  90. while ($file = readdir($handle)) {
  91. $filestack[] = $file;
  92. }
  93. closedir($handle);
  94. sort($filestack);
  95. foreach ($filestack as $file) {
  96. if (preg_match('|^.*__.*\.inc\.php$|', $file)) {
  97. // File contains transformation functions.
  98. $base = explode('__', str_replace('.inc.php', '', $file));
  99. $mimetype = str_replace('_', '/', $base[0]);
  100. $stack['mimetype'][$mimetype] = $mimetype;
  101. $stack['transformation'][] = $mimetype . ': ' . $base[1];
  102. $stack['transformation_file'][] = $file;
  103. } elseif (preg_match('|^.*\.inc\.php$|', $file)) {
  104. // File is a plain mimetype, no functions.
  105. $base = str_replace('.inc.php', '', $file);
  106. if ($base != 'global') {
  107. $mimetype = str_replace('_', '/', $base);
  108. $stack['mimetype'][$mimetype] = $mimetype;
  109. $stack['empty_mimetype'][$mimetype] = $mimetype;
  110. }
  111. }
  112. }
  113. return $stack;
  114. }
  115. /**
  116. * Gets the mimetypes for all columns of a table
  117. *
  118. * @uses $GLOBALS['controllink']
  119. * @uses PMA_getRelationsParam()
  120. * @uses PMA_backquote()
  121. * @uses PMA_sqlAddslashes()
  122. * @uses PMA_DBI_fetch_result()
  123. * @access public
  124. * @param string $db the name of the db to check for
  125. * @param string $table the name of the table to check for
  126. * @param string $strict whether to include only results having a mimetype set
  127. * @return array [field_name][field_key] = field_value
  128. */
  129. function PMA_getMIME($db, $table, $strict = false)
  130. {
  131. $cfgRelation = PMA_getRelationsParam();
  132. if (! $cfgRelation['commwork']) {
  133. return false;
  134. }
  135. $com_qry = '
  136. SELECT `column_name`,
  137. `mimetype`,
  138. `transformation`,
  139. `transformation_options`
  140. FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
  141. WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
  142. AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
  143. AND ( `mimetype` != \'\'' . (!$strict ? '
  144. OR `transformation` != \'\'
  145. OR `transformation_options` != \'\'' : '') . ')';
  146. return PMA_DBI_fetch_result($com_qry, 'column_name', null, $GLOBALS['controllink']);
  147. } // end of the 'PMA_getMIME()' function
  148. /**
  149. * Set a single mimetype to a certain value.
  150. *
  151. * @uses PMA_DBI_QUERY_STORE
  152. * @uses PMA_getRelationsParam()
  153. * @uses PMA_backquote()
  154. * @uses PMA_sqlAddslashes()
  155. * @uses PMA_query_as_controluser()
  156. * @uses PMA_DBI_num_rows()
  157. * @uses PMA_DBI_fetch_assoc()
  158. * @uses PMA_DBI_free_result()
  159. * @uses strlen()
  160. * @access public
  161. * @param string $db the name of the db
  162. * @param string $table the name of the table
  163. * @param string $key the name of the column
  164. * @param string $mimetype the mimetype of the column
  165. * @param string $transformation the transformation of the column
  166. * @param string $transformation_options the transformation options of the column
  167. * @param string $forcedelete force delete, will erase any existing comments for this column
  168. * @return boolean true, if comment-query was made.
  169. */
  170. function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
  171. $transformation_options, $forcedelete = false)
  172. {
  173. $cfgRelation = PMA_getRelationsParam();
  174. if (! $cfgRelation['commwork']) {
  175. return false;
  176. }
  177. $test_qry = '
  178. SELECT `mimetype`,
  179. `comment`
  180. FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
  181. WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
  182. AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
  183. AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
  184. $test_rs = PMA_query_as_controluser($test_qry, true, PMA_DBI_QUERY_STORE);
  185. if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
  186. $row = @PMA_DBI_fetch_assoc($test_rs);
  187. PMA_DBI_free_result($test_rs);
  188. if (! $forcedelete
  189. && (strlen($mimetype) || strlen($transformation)
  190. || strlen($transformation_options) || strlen($row['comment']))) {
  191. $upd_query = '
  192. UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
  193. SET `mimetype` = \'' . PMA_sqlAddslashes($mimetype) . '\',
  194. `transformation` = \'' . PMA_sqlAddslashes($transformation) . '\',
  195. `transformation_options` = \'' . PMA_sqlAddslashes($transformation_options) . '\'';
  196. } else {
  197. $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']);
  198. }
  199. $upd_query .= '
  200. WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
  201. AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'
  202. AND `column_name` = \'' . PMA_sqlAddslashes($key) . '\'';
  203. } elseif (strlen($mimetype) || strlen($transformation)
  204. || strlen($transformation_options)) {
  205. $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info'])
  206. . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
  207. . ' VALUES('
  208. . '\'' . PMA_sqlAddslashes($db) . '\','
  209. . '\'' . PMA_sqlAddslashes($table) . '\','
  210. . '\'' . PMA_sqlAddslashes($key) . '\','
  211. . '\'' . PMA_sqlAddslashes($mimetype) . '\','
  212. . '\'' . PMA_sqlAddslashes($transformation) . '\','
  213. . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
  214. }
  215. if (isset($upd_query)){
  216. return PMA_query_as_controluser($upd_query);
  217. } else {
  218. return false;
  219. }
  220. } // end of 'PMA_setMIME()' function
  221. ?>