←':''; //Check if allowed if (isAllowed($page,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'R') === 0){ header('location: index.php'); exit; } //PAGE Security $page_manage = 'access_element_manage'; $update_allowed = isAllowed($page_manage ,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'U'); $delete_allowed = isAllowed($page_manage ,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'D'); $create_allowed = isAllowed($page_manage ,$_SESSION['authorization']['profile'],$_SESSION['authorization']['permission'],'C'); // Function to scan project for new PHP files and add to access_elements function scan_and_update_access_elements() { $new_elements = []; $base_path = dirname(__FILE__); // Scan root PHP files (excluding index, login, logout) $root_files = glob($base_path . '/*.php'); foreach ($root_files as $file) { $filename = basename($file, '.php'); if (!in_array($filename, ['index', 'login', 'logout'])) { // Only add if not already in array (first occurrence wins) if (!isset($new_elements[$filename])) { $new_elements[$filename] = [ 'name' => ucwords(str_replace('_', ' ', $filename)), 'path' => $filename, 'group' => 'Views', 'description' => 'Auto-scanned: ' . $filename ]; } } } // Scan API v2 get folder - only add if not already found in root $get_files = glob($base_path . '/api/v2/get/*.php'); foreach ($get_files as $file) { $filename = basename($file, '.php'); if (!isset($new_elements[$filename])) { $new_elements[$filename] = [ 'name' => ucwords(str_replace('_', ' ', $filename)), 'path' => $filename, 'group' => 'API', 'description' => 'Auto-scanned: ' . $filename ]; } } // Scan API v2 post folder - only add if not already found $post_files = glob($base_path . '/api/v2/post/*.php'); foreach ($post_files as $file) { $filename = basename($file, '.php'); if (!isset($new_elements[$filename])) { $new_elements[$filename] = [ 'name' => ucwords(str_replace('_', ' ', $filename)), 'path' => $filename, 'group' => 'API', 'description' => 'Auto-scanned: ' . $filename ]; } } // Get existing access elements from API $api_url = '/v2/access_elements/'; $existing = ioServer($api_url, ''); $existing_paths = []; if (!empty($existing)) { $existing_data = json_decode($existing); foreach ($existing_data as $element) { $existing_paths[] = $element->access_path; } } // Filter out elements that already exist $elements_to_add = []; foreach ($new_elements as $path => $element) { if (!in_array($path, $existing_paths)) { $elements_to_add[] = $element; } } // Add new elements via API $added_count = 0; foreach ($elements_to_add as $element) { $data = json_encode([ 'access_name' => $element['name'], 'access_path' => $element['path'], 'access_group' => $element['group'], 'description' => $element['description'], 'is_active' => 1 ], JSON_UNESCAPED_UNICODE); $response = ioServer('/v2/access_elements', $data); if ($response !== 'NOK') { $added_count++; } } return $added_count; } // Handle scan request if (isset($_POST['scan_elements']) && $create_allowed === 1) { $added_count = scan_and_update_access_elements(); header('Location: index.php?page=access_elements&elements_added=' . $added_count); exit; } //GET PARAMETERS && STORE in SESSION for FURTHER USE/NAVIGATION $pagination_page = $_SESSION['p'] = isset($_GET['p']) ? $_GET['p'] : 1; $status = $_SESSION['status'] = isset($_GET['status']) ? '&status='.$_GET['status'] : ''; $sort = $_SESSION['sort'] = isset($_GET['sort']) ? '&sort='.$_GET['sort'] : ''; $search = $_SESSION['search'] = isset($_GET['search']) ? '&search='.$_GET['search'] : ''; //GET PARAMETERS FOR FILTERS $filter = urlGETdetailsFilter($_GET) ?? ''; // Determine the URL $url = 'index.php?page=access_elements'.$status.$search.$sort; //GET Details from URL $GET_VALUES = urlGETdetails($_GET) ?? ''; //CALL TO API $api_url = '/v2/access_elements/'.$GET_VALUES; $responses = ioServer($api_url,''); //Decode Payload if (!empty($responses)){$responses = json_decode($responses);}else{$responses = null;} //Return QueryTotal from API $total_url = ((!empty($GET_VALUES) && $GET_VALUES !='') ? '&totals=' : 'totals=' ); $api_url = '/v2/access_elements/'.$GET_VALUES.$total_url; $query_total = ioServer($api_url,''); //Decode Payload if (!empty($query_total)){$query_total = json_decode($query_total);}else{$query_total = null;} // Handle success messages if (isset($_GET['success_msg'])) { if ($_GET['success_msg'] == 1) { $success_msg = ($message_access_1 ?? 'Access element created successfully'); } if ($_GET['success_msg'] == 2) { $success_msg = ($message_access_2 ?? 'Access element updated successfully'); } if ($_GET['success_msg'] == 3) { $success_msg = ($message_access_3 ?? 'Access element deleted successfully'); } } // Handle elements added message from scan if (isset($_GET['elements_added'])) { $added_count = (int)$_GET['elements_added']; if ($added_count > 0) { $success_msg = $added_count . ' ' . ($message_elements_added ?? 'new access elements added'); } else { $success_msg = ($message_no_new_elements ?? 'No new elements found. All elements are up to date.'); } } template_header(($access_elements_title ?? 'Access Elements'), 'access_elements','view'); $view = '

'.($access_elements_h2 ?? 'Access Elements').' ('.$query_total.')

'.($access_elements_p ?? 'Manage system access elements and paths').'

'.$back_btn_orgin; // Scan button - only show if user has create permission if ($create_allowed === 1){ $view .= '
'; $view .= '+'; } $view .= '
'; if (isset($success_msg)){ $view .= '

'.$success_msg.'

'; } $view .= ' '; $view .= '
'; if (empty($responses)){ $view .= ' '; } foreach ($responses as $response){ //Translate status INT to STR $status_text = ($response->is_active == 1) ? ($enabled ?? 'Active') : ($disabled ?? 'Inactive'); $status_class = ($response->is_active == 1) ? 'id1' : 'id0'; $view .= ' '; } $view .= '
'.($access_element_name ?? 'Name').' '.($access_element_path ?? 'Path').' '.($access_element_group ?? 'Group').' '.($role_description ?? 'Description').' '.($general_status ?? 'Status').' '.($general_created ?? 'Created').'
'.($message_no_access_elements ?? 'No access elements found').'
'.$response->access_name.' '.$response->access_path.' '.($response->access_group ?? '-').' '.($response->description ?? '-').' '.$status_text.' '.getRelativeTime($response->created).'
'; $page_rows = $page_rows_equipment ?? 20; $view.=''; //OUTPUT echo $view; template_footer(); ?>