/phpmyadmin/libraries/plugins/transformations/abs/DateFormatTransformationsPlugin.php

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