| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Jilo settings management. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This page ("settings") handles Jilo settings by | 
					
						
							|  |  |  |  * adding, editing, and deleting platforms, hosts, agents. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-22 16:55:17 +00:00
										 |  |  | // Check if this is an AJAX request
 | 
					
						
							|  |  |  | $isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&  | 
					
						
							|  |  |  |           strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 08:24:50 +00:00
										 |  |  | // Get any new feedback messages
 | 
					
						
							| 
									
										
										
										
											2025-02-17 14:50:57 +00:00
										 |  |  | include '../app/helpers/feedback.php'; | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $action = $_REQUEST['action'] ?? ''; | 
					
						
							|  |  |  | $agent = $_REQUEST['agent'] ?? ''; | 
					
						
							|  |  |  | $host = $_REQUEST['host'] ?? ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require '../app/classes/host.php'; | 
					
						
							|  |  |  | require '../app/classes/agent.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  | $hostObject = new Host($db); | 
					
						
							|  |  |  | $agentObject = new Agent($db); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if ($_SERVER['REQUEST_METHOD'] == 'POST') { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handles form submissions from editing | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-17 13:15:05 +00:00
										 |  |  |     // Apply rate limiting for profile operations
 | 
					
						
							|  |  |  |     require_once '../app/includes/rate_limit_middleware.php'; | 
					
						
							| 
									
										
										
										
											2025-04-25 09:10:29 +00:00
										 |  |  |     checkRateLimit($db, 'profile', $userId); | 
					
						
							| 
									
										
										
										
											2025-02-17 13:15:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |     // Get hash from URL if present
 | 
					
						
							|  |  |  |     $hash = parse_url($_SERVER['REQUEST_URI'], PHP_URL_FRAGMENT) ?? ''; | 
					
						
							|  |  |  |     $redirectUrl = htmlspecialchars($app_root) . '?page=settings'; | 
					
						
							|  |  |  |     if ($hash) { | 
					
						
							|  |  |  |         $redirectUrl .= '#' . $hash; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // host operations
 | 
					
						
							|  |  |  |     if (isset($_POST['item']) && $_POST['item'] === 'host') { | 
					
						
							|  |  |  |         if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is a host delete
 | 
					
						
							|  |  |  |             $host_id = $_POST['host']; | 
					
						
							|  |  |  |             $result = $hostObject->deleteHost($host_id); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "Host deleted successfully.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Deleting the host failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else if (!isset($_POST['host'])) { // This is a new host
 | 
					
						
							|  |  |  |             $newHost = [ | 
					
						
							|  |  |  |                 'address'       => $_POST['address'], | 
					
						
							|  |  |  |                 'platform_id'   => $_POST['platform'], | 
					
						
							|  |  |  |                 'name'          => empty($_POST['name']) ? $_POST['address'] : $_POST['name'], | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |             $result = $hostObject->addHost($newHost); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "New Jilo host added.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Adding the host failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { // This is an edit of existing host
 | 
					
						
							|  |  |  |             $host_id = $_POST['host']; | 
					
						
							|  |  |  |             $platform_id = $_POST['platform']; | 
					
						
							|  |  |  |             $updatedHost = [ | 
					
						
							|  |  |  |                 'id'      => $host_id, | 
					
						
							|  |  |  |                 'address' => $_POST['address'], | 
					
						
							|  |  |  |                 'name'    => empty($_POST['name']) ? $_POST['address'] : $_POST['name'], | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |             $result = $hostObject->editHost($platform_id, $updatedHost); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "Host edited.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Editing the host failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-22 16:55:17 +00:00
										 |  |  |         if (!$isAjax) { | 
					
						
							|  |  |  |             header('Location: ' . $redirectUrl); | 
					
						
							|  |  |  |             exit; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // agent operations
 | 
					
						
							|  |  |  |     } elseif (isset($_POST['item']) && $_POST['item'] === 'agent') { | 
					
						
							|  |  |  |         if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is an agent delete
 | 
					
						
							|  |  |  |             $agent_id = $_POST['agent']; | 
					
						
							|  |  |  |             $result = $agentObject->deleteAgent($agent_id); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "Agent deleted successfully.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Deleting the agent failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else if (isset($_POST['new']) && $_POST['new'] === 'true') { // This is a new agent
 | 
					
						
							|  |  |  |             $newAgent = [ | 
					
						
							|  |  |  |                 'type_id'       => $_POST['type'], | 
					
						
							|  |  |  |                 'url'           => $_POST['url'], | 
					
						
							|  |  |  |                 'secret_key'    => empty($_POST['secret_key']) ? null : $_POST['secret_key'], | 
					
						
							|  |  |  |                 'check_period'  => empty($_POST['check_period']) ? 0 : $_POST['check_period'], | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |             $result = $agentObject->addAgent($_POST['host'], $newAgent); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "New Jilo agent added.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Adding the agent failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { // This is an edit of existing agent
 | 
					
						
							|  |  |  |             $agent_id = $_POST['agent']; | 
					
						
							|  |  |  |             $updatedAgent = [ | 
					
						
							|  |  |  |                 'agent_type_id' => $_POST['agent_type_id'], | 
					
						
							|  |  |  |                 'url'          => $_POST['url'], | 
					
						
							|  |  |  |                 'secret_key'   => empty($_POST['secret_key']) ? null : $_POST['secret_key'], | 
					
						
							|  |  |  |                 'check_period' => empty($_POST['check_period']) ? 0 : $_POST['check_period'], | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |             $result = $agentObject->editAgent($agent_id, $updatedAgent); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "Agent edited.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Editing the agent failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-22 16:55:17 +00:00
										 |  |  |         if (!$isAjax) { | 
					
						
							|  |  |  |             header('Location: ' . $redirectUrl); | 
					
						
							|  |  |  |             exit; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // platform operations
 | 
					
						
							|  |  |  |     } elseif (isset($_POST['item']) && $_POST['item'] === 'platform') { | 
					
						
							|  |  |  |         if (isset($_POST['delete']) && $_POST['delete'] === 'true') { // This is a platform delete
 | 
					
						
							|  |  |  |             $platform_id = $_POST['platform']; | 
					
						
							|  |  |  |             $result = $platformObject->deletePlatform($platform_id); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "Platform deleted successfully.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Deleting the platform failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else if (!isset($_POST['platform'])) { // This is a new platform
 | 
					
						
							|  |  |  |             $newPlatform = [ | 
					
						
							|  |  |  |                 'name'          => $_POST['name'], | 
					
						
							|  |  |  |                 'jitsi_url'     => $_POST['jitsi_url'], | 
					
						
							|  |  |  |                 'jilo_database' => $_POST['jilo_database'], | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |             $result = $platformObject->addPlatform($newPlatform); | 
					
						
							|  |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "New Jitsi platform added.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Adding the platform failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { // This is an edit of existing platform
 | 
					
						
							|  |  |  |             $platform_id = $_POST['platform']; | 
					
						
							|  |  |  |             $updatedPlatform = [ | 
					
						
							|  |  |  |                 'name'          => $_POST['name'], | 
					
						
							|  |  |  |                 'jitsi_url'     => $_POST['jitsi_url'], | 
					
						
							|  |  |  |                 'jilo_database' => $_POST['jilo_database'], | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2025-01-23 16:40:55 +00:00
										 |  |  |             $result = $platformObject->editPlatform($platform_id, $updatedPlatform); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             if ($result === true) { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('NOTICE', 'DEFAULT', "Platform edited.", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2025-02-23 11:14:58 +00:00
										 |  |  |                 Feedback::flash('ERROR', 'DEFAULT', "Editing the platform failed. Error: $result", true); | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-02-22 22:02:15 +00:00
										 |  |  |         header('Location: ' . $redirectUrl); | 
					
						
							|  |  |  |         exit; | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Handles GET requests to display templates. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-08 17:07:53 +00:00
										 |  |  |     if ($userObject->hasRight($userId, 'view settings') || $userObject->hasRight($userId, 'superuser')) { | 
					
						
							| 
									
										
										
										
											2025-01-23 10:41:29 +00:00
										 |  |  |         $jilo_agent_types = $agentObject->getAgentTypes(); | 
					
						
							|  |  |  |         include '../app/templates/settings.php'; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         include '../app/templates/error-unauthorized.php'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |