Passed
Push — 1.11.x ( e4e88c...b4241a )
by Yannick
09:32 queued 16s
created

OnlyofficePlugin::uninstallHook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * (c) Copyright Ascensio System SIA 2023
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 */
19
20
/**
21
 * Plugin class for the Onlyoffice plugin.
22
 *
23
 * @author Asensio System SIA
24
 */
25
class OnlyofficePlugin extends Plugin implements HookPluginInterface
26
{
27
28
    /**
29
     * OnlyofficePlugin name.
30
     */
31
    private $pluginName = "onlyoffice";
32
33
    /**
34
     * OnlyofficePlugin constructor.
35
     */
36
    protected function __construct()
37
    {
38
        parent::__construct(
39
            "1.3.0",
40
            "Asensio System SIA",
41
            [
42
                "enable_onlyoffice_plugin" => "boolean",
43
                "document_server_url" => "text",
44
                "jwt_secret" => "text",
45
                "jwt_header" => "text",
46
                "document_server_internal" => "text",
47
                "storage_url" => "text"
48
            ]
49
        );
50
    }
51
52
    /**
53
     * Create OnlyofficePlugin object
54
     */
55
    public static function create(): OnlyofficePlugin
56
    {
57
        static $result = null;
58
59
        return $result ?: $result = new self();
60
    }
61
62
    /**
63
     * This method install the plugin tables.
64
     */
65
    public function install()
66
    {
67
        $this->installHook();
68
    }
69
70
    /**
71
     * This method drops the plugin tables.
72
     */
73
    public function uninstall()
74
    {
75
        $this->uninstallHook();
76
    }
77
78
    /**
79
     * Install the "create" hooks.
80
     */
81
    public function installHook()
82
    {
83
        $itemActionObserver = OnlyofficeItemActionObserver::create();
84
        HookDocumentItemAction::create()->attach($itemActionObserver);
85
86
        $actionObserver = OnlyofficeActionObserver::create();
87
        HookDocumentAction::create()->attach($actionObserver);
88
89
        $viewObserver = OnlyofficeItemViewObserver::create();
90
        HookDocumentItemView::create()->attach($viewObserver);
91
    }
92
93
    /**
94
     * Uninstall the "create" hooks.
95
     */
96
    public function uninstallHook()
97
    {
98
        $itemActionObserver = OnlyofficeItemActionObserver::create();
99
        HookDocumentItemAction::create()->detach($itemActionObserver);
100
101
        $actionObserver = OnlyofficeActionObserver::create();
102
        HookDocumentAction::create()->detach($actionObserver);
103
104
        $viewObserver = OnlyofficeItemViewObserver::create();
105
        HookDocumentItemView::create()->detach($viewObserver);
106
    }
107
108
    /**
109
     * Get status of demo server
110
     *
111
     * @return bool
112
     */
113
    public function useDemo() {
114
        return $this->getDemoData()["enabled"] === true;
115
    }
116
117
    /**
118
     * Get demo data
119
     *
120
     * @return array
121
     */
122
    public function getDemoData() {
123
        $data = api_get_setting('onlyoffice_connect_demo_data')[0];
124
125
        if (empty($data)) {
126
            $data = [
127
                "available" => true,
128
                "enabled" => false
129
            ];
130
            api_add_setting(json_encode($data), 'onlyoffice_connect_demo_data', null, 'setting', 'Plugins');
131
            return $data;
132
        }
133
        $data = json_decode($data, true);
134
135
        if (isset($data['start'])) {
136
            $overdue = $data["start"];
137
            $overdue += 24*60*60*AppConfig::GetDemoParams()["TRIAL"];
138
            if ($overdue > time()) {
139
                $data["available"] = true;
140
                $data["enabled"] = $data["enabled"] === true;
141
            } else {
142
                $data["available"] = false;
143
                $data["enabled"] = false;
144
                api_set_setting('onlyoffice_connect_demo_data', json_encode($data));
145
            }
146
        }
147
        return $data;
148
    }
149
150
    /**
151
     * Switch on demo server
152
     *
153
     * @param bool $value - select demo
154
     *
155
     * @return bool
156
     */
157
    public function selectDemo($value) {
158
        $data = $this->getDemoData();
159
160
        if ($value === true && !$data["available"]) {
161
            return false;
162
        }
163
164
        $data["enabled"] = $value === true;
165
166
        if (!isset($data["start"])) {
167
            $data["start"] = time();
168
        }
169
        api_set_setting('onlyoffice_connect_demo_data', json_encode($data));
170
        return true;
171
    }
172
173
    /**
174
     * Get the document server url
175
     *
176
     * @param bool $origin - take origin
177
     *
178
     * @return string
179
     */
180
    public function getDocumentServerUrl($origin = false) 
181
    {
182
        if (!$origin && $this->useDemo()) {
183
            return AppConfig::GetDemoParams()["ADDR"];
184
        }
185
186
        $url = $this->get("document_server_url");
187
        return self::processUrl($url);
188
    }
189
190
    /**
191
     * Get the document service address available from Chamilo from the application configuration
192
     *
193
     * @param bool $origin - take origin
194
     *
195
     * @return string
196
     */
197
    public function getDocumentServerInternalUrl($origin = false) {
198
        if (!$origin && $this->useDemo()) {
199
            return $this->getDocumentServerUrl();
200
        }
201
202
        $url = $this->get('document_server_internal');
203
        if (empty($url)) {
204
            $url = AppConfig::InternalUrl();
205
        }
206
        if (!$origin && empty($url)) {
207
            $url = $this->getDocumentServerUrl();
208
        }
209
        return self::processUrl($url);
210
    }
211
212
    /**
213
     * Replace domain in document server url with internal address from configuration
214
     *
215
     * @param string $url - document server url
216
     *
217
     * @return string
218
     */
219
    public function replaceDocumentServerUrlToInternal($url) {
220
        $documentServerUrl = $this->getDocumentServerInternalUrl();
221
        if (!empty($documentServerUrl)) {
222
            $from = $this->getDocumentServerUrl();
223
224
            if (!preg_match("/^https?:\/\//i", $from)) {
225
                $parsedUrl = parse_url($url);
226
                $from = $parsedUrl["scheme"] . "://" . $parsedUrl["host"] . (array_key_exists("port", $parsedUrl) ? (":" . $parsedUrl["port"]) : "") . $from;
227
            }
228
229
            if ($from !== $documentServerUrl)
230
            {
231
                $url = str_replace($from, $documentServerUrl, $url);
232
            }
233
        }
234
        return $url;
235
    }
236
237
    /**
238
     * Get the Chamilo address available from document server from the application configuration
239
     *
240
     * @return string
241
     */
242
    public function getStorageUrl() {
243
        $storageUrl = $this->get('storage_url');
244
        if (empty($storageUrl)) {
245
            $storageUrl = AppConfig::StorageUrl();
246
        }
247
        return self::processUrl($storageUrl);
248
    }
249
250
    /**
251
     * Get the document service secret key from the application configuration
252
     *
253
     * @param bool $origin - take origin
254
     *
255
     * @return string
256
     */
257
    public function getDocumentServerSecret($origin = false) {
258
        if (!$origin && $this->useDemo()) {
259
            return AppConfig::GetDemoParams()["SECRET"];
260
        }
261
        return $this->get("jwt_secret");
262
    }
263
264
    /**
265
     * Get the jwt header setting
266
     *
267
     * @param bool $origin - take origin
268
     *
269
     * @return string
270
     */
271
    public function getJwtHeader($origin = false) {
272
        if (!$origin && $this->useDemo()) {
273
            return AppConfig::GetDemoParams()["HEADER"];
274
        }
275
276
        $header = $this->get('jwt_header');
277
        if (empty($header)) {
278
            $header = AppConfig::JwtHeader() ? AppConfig::JwtHeader() : "Authorization";
279
        }
280
        return $header;
281
    }
282
283
    /**
284
     * Get link to plugin settings
285
     *
286
     * @return string
287
     */
288
    public function getConfigLink() {
289
        return api_get_path(WEB_PATH)."main/admin/configure_plugin.php?name=".$this->pluginName;
290
    }
291
292
    /**
293
     * Get plugin name
294
     *
295
     * @return string
296
     */
297
    public function getPluginName() {
298
        return $this->pluginName;
299
    }
300
301
    /**
302
     * Add backslash to url if it's needed
303
     *
304
     * @return string
305
     */
306
    public function processUrl($url) {
307
        if ($url !== null && $url !== "/") {
308
            $url = rtrim($url, "/");
309
            if (strlen($url) > 0) {
310
                $url = $url . "/";
311
            }
312
        }
313
        return $url;
314
    }
315
}
316