aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/settings/privacy.php
blob: d62f3a839e1e2d3bf9d0e023c5c0014a90cc93d8 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
 * Settings_PrivacyController - Administration of all user privacy 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>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       2.4
 */

require_once 'settings.php';

class Settings_PrivacyController extends Settings_SettingsController
{
    /**
     * Set up this controller.
     *
     * @param String $action Name of the action to be invoked
     * @param Array $args    Arguments to be passed to the action method
     */
    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        PageLayout::setHelpKeyword('Basis.MyStudIPPrivacy');
        PageLayout::setTitle(_('Privatsphäre'));

        Navigation::activateItem('/profile/settings/privacy');
    }

    /**
     * Displays the privacy settings of a user.
     */
    public function index_action()
    {
        // Get visibility settings from database.
        $this->global_visibility = $this->user->visible;
        $this->online_visibility = get_local_visibility_by_id($this->user->user_id, 'online');
        $this->search_visibility = get_local_visibility_by_id($this->user->user_id, 'search');
        $this->email_visibility  = get_local_visibility_by_id($this->user->user_id, 'email');

        // Get default visibility for homepage elements.
        $this->default_homepage_visibility = Visibility::get_default_homepage_visibility($this->user->user_id);

        $this->user_perm    = $GLOBALS['perm']->get_perm($this->user->user_id);
        $this->user_domains = UserDomain::getUserDomains();

        // Calculate colWidth and colCount for different visibilities
        $this->colCount          = Visibility::getColCount();
        $this->colWidth          = 67 / $this->colCount;
        $this->visibilities      = Visibility::getVisibilities();
        $this->homepage_elements = Visibility::getHTMLArgs($this->user->user_id);
    }

    /**
     * Stores the privacy settings concerning the appearance of a user inside
     * the system.
     */
    public function global_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        $visibility = Request::option('global_visibility');

        // Globally visible or unknown -> set local visibilities accordingly.
        if ($visibility != 'no') {
            $online             = Request::int('online') ?: 0;
            $search             = Request::int('search') ?: 0;
            $email              = Request::int('email') ?: 0;
            $foaf_show_identity = Request::int('foaf_show_identity') ?: 0;
            // Globally invisible -> set all local fields to invisible.
        } else {
            $online  = $search = $foaf_show_identity = 0;
            $email   = Config::get()->DOZENT_ALLOW_HIDE_EMAIL ? 0 : 1;
            $success = $this->changeCompleteHomepageVisibility(VISIBILITY_ME);
        }

        $this->config->store('FOAF_SHOW_IDENTITY', $foaf_show_identity);

        $this->user->visible = $visibility;
        $this->user->store();

        $query = "INSERT INTO user_visibility
                    (user_id, online, search, email, mkdate)
                  VALUES (?, ?, ?, ?, UNIX_TIMESTAMP())
                  ON DUPLICATE KEY
                    UPDATE online = VALUES(online),
                           search = VALUES(search),
                           email = VALUES(email)";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([
            $this->user->user_id,
            $online,
            $search,
            $email,
        ]);
        NotificationCenter::postNotification('UserVisibilityDidCreate', $GLOBALS['user']->id);

        PageLayout::postSuccess(_('Ihre Sichtbarkeitseinstellungen wurden gespeichert.'));
        $this->redirect('settings/privacy');
    }

    private function changeCompleteHomepageVisibility($new_visibility)
    {
        $result    = [];
        $new_data  = [];
        $db_result = [];
        // Retrieve homepage elements.
        $data = $this->user->getHomepageElements();
        // Iterate through data and set new visibility.
        foreach ($data as $key => $entry) {
            $new_data[$key] = [
                'name'       => $entry['name'],
                'visibility' => $new_visibility,
            ];
            $new_data[$key]['extern'] = !empty($entry['extern']);
            $new_data[$key]['category'] = $entry['category'] ?? '';

            $db_result[$key] = $new_visibility;
        }
        $success = $this->change_homepage_visibility($db_result);
        if ($success) {
            $result = $new_data;
        }
        return $result;
    }

    /**
     * Saves user specified visibility settings for homepage elements.
     *
     * @param array $data all homepage elements with their visiblities in
     *                    the form $name => $visibility
     * @return int Number of affected database rows (hopefully 1).
     */
    private function change_homepage_visibility($data)
    {
        $query = "INSERT INTO user_visibility
                    (user_id, homepage, mkdate)
                  VALUES (?, ?, UNIX_TIMESTAMP())
                  ON DUPLICATE KEY
                    UPDATE homepage = VALUES(homepage)";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([
            $this->user->id,
            json_encode($data),
        ]);
        return $statement->rowCount();
    }

    /**
     * Sets a default visibility for elements that are added to a user's
     * homepage but whose visibility hasn't been configured explicitly yet.
     *
     * @param int $visibility default visibility for new homepage elements
     * @return Number of affected database rows (hopefully 1).
     */
    private function set_default_homepage_visibility($visibility)
    {
        $query = "INSERT INTO user_visibility
                    (user_id, default_homepage_visibility, mkdate)
                  VALUES (?, ?, UNIX_TIMESTAMP())
                  ON DUPLICATE KEY
                    UPDATE default_homepage_visibility = VALUES(default_homepage_visibility)";
        $statement = DBManager::get()->prepare($query);
        $statement->execute([
            $this->user->id,
            (int)$visibility,
        ]);
        return $statement->rowCount();
    }

    /**
     * Stores the privacy settings concerning the homepage / profile of a
     * user.
     */
    public function homepage_action()
    {
        CSRFProtection::verifyUnsafeRequest();

        // If no bulk action is performed set all visibilitysettings seperately
        if (!$this->bulk()) {
            $data = Request::getArray('visibility_update');
            if (Visibility::updateUserFromRequest($data)) {
                PageLayout::postSuccess(_('Ihre Sichtbarkeitseinstellungen wurden gespeichert.'));
            } else {
                PageLayout::postError(_('Ihre Sichtbarkeitseinstellungen wurden nicht gespeichert!'));
            }
        }
        $this->redirect('settings/privacy');
    }

    /**
     * Performs bulk actions on the privacy settings of a user. This can be
     * either the setting of new default values or the changing of all privacy
     * values at once.
     *
     * @return boolean Returns <b>true</b> if all visibilities have been set
     */
    public function bulk()
    {
        if ($default_visibility = Request::int('default')) {
            $this->set_default_homepage_visibility($default_visibility);
        }

        if ($visibility = Request::int('all')) {
            if (Visibility::setAllSettingsForUser($visibility, $this->user->user_id)) {
                PageLayout::postSuccess(_('Die Sichtbarkeit der Profilelemente wurde gespeichert.'));
                return true;
            } else {
                PageLayout::postError(_('Die Sichtbarkeitseinstellungen der Profilelemente wurden nicht gespeichert!'));
            }
        }
        return false;
    }
}