Passed
Push — master ( fe7dcd...466ab7 )
by Angel Fernando Quiroz
08:26
created

ExtraMenuFromWebservicePlugin::get_name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Define the ExtraMenuFromWebservice class as an extension of Plugin
6
 * install/uninstall the plugin.
7
 */
8
class ExtraMenuFromWebservicePlugin extends Plugin
9
{
10
    /**
11
     * ExtraMenuFromWebservice constructor.
12
     */
13
    protected function __construct()
14
    {
15
        $settings = [
16
            'tool_enable' => 'boolean',
17
            'authentication_url' => 'text',
18
            'authentication_email' => 'text',
19
            'authentication_password' => 'text',
20
            'normal_menu_url' => 'text',
21
            'mobile_menu_url' => 'text',
22
            'session_timeout' => 'text',
23
            'list_css_imports' => 'text',
24
            'list_fonts_imports' => 'text',
25
        ];
26
27
        parent::__construct(
28
            '0.1',
29
            'Borja Sanchez',
30
            $settings
31
        );
32
    }
33
34
    public static function create()
35
    {
36
        static $result = null;
37
38
        return $result ? $result : $result = new self();
39
    }
40
41
    public function install()
42
    {
43
        return true;
44
    }
45
46
    public function uninstall()
47
    {
48
        $settings = [
49
            'tool_enable',
50
            'authentication_url',
51
            'authentication_email',
52
            'authentication_password',
53
            'normal_menu_url',
54
            'mobile_menu_url',
55
            'username_parameter',
56
            'session_timeout',
57
            'list_css_imports' => 'text',
58
            'list_fonts_imports' => 'text',
59
        ];
60
61
        $tableSettings = Database::get_main_table(TABLE_MAIN_SETTINGS);
62
        $urlId = api_get_current_access_url_id();
63
64
        foreach ($settings as $variable) {
65
            $sql = "DELETE FROM $tableSettings WHERE variable = '$variable' AND access_url = $urlId";
66
            Database::query($sql);
67
        }
68
    }
69
70
    /**
71
     * Get a token through the WS indicated in plugin configuration.
72
     */
73
    public function getToken()
74
    {
75
        $response = [];
76
        $authenticationUrl = (string) $this->get('authentication_url');
77
        $authenticationEmail = (string) $this->get('authentication_email');
78
        $authenticationPassword = (string) $this->get('authentication_password');
79
80
        if (!empty($authenticationUrl) && !empty($authenticationEmail) && !empty($authenticationPassword)) {
81
            $curl = curl_init();
82
83
            curl_setopt_array($curl, [
84
                CURLOPT_URL => $authenticationUrl,
85
                CURLOPT_RETURNTRANSFER => true,
86
                CURLOPT_ENCODING => '',
87
                CURLOPT_MAXREDIRS => 10,
88
                CURLOPT_TIMEOUT => 5,
89
                CURLOPT_NOSIGNAL => 1,
90
                CURLOPT_SSL_VERIFYHOST => 0,
91
                CURLOPT_SSL_VERIFYPEER => 0,
92
                CURLOPT_FOLLOWLOCATION => true,
93
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
94
                CURLOPT_CUSTOMREQUEST => 'POST',
95
                CURLOPT_POSTFIELDS => '{
96
                    "email": "'.$authenticationEmail.'",
97
                    "password": "'.$authenticationPassword.'"
98
                }',
99
                CURLOPT_HTTPHEADER => [
100
                    'Content-Type: application/json',
101
                ],
102
            ]);
103
104
            $curlResponse = curl_exec($curl);
105
            curl_close($curl);
106
            if (false !== $curlResponse) {
107
                $curlResponse = json_decode($curlResponse, true);
108
109
                if (isset($curlResponse['data']['data']['token'])) {
110
                    $response = $curlResponse['data']['data']['token'];
111
                }
112
            }
113
        }
114
115
        return $response;
116
    }
117
118
    /**
119
     * Get the menu from the WS indicated in plugin configuration.
120
     * */
121
    public function getMenu(
122
        string $token,
123
        string $userEmail,
124
        bool $isMobile = false
125
    ): array {
126
        $response = [];
127
        $menuUrl = $isMobile ? (string) $this->get('mobile_menu_url') : (string) $this->get('normal_menu_url');
128
        if (!empty($menuUrl) && !empty($token) && !empty($userEmail)) {
129
            $menuUrl = substr($menuUrl, -1) === '/' ? $menuUrl : $menuUrl.'/';
130
            $curl = curl_init();
131
132
            curl_setopt_array($curl, [
133
                CURLOPT_URL => $menuUrl.$userEmail,
134
                CURLOPT_RETURNTRANSFER => true,
135
                CURLOPT_ENCODING => '',
136
                CURLOPT_MAXREDIRS => 10,
137
                CURLOPT_TIMEOUT => 5,
138
                CURLOPT_NOSIGNAL => 1,
139
                CURLOPT_FOLLOWLOCATION => true,
140
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
141
                CURLOPT_CUSTOMREQUEST => 'GET',
142
                CURLOPT_HTTPHEADER => [
143
                    "Authorization: Bearer $token",
144
                ],
145
            ]);
146
147
            $curlResponse = curl_exec($curl);
148
            if (false !== $curlResponse) {
149
                $curlResponse = json_decode($curlResponse, true);
150
                if (isset($curlResponse['data']['data']['html']['data'])) {
151
                    $response['html'] = $curlResponse['data']['data']['html']['data'];
152
                }
153
                if (isset($curlResponse['data']['data']['css']['data'])) {
154
                    $response['css'] = $curlResponse['data']['data']['css']['data'];
155
                }
156
                if (isset($curlResponse['data']['data']['js']['data'])) {
157
                    $response['js'] = $curlResponse['data']['data']['js']['data'];
158
                }
159
            }
160
            curl_close($curl);
161
        }
162
163
        return $response;
164
    }
165
166
    /**
167
     * Checks if the login token is expired.
168
     */
169
    public static function tokenIsExpired(int $tokenStartTime, int $pluginSessionTimeout): bool
170
    {
171
        $now = api_get_utc_datetime(null, false, true)->getTimestamp();
172
173
        return ($now - $tokenStartTime) > $pluginSessionTimeout;
174
    }
175
176
    /**
177
     * Get the list of CSS or fonts indicated in plugin configuration.
178
     */
179
    public static function getImports(string $list = '')
180
    {
181
        $importsArray = [];
182
183
        if (!empty($list)) {
184
            $importsArray = explode(";", $list);
185
        }
186
187
        return $importsArray;
188
    }
189
190
    public function get_name()
191
    {
192
        return 'ExtraMenuFromWebservice';
193
    }
194
}
195