diff --git a/nginx_proxy_manager_connector.module b/nginx_proxy_manager_connector.module index f04c7c4..3f9a39b 100644 --- a/nginx_proxy_manager_connector.module +++ b/nginx_proxy_manager_connector.module @@ -2,14 +2,13 @@ /** * @file - * Functions for communicating with NginxProxyManagar instance - * - * Use create_proxy_host() to add a new proxy_host - * - * Use deploy_proxy_host() to enable the proxy_host - * - * Use disable_proxy_host() to disable the proxy_host - * + * Functions for communicating with NginxProxyManagar instance. + * + * Use createProxyHost() to add a new proxy_host. + * + * Use deployProxyHost() to enable the proxy_host + * + * Use disableProxyHost() to disable the proxy_host */ use Drupal\Core\Routing\RouteMatchInterface; @@ -28,4 +27,4 @@ function nginx_proxy_manager_connector_help($route_name, RouteMatchInterface $ro default: } -} \ No newline at end of file +} diff --git a/src/Form/NginxProxySettingsForm.php b/src/Form/NginxProxySettingsForm.php index 989ac43..b5e9bc0 100644 --- a/src/Form/NginxProxySettingsForm.php +++ b/src/Form/NginxProxySettingsForm.php @@ -63,14 +63,14 @@ class NginxProxySettingsForm extends ConfigFormBase { $form['email_nginx_proxy'] = [ '#type' => 'email', '#title' => $this->t('Email'), - '#description' => $this->t('User credentials of Nginx Proxy Manager (Update .env file to modify the value)'), + '#description' => $this->t('User credentials of Nginx Proxy Manager (Update .env file to modify the value)'), '#disabled' => TRUE, '#default_value' => $_ENV['NGINX_PROXY_MANAGER_USER_EMAIL'], ]; $form['password_nginx_proxy'] = [ '#type' => 'textfield', '#title' => $this->t('Password'), - '#description' => $this->t('User credentials of Nginx Proxy Manager (Update .env file to modify the value)'), + '#description' => $this->t('User credentials of Nginx Proxy Manager (Update .env file to modify the value)'), '#disabled' => TRUE, '#maxlength' => 64, '#size' => 64, diff --git a/src/Services/NginxProxyManagerService.php b/src/Services/NginxProxyManagerService.php index be9c96c..472d5cc 100644 --- a/src/Services/NginxProxyManagerService.php +++ b/src/Services/NginxProxyManagerService.php @@ -44,17 +44,23 @@ class NginxProxyManagerService { * Create Proxy Host in NginxProxyManager. * * @param string $custom_domain + * Add custom domain. * @param string $custom_subdomain + * Add Sub Domain. * @param string $forward_host + * Add host. * @param string $port + * Add port number. * @param string $letsencrypt_email + * Add email for certification. * - * @return array $response + * @return array + * It is returning the boolean. */ - public function create_proxy_host($custom_domain, $custom_subdomain, $forward_host = NULL, $port = NULL, $letsencrypt_email = NULL) { + public function createProxyHost($custom_domain, $custom_subdomain, $forward_host = NULL, $port = NULL, $letsencrypt_email = NULL) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not created.']; } @@ -77,14 +83,14 @@ class NginxProxyManagerService { $www_domain_name = 'www.' . $non_www_domain_name; $custom_domain_domain_names = '"' . $non_www_domain_name . '", "' . $www_domain_name . '"'; - $proxy_host_id = $this->get_proxy_host_id($non_www_domain_name); + $proxy_host_id = $this->getProxyHostId($non_www_domain_name); if ($proxy_host_id == 0) { // Create Proxy Host for custom domain. - $api_response_custom_domain = Json::decode($this->api_call_create_proxy_host_without_certificate($custom_domain_domain_names, $forward_host, $port)); + $api_response_custom_domain = Json::decode($this->apiCallCreateProxyHostWithoutCertificate($custom_domain_domain_names, $forward_host, $port)); } else { - $api_response_custom_domain = Json::decode($this->api_call_enable_proxy_host($proxy_host_id)); + $api_response_custom_domain = Json::decode($this->apiCallEnableProxyHost($proxy_host_id)); } // If error, send appropriate failure response. // Show subdomain error, if both throw error. @@ -94,17 +100,17 @@ class NginxProxyManagerService { } } - if ($this->get_proxy_host_id($custom_subdomain) == 0) { + if ($this->getProxyHostId($custom_subdomain) == 0) { $custom_subdomain_domain_names = '"' . $custom_subdomain . '"'; // Get certificate for custom subdomain and create if needed. - $this->api_call_create_certificate($custom_subdomain, $letsencrypt_email); + $this->apiCallCreateCertificate($custom_subdomain, $letsencrypt_email); - $certificate_id = $this->get_certificate_id($custom_subdomain); + $certificate_id = $this->getCertificateId($custom_subdomain); // Create Proxy Host for custom subdomain. - $api_response_custom_subdomain = Json::decode($this->api_call_create_proxy_host($custom_subdomain_domain_names, $forward_host, $certificate_id, $port)); + $api_response_custom_subdomain = Json::decode($this->apiCallCreateProxyHost($custom_subdomain_domain_names, $forward_host, $certificate_id, $port)); // If error, send appropriate failure response. // Show subdomain error, if both throw error. if (isset($api_response_custom_subdomain['error'])) { @@ -127,22 +133,25 @@ class NginxProxyManagerService { * Deploy Proxy Host in NginxProxyManager. * * @param string $custom_domain + * Call custom domain. * @param int $proxy_host_id + * Define proxy host id. * * @return bool + * It is returning boolean */ - public function deploy_proxy_host($custom_domain, $proxy_host_id = 0) { + public function deployProxyHost($custom_domain, $proxy_host_id = 0) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not enabled.']; } // If Proxy Host Id is not provided find it by custom_domain. if ($proxy_host_id == 0) { - $proxy_host_id = $this->get_proxy_host_id($custom_domain); + $proxy_host_id = $this->getProxyHostId($custom_domain); } - $response = Json::decode($this->api_call_enable_proxy_host($proxy_host_id)); + $response = Json::decode($this->apiCallEnableProxyHost($proxy_host_id)); if (isset($response['error'])) { \Drupal::logger('nginx_proxy_manager_connector')->error("Nginx Proxy Manager Error, Code:" . $response['error']['code'] . ", Message:" . $response['error']['message']); } @@ -153,21 +162,24 @@ class NginxProxyManagerService { * Disable Proxy Host in NginxProxyManager. * * @param string $custom_domain + * Call custom domain. * @param int $proxy_host_id + * Define proxy host id. * * @return bool + * It is returning boolean */ - public function disable_proxy_host($custom_domain, $proxy_host_id = 0) { + public function disableProxyHost($custom_domain, $proxy_host_id = 0) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not disabled.']; } // If Proxy Host Id is not provided find it by custom_domain. if ($proxy_host_id == 0) { - $proxy_host_id = $this->get_proxy_host_id($custom_domain); + $proxy_host_id = $this->getProxyHostId($custom_domain); } - $response = Json::decode($this->api_call_disable_proxy_host($proxy_host_id)); + $response = Json::decode($this->apiCallDisableProxyHost($proxy_host_id)); if (isset($response['error'])) { \Drupal::logger('nginx_proxy_manager_connector')->error("Nginx Proxy Manager Error, Code:" . $response['error']['code'] . ", Message:" . $response['error']['message']); } @@ -178,22 +190,27 @@ class NginxProxyManagerService { * Update Proxy Host in NginxProxyManager. * * @param string $old_custom_domain + * Call old custom domain. * @param string $new_custom_domain + * Call new custom domain. * @param int $proxy_host_id + * Call current proxy host id. * @param int $enable_ssl + * Add ssh status enable/disable. * * @return bool + * It is returning boolean. */ - public function update_proxy_host($old_custom_domain, $new_custom_domain, $proxy_host_id = 0, $enable_ssl = 0) { + public function updateProxyHost($old_custom_domain, $new_custom_domain, $proxy_host_id = 0, $enable_ssl = 0) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not updated.']; } // If Proxy Host Id is not provided find it by custom_domain. if ($old_custom_domain !== "" && $proxy_host_id == 0) { - $proxy_host_id = $this->get_proxy_host_id($old_custom_domain); + $proxy_host_id = $this->getProxyHostId($old_custom_domain); } // Set domain names. @@ -207,14 +224,14 @@ class NginxProxyManagerService { $forward_host = $_ENV['NGINX_PROXY_MANAGER_DEFAULT_FORWARD_HOST']; $port = $config->get('default_forward_host_port'); // Create Proxy Host for custom domain. - $response = Json::decode($this->api_call_create_proxy_host_without_certificate($custom_domain_domain_names, $forward_host, $port)); + $response = Json::decode($this->apiCallCreateProxyHostWithoutCertificate($custom_domain_domain_names, $forward_host, $port)); } else { - $response = Json::decode($this->api_call_update_proxy_host($custom_domain_domain_names, $proxy_host_id)); + $response = Json::decode($this->apiCallUpdateProxyHost($custom_domain_domain_names, $proxy_host_id)); } if ($enable_ssl) { - $cert_response = $this->update_proxy_host_certificate($new_custom_domain); + $cert_response = $this->updateProxyHostCertificate($new_custom_domain); if (isset($cert_response['error'])) { \Drupal::logger('nginx_proxy_manager_connector')->error("Nginx Proxy Manager Error, Code:" . $response['error']['code'] . ", Message:" . $response['error']['message']); } @@ -230,16 +247,21 @@ class NginxProxyManagerService { * Update Proxy Host in NginxProxyManager. * * @param string $custom_domain + * Get custom domain. * @param string $action + * Define the action. * @param string $letsencrypt_email + * Add user email id for letsencrypt. * @param int $proxy_host_id + * Use current proxy host id. * * @return bool + * Boolean. */ - public function update_proxy_host_certificate($custom_domain, $action = "add", $letsencrypt_email = NULL, $proxy_host_id = 0) { + public function updateProxyHostCertificate($custom_domain, $action = "add", $letsencrypt_email = NULL, $proxy_host_id = 0) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not updated.']; } if ($custom_domain == '') { @@ -253,12 +275,12 @@ class NginxProxyManagerService { if ($action == "add") { // Check if certificate exists. - $certificate_id_from_list = $this->get_certificate_id($custom_domain); + $certificate_id_from_list = $this->getCertificateId($custom_domain); // Create if it doesn't exist. if ($certificate_id_from_list == 0) { // Create certificate. - $this->api_call_create_certificate($custom_domain, $letsencrypt_email); - $certificate_id_from_list = $this->get_certificate_id($custom_domain); + $this->apiCallCreateCertificate($custom_domain, $letsencrypt_email); + $certificate_id_from_list = $this->getCertificateId($custom_domain); if ($certificate_id_from_list == 0) { return ["failure" => "Certificate not generated"]; @@ -272,10 +294,10 @@ class NginxProxyManagerService { // If Proxy Host Id is not provided find it by custom_domain. if ($proxy_host_id == 0) { - $proxy_host_id = $this->get_proxy_host_id($custom_domain); + $proxy_host_id = $this->getProxyHostId($custom_domain); } - $response = Json::decode($this->api_call_update_proxy_host_certificate($certificate_id, $proxy_host_id)); + $response = Json::decode($this->apiCallUpdateProxyHostCertificate($certificate_id, $proxy_host_id)); if (isset($response['error'])) { \Drupal::logger('nginx_proxy_manager_connector')->error("Nginx Proxy Manager Error, Code:" . $response['error']['code'] . ", Message:" . $response['error']['message']); } @@ -286,17 +308,19 @@ class NginxProxyManagerService { * Get Proxy Host Id using domain name. * * @param string $custom_domain + * Use the custom domain to get proxy host id. * * @return int + * Return int. */ - public function get_proxy_host_id($custom_domain) { + public function getProxyHostId($custom_domain) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not created.']; } // Get list of proxy hosts. - $proxy_hosts_list = Json::decode($this->api_call_get_proxy_hosts()); + $proxy_hosts_list = Json::decode($this->apiCallGetProxyHosts()); foreach ($proxy_hosts_list as $proxy_host) { if (in_array($custom_domain, $proxy_host['domain_names'])) { @@ -310,17 +334,19 @@ class NginxProxyManagerService { * Get Proxy Host Id using domain name. * * @param string $custom_domain + * Use the custom domain to get proxy host name. * * @return int + * Return int. */ - public function get_proxy_host_certificate($custom_domain) { + public function getProxyHostCertificate($custom_domain) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not created.']; } // Get list of proxy hosts. - $proxy_hosts_list = Json::decode($this->api_call_get_proxy_hosts()); + $proxy_hosts_list = Json::decode($this->apiCallGetProxyHosts()); foreach ($proxy_hosts_list as $proxy_host) { if (in_array($custom_domain, $proxy_host['domain_names'])) { @@ -334,17 +360,19 @@ class NginxProxyManagerService { * Get Certificate Id using domain name. * * @param string $custom_domain + * Use the custom domain to get certificate id. * * @return int + * Return int. */ - public function get_certificate_id($custom_domain) { + public function getCertificateId($custom_domain) { // Initiate API calls configuration. - if (!$this->api_call_init()) { + if (!$this->apiCallInit()) { return ['failure' => 'Proxy host not created.']; } // Get list of proxy hosts. - $certificates_list = Json::decode($this->api_call_get_certificates()); + $certificates_list = Json::decode($this->apiCallGetCertificates()); foreach ($certificates_list as $certificate) { if (in_array($custom_domain, $certificate['domain_names'])) { @@ -361,18 +389,19 @@ class NginxProxyManagerService { * Check Nginx Proxy Manager status and generate token. * * @return array + * Return the array. */ - public function api_call_init() { + public function apiCallInit() { // Check if Nginx Proxy Manager is online. - if (Json::decode($this->api_call_health_status())['status'] == 'OK') { + if (Json::decode($this->apiCallHealthStatus())['status'] == 'OK') { // Check if user credentials are configured. if ($_ENV['NGINX_PROXY_MANAGER_USER_EMAIL'] !== NULL && $_ENV['NGINX_PROXY_MANAGER_PASSWORD'] !== NULL) { // Generate token. - if (isset(Json::decode($this->api_call_generate_token())['token'])) { - \Drupal::state()->set('api_calls_token', Json::decode($this->api_call_generate_token())['token']); + if (isset(Json::decode($this->apiCallGenerateToken())['token'])) { + \Drupal::state()->set('api_calls_token', Json::decode($this->apiCallGenerateToken())['token']); return TRUE; } else { @@ -392,8 +421,9 @@ class NginxProxyManagerService { * API Call for checking Nginx Proxy Manager status. * * @return \Drupal\Component\Serialization\Json + * Return the json data. */ - public function api_call_health_status() { + public function apiCallHealthStatus() { // Make request and return response. return $this->httpClient->request('GET', $this->api_calls_base_url)->getBody()->getContents(); @@ -402,9 +432,10 @@ class NginxProxyManagerService { /** * API Call for generating token for authentication. * - * @return \Drupal\Component\Serialization\Json $response_json + * @return \Drupal\Component\Serialization\Json + * Return the json data. */ - public function api_call_generate_token() { + public function apiCallGenerateToken() { // Set data. $data = [ @@ -434,13 +465,18 @@ class NginxProxyManagerService { * API Call. * * @param string $domain_names + * Use domain name to create proxy host. * @param string $forward_host + * Use host. * @param int $certificate_id + * Add certificate id for ssl. * @param string $port + * Add port number. * - * @return \Drupal\Component\Serialization\Json $json + * @return \Drupal\Component\Serialization\Json + * Return the json data. */ - public function api_call_create_proxy_host($domain_names, $forward_host, $certificate_id, $port) { + public function apiCallCreateProxyHost($domain_names, $forward_host, $certificate_id, $port) { // Set data. $data = [ @@ -486,12 +522,16 @@ class NginxProxyManagerService { * API Call. * * @param string $domain_names + * Use domain name to create proxy host. * @param string $forward_host + * Use host. * @param string $port + * Add port number. * - * @return \Drupal\Component\Serialization\Json $json + * @return \Drupal\Component\Serialization\Json + * Return the json data. */ - public function api_call_create_proxy_host_without_certificate($domain_names, $forward_host, $port) { + public function apiCallCreateProxyHostWithoutCertificate($domain_names, $forward_host, $port) { // Set data. $data = [ @@ -536,9 +576,10 @@ class NginxProxyManagerService { /** * API Call. * - * @return \Drupal\Component\Serialization\Json $json + * @return \Drupal\Component\Serialization\Json + * Return the Json data. */ - public function api_call_get_proxy_hosts() { + public function apiCallGetProxyHosts() { // Make request. $response_json = $this->httpClient->request( @@ -562,10 +603,12 @@ class NginxProxyManagerService { * API Call. * * @param int $proxy_host_id + * Use the proxy host id to enable the host. * * @return bool + * Return the boolean. */ - public function api_call_enable_proxy_host($proxy_host_id) { + public function apiCallEnableProxyHost($proxy_host_id) { // Make request. $response_json = $this->httpClient->request( @@ -589,10 +632,12 @@ class NginxProxyManagerService { * API Call. * * @param int $proxy_host_id + * Use the proxy host id to enable the host. * * @return bool + * Return the boolean. */ - public function api_call_disable_proxy_host($proxy_host_id) { + public function apiCallDisableProxyHost($proxy_host_id) { // Make request. $response_json = $this->httpClient->request( 'POST', @@ -615,11 +660,14 @@ class NginxProxyManagerService { * API Call. * * @param string $domain_names + * Use domain name. * @param int $proxy_host_id + * Use proxy host id. * - * @return \Drupal\Component\Serialization\Json $json + * @return \Drupal\Component\Serialization\Json + * Return json data. */ - public function api_call_update_proxy_host($domain_names, $proxy_host_id) { + public function apiCallUpdateProxyHost($domain_names, $proxy_host_id) { // Set data. $data = ["domain_names" => ""]; $data = str_replace('"domain_names":""', '"domain_names": [' . $domain_names . ']', json_encode($data, JSON_FORCE_OBJECT)); @@ -646,11 +694,14 @@ class NginxProxyManagerService { * API Call. * * @param int $certificate_id + * Call certificate id. * @param int $proxy_host_id + * Call host id. * - * @return \Drupal\Component\Serialization\Json $json - */ - public function api_call_update_proxy_host_certificate($certificate_id, $proxy_host_id) { + * @return \Drupal\Component\Serialization\Json + * Return json data. + * */ + public function apiCallUpdateProxyHostCertificate($certificate_id, $proxy_host_id) { // Set data. $data = [ @@ -680,11 +731,14 @@ class NginxProxyManagerService { * API Call. * * @param string $custom_domain + * Use domain name. * @param string $letsencrypt_email + * Use email id. * - * @return \Drupal\Component\Serialization\Json $json + * @return \Drupal\Component\Serialization\Json + * Return json data. */ - public function api_call_create_certificate($custom_domain, $letsencrypt_email) { + public function apiCallCreateCertificate($custom_domain, $letsencrypt_email) { $non_www_domain_name = preg_replace('/^www\./', '', $custom_domain); $www_domain_name = 'www.' . $non_www_domain_name; @@ -726,9 +780,10 @@ class NginxProxyManagerService { /** * API Call for getting list og all certificates. * - * @return \Drupal\Component\Serialization\Json $json + * @return \Drupal\Component\Serialization\Json + * Return json data. */ - public function api_call_get_certificates() { + public function apiCallGetCertificates() { // Make request. $response_json = $this->httpClient->request( 'GET',