Transformations.php 16 KB

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