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