]>
Commit | Line | Data |
---|---|---|
f07b75b0 | 1 | PMG = { |
43f0b189 DC |
2 | Utils: { |
3 | getCookie(name) { | |
4 | let cookies = document.cookie.split(/;\s*/); | |
5 | for (let i = 0; i < cookies.length; i++) { | |
6 | let cookie = cookies[i].split('='); | |
7 | if (cookie[0] === name && cookie.length > 1) { | |
8 | return cookie[1]; | |
9 | } | |
10 | } | |
11 | return undefined; | |
12 | }, | |
13 | setCookie(name, value, expires) { | |
14 | value = encodeURIComponent(value); | |
15 | let cookie = `${name}=${value}`; | |
16 | if (expires) { | |
17 | cookie += `; expires=${expires}`; | |
18 | } | |
19 | document.cookie = cookie; | |
20 | }, | |
21 | deleteCookie(name) { | |
22 | PMG.Utils.setCookie(name, "", "Thu, 01 Jan 1970 00:00:00 UTC"); | |
23 | }, | |
24 | authOK(options) { | |
25 | var authCookie = PMG.Utils.getCookie('PMGAuthCookie') || ""; | |
c87d46fb | 26 | return authCookie.substr(0, 7) === 'PMGQUAR' && Proxmox.UserName !== ''; |
43f0b189 DC |
27 | }, |
28 | isoToUnix(iso) { | |
29 | let fields = iso.split('-').map((field) => parseInt(field, 10)); | |
30 | // monthIndex starts at 0 | |
c87d46fb | 31 | let date = new Date(fields[0], fields[1]-1, fields[2]); |
43f0b189 DC |
32 | return Math.round(date.getTime()/1000); |
33 | }, | |
34 | unixToIso(unix) { | |
35 | let date = new Date(unix*1000); | |
36 | let year = date.getFullYear().toString(); | |
37 | let month = (date.getMonth()+1).toString().padStart(2, "0"); | |
38 | let day = date.getDate().toString().padStart(2, "0"); | |
39 | return `${year}-${month}-${day}`; | |
40 | }, | |
41 | showError(xhr) { | |
42 | let statusText = "", errorText = ""; | |
43 | if (xhr instanceof Error) { | |
44 | statusText = gettext("Error"); | |
45 | errorText = xhr.message; | |
46 | } else if (xhr.error instanceof Error) { | |
47 | statusText = gettext("Error"); | |
48 | errorText = xhr.error.message; | |
49 | } else { | |
50 | statusText = xhr.status.toString() + ' ' + xhr.statusText; | |
51 | try { | |
52 | let errorObj = JSON.parse(xhr.responseText); | |
53 | if (errorObj.errors) { | |
54 | let errors = Object.keys(errorObj.errors).map((key) => key + ": " + errorObj.errors[key]); | |
55 | errorText = errors.join('<br>'); | |
56 | } | |
57 | } catch (e) { | |
58 | statusText = gettext("Error"); | |
59 | errorText = e.message; | |
60 | } | |
61 | } | |
62 | app.toast.show({ | |
63 | text: `Error:<br> | |
64 | ${statusText}<br> | |
65 | ${errorText} | |
66 | `, | |
67 | closeButton: true, | |
c87d46fb | 68 | destroyOnClose: true, |
43f0b189 DC |
69 | }); |
70 | }, | |
016ba5ab TL |
71 | showSuccessToast(msg) { |
72 | app.toast.show({ | |
73 | text: msg, | |
74 | closeTimeout: 2000, | |
75 | closeButton: true, | |
76 | destroyOnClose: true, | |
77 | }); | |
78 | }, | |
43f0b189 DC |
79 | extractParams() { |
80 | let queryObj = app.utils.parseUrlQuery(location.search); | |
81 | let mail, action, date, username, ticket; | |
82 | if (queryObj.ticket) { | |
83 | let tocheck = decodeURIComponent(queryObj.ticket); | |
f07b75b0 | 84 | let match = tocheck.match(/^PMGQUAR:([^\s:]+):/); |
43f0b189 DC |
85 | if (match) { |
86 | ticket = tocheck; | |
87 | username = match[1]; | |
88 | } | |
89 | delete queryObj.ticket; | |
90 | } | |
91 | ||
92 | if (queryObj.date) { | |
93 | date =queryObj.date; | |
94 | delete queryObj.date; | |
95 | } | |
96 | ||
97 | if (queryObj.cselect) { | |
98 | mail = queryObj.cselect; | |
99 | action = queryObj.action; | |
100 | delete queryObj.cselect; | |
101 | delete queryObj.action; | |
102 | } | |
103 | ||
104 | if (mail || action || date || ticket) { | |
105 | let queryString = app.utils.serializeObject(queryObj); | |
106 | window.history.replaceState( | |
107 | window.history.state, | |
108 | document.title, | |
c87d46fb | 109 | location.pathname + (queryString? "?" + queryString : ''), |
43f0b189 DC |
110 | ); |
111 | } | |
112 | ||
113 | return { mail, action, date, username, ticket }; | |
114 | }, | |
115 | setLoginInfo(result) { | |
116 | PMG.Utils.setCookie('PMGAuthCookie', result.data.ticket); | |
117 | Proxmox.CSRFPreventionToken = result.data.CSRFPreventionToken; | |
118 | }, | |
119 | getSubscriptionInfo() { | |
120 | return new Promise(function(resolve, reject) { | |
121 | app.request({ | |
122 | url: '/api2/json/nodes/localhost/subscription', | |
123 | dataType: 'json', | |
124 | success: (result, status, xhr) => { | |
125 | resolve(result.data); | |
126 | }, | |
127 | error: (xhr, status) => { | |
128 | reject(xhr); | |
c87d46fb | 129 | }, |
43f0b189 DC |
130 | }); |
131 | }); | |
132 | }, | |
133 | checkSubscription(data, showPopup) { | |
134 | return new Promise(function(resolve, reject) { | |
851cb026 | 135 | if (data.status?.toLowerCase() !== 'active') { |
891bd7f1 | 136 | let url = data.url || 'https://www.proxmox.com'; |
43f0b189 DC |
137 | let err = `You do not have a valid subscription for this server. |
138 | Please visit | |
139 | <a target="_blank" href="${url}">www.proxmox.com</a> | |
140 | to get a list of available options.`; | |
141 | app.toolbar.show('.toolbar.subscription'); | |
142 | $$('.button.subscription').on('click', () => { | |
143 | app.dialog.alert( | |
144 | err, | |
145 | gettext("No valid subscription"), | |
146 | ); | |
147 | }); | |
148 | if (showPopup) { | |
149 | app.dialog.alert( | |
150 | err, | |
151 | gettext("No valid subscription"), | |
152 | () => { | |
153 | resolve(data); | |
c87d46fb | 154 | }, |
43f0b189 DC |
155 | ); |
156 | } else { | |
157 | resolve(); | |
158 | } | |
159 | } else { | |
c29d577d | 160 | $$('.toolbar.subscription').remove(); |
43f0b189 DC |
161 | resolve(); |
162 | } | |
163 | }); | |
c87d46fb TL |
164 | }, |
165 | }, | |
43f0b189 DC |
166 | }; |
167 |