transformations.lib.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 boolean $html_formatted whether the description should be formatted
  122. * as HTML
  123. *
  124. * @return String 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. return $class_name::getInfo();
  134. }
  135. /**
  136. * Gets the mimetypes for all columns of a table
  137. *
  138. * @param string $db the name of the db to check for
  139. * @param string $table the name of the table to check for
  140. * @param boolean $strict whether to include only results having a mimetype set
  141. *
  142. * @access public
  143. *
  144. * @return array [field_name][field_key] = field_value
  145. */
  146. function PMA_getMIME($db, $table, $strict = false)
  147. {
  148. $cfgRelation = PMA_getRelationsParam();
  149. if (! $cfgRelation['commwork']) {
  150. return false;
  151. }
  152. $com_qry = '
  153. SELECT `column_name`,
  154. `mimetype`,
  155. `transformation`,
  156. `transformation_options`
  157. FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
  158. . PMA_Util::backquote($cfgRelation['column_info']) . '
  159. WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  160. AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
  161. AND ( `mimetype` != \'\'' . (!$strict ? '
  162. OR `transformation` != \'\'
  163. OR `transformation_options` != \'\'' : '') . ')';
  164. $result = $GLOBALS['dbi']->fetchResult(
  165. $com_qry, 'column_name', null, $GLOBALS['controllink']
  166. );
  167. foreach ($result as $column => $values) {
  168. // replacements in mimetype and transformation
  169. $values = str_replace("jpeg", "JPEG", $values);
  170. $values = str_replace("png", "PNG", $values);
  171. $values = str_replace("octet-stream", "Octetstream", $values);
  172. // convert mimetype to new format (f.e. Text_Plain, etc)
  173. $delimiter_space = '- ';
  174. $delimiter = "_";
  175. $values['mimetype'] = str_replace(
  176. $delimiter_space,
  177. $delimiter,
  178. ucwords(
  179. str_replace(
  180. $delimiter,
  181. $delimiter_space,
  182. $values['mimetype']
  183. )
  184. )
  185. );
  186. // convert transformation to new format (class name)
  187. // f.e. Text_Plain_Substring.class.php
  188. $values = str_replace("__", "_", $values);
  189. $values = str_replace(".inc.php", ".class.php", $values);
  190. $values['transformation'] = str_replace(
  191. $delimiter_space,
  192. $delimiter,
  193. ucwords(
  194. str_replace(
  195. $delimiter,
  196. $delimiter_space,
  197. $values['transformation']
  198. )
  199. )
  200. );
  201. $result[$column] = $values;
  202. }
  203. return $result;
  204. } // end of the 'PMA_getMIME()' function
  205. /**
  206. * Set a single mimetype to a certain value.
  207. *
  208. * @param string $db the name of the db
  209. * @param string $table the name of the table
  210. * @param string $key the name of the column
  211. * @param string $mimetype the mimetype of the column
  212. * @param string $transformation the transformation of the column
  213. * @param string $transformationOpts the transformation options of the column
  214. * @param boolean $forcedelete force delete, will erase any existing
  215. * comments for this column
  216. *
  217. * @access public
  218. *
  219. * @return boolean true, if comment-query was made.
  220. */
  221. function PMA_setMIME($db, $table, $key, $mimetype, $transformation,
  222. $transformationOpts, $forcedelete = false
  223. ) {
  224. $cfgRelation = PMA_getRelationsParam();
  225. if (! $cfgRelation['commwork']) {
  226. return false;
  227. }
  228. // convert mimetype to old format (f.e. text_plain)
  229. $mimetype = strtolower($mimetype);
  230. // old format has octet-stream instead of octetstream for mimetype
  231. if (strstr($mimetype, "octetstream")) {
  232. $mimetype = "application_octet-stream";
  233. }
  234. // convert transformation to old format (f.e. text_plain__substring.inc.php)
  235. $transformation = strtolower($transformation);
  236. $transformation = str_replace(".class.php", ".inc.php", $transformation);
  237. $last_pos = strrpos($transformation, "_");
  238. $transformation = substr($transformation, 0, $last_pos) . "_"
  239. . substr($transformation, $last_pos);
  240. $test_qry = '
  241. SELECT `mimetype`,
  242. `comment`
  243. FROM ' . PMA_Util::backquote($cfgRelation['db']) . '.'
  244. . PMA_Util::backquote($cfgRelation['column_info']) . '
  245. WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  246. AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
  247. AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
  248. $test_rs = PMA_queryAsControlUser(
  249. $test_qry, true, PMA_DatabaseInterface::QUERY_STORE
  250. );
  251. if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
  252. $row = @$GLOBALS['dbi']->fetchAssoc($test_rs);
  253. $GLOBALS['dbi']->freeResult($test_rs);
  254. if (! $forcedelete
  255. && (strlen($mimetype) || strlen($transformation)
  256. || strlen($transformationOpts) || strlen($row['comment']))
  257. ) {
  258. $upd_query = 'UPDATE ' . PMA_Util::backquote($cfgRelation['db']) . '.'
  259. . PMA_Util::backquote($cfgRelation['column_info'])
  260. . ' SET '
  261. . '`mimetype` = \''
  262. . PMA_Util::sqlAddSlashes($mimetype) . '\', '
  263. . '`transformation` = \''
  264. . PMA_Util::sqlAddSlashes($transformation) . '\', '
  265. . '`transformation_options` = \''
  266. . PMA_Util::sqlAddSlashes($transformationOpts) . '\'';
  267. } else {
  268. $upd_query = 'DELETE FROM ' . PMA_Util::backquote($cfgRelation['db'])
  269. . '.' . PMA_Util::backquote($cfgRelation['column_info']);
  270. }
  271. $upd_query .= '
  272. WHERE `db_name` = \'' . PMA_Util::sqlAddSlashes($db) . '\'
  273. AND `table_name` = \'' . PMA_Util::sqlAddSlashes($table) . '\'
  274. AND `column_name` = \'' . PMA_Util::sqlAddSlashes($key) . '\'';
  275. } elseif (strlen($mimetype)
  276. || strlen($transformation)
  277. || strlen($transformationOpts)
  278. ) {
  279. $upd_query = 'INSERT INTO ' . PMA_Util::backquote($cfgRelation['db'])
  280. . '.' . PMA_Util::backquote($cfgRelation['column_info'])
  281. . ' (db_name, table_name, column_name, mimetype, '
  282. . 'transformation, transformation_options) '
  283. . ' VALUES('
  284. . '\'' . PMA_Util::sqlAddSlashes($db) . '\','
  285. . '\'' . PMA_Util::sqlAddSlashes($table) . '\','
  286. . '\'' . PMA_Util::sqlAddSlashes($key) . '\','
  287. . '\'' . PMA_Util::sqlAddSlashes($mimetype) . '\','
  288. . '\'' . PMA_Util::sqlAddSlashes($transformation) . '\','
  289. . '\'' . PMA_Util::sqlAddSlashes($transformationOpts) . '\')';
  290. }
  291. if (isset($upd_query)) {
  292. return PMA_queryAsControlUser($upd_query);
  293. } else {
  294. return false;
  295. }
  296. } // end of 'PMA_setMIME()' function
  297. /**
  298. * GLOBAL Plugin functions
  299. */
  300. /**
  301. * Replaces "[__BUFFER__]" occurences found in $options['string'] with the text
  302. * in $buffer, after performing a regular expression search and replace on
  303. * $buffer using $options['regex'] and $options['regex_replace'].
  304. *
  305. * @param string $buffer text that will be replaced in $options['string'],
  306. * after being formatted
  307. * @param array $options the options required to format $buffer
  308. * = array (
  309. * 'string' => 'string', // text containing "[__BUFFER__]"
  310. * 'regex' => 'mixed', // the pattern to search for
  311. * 'regex_replace' => 'mixed', // string or array of strings to replace
  312. * // with
  313. * );
  314. *
  315. * @return string containing the text with all the replacements
  316. */
  317. function PMA_Transformation_globalHtmlReplace($buffer, $options = array())
  318. {
  319. if ( ! isset($options['string']) ) {
  320. $options['string'] = '';
  321. }
  322. if (isset($options['regex']) && isset($options['regex_replace'])) {
  323. $buffer = preg_replace(
  324. '@' . str_replace('@', '\@', $options['regex']) . '@si',
  325. $options['regex_replace'],
  326. $buffer
  327. );
  328. }
  329. // Replace occurences of [__BUFFER__] with actual text
  330. $return = str_replace("[__BUFFER__]", $buffer, $options['string']);
  331. return $return;
  332. }
  333. /**
  334. * Delete related transformation details
  335. * after deleting database. table or column
  336. *
  337. * @param string $db Database name
  338. * @param string $table Table name
  339. * @param string $column Column name
  340. *
  341. * @return boolean State of the query execution
  342. */
  343. function PMA_clearTransformations($db, $table = '', $column = '')
  344. {
  345. $cfgRelation = PMA_getRelationsParam();
  346. if (! isset($cfgRelation['column_info'])) {
  347. return false;
  348. }
  349. $delete_sql = 'DELETE FROM '
  350. . PMA_Util::backquote($cfgRelation['db']) . '.'
  351. . PMA_Util::backquote($cfgRelation['column_info'])
  352. . ' WHERE ';
  353. if (($column != '') && ($table != '')) {
  354. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  355. . '`table_name` = \'' . $table . '\' AND '
  356. . '`column_name` = \'' . $column . '\' ';
  357. } else if ($table != '') {
  358. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  359. . '`table_name` = \'' . $table . '\' ';
  360. } else {
  361. $delete_sql .= '`db_name` = \'' . $db . '\' ';
  362. }
  363. return $GLOBALS['dbi']->tryQuery($delete_sql);
  364. }
  365. ?>