diff --git a/data/.gitignore b/data/.gitignore index 7d3317ca..f84f8da4 100644 --- a/data/.gitignore +++ b/data/.gitignore @@ -1,5 +1,3 @@ db.sqlite executed_upgrades.txt local.ini -thumbnails -posts diff --git a/public_html/.htaccess b/public_html/.htaccess index 7568c3df..41abf123 100644 --- a/public_html/.htaccess +++ b/public_html/.htaccess @@ -2,7 +2,17 @@ DirectoryIndex app.min.html DirectoryIndex index.html RewriteEngine On -RewriteRule ^/?api/(.*) api-dispatch.php?q=$1 [QSA,L] + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^/?data/posts/([^/]+)/? /api/posts/$1/content [L] + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^/?data/thumbnails/(\d+)x(\d+)/posts/([^/]+)/? /api/posts/$3/thumbnail/$1 [L] + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^/?data/thumbnails/(\d+)x(\d+)/avatars/([^/]+)/? /api/users/$3/avatar/$1 [L] + +RewriteRule ^/?api/(.*) api-dispatch.php?q=$0 [QSA,L] AddType text/html .tpl diff --git a/public_html/api-dispatch.php b/public_html/api-dispatch.php index 005e2848..d76ac8b1 100644 --- a/public_html/api-dispatch.php +++ b/public_html/api-dispatch.php @@ -2,5 +2,6 @@ $start = microtime(true); require_once(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'AutoLoader.php'); +$httpHelper = \Szurubooru\Injector::get(\Szurubooru\Helpers\HttpHelper::class); $dispatcher = \Szurubooru\Injector::get(\Szurubooru\Dispatcher::class); -$dispatcher->run(); +$dispatcher->run($httpHelper->getRequestMethod(), $_GET['q']); diff --git a/public_html/data/.gitignore b/public_html/data/.gitignore new file mode 100644 index 00000000..fed7cebe --- /dev/null +++ b/public_html/data/.gitignore @@ -0,0 +1,2 @@ +posts +thumbnails diff --git a/data/avatars/.gitignore b/public_html/data/avatars/.gitignore similarity index 100% rename from data/avatars/.gitignore rename to public_html/data/avatars/.gitignore diff --git a/data/avatars/blank.png b/public_html/data/avatars/blank.png similarity index 100% rename from data/avatars/blank.png rename to public_html/data/avatars/blank.png diff --git a/public_html/templates/user-list-item.tpl b/public_html/templates/user-list-item.tpl index 7300aeed..91900441 100644 --- a/public_html/templates/user-list-item.tpl +++ b/public_html/templates/user-list-item.tpl @@ -1,6 +1,6 @@
  • - <%= user.name %> + <%= user.name %>

    diff --git a/public_html/templates/user.tpl b/public_html/templates/user.tpl index 696cb134..434bc51b 100644 --- a/public_html/templates/user.tpl +++ b/public_html/templates/user.tpl @@ -3,7 +3,7 @@
    - Avatar + Avatar
    <%= user.name %>
    diff --git a/src/Config.php b/src/Config.php index 6d1c52f8..e2cef918 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,11 +4,13 @@ namespace Szurubooru; class Config extends \ArrayObject { private $dataDirectory; + private $publicDataDirectory; - public function __construct($dataDirectory) + public function __construct($dataDirectory, $publicDataDirectory) { $this->setFlags($this->getArrayObjectFlags()); $this->dataDirectory = $dataDirectory; + $this->publicDataDirectory = $publicDataDirectory; $this->tryLoadFromIni([ $dataDirectory . DIRECTORY_SEPARATOR . 'config.ini', $dataDirectory . DIRECTORY_SEPARATOR . 'local.ini']); @@ -31,6 +33,11 @@ class Config extends \ArrayObject return $this->dataDirectory; } + public function getPublicDataDirectory() + { + return $this->publicDataDirectory; + } + public function offsetGet($index) { if (!parent::offsetExists($index)) diff --git a/src/Dispatcher.php b/src/Dispatcher.php index 2ae70ae6..6236acc6 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -26,16 +26,14 @@ final class Dispatcher $controller->registerRoutes($router); } - public function run() + public function run($requestMethod, $requestUri) { global $start; try { $code = 200; $this->authorizeFromRequestHeader(); - $json = (array) $this->router->handle( - $this->httpHelper->getRequestMethod(), - $this->httpHelper->getRequestUri()); + $json = (array) $this->router->handle($requestMethod, $requestUri); } catch (\Exception $e) { diff --git a/src/Services/FileService.php b/src/Services/FileService.php index 8a0eb3ba..15adb4f6 100644 --- a/src/Services/FileService.php +++ b/src/Services/FileService.php @@ -8,7 +8,7 @@ class FileService public function __construct(\Szurubooru\Config $config, \Szurubooru\Helpers\HttpHelper $httpHelper) { - $this->dataDirectory = $config->getDataDirectory(); + $this->dataDirectory = $config->getPublicDataDirectory(); $this->httpHelper = $httpHelper; } diff --git a/src/di.php b/src/di.php index efb5c7c0..9ba7b4d7 100644 --- a/src/di.php +++ b/src/di.php @@ -1,8 +1,15 @@ DI\object()->constructor($dataDirectory), + \Szurubooru\Config::class => DI\object()->constructor($dataDirectory, $publicDataDirectory), \Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')), \Szurubooru\Upgrades\UpgradeRepository::class => DI\object()->constructor(DI\link('upgrades')), diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 0d28df13..64b8699e 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -8,9 +8,9 @@ abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock(); } - public function mockConfig($path = null) + public function mockConfig($dataPath = null, $publicDataPath = null) { - return new ConfigMock($path); + return new ConfigMock($dataPath, $publicDataPath); } public function mockTransactionManager() diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 111b92b0..7eb88abf 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -18,21 +18,21 @@ final class ConfigTest extends \Szurubooru\Tests\AbstractTestCase public function testReadingNonSections() { file_put_contents($this->baseConfigFilePath, 'test=value'); - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $this->assertEquals('value', $config->test); } public function testReadingUnnestedSections() { file_put_contents($this->baseConfigFilePath, '[test]' . PHP_EOL . 'key=value'); - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $this->assertEquals('value', $config->test->key); } public function testReadingNestedSections() { file_put_contents($this->baseConfigFilePath, '[test.subtest]' . PHP_EOL . 'key=value'); - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $this->assertEquals('value', $config->test->subtest->key); } @@ -42,14 +42,14 @@ final class ConfigTest extends \Szurubooru\Tests\AbstractTestCase $this->baseConfigFilePath, '[test.subtest]' . PHP_EOL . 'key=value' . PHP_EOL . '[test.subtest.deeptest]' . PHP_EOL . 'key=zombie'); - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $this->assertEquals('value', $config->test->subtest->key); $this->assertEquals('zombie', $config->test->subtest->deeptest->key); } public function testReadingNonExistentFiles() { - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $this->assertEquals(0, count(iterator_to_array($config->getIterator()))); } @@ -57,22 +57,27 @@ final class ConfigTest extends \Szurubooru\Tests\AbstractTestCase { file_put_contents($this->baseConfigFilePath, 'test=trash'); file_put_contents($this->localConfigFilePath, 'test=overridden'); - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $this->assertEquals('overridden', $config->test); } public function testReadingUnexistingProperties() { file_put_contents($this->baseConfigFilePath, 'meh=value'); - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $this->assertNull($config->unexistingSection); } public function testOverwritingValues() { file_put_contents($this->baseConfigFilePath, 'meh=value'); - $config = new \Szurubooru\Config($this->testDirectory); + $config = $this->getTestConfig(); $config->newKey = 'fast'; $this->assertEquals('fast', $config->newKey); } + + private function getTestConfig() + { + return new \Szurubooru\Config($this->testDirectory, null); + } } diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 621d84c8..d05701da 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -31,7 +31,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase $this->controllerRepositoryMock->method('getControllers')->willReturn([]); $dispatcher = $this->getDispatcher(); - $actual = $dispatcher->run(); + $actual = $dispatcher->run('GET', '/'); unset($actual['__time']); $this->assertEquals($expected, $actual); @@ -47,7 +47,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase $this->controllerRepositoryMock->method('getControllers')->willReturn([]); $dispatcher = $this->getDispatcher(); - $actual = $dispatcher->run(); + $actual = $dispatcher->run('GET', '/'); unset($actual['__time']); $this->assertEquals($expected, $actual); @@ -60,7 +60,7 @@ final class DispatcherTest extends \Szurubooru\Tests\AbstractTestCase $this->controllerRepositoryMock->method('getControllers')->willReturn([]); $dispatcher = $this->getDispatcher(); - $dispatcher->run(); + $dispatcher->run('GET', '/'); } private function getDispatcher() diff --git a/tests/PrivilegeTest.php b/tests/PrivilegeTest.php index a8520d47..04105b80 100644 --- a/tests/PrivilegeTest.php +++ b/tests/PrivilegeTest.php @@ -18,11 +18,7 @@ class PrivilegeTest extends \Szurubooru\Tests\AbstractTestCase $refl = new \ReflectionClass(\Szurubooru\Privilege::class); $constants = array_values($refl->getConstants()); - $dataPath = __DIR__ - . DIRECTORY_SEPARATOR . '..' - . DIRECTORY_SEPARATOR . 'data'; - - $config = new \Szurubooru\Config($dataPath); + $config = \Szurubooru\Injector::get(\Szurubooru\Config::class); foreach ($config->security->privileges as $key => $value) { $this->assertTrue(in_array($key, $constants), "$key not in constants"); diff --git a/tests/Services/FileServiceTest.php b/tests/Services/FileServiceTest.php index fc22f09a..7248bda9 100644 --- a/tests/Services/FileServiceTest.php +++ b/tests/Services/FileServiceTest.php @@ -6,7 +6,7 @@ class FileServiceTest extends \Szurubooru\Tests\AbstractTestCase public function testSaving() { $testDirectory = $this->createTestDirectory(); - $configMock = $this->mockConfig($testDirectory); + $configMock = $this->mockConfig(null, $testDirectory); $httpHelper = $this->mock(\Szurubooru\Helpers\HttpHelper::class); $fileService = new \Szurubooru\Services\FileService($configMock, $httpHelper); $fileService->save('dog.txt', 'awesome dog'); diff --git a/tests/Services/ThumbnailServiceTest.php b/tests/Services/ThumbnailServiceTest.php index 06d7662a..1b1753c0 100644 --- a/tests/Services/ThumbnailServiceTest.php +++ b/tests/Services/ThumbnailServiceTest.php @@ -15,7 +15,7 @@ class ThumbnailServiceTest extends \Szurubooru\Tests\AbstractTestCase touch($tempDirectory . DS . 'thumbnails' . DS . '5x5' . DS . 'keep'); touch($tempDirectory . DS . 'thumbnails' . DS . '10x10' . DS . 'remove'); - $configMock = $this->mockConfig($tempDirectory); + $configMock = $this->mockConfig(null, $tempDirectory); $httpHelperMock = $this->mock(\Szurubooru\Helpers\HttpHelper::class); $fileService = new \Szurubooru\Services\FileService($configMock, $httpHelperMock); $thumbnailGeneratorMock = $this->mock(\Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator::class);