DateFormatTransformationsPlugin.php 5.5 KB

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