SiteโฏBuilder
Editing:
initokkkk.php
writable 0666
<?php /***************************************************************** * OpenAI helper v1.1 (Augโ2025) * path: /openai/init.php *****************************************************************/ declare(strict_types=1); /* ---- constants ------------------------------------------- */ const AI_COOKIE = 'openai_key'; const AI_SECRET = 'openai-shared-v1'; const AI_TTL = 30 * 24 * 3600; // 30ย days /* ---- cookie domain --------------------------------------- */ $AI_ROOT = (static function (): string { $h = $_SERVER['HTTP_HOST'] ?? ''; if (!filter_var($h, FILTER_VALIDATE_IP) && preg_match('/([a-z0-9-]+\.[a-z]{2,})$/i', $h, $m)) { return '.'.$m[1]; } return ''; })(); /* ---- crypto ---------------------------------------------- */ function ai_enc(string $v): string { $m='aes-128-ctr'; $k=substr(hash('sha256',AI_SECRET,true),0,16); $iv=random_bytes(openssl_cipher_iv_length($m)); return base64_encode(openssl_encrypt($v,$m,$k,0,$iv)."::{$iv}"); } function ai_dec(string $c): string { [$ct,$iv]=explode('::',base64_decode($c),2)+['','']; $m='aes-128-ctr'; $k=substr(hash('sha256',AI_SECRET,true),0,16); return openssl_decrypt($ct,$m,$k,0,$iv) ?: ''; } /* ---- key helpers ----------------------------------------- */ function ai_has_key(): bool { return isset($_COOKIE[AI_COOKIE]) && ai_dec($_COOKIE[AI_COOKIE]); } function ai_get_key(): string { return ai_has_key() ? ai_dec($_COOKIE[AI_COOKIE]) : ''; } /* ---- global message -------------------------------------- */ $AI_MSG = ''; // populated on bad key /* ---- save / delete key ----------------------------------- */ function ai_handle_key_post(): void { global $AI_ROOT, $AI_MSG; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_key'])) { /* always drop any stale cookie first */ setcookie(AI_COOKIE,'',time()-3600,'/',$AI_ROOT,isset($_SERVER['HTTPS']),true); $raw = trim($_POST['api_key'] ?? ''); if ($raw === '') { $AI_MSG = 'API key empty'; return; } /* quick validation */ $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; } if (isset($_GET['logout'])) { setcookie(AI_COOKIE, '', time() - 3600, '/', $AI_ROOT, isset($_SERVER['HTTPS']), true); header('Location: ' . $_SERVER['PHP_SELF']); exit; } } /* ---- key bar --------------------------------------------- */ function ai_render_key_bar(array $extraModels = []): void { global $AI_MSG; $models = array_merge(['gpt-3.5-turbo','gpt-4o-mini','gpt-4'], $extraModels); ?> <style> .top-bar { background: #0d1117; /* reduce vertical padding for a slimmer bar */ padding: 0.25rem 1rem; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem; font-size: 0.9rem; } /* keep left/right groups tight */ .top-bar .left, .top-bar .right { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; } /* title sizing stays the same */ .top-bar h1 { margin: 0; font-size: 1rem; color: #ffb63b; display: flex; align-items: center; gap: 0.4rem; } /* inline โ after text */ .top-bar .logout-btn { background: none; color: #e24d4b; font-size: 1.1rem; font-weight: 700; cursor: pointer; line-height: 1; padding-left: 0.4rem; border: none; } /* unified flex centers for input, select, button */ .top-bar input[type="password"], .top-bar select, .top-bar button[name="save_key"] { display: flex; align-items: center; justify-content: center; height: 34px; /* slightly less height */ font-size: 0.9rem; border: none; border-radius: 4px; padding: 0 0.8rem; box-sizing: border-box; line-height: 1; margin: 0; } /* input width and colors */ .top-bar input[type="password"] { width: 180px; background: #fff; color: #000; } /* Save Key background */ .top-bar button[name="save_key"] { background: #004cff; color: #fff; font-weight: 600; padding: 0 1rem; } /* link styling */ .top-bar a { font-size: 0.85rem; color: #5af287; text-decoration: none; white-space: nowrap; } /* dark dropdown */ .top-bar select { background-color: #1e2430; color: #fff; appearance: none; -webkit-appearance: none; -moz-appearance: none; } </style> <div class="top-bar"> <?php if (!ai_has_key()): ?> <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:0.25rem"> <?= $AI_MSG ?> </div> <?php endif; ?> <?php else: ?> <div class="left"> <h1> Connected to OpenAI <button class="logout-btn" onclick="location='?logout=1'" title="Delete key">ร</button> </h1> </div> <div class="right"> <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> <script> document.getElementById('modelSel')?.addEventListener('change', e => { document.querySelectorAll('input[name=model]').forEach(i => i.value = e.target.value); }); </script> <?php } /* ---- chat wrapper ---------------------------------------- */ function ai_chat(string $prompt, array $opts = []): string { $key = ai_get_key(); if (!$key) return ''; $body = [ 'model' => $opts['model'] ?? 'gpt-3.5-turbo', 'messages' => [['role'=>'user','content'=>$prompt]], 'temperature' => $opts['temperature'] ?? 0.7, 'max_tokens' => $opts['max_tokens'] ?? 800, ]; $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'] ?? '') : ''; }
Save changes
Create folder
writable 0777
Create
Cancel