hosting.inc

General purpose Hosting module functions.

These can be used by both the frontend Hosting module and drush commands.

Functions

Namesort descending Description
hosting_format_interval Format a timestamp as a string in a friendly way.
hosting_get_hostmaster_nid Shim to preserve API stability. Deprecated. Use hosting_get_hostmaster_site_nid().
hosting_get_hostmaster_platform_nid Helper function to get the node ID of the Aegir platform.
hosting_get_hostmaster_site_nid Helper function to get the node ID of the Aegir front-end site.
hosting_path_normalize Make a path canonical.
_hosting_valid_fqdn Check if the FQDN provided is valid.
_hosting_valid_fqdn_wildcard Check if the FQDN provided is valid.
_hosting_valid_ip Check if a hostname provided is an ip address.

File

hosting.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * General purpose Hosting module functions.
  6. *
  7. * These can be used by both the frontend Hosting module and drush commands.
  8. */
  9. /**
  10. * Check if a hostname provided is an ip address.
  11. *
  12. * @param string $hostname
  13. * The hostname to check.
  14. * @return bool
  15. * TRUE is the $hostname is a valid IP address, FALSE otherwise.
  16. */
  17. function _hosting_valid_ip($hostname) {
  18. return is_string(inet_pton($hostname));
  19. }
  20. /**
  21. * Check if the FQDN provided is valid.
  22. *
  23. * @param string $fqdn
  24. * The Fully Qualified Domain Name (FQDN) to validate.
  25. * @return int
  26. * An integer greater than 0 if the $fqdn is valid, or 0 or FALSE if it not
  27. * valid.
  28. */
  29. function _hosting_valid_fqdn($fqdn) {
  30. // regex is an implementation of RFC1035, a little relaxed to allow
  31. // commonly registered hostnames (e.g. domaines starting with digits)
  32. return preg_match("/^([a-z0-9]([a-z0-9-]*[a-z0-9])?\.?)+$/i", $fqdn);
  33. }
  34. /**
  35. * Check if the FQDN provided is valid.
  36. *
  37. * This is a different function because wildcards are not part of the
  38. * RFC and may not be allowed everywhere. For example, the main site
  39. * title shouldn't have a wildcard entry.
  40. *
  41. * @param string $fqdn
  42. * The Fully Qualified Domain Name (FQDN) to validate.
  43. * @return bool
  44. * TRUE if the $fqdn is valid, or FALSE if it not valid.
  45. */
  46. function _hosting_valid_fqdn_wildcard($fqdn) {
  47. // regex is an implementation of RFC1035, but allows "star" for wildcards
  48. return preg_match("/^(\*\.)?([a-z0-9]([a-z0-9-]*[a-z0-9])?\.?)+$/i", $fqdn) &&
  49. !preg_match("/^(\*\.)[^.]*$/", $fqdn); // don't allow wildcards on TLDs
  50. }
  51. /**
  52. * Format a timestamp as a string in a friendly way.
  53. *
  54. * @param int $ts
  55. * The timestamp to format as a an interval.
  56. * @return string
  57. * Returns a string representing the given timestamp:
  58. * - If the timestamp is the current time: 'Now'.
  59. * - If the timestamp is 0 or FALSE: 'Never'
  60. * - Otherwise formatted as 'X ago' where 'X' is for example 1 year or 1
  61. * minute etc.
  62. *
  63. * @see format_interval()
  64. */
  65. function hosting_format_interval($ts) {
  66. if ($ts == REQUEST_TIME) {
  67. return t('Now');
  68. }
  69. if ($ts <= 1) {
  70. //Treats the UNIX EPOCH as never.
  71. return t('Never');
  72. }
  73. return t("@interval ago", array("@interval" => format_interval(REQUEST_TIME - $ts, 1)));
  74. }
  75. /**
  76. * Make a path canonical.
  77. *
  78. * This removes duplicate slashes, trailing slashes and /./ occurences. It does
  79. * not (yet?) resolve .. instances.
  80. */
  81. function hosting_path_normalize($path) {
  82. return rtrim(preg_replace('/(\/\/*\.)?\/\/*/', '/', $path), '/');
  83. }
  84. /**
  85. * Shim to preserve API stability. Deprecated. Use hosting_get_hostmaster_site_nid().
  86. *
  87. * @deprecated
  88. */
  89. function hosting_get_hostmaster_nid() {
  90. return hosting_get_hostmaster_site_nid();
  91. }
  92. /**
  93. * Helper function to get the node ID of the Aegir front-end site.
  94. */
  95. function hosting_get_hostmaster_site_nid() {
  96. return db_query('SELECT site.nid
  97. FROM hosting_site site
  98. JOIN hosting_package_instance pkgi
  99. ON pkgi.rid = site.nid
  100. JOIN hosting_package pkg
  101. ON pkg.nid = pkgi.package_id
  102. WHERE pkg.short_name = \'hostmaster\'')->fetchField();
  103. }
  104. /**
  105. * Helper function to get the node ID of the Aegir platform.
  106. */
  107. function hosting_get_hostmaster_platform_nid() {
  108. $hostmaster_site = node_load(hosting_get_hostmaster_site_nid());
  109. return $hostmaster_site->platform;
  110. }