Transformations.php 16 KB

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