Transformations.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. // include and instantiate the class
  173. include_once $include_file;
  174. return $class_name::getInfo();
  175. }
  176. /**
  177. * Returns the name of the transformation
  178. *
  179. * @param string $file transformation file
  180. *
  181. * @return String the name of the transformation
  182. */
  183. public static function getName($file)
  184. {
  185. $include_file = 'libraries/classes/Plugins/Transformations/' . $file;
  186. /* @var $class_name PhpMyAdmin\Plugins\TransformationsInterface */
  187. $class_name = self::getClassName($include_file);
  188. // include and instantiate the class
  189. include_once $include_file;
  190. return $class_name::getName();
  191. }
  192. /**
  193. * Fixups old MIME or transformation name to new one
  194. *
  195. * - applies some hardcoded fixups
  196. * - adds spaces after _ and numbers
  197. * - capitalizes words
  198. * - removes back spaces
  199. *
  200. * @param string $value Value to fixup
  201. *
  202. * @return string
  203. */
  204. static function fixupMIME($value)
  205. {
  206. $value = str_replace(
  207. array("jpeg", "png"), array("JPEG", "PNG"), $value
  208. );
  209. return str_replace(
  210. ' ',
  211. '',
  212. ucwords(
  213. preg_replace('/([0-9_]+)/', '$1 ', $value)
  214. )
  215. );
  216. }
  217. /**
  218. * Gets the mimetypes for all columns of a table
  219. *
  220. * @param string $db the name of the db to check for
  221. * @param string $table the name of the table to check for
  222. * @param boolean $strict whether to include only results having a mimetype set
  223. * @param boolean $fullName whether to use full column names as the key
  224. *
  225. * @access public
  226. *
  227. * @return array [field_name][field_key] = field_value
  228. */
  229. public static function getMIME($db, $table, $strict = false, $fullName = false)
  230. {
  231. $relation = new Relation();
  232. $cfgRelation = $relation->getRelationsParam();
  233. if (! $cfgRelation['mimework']) {
  234. return false;
  235. }
  236. $com_qry = '';
  237. if ($fullName) {
  238. $com_qry .= "SELECT CONCAT("
  239. . "`db_name`, '.', `table_name`, '.', `column_name`"
  240. . ") AS column_name, ";
  241. } else {
  242. $com_qry = "SELECT `column_name`, ";
  243. }
  244. $com_qry .= '`mimetype`,
  245. `transformation`,
  246. `transformation_options`,
  247. `input_transformation`,
  248. `input_transformation_options`
  249. FROM ' . Util::backquote($cfgRelation['db']) . '.'
  250. . Util::backquote($cfgRelation['column_info']) . '
  251. WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
  252. AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table) . '\'
  253. AND ( `mimetype` != \'\'' . (!$strict ? '
  254. OR `transformation` != \'\'
  255. OR `transformation_options` != \'\'
  256. OR `input_transformation` != \'\'
  257. OR `input_transformation_options` != \'\'' : '') . ')';
  258. $result = $GLOBALS['dbi']->fetchResult(
  259. $com_qry, 'column_name', null, DatabaseInterface::CONNECT_CONTROL
  260. );
  261. foreach ($result as $column => $values) {
  262. // convert mimetype to new format (f.e. Text_Plain, etc)
  263. $delimiter_space = '- ';
  264. $delimiter = "_";
  265. $values['mimetype'] = self::fixupMIME($values['mimetype']);
  266. // For transformation of form
  267. // output/image_jpeg__inline.inc.php
  268. // extract dir part.
  269. $dir = explode('/', $values['transformation']);
  270. $subdir = '';
  271. if (count($dir) === 2) {
  272. $subdir = ucfirst($dir[0]) . '/';
  273. $values['transformation'] = $dir[1];
  274. }
  275. $values['transformation'] = self::fixupMIME($values['transformation']);
  276. $values['transformation'] = $subdir . $values['transformation'];
  277. $result[$column] = $values;
  278. }
  279. return $result;
  280. } // end of the 'getMIME()' function
  281. /**
  282. * Set a single mimetype to a certain value.
  283. *
  284. * @param string $db the name of the db
  285. * @param string $table the name of the table
  286. * @param string $key the name of the column
  287. * @param string $mimetype the mimetype of the column
  288. * @param string $transformation the transformation of the column
  289. * @param string $transformationOpts the transformation options of the column
  290. * @param string $inputTransform the input transformation of the column
  291. * @param string $inputTransformOpts the input transformation options of the column
  292. * @param boolean $forcedelete force delete, will erase any existing
  293. * comments for this column
  294. *
  295. * @access public
  296. *
  297. * @return boolean true, if comment-query was made.
  298. */
  299. public static function setMIME($db, $table, $key, $mimetype, $transformation,
  300. $transformationOpts, $inputTransform, $inputTransformOpts, $forcedelete = false
  301. ) {
  302. $relation = new Relation();
  303. $cfgRelation = $relation->getRelationsParam();
  304. if (! $cfgRelation['mimework']) {
  305. return false;
  306. }
  307. // lowercase mimetype & transformation
  308. $mimetype = mb_strtolower($mimetype);
  309. $transformation = mb_strtolower($transformation);
  310. // Do we have any parameter to set?
  311. $has_value = (
  312. strlen($mimetype) > 0 ||
  313. strlen($transformation) > 0 ||
  314. strlen($transformationOpts) > 0 ||
  315. strlen($inputTransform) > 0 ||
  316. strlen($inputTransformOpts) > 0
  317. );
  318. $test_qry = '
  319. SELECT `mimetype`,
  320. `comment`
  321. FROM ' . Util::backquote($cfgRelation['db']) . '.'
  322. . Util::backquote($cfgRelation['column_info']) . '
  323. WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
  324. AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table) . '\'
  325. AND `column_name` = \'' . $GLOBALS['dbi']->escapeString($key) . '\'';
  326. $test_rs = $relation->queryAsControlUser(
  327. $test_qry, true, DatabaseInterface::QUERY_STORE
  328. );
  329. if ($test_rs && $GLOBALS['dbi']->numRows($test_rs) > 0) {
  330. $row = @$GLOBALS['dbi']->fetchAssoc($test_rs);
  331. $GLOBALS['dbi']->freeResult($test_rs);
  332. if (! $forcedelete && ($has_value || strlen($row['comment']) > 0)) {
  333. $upd_query = 'UPDATE '
  334. . Util::backquote($cfgRelation['db']) . '.'
  335. . Util::backquote($cfgRelation['column_info'])
  336. . ' SET '
  337. . '`mimetype` = \''
  338. . $GLOBALS['dbi']->escapeString($mimetype) . '\', '
  339. . '`transformation` = \''
  340. . $GLOBALS['dbi']->escapeString($transformation) . '\', '
  341. . '`transformation_options` = \''
  342. . $GLOBALS['dbi']->escapeString($transformationOpts) . '\', '
  343. . '`input_transformation` = \''
  344. . $GLOBALS['dbi']->escapeString($inputTransform) . '\', '
  345. . '`input_transformation_options` = \''
  346. . $GLOBALS['dbi']->escapeString($inputTransformOpts) . '\'';
  347. } else {
  348. $upd_query = 'DELETE FROM '
  349. . Util::backquote($cfgRelation['db'])
  350. . '.' . Util::backquote($cfgRelation['column_info']);
  351. }
  352. $upd_query .= '
  353. WHERE `db_name` = \'' . $GLOBALS['dbi']->escapeString($db) . '\'
  354. AND `table_name` = \'' . $GLOBALS['dbi']->escapeString($table)
  355. . '\'
  356. AND `column_name` = \'' . $GLOBALS['dbi']->escapeString($key)
  357. . '\'';
  358. } elseif ($has_value) {
  359. $upd_query = 'INSERT INTO '
  360. . Util::backquote($cfgRelation['db'])
  361. . '.' . Util::backquote($cfgRelation['column_info'])
  362. . ' (db_name, table_name, column_name, mimetype, '
  363. . 'transformation, transformation_options, '
  364. . 'input_transformation, input_transformation_options) '
  365. . ' VALUES('
  366. . '\'' . $GLOBALS['dbi']->escapeString($db) . '\','
  367. . '\'' . $GLOBALS['dbi']->escapeString($table) . '\','
  368. . '\'' . $GLOBALS['dbi']->escapeString($key) . '\','
  369. . '\'' . $GLOBALS['dbi']->escapeString($mimetype) . '\','
  370. . '\'' . $GLOBALS['dbi']->escapeString($transformation) . '\','
  371. . '\'' . $GLOBALS['dbi']->escapeString($transformationOpts) . '\','
  372. . '\'' . $GLOBALS['dbi']->escapeString($inputTransform) . '\','
  373. . '\'' . $GLOBALS['dbi']->escapeString($inputTransformOpts) . '\')';
  374. }
  375. if (isset($upd_query)) {
  376. return $relation->queryAsControlUser($upd_query);
  377. }
  378. return false;
  379. } // end of 'setMIME()' function
  380. /**
  381. * GLOBAL Plugin functions
  382. */
  383. /**
  384. * Delete related transformation details
  385. * after deleting database. table or column
  386. *
  387. * @param string $db Database name
  388. * @param string $table Table name
  389. * @param string $column Column name
  390. *
  391. * @return boolean State of the query execution
  392. */
  393. public static function clear($db, $table = '', $column = '')
  394. {
  395. $relation = new Relation();
  396. $cfgRelation = $relation->getRelationsParam();
  397. if (! isset($cfgRelation['column_info'])) {
  398. return false;
  399. }
  400. $delete_sql = 'DELETE FROM '
  401. . Util::backquote($cfgRelation['db']) . '.'
  402. . Util::backquote($cfgRelation['column_info'])
  403. . ' WHERE ';
  404. if (($column != '') && ($table != '')) {
  405. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  406. . '`table_name` = \'' . $table . '\' AND '
  407. . '`column_name` = \'' . $column . '\' ';
  408. } elseif ($table != '') {
  409. $delete_sql .= '`db_name` = \'' . $db . '\' AND '
  410. . '`table_name` = \'' . $table . '\' ';
  411. } else {
  412. $delete_sql .= '`db_name` = \'' . $db . '\' ';
  413. }
  414. return $GLOBALS['dbi']->tryQuery($delete_sql);
  415. }
  416. }