diff --git a/banner/banner.php b/banner/banner.php
index 94e8bbd7..d2fe0818 100644
--- a/banner/banner.php
+++ b/banner/banner.php
@@ -10,32 +10,29 @@
namespace phpbb\ads\banner;
+use phpbb\storage\storage;
+
class banner
{
/** @var \phpbb\files\upload */
protected $files_upload;
- /** @var \phpbb\filesystem\filesystem_interface */
- protected $filesystem;
-
- /** @var string */
- protected $root_path;
-
- /** @var \phpbb\files\filespec */
+ /** @var \phpbb\files\filespec_storage */
protected $file;
+ /** @var storage */
+ protected $storage;
+
/**
* Constructor
*
- * @param \phpbb\files\upload $files_upload Files upload object
- * @param \phpbb\filesystem\filesystem_interface $filesystem Filesystem object
- * @param string $root_path Root path
+ * @param \phpbb\files\upload $files_upload Files upload object
+ * @param storage $storage Storage object
*/
- public function __construct(\phpbb\files\upload $files_upload, \phpbb\filesystem\filesystem_interface $filesystem, $root_path)
+ public function __construct(\phpbb\files\upload $files_upload, storage $storage)
{
$this->files_upload = $files_upload;
- $this->filesystem = $filesystem;
- $this->root_path = $root_path;
+ $this->storage = $storage;
}
public function set_file($file)
@@ -43,19 +40,6 @@ public function set_file($file)
$this->file = $file;
}
- /**
- * Create storage directory for banners uploaded by Ads Management
- *
- * @throws \phpbb\filesystem\exception\filesystem_exception
- */
- public function create_storage_dir()
- {
- if (!$this->filesystem->exists($this->root_path . 'images/phpbb_ads'))
- {
- $this->filesystem->mkdir($this->root_path . 'images/phpbb_ads');
- }
- }
-
/**
* Handle banner upload
*
@@ -69,11 +53,11 @@ public function upload()
$this->files_upload->set_allowed_extensions(array('gif', 'jpg', 'jpeg', 'png'));
// Upload file
- $this->set_file($this->files_upload->handle_upload('files.types.form', 'banner'));
+ $this->set_file($this->files_upload->handle_upload('files.types.form_storage', 'banner'));
$this->file->clean_filename('unique_ext');
// Move file to proper location
- if (!$this->file->move_file('images/phpbb_ads'))
+ if (!$this->file->move_file($this->storage))
{
$this->file->set_error('FILE_MOVE_UNSUCCESSFUL');
}
@@ -91,6 +75,6 @@ public function upload()
*/
public function remove()
{
- $this->file->remove();
+ $this->file->remove($this->storage);
}
}
diff --git a/config/routing.yml b/config/routing.yml
index 47de1f2f..8aaa97a2 100644
--- a/config/routing.yml
+++ b/config/routing.yml
@@ -15,3 +15,8 @@ phpbb_ads_visual_demo:
defaults: { _controller: phpbb.ads.visual_demo.controller:handle }
requirements:
action: enable|disable
+
+phpbb_ads_storage_banner:
+ path: /ads_download/{file}
+ defaults:
+ _controller: phpbb.ads.controller.banner:handle
diff --git a/config/services.yml b/config/services.yml
index 30708d2b..ce75317d 100644
--- a/config/services.yml
+++ b/config/services.yml
@@ -4,6 +4,24 @@ imports:
- { resource: location.yml }
services:
+ phpbb.ads.storage:
+ class: phpbb\storage\storage
+ arguments:
+ - '@storage.adapter.factory'
+ - '@storage.file_tracker'
+ - 'phpbb_ads'
+ tags:
+ - { name: storage }
+
+ phpbb.ads.controller.banner:
+ class: phpbb\storage\controller\controller
+ arguments:
+ - '@cache'
+ - '@dbal.conn'
+ - '@mimetype.extension_guesser'
+ - '@phpbb.ads.storage'
+ - '@symfony_request'
+
phpbb.ads.ad.manager:
class: phpbb\ads\ad\manager
arguments:
@@ -17,9 +35,7 @@ services:
class: phpbb\ads\banner\banner
arguments:
- '@files.upload'
- - '@filesystem'
- - '%core.root_path%'
- - '%phpbb.ads.tables.ad_locations%'
+ - '@phpbb.ads.storage'
phpbb.ads.helper:
class: phpbb\ads\controller\helper
@@ -38,6 +54,7 @@ services:
phpbb.ads.admin.input:
class: phpbb\ads\controller\admin_input
arguments:
+ - '@controller.helper'
- '@user'
- '@user_loader'
- '@language'
diff --git a/controller/admin_input.php b/controller/admin_input.php
index ccda2c3e..ecaff3d0 100644
--- a/controller/admin_input.php
+++ b/controller/admin_input.php
@@ -17,6 +17,9 @@
*/
class admin_input
{
+ /** @var \phpbb\controller\helper */
+ protected $controller_helper;
+
/** @var \phpbb\user */
protected $user;
@@ -38,14 +41,16 @@ class admin_input
/**
* Constructor
*
- * @param \phpbb\user $user User object
- * @param \phpbb\user_loader $user_loader User loader object
- * @param \phpbb\language\language $language Language object
- * @param \phpbb\request\request $request Request object
- * @param \phpbb\ads\banner\banner $banner Banner upload object
+ * @param \phpbb\controller\helper $controller_helper Controller helper object
+ * @param \phpbb\user $user User object
+ * @param \phpbb\user_loader $user_loader User loader object
+ * @param \phpbb\language\language $language Language object
+ * @param \phpbb\request\request $request Request object
+ * @param \phpbb\ads\banner\banner $banner Banner upload object
*/
- public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
+ public function __construct(\phpbb\controller\helper $controller_helper, \phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
{
+ $this->controller_helper = $controller_helper;
$this->user = $user;
$this->user_loader = $user_loader;
$this->language = $language;
@@ -134,10 +139,9 @@ public function banner_upload($ad_code)
{
try
{
- $this->banner->create_storage_dir();
$realname = $this->banner->upload();
- $banner_html = '
';
+ $banner_html = '
';
if ($this->request->is_ajax())
{
diff --git a/language/en/common.php b/language/en/common.php
index ebe45c83..43dd711c 100644
--- a/language/en/common.php
+++ b/language/en/common.php
@@ -66,4 +66,7 @@
'AD_SLIDE_UP_DESC' => 'Displays on every page after user scrolls below main content. Slides up from the bottom.',
'AD_SCRIPTS' => 'Scripts',
'AD_SCRIPTS_DESC' => 'This location is for specialty JavaScript code like AdSense Auto ads, tracking codes, etc. The code entered here will be inserted into the page’s HEAD tag and is not intended for ad placement, but only for helper scripts.',
+
+ // Storage
+ 'STORAGE_PHPBB_ADS_TITLE' => 'phpBB Ads',
));
diff --git a/migrations/v30x/m1_storage.php b/migrations/v30x/m1_storage.php
new file mode 100644
index 00000000..31244fdb
--- /dev/null
+++ b/migrations/v30x/m1_storage.php
@@ -0,0 +1,114 @@
+
+ * @license GNU General Public License, version 2 (GPL-2.0)
+ *
+ */
+
+namespace phpbb\ads\migrations\v30x;
+
+use phpbb\filesystem\filesystem;
+use phpbb\storage\provider\local;
+
+class m1_storage extends \phpbb\db\migration\container_aware_migration
+{
+ private const BATCH_SIZE = 100;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function effectively_installed()
+ {
+ /** @var filesystem $filesystem_interface */
+ $filesystem = $this->container->get('filesystem');
+
+ return $this->config->offsetExists('storage\\phpbb_ads\\provider') &&
+ $this->config->offsetExists('storage\\phpbb_ads\\config\\path') &&
+ $filesystem->exists($this->phpbb_root_path . 'images/phpbb_ads');
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public static function depends_on()
+ {
+ return [
+ '\phpbb\db\migration\data\v400\dev',
+ '\phpbb\ads\migrations\v20x\m1_hide_ad_for_group',
+ ];
+ }
+
+ public function update_data()
+ {
+ return [
+ ['config.add', ['storage\\phpbb_ads\\provider', local::class]],
+ ['config.add', ['storage\\phpbb_ads\\config\\path', 'images/phpbb_ads']],
+ ['custom', [[$this, 'migrate_ads_storage']]],
+ ];
+ }
+
+ public function migrate_ads_storage()
+ {
+ /** @var filesystem $filesystem_interface */
+ $filesystem = $this->container->get('filesystem');
+
+ /** @var file_tracker $file_tracker */
+ $file_tracker = $this->container->get('storage.file_tracker');
+
+ $dir = $this->phpbb_root_path . 'images/phpbb_ads';
+
+ if (!$filesystem->exists($dir))
+ {
+ $filesystem->mkdir($dir);
+ }
+
+ $handle = @opendir($dir);
+
+ if ($handle)
+ {
+ $files = [];
+ while (($file = readdir($handle)) !== false)
+ {
+ if ($file === '.' || $file === '..')
+ {
+ continue;
+ }
+
+ $files[] = [
+ 'file_path' => $file,
+ 'filesize' => filesize($dir . '/' . $file),
+ ];
+
+ if (count($files) >= self::BATCH_SIZE)
+ {
+ $file_tracker->track_files('phpbb_ads', $files);
+ $files = [];
+ }
+ }
+
+ if (!empty($files))
+ {
+ $file_tracker->track_files('phpbb_ads', $files);
+ }
+
+ closedir($handle);
+ }
+ }
+
+ public function revert_data()
+ {
+ return [
+ ['config.remove', ['storage\\phpbb_ads\\provider']],
+ ['config.remove', ['storage\\phpbb_ads\\config\\path']],
+ ['custom', [[$this, 'revert_ads_storage']]],
+ ];
+ }
+
+ public function revert_ads_storage()
+ {
+ $this->sql_query('DELETE FROM ' . $this->tables['storage'] . ' WHERE storage = "phpbb_ads"');
+ }
+}
diff --git a/tests/banner/banner_base.php b/tests/banner/banner_base.php
index ec60431b..9ced323f 100644
--- a/tests/banner/banner_base.php
+++ b/tests/banner/banner_base.php
@@ -15,14 +15,12 @@ class banner_base extends \phpbb_test_case
/** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\files\upload */
protected $files_upload;
- /** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\filesystem\filesystem */
- protected $filesystem;
+ /** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\files\filespec_storage */
+ protected $file;
- /** @var string */
- protected $root_path;
+ /** @var \phpbb\storage\storage */
- /** @var \PHPUnit\Framework\MockObject\MockObject|\phpbb\files\filespec */
- protected $file;
+ protected $storage;
protected static function setup_extensions()
{
@@ -36,16 +34,12 @@ protected function setUp(): void
{
parent::setUp();
- global $phpbb_root_path;
-
$this->files_upload = $this->getMockBuilder('\phpbb\files\upload')
->disableOriginalConstructor()
->getMock();
- $this->filesystem = $this->getMockBuilder('\phpbb\filesystem\filesystem')
+ $this->storage = $this->getMockBuilder('\phpbb\storage\storage')
->disableOriginalConstructor()
->getMock();
-
- $this->root_path = $phpbb_root_path;
}
/**
@@ -57,8 +51,7 @@ public function get_manager()
{
return new \phpbb\ads\banner\banner(
$this->files_upload,
- $this->filesystem,
- $this->root_path
+ $this->storage
);
}
}
diff --git a/tests/banner/create_storage_dir_test.php b/tests/banner/create_storage_dir_test.php
deleted file mode 100644
index efd790e0..00000000
--- a/tests/banner/create_storage_dir_test.php
+++ /dev/null
@@ -1,61 +0,0 @@
-
- * @license GNU General Public License, version 2 (GPL-2.0)
- *
- */
-
-namespace phpbb\ads\tests\banner;
-
-class create_storage_dir_test extends banner_base
-{
- /**
- * Test data provider for test_create_storage_dir()
- *
- * @return array Array of test data
- */
- public function create_storage_dir_data()
- {
- return array(
- array(false, false),
- array(false, true),
- array(true, false),
- array(true, true),
- );
- }
-
- /**
- * Test create_storage_dir() method
- *
- * @dataProvider create_storage_dir_data
- */
- public function test_create_storage_dir($dir_exists, $success)
- {
- $manager = $this->get_manager();
-
- $this->filesystem->expects(self::once())
- ->method('exists')
- ->with($this->root_path . 'images/phpbb_ads')
- ->willReturn($dir_exists);
-
- if (!$dir_exists)
- {
- $mkdir = $this->filesystem->expects(self::once())
- ->method('mkdir')
- ->with($this->root_path . 'images/phpbb_ads');
-
- if (!$success)
- {
- $mkdir->willThrowException(new \phpbb\filesystem\exception\filesystem_exception('CANNOT_CREATE_DIRECTORY'));
-
- $this->expectException('\phpbb\filesystem\exception\filesystem_exception');
- $this->expectExceptionMessage('CANNOT_CREATE_DIRECTORY');
- }
- }
-
- $manager->create_storage_dir();
- }
-}
diff --git a/tests/banner/remove_test.php b/tests/banner/remove_test.php
index d0d05ed6..14ae56d2 100644
--- a/tests/banner/remove_test.php
+++ b/tests/banner/remove_test.php
@@ -20,13 +20,14 @@ public function test_remove()
$manager = $this->get_manager();
// Mock filespec
- $file = $this->getMockBuilder('\phpbb\files\filespec')
+ $file = $this->getMockBuilder('\phpbb\files\filespec_storage')
->disableOriginalConstructor()
->getMock();
$manager->set_file($file);
$file->expects(self::once())
- ->method('remove');
+ ->method('remove')
+ ->with($this->storage);
$manager->remove();
}
diff --git a/tests/banner/upload_test.php b/tests/banner/upload_test.php
index 63e0f368..19bcb933 100644
--- a/tests/banner/upload_test.php
+++ b/tests/banner/upload_test.php
@@ -42,7 +42,7 @@ public function test_upload($file_move_success)
->with(array('gif', 'jpg', 'jpeg', 'png'));
// Mock filespec
- $file = $this->getMockBuilder('\phpbb\files\filespec')
+ $file = $this->getMockBuilder('\phpbb\files\filespec_storage')
->disableOriginalConstructor()
->getMock();
if (!$file_move_success)
@@ -52,7 +52,7 @@ public function test_upload($file_move_success)
$this->files_upload->expects(self::once())
->method('handle_upload')
- ->with('files.types.form', 'banner')
+ ->with('files.types.form_storage', 'banner')
->willReturn($file);
$file->expects(self::once())
@@ -61,7 +61,7 @@ public function test_upload($file_move_success)
$file->expects(self::once())
->method('move_file')
- ->with('images/phpbb_ads')
+ ->with($this->storage)
->willReturn($file_move_success);
if (!$file_move_success)
diff --git a/tests/controller/admin_input_test.php b/tests/controller/admin_input_test.php
index 07ba124e..d28d18d1 100644
--- a/tests/controller/admin_input_test.php
+++ b/tests/controller/admin_input_test.php
@@ -15,6 +15,9 @@ class admin_input_test extends \phpbb_database_test_case
/** @var bool A return value for check_form_key() */
public static $valid_form = true;
+ /** @var \phpbb\controller\helper */
+ protected $controller_helper;
+
/** @var \phpbb\user */
protected $user;
@@ -60,6 +63,9 @@ protected function setUp(): void
// Load/Mock classes required by the controller class
$this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx));
+ $this->controller_helper = $this->getMockBuilder('\phpbb\controller\helper')
+ ->disableOriginalConstructor()
+ ->getMock();
$this->user = $user = new \phpbb\user($this->language, '\phpbb\datetime');
$this->user->timezone = new \DateTimeZone('UTC');
$avatar_helper = $this->getMockBuilder('\phpbb\avatar\helper')
@@ -92,6 +98,7 @@ protected function setUp(): void
public function get_input_controller()
{
$input = new \phpbb\ads\controller\admin_input(
+ $this->controller_helper,
$this->user,
$this->user_loader,
$this->language,
@@ -191,9 +198,6 @@ public function test_get_form_data($valid_form, $data, $ad_owner_expected, $erro
public function banner_upload_data()
{
return array(
- array(false, false, false, array('CANNOT_CREATE_DIRECTORY'), '', ''),
- array(false, true, false, array('CANNOT_CREATE_DIRECTORY'), '', ''),
- array(false, true, true, array('CANNOT_CREATE_DIRECTORY'), '', ''),
array(true, false, false, array('FILE_MOVE_UNSUCCESSFUL'), '', ''),
array(true, true, false, array(), '', '
'),
array(true, true, false, array(), 'abc', "abc\n\n
"),
@@ -210,24 +214,15 @@ public function test_banner_upload($can_create_directory, $can_move_file, $is_aj
{
$input_controller = $this->get_input_controller();
- $create_storage_dir = $this->banner->expects(self::once())
- ->method('create_storage_dir');
- if (!$can_create_directory)
+ $upload = $this->banner->expects(self::once())
+ ->method('upload');
+ if (!$can_move_file)
{
- $create_storage_dir->willThrowException(new \phpbb\exception\runtime_exception('CANNOT_CREATE_DIRECTORY'));
+ $upload->willThrowException(new \phpbb\exception\runtime_exception('FILE_MOVE_UNSUCCESSFUL'));
}
else
{
- $upload = $this->banner->expects(self::once())
- ->method('upload');
- if (!$can_move_file)
- {
- $upload->willThrowException(new \phpbb\exception\runtime_exception('FILE_MOVE_UNSUCCESSFUL'));
- }
- else
- {
- $upload->willReturn('abcdef.jpg');
- }
+ $upload->willReturn('abcdef.jpg');
}
if (!$can_create_directory || !$can_move_file)
@@ -236,6 +231,9 @@ public function test_banner_upload($can_create_directory, $can_move_file, $is_aj
->method('remove');
}
+ $this->controller_helper->method('route')
+ ->willReturn('/images/phpbb_ads/abcdef.jpg');
+
$this->request->expects(self::once())
->method('is_ajax')
->willReturn($is_ajax);