diff --git a/rest_api_authentication.install b/rest_api_authentication.install index bed242d12aac417160adfa46cb741615bb4c38ea..abef40279368207bb4ca236d4a4d6084937b5819 100644 --- a/rest_api_authentication.install +++ b/rest_api_authentication.install @@ -7,19 +7,7 @@ use Drupal\rest_api_authentication\MiniorangeAPIAuthfeedback; use Drupal\rest_api_authentication\Utilities; function rest_api_authentication_uninstall(){ - - $config = \Drupal::config('rest_api_authentication.settings'); - if(!( $config->get('miniorange_rest_api_authentication_uninstall_status') == 0)){ - $config->set('miniorange_rest_api_authentication_uninstall_status',0)->save(); - } - if ($config->get('miniorange_rest_api_authentication_uninstall_status') === 0) - { - $drupal_is_cli = Utilities::drupal_is_cli(); - if( !$drupal_is_cli ) - { - MiniorangeAPIAuthfeedback::rest_api_authentication_feedback_form(); - } - } + Utilities::restAPIAuthenticationFeedbackFunc(); } @@ -33,4 +21,4 @@ function rest_api_authentication_install(){ $config->set('miniorange_rest_api_authentication_uninstall_status',0); $config->set('miniorange_rest_api_installation_time_ref', time()); $config->save(); -} \ No newline at end of file +} diff --git a/rest_api_authentication.module b/rest_api_authentication.module index 682588c0502600d84895dcf4bd13afcae2933ca1..ad932c8f82db5f8e9010ec59ee0685e296682076 100644 --- a/rest_api_authentication.module +++ b/rest_api_authentication.module @@ -50,5 +50,116 @@ function rest_api_authentication_form_alter(&$form, FormStateInterface $form_sta if($form_id === 'rest_api_authentication_config_client'){ $form['api_auth']['rest_api_authentication_authentication_method_submit'] = [ '#disabled' => TRUE ]; } + + if($form_id == 'system_modules_uninstall_confirm_form') { + if (in_array('REST & JSON API Authentication', $form['modules']['#items'])) { + $site_mail = \Drupal::currentUser()->getEmail(); + $admin_email = $username = \Drupal::config('rest_api_authentication.settings')->get('rest_api_authentication_customer_admin_email'); + $email = empty($admin_email) ? $site_mail : $admin_email; + $form['#attached']['library'][] = 'rest_api_authentication/rest_api_authentication.basic_style_settings'; + + $form['uninstall_field_set'] = array( + '#type' => 'fieldset', + ); + + $form['uninstall_field_set']['uninstall_form_markup'] = [ + '#markup' => t('<span class="mo_rest_api_highlight_background_note_1">Hey, it seems like you want to uninstall miniOrange REST & JSON API Authentication module. Tell us how could we do better?</span><br>'), + ]; + + $form['uninstall_field_set'] ['uninstall_email'] = [ + '#prefix' => '<br>', + '#type' => 'email', + '#title' => t('Email <span style="color: #FF0000">*</span>'), + '#default_value' => $email, + '#attributes' => [ + 'placeholder' => 'Please enter an email', + ], + '#states' => [ + 'visible' => [ + ':input[name="skip"]' => ['checked' => false], + ], + ], + + ]; + + $form['uninstall_field_set'] ['container'] = array( + '#type' => 'container', + '#states' => [ + 'visible' => [ + ':input[name="skip"]' => ['checked' => false], + ], + ] + ); + $form['uninstall_field_set'] ['container']['uninstall_reason'] = [ + '#type' => 'radios', + '#title' => t('Reason <span style="color: #FF0000">*</span>'), + '#options' => [ + 'Module is not working' => t('Module is not working'), + 'Basic Authentication Not Working' => t('Basic Authentication Not Working'), + 'API key Authentication Not Working' => t('API key Authentication Not Working'), + 'Does not have the features I\'m looking for' => t('Does not have the features I\'m looking for'), + 'Confusing interface' => t('Confusing interface'), + 'Bugs in the module' => t('Bugs in the module'), + 'Other reasons' => t('Other reasons'), + ], + '#attributes' => [ + 'name' => 'uninstall_reason', + ], + + ]; + + $form['uninstall_field_set'] ['container']['uninstall_other_reason'] = [ + '#type' => 'textfield', + '#attributes' => [ + 'id' => 'custom-colour', + 'placeholder' => t('Specify your other reason'), + ], + '#states' => [ + 'visible' => [ + ':input[name="uninstall_reason"]' => ['value' => 'Other reasons'], + ], + ], + ]; + + $form['uninstall_field_set'] ['uninstall_skip'] = array( + '#type' => 'checkbox', + '#title' => t('Skip the feedback'), + '#attributes' => [ + 'name' => 'skip', + ], + '#default_value' => false, + ); + + $form['#validate'][] = 'uninstall_custom_validate_rest_api'; + } + } return $form; } + +function uninstall_custom_validate_rest_api(&$form, FormStateInterface &$form_state) { + $post = \Drupal::request()->request->all(); + $skip = isset($post['skip']) ? 1 : 0 ; + if(!$skip) { + $email = $form_state->getValue('uninstall_email'); + $reason = $form_state->getValue('uninstall_reason'); + + if(!isset($reason) && empty($email)) { + $form_state->setErrorByName( + 'uninstall_email', + t('Email field is required')); + $form_state->setErrorByName( + 'uninstall_reason', + t('Reason field is required')); + } + else if(!isset($reason)) { + $form_state->setErrorByName( + 'uninstall_reason', + t('Reason field is required')); + } + else if (empty($email)) { + $form_state->setErrorByName( + 'uninstall_email', + t('Email field is required')); + } + } +} diff --git a/rest_api_authentication.routing.yml b/rest_api_authentication.routing.yml index 0ec0b866c095a72e01f0db82e313e9a6f126fb59..e81a0664e610160c15f086c38a83c6a2cb689a5e 100644 --- a/rest_api_authentication.routing.yml +++ b/rest_api_authentication.routing.yml @@ -25,13 +25,3 @@ rest_api_authentication.request_trial: _permission: 'administer site configuration' -#Route for feedback -rest_api_authentication.feedback: - path: /feedback - defaults: - _controller: '\Drupal\rest_api_authentication\Controller\rest_api_authenticationController::miniorange_API_Auth_feedback' - requirements: - _permission: 'administer site configuration' - - - diff --git a/src/API_Authentication_API_Token.php b/src/API_Authentication_API_Token.php index f7bcd33795fae031e42323545aa8d1d8b17ea93f..cd9fe26f830a8efdc6d677d8729bc5b6f491ad08 100644 --- a/src/API_Authentication_API_Token.php +++ b/src/API_Authentication_API_Token.php @@ -16,11 +16,11 @@ class API_Authentication_API_Token { $authorization_header = $request->headers->get('AUTHORIZATION') !=null ? $request->headers->get('AUTHORIZATION') : $request->headers->get('AUTHORISATION'); if(empty($authorization_header) || $authorization_header == ""){ - $api_error = array('status' => 'error', 'code' => '401', 'error_description' => 'MISSING AUTHORIZATION HEADER'); + $api_error = array('status' => 'error', 'error' => 'MISSING_AUTHORIZATION_HEADER', 'error_description' => 'Authorization header not received'); return $api_error; } if (!preg_match('/\Basic\b/', $authorization_header)) { - $api_error = array('status' => 'error', 'code' => '400', 'error_description' => 'INVALID AUTHORIZATION HEADER TOKEN TYPE'); + $api_error = array('status' => 'error', 'error' => 'INVALID_AUTHORIZATION_HEADER_TOKEN_TYPE', 'error_description' => 'Authorization header must be the type of Basic.'); return $api_error; } @@ -40,11 +40,11 @@ class API_Authentication_API_Token { if(\Drupal::config('rest_api_authentication.settings')->get('api_token') != $api_key ){ $config->set('miniorange_api_key_authentication_tried', "Failed")->save(); - $api_error = array('status' => 'error', 'code' => '401', "error" => "INVALID_API_KEY", 'error_description' => 'Sorry, you are using invalid API Key'); + $api_error = array('status' => 'error',"error" => "INVALID_API_KEY", 'error_description' => 'Sorry, you are using invalid API Key'); } else if(empty($user)){ $config->set('miniorange_api_key_authentication_tried', "Failed")->save(); - $api_error = array('status' => 'error', 'code' => '401', "error" => "USER_DOES_NOT_EXIST", 'error_description' => 'The user does not exists'); + $api_error = array('status' => 'error', "error" => "USER_DOES_NOT_EXIST", 'error_description' => 'The user does not exists'); } else{ $api_error['status'] = 'SUCCESS'; @@ -54,12 +54,12 @@ class API_Authentication_API_Token { } else{ $config->set('miniorange_api_key_authentication_tried', "Failed")->save(); - $api_error = array('status' => 'error', 'code' => '401', "error" => "INVALID_AUTHORIZATION_HEADER", 'error_description' => 'The authorization header seems to be invalid'); + $api_error = array('status' => 'error', "error" => "INVALID_AUTHORIZATION_HEADER", 'error_description' => 'The authorization header seems to be invalid'); } } else{ $config->set('miniorange_api_key_authentication_tried', "Failed")->save(); - $api_error = array('status' => 'error', 'code' => '401', "error" => "MISSING_AUTHORIZATION_HEADER", 'error_description' => 'The Authorization header is missing from the request.'); + $api_error = array('status' => 'error', "error" => "MISSING_AUTHORIZATION_HEADER", 'error_description' => 'The Authorization header is missing from the request.'); } return $api_error; } diff --git a/src/API_Authentication_Basic_Auth.php b/src/API_Authentication_Basic_Auth.php index 03eeef905d237ca229459e90d3b13095f1e23dd8..666438bb664c58b14db9a2a0ff0b7de57ffc3f6b 100644 --- a/src/API_Authentication_Basic_Auth.php +++ b/src/API_Authentication_Basic_Auth.php @@ -19,11 +19,11 @@ class API_Authentication_Basic_Auth { $authorization_header = Html::escape($authorization_header); //html::escape() is used to filtering or escaping or XSS vulnerabilities if(empty($authorization_header) || $authorization_header == ""){ - $api_error = array('status' => 'error', 'code' => '401', 'error_description' => 'MISSING AUTHORIZATION HEADER'); + $api_error = array('status' => 'error', 'error' => 'MISSING_AUTHORIZATION_HEADER', 'error_description' => 'Authorization header not received.'); return $api_error; } if (!preg_match('/\Basic\b/', $authorization_header)) { - $api_error = array('status' => 'error', 'code' => '400', 'error_description' => 'INVALID AUTHORIZATION HEADER TOKEN TYPE'); + $api_error = array('status' => 'error', 'error' => 'INVALID_AUTHORIZATION_HEADER_TOKEN_TYPE', 'error_description' => 'Authorization header must be the type of Basic.'); return $api_error; } $authorization_header_array = explode( " ", $authorization_header ); @@ -35,14 +35,14 @@ class API_Authentication_Basic_Auth { if( isset($creds[0]) && isset($creds[1]) ) { $name = $creds[0]; if(empty($name)){ - $api_error = array('status' => 'error', 'code' => '400', "error" => "MISSING_USERNAME", 'error_description' => 'Username Not Found'); + $api_error = array('status' => 'error', "error" => "MISSING_USERNAME", 'error_description' => 'Username Not Found'); return $api_error; } $pwd = $creds[1]; if(! ( \Drupal::service('user.auth')->authenticate( $name, $pwd ) ) ){ $config->set('miniorange_basic_authentication_tried', "Failed")->save(); - $api_error = array('status' => 'error', 'code' => '400', "error" => "INVALID_CREDENTIALS", 'error_description' => 'Invalid username or password'); + $api_error = array('status' => 'error', "error" => "INVALID_CREDENTIALS", 'error_description' => 'Invalid username or password'); return $api_error; } $user = user_load_by_name($name); @@ -54,7 +54,7 @@ class API_Authentication_Basic_Auth { else{ $config->set('miniorange_basic_authentication_tried', "Failed")->save(); - $api_error = array('status' => 'error', 'code' => '401', "error" => "INCOMPLETE_REQUEST", 'error_description' => 'Incomplete request'); + $api_error = array('status' => 'error', "error" => "INCOMPLETE_REQUEST", 'error_description' => 'Incomplete request'); } return $api_error; } diff --git a/src/Controller/rest_api_authenticationController.php b/src/Controller/rest_api_authenticationController.php index 26d5d6bfc43119757997542e248e220e88baa8b2..0be26bfc89e8612569435f954725feba17d69a3e 100644 --- a/src/Controller/rest_api_authenticationController.php +++ b/src/Controller/rest_api_authenticationController.php @@ -36,111 +36,4 @@ class rest_api_authenticationController extends ControllerBase { return $response; } - /** - * sends feedback mail to drupalsupport - */ - public function miniorange_API_Auth_feedback(){ - - global $base_url; - $reason=""; - if(isset($_GET['query']) && trim($_GET['query']!="")){ - $reason=$_GET['query']; - } - else{ - $reason = "Not Specified"; - } - - $query_feedback = $_GET['query_feedback']; - - $message = 'Reason: ' . $reason . '<br>' . 'Feedback: ' . $query_feedback; - - $config = \Drupal::config('rest_api_authentication.settings'); - if (isset($_GET['rest_feedback_submit']) || isset($_GET['rest_feedback_skip'])) { - $module_info = \Drupal::service('extension.list.module')->getExtensionInfo('rest_api_authentication'); - $module_version = $module_info['version']; - $_SESSION['mo_other'] = "False"; - $url = MiniorangeApiAuthConstants::BASE_URL . '/moas/api/notify/send'; - - if (isset($_GET['rest_feedback_skip']) && !empty($_GET['rest_feedback_skip'])) { - - Utilities::skipped_feedback(); - - } else { - - $config = \Drupal::config('rest_api_authentication.settings'); - $email = $config->get('rest_api_authentication_customer_admin_email'); - - if (empty($email)) - $email = $_GET['rest_feedback_email']; - - $customerKey = $config->get('rest_api_authentication_customer_id'); - $apikey = $config->get('rest_api_authentication_customer_api_key'); - if ($customerKey == '') { - $customerKey = "16555"; - $apikey = "fFd2XcvTGDemZvbw1bcUesNJWEqKbbUq"; - } - - $basicAuthTried = $config->get('miniorange_basic_authentication_tried'); - $apikeyAuthTried = $config->get('miniorange_api_key_authentication_tried'); - $licensePageVisited = $config->get('miniorange_rest_api_license_page_visited'); - $triedAuthMethods = ($basicAuthTried == 'Did not Try') ? 'None' : '<br>Basic Auth: ' . $basicAuthTried . '</br>'; - - if (!str_contains($triedAuthMethods, 'None')) { - $triedAuthMethods .= $apikeyAuthTried !== 'Did not Try' ? '<br>Api Key Auth: ' . $apikeyAuthTried . '</br>' : ''; - } else { - $triedAuthMethods = $apikeyAuthTried !== 'Did not Try' ? '<br>Api Key Auth: ' . $apikeyAuthTried . '<br>' : 'None'; - } - - $skipped = isset($_GET['rest_feedback_skip']) ? TRUE : FALSE; - $add_skip = $skipped ? "<b>Skipped: True</b><br><br>" : ""; - - $users_OS = Utilities::getUsersOS(); - - $installed_on = $config->get('miniorange_rest_api_installation_time_ref'); - $installed_date = date('d/m/Y H:i:s', $installed_on); - $current_time_in_ms = Utilities::get_timestamp(); - $stringToHash = $customerKey . $current_time_in_ms . $apikey; - $hashValue = hash("sha512", $stringToHash); - - $fromEmail = $email; - $subject = 'Drupal ' . \DRUPAL::VERSION . ' REST API Authentication Module Feedback | ' . $module_version . ' | PHP Version ' . phpversion(); - $query = '[Drupal ' . \DRUPAL::VERSION . ' REST API Authentication | ' . $module_version . ' | PHP Version ' . phpversion() . ' ]: ' . $message; - $content = '<div >Hello, <br><br>Company :<a href="' . $base_url . '" target="_blank" >' . $base_url . '</a><br><br>Email :<a href="mailto:' . $fromEmail . '" target="_blank">' . $fromEmail . '</a><br><br>Installed on: ' . $installed_date . '<br><br>Operating System:' . $users_OS . '<br><br>Payment Page Visited: ' . $licensePageVisited . '<br><br>Tried Authentication Methods: ' . $triedAuthMethods . '<br><br>' . $add_skip . 'Query: ' . $query . '</div>'; - $fields = array( - 'customerKey' => $customerKey, - 'sendEmail' => true, - 'email' => array( - 'customerKey' => $customerKey, - 'fromEmail' => $fromEmail, - 'fromName' => 'miniOrange', - 'toEmail' => 'drupalsupport@xecurify.com', - 'toName' => 'drupalsupport@xecurify.com', - 'subject' => $subject, - 'content' => $content - ), - ); - - $field_string = json_encode($fields); - - - $header = ['Content-Type' => 'application/json', 'Customer-Key' => $customerKey, 'Timestamp' => $current_time_in_ms, 'Authorization' => $hashValue]; - - try { - $response = \Drupal::httpClient()->post($url, ['headers' => $header, 'body' => $field_string, 'verify' => FALSE]); - - - } catch (Exception $exception) { - - } - - } - } - - \Drupal::configFactory()->getEditable('rest_api_authentication.settings')->clear('miniorange_rest_api_authentication_uninstall_status')->save(); - \Drupal::service('module_installer')->uninstall(['rest_api_authentication']); - $uninstall_redirect = $base_url . '/admin/modules'; - \Drupal::messenger()->addMessage('The module has been successfully uninstalled.'); - return new RedirectResponse($uninstall_redirect); - - } -} + } diff --git a/src/Form/miniorangeAPIAuth.php b/src/Form/miniorangeAPIAuth.php index f5307119f01608943587472616dc7a953641679f..9f081a52771a149732d554a05ece196dff784e07 100644 --- a/src/Form/miniorangeAPIAuth.php +++ b/src/Form/miniorangeAPIAuth.php @@ -82,7 +82,7 @@ class miniorangeAPIAuth extends FormBase { \Drupal::configFactory()->getEditable('rest_api_authentication.settings')->set('enable_authentication',$enable_authentication)->save(); \Drupal::messenger()->addMessage(t('Settings Saved Successfully.')); - $response = new RedirectResponse($base_url."/admin/config/people/rest_api_authentication/auth_settings/?tab=edit-api-auth"); + $response = new RedirectResponse($base_url."/admin/config/people/rest_api_authentication/auth_settings?tab=edit-api-auth"); $response->send(); return; } diff --git a/src/MiniorangeAPIAuthfeedback.php b/src/MiniorangeAPIAuthfeedback.php deleted file mode 100644 index 5e8f1a5240be1736fabaf3a88e51807f2f094878..0000000000000000000000000000000000000000 --- a/src/MiniorangeAPIAuthfeedback.php +++ /dev/null @@ -1,173 +0,0 @@ -<?php -namespace Drupal\rest_api_authentication; -use Drupal\rest_api_authentication\utilities; - -class MiniorangeAPIAuthfeedback{ - -public static function rest_api_authentication_feedback_form(){ - - - global $base_url; - $feedback_url = $base_url.'/feedback'; - $_SESSION['mo_other'] = 'True'; - $form_id = $_POST['form_id']; - $form_token = $_POST['form_token']; - $admin_email=Utilities::getCustomerEmail(); -?> -<html> -<head> -<link href="https://fonts.googleapis.com/css?family=PT+Serif" rel="stylesheet"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> - <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> - <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script> -<style> - - .container{ - font-family: sans-serif; - } -/* h4.modal-title { - font-size: 18px; -} - p { - padding-top: 20px; - text-align: left; - margin-left: 10px; -}*/ -input#rest_feedback_email { - margin-left: 7px; - width: 125%; -} -/* .modal-header { - margin-bottom: -11px; -} */ - .rest_loader { - margin: auto; - display: block; - border: 5px solid #f3f3f3; /* Light grey */ - border-top: 5px solid #3498db; /* Blue */ - border-radius: 50%; - width: 50px; - height: 50px; - animation: spin 2s linear infinite; - } - - @keyframes spin { - 0% { transform: rotate(0deg); } - 100% { transform: rotate(360deg); } - } - </style> - <script> - $(document).ready(function () { - $("#myModal").modal({ - backdrop: 'dynamic', - keyboard: TRUE - }); - }); - - $(function () { - $(".button").click(function () { - document.getElementById('rest_loader').style.display = 'block'; - var reason = $("input[name='performance']:checked").val(); - var q_feedback = document.getElementById("sso_feedback").value; - return false; - }); - }) - - </script> -</head> -<body> - <div class="container" style="background: rgba(0, 0, 0, 0.1);width:100%;" > - <div class="modal_fade" id="myModal" role="dialog" > - <div class="modal-dialog" style="width: 500px;" role="dialog" > - <div class="modal-content" style="border-radius: 20px;" > - <div class="modal-header" style="padding: 25px; border-top-left-radius: 20px; border-top-right-radius: 20px; background-color: #8fc1e3;"> - <h4 class="modal-title" style="color: white; text-align: center;">Hey, it seems like you want to deactivate Rest API Authentication Module</h4> - <hr> - <h4 style="text-align: center; color: white;" class="modal-title">What happened?</h4> - </div> - <div class="modal-body" style="font-size: 11px; padding-right: 25px; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; background-color: #ececec;"> - <form action="<?php echo $feedback_url; ?>" id="restapi_feedback"> - <div> - <p> - <?php - if(empty(\Drupal::config('rest_api_authentication.settings')->get('rest_api_authentication_customer_admin_email'))) { ?> - <br><label style="font-size: 12.5px">Email ID:<label> <input onblur="validateEmail(this)" class="form-control" - type="email" id="rest_feedback_email" required value= <?php echo $admin_email; ?> - name="rest_feedback_email"/> - <p style="display: none;color:red" id="email_error">Invalid Email</p> - <?php - } ?> - <br> - <?php - $deactivate_reasons = array( - t("Not Working"), - t("Basic Authentication Not Working"), - t("API key Authentication Not Working"), - t("Does not have the features I'm looking for"), - t("Confusing interface"), - t("Bugs in the module"), - t("Other reasons: "), - ); - foreach ($deactivate_reasons as $deactivate_reasons) { - ?> - <div class="radio" style="padding:2px;font-size: 8px;text-align:left"> - <label style="font-weight:normal;font-size:14.6px;color:maroon;" for="<?php echo $deactivate_reasons; ?>"> - <input type="radio" name="query" value="<?php echo $deactivate_reasons;?>" required> - <?php echo $deactivate_reasons; ?> - </label> - </div> - <?php } ?> - <input type="hidden" name="form_token" value=<?php echo $form_token ?> > - <input type="hidden" name="form_id" value= <?php echo $form_id ?>> - <br> - <textarea class="form-control" id="query_feedback" name="query_feedback" rows="4" cols="50" style="margin-left:2%;text-align:left" placeholder="Write your query here"></textarea> - <br><br> - <div class="mo2f_modal-footer" style="margin-bottom: 5% !important;"> - <input type="submit" id="submit_button" name="rest_feedback_submit" class="button btn btn-primary" value="Submit and Continue" style=" display: block; font-size: 11px;float: left; margin-left: 21px;margin-bottom: 15%;" /> - <input type="submit" formnovalidate="formnovalidate" style="margin: auto; display: block; font-size: 11px; float: right;" name="rest_feedback_skip" class="btn btn-link" value="Skip" /> - </div> - <div class="rest_loader" id="rest_api_feedback" style="display: none;"></div> - <?php - echo "<br><br>"; - foreach($_POST as $key => $value) { - self::hiddenRestapifields($key,$value); - } - ?> - </div> - </form> - </div> - - </div> - </div> - </div> - </div> - </div> -</body> -</html> -<?php -exit; - - } - - public static function hiddenRestapiFields($key,$value) - { - $hiddenRestapiField = ""; - $value2 = array(); - if(is_array($value)) { - foreach($value as $key2 => $value2) - { - if(is_array($value2)){ - self::hiddenRestapiFields($key."[".$key2."]",$value2); - } else { - $hiddenRestapiField = "<input type='hidden' name='".$key."[".$key2."]"."' value='".$value2."'>"; - } - } - }else{ - $hiddenRestapiField = "<input type='hidden' name='".$key."' value='".$value."'>"; - } - - echo $hiddenRestapiField; - } -} - diff --git a/src/Utilities.php b/src/Utilities.php index d3b123e6c027490d95391a2d75d78ac477e80408..d950ea1474ae0152d4832979bcf1af5f67367604 100644 --- a/src/Utilities.php +++ b/src/Utilities.php @@ -161,12 +161,19 @@ class Utilities { return !isset($server_software) && (php_sapi_name() == 'cli' || (is_numeric($server_argc) && $server_argc > 0)) ? true : false; } - public static function skipped_feedback(){ + public static function skippedFeedback(){ $module_info = \Drupal::service('extension.list.module')->getExtensionInfo('rest_api_authentication'); $modules_version = $module_info['version']; $url = MiniorangeApiAuthConstants::BASE_URL . '/moas/api/notify/send'; $config = \Drupal::config('rest_api_authentication.settings'); - $email = \Drupal::config('system.site')->get('mail'); + $email = ''; + $email = $config->get('rest_api_authentication_customer_admin_email'); + $current_user = 'there'; + if($email == null){ + $email = \Drupal::currentUser()->getEmail(); + $current_user = \Drupal::currentUser()->getDisplayName(); + $current_user = !empty($current_user) ? $current_user : 'there'; + } $customerKey = $config->get('rest_api_authentication_customer_id'); $apikey = $config->get('rest_api_authentication_customer_api_key'); if($customerKey==''){ @@ -179,9 +186,6 @@ class Utilities { $fromEmail = 'no-reply@xecurify.com'; $subject = 'Regarding miniOrange ' . \DRUPAL::VERSION . ' REST API Authentication Module - '.$modules_version.' Feedback'; - $current_user = \Drupal::currentUser()->getDisplayName(); - $current_user = !empty($current_user) ? $current_user : 'there'; - $content = '<div>Hello '.$current_user.', <br><br>Thank you for showing interest in the miniOrange Drupal REST API Authentication module. @@ -218,4 +222,91 @@ class Utilities { } } + public static function restAPIAuthenticationFeedbackFunc(){ + global $base_url; + $post = \Drupal::request()->request->all(); + $skip = isset($post['skip']) ? 1 : 0 ; + $is_cli = Utilities::drupal_is_cli(); + $config = \Drupal::config('rest_api_authentication.settings'); + $site_mail = \Drupal::currentUser()->getEmail(); + $modules_info = \Drupal::service('extension.list.module')->getExtensionInfo('rest_api_authentication'); + $customerKey = $config->get('rest_api_authentication_customer_id'); + $fromEmail = ''; + if(isset($post['uninstall_email']) && !empty($post['uninstall_email'])) { + $fromEmail = $post['uninstall_email']; + } + else { + $admin_email = \Drupal::config('rest_api_authentication.settings')->get('rest_api_authentication_customer_admin_email'); + $fromEmail = empty($admin_email) ? $site_mail : $admin_email; + } + $apikey = $config->get('rest_api_authentication_customer_api_key'); + if ($customerKey == '') { + $customerKey = "16555"; + $apikey = "fFd2XcvTGDemZvbw1bcUesNJWEqKbbUq"; + } + $module_version = $modules_info['version']; + + if( $skip || $is_cli){ + Utilities::skippedFeedback(); + }else{ + $url = MiniorangeApiAuthConstants::BASE_URL. '/moas/api/notify/send'; + $reason = ''; + if(isset($post['uninstall_reason'])) { + $reason = $post['uninstall_reason']; + } + + $q_feedback = $post['uninstall_other_reason'] ?? '-'; + $basicAuthTried = $config->get('miniorange_basic_authentication_tried'); + $apikeyAuthTried = $config->get('miniorange_api_key_authentication_tried'); + $licensePageVisited = $config->get('miniorange_rest_api_license_page_visited'); + $users_OS = Utilities::getUsersOS(); + $installed_on = $config->get('miniorange_rest_api_installation_time_ref'); + $installed_date = date('d/m/Y H:i:s', $installed_on); + $triedAuthMethods = ($basicAuthTried == 'Did not Try') ? 'None' : '<br>Basic Auth: ' . $basicAuthTried . '</br>'; + + if (!str_contains($triedAuthMethods, 'None')) { + $triedAuthMethods .= $apikeyAuthTried !== 'Did not Try' ? '<br>Api Key Auth: ' . $apikeyAuthTried . '</br>' : ''; + } else { + $triedAuthMethods = $apikeyAuthTried !== 'Did not Try' ? '<br>Api Key Auth: ' . $apikeyAuthTried . '<br>' : 'None'; + } + + if(isset($post['uninstall_other_reason']) && !empty($post['uninstall_other_reason'])) { + $reason = $reason . ' ('. $q_feedback . ') '; + } + $message = 'Reason: ' . $reason; + + $current_time_in_ms = Utilities::get_timestamp(); + $stringToHash = $customerKey . $current_time_in_ms . $apikey; + $hashValue = hash("sha512", $stringToHash); + + $subject = 'Drupal ' . \DRUPAL::VERSION . ' REST API Authentication Module Feedback | ' . $module_version . ' | PHP Version ' . phpversion(); + $query = '[Drupal ' . \DRUPAL::VERSION . ' REST API Authentication | ' . $module_version . ' | PHP Version ' . phpversion() . ' ]: ' . $message; + $content = '<div >Hello, <br><br>Company :<a href="' . $base_url . '" target="_blank" >' . $base_url . '</a><br><br>Email :<a href="mailto:' . $fromEmail . '" target="_blank">' . $fromEmail . '</a><br><br>Installed on: ' . $installed_date . '<br><br>Operating System:' . $users_OS . '<br><br>Payment Page Visited: ' . $licensePageVisited . '<br><br>Tried Authentication Methods: ' . $triedAuthMethods . '<br><br>Query: ' . $query . '</div>'; + $fields = array( + 'customerKey' => $customerKey, + 'sendEmail' => true, + 'email' => array( + 'customerKey' => $customerKey, + 'fromEmail' => $fromEmail, + 'fromName' => 'miniOrange', + 'toEmail' => 'drupalsupport@xecurify.com', + 'toName' => 'drupalsupport@xecurify.com', + 'subject' => $subject, + 'content' => $content + ), + ); + + $field_string = json_encode($fields); + + $header = ['Content-Type' => 'application/json', 'Customer-Key' => $customerKey, 'Timestamp' => $current_time_in_ms, 'Authorization' => $hashValue]; + + try { + $response = \Drupal::httpClient()->post($url, ['headers' => $header, 'body' => $field_string, 'verify' => FALSE]); + + } catch (Exception $exception) { + + } + } + } + }