Transformations.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 plugins!
  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. namespace PhpMyAdmin;
  19. use PhpMyAdmin\DatabaseInterface;
  20. use PhpMyAdmin\Relation;
  21. use PhpMyAdmin\Util;
  22. /**
  23. * Transformations class
  24. *
  25. * @package PhpMyAdmin
  26. */
  27. class Transformations
  28. {
  29. /**
  30. * Returns array of options from string with options separated by comma,
  31. * removes quotes
  32. *
  33. * <code>
  34. * getOptions("'option ,, quoted',abd,'2,3',");
  35. * // array {
  36. * // 'option ,, quoted',
  37. * // 'abc',
  38. * // '2,3',
  39. * // '',
  40. * // }
  41. * </code>
  42. *
  43. * @param string $option_string comma separated options
  44. *
  45. * @return array options
  46. */
  47. public static function getOptions($option_string)
  48. {
  49. $result = array();
  50. if (strlen($option_string) === 0
  51. || ! $transform_options = preg_split('/,/', $option_string)
  52. ) {
  53. return $result;
  54. }
  55. while (($option = array_shift($transform_options)) !== null) {
  56. $trimmed = trim($option);
  57. if (strlen($trimmed) > 1
  58. && $trimmed[0] == "'"
  59. && $trimmed[strlen($trimmed) - 1] == "'"
  60. ) {
  61. // '...'
  62. $option = mb_substr($trimmed, 1, -1);
  63. } elseif (isset($trimmed[0]) && $trimmed[0] == "'") {
  64. // '...,
  65. $trimmed = ltrim($option);
  66. while (($option = array_shift($transform_options)) !== null) {
  67. // ...,
  68. $trimmed .= ',' . $option;
  69. $rtrimmed = rtrim($trimmed);
  70. if ($rtrimmed[strlen($rtrimmed) - 1] == "'") {
  71. // ,...'
  72. break;
  73. }
  74. }
  75. $option = mb_substr($rtrimmed, 1, -1);
  76. }
  77. $result[] = stripslashes($option);
  78. }
  79. return $result;
  80. }
  81. /**
  82. * Gets all available MIME-types
  83. *
  84. * @access public
  85. * @staticvar array mimetypes
  86. * @return array array[mimetype], array[transformation]
  87. */
  88. public static function getAvailableMIMEtypes()
  89. {
  90. static $stack = null;
  91. if (null !== $stack) {
  92. return $stack;
  93. }
  94. $stack = array();
  95. $sub_dirs = array(
  96. 'Input/' => 'input_',
  97. 'Output/' => '',
  98. '' => ''
  99. );
  100. foreach ($sub_dirs as $sd => $prefix) {
  101. $handle = opendir('libraries/classes/Plugins/Transformations/' . $sd);
  102. if (! $handle) {
  103. $stack[$prefix . 'transformation'] = array();
  104. $stack[$prefix . 'transformation_file'] = array();
  105. continue;
  106. }
  107. $filestack = array();
  108. while ($file = readdir($handle)) {
  109. // Ignore hidden files
  110. if ($file[0] == '.') {
  111. continue;
  112. }
  113. // Ignore old plugins (.class in filename)
  114. if (strpos($file, '.class') !== false) {
  115. continue;
  116. }
  117. $filestack[] = $file;
  118. }
  119. closedir($handle);
  120. sort($filestack);
  121. foreach ($filestack as $file) {
  122. if (preg_match('|^[^.].*_.*_.*\.php$|', $file)) {
  123. // File contains transformation functions.
  124. $parts = explode('_', str_replace('.php', '', $file));
  125. $mimetype = $parts[0] . "/" . $parts[1];
  126. $stack['mimetype'][$mimetype] = $mimetype;
  127. $stack[$prefix . 'transformation'][] = $mimetype . ': ' . $parts[2];
  128. $stack[$prefix . 'transformation_file'][] = $sd . $file;
  129. if ($sd === '') {
  130. $stack['input_transformation'][] = $mimetype . ': ' . $parts[2];
  131. $stack['input_transformation_file'][] = $sd . $file;
  132. }
  133. } elseif (preg_match('|^[^.].*\.php$|', $file)) {
  134. // File is a plain mimetype, no functions.
  135. $base = str_replace('.php', '', $file);
  136. if ($base != 'global') {
  137. $mimetype = str_replace('_', '/', $base);
  138. $stack['mimetype'][$mimetype] = $mimetype;
  139. $stack['empty_mimetype'][$mimetype] = $mimetype;
  140. }
  141. }
  142. }
  143. }
  144. return $stack;
  145. }
  146. /**
  147. * Returns the class name of the transformation
  148. *
  149. * @param string $filename transformation file name
  150. *
  151. * @return string the class name of transformation
  152. */
  153. public static function getClassName($filename)
  154. {
  155. // get the transformation class name
  156. $class_name = explode(".php", $filename);
  157. $class_name = 'PhpMyAdmin\\' . str_replace('/', '\\', mb_substr($class_name[0], 18));
  158. return $class_name;
  159. }
  160. /**
  161. * Returns the description of the transformation
  162. *
  163. * @param string $file transformation file
  164. *
  165. * @return string the description of the transformation
  166. */
  167. public static function getDescription($file)
  168. {
  169. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  170. /* @var $class_name \PhpMyAdmin\Plugins\TransformationsInterface */
  171. $class_name = self::getClassName($include_file);
  172. if (class_exists($class_name)) {
  173. return $class_name::getInfo();
  174. }
  175. return '';
  176. }
  177. /**
  178. * Returns the name of the transformation
  179. *
  180. * @param string $file transformation file
  181. *
  182. * @return string the name of the transformation
  183. */
  184. public static function getName($file)
  185. {
  186. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  187. /* @var $class_name \PhpMyAdmin\Plugins\TransformationsInterface */
  188. $class_name = self::getClassName($include_file);
  189. if (class_exists($class_name)) {
  190. return $class_name::getName();
  191. }
  192. return '';
  193. }
  194. /**
  195. * Fixups old MIME or transformation name to new one
  196. *
  197. * - applies some hardcoded fixups
  198. * - adds spaces after _ and numbers
  199. * - capitalizes words
  200. * - removes back spaces
  201. *
  202. * @param string $value Value to fixup
  203. *
  204. * @return string
  205. */
  206. static function fixupMIME($value)
  207. {
  208. $value = str_replace(
  209. array("jpeg", "png"), array("JPEG", "PNG"), $value
  210. );
  211. return str_replace(
  212. ' ',
  213. '',
  214. ucwords(
  215. preg_replace('/([0-9_]+)/', '$1 ', $value)
  216. )
  217. );
  218. }
  219. /**
  220. * Gets the mimetypes for all columns of a table
  221. *
  222. * @param string $db the name of the db to check for
  223. * @param string $table the name of the table to check for
  224. * @param boolean $strict whether to include only results having a mimetype set
  225. * @param boolean $fullName whether to use full column names as the key
  226. *
  227. * @access public
  228. *
  229. * @return array [field_name][field_key] = field_value
  230. */
  231. public static function getMIME($db, $table, $strict = false, $fullName = false)
  232. {
  233. $relation = new Relation();
  234. $cfgRelation = $relation->getRelationsParam();
  235. if (! $cfgRelation['mimework']) {
  236. return false;
  237. }
  238. $com_qry = '';
  239. if ($fullName) {
  240. $com_qry .= "SELECT CONCAT("
  241. . "`db_name`, '.', `table_name`, '.', `column_name`"
  242. . ") AS column_name, ";
  243. } else {
  244. $com_qry = "SELECT `column_name`, ";
  245. }
  246. $com_qry .= '`mimetype`,
  247. `transformation`,
  248. `transformation_options`,
  249. `input_transformation`,
  250. `input_transformation_options`
  251. FROM ' . Util::backquote($cfgRelation['db']) . '.'
  252. . Util::backquote($cfgRelation['column_info']) . '
  253. WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
  254. AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table) . '\'
  255. AND ( `mimetype` != \'\'' . (!$strict ? '
  256. OR `transformation` != \'\'
  257. OR `transformation_options` != \'\'
  258. OR `input_transformation` != \'\'
  259. OR `input_transformation_options` != \'\'' : '') . ')';
  260. $result = $GLOBALS['dbi']->fetchResult(
  261. $com_qry, 'column_name', null, DatabaseInterface::CONNECT_CONTROL
  262. );
  263. foreach ($result as $column => $values) {
  264. // convert mimetype to new format (f.e. Text_Plain, etc)
  265. $delimiter_space = '- ';
  266. $delimiter = "_";
  267. $values['mimetype'] = self::fixupMIME($values['mimetype']);
  268. // For transformation of form
  269. // output/image_jpeg__inline.inc.php
  270. // extract dir part.
  271. $dir = explode('/', $values['transformation']);
  272. $subdir = '';
  273. if (count($dir) === 2) {
  274. $subdir = ucfirst($dir[0]) . '/';
  275. $values['transformation'] = $dir[1];
  276. }
  277. $values['transformation'] = self::fixupMIME($values['transformation']);
  278. $values['transformation'] = $subdir . $values['transformation'];
  279. $result[$column] = $values;
  280. }
  281. return $result;
  282. } // end of the 'getMIME()' function
  283. /**
  284. * Set a single mimetype to a certain value.
  285. *
  286. * @param string $db the name of the db
  287. * @param string $table the name of the table
  288. * @param string $key the name of the column
  289. * @param string $mimetype the mimetype of the column
  290. * @param string $transformation the transformation of the column
  291. * @param string $transformationOpts the transformation options of the column
  292. * @param string $inputTransform the input transformation of the column
  293. * @param string $inputTransformOpts the input transformation options of the column
  294. * @param boolean $forcedelete force delete, will erase any existing
  295. * comments for this column
  296. *
  297. * @access public
  298. *
  299. * @return boolean true, if comment-query was made.
  300. */
  301. public static function setMIME($db, $table, $key, $mimetype, $transformation,
  302. $transformationOpts, $inputTransform, $inputTransformOpts, $forcedelete = false
  303. ) {
  304. $relation = new Relation();
  305. $cfgRelation = $relation->getRelationsParam();
  306. if (! $cfgRelation['mimework']) {
  307. return false;
  308. }
  309. // lowercase mimetype & transformation
  310. $mimetype = mb_strtolower($mimetype);
  311. $transformation = mb_strtolower($transformation);
  312. // Do we have any parameter to set?
  313. $has_value = (
  314. strlen($mimetype) > 0 ||
  315. strlen($transformation) > 0 ||
  316. strlen($transformationOpts) > 0 ||
  317. strlen($inputTransform) > 0 ||
  318. strlen($inputTransformOpts) > 0
  319. );
  320. $test_qry = '
  321. SELECT `mimetype`,
  322. `comment`
  323. FROM ' . Util::backquote($cfgRelation['db']) . '.'
  324. . Util::backquote($cfgRelation['column_info']) . '
  325. WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
  326. AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table) . '\'
  327. AND `column_name` = \'' . $GLOBALS['dbi']->escapeString($key) . '\'';
  328. $test_rs = $relation->queryAsControlUser(
  329. $test_qry, true, DatabaseInterface::QUERY_STORE
  330. );
  331. if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
  332. $row = @$GLOBALS['dbi']->fetchAssoc($test_rs);
  333. $GLOBALS['dbi']->freeResult($test_rs);
  334. if (! $forcedelete && ($has_value || strlen($row['comment']) > 0)) {
  335. $upd_query = 'UPDATE '
  336. . Util::backquote($cfgRelation['db']) . '.'
  337. . Util::backquote($cfgRelation['column_info'])
  338. . ' SET '
  339. . '`mimetype` = \''
  340. . $GLOBALS['dbi']->escapeString($mimetype) . '\', '
  341. . '`transformation` = \''
  342. . $GLOBALS['dbi']->escapeString($transformation) . '\', '
  343. . '`transformation_options` = \''
  344. . $GLOBALS['dbi']->escapeString($transformationOpts) . '\', '
  345. . '`input_transformation` = \''
  346. . $GLOBALS['dbi']->escapeString($inputTransform) . '\', '
  347. . '`input_transformation_options` = \''
  348. . $GLOBALS['dbi']->escapeString($inputTransformOpts) . '\'';
  349. } else {
  350. $upd_query = 'DELETE FROM '
  351. . Util::backquote($cfgRelation['db'])
  352. . '.' . Util::backquote($cfgRelation['column_info']);
  353. }
  354. $upd_query .= '
  355. WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
  356. AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table)
  357. . '\'
  358. AND `column_name` = \'' . $GLOBALS['dbi']->escapeString($key)
  359. . '\'';
  360. } elseif ($has_value) {
  361. $upd_query = 'INSERT INTO '
  362. . Util::backquote($cfgRelation['db'])
  363. . '.' . Util::backquote($cfgRelation['column_info'])
  364. . ' (db_name, table_name, column_name, mimetype, '
  365. . 'transformation, transformation_options, '
  366. . 'input_transformation, input_transformation_options) '
  367. . ' VALUES('
  368. . '\'' . $GLOBALS['dbi']->escapeString($db) . '\','
  369. . '\'' . $GLOBALS['dbi']->escapeString($table) . '\','
  370. . '\'' . $GLOBALS['dbi']->escapeString($key) . '\','
  371. . '\'' . $GLOBALS['dbi']->escapeString($mimetype) . '\','
  372. . '\'' . $GLOBALS['dbi']->escapeString($transformation) . '\','
  373. . '\'' . $GLOBALS['dbi']->escapeString($transformationOpts) . '\','
  374. . '\'' . $GLOBALS['dbi']->escapeString($inputTransform) . '\','
  375. . '\'' . $GLOBALS['dbi']->escapeString($inputTransformOpts) . '\')';
  376. }
  377. if (isset($upd_query)) {
  378. return $relation->queryAsControlUser($upd_query);
  379. }
  380. return false;
  381. } // end of 'setMIME()' function
  382. /**
  383. * GLOBAL Plugin functions
  384. */
  385. /**
  386. * Delete related transformation details
  387. * after deleting database. table or column
  388. *
  389. * @param string $db Database name
  390. * @param string $table Table name
  391. * @param string $column Column name
  392. *
  393. * @return boolean State of the query execution
  394. */
  395. public static function clear($db, $table = '', $column = '')
  396. {
  397. $relation = new Relation();
  398. $cfgRelation = $relation->getRelationsParam();
  399. if (! isset($cfgRelation['column_info'])) {
  400. return false;
  401. }
  402. $delete_sql = 'DELETE FROM '
  403. . Util::backquote($cfgRelation['db']) . '.'
  404. . Util::backquote($cfgRelation['column_info'])
  405. . ' WHERE ';
  406. if (($column != '') && ($table != '')) {
  407. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  408. . '`table_name` = \'' . $table . '\' AND '
  409. . '`column_name` = \'' . $column . '\' ';
  410. } elseif ($table != '') {
  411. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  412. . '`table_name` = \'' . $table . '\' ';
  413. } else {
  414. $delete_sql .= '`db_name` = \'' . $db . '\' ';
  415. }
  416. return $GLOBALS['dbi']->tryQuery($delete_sql);
  417. }
  418. }