/22222/htdocs/db/pma/libraries/plugins/transformations/abs/DateFormatTransformationsPlugin.php

https://gitlab.com/jmcdowall/transactions · PHP · 168 lines · 106 code · 17 blank · 45 comment · 28 complexity · 1c5f7bf0aaa984d680aad9aa9c020ac0 MD5 · raw file

  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 PMA\libraries\plugins\transformations\abs;
  10. use PMA;
  11. use PMA\libraries\plugins\TransformationsPlugin;
  12. require_once 'libraries/js_escape.lib.php';
  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, $options = array(), $meta = '')
  49. {
  50. // possibly use a global transform and feed it with special options
  51. // further operations on $buffer using the $options[] array.
  52. if (empty($options[0])) {
  53. $options[0] = 0;
  54. }
  55. if (empty($options[2])) {
  56. $options[2] = 'local';
  57. } else {
  58. $options[2] = mb_strtolower($options[2]);
  59. }
  60. if (empty($options[1])) {
  61. if ($options[2] == 'local') {
  62. $options[1] = __('%B %d, %Y at %I:%M %p');
  63. } else {
  64. $options[1] = 'Y-m-d H:i:s';
  65. }
  66. }
  67. $timestamp = -1;
  68. // INT columns will be treated as UNIX timestamps
  69. // and need to be detected before the verification for
  70. // MySQL TIMESTAMP
  71. if ($meta->type == 'int') {
  72. $timestamp = $buffer;
  73. // Detect TIMESTAMP(6 | 8 | 10 | 12 | 14)
  74. // TIMESTAMP (2 | 4) not supported here.
  75. // (Note: prior to MySQL 4.1, TIMESTAMP has a display size
  76. // for example TIMESTAMP(8) means YYYYMMDD)
  77. } else {
  78. if (preg_match('/^(\d{2}){3,7}$/', $buffer)) {
  79. if (mb_strlen($buffer) == 14 || mb_strlen($buffer) == 8) {
  80. $offset = 4;
  81. } else {
  82. $offset = 2;
  83. }
  84. $aDate = array();
  85. $aDate['year'] = (int)
  86. mb_substr($buffer, 0, $offset);
  87. $aDate['month'] = (int)
  88. mb_substr($buffer, $offset, 2);
  89. $aDate['day'] = (int)
  90. mb_substr($buffer, $offset + 2, 2);
  91. $aDate['hour'] = (int)
  92. mb_substr($buffer, $offset + 4, 2);
  93. $aDate['minute'] = (int)
  94. mb_substr($buffer, $offset + 6, 2);
  95. $aDate['second'] = (int)
  96. mb_substr($buffer, $offset + 8, 2);
  97. if (checkdate($aDate['month'], $aDate['day'], $aDate['year'])) {
  98. $timestamp = mktime(
  99. $aDate['hour'],
  100. $aDate['minute'],
  101. $aDate['second'],
  102. $aDate['month'],
  103. $aDate['day'],
  104. $aDate['year']
  105. );
  106. }
  107. // If all fails, assume one of the dozens of valid strtime() syntaxes
  108. // (http://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html)
  109. } else {
  110. if (preg_match('/^[0-9]\d{1,9}$/', $buffer)) {
  111. $timestamp = (int)$buffer;
  112. } else {
  113. $timestamp = strtotime($buffer);
  114. }
  115. }
  116. }
  117. // If all above failed, maybe it's a Unix timestamp already?
  118. if ($timestamp < 0 && preg_match('/^[1-9]\d{1,9}$/', $buffer)) {
  119. $timestamp = $buffer;
  120. }
  121. // Reformat a valid timestamp
  122. if ($timestamp >= 0) {
  123. $timestamp -= $options[0] * 60 * 60;
  124. $source = $buffer;
  125. if ($options[2] == 'local') {
  126. $text = PMA\libraries\Util::localisedDate(
  127. $timestamp,
  128. $options[1]
  129. );
  130. } elseif ($options[2] == 'utc') {
  131. $text = gmdate($options[1], $timestamp);
  132. } else {
  133. $text = 'INVALID DATE TYPE';
  134. }
  135. return '<dfn onclick="alert(\'' . PMA_jsFormat($source, false) . '\');" title="'
  136. . htmlspecialchars($source) . '">' . htmlspecialchars($text) . '</dfn>';
  137. } else {
  138. return htmlspecialchars($buffer);
  139. }
  140. }
  141. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  142. /**
  143. * Gets the transformation name of the specific plugin
  144. *
  145. * @return string
  146. */
  147. public static function getName()
  148. {
  149. return "Date Format";
  150. }
  151. }