Issues (1798)

public/plugin/ExtraMenuFromWebservice/index.php (1 issue)

Labels
Severity
1
<?php
2
/* For license terms, see /license.txt */
3
4
use ChamiloSession as Session;
5
6
/**
7
 * This is the main script of the extra menu from webservice plugin.
8
 *
9
 * @author Borja Sanchez
10
 */
11
12
// This plugin doesn't work for anonymous users
13
if (!api_is_anonymous()) {
14
    $extraMenuFromWebservice = ExtraMenuFromWebservicePlugin::create();
15
    $pluginEnabled = $extraMenuFromWebservice->get('tool_enable');
16
    // If the configuration option 'tool_enable' is disabled, doesn't show the menu
17
    if ($pluginEnabled === 'true') {
18
        $menuContent = "";
19
        $userId = api_get_user_id();
20
        $userData = $originalUserInfo = api_get_user_info(
21
            api_get_user_id(),
22
            false,
23
            false,
24
            false,
25
            false,
26
            false,
27
            true
28
        );
29
        $pluginPath = api_get_path(WEB_PLUGIN_PATH).'ExtraMenuFromWebservice/resources/';
30
        //Check if the token is in session, if not get a new token and write in session
31
        if (
32
            Session::has('extramenufromwebservice_plugin_token') &&
33
            Session::has('extramenufromwebservice_plugin_token_start')
34
        ) {
35
            //if no session lifetime exists, set 1 day
36
            $pluginSessionTimeout = !empty((int) $extraMenuFromWebservice->get('session_timeout')) ?
37
                $extraMenuFromWebservice->get('session_timeout') :
38
                86400;
39
40
            $tokenStartTime = new DateTime(Session::read('extramenufromwebservice_plugin_token_start'));
41
42
            // If token is expired, get other new token
43
            if ($extraMenuFromWebservice::tokenIsExpired($tokenStartTime->getTimestamp(), $pluginSessionTimeout)) {
44
                $loginToken = $extraMenuFromWebservice->getToken();
45
                Session::write('extramenufromwebservice_plugin_token', $loginToken);
46
                $now = api_get_utc_datetime();
47
                Session::write('extramenufromwebservice_plugin_token_start', $now);
48
            }
49
        } else {
50
            $loginToken = $extraMenuFromWebservice->getToken();
51
            if (!empty($loginToken)) {
52
                Session::write('extramenufromwebservice_plugin_token', $loginToken);
53
                $now = api_get_utc_datetime();
54
                Session::write('extramenufromwebservice_plugin_token_start', $now);
55
            }
56
        }
57
58
        $isMobile = api_is_browser_mobile();
0 ignored issues
show
The function api_is_browser_mobile was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        $isMobile = /** @scrutinizer ignore-call */ api_is_browser_mobile();
Loading history...
59
        $menuResponse = $extraMenuFromWebservice->getMenu(
60
            Session::read('extramenufromwebservice_plugin_token'),
61
            $userData['email'],
62
            $isMobile
63
        );
64
        if (!empty($menuResponse)) {
65
            $menuContent = $menuResponse;
66
            $fh = '<script type="text/javascript" src="'.$pluginPath.'js/extramenufromwebservice.js" ></script>';
67
            $fh .= '<link href="'.$pluginPath.'css/extramenufromwebservice.css" rel="stylesheet" type="text/css">';
68
            if (!empty($extraMenuFromWebservice->get('list_css_imports'))) {
69
                $cssListToImport = $extraMenuFromWebservice->getImports(
70
                    $extraMenuFromWebservice->get('list_css_imports')
71
                );
72
            }
73
            if (!empty($extraMenuFromWebservice->get('list_fonts_imports'))) {
74
                $fontListToImport = $extraMenuFromWebservice->getImports(
75
                    $extraMenuFromWebservice->get('list_fonts_imports')
76
                );
77
            }
78
            $fh .= '<div class="extra-menu-from-webservice">';
79
            $fh .= '<input id="menu-toggle" type="checkbox" />';
80
            $fh .= '<label class="menu-btn" for="menu-toggle">';
81
            $fh .= '<span></span>';
82
            $fh .= '</label>';
83
            $fh .= '<div class="nav-from-webservice" id="nav-from-webservice">';
84
85
            if (isset($cssListToImport)) {
86
                foreach ($cssListToImport as $cssUrl) {
87
                    $fh .= '<link href="'.$cssUrl.'" rel="stylesheet" type="text/css">';
88
                }
89
            }
90
91
            $fh .= '<style>';
92
            if (isset($fontListToImport)) {
93
                foreach ($fontListToImport as $fontUrl) {
94
                    $fh .= '@import url("'.$fontUrl.'");';
95
                }
96
            }
97
            $fh .= $menuContent['css'];
98
            $fh .= '</style>';
99
100
            $fh .= $menuContent['html'];
101
102
            $fh .= '<script>';
103
            $fh .= $menuContent['js'];
104
            $fh .= '</script>';
105
106
            $fh .= '</div>';
107
            $fh .= '</div>';
108
109
            echo $fh;
110
        }
111
    }
112
}
113