aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/oer/admin.php
blob: 3404e54f7f5e1174a78187357bfa2704fc47fb46 (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
<?php

class Oer_AdminController extends AuthenticatedController
{

    function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);
        Navigation::activateItem("/admin/locations/oer");
        if (!$GLOBALS['perm']->have_perm("root")) {
            throw new AccessDeniedException();
        }
    }

    public function hosts_action()
    {
        //init
        OERHost::thisOne();
        $this->hosts = OERHost::findAll();
        foreach ($this->hosts as $host) {
            if (strpos($host['public_key'], "\r") !== false) {
                $host['public_key'] = str_replace("\r", "", $host['public_key']);
                $host->store();
            }
        }

        $this->federation_problems = \Studip\ENV !== "production";
        if (!function_exists("curl_init")) {
            $this->federation_problems = true;
            PageLayout::postError(_('Ihr PHP hat kein aktiviertes cURL-Modul.'));
        }
        if (Config::get()->OER_PUBLIC_STATUS !== "nobody") {
            $this->federation_problems = true;
            PageLayout::postInfo(_('Der OER Campus ist nicht für nobody freigegeben. Damit kann er sich nicht mit anderen Stud.IPs verbinden. Um das zu ändern, setzen Sie die Konfiguration OER_PUBLIC_STATUS auf "nobody".'));
        }

    }

    public function add_new_host_action()
    {
        PageLayout::setTitle(_("Neue Lernmaterialien einstellen"));
        if (Request::submitted("nothanx")) {
            $_SESSION['Lernmarktplatz_no_thanx'] = true;
            $this->redirect("oer/admin/hosts");
        } elseif (Request::isPost()) {
            $host = OERHost::findOneByUrl(trim(Request::get("url")));
            if (!$host) {
                $host = new OERHost();
                $host['url'] = trim(Request::get("url"));
                $host['last_updated'] = time();
                $host->fetchPublicKey();
                if ($host['public_key']) {
                    $host->store();
                    PageLayout::postSuccess(_("Server wurde gefunden und hinzugefügt."));
                } else {
                    PageLayout::postError(_("Server ist nicht erreichbar oder hat die Anfrage abgelehnt."));
                }
            } else {
                $host->fetchPublicKey();
                PageLayout::postInfo(_("Server ist schon in Liste."));
            }

            $this->redirect("oer/admin/hosts");
        }
    }

    public function ask_for_hosts_action($host_id)
    {
        $host = new OERHost($host_id);
        $added = $this->askForHosts($host);
        if ($added > 0) {
            PageLayout::postSuccess(sprintf(_("%s neue Server hinzugefügt."), $added));
        } else {
            PageLayout::postInfo(_("Keine neuen Server gefunden."));
        }
        $this->redirect("oer/admin/hosts");
    }

    protected function askForHosts(OERHost $host)
    {
        $data = $host->askKnownHosts();
        $added = 0;
        if (!empty($data['hosts'])) {
            foreach ($data['hosts'] as $host_data) {
                $host = OERHost::findOneByUrl($host_data['url']);
                if (!$host) {
                    $host = new OERHost();
                    $host['url'] = $host_data['url'];
                    $host->fetchPublicKey();
                    if ($host['public_key']) {
                        $added++;
                        $host->store();
                    }
                } else {
                    $host->fetchPublicKey();
                }
            }
        }
        return $added;
    }

    public function toggle_index_server_action()
    {
        if (Request::isPost()) {
            $host = new OERHost(Request::option("host_id"));
            if ($host->isMe()) {
                $host['index_server'] = Request::int("active", 0);
                $host->store();
                //distribute this info to adjacent server
                $data = [
                    'data' => [
                        'public_key' => $host['public_key'],
                        'url' => $host['url'],
                        'name' => $host['name'],
                        'index_server' => $host['index_server']
                    ]
                ];

                foreach (OERHost::findAll() as $remote) {
                    if (!$remote->isMe()) {
                        $remote->pushDataToEndpoint("update_server_info", $data);
                    }
                }

            } else {
                $host['allowed_as_index_server'] = Request::int("active", 0);
                $host->store();
            }
        }

        $this->render_text((
            Icon::create("checkbox-".(Request::int("active") ? "" : "un")."checked")->asSvg()
        ));
    }

    public function toggle_server_active_action()
    {
        if (Request::isPost()) {
            $host = OERHost::find(Request::option("host_id"));
            if ($host && !$host->isMe()) {
                $host['active'] = Request::int("active", 0);
                $host->store();
            }
        }

        $this->render_text((
            Icon::create('checkbox-'.(Request::int('active') ? '' : 'un').'checked')->asSvg()
        ));
    }

    public function refresh_hosts_action()
    {
        foreach (OERHost::findAll() as $host) {
            if (!$host->isMe()) {
                $host->fetchPublicKey();
            }
            $host->store();
        }

        PageLayout::postSuccess(_("Daten der Server wurden abgerufen und aufgefrischt."));
        $this->redirect("oer/admin/hosts");
    }

}