PageRenderTime 41ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/classes/Mibew/Maintenance/Utils.php

https://gitlab.com/fabiorf/chat
PHP | 144 lines | 65 code | 16 blank | 63 comment | 8 complexity | 08d708198644e6442d9074f38988fbc4 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is a part of Mibew Messenger.
  4. *
  5. * Copyright 2005-2015 the original author or authors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. namespace Mibew\Maintenance;
  20. /**
  21. * Contains a set of utility methods.
  22. */
  23. class Utils
  24. {
  25. /**
  26. * Converts version ID to human readable representation.
  27. *
  28. * Example of usage:
  29. * <code>
  30. * $version = 50303;
  31. * echo(format_version_id($version)); // Outputs "5.3.3"
  32. * </code>
  33. *
  34. * @param int $version_id Version ID
  35. * @return string Human readable version.
  36. */
  37. public static function formatVersionId($version_id)
  38. {
  39. $parts = array();
  40. $tmp = (int)$version_id;
  41. for ($i = 0; $i < 3; $i++) {
  42. $parts[] = $tmp % 100;
  43. $tmp = floor($tmp / 100);
  44. }
  45. return implode('.', array_reverse($parts));
  46. }
  47. /**
  48. * Gets list of all available updates.
  49. *
  50. * @param object|string $container Either an instance of the class that
  51. * keeps update methods or fully classified name of such class.
  52. * @return array The keys of this array are version numbers and values are
  53. * methods of the $container class that should be performed.
  54. */
  55. public static function getUpdates($container)
  56. {
  57. $updates = array();
  58. if (is_object($container)) {
  59. // If an objects is passed to the method we can use its public
  60. // static and non-static methods as updates.
  61. $methods_filter = \ReflectionMethod::IS_PUBLIC;
  62. } else {
  63. // If a class name is passed to the method we can use only its
  64. // public static methods as updates. Also we need to make sure the
  65. // class exists.
  66. if (!class_exists($container)) {
  67. throw new \InvalidArgumentException(sprintf(
  68. 'Class "%s" does not exist',
  69. $container
  70. ));
  71. }
  72. $methods_filter = \ReflectionMethod::IS_PUBLIC & \ReflectionMethod::IS_STATIC;
  73. }
  74. $container_reflection = new \ReflectionClass($container);
  75. foreach ($container_reflection->getMethods() as $method_reflection) {
  76. // Filter update methods
  77. $name = $method_reflection->getName();
  78. if (preg_match("/^update([0-9]+)(?:(Alpha|Beta|Rc)([0-9]+))?$/", $name, $matches)) {
  79. $version = self::formatVersionId($matches[1]);
  80. // Check if a beta version is defined.
  81. if (!empty($matches[2])) {
  82. $version .= sprintf('-%s.%u', strtolower($matches[2]), $matches[3]);
  83. }
  84. $updates[$version] = array(
  85. $method_reflection->isStatic() ? $container_reflection->getName() : $container,
  86. $name
  87. );
  88. }
  89. }
  90. uksort($updates, 'version_compare');
  91. return $updates;
  92. }
  93. /**
  94. * Generates random unique 64 characters length ID for Mibew instance.
  95. *
  96. * WARNING: This ID should not be used for any security/cryptographic. If
  97. * you need an ID for such purpose you have to use PHP's
  98. * {@link openssl_random_pseudo_bytes()} function instead.
  99. *
  100. * @return string
  101. */
  102. public static function generateInstanceId()
  103. {
  104. $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
  105. $rnd = (string)microtime(true);
  106. // Add ten random characters before and after the timestamp
  107. $max_char = strlen($chars) - 1;
  108. for ($i = 0; $i < 10; $i++) {
  109. $rnd = $chars[rand(0, $max_char)] . $rnd . $chars[rand(0, $max_char)];
  110. }
  111. if (function_exists('hash')) {
  112. // There is hash function that can give us 64-length hash.
  113. return hash('sha256', $rnd);
  114. }
  115. // We should build random 64 character length hash using old'n'good md5
  116. // function.
  117. $middle = (int)floor(strlen($rnd) / 2);
  118. $rnd_left = substr($rnd, 0, $middle);
  119. $rnd_right = substr($rnd, $middle);
  120. return md5($rnd_left) . md5($rnd_right);
  121. }
  122. /**
  123. * This class should not be instantiated
  124. */
  125. private function __construct()
  126. {
  127. }
  128. }