Skip to content
Open
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
2 changes: 2 additions & 0 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $p->startBuffering();

// creating our library using whole directory
$p->buildFromDirectory($input);
$p->buildFromDirectory($root . '/vendor');

// Create a custom CLI-only stub without webPhar()
// This avoids signature verification issues when the phar is downloaded/transferred
Expand All @@ -51,6 +52,7 @@ $stub = <<<'STUB'
#!/usr/bin/env php
<?php
Phar::mapPhar();
require 'phar://' . __FILE__ . '/autoload.php';
require 'phar://' . __FILE__ . '/index.php';
__HALT_COMPILER();
STUB;
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@
},
"require": {
"php": ">=7.4",
"ext-json": "*",
"ext-fileinfo": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-zlib": "*",
"ext-mbstring": "*"
"sebastian/diff": "^4.0"
},
"require-dev": {
"brainmaestro/composer-git-hooks": "^2.8",
Expand Down
145 changes: 73 additions & 72 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions src/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace AMWScan;

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

class Actions
{
/**
Expand Down Expand Up @@ -161,6 +164,9 @@ public static function openWithVim($file, $lineNumber = null)
self::makeBackup($file);
}

// Store original content for diff
$originalContent = file_get_contents($file);

$descriptors = [
['file', '/dev/tty', 'r'],
['file', '/dev/tty', 'w'],
Expand All @@ -178,6 +184,38 @@ public static function openWithVim($file, $lineNumber = null)
break;
}
}

// Show diff if file was modified
$newContent = file_get_contents($file);
if ($newContent !== $originalContent) {
self::showDiff($file, $originalContent, $newContent);
}
}

/**
* Show diff of changes made to a file.
*/
public static function showDiff($file, $originalContent, $newContent)
{
if ($originalContent === $newContent) {
return;
}

$builder = new UnifiedDiffOutputBuilder(
"--- Original\n+++ New\n", // header
true // show line numbers
);

$differ = new Differ($builder);
$diff = $differ->diff($originalContent, $newContent);

if (!empty($diff)) {
echo "\n" . str_repeat('=', 50) . "\n";
echo "Changes made to: $file\n";
echo str_repeat('=', 50) . "\n";
echo $diff;
echo "\n" . str_repeat('=', 50) . "\n\n";
}
}

/**
Expand All @@ -189,6 +227,9 @@ public static function openWithNano($file, $lineNumber = null)
self::makeBackup($file);
}

// Store original content for diff
$originalContent = file_get_contents($file);

$descriptors = [
['file', '/dev/tty', 'r'],
['file', '/dev/tty', 'w'],
Expand All @@ -206,6 +247,12 @@ public static function openWithNano($file, $lineNumber = null)
break;
}
}

// Show diff if file was modified
$newContent = file_get_contents($file);
if ($newContent !== $originalContent) {
self::showDiff($file, $originalContent, $newContent);
}
}

/**
Expand Down
21 changes: 19 additions & 2 deletions src/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,15 @@ private function scan($iterator)
Actions::openWithVim($filePath, $firstLineNumber);
self::$report['edited'][] = $filePath;
CLI::writeLine("File '$filePath' edited with vim!", 2, 'green');
self::$report['removed'][] = $filePath;

$markCleaned = 'n';
if (self::$prompt === null) {
$markCleaned = CLI::read('Mark file as cleaned [y|N]? ', 'purple');
}
if ($markCleaned === 'y') {
self::$report['removed'][] = $filePath;
$inLoop = false;
}
break;
// Open with nano
case in_array($confirmation, ['6', 'nano']):
Expand All @@ -1435,8 +1443,17 @@ private function scan($iterator)
Actions::openWithNano($filePath, $firstLineNumber);
self::$report['edited'][] = $filePath;
CLI::writeLine("File '$filePath' edited with nano!", 2, 'green');
self::$report['removed'][] = $filePath;

$markCleaned = 'n';
if (self::$prompt === null) {
$markCleaned = CLI::read('Mark file as cleaned [y|N]? ', 'purple');
}
if ($markCleaned === 'y') {
self::$report['removed'][] = $filePath;
$inLoop = false;
}
break;

// Add to whitelist
case in_array($confirmation, ['7', 'whitelist']):
if (Actions::addToWhitelist($filePath, $patternFound)) {
Expand Down
5 changes: 5 additions & 0 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

namespace AMWScan;

// Composer autoload
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}

// Autoload
spl_autoload_register(function ($name) {
$namespace = __NAMESPACE__ . '\\';
Expand Down
Loading