Permalink
Browse files

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

  • Loading branch information...
1 parent 4562c88 commit 9f112c15b9948a548a297b077c2ff2a2b4a3c3ed @Rudloff committed Jan 10, 2017
Showing with 162 additions and 0 deletions.
  1. +6 −0 classes/Config.php
  2. +60 −0 classes/UglyRouter.php
  3. +1 −0 config.example.yml
  4. +1 −0 controllers/FrontController.php
  5. +6 −0 index.php
  6. +3 −0 templates/index.tpl
  7. +3 −0 templates/video.tpl
  8. +82 −0 tests/UglyRouterTest.php
View
@@ -76,6 +76,12 @@ class Config
public $curl_params = [];
/**
+ * Disable URL rewriting.
+ * @var boolean
+ */
+ public $uglyUrls = false;
+
+ /**
* YAML config file path.
*
* @var string
@@ -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
@@ -11,3 +11,4 @@ convert: false
avconv: vendor/bin/ffmpeg
rtmpdump: vendor/bin/rtmpdump
curl: /usr/bin/curl
+uglyUrls: false
@@ -85,6 +85,7 @@ public function index(Request $request, Response $response)
'index.tpl',
[
'convert' => $this->config->convert,
+ 'uglyUrls' => $this->config->uglyUrls,
'class' => 'index',
'description' => 'Easily download videos from Youtube, Dailymotion, Vimeo and other websites.',
]
View
@@ -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 @@
$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
@@ -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
@@ -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]">
@@ -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', [], [])
+ );
+ }
+}

0 comments on commit 9f112c1

Please sign in to comment.