diff --git a/core/Plugin/Manager.php b/core/Plugin/Manager.php index 77b058b11384245812cd3d6e3b56abdbecc1f1d9..010838acf8243b9847342f2b881d307a264725ae 100644 --- a/core/Plugin/Manager.php +++ b/core/Plugin/Manager.php @@ -499,7 +499,7 @@ class Manager { $existingPlugins = $this->readPluginsDirectory(); $isPluginInFilesystem = array_search($pluginName, $existingPlugins) !== false; - return Filesystem::isValidFilename($pluginName) + return $this->isValidPluginName($pluginName) && $isPluginInFilesystem; } @@ -896,6 +896,11 @@ class Manager return $newPlugin; } + public function isValidPluginName($pluginName) + { + return (bool) preg_match('/^[a-zA-Z]([a-zA-Z0-9]*)$/D', $pluginName); + } + /** * @param $pluginName * @return Plugin @@ -906,8 +911,8 @@ class Manager $pluginFileName = sprintf("%s/%s.php", $pluginName, $pluginName); $pluginClassName = $pluginName; - if (!Filesystem::isValidFilename($pluginName)) { - throw new \Exception(sprintf("The plugin filename '%s' is not a valid filename", $pluginFileName)); + if (!$this->isValidPluginName($pluginName)) { + throw new \Exception(sprintf("The plugin filename '%s' is not a valid plugin name", $pluginFileName)); } $path = self::getPluginsDirectory() . $pluginFileName; diff --git a/plugins/CoreConsole/Commands/GeneratePlugin.php b/plugins/CoreConsole/Commands/GeneratePlugin.php index 27fb39df5a7a5c2c50a79d93571ba517c096e555..bf83889305e793d3be703a210b3900d044dda4e2 100644 --- a/plugins/CoreConsole/Commands/GeneratePlugin.php +++ b/plugins/CoreConsole/Commands/GeneratePlugin.php @@ -12,6 +12,7 @@ namespace Piwik\Plugins\CoreConsole\Commands; use Piwik\Filesystem; use Piwik\Plugins\ExamplePlugin\ExamplePlugin; use Piwik\Version; +use Piwik\Plugin; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -125,8 +126,8 @@ class GeneratePlugin extends GeneratePluginBase throw new \RuntimeException('You have to enter a plugin name'); } - if (!Filesystem::isValidFilename($pluginName)) { - throw new \RuntimeException(sprintf('The plugin name %s is not valid', $pluginName)); + if (!Plugin\Manager::getInstance()->isValidPluginName($pluginName)) { + throw new \RuntimeException(sprintf('The plugin name %s is not valid. The name must start with a letter and is only allowed to contain numbers and letters.', $pluginName)); } $pluginPath = $self->getPluginPath($pluginName); diff --git a/tests/PHPUnit/Integration/Plugin/ManagerTest.php b/tests/PHPUnit/Integration/Plugin/ManagerTest.php index d3fea6935c83ed893e0bb1400d073052b160625e..2a36d3462c0e9cb14b46a7433857254e1b5faa02 100644 --- a/tests/PHPUnit/Integration/Plugin/ManagerTest.php +++ b/tests/PHPUnit/Integration/Plugin/ManagerTest.php @@ -84,6 +84,36 @@ class ManagerTest extends IntegrationTestCase $this->assertFalse($this->manager->isPluginActivated('ExampleTheme')); } + /** + * @dataProvider getPluginNameProvider + */ + public function test_isValidPluginName($expectedIsValid, $pluginName) + { + $valid = $this->manager->isValidPluginName($pluginName); + $this->assertSame($expectedIsValid, $valid); + } + + public function getPluginNameProvider() + { + return array( + array(true, 'a'), + array(true, 'a0'), + array(true, 'pluginNameTest'), + array(true, 'PluginNameTest'), + array(true, 'PluginNameTest92323232eerwrwere938'), + array(false, ''), + array(false, '0'), + array(false, '0a'), + array(false, 'a.'), + array(false, 'a-'), + array(false, 'a_'), + array(false, 'a-ererer'), + array(false, 'a_ererer'), + array(false, '..'), + array(false, '/'), + ); + } + private function getCacheForTrackerPlugins() { return PiwikCache::getEagerCache();