OpenSource CLI-App to install and handle stuff related to Web-Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Helpers;
  3. /**
  4. * hande nginx vhosts
  5. *
  6. * @author Björn Hase, Tentakelfabrik
  7. * @license http://opensource.org/licenses/MIT The MIT License
  8. * @link https://gitea.tentakelfabrik.de/Tentakelfabrik/mcp
  9. *
  10. */
  11. class NginxVhostHelper
  12. {
  13. // path for available files
  14. const SITES_AVAILABLE = '/etc/nginx/sites-available';
  15. // path for enabled files
  16. const SITES_ENABLED = '/etc/nginx/sites-enabled';
  17. // regex to get files for
  18. const REGEX_SSL_CERTIFICATE = '/\bssl_certificate\s+\K\S+/';
  19. const REGEX_SSL_CERTIFICATE_KEY = '/\bssl_certificate_key\s+\K\S+/';
  20. // ignore files
  21. const IGNORE_FILES = [
  22. '.', '..'
  23. ];
  24. /**
  25. * get sites that are in sites-available
  26. *
  27. *
  28. * @return array
  29. *
  30. */
  31. private function getSitesAvailable()
  32. {
  33. return scandir(self::SITES_AVAILABLE);
  34. }
  35. /**
  36. * get sites that are in sites-enabled
  37. *
  38. *
  39. * @return array
  40. *
  41. */
  42. private function getSitesEnabled()
  43. {
  44. return scandir(self::SITES_ENABLED);
  45. }
  46. /**
  47. * find path for certificates in .conf-files
  48. *
  49. *
  50. * ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem;
  51. * ssl_certificate_key /etc/letsencrypt/live/<domain>/privkey.pem;
  52. *
  53. * @param string $filename
  54. *
  55. */
  56. private function getCertificate($path)
  57. {
  58. $result = [
  59. 'ssl' => false,
  60. 'ssl_certificate_exists' => false,
  61. 'ssl_certificate_expired_at' => false
  62. ];
  63. // getting .conf-file
  64. $content = file_get_contents($path);
  65. // check for path of sites
  66. preg_match(self::REGEX_SSL_CERTIFICATE, $content, $matches);
  67. preg_match(self::REGEX_SSL_CERTIFICATE_KEY, $content, $matches);
  68. // check ssl certificates
  69. if (count($matches) >= 2) {
  70. $result['ssl'] = true;
  71. if (file_exists($matches[0]) && file_exists($matches[1])) {
  72. $result['ssl_certificate_exists'] = true;
  73. }
  74. // getting expired
  75. exec('openssl x509 -noout -dates -in '.$path, $openssl);
  76. if (isset($openssl[1])) {
  77. $openssl = str_replace('notAfter=', '', $openssl[1]);
  78. }
  79. }
  80. return $result;
  81. }
  82. /**
  83. * get vhost
  84. *
  85. * @param string $filename
  86. * @param array $enabled
  87. * @return array
  88. */
  89. private function getVhost($filename, $enabled)
  90. {
  91. // getting full path
  92. $path = self::SITES_AVAILABLE.'/'.$filename;
  93. // getting certificates from a configuration
  94. $certificate = $this->getCertificate($path);
  95. $result = array_merge([
  96. 'domain' => str_replace('.conf', '', $filename),
  97. 'path' => $path,
  98. 'file' => $filename,
  99. 'enabled' => in_array($filename, $enabled),
  100. ], $certificate);
  101. return $result;
  102. }
  103. /**
  104. * find single vhost by filename
  105. *
  106. * @param string $filename
  107. * @return array
  108. */
  109. public function findOneByFilename($filename)
  110. {
  111. // getting enabled
  112. $enabled = $this->getSitesEnabled();
  113. return $this->getVhost($filename, $enabled);
  114. }
  115. /**
  116. * find all vhost
  117. *
  118. * @return array
  119. */
  120. public function find()
  121. {
  122. $results = [];
  123. // getting available
  124. $available = $this->getSitesAvailable();
  125. // getting enabled
  126. $enabled = $this->getSitesEnabled();
  127. foreach($available as $filename)
  128. {
  129. if (!in_array($filename, self::IGNORE_FILES)) {
  130. $results[] = $this->getVhost($filename, $enabled);
  131. }
  132. }
  133. return $results;
  134. }
  135. }