DateFormatTransformationsPlugin.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Abstract class for the date format transformations plugins
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  7. use PhpMyAdmin\FieldMetadata;
  8. use PhpMyAdmin\Plugins\TransformationsPlugin;
  9. use PhpMyAdmin\Sanitize;
  10. use PhpMyAdmin\Util;
  11. use function __;
  12. use function checkdate;
  13. use function gmdate;
  14. use function htmlspecialchars;
  15. use function mb_strlen;
  16. use function mb_strtolower;
  17. use function mb_substr;
  18. use function mktime;
  19. use function preg_match;
  20. use function strtotime;
  21. /**
  22. * Provides common methods for all of the date format transformations plugins.
  23. */
  24. abstract class DateFormatTransformationsPlugin extends TransformationsPlugin
  25. {
  26. /**
  27. * Gets the transformation description of the specific plugin
  28. *
  29. * @return string
  30. */
  31. public static function getInfo()
  32. {
  33. return __(
  34. 'Displays a TIME, TIMESTAMP, DATETIME or numeric unix timestamp'
  35. . ' column as formatted date. The first option is the offset (in'
  36. . ' hours) which will be added to the timestamp (Default: 0). Use'
  37. . ' second option to specify a different date/time format string.'
  38. . ' Third option determines whether you want to see local date or'
  39. . ' UTC one (use "local" or "utc" strings) for that. According to'
  40. . ' that, date format has different value - for "local" see the'
  41. . ' documentation for PHP\'s strftime() function and for "utc" it'
  42. . ' is done using gmdate() function.'
  43. );
  44. }
  45. /**
  46. * Does the actual work of each specific transformations plugin.
  47. *
  48. * @param string $buffer text to be transformed
  49. * @param array $options transformation options
  50. * @param FieldMetadata|null $meta meta information
  51. *
  52. * @return string
  53. */
  54. public function applyTransformation($buffer, array $options = [], ?FieldMetadata $meta = null)
  55. {
  56. $buffer = (string) $buffer;
  57. // possibly use a global transform and feed it with special options
  58. $cfg = $GLOBALS['cfg'];
  59. $options = $this->getOptions($options, $cfg['DefaultTransformations']['DateFormat']);
  60. // further operations on $buffer using the $options[] array.
  61. $options[2] = mb_strtolower($options[2]);
  62. if (empty($options[1])) {
  63. if ($options[2] === 'local') {
  64. $options[1] = __('%B %d, %Y at %I:%M %p');
  65. } else {
  66. $options[1] = 'Y-m-d H:i:s';
  67. }
  68. }
  69. $timestamp = -1;
  70. // INT columns will be treated as UNIX timestamps
  71. // and need to be detected before the verification for
  72. // MySQL TIMESTAMP
  73. if ($meta !== null && $meta->isType(FieldMetadata::TYPE_INT)) {
  74. $timestamp = $buffer;
  75. // Detect TIMESTAMP(6 | 8 | 10 | 12 | 14)
  76. // TIMESTAMP (2 | 4) not supported here.
  77. // (Note: prior to MySQL 4.1, TIMESTAMP has a display size
  78. // for example TIMESTAMP(8) means YYYYMMDD)
  79. } else {
  80. if (preg_match('/^(\d{2}){3,7}$/', $buffer)) {
  81. if (mb_strlen($buffer) == 14 || mb_strlen($buffer) == 8) {
  82. $offset = 4;
  83. } else {
  84. $offset = 2;
  85. }
  86. $aDate = [];
  87. $aDate['year'] = (int) mb_substr($buffer, 0, $offset);
  88. $aDate['month'] = (int) mb_substr($buffer, $offset, 2);
  89. $aDate['day'] = (int) mb_substr($buffer, $offset + 2, 2);
  90. $aDate['hour'] = (int) mb_substr($buffer, $offset + 4, 2);
  91. $aDate['minute'] = (int) mb_substr($buffer, $offset + 6, 2);
  92. $aDate['second'] = (int) mb_substr($buffer, $offset + 8, 2);
  93. if (checkdate($aDate['month'], $aDate['day'], $aDate['year'])) {
  94. $timestamp = mktime(
  95. $aDate['hour'],
  96. $aDate['minute'],
  97. $aDate['second'],
  98. $aDate['month'],
  99. $aDate['day'],
  100. $aDate['year']
  101. );
  102. }
  103. // If all fails, assume one of the dozens of valid strtime() syntaxes
  104. // (https://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html)
  105. } else {
  106. if (preg_match('/^[0-9]\d{1,9}$/', $buffer)) {
  107. $timestamp = (int) $buffer;
  108. } else {
  109. $timestamp = strtotime($buffer);
  110. }
  111. }
  112. }
  113. // If all above failed, maybe it's a Unix timestamp already?
  114. if ($timestamp < 0 && preg_match('/^[1-9]\d{1,9}$/', $buffer)) {
  115. $timestamp = $buffer;
  116. }
  117. // Reformat a valid timestamp
  118. if ($timestamp >= 0) {
  119. $timestamp -= (int) $options[0] * 60 * 60;
  120. $source = $buffer;
  121. if ($options[2] === 'local') {
  122. $text = Util::localisedDate($timestamp, $options[1]);
  123. } elseif ($options[2] === 'utc') {
  124. $text = gmdate($options[1], $timestamp);
  125. } else {
  126. $text = 'INVALID DATE TYPE';
  127. }
  128. return '<dfn onclick="alert(\'' . Sanitize::jsFormat($source, false) . '\');" title="'
  129. . htmlspecialchars((string) $source) . '">' . htmlspecialchars((string) $text) . '</dfn>';
  130. }
  131. return htmlspecialchars((string) $buffer);
  132. }
  133. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  134. /**
  135. * Gets the transformation name of the specific plugin
  136. *
  137. * @return string
  138. */
  139. public static function getName()
  140. {
  141. return 'Date Format';
  142. }
  143. }