aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/avatar.php
blob: 5111078df442358e768742f7bd10e5da9b52ef82 (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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
 * AvatarController - Administration of all avatar related settings
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @author      Thomas Hackl <thomas.hackl@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       4.2
 */

class AvatarController extends AuthenticatedController
{
    /**
     * Display the avatar information of a user, course or institute
     * @param string $type object type: 'user', 'course' or 'institute'
     * @param string $id ID of the object this avatar belongs to
     */
    public function update_action($type, $id)
    {
        // Check for permission to save a new avatar.
        if ($type == 'user') {
            PageLayout::setHelpKeyword('Basis.HomepageBild');
            PageLayout::setTitle(_('Profilbild ändern'));

            $has_perm = $GLOBALS['perm']->have_profile_perm('user', $id);
            $class = Avatar::class;
            $this->cancel_link = $this->url_for('profile', ['username' => User::find($id)->username]);
        } else if ($type == 'institute') {
            PageLayout::setTitle(Context::getHeaderLine() . ' - ' . _('Einrichtungsbild ändern'));

            $has_perm = $GLOBALS['perm']->have_studip_perm('admin', $id);
            $class = InstituteAvatar::class;
            $this->cancel_link = $this->url_for('institute/basicdata/index', ['cid' => $id]);
        } else {
            PageLayout::setTitle(Context::getHeaderLine() . ' - ' . _('Veranstaltungsbild ändern'));

            $has_perm = $GLOBALS['perm']->have_studip_perm('tutor', $id);
            $course = Course::find($id);
            if ($course->isStudygroup()) {
                $class = 'StudygroupAvatar';
                $this->cancel_link = $this->url_for('course/studygroup/edit?cid=' . $id);
            } else {
                $class = CourseAvatar::class;
                $this->cancel_link = $this->url_for('course/management?cid=' . $id);
            }
        }

        if (!$has_perm) {
            throw new AccessDeniedException(_('Sie haben keine Berechtigung, das Bild zu ändern.'));
        }

        if ($type === 'user') {
            Navigation::activateItem('/profile/index');
        } else if ($type === 'institute') {
            Navigation::activateItem('/admin/institute/details');
        } else {
            Navigation::activateItem('/course/admin/avatar');

            if ($GLOBALS['perm']->have_studip_perm('admin', $id)) {
                $widget = new CourseManagementSelectWidget();
                Sidebar::get()->addWidget($widget);
            }
        }


        $avatar = $class::getAvatar($id);
        $this->avatar = $avatar->getURL($class::NORMAL);
        $this->customized = $avatar->is_customized();

        $this->type = $type;
        $this->id = $id;
    }

    /**
     * Upload a new avatar or removes the current avatar.
     * Sends an information email to the user if the action was not invoked by himself.
     * @param string $type object type: 'user', 'course' or 'institute'
     * @param string $id ID of the object this avatar belongs to
     */
    public function upload_action($type, $id)
    {
        CSRFProtection::verifyUnsafeRequest();

        // Check for permission to save a new avatar.
        if ($type == 'user') {
            $has_perm = $GLOBALS['perm']->have_profile_perm('user', $id);
            $class = Avatar::class;
            $redirect = 'profile?username=' . User::find($id)->username;
        } else if ($type == 'institute') {
            $has_perm = $GLOBALS['perm']->have_studip_perm('admin', $id);
            $class = InstituteAvatar::class;
            $redirect = 'institute/basicdata/index';
        } else {
            $has_perm = $GLOBALS['perm']->have_studip_perm('tutor', $id);
            $course = Course::find($id);
            if ($course->isStudygroup()) {
                $class = 'StudygroupAvatar';
                $redirect = 'course/studygroup/edit/?cid=' . $id;
            } else {
                $class = CourseAvatar::class;
                $redirect = 'course/management';
            }
        }

        if (!$has_perm) {
            throw new AccessDeniedException(_('Sie haben keine Berechtigung, das Bild zu ändern.'));
        }

        if (Request::submitted('reset')) {

            $class::getAvatar($id)->reset();
            if ($type == 'user') {
                Visibility::removePrivacySetting('picture', $id);
            }
            PageLayout::postSuccess(_('Bild gelöscht.'));

        } elseif (Request::submitted('upload')) {
            try {

                // Get the Base64-encoded data from cropper.
                $imgdata = Request::get('cropped-image');

                // Extract actual image data (prepended by mime type and meta data)
                list($type, $imgdata) = explode(';', $imgdata);
                list(, $imgdata) = explode(',', $imgdata);
                $imgdata = base64_decode($imgdata);
                // Write data to file.
                $filename = $GLOBALS['TMP_PATH'] . '/avatar-' . $id . '.png';
                file_put_contents($filename, $imgdata);

                // Use new image file for avatar creation.
                $class::getAvatar($id)->createFrom($filename);

                NotificationCenter::postNotification('AvatarDidUpload', $id);

                $message = _('Die Bilddatei wurde erfolgreich hochgeladen. '
                            .'Eventuell sehen Sie das neue Bild erst, nachdem Sie diese Seite '
                            .'neu geladen haben (in den meisten Browsern F5 drücken).');
                PageLayout::postSuccess($message);

                // Send message to user if necessary.
                if ($type == 'user') {
                    setTempLanguage($id);
                    $this->postPrivateMessage(_("Ein neues Bild wurde hochgeladen.\n"));
                    restoreLanguage();
                    Visibility::addPrivacySetting(_('Eigenes Bild'), 'picture', 'commondata', 1, $id);
                }

                unlink($filename);
            } catch (Exception $e) {
                PageLayout::postError($e->getMessage());
            }
        }
        $this->relocate($redirect);
    }

    /**
     * Deletes a custom avatar.
     * @param string $type object type: 'user', 'course' or 'institute'
     * @param string $id ID of the object this avatar belongs to
     */
    public function delete_action($type, $id)
    {
        // Check for permission to delete avatar.
        if ($type == 'user') {
            $has_perm = $GLOBALS['perm']->have_profile_perm('user', $id);
            $class = 'Avatar';
            $redirect = 'profile';
        } else if ($type == 'institute') {
            $has_perm = $GLOBALS['perm']->have_studip_perm('admin', $id);
            $class = 'InstituteAvatar';
            $redirect = 'institute/basicdata/index';
        } else {
            $has_perm = $GLOBALS['perm']->have_studip_perm('tutor', $id);
            $course = Course::find($id);
            if ($course->isStudygroup()) {
                $class = 'StudygroupAvatar';
                $redirect = 'course/studygroup/edit/?cid=' . $id;
            } else {
                $class = 'CourseAvatar';
                $redirect = 'course/management';
            }
        }

        if (!$has_perm) {
            throw new AccessDeniedException(_('Sie haben keine Berechtigung, das Bild zu ändern.'));
        }

        $class::getAvatar($id)->reset();

        PageLayout::postMessage(MessageBox::success(_('Das Bild wurde gelöscht.')));
        $this->relocate($redirect);
    }
}