Add an uglyUrls option that disables URL rewriting (fixes #88)

This commit is contained in:
Pierre Rudloff 2017-01-10 23:37:29 +01:00
parent 4562c88859
commit 9f112c15b9
8 changed files with 162 additions and 0 deletions

View file

@ -75,6 +75,12 @@ class Config
*/
public $curl_params = [];
/**
* Disable URL rewriting.
* @var boolean
*/
public $uglyUrls = false;
/**
* YAML config file path.
*

60
classes/UglyRouter.php Normal file
View file

@ -0,0 +1,60 @@
<?php
/**
* UglyRouter class.
*/
namespace Alltube;
use Slim\Router;
use Psr\Http\Message\ServerRequestInterface;
/**
* Extend Slim's router class in order to disable URL rewriting.
*/
class UglyRouter extends Router
{
/**
* Dispatch router for HTTP request
*
* @param ServerRequestInterface $request The current HTTP request object
*
* @return array
*
* @link https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php
*/
public function dispatch(ServerRequestInterface $request)
{
parse_str($request->getUri()->getQuery(), $args);
$uri = '/';
if (isset($args['page'])) {
$uri .= $args['page'];
}
return $this->createDispatcher()->dispatch(
$request->getMethod(),
$uri
);
}
/**
* Build the path for a named route including the base path
*
* @param string $name Route name
* @param array $data Named argument replacement data
* @param array $queryParams Optional query string parameters
*
* @return string
*
* @throws \RuntimeException If named route does not exist
* @throws \InvalidArgumentException If required data not provided
*/
public function pathFor($name, array $data = [], array $queryParams = [])
{
$url = str_replace('/', '/?page=', $this->relativePathFor($name, $data, $queryParams));
if ($this->basePath) {
$url = $this->basePath . $url;
}
return $url;
}
}

View file

@ -11,3 +11,4 @@ convert: false
avconv: vendor/bin/ffmpeg
rtmpdump: vendor/bin/rtmpdump
curl: /usr/bin/curl
uglyUrls: false

View file

@ -85,6 +85,7 @@ class FrontController
'index.tpl',
[
'convert' => $this->config->convert,
'uglyUrls' => $this->config->uglyUrls,
'class' => 'index',
'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.',
]

View file

@ -2,6 +2,8 @@
require_once __DIR__.'/vendor/autoload.php';
use Alltube\Controller\FrontController;
use Alltube\Config;
use Alltube\UglyRouter;
if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.php') !== false) {
header('Location: '.str_ireplace('/index.php', '/', $_SERVER['REQUEST_URI']));
@ -10,6 +12,10 @@ if (isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/index.ph
$app = new \Slim\App();
$container = $app->getContainer();
$config = Config::getInstance();
if ($config->uglyUrls) {
$container['router'] = new UglyRouter();
}
$container['view'] = function ($c) {
$view = new \Slim\Views\Smarty(__DIR__.'/templates/');

View file

@ -12,6 +12,9 @@
<input class="URLinput" type="url" name="url" id="url"
required autofocus placeholder="http://example.com/video" />
</span>
{if uglyUrls}
<input type="hidden" name="page" value="video" />
{/if}
<input class="downloadBtn" type="submit" value="Download" /><br/>
{if $convert}
<div class="mp3">

View file

@ -24,6 +24,9 @@
<h3><label for="format">Available formats:</label></h3>
<form action="{path_for name="redirect"}">
<input type="hidden" name="url" value="{$video->webpage_url}" />
{if uglyUrls}
<input type="hidden" name="page" value="redirect" />
{/if}
<select name="format" id="format" class="formats monospace">
<optgroup label="Generic formats">
<option value="best[protocol^=http]">

82
tests/UglyRouterTest.php Normal file
View file

@ -0,0 +1,82 @@
<?php
/**
* UglyRouterTest class.
*/
namespace Alltube\Test;
use Alltube\UglyRouter;
use Slim\Http\Request;
use Slim\Http\Uri;
use Slim\Http\Headers;
use Slim\Http\Environment;
use Slim\Http\Stream;
/**
* Unit tests for the UglyRouter class.
*/
class UglyRouterTest extends \PHPUnit_Framework_TestCase
{
/**
* UglyRouter instance.
*
* @var UglyRouter
*/
private $router;
/**
* Prepare tests.
*/
protected function setUp()
{
$this->router = new UglyRouter();
$this->router->map(['GET'], '/foo', 'print')->setName('foo');
}
/**
* Test the dispatch() function.
* @return void
*/
public function testDispatch()
{
$this->assertEquals(
[1, 'route0', []],
$this->router->dispatch(
new Request(
'GET',
Uri::createFromString('http://example.com/?page=foo'),
Headers::createFromEnvironment(new Environment()),
[],
[],
new Stream(fopen('php://temp', 'r'))
)
)
);
}
/**
* Test the pathFor() function.
* @return void
*/
public function testPathFor()
{
$this->assertEquals(
'/?page=foo',
$this->router->pathFor('foo', [], [])
);
}
/**
* Test the pathFor() function with a base path.
* @return void
*/
public function testPathForWithBasePath()
{
$this->router->setBasePath('/bar');
$this->assertEquals(
'/bar/?page=foo',
$this->router->pathFor('foo', [], [])
);
}
}