In what scenarios should a wrapper or intermediary user be used to execute external programs in PHP?

When executing external programs in PHP, it is recommended to use a wrapper or intermediary user to enhance security and prevent unauthorized access to sensitive resources on the server. This approach helps to limit the permissions of the executed program and reduce the risk of potential security vulnerabilities.

<?php

// Define the command to be executed
$command = 'ls -la';

// Define the user to run the command as
$user = 'www-data';

// Execute the command using sudo with the specified user
$result = shell_exec("sudo -u $user $command");

// Output the result
echo $result;

?>