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.

192 lines
4.5 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
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, $certificates);
  67. $matches = array_merge([], $certificates);
  68. preg_match(self::REGEX_SSL_CERTIFICATE_KEY, $content, $certificates);
  69. $matches = array_merge($matches, $certificates);
  70. // check ssl certificates
  71. if (count($matches) >= 2) {
  72. $result['ssl'] = true;
  73. // @TODO find a regex that ignore the ";"
  74. foreach($matches as $index => $match) {
  75. $matches[$index] = str_replace(';', '', $match);
  76. }
  77. if (file_exists($matches[0]) && file_exists($matches[1])) {
  78. $result['ssl_certificate_exists'] = true;
  79. // getting expired
  80. exec('openssl x509 -noout -dates -in '.$matches[0], $openssl);
  81. if (isset($openssl[1])) {
  82. $result['ssl_certificate_expired_at'] = str_replace('notAfter=', '', $openssl[1]);
  83. }
  84. }
  85. }
  86. return $result;
  87. }
  88. /**
  89. *
  90. * @return
  91. */
  92. private function getRedirect($path, $domain)
  93. {
  94. // getting .conf-file
  95. $content = file_get_contents($path);
  96. // result
  97. $result = false;
  98. preg_match('/server_name www.'.$domain.'/', $content, $matches);
  99. if (count($matches) > 0) {
  100. $result = true;
  101. }
  102. return $result;
  103. }
  104. /**
  105. * get vhost
  106. *
  107. * @param string $filename
  108. * @param array $enabled
  109. * @return array
  110. */
  111. private function getVhost($filename, $enabled)
  112. {
  113. // getting full path
  114. $path = self::SITES_AVAILABLE.'/'.$filename;
  115. // getting certificates from a configuration
  116. $certificate = $this->getCertificate($path);
  117. // domain
  118. $domain = str_replace('.conf', '', $filename);
  119. $result = array_merge([
  120. 'domain' => $domain,
  121. 'path' => $path,
  122. 'file' => $filename,
  123. 'redirect_www' => $this->getRedirect($path, $domain),
  124. 'enabled' => in_array($filename, $enabled),
  125. ], $certificate);
  126. return $result;
  127. }
  128. /**
  129. * find single vhost by filename
  130. *
  131. * @param string $filename
  132. * @return array
  133. */
  134. public function findOneByFilename($filename)
  135. {
  136. // getting enabled
  137. $enabled = $this->getSitesEnabled();
  138. return $this->getVhost($filename, $enabled);
  139. }
  140. /**
  141. * find all vhost
  142. *
  143. * @return array
  144. */
  145. public function find()
  146. {
  147. $results = [];
  148. // getting available
  149. $available = $this->getSitesAvailable();
  150. // getting enabled
  151. $enabled = $this->getSitesEnabled();
  152. foreach($available as $filename)
  153. {
  154. if (!in_array($filename, self::IGNORE_FILES)) {
  155. $results[] = $this->getVhost($filename, $enabled);
  156. }
  157. }
  158. return $results;
  159. }
  160. }