-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.php
More file actions
47 lines (39 loc) · 1.95 KB
/
Copy pathhandler.php
File metadata and controls
47 lines (39 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
session_start(); #like session_state, cookies to remember this session
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Store user selection
if (isset($_POST['userSelection'])) { #if the browser sent something w/ this key
$_SESSION['searchType'] = $_POST['userSelection']; #save it here
}
// Handle form submission
if (isset($_POST['request1'])) { #if at least request1 is filled in
$requests = [];
$likes = [];
# Save requests and likes in PHP list
for ($i = 1; $i <= 5; $i++) { #loop through all 5 boxes
if (!empty($_POST["request$i"])) {
$requests[] = escapeshellarg($_POST["request$i"]); #clean and append
}
if (!empty($_POST["likes$i"])) {
$likes[] = escapeshellarg($_POST["likes$i"]);
}
}
# Prepare everything for command-line
$searchType = $_SESSION['searchType'] ?? 'keyword'; #use this value, but if it's not set, fall back to 'keyword'
$requestsArg = implode(',', $requests); #joins the array back into a single comma-separated string ("'key1', 'key2'")
$likesArg = implode(',', $likes);
# Run command on the server (e.g., python3.10 backend.py 'keyword' 'bts,jensen' '1000,500')
$output = shell_exec("arch -arm64 /Library/Frameworks/Python.framework/Versions/3.10/bin/python3.10 backend.py " .
escapeshellarg($searchType) . " " .
escapeshellarg($requestsArg) . " " .
escapeshellarg($likesArg) . " 2>&1");
# Prev. ^ goes through scraper -> summarizer, returns result JSON
$result = json_decode($output, true);
$summaries = $result['bullets'] ?? [];
# Send to broswer
header('Content-Type: application/json'); # HTTP response header
echo json_encode(['summaries' => $summaries]); #PHP array -> JSON string
exit; #stop script after sending response
}
}
?>