aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/legacy/LegacyController.php
blob: 75fa9d2cfb962cf1009bea63da16ed0d41a74c93 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

declare(strict_types=1);

namespace Shaarli\Legacy;

use Shaarli\Feed\FeedBuilder;
use Shaarli\Front\Controller\Visitor\ShaarliVisitorController;
use Slim\Http\Request;
use Slim\Http\Response;

/**
 * We use this to maintain legacy routes, and redirect requests to the corresponding Slim route.
 * Only public routes, and both `?addlink` and `?post` were kept here.
 * Other routes will just display the linklist.
 *
 * @deprecated
 */
class LegacyController extends ShaarliVisitorController
{
    /** @var string[] Both `?post` and `?addlink` do not use `?do=` format. */
    public const LEGACY_GET_ROUTES = [
        'post',
        'addlink',
    ];

    /**
     * This method will call `$action` method, which will redirect to corresponding Slim route.
     */
    public function process(Request $request, Response $response, string $action): Response
    {
        if (!method_exists($this, $action)) {
            throw new UnknowLegacyRouteException();
        }

        return $this->{$action}($request, $response);
    }

    /** Legacy route: ?post= */
    public function post(Request $request, Response $response): Response
    {
        $parameters = count($request->getQueryParams()) > 0 ? '?' . http_build_query($request->getQueryParams()) : '';
        $route = '/admin/shaare';

        if (!$this->container->loginManager->isLoggedIn()) {
            return $this->redirect($response, '/login?returnurl='. $this->getBasePath() . $route . $parameters);
        }

        return $this->redirect($response, $route . $parameters);
    }

    /** Legacy route: ?addlink= */
    protected function addlink(Request $request, Response $response): Response
    {
        $route = '/admin/add-shaare';

        if (!$this->container->loginManager->isLoggedIn()) {
            return $this->redirect($response, '/login?returnurl=' . $this->getBasePath() . $route);
        }

        return $this->redirect($response, $route);
    }

    /** Legacy route: ?do=login */
    protected function login(Request $request, Response $response): Response
    {
        $returnUrl = $request->getQueryParam('returnurl');

        return $this->redirect($response, '/login' . ($returnUrl ? '?returnurl=' . $returnUrl : ''));
    }

    /** Legacy route: ?do=logout */
    protected function logout(Request $request, Response $response): Response
    {
        return $this->redirect($response, '/admin/logout');
    }

    /** Legacy route: ?do=picwall */
    protected function picwall(Request $request, Response $response): Response
    {
        return $this->redirect($response, '/picture-wall');
    }

    /** Legacy route: ?do=tagcloud */
    protected function tagcloud(Request $request, Response $response): Response
    {
        return $this->redirect($response, '/tags/cloud');
    }

    /** Legacy route: ?do=taglist */
    protected function taglist(Request $request, Response $response): Response
    {
        return $this->redirect($response, '/tags/list');
    }

    /** Legacy route: ?do=daily */
    protected function daily(Request $request, Response $response): Response
    {
        $dayParam = !empty($request->getParam('day')) ? '?day=' . escape($request->getParam('day')) : '';

        return $this->redirect($response, '/daily' . $dayParam);
    }

    /** Legacy route: ?do=rss */
    protected function rss(Request $request, Response $response): Response
    {
        return $this->feed($request, $response, FeedBuilder::$FEED_RSS);
    }

    /** Legacy route: ?do=atom */
    protected function atom(Request $request, Response $response): Response
    {
        return $this->feed($request, $response, FeedBuilder::$FEED_ATOM);
    }

    /** Legacy route: ?do=opensearch */
    protected function opensearch(Request $request, Response $response): Response
    {
        return $this->redirect($response, '/open-search');
    }

    /** Legacy route: ?do=dailyrss */
    protected function dailyrss(Request $request, Response $response): Response
    {
        return $this->redirect($response, '/daily-rss');
    }

    /** Legacy route: ?do=feed */
    protected function feed(Request $request, Response $response, string $feedType): Response
    {
        $parameters = count($request->getQueryParams()) > 0 ? '?' . http_build_query($request->getQueryParams()) : '';

        return $this->redirect($response, '/feed/' . $feedType . $parameters);
    }
}