Move ajax logic to ajax.php

This commit is contained in:
Marcus Hill 2022-02-26 11:16:53 +00:00
parent 3bd79635ec
commit 0fac1f3039
5 changed files with 128 additions and 105 deletions

122
ajax.php Normal file
View File

@ -0,0 +1,122 @@
<?php
/*
* ajax.php
* Similar to post.php, but for requests using Asynchronous JavaScript
* Always returns data in JSON format, unless otherwise specified
*/
include("config.php");
include("functions.php");
include("check_login.php");
/*
* Fetches SSL certificates from remote hosts & returns the relevant info (host, issuer, expiry)
*/
if(isset($_GET['certificate_fetch_parse_json_details'])){
// PHP doesn't appreciate attempting SSL sockets to non-existent domains
if(empty($_GET['domain'])){
exit();
}
$domain = $_GET['domain'];
// FQDNs in database shouldn't have a URL scheme, adding one
$domain = "https://".$domain;
// Parse host and port
$url = parse_url($domain, PHP_URL_HOST);
$port = parse_url($domain, PHP_URL_PORT);
// Default port
if(!$port){
$port = "443";
}
// Get certificate (using verify peer false to allow for self-signed certs)
$socket = "ssl://$url:$port";
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE, "verify_peer" => FALSE,)));
$read = stream_socket_client($socket, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$cert_public_key_obj = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
openssl_x509_export($cert['options']['ssl']['peer_certificate'], $export);
// Process data
if($cert_public_key_obj){
$response['success'] = "TRUE";
$response['expire'] = date('Y-m-d', $cert_public_key_obj['validTo_time_t']);
$response['issued_by'] = strip_tags($cert_public_key_obj['issuer']['O']);
$response['public_key'] = $export; //nl2br
}
else{
$response['success'] = "FALSE";
}
echo json_encode($response);
}
/*
* Looks up info for a given certificate ID from the database, used to dynamically populate modal fields
*/
if(isset($_GET['certificate_get_json_details'])){
$certificate_id = intval($_GET['certificate_id']);
$client_id = intval($_GET['client_id']);
// Individual certificate lookup
$cert_sql = mysqli_query($mysqli,"SELECT * FROM certificates WHERE certificate_id = $certificate_id AND certificate_client_id = $client_id");
while($row = mysqli_fetch_array($cert_sql)){
$response['certificate'][] = $row;
}
// Get all domains for this client that could be linked to this certificate
$domains_sql = mysqli_query($mysqli, "SELECT domain_id, domain_name FROM domains WHERE domain_client_id = '$client_id' AND company_id = '$session_company_id'");
while($row = mysqli_fetch_array($domains_sql)){
$response['domains'][] = $row;
}
echo json_encode($response);
}
/*
* Looks up info on the ticket number provided, used to populate the ticket merge modal
*/
if(isset($_GET['merge_ticket_get_json_details'])){
$merge_into_ticket_number = intval($_GET['merge_into_ticket_number']);
$sql = mysqli_query($mysqli,"SELECT * FROM tickets
LEFT JOIN clients ON ticket_client_id = client_id
LEFT JOIN contacts ON ticket_contact_id = contact_id
WHERE ticket_number = '$merge_into_ticket_number' AND tickets.company_id = '$session_company_id'");
if(mysqli_num_rows($sql) == 0){
//Do nothing.
}
else {
//Return ticket, client and contact details for the given ticket number
$response = mysqli_fetch_array($sql);
echo json_encode($response);
}
}
/*
* Looks up info for a given network ID from the database, used to dynamically populate modal fields
*/
if(isset($_GET['network_get_json_details'])){
$network_id = intval($_GET['network_id']);
$client_id = intval($_GET['client_id']);
// Individual network lookup
$network_sql = mysqli_query($mysqli,"SELECT * FROM networks WHERE network_id = $network_id AND network_client_id = $client_id");
while($row = mysqli_fetch_array($network_sql)){
$response['network'][] = $row;
}
// Lookup all client locations, as networks can be associated with any client location
$locations_sql = mysqli_query($mysqli, "SELECT location_id, location_name FROM locations
WHERE location_client_id = '$client_id' AND company_id = '$session_company_id'"
);
while($row = mysqli_fetch_array($locations_sql)){
$response['locations'][] = $row;
}
echo json_encode($response);
}

View File

@ -140,7 +140,7 @@ include("client_certificate_add_modal.php");
// Send a GET request to post.php as post.php?certificate_get_json_details=true&client_id=NUM&certificate_id=NUM
jQuery.get(
"post.php",
"ajax.php",
{certificate_get_json_details: 'true', client_id: client_id, certificate_id: certificate_id},
function(data){
@ -203,10 +203,10 @@ include("client_certificate_add_modal.php");
var publicKey = document.getElementById("editPublicKey");
}
//Send a GET request to post.php as post.php?fetch_certificate=TRUE&domain=DOMAIN
//Send a GET request to post.php as post.php?certificate_fetch_parse_json_details=TRUE&domain=DOMAIN
jQuery.get(
"post.php",
{fetch_certificate: 'TRUE', domain: domain},
"ajax.php",
{certificate_fetch_parse_json_details: 'TRUE', domain: domain},
function(data){
//If we get a response from post.php, parse it as JSON
const ssl_data = JSON.parse(data);

View File

@ -170,7 +170,7 @@ function populateNetworkEditModal(client_id, network_id) {
// Send a GET request to post.php as post.php?network_get_json_details=true&client_id=NUM&network_id=NUM
jQuery.get(
"post.php",
"ajax.php",
{network_get_json_details: 'true', client_id: client_id, network_id: network_id},
function(data){

View File

@ -5073,25 +5073,6 @@ if(isset($_POST['edit_network'])){
}
if(isset($_GET['network_get_json_details'])){
$network_id = intval($_GET['network_id']);
$client_id = intval($_GET['client_id']);
$network_sql = mysqli_query($mysqli,"SELECT * FROM networks WHERE network_id = $network_id AND network_client_id = $client_id");
while($row = mysqli_fetch_array($network_sql)){
$response['network'][] = $row;
}
$locations_sql = mysqli_query($mysqli, "SELECT location_id, location_name FROM locations
WHERE location_client_id = '$client_id' AND company_id = '$session_company_id'"
);
while($row = mysqli_fetch_array($locations_sql)){
$response['locations'][] = $row;
}
echo json_encode($response);
}
if(isset($_GET['delete_network'])){
$network_id = intval($_GET['delete_network']);
@ -5219,68 +5200,6 @@ if(isset($_POST['edit_certificate'])){
}
if(isset($_GET['certificate_get_json_details'])){
$certificate_id = intval($_GET['certificate_id']);
$client_id = intval($_GET['client_id']);
$cert_sql = mysqli_query($mysqli,"SELECT * FROM certificates WHERE certificate_id = $certificate_id AND certificate_client_id = $client_id");
while($row = mysqli_fetch_array($cert_sql)){
$response['certificate'][] = $row;
}
$domains_sql = mysqli_query($mysqli, "SELECT domain_id, domain_name FROM domains
WHERE domain_client_id = '$client_id' AND company_id = '$session_company_id'"
);
while($row = mysqli_fetch_array($domains_sql)){
$response['domains'][] = $row;
}
echo json_encode($response);
}
if(isset($_GET['fetch_certificate'])){
// PHP doesn't appreciate attempting SSL sockets to non-existent domains
if(empty($_GET['domain'])){
exit();
}
$domain = $_GET['domain'];
// FQDNs in database shouldn't have a URL scheme, adding one
$domain = "https://".$domain;
// Parse host and port
$url = parse_url($domain, PHP_URL_HOST);
$port = parse_url($domain, PHP_URL_PORT);
// Default port
if(!$port){
$port = "443";
}
// Get certificate
// Using verify peer false to allow for self-signed / internal CA certs
$socket = "ssl://$url:$port";
$get = stream_context_create(array("ssl" => array("capture_peer_cert" => TRUE, "verify_peer" => FALSE,)));
$read = stream_socket_client($socket, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get);
$cert = stream_context_get_params($read);
$cert_public_key_obj = openssl_x509_parse($cert['options']['ssl']['peer_certificate']);
openssl_x509_export($cert['options']['ssl']['peer_certificate'], $export);
// Process data
if($cert_public_key_obj){
$cert_data['success'] = "TRUE";
$cert_data['expire'] = date('Y-m-d', $cert_public_key_obj['validTo_time_t']);
$cert_data['issued_by'] = strip_tags($cert_public_key_obj['issuer']['O']);
$cert_data['public_key'] = $export; //nl2br
}
else{
$cert_data['success'] = "FALSE";
}
// Return as JSON
echo json_encode($cert_data);
}
if(isset($_GET['delete_certificate'])){
$certificate_id = intval($_GET['delete_certificate']);
@ -5728,24 +5647,6 @@ if(isset($_GET['archive_ticket_reply'])){
}
if(isset($_GET['merge_ticket_get_json_details'])){
$merge_into_ticket_number = intval($_GET['merge_into_ticket_number']);
$sql = mysqli_query($mysqli,"SELECT * FROM tickets
LEFT JOIN clients ON ticket_client_id = client_id
LEFT JOIN contacts ON ticket_contact_id = contact_id
WHERE ticket_number = '$merge_into_ticket_number' AND tickets.company_id = '$session_company_id'");
if(mysqli_num_rows($sql) == 0){
//Do nothing.
}
else {
//Return ticket, client and contact details for the given ticket number
$row = mysqli_fetch_array($sql);
echo json_encode($row);
}
}
if(isset($_POST['merge_ticket'])){
$ticket_id = intval($_POST['ticket_id']);
$merge_into_ticket_number = intval($_POST['merge_into_ticket_number']);

View File

@ -74,7 +74,7 @@
//Send a GET request to post.php as post.php?merge_ticket_get_json_details=true&merge_into_ticket_number=NUMBER
jQuery.get(
"post.php",
"ajax.php",
{merge_ticket_get_json_details: 'true', merge_into_ticket_number: merge_into_ticket_number},
function(data){
//If we get a response from post.php, parse it as JSON