transformations.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. * This file also provides basic functions to use in other plungins!
  7. * These are declared in the 'GLOBAL Plugin functions' section
  8. *
  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.
  12. *
  13. * Please provide a comment for your function,
  14. * what it does and what parameters are available.
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. if (! defined('PHPMYADMIN')) {
  19. exit;
  20. }
  21. /**
  22. * Returns array of options from string with options separated by comma,
  23. * removes quotes
  24. *
  25. * <code>
  26. * PMA_transformation_getOptions("'option ,, quoted',abd,'2,3',");
  27. * // array {
  28. * // 'option ,, quoted',
  29. * // 'abc',
  30. * // '2,3',
  31. * // '',
  32. * // }
  33. * </code>
  34. *
  35. * @param string $option_string comma separated options
  36. *
  37. * @return array options
  38. */
  39. function PMA_transformation_getOptions($option_string)
  40. {
  41. $result = array();
  42. if (! strlen($option_string)
  43. || ! $transform_options = preg_split('/,/', $option_string)
  44. ) {
  45. return $result;
  46. }
  47. while (($option = array_shift($transform_options)) !== null) {
  48. $trimmed = trim($option);
  49. if (strlen($trimmed) > 1
  50. && $trimmed[0] == "'"
  51. && $trimmed[strlen($trimmed) - 1] == "'"
  52. ) {
  53. // '...'
  54. $option = substr($trimmed, 1, -1);
  55. } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
  56. // '...,
  57. $trimmed = ltrim($option);
  58. while (($option = array_shift($transform_options)) !== null) {
  59. // ...,
  60. $trimmed .= ',' . $option;
  61. $rtrimmed = rtrim($trimmed);
  62. if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
  63. // ,...'
  64. break;
  65. }
  66. }
  67. $option = substr($rtrimmed, 1, -1);
  68. }
  69. $result[] = stripslashes($option);
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Gets all available MIME-types
  75. *
  76. * @access public
  77. * @staticvar array mimetypes
  78. * @return array array[mimetype], array[transformation]
  79. */
  80. function PMA_getAvailableMIMEtypes()
  81. {
  82. static $stack = null;
  83. if (null !== $stack) {
  84. return $stack;
  85. }
  86. $stack = array();
  87. $filestack = array();
  88. $handle = opendir('./libraries/plugins/transformations');
  89. if (! $handle) {
  90. return $stack;
  91. }
  92. while ($file = readdir($handle)) {
  93. $filestack[] = $file;
  94. }
  95. closedir($handle);
  96. sort($filestack);
  97. foreach ($filestack as $file) {
  98. if (preg_match('|^.*_.*_.*\.class\.php$|', $file)) {
  99. // File contains transformation functions.
  100. $parts = explode('_', str_replace('.class.php', '', $file));
  101. $mimetype = $parts[0] . "/" . $parts[1];
  102. $stack['mimetype'][$mimetype] = $mimetype;
  103. $stack['transformation'][] = $mimetype . ': ' . $parts[2];
  104. $stack['transformation_file'][] = $file;
  105. } elseif (preg_match('|^.*\.class.php$|', $file)) {
  106. // File is a plain mimetype, no functions.
  107. $base = str_replace('.class.php', '', $file);
  108. if ($base != 'global') {
  109. $mimetype = str_replace('_', '/', $base);
  110. $stack['mimetype'][$mimetype] = $mimetype;
  111. $stack['empty_mimetype'][$mimetype] = $mimetype;
  112. }
  113. }
  114. }
  115. return $stack;
  116. }
  117. /**
  118. * Returns the description of the transformation
  119. *
  120. * @param string $file transformation file
  121. * @param string $html_formatted whether the description should be formatted
  122. * as HTML
  123. *
  124. * @return the description of the transformation
  125. */
  126. function PMA_getTransformationDescription($file, $html_formatted = true)
  127. {
  128. // get the transformation class name
  129. $class_name = explode(".class.php", $file);
  130. $class_name = $class_name[0];
  131. // include and instantiate the class
  132. include_once 'libraries/plugins/transformations/' . $file;
  133. /* Temporary workaround for bug #3783 :
  134. Calling a method from a variable class is not possible before PHP 5.3. */
  135. $functionGetInfo = $class_name . "_getInfo";
  136. return $functionGetInfo();
  137. }
  138. /**
  139. * Gets the mimetypes for all columns of a table
  140. *
  141. * @param string $db the name of the db to check for
  142. * @param string $table the name of the table to check for
  143. * @param string $strict whether to include only results having a mimetype set
  144. *
  145. * @access public
  146. *
  147. * @return array [field_name][field_key] = field_value
  148. */
  149. function PMA_getMIME($db, $table, $strict = false)
  150. {
  151. $cfgRelation = PMA_getRelationsParam();
  152. if (! $cfgRelation['commwork']) {
  153. return false;
  154. }
  155. $com_qry = '
  156. SELECT `column_name`,
  157. `mimetype`,
  158. `transformation`,
  159. `transformation_options`
  160. FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
  161. . PMA_Util::backquote($cfgRelation['column_info']) . '
  162. WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  163. AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
  164. AND ( `mimetype` != \'\'' . (!$strict ? '
  165. OR `transformation` != \'\'
  166. OR `transformation_options` != \'\'' : '') . ')';
  167. $result = PMA_DBI_fetch_result(
  168. $com_qry, 'column_name', null, $GLOBALS['controllink']
  169. );
  170. foreach ($result as $column => $values) {
  171. // replacements in mimetype and transformation
  172. $values = str_replace("jpeg", "JPEG", $values);
  173. $values = str_replace("png", "PNG", $values);
  174. $values = str_replace("octet-stream", "Octetstream", $values);
  175. // convert mimetype to new format (f.e. Text_Plain, etc)
  176. $delimiter_space = '- ';
  177. $delimiter = "_";
  178. $values['mimetype'] = str_replace(
  179. $delimiter_space,
  180. $delimiter,
  181. ucwords(
  182. str_replace(
  183. $delimiter,
  184. $delimiter_space,
  185. $values['mimetype']
  186. )
  187. )
  188. );
  189. // convert transformation to new format (class name)
  190. // f.e. Text_Plain_Substring.class.php
  191. $values = str_replace("__", "_", $values);
  192. $values = str_replace(".inc.php", ".class.php", $values);
  193. $values['transformation'] = str_replace(
  194. $delimiter_space,
  195. $delimiter,
  196. ucwords(
  197. str_replace(
  198. $delimiter,
  199. $delimiter_space,
  200. $values['transformation']
  201. )
  202. )
  203. );
  204. $result[$column] = $values;
  205. }
  206. return $result;
  207. } // end of the 'PMA_getMIME()' function
  208. /**
  209. * Set a single mimetype to a certain value.
  210. *
  211. * @param string $db the name of the db
  212. * @param string $table the name of the table
  213. * @param string $key the name of the column
  214. * @param string $mimetype the mimetype of the column
  215. * @param string $transformation the transformation of the column
  216. * @param string $transformation_options the transformation options of the column
  217. * @param string $forcedelete force delete, will erase any existing
  218. * comments for this column
  219. *
  220. * @access public
  221. *
  222. * @return boolean true, if comment-query was made.
  223. */
  224. function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
  225. $transformation_options, $forcedelete = false
  226. ) {
  227. $cfgRelation = PMA_getRelationsParam();
  228. if (! $cfgRelation['commwork']) {
  229. return false;
  230. }
  231. // convert mimetype to old format (f.e. text_plain)
  232. $mimetype = strtolower($mimetype);
  233. // old format has octet-stream instead of octetstream for mimetype
  234. if (strstr($mimetype, "octetstream")) {
  235. $mimetype = "application_octet-stream";
  236. }
  237. // convert transformation to old format (f.e. text_plain__substring.inc.php)
  238. $transformation = strtolower($transformation);
  239. $transformation = str_replace(".class.php", ".inc.php", $transformation);
  240. $last_pos = strrpos($transformation, "_");
  241. $transformation = substr($transformation, 0, $last_pos) . "_"
  242. . substr($transformation, $last_pos);
  243. $test_qry = '
  244. SELECT `mimetype`,
  245. `comment`
  246. FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
  247. . PMA_Util::backquote($cfgRelation['column_info']) . '
  248. WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  249. AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
  250. AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
  251. $test_rs = PMA_queryAsControlUser($test_qry, true, PMA_DBI_QUERY_STORE);
  252. if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) {
  253. $row = @PMA_DBI_fetch_assoc($test_rs);
  254. PMA_DBI_free_result($test_rs);
  255. if (! $forcedelete
  256. && (strlen($mimetype) || strlen($transformation)
  257. || strlen($transformation_options) || strlen($row['comment']))
  258. ) {
  259. $upd_query = '
  260. UPDATE ' . PMA_Util::backquote($cfgRelation['db']) . '.'
  261. . PMA_Util::backquote($cfgRelation['column_info']) . '
  262. SET `mimetype` = \'' . PMA_Util::sqlAddSlashes($mimetype) . '\',
  263. `transformation` = \'' . PMA_Util::sqlAddSlashes($transformation) . '\',
  264. `transformation_options` = \'' . PMA_Util::sqlAddSlashes($transformation_options) . '\'';
  265. } else {
  266. $upd_query = 'DELETE FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['column_info']);
  267. }
  268. $upd_query .= '
  269. WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  270. AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
  271. AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
  272. } elseif (strlen($mimetype) || strlen($transformation)
  273. || strlen($transformation_options)) {
  274. $upd_query = 'INSERT INTO ' . PMA_Util::backquote($cfgRelation['db']) . '.' . PMA_Util::backquote($cfgRelation['column_info'])
  275. . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
  276. . ' VALUES('
  277. . '\'' . PMA_Util::sqlAddSlashes($db) . '\','
  278. . '\'' . PMA_Util::sqlAddSlashes($table) . '\','
  279. . '\'' . PMA_Util::sqlAddSlashes($key) . '\','
  280. . '\'' . PMA_Util::sqlAddSlashes($mimetype) . '\','
  281. . '\'' . PMA_Util::sqlAddSlashes($transformation) . '\','
  282. . '\'' . PMA_Util::sqlAddSlashes($transformation_options) . '\')';
  283. }
  284. if (isset($upd_query)) {
  285. return PMA_queryAsControlUser($upd_query);
  286. } else {
  287. return false;
  288. }
  289. } // end of 'PMA_setMIME()' function
  290. /**
  291. * GLOBAL Plugin functions
  292. */
  293. /**
  294. * Delete related transformation details
  295. * after deleting database. table or column
  296. *
  297. * @param string $db Database name
  298. * @param string $table Table name
  299. * @param string $column Column name
  300. *
  301. * @return boolean State of the query execution
  302. */
  303. function PMA_clearTransformations($db, $table = '', $column = '')
  304. {
  305. $cfgRelation = PMA_getRelationsParam();
  306. if (! isset($cfgRelation['column_info'])) {
  307. return false;
  308. }
  309. $delete_sql = 'DELETE FROM '
  310. . PMA_Util::backquote($cfgRelation['db']) . '.'
  311. . PMA_Util::backquote($cfgRelation['column_info'])
  312. . ' WHERE ';
  313. if (($column != '') && ($table != '')) {
  314. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  315. . '`table_name` = \'' . $table . '\' AND '
  316. . '`column_name` = \'' . $column . '\' ';
  317. } else if ($table != '') {
  318. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  319. . '`table_name` = \'' . $table . '\' ';
  320. } else {
  321. $delete_sql .= '`db_name` = \'' . $db . '\' ';
  322. }
  323. return PMA_DBI_try_query($delete_sql);
  324. }
  325. ?>