Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/BackgroundJobs/Launcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,20 @@ public function __construct(ITimeFactory $time, LoggerInterface $logger) {
*/
#[\Override]
protected function run($argument): void {
$command = (string)$argument['command'];
if (!isset($argument['template'])) {
$this->logger->error('Skipping job: template argument is missing', [
'app' => Application::APPID,
'argument' => $argument
]);
return;
}

$template = (string)$argument['template'];
$replacements = is_array($argument['replacements'] ?? null) ? $argument['replacements'] : [];

if (strpos($command, '%f')) {
$path = isset($argument['path']) ? (string)$argument['path'] : '';
if (isset($argument['path']) && strpos($template, '%f') !== false) {
try {
$command = str_replace('%f', escapeshellarg($this->resolveLocalPath($path)), $command);
$replacements['%f'] = escapeshellarg($this->resolveLocalPath((string)$argument['path']));
} catch (Exception $e) {
$this->logger->warning($e->getMessage(), [
'app' => Application::APPID,
Expand All @@ -46,6 +54,8 @@ protected function run($argument): void {
}
}

$command = strtr($template, $replacements);

// with wrapping sh around the command, we leave any redirects intact,
// but ensure that the script is not blocking Nextcloud's execution
$wrapper = 'sh -c ' . escapeshellarg($command) . ' >/dev/null &';
Expand Down
53 changes: 26 additions & 27 deletions lib/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,27 @@ public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatch
}

foreach ($matches as $match) {
$template = $match['operation'];
try {
$command = $this->buildCommand($match['operation'], $node, $eventName, $extra);
$replacements = $this->buildReplacements($template, $node, $eventName, $extra);
} catch (PlaceholderNotSubstituted $e) {
$this->logger->warning(
'Could not substitute {placeholder} in {command} with node {node}',
[
'app' => Application::APPID,
'placeholder' => $e->getPlaceholder(),
'command' => $match['operation'],
'command' => $template,
'node' => $node,
'exception' => $e,
]
);
continue;
}
$args = ['command' => $command];
if (strpos($command, '%f')) {
$args = [
'template' => $template,
'replacements' => $replacements,
];
if (strpos($template, '%f') !== false) {
$args['path'] = $node->getPath();
}
$this->jobList->add(Launcher::class, $args);
Expand All @@ -169,50 +173,48 @@ public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatch
}

/**
* @return array<string, string>
* @throws PlaceholderNotSubstituted
*/
protected function buildCommand(string $template, Node $node, string $event, array $extra = []): string {
$command = $template;
protected function buildReplacements(string $template, Node $node, string $event, array $extra = []): array {
$replacements = [];

if (strpos($command, '%e')) {
$command = str_replace('%e', escapeshellarg($event), $command);
if (strpos($template, '%e') !== false) {
$replacements['%e'] = escapeshellarg($event);
}

if (strpos($command, '%n')) {
// Nextcloud relative-path
$ncRelPath = $this->replacePlaceholderN($node);
$command = str_replace('%n', escapeshellarg($ncRelPath), $command);
unset($ncRelPath);
if (strpos($template, '%n') !== false) {
$replacements['%n'] = escapeshellarg($this->replacePlaceholderN($node));
}

if (strpos($command, '%i')) {
if (strpos($template, '%i') !== false) {
$nodeID = -1;
try {
$nodeID = $node->getId();
} catch (InvalidPathException|NotFoundException) {
}
$command = str_replace('%i', escapeshellarg((string)$nodeID), $command);
$replacements['%i'] = escapeshellarg((string)$nodeID);
}

if (strpos($command, '%a')) {
if (strpos($template, '%a') !== false) {
$user = $this->session->getUser();
$userID = '';
if ($user instanceof IUser) {
$userID = $user->getUID();
}
$command = str_replace('%a', escapeshellarg($userID), $command);
$replacements['%a'] = escapeshellarg($userID);
}

if (strpos($command, '%o')) {
if (strpos($template, '%o') !== false) {
$user = $node->getOwner();
$userID = '';
if ($user instanceof IUser) {
$userID = $user->getUID();
}
$command = str_replace('%o', escapeshellarg($userID), $command);
$replacements['%o'] = escapeshellarg($userID);
}

if (strpos($command, '%r')) {
if (strpos($template, '%r') !== false) {
// filled by ApprovalStateChangedListener when the flow was
// triggered by an approval state change of the approval app
$requesterID = null;
Expand All @@ -223,17 +225,14 @@ protected function buildCommand(string $template, Node $node, string $event, arr
if ($requesterID === null) {
throw new PlaceholderNotSubstituted('r');
}
$command = str_replace('%r', escapeshellarg($requesterID), $command);
$replacements['%r'] = escapeshellarg($requesterID);
}

if (strpos($command, '%x')) {
if (!isset($extra['oldFilePath'])) {
$extra['oldFilePath'] = '';
}
$command = str_replace('%x', escapeshellarg($extra['oldFilePath']), $command);
if (strpos($template, '%x') !== false) {
$replacements['%x'] = escapeshellarg($extra['oldFilePath'] ?? '');
}

return $command;
return $replacements;
}

/**
Expand Down
Loading