| Conditions | 13 |
| Paths | 147 |
| Total Lines | 85 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 30 | function account_ownership_action($user) { |
||
| 31 | // POST request - the user has submitted the form. |
||
| 32 | page_head(tra("Proof of account ownership results"), null, null, null, boinc_recaptcha_get_head_extra()); |
||
| 33 | |||
| 34 | if (recaptcha_private_key()) { |
||
| 35 | // Recaptcha is enabled on the BOINC server |
||
| 36 | if (!boinc_recaptcha_isValidated(recaptcha_private_key())) { |
||
| 37 | // The user failed to solve the recaptcha prompt - redirect them to an error message! |
||
| 38 | error_page( |
||
| 39 | tra("Your reCAPTCHA response was not correct. Please try again.") |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | // Input is passed in from the openssl_sign_form |
||
| 45 | $user_data = htmlentities(post_str("user_data", true), ENT_QUOTES, "UTF-8"); // Convert special characters to html equivelant |
||
| 46 | |||
| 47 | if ((strlen($user_data) > 0) && (strlen($user_data) <= 4096)) { |
||
| 48 | require_once("../inc/account_ownership.inc"); |
||
| 49 | // Check that the private key file exists where specified. If not, redirect to error page. |
||
| 50 | if (!file_exists($account_ownership_private_key_file_path)) { |
||
|
|
|||
| 51 | error_page(tra("The proof of account ownership feature is not set up properly. Contact the project administrator to resolve the issue.")); |
||
| 52 | } |
||
| 53 | |||
| 54 | // Check that the public key file exists where specified. If not, redirect to error page. |
||
| 55 | if (!file_exists($account_ownership_public_key_file_path)) { |
||
| 56 | error_page(tra("The proof of account ownership feature is not set up properly. Contact the project administrator to resolve the issue.")); |
||
| 57 | } |
||
| 58 | |||
| 59 | $privkey = fopen($account_ownership_private_key_file_path, "r"); // Opening private key file |
||
| 60 | if (!isset($privkey) || empty($privkey)) { |
||
| 61 | error_page(tra("The proof of account ownership feature is not set up properly. Contact the project administrator to resolve the issue.")); |
||
| 62 | } |
||
| 63 | $privkey_contents = fread($privkey, 8192); // Reading contents of private key into var |
||
| 64 | fclose($privkey); // Closing private key file |
||
| 65 | |||
| 66 | $userid = $user->id; // Retrieving the user's UserId |
||
| 67 | $message_data = "$userid $user_data"; // Create the message which will be signed. |
||
| 68 | |||
| 69 | $private_key_pem = openssl_pkey_get_private($privkey_contents); // Loading the private key into memory |
||
| 70 | openssl_sign($message_data, $signature, $private_key_pem, OPENSSL_ALGO_SHA512); // Compute signature using SHA512 |
||
| 71 | openssl_free_key($private_key_pem); // Free the private key from memory for additional security |
||
| 72 | |||
| 73 | $pubkey = fopen($account_ownership_public_key_file_path, "r"); // Open public key file |
||
| 74 | if ((!isset($pubkey)) || empty($pubkey)) { |
||
| 75 | error_page(tra("The proof of account ownership feature is not set up properly. Contact the project administrator to resolve the issue.")); |
||
| 76 | } |
||
| 77 | $pubkey_contents = fread($pubkey, 8192); // Read contents to var |
||
| 78 | fclose($pubkey); // Close pub key file |
||
| 79 | |||
| 80 | $base64_sig = base64_encode($signature); // Base64 encode the generated signature to enable safe output to text file. |
||
| 81 | $decoded_sig = base64_decode($base64_sig); // Decode base64 sig for use in sig_verification |
||
| 82 | $pubkeyid = openssl_pkey_get_public($pubkey_contents); // fetch public key into memory |
||
| 83 | $sig_verification = openssl_verify($message_data, $decoded_sig, $pubkeyid, OPENSSL_ALGO_SHA512); // Verify that the generated signature against the original data, using the public key. |
||
| 84 | openssl_free_key($pubkeyid); // Free the public key from memory |
||
| 85 | |||
| 86 | // Check if signature was successfully validated |
||
| 87 | if ($sig_verification == 1) { |
||
| 88 | $url_tokens = url_tokens($user->authenticator); |
||
| 89 | // The generated signature has been successfully verified using the public key. |
||
| 90 | $master_url = master_url(); |
||
| 91 | echo "<p>Do not share this information with anyone other than the external system which has requested this proof of account ownership.</p>"; |
||
| 92 | echo "<textarea rows='13' cols='50' id='result_textbox'><account_ownership_verification>\n<master_url>$master_url</master_url>\n<msg>$message_data</msg>\n<signature>$base64_sig</signature>\n</account_ownership_verification></textarea>"; |
||
| 93 | echo "<br/><br/><button class='btn btn-success' onclick='copy_result_textbox()'>Copy text</button>"; |
||
| 94 | echo "<a href='account_ownership.php?$url_tokens'><button class='btn btn-default'>Go back</button></a>"; |
||
| 95 | echo '<script type="text/javascript">'; |
||
| 96 | echo 'function copy_result_textbox() { |
||
| 97 | var target_textbox = document.getElementById("result_textbox"); |
||
| 98 | target_textbox.select(); |
||
| 99 | document.execCommand("copy"); |
||
| 100 | alert("Copied to clipboard"); |
||
| 101 | }'; |
||
| 102 | echo '</script>'; |
||
| 103 | page_tail(); |
||
| 104 | |||
| 105 | } elseif ($sig_verification == 0) { |
||
| 106 | // The generated signature has not been verified. The private/public keys do not match. |
||
| 107 | error_page(tra("Signature verification failed. Contact the project administrator to resolve the issue.")); |
||
| 108 | } else { |
||
| 109 | // Something has gone wrong & an error has occurred. |
||
| 110 | error_page(tra("An error occurred during the signature verification. Contact the project administrator to resolve the issue.")); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | // User data input invalid |
||
| 114 | error_page(tra("Invalid input. User input must have a length > 0 and < 4096. <form><input type='button' value='Go back!'' onclick='history.back()'></form>")); |
||
| 115 | } |
||
| 172 |