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.

141 lines
3.1 KiB

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. *
  5. *
  6. *
  7. *
  8. */
  9. class NginxVhostHelper
  10. {
  11. // path for available files
  12. const SITES_AVAILABLE = '/etc/nginx/sites-available';
  13. // path for enabled files
  14. const SITES_ENABLED = '/etc/nginx/sites-enabled';
  15. // regex to get files for
  16. const REGEX_SSL_CERTIFICATE = '/\bssl_certificate\s+\K\S+/';
  17. const REGEX_SSL_CERTIFICATE_KEY = '/\bssl_certificate_key\s+\K\S+/';
  18. // ignore files
  19. const IGNORE_FILES = [
  20. '.', '..'
  21. ];
  22. /**
  23. *
  24. *
  25. *
  26. * @return array
  27. *
  28. */
  29. private function getSitesAvailable()
  30. {
  31. return scandir(self::SITES_AVAILABLE);
  32. }
  33. /**
  34. *
  35. *
  36. *
  37. * @return array
  38. *
  39. */
  40. private function getSitesEnabled()
  41. {
  42. return scandir(self::SITES_ENABLED);
  43. }
  44. /**
  45. * find path for certificates in .conf-files
  46. *
  47. *
  48. * ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem;
  49. * ssl_certificate_key /etc/letsencrypt/live/<domain>/privkey.pem;
  50. *
  51. */
  52. private function getCertificate($path)
  53. {
  54. $result = [
  55. 'ssl' => false,
  56. 'ssl_certificate_exists' => false,
  57. 'ssl_certificate_expired_at' => false
  58. ];
  59. // getting .conf-file
  60. $content = file_get_contents($path);
  61. // check for path of sites
  62. preg_match(self::REGEX_SSL_CERTIFICATE, $content, $matches);
  63. preg_match(self::REGEX_SSL_CERTIFICATE_KEY, $content, $matches);
  64. // check for ssl certificates
  65. if (count($matches) >= 2) {
  66. $result['ssl'] = true;
  67. if (file_exists($matches[0]) && file_exists($matches[1])) {
  68. $result['ssl_certificate_exists'] = true;
  69. }
  70. exec('openssl x509 -noout -dates -in '.$path, $openssl);
  71. if (isset($openssl[1])) {
  72. $openssl = str_replace('notAfter=', '', $openssl[1]);
  73. }
  74. }
  75. return $result;
  76. }
  77. private function getVhost($filename, $enabled)
  78. {
  79. // getting full path
  80. $path = self::SITES_AVAILABLE.'/'.$filename;
  81. // getting certificates from a configuration
  82. $certificate = $this->getCertificate($path);
  83. $result = array_merge([
  84. 'domain' => str_replace('.conf', '', $filename),
  85. 'path' => $path,
  86. 'file' => $filename,
  87. 'enabled' => in_array($filename, $enabled),
  88. ], $certificate);
  89. return $result;
  90. }
  91. public function findOneByFilename($filename)
  92. {
  93. // getting enabled
  94. $enabled = $this->getSitesEnabled();
  95. return $this->getVhost($filename, $enabled);
  96. }
  97. /**
  98. * getting vhosts
  99. *
  100. */
  101. public function find()
  102. {
  103. $results = [];
  104. // getting available
  105. $available = $this->getSitesAvailable();
  106. // getting enabled
  107. $enabled = $this->getSitesEnabled();
  108. foreach($available as $filename)
  109. {
  110. if (!in_array($filename, self::IGNORE_FILES)) {
  111. $results[] = $this->getVhost($filename, $enabled);
  112. }
  113. }
  114. return $results;
  115. }
  116. }