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.

130 lines
2.9 KiB

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. /**
  78. * getting vhosts
  79. *
  80. */
  81. public function find()
  82. {
  83. $results = [];
  84. // getting available
  85. $available = $this->getSitesAvailable();
  86. // getting enabled
  87. $enabled = $this->getSitesEnabled();
  88. foreach($available as $filename)
  89. {
  90. if (!in_array($filename, self::IGNORE_FILES)) {
  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. $results[] = $result;
  102. }
  103. }
  104. return $results;
  105. }
  106. }