Skip to content

Commit

Permalink
Fix curl params
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Oct 26, 2023
1 parent ff9d124 commit 33c86c5
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions rule/ftpsyncfiles/rule_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ private function download_files(int $did): void {
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_DIRLISTONLY, 1);
curl_setopt($ch, CURLOPT_URL, $connection);

$result = curl_exec($ch);
Expand All @@ -167,11 +168,9 @@ private function download_files(int $did): void {
} else {
// List of remote files and directories.
$remotelist = explode("\n", trim($result));

foreach ($remotelist as $line) {
$parts = preg_split('/\s+/', trim($line));
$filename = end($parts);

mtrace($line);
$filename = trim($line);
if (!empty($filename) && $filename !== '.' && $filename !== '..') {
$remotepath = "$connection/$filename";
mtrace($remotepath);
Expand Down Expand Up @@ -205,21 +204,37 @@ private function download_files(int $did): void {
$filedata
);
echo "Downloaded $filename successfully." . PHP_EOL;
// Delete the file from the remote server using cURL.
$deletech = curl_init();
curl_setopt($deletech, CURLOPT_URL, $remotepath);
curl_setopt($deletech, CURLOPT_QUOTE, array("rm ".escapeshellarg("$remotepath"));
$deleteresult = curl_exec($deletech);
curl_close($deletech);
} else {
echo "Failed to download $filename." . PHP_EOL;
}
echo json_encode(curl_getinfo($filehandle));
curl_close($filehandle);
}
}
foreach ($remotelist as $line) {
$filename = trim($line);
if (!empty($filename) && $filename !== '.' && $filename !== '..') {
// Delete the file from the remote server using cURL.
curl_setopt($ch, CURLOPT_QUOTE, ["rm " . escapeshellarg($filename)]);
$deleteresult = curl_exec($ch);
if ($deleteresult === false) {
curl_setopt($ch, CURLOPT_QUOTE, ["rm " . $filename]);
$deleteresult = curl_exec($ch);
if ($deleteresult === false) {
echo "Failed to delete $filename on the remote server." . PHP_EOL;
curl_setopt($ch, CURLOPT_QUOTE, ["rm \"$filename\""]);
$deleteresult = curl_exec($ch);
if ($deleteresult === false) {
echo "Failed to delete $filename on the remote server." . PHP_EOL;
var_dump(curl_errno($ch),curl_error($ch));
} else {
echo "Deleted $filename successfully." . PHP_EOL;
}
} else {
echo "Deleted $filename successfully." . PHP_EOL;
}
} else {
echo "Failed to download $filename." . PHP_EOL;
echo "Deleted $filename successfully." . PHP_EOL;
}
curl_close($filehandle);
}
}
}
Expand Down

0 comments on commit 33c86c5

Please sign in to comment.