Site Builder
Editing:
initokkkkkkkkkkkk.php
writable 0666
<?php /***************************************************************** * OpenAI helper (v1.5) • BestDealOn • Aug‑2025 * ------------------------------------------------------------- * ▸ AES‑encrypted API‑key cookie (never leaves the browser) * ▸ Re‑usable dark key‑bar (save / delete / model / usage link) * ▸ ⚙ modal for user defaults (temperature & max‑tokens) * ▸ JS that injects 3 hidden inputs – model / temperature / * max_tokens – into every prompt‑form, so tool authors can * focus only on the UI + prompt. *****************************************************************/ declare(strict_types=1); /* ---------------------------------------------------------------- 0. CONFIG ---------------------------------------------------------------- */ const AI_COOKIE = 'openai_key'; // cookie that holds the AES blob const AI_SECRET = 'openai-shared-v1'; // shared secret – SAME in every tool const AI_TTL = 30 * 24 * 3600; // 30 days /* cookie domain (“.example.com”) so the key is visible anywhere on the same second‑level domain */ $AI_ROOT = (static function (): string { $host = $_SERVER['HTTP_HOST'] ?? ''; if (!filter_var($host, FILTER_VALIDATE_IP) && preg_match('/([a-z0-9-]+\.[a-z]{2,})$/i', $host, $m)) { return '.' . $m[1]; } return ''; // e.g. localhost → keep cookie path‑only })(); /* ---------------------------------------------------------------- 1. LOW‑LEVEL CRYPTO ---------------------------------------------------------------- */ function ai_enc(string $plain): string { $algo = 'aes-128-ctr'; $key = substr(hash('sha256', AI_SECRET, true), 0, 16); $iv = random_bytes(openssl_cipher_iv_length($algo)); return base64_encode(openssl_encrypt($plain, $algo, $key, 0, $iv).'::'.$iv); } function ai_dec(string $cipher): string { [$ct, $iv] = explode('::', base64_decode($cipher), 2) + ['', '']; $algo = 'aes-128-ctr'; $key = substr(hash('sha256', AI_SECRET, true), 0, 16); return openssl_decrypt($ct, $algo, $key, 0, $iv) ?: ''; } /* ---------------------------------------------------------------- 2. KEY HELPERS ---------------------------------------------------------------- */ function ai_has_key(): bool { return !empty($_COOKIE[AI_COOKIE]) && ai_dec($_COOKIE[AI_COOKIE]); } function ai_get_key(): string { return ai_has_key() ? ai_dec($_COOKIE[AI_COOKIE]) : ''; } /* flash message (invalid key, empty key, etc.) */ $AI_MSG = ''; /* ---------------------------------------------------------------- 3. HANDLE save / delete ACTIONS ---------------------------------------------------------------- */ function ai_handle_key_post(): void { global $AI_ROOT, $AI_MSG; /* ---------- save ---------- */ if ($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['save_key'])) { /* flush any previous cookie */ setcookie(AI_COOKIE,'',time()-3600,'/',$AI_ROOT,isset($_SERVER['HTTPS']),true); $raw = trim($_POST['api_key'] ?? ''); if ($raw === '') { $AI_MSG = 'API key empty'; return; } /* tiny 6‑s HEAD request to confirm the key is valid & the client has outbound connectivity */ $ctx = stream_context_create(['http'=>[ 'method'=>'GET', 'header'=>"Authorization: Bearer $raw\r\n", 'timeout'=>6 ]]); if (@file_get_contents('https://api.openai.com/v1/models', false, $ctx)) { setcookie( AI_COOKIE, ai_enc($raw), time()+AI_TTL, '/', $AI_ROOT, isset($_SERVER['HTTPS']), true ); header('Location: '.$_SERVER['REQUEST_URI']); exit; } $AI_MSG = '❌ Invalid API key (or network error).'; return; } /* ---------- delete ---------- */ if (isset($_GET['logout'])) { setcookie(AI_COOKIE,'',time()-3600,'/',$AI_ROOT,isset($_SERVER['HTTPS']),true); header('Location: '.$_SERVER['PHP_SELF']); exit; } } /* ---------------------------------------------------------------- 4. KEY‑BAR (UI + JS) ---------------------------------------------------------------- */ function ai_render_key_bar(array $extraModels = []): void { global $AI_MSG; /* default list + any extra the tool wants to add */ $models = array_merge(['gpt-3.5-turbo','gpt-4o-mini','gpt-4'], $extraModels); ?> <!-- ========== BAR styles ========== --> <style> .top-bar{background:#0d1117;padding:.25rem 1rem;display:flex;flex-wrap:wrap;align-items:center; justify-content:space-between;gap:.75rem;font-size:.9rem} .top-bar .left,.top-bar .right{display:flex;flex-wrap:wrap;align-items:center;gap:.5rem} .top-bar h1{margin:0;font-size:1rem;color:#ffb63b;display:flex;align-items:center;gap:.4rem} .logout-btn{background:none;color:#e24d4b;font-size:1.1rem;font-weight:700;cursor:pointer;border:none} .gear-btn{background:#1e2430;color:#fff;border:none;border-radius:4px;width:34px;height:34px;font-size:1.05rem;cursor:pointer} .top-bar input[type=password],.top-bar select,.top-bar button[name=save_key]{ height:34px;font-size:.9rem;border:none;border-radius:4px;padding:0 .8rem;line-height:1} .top-bar input[type=password]{width:180px;background:#fff} .top-bar button[name=save_key]{background:#004cff;color:#fff;font-weight:600;padding:0 1rem;cursor:pointer} .top-bar a{font-size:.85rem;color:#5af287;text-decoration:none;white-space:nowrap} .top-bar select{background:#1e2430;color:#fff} </style> <div class="top-bar"> <?php if (!ai_has_key()): ?> <!-- ——— NO KEY YET ——— --> <div class="left"> <h1>Connect to OpenAI</h1> <a href="https://platform.openai.com/account/api-keys" target="_blank">Get an API key ↗</a> </div> <form method="post" autocomplete="off" class="right"> <input type="password" name="api_key" placeholder="sk-…" required> <button name="save_key">Save Key</button> </form> <?php if ($AI_MSG): ?> <div style="width:100%;color:#e24d4b;font-weight:600;margin-top:.25rem"><?=$AI_MSG?></div> <?php endif; ?> <?php else: ?> <!-- ——— KEY PRESENT ——— --> <div class="left"> <h1>Connected to OpenAI <button class="logout-btn" onclick="location='?logout=1'" title="Delete key">×</button> </h1> </div> <div class="right"> <button id="aiGear" class="gear-btn" title="Advanced settings">⚙</button> <select id="modelSel"><?php foreach ($models as $m) echo "<option>$m</option>"; ?></select> <a href="https://platform.openai.com/account/usage" target="_blank">Check usage ↗</a> <?php if ($AI_MSG): ?><span style="color:#e24d4b;font-weight:600"><?=$AI_MSG?></span><?php endif; ?> </div> <?php endif; ?> </div> <!-- ---------- tiny modal ---------- --> <div id="aiModal" style="display:none;position:fixed;inset:0;background:rgba(0,0,0,.5);z-index:9999"> <div style="background:#fff;padding:1.6rem;border-radius:8px;max-width:320px;width:90%; margin:8% auto;font-size:.95rem;box-shadow:0 4px 18px rgba(0,0,0,.2)"> <h3 style="margin-top:0">Advanced settings</h3> <label for="aiTemp" style="display:block;margin:.7rem 0 .25rem;font-weight:600"> Temperature (0–1) </label> <input type="number" id="aiTemp" min="0" max="1" step="0.01" value="0.7" style="width:100%;padding:.5rem .7rem;border:1px solid #ccd4e8;border-radius:6px"> <label for="aiTok" style="display:block;margin:.9rem 0 .25rem;font-weight:600"> Max tokens (1–4096) </label> <input type="number" id="aiTok" min="1" max="4096" value="800" style="width:100%;padding:.5rem .7rem;border:1px solid #ccd4e8;border-radius:6px"> <div style="display:flex;gap:.8rem;margin-top:1.4rem;justify-content:flex-end"> <button id="aiCancel" type="button" style="background:#e5e5e5;border:none;border-radius:6px;padding:.5rem 1rem;cursor:pointer"> Cancel </button> <button id="aiSave" type="button" style="background:#004cff;color:#fff;border:none;border-radius:6px;padding:.5rem 1rem; font-weight:600;cursor:pointer"> Save </button> </div> </div> </div> <!-- ---------- behaviour ---------- --> <script> /* ===== 1. model selector (sticky) ========================= */ const ms = document.getElementById('modelSel'); if (ms){ /* restore last choice */ const last = localStorage.getItem('aiModel'); if (last && [...ms.options].some(o => o.value === last)) ms.value = last; ms.addEventListener('change', e => { localStorage.setItem('aiModel', e.target.value); document.querySelectorAll('input[name=model]').forEach(i => i.value = e.target.value); }); } /* ===== 2. advanced modal (temp / tokens) ================= */ const gear = document.getElementById('aiGear'), modal = document.getElementById('aiModal'); if (gear && modal){ const tI=document.getElementById('aiTemp'), mI=document.getElementById('aiTok'), save=document.getElementById('aiSave'), cancel=document.getElementById('aiCancel'); /* restore previous defaults */ try{ const d = JSON.parse(localStorage.getItem('aiDefaults') || '{}'); if (d.temp) tI.value = d.temp; if (d.max) mI.value = d.max; }catch{} gear.onclick = () => modal.style.display = 'block'; cancel.onclick = () => modal.style.display = 'none'; modal.addEventListener('click', e => { if (e.target === modal) modal.style.display = 'none'; }); save.onclick = () => { const obj = { temp : Math.max(0, Math.min(1, parseFloat(tI.value)||0.7)), max : Math.max(1, Math.min(4096, parseInt (mI.value)||800)) }; localStorage.setItem('aiDefaults', JSON.stringify(obj)); modal.style.display = 'none'; dispatchEvent(new CustomEvent('ai-defaults-changed', {detail: obj})); }; } /* ===== 3. inject hidden inputs into every prompt‑form ===== */ (function injectHidden(){ function apply(def){ document.querySelectorAll('form').forEach(f => { if (!f.querySelector('textarea[name=prompt],textarea[id*=prompt]')) return; /* ensure 3 inputs exist */ ['model','temperature','max_tokens'].forEach(n => { if (!f.querySelector(`input[name=${n}]`)){ const inp=document.createElement('input'); inp.type='hidden'; inp.name=n; f.appendChild(inp); } }); /* model from LS or current dropdown */ f.querySelector('input[name=model]').value = localStorage.getItem('aiModel') || (ms ? ms.value : 'gpt-3.5-turbo'); f.querySelector('input[name=temperature]').value = def.temp ?? 0.7; f.querySelector('input[name=max_tokens]').value = def.max ?? 800; }); } let cur={temp:0.7,max:800}; try{cur = {...cur, ...JSON.parse(localStorage.getItem('aiDefaults')||'{}')}}catch{} apply(cur); addEventListener('ai-defaults-changed', e => apply(e.detail)); })(); </script> <?php } /* --- END ai_render_key_bar() --- */ /* ---------------------------------------------------------------- 5. SIMPLE ai_chat() WRAPPER ---------------------------------------------------------------- */ function ai_chat(string $prompt, array $override = []): string { $key = ai_get_key(); if (!$key) return '➡ (no API key)'; /* defaults injected by JS; can be overridden per‑call */ $temp = isset($override['temperature']) ? $override['temperature'] : (isset($_POST['temperature']) ? floatval($_POST['temperature']) : 0.7); $max = isset($override['max_tokens']) ? $override['max_tokens'] : (isset($_POST['max_tokens']) ? intval($_POST['max_tokens']) : 800); $model = $override['model'] ?? ($_POST['model'] ?? 'gpt-3.5-turbo'); /* --- build body & call ----------------------------------- */ $body = [ 'model' => $model, 'messages' => [['role'=>'user','content'=>$prompt]], 'temperature' => $temp, 'max_tokens' => $max ]; $ch = curl_init('https://api.openai.com/v1/chat/completions'); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => 1, CURLOPT_POST => 1, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer '.$key, 'Content-Type: application/json' ], CURLOPT_POSTFIELDS => json_encode($body), CURLOPT_TIMEOUT => 60 ]); $raw = curl_exec($ch); curl_close($ch); return $raw ? (json_decode($raw, true)['choices'][0]['message']['content'] ?? '') : '⚠ no response'; }
Save changes
Create folder
writable 0777
Create
Cancel