Skip to content

Commit

Permalink
Resolves #113, split into multiple commands
Browse files Browse the repository at this point in the history
  • Loading branch information
steverobbins committed Oct 21, 2015
1 parent cbc5d4d commit 97e5ed5
Show file tree
Hide file tree
Showing 12 changed files with 845 additions and 401 deletions.
156 changes: 156 additions & 0 deletions src/MageScan/Command/Scan/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Mage Scan
*
* PHP version 5
*
* @category MageScan
* @package MageScan
* @author Steve Robbins <[email protected]>
* @copyright 2015 Steve Robbins
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
* @link https://github.com/steverobbins/magescan
*/

namespace MageScan\Command\Scan;

use MageScan\Request;
use MageScan\Url;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Abstract scan command
*
* @category MageScan
* @package MageScan
* @author Steve Robbins <[email protected]>
* @copyright 2015 Steve Robbins
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
* @link https://github.com/steverobbins/magescan
*/
abstract class AbstractCommand extends Command
{
/**
* Input object
*
* @var \Symfony\Component\Console\Input\InputInterface
*/
protected $input;

/**
* Output object
*
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;

/**
* URL of Magento site
*
* @var string
*/
protected $url;

/**
* Cached request object with desired secure flag
*
* @var \MageScan\Request
*/
protected $request;

/**
* Configure command
*
* @return void
*/
protected function configure()
{
$this
->addArgument(
'url',
InputArgument::REQUIRED,
'The URL of the Magento application'
)
->addOption(
'insecure',
'k',
InputOption::VALUE_NONE,
'Don\'t validate SSL certificate if URL is https'
);
}

/**
* Initialize command
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->request = new Request;
$this->request->setInsecure($this->input->getOption('insecure'));

$style = new OutputFormatterStyle('white', 'blue', array('bold'));
$this->output->getFormatter()->setStyle('header', $style);

$this->setUrl($input->getArgument('url'));
}

/**
* Validate and set url
*
* @param string $input
*
* @return void
* @throws InvalidArgumentException
*/
protected function setUrl($input)
{
if (empty(trim($input))) {
throw new \InvalidArgumentException(
'Target URL not specified'
);
}
$url = new Url;
$this->url = $url->clean($input);
$request = $this->request;
$response = $request->fetch($this->url, array(
CURLOPT_NOBODY => true
));
if ($response->code == 0) {
throw new \InvalidArgumentException(
'Could not connect to URL: ' . $this->url
);
}
if (isset($response->header['Location'])) {
$this->url = $response->header['Location'];
}
}

/**
* Write a header block
*
* @param string $text
* @param string $style
*
* @return void
*/
protected function writeHeader($text, $style = 'bg=blue;fg=white')
{
$this->output->writeln(array(
'',
$this->getHelperSet()->get('formatter')
->formatBlock($text, $style, true),
'',
));
}
}
85 changes: 85 additions & 0 deletions src/MageScan/Command/Scan/AllCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Mage Scan
*
* PHP version 5
*
* @category MageScan
* @package MageScan
* @author Steve Robbins <[email protected]>
* @copyright 2015 Steve Robbins
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
* @link https://github.com/steverobbins/magescan
*/

namespace MageScan\Command\Scan;

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Run all scan commands
*
* @category MageScan
* @package MageScan
* @author Steve Robbins <[email protected]>
* @copyright 2015 Steve Robbins
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
* @link https://github.com/steverobbins/magescan
*/
class AllCommand extends AbstractCommand
{
/**
* Configure command
*
* @return void
*/
protected function configure()
{
$this
->setName('scan:all')
->setDescription('Run all scans')
->addOption(
'show-modules',
null,
InputOption::VALUE_NONE,
'Show all modules that were scanned for, not just matches'
);
parent::configure();
}

/**
* Execute command
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output->writeln('Scanning <info>' . $this->url . '</info>...');
foreach ([
'scan:version',
'scan:module',
'scan:catalog',
'scan:patch',
'scan:sitemap',
'scan:server',
'scan:unreachable',
] as $commandName) {
$command = $this->getApplication()->find($commandName);
$args = [
'command' => $commandName,
'url' => $input->getArgument('url')
];
if ($commandName === 'scan:module' && $input->getOption('show-modules')) {
$args['--show-modules'] = true;
}
$command->run(new ArrayInput($args), $output);
}
}
}
77 changes: 77 additions & 0 deletions src/MageScan/Command/Scan/CatalogCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Mage Scan
*
* PHP version 5
*
* @category MageScan
* @package MageScan
* @author Steve Robbins <[email protected]>
* @copyright 2015 Steve Robbins
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
* @link https://github.com/steverobbins/magescan
*/

namespace MageScan\Command\Scan;

use MageScan\Check\Catalog;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Scan catalog command
*
* @category MageScan
* @package MageScan
* @author Steve Robbins <[email protected]>
* @copyright 2015 Steve Robbins
* @license http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
* @link https://github.com/steverobbins/magescan
*/
class CatalogCommand extends AbstractCommand
{
/**
* Configure command
*
* @return void
*/
protected function configure()
{
$this
->setName('scan:catalog')
->setDescription('Get catalog information');
parent::configure();
}

/**
* Execute command
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->writeHeader('Catalog Information');
$rows = array();
$catalog = new Catalog;
$catalog->setRequest($this->request);
$categoryCount = $catalog->categoryCount($this->url);
$rows[] = array(
'Categories',
$categoryCount !== false ? $categoryCount : 'Unknown'
);
$productCount = $catalog->productCount($this->url);
$rows[] = array(
'Products',
$productCount !== false ? $productCount : 'Unknown'
);
$table = new Table($this->output);
$table
->setHeaders(array('Type', 'Count'))
->setRows($rows)
->render();
}
}
Loading

0 comments on commit 97e5ed5

Please sign in to comment.