aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/u.php
blob: 98414b7c48f20bcfc9de94e5e0769e62ffde47f8 (plain)
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
<?php


/**
 * The U controller is responsible for short URLs. Therefore, it is just named "u" to not waste space.
 */
class UController extends AuthenticatedController
{
    /**
     * The resolve action for a short URL. Shortened, to be short itself.
     */
    public function r_action($id)
    {
        $url = ShortURL::findOneBySql('id = :id OR alias = :id', ['id' => $id]);
        if ($url) {
            $this->redirect($url->url);
        } else {
            throw new AccessDeniedException(_('Die Kurz-URL ist ungültig!'));
        }
    }


    public function create_action()
    {
        if (!Request::isPost()) {
            throw new AccessDeniedException();
        }

        $user = User::findCurrent();
        $path = Request::get('path');
        //Check if the user has already created such a short-URL:
        $short_url = ShortURL::findOneBySql(
            'url = :path AND user_id = :user_id',
            [
                'path' => $path,
                'user_id' => $user->id
            ]
        );
        if (!$short_url) {
            $short_url = new ShortURL();
            $short_url->url = $path;
            $short_url->user_id = $user->id;
            $short_url->store();
        }
        $this->render_json(
            [
                'full_short_url' => URLHelper::getURL('dispatch.php/u/r/' . $short_url->alias),
                'url_id' => $short_url->id
            ]
        );
    }


    public function alias_action($url_id)
    {
        PageLayout::setTitle(_('Bezeichnung ändern'));
        $short_url = new ShortURL($url_id);
        $this->form = \Studip\Forms\Form::fromSORM(
            $short_url,
            [
                'fields' => [
                    'alias' => [
                        'label' => _('Bezeichnung'),
                        'type' => 'text',
                        'pattern' => '[a-záæäéèôøöü0-9\-]{4,256}'
                    ]
                ]
            ]
        );
        $this->form->autoStore();
    }


    public function overview_action()
    {
        PageLayout::setTitle(_('Meine Kurz-URLs'));
        if (Navigation::hasItem('/contents/short_urls')) {
            Navigation::activateItem('/contents/short_urls');
        }
        $this->short_urls = ShortURL::findBySql('user_id = :user_id ORDER BY `alias` ASC', ['user_id' => $GLOBALS['user']->id]);
    }
}