aboutsummaryrefslogtreecommitdiff
path: root/actions/DetectAction.php
blob: 1ed002afd56b545fa880c11dc13b81926fb46f7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

/**
 * This file is part of RSS-Bridge, a PHP project capable of generating RSS and
 * Atom feeds for websites that don't have one.
 *
 * For the full license information, please view the UNLICENSE file distributed
 * with this source code.
 *
 * @package Core
 * @license http://unlicense.org/ UNLICENSE
 * @link    https://github.com/rss-bridge/rss-bridge
 */

class DetectAction implements ActionInterface
{
    public function execute(array $request)
    {
        $targetURL = $request['url']
            or returnClientError('You must specify a url!');

        $format = $request['format']
            or returnClientError('You must specify a format!');

        $bridgeFactory = new \BridgeFactory();

        foreach ($bridgeFactory->getBridgeClassNames() as $bridgeClassName) {
            if (!$bridgeFactory->isWhitelisted($bridgeClassName)) {
                continue;
            }

            $bridge = $bridgeFactory->create($bridgeClassName);

            if ($bridge === false) {
                continue;
            }

            $bridgeParams = $bridge->detectParameters($targetURL);

            if (is_null($bridgeParams)) {
                continue;
            }

            $bridgeParams['bridge'] = $bridgeClassName;
            $bridgeParams['format'] = $format;

            header('Location: ?action=display&' . http_build_query($bridgeParams), true, 301);
            exit;
        }

        returnClientError('No bridge found for given URL: ' . $targetURL);
    }
}