(function () { 'use strict'; const SERVICE = '/cgi/service.cgi'; const ERR_UNAUTHENTICATED = -31998; const TEXT = { ko: { id: '로그인 이름', idHint: '사용자 이름을 입력하세요.', pw: '로그인 암호', pwHint: '암호를 입력하세요.', initPwHint: '초기암호는 admin', needChangePassword: '암호 변경이 필요합니다!', captchaHint: '이미지의 문자를 입력하세요.', refreshCaptcha: '새로고침', login: '로그인', logging: '로그인 중입니다.', tooManyGuest: '게스트 세션이 최대 수에 도달하여 로그인할 수 없습니다.', }, en: { id: 'Login ID', idHint: 'Please enter your ID', pw: 'Password', pwHint: 'Please enter your password', initPwHint: "Password is 'admin'.", needChangePassword: 'Change the default password!', captchaHint: 'Please input string in lower case', refreshCaptcha: 'Refresh', login: 'Login', logging: 'Logging in', tooManyGuest: 'Unable to login. Maximum number of guest sessions reached.', }, }; const RETRY_DELAYS_MS = [500, 750, 1125]; function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } const REQUEST_TIMEOUT_MS = 10000; async function postService(body) { const abort = new AbortController(); const timer = setTimeout(() => abort.abort(), REQUEST_TIMEOUT_MS); try { return await fetch(SERVICE, { method: 'POST', headers: { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' }, body: JSON.stringify(body), credentials: 'same-origin', signal: abort.signal, }); } finally { clearTimeout(timer); } } async function callService(method, params) { const body = { method: method }; if (params !== undefined) { body.params = params; } try { let res = await postService(body); for (const delay of RETRY_DELAYS_MS) { if (res.status !== 503) { break; } await sleep(delay); res = await postService(body); } if (!res.ok) { return null; } return await res.json(); } catch (e) { return null; } } function readSession(key) { try { return window.sessionStorage.getItem(key); } catch (e) { return null; } } function isAppRoot() { const base = new URL(document.baseURI).pathname.replace(/\/$/, ''); const path = window.location.pathname.replace(/\/(index\.html)?$/, ''); // lighttpd 제품은 "/" 에 앱 index.html 을 주소를 바꾸지 않고 내준다(404 처리). return path === base || path === ''; } function hasEntryParams() { return !!window.location.search || !!window.location.hash || !!readSession('sk') || !!readSession('ot'); } function isPlainEntry() { return isAppRoot() && !hasEntryParams(); } function hasSpecialLoginScreen(productName) { const name = (productName || '').toUpperCase(); return name.includes('SCS') || name.includes('KTT'); } const ICON = { logo: { w: 4500, d: 'M1716.3 27.8H736.8V167.7H1016.7V797.3H1436.4V167.7H1716.3V27.8ZM4444.7 27.8V167.7H4024.9C3885 167.7 3850 284.3 3850 342.6H4444.7V482.5H3850C3850 529.1 3878 657.4 3989.9 657.4H4444.7V797.3H3710.1C3605.2 797.3 3465.2 692.4 3465.2 587.5V272.6C3465.2 202.7 3535.2 27.8 3745.1 27.8H4444.7ZM1821.2 27.8V797.3H2240.9V27.8H1821.2ZM282.1 972.2V342.6H387V447.5C387 412.6 457 342.6 596.9 342.6 666.9 342.6 841.8 377.6 841.8 552.5 841.8 727.4 736.8 797.3 596.9 797.3 457 797.3 410.4 727.4 387 692.4V972.2H282.1ZM2345.9 797.3V27.8H2660.7C2730.7 27.8 2800.6 97.7 2835.6 132.7 2858.9 97.7 2926.5 27.8 3010.5 27.8H3360.3V797.3H3115.4V342.6C3115.4 342.6 2940.5 552.5 2905.6 587.5 2870.6 622.4 2800.6 622.4 2765.6 587.5 2730.7 552.5 2555.8 342.6 2555.8 342.6V797.3H2345.9ZM131.9 343.2L2.3 342.6 0 797.3H131.9V343.2ZM70.2 259.3C109 259.3 140.5 236.5 140.5 208.3 140.5 180.2 109 157.4 70.2 157.4 31.4 157.4 0 180.2 0 208.3 0 236.5 31.4 259.3 70.2 259.3ZM594.6 692.4C699.6 692.4 734.6 622.4 734.6 552.5 734.6 482.5 664.6 447.5 594.6 447.5 524.7 447.5 454.7 466.6 454.7 552.5 454.7 638.3 489.7 692.4 594.6 692.4Z' }, lockClose: { w: 1000, d: 'M421.4 319.9V421.6H578.6V319.9C578.6 304.3 565.7 291.7 549.9 291.7H450.1C434.3 291.7 421.4 304.3 421.4 319.9ZM621 319.9V421.6H687.1H708.3V442.8V638.5C708.3 677.1 676.6 708.3 637.2 708.3H362.7C323.4 708.3 291.7 677.1 291.7 638.5V442.8V421.6H312.9H379.1V319.9C379.1 281.3 410.8 250 450.1 250H549.9C589.2 250 621 281.3 621 319.9ZM566.2 515.9V613.9H608.7V515.9H566.2Z' }, lockOpen: { w: 1000, d: 'M613 250C596.9 250 584.1 262.6 584.1 278.2V421.6H687.1H708.3V442.8V638.5C708.3 677.1 676.6 708.3 637.2 708.3H362.7C323.4 708.3 291.7 677.1 291.7 638.5V442.8V421.6H312.9H541.7V278.2C541.7 239.6 573.4 208.3 613 208.3H712.9C752.2 208.3 784 239.6 784 278.2V375H741.5V278.2C741.5 262.6 728.6 250 712.9 250H613ZM608.7 515.9H566.2V613.9H608.7V515.9Z' }, refresh: { w: 1000, d: 'M594 264.2L489 328.3 511 362.7 706 243.9 511 125 489 159.5 594 223.5H250C204 223.5 166.7 259.9 166.7 304.8V752C166.7 796.9 204 833.3 250 833.3H750C796 833.3 833.3 796.9 833.3 752V508.1H791.7V752C791.7 774.5 773 792.7 750 792.7H250C227 792.7 208.3 774.5 208.3 752V304.8C208.3 282.4 227 264.2 250 264.2H594Z' }, }; const SVG_NS = 'http://www.w3.org/2000/svg'; function icon(name) { const svg = document.createElementNS(SVG_NS, 'svg'); svg.setAttribute('viewBox', '0 0 ' + ICON[name].w + ' 1000'); svg.setAttribute('aria-hidden', 'true'); const path = document.createElementNS(SVG_NS, 'path'); path.setAttribute('d', ICON[name].d); svg.appendChild(path); return svg; } function setIcon(button, name) { button.replaceChildren(icon(name)); } function field(input) { const box = el('div', 'hl-field'); box.appendChild(input); return box; } function plainTextInput(input) { input.setAttribute('autocapitalize', 'none'); input.setAttribute('autocorrect', 'off'); input.spellcheck = false; } function el(tag, className, text) { const e = document.createElement(tag); if (className) { e.className = className; } if (text !== undefined) { e.textContent = text; } return e; } async function loadLoginConfig(langPromise) { const info = await callService('session/info'); if (!info || !info.error || info.error.code !== ERR_UNAUTHENTICATED) { return null; } const productName = await callService('product/name'); if (!productName || typeof productName.result !== 'string' || hasSpecialLoginScreen(productName.result)) { return null; } const probe = await callService('session/login'); if (!probe) { return null; } const changed = await callService('admin/changed'); const defaultAdmin = await callService('default/admin'); const isKo = await langPromise; const name = productName.result; return { productName: name.replace('ipTIME ', ''), needCaptcha: needsCaptcha(probe), showDefaultHint: !!changed && changed.result === false && !!defaultAdmin && defaultAdmin.result === true, text: isKo ? TEXT.ko : TEXT.en, }; } function needsCaptcha(probe) { const data = probe && probe.error ? probe.error.data : null; return Array.isArray(data) && data.includes('captcha'); } function buildDots(productName) { const dots = el('div', 'hl-dots'); const match = productName.split('.').join('').match(/[0-9]+/); for (const ch of (match ? match[0] : '')) { const n = Number(ch); if (n <= 1) { dots.appendChild(el('div', 'hl-dot ' + (n === 0 ? 'hl-dot-zero' : 'hl-dot-one'))); continue; } const cols = n <= 4 ? 2 : Math.floor(n / 2) + (n % 2); const digit = el('div', 'hl-dot'); digit.style.gridTemplateColumns = 'repeat(' + cols + ', 4px)'; for (let i = 0; i < cols * 2; i++) { const blank = (n === 2 && (i === 1 || i === 2)) || (n % 2 !== 0 && i === cols - 1); digit.appendChild(el('div', blank ? '' : 'hl-dot-cell')); } dots.appendChild(digit); } return dots; } function buildForm(config, isDark) { const t = config.text; const root = el('div', 'hl-root' + (isDark ? ' hl-dark' : '')); const form = el('form', 'hl-form'); form.method = 'post'; form.setAttribute('autocomplete', 'on'); form.noValidate = true; const head = el('div', 'hl-head'); const logo = el('span', 'hl-logo'); logo.title = 'ipTIME'; logo.appendChild(icon('logo')); head.appendChild(logo); head.appendChild(el('span', 'hl-product', config.productName)); form.appendChild(head); const idLabel = el('label', 'hl-label', t.id); idLabel.htmlFor = 'hl-id'; const id = el('input', 'hl-input'); id.id = 'hl-id'; id.name = 'username'; id.autocomplete = 'username'; plainTextInput(id); id.placeholder = t.idHint; id.value = config.showDefaultHint ? 'admin' : ''; form.appendChild(idLabel); form.appendChild(field(id)); const pwLabel = el('label', 'hl-label', t.pw); pwLabel.htmlFor = 'hl-pw'; if (config.showDefaultHint) { pwLabel.appendChild(document.createTextNode(' - ')); pwLabel.appendChild(el('span', 'hl-primary', t.needChangePassword)); } const pw = el('input', 'hl-input hl-input-pw'); pw.id = 'hl-pw'; pw.type = 'password'; pw.name = 'password'; pw.autocomplete = 'current-password'; pw.placeholder = config.showDefaultHint ? t.initPwHint : t.pwHint; const pwToggle = el('button', 'hl-icon-button'); pwToggle.type = 'button'; pwToggle.tabIndex = -1; setIcon(pwToggle, 'lockClose'); pwToggle.addEventListener('click', () => { const show = pw.type === 'password'; pw.type = show ? 'text' : 'password'; setIcon(pwToggle, show ? 'lockOpen' : 'lockClose'); }); const pwField = field(pw); pwField.appendChild(pwToggle); form.appendChild(pwLabel); form.appendChild(pwField); const captchaBox = el('div', 'hl-captcha'); const captchaRow = el('div', 'hl-captcha-row'); const captchaRefresh = el('button', 'hl-captcha-refresh'); captchaRefresh.appendChild(icon('refresh')); captchaRefresh.type = 'button'; captchaRefresh.title = t.refreshCaptcha; const captchaImg = el('img', 'hl-captcha-img'); captchaImg.alt = ''; captchaRow.appendChild(captchaRefresh); captchaRow.appendChild(captchaImg); const captcha = el('input', 'hl-input'); captcha.placeholder = t.captchaHint; captcha.autocomplete = 'off'; plainTextInput(captcha); captchaBox.appendChild(captchaRow); captchaBox.appendChild(field(captcha)); form.appendChild(captchaBox); const message = el('div', 'hl-message'); form.appendChild(message); const submit = el('button', 'hl-submit', t.login); submit.type = 'submit'; form.appendChild(submit); const main = el('div', 'hl-main'); main.appendChild(form); root.appendChild(main); if (config.productName) { root.appendChild(buildDots(config.productName)); } return { root, form, id, pw, captchaBox, captchaImg, captchaRefresh, captcha, message, submit }; } async function updateCaptcha(view, need) { view.captchaBox.style.display = need ? '' : 'none'; view.captcha.value = ''; view.captchaImg.removeAttribute('src'); if (!need) { return; } const r = await callService('captcha/new'); if (r && typeof r.result === 'string') { view.captchaImg.src = r.result; } } function notifyFileManager() { try { const channel = new BroadcastChannel('fm_session_sync'); channel.postMessage('LOGIN_SUCCESS'); channel.close(); } catch (e) { } } function setBusy(view, busy, text) { view.submit.disabled = busy; view.submit.textContent = busy ? text.logging : text.login; } async function submitLogin(view, config, state) { const login = { id: view.id.value, pw: view.pw.value }; if (state.needCaptcha) { login.captcha = { text: view.captcha.value, url: view.captchaImg.getAttribute('src') || '' }; } view.message.textContent = ''; setBusy(view, true, config.text); const r = await callService('session/login', login); view.pw.value = ''; if (r && r.result === 'done') { notifyFileManager(); if (config.showDefaultHint) { try { window.sessionStorage.setItem('login_warn_set_account', '1'); } catch (e) { } } return true; } if (r && r.error && r.error.data === 'too_many_guest_sessions') { view.message.textContent = config.text.tooManyGuest; } const probe = await callService('session/login'); if (probe) { state.needCaptcha = needsCaptcha(probe); } await updateCaptcha(view, state.needCaptcha); setBusy(view, false, config.text); view.pw.focus(); return false; } function start(options) { const isDark = !!options.isDark; const loader = options.loader; const ready = typeof options.onReady === 'function' ? options.onReady : () => {}; window.htmlLoginEntry = !hasEntryParams(); if (!isPlainEntry()) { ready(); return Promise.resolve(); } return loadLoginConfig(options.langPromise).then((config) => { if (!config) { ready(); return; } return new Promise((resolve) => { const view = buildForm(config, isDark); const state = { needCaptcha: config.needCaptcha }; view.form.addEventListener('submit', async (ev) => { ev.preventDefault(); if (view.submit.disabled) { return; } if (await submitLogin(view, config, state)) { view.root.remove(); if (loader) { loader.style.display = ''; } resolve(); } }); view.captchaRefresh.addEventListener('click', () => updateCaptcha(view, true)); updateCaptcha(view, state.needCaptcha); if (loader) { loader.style.display = 'none'; } document.body.appendChild(view.root); (view.id.value ? view.pw : view.id).focus(); ready(); }); }).catch((e) => { console.error(e); ready(); }); } window.htmlLogin = { start: start }; })();