Site Builder
Editing:
models.php
writable 0666
<?php require_once rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/members/lib/auth.php'; require_login(); if (current_user()['role'] !== 'admin') forbidden_page(); /***************************************************************** * Model list editor * ------------------------------------------------------------- * • Reads live model IDs from /v1/models (API) * • Reads / writes to /cache/models.json (AI_MODEL_FILE) * Only rows with "active": true will be offered in all tools *****************************************************************/ require_once __DIR__ . '/init.php'; // ← loads helpers & AI_MODEL_FILE ai_handle_key_post(); if (!ai_has_key()) { // editor usable only after a key is saved echo 'Save your OpenAI API key first.'; exit; } /* ---------- paths ---------- */ $FILE = AI_MODEL_FILE; // constant from init.php /* ---------- save new list ---------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['model'])) { $out = []; foreach ($_POST['model'] as $id) { $out[] = [ 'id' => $id, 'active' => in_array($id, $_POST['active'] ?? [], true) ]; } if (!is_dir(dirname($FILE))) mkdir(dirname($FILE), 0777, true); file_put_contents($FILE, json_encode($out, JSON_PRETTY_PRINT)); header('Location: ' . $_SERVER['REQUEST_URI']); // simple PRG pattern exit; } /* ---------- fetch live list ---------- */ $key = ai_get_key(); $ctx = stream_context_create(['http'=>[ 'method'=>'GET', 'header'=>"Authorization: Bearer $key\r\n", 'timeout'=>10 ]]); $live = []; $json = @file_get_contents('https://api.openai.com/v1/models', false, $ctx); if ($json) { $live = array_column(json_decode($json, true)['data'] ?? [], 'id'); } /* fallback if fetch fails */ if (!$live) $live = ['gpt-3.5-turbo','gpt-4o-mini','gpt-4']; /* ---------- merge with stored flags ---------- */ $stored = is_file($FILE) ? json_decode(file_get_contents($FILE), true) : []; $flag = []; foreach ($stored as $row) if (isset($row['id'])) $flag[$row['id']] = (bool)$row['active']; $list = []; foreach ($live as $id) { $list[] = ['id'=>$id, 'active'=>$flag[$id] ?? false]; } /* ---------- HTML ---------- */ ?><!doctype html><html lang="en"><head><meta charset="utf-8"> <title>OpenAI model list editor</title> <style> body{font-family:sans-serif;margin:0;background:#f1f4fb;padding:2rem} h2{margin-top:0} table{border-collapse:collapse;width:100%;max-width:640px;margin:auto;background:#fff} th,td{padding:.6rem 1rem;border:1px solid #ccc;font-size:.95rem} th{background:#eee;text-align:left} button{padding:.6rem 1.4rem;font-size:1rem;background:#004cff;color:#fff; border:none;border-radius:6px;cursor:pointer} </style></head><body> <h2 style="text-align:center">Visible OpenAI models</h2> <form method="post"> <table> <tr><th style="width:80px">Show</th><th>Model ID</th></tr> <?php foreach ($list as $row): ?> <tr> <td style="text-align:center"> <input type="hidden" name="model[]" value="<?=htmlspecialchars($row['id'])?>"> <input type="checkbox" name="active[]" value="<?=htmlspecialchars($row['id'])?>" <?=$row['active']?'checked':''?>> </td> <td><code><?=htmlspecialchars($row['id'])?></code></td> </tr> <?php endforeach; ?> </table> <p style="text-align:center;margin-top:1.2rem"> <button>Save list</button> </p> <p style="max-width:640px;margin:1rem auto;color:#555;font-size:.9rem"> Checked rows are written to <code><?=str_replace($_SERVER['DOCUMENT_ROOT'],'',$FILE)?></code>. Every tool that includes <code>init.php</code> will immediately present only those models in its drop‑down. (Unchecked models remain available in the API – you are only hiding them.) </p> </form> </body></html>
Save changes
Create folder
writable 0777
Create
Cancel