Moved submission code to separate file

This commit is contained in:
Fred Boniface 2022-09-20 20:36:00 +01:00
parent 64c69ef01c
commit 2ac8c3fa83
2 changed files with 19 additions and 44 deletions

View File

@ -8,56 +8,24 @@
<body> <body>
<?php <?php
// Define form variables and set to empty values (or to the UA)
// Imports the functions needed to validate and submit data
require './php/submitIssue.php';
// Set initial values
$subject = $detail = ""; $subject = $detail = "";
$ua_str = $_SERVER["HTTP_USER_AGENT"]; $ua_str = $_SERVER["HTTP_USER_AGENT"];
// Define response variable & data variable as blank before POSTing // If the page has been POSTed to then run this:
$giteaResponse = "";
$issueData = "";
// If the page is loading after submission then run this:
if ($_SERVER["REQUEST_METHOD"] == "POST" ) { if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
$subject = validate($_POST["subject"]); $title = cleanInput($_POST["subject"]);
$detail = validate($_POST["detail"]); $detail = cleanInput($_POST["detail"]);
// Get API Key for git.fjla.uk
require '/srv/php-keys/athena/gitea.php';
// Set httpHeaders
$httpHeaders = array(
"accept: application/json",
$giteaKey,
"Content-Type: application/json"
);
// Prepare $detail to POST // Prepare $detail to POST
$body = "User Agent: " . $ua_str . "\n Server PHP Version: " . PHP_VERSION . "\n\n\n" . $detail; $body = "User Agent: " . $ua_str . "\n\n Server PHP Version: " . PHP_VERSION . "\n\n" . $detail;
// Prepare the request // Call POST function
$rawData = array( sendInput($title,$body);
'body' => $body,
'title' => $subject
);
$issueData = json_encode($rawData);
// Prepare CURL
$curlConnection = curl_init('https://git.fjla.uk/api/v1/repos/fred.boniface/athena.fb-infra.uk/issues');
curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $issueData);
curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlConnection, CURLOPT_HTTPHEADER, $httpHeaders);
// Get response and exit CURL
$giteaResponse = curl_exec($curlConnection);
curl_close($curlConnection);
}
function validate($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
} }
?> ?>

View File

@ -1,6 +1,6 @@
<?php <?php
function submitToGitea($title,$body) { function sendInput($title,$body) {
// Get API Key for git.fjla.uk // Get API Key for git.fjla.uk
require '/srv/php-keys/athena/gitea.php'; require '/srv/php-keys/athena/gitea.php';
@ -32,3 +32,10 @@ function submitToGitea($title,$body) {
return $response; return $response;
} }
function cleanInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}