diff --git a/.perm_test_apache b/.perm_test_apache new file mode 100644 index 0000000..e69de29 diff --git a/.perm_test_exec b/.perm_test_exec new file mode 100644 index 0000000..e69de29 diff --git a/ai/LocalAIApi.php b/ai/LocalAIApi.php new file mode 100644 index 0000000..00b1b00 --- /dev/null +++ b/ai/LocalAIApi.php @@ -0,0 +1,311 @@ + [ +// ['role' => 'system', 'content' => 'You are a helpful assistant.'], +// ['role' => 'user', 'content' => 'Tell me a bedtime story.'], +// ], +// ]); +// if (!empty($response['success'])) { +// $decoded = LocalAIApi::decodeJsonFromResponse($response); +// } + +class LocalAIApi +{ + /** @var array|null */ + private static ?array $configCache = null; + + /** + * Signature compatible with the OpenAI Responses API. + * + * @param array $params Request body (model, input, text, reasoning, metadata, etc.). + * @param array $options Extra options (timeout, verify_tls, headers, path, project_uuid). + * @return array{ + * success:bool, + * status?:int, + * data?:mixed, + * error?:string, + * response?:mixed, + * message?:string + * } + */ + public static function createResponse(array $params, array $options = []): array + { + $cfg = self::config(); + $payload = $params; + + if (empty($payload['input']) || !is_array($payload['input'])) { + return [ + 'success' => false, + 'error' => 'input_missing', + 'message' => 'Parameter "input" is required and must be an array.', + ]; + } + + if (!isset($payload['model']) || $payload['model'] === '') { + $payload['model'] = $cfg['default_model']; + } + + return self::request($options['path'] ?? null, $payload, $options); + } + + /** + * Snake_case alias for createResponse (matches the provided example). + * + * @param array $params + * @param array $options + * @return array + */ + public static function create_response(array $params, array $options = []): array + { + return self::createResponse($params, $options); + } + + /** + * Perform a raw request to the AI proxy. + * + * @param string $path Endpoint (may be an absolute URL). + * @param array $payload JSON payload. + * @param array $options Additional request options. + * @return array + */ + public static function request(?string $path = null, array $payload = [], array $options = []): array + { + if (!function_exists('curl_init')) { + return [ + 'success' => false, + 'error' => 'curl_missing', + 'message' => 'PHP cURL extension is missing. Install or enable it on the VM.', + ]; + } + + $cfg = self::config(); + + $projectUuid = $cfg['project_uuid']; + if (empty($projectUuid)) { + return [ + 'success' => false, + 'error' => 'project_uuid_missing', + 'message' => 'PROJECT_UUID is not defined; aborting AI request.', + ]; + } + + $defaultPath = $cfg['responses_path'] ?? null; + $resolvedPath = $path ?? ($options['path'] ?? $defaultPath); + if (empty($resolvedPath)) { + return [ + 'success' => false, + 'error' => 'project_id_missing', + 'message' => 'PROJECT_ID is not defined; cannot resolve AI proxy endpoint.', + ]; + } + + $url = self::buildUrl($resolvedPath, $cfg['base_url']); + $baseTimeout = isset($cfg['timeout']) ? (int) $cfg['timeout'] : 30; + $timeout = isset($options['timeout']) ? (int) $options['timeout'] : $baseTimeout; + if ($timeout <= 0) { + $timeout = 30; + } + + $baseVerifyTls = array_key_exists('verify_tls', $cfg) ? (bool) $cfg['verify_tls'] : true; + $verifyTls = array_key_exists('verify_tls', $options) + ? (bool) $options['verify_tls'] + : $baseVerifyTls; + + $projectHeader = $cfg['project_header']; + + $headers = [ + 'Content-Type: application/json', + 'Accept: application/json', + ]; + $headers[] = $projectHeader . ': ' . $projectUuid; + if (!empty($options['headers']) && is_array($options['headers'])) { + foreach ($options['headers'] as $header) { + if (is_string($header) && $header !== '') { + $headers[] = $header; + } + } + } + + if (!empty($projectUuid) && !array_key_exists('project_uuid', $payload)) { + $payload['project_uuid'] = $projectUuid; + } + + $body = json_encode($payload, JSON_UNESCAPED_UNICODE); + if ($body === false) { + return [ + 'success' => false, + 'error' => 'json_encode_failed', + 'message' => 'Failed to encode request body to JSON.', + ]; + } + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifyTls); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $verifyTls ? 2 : 0); + curl_setopt($ch, CURLOPT_FAILONERROR, false); + + $responseBody = curl_exec($ch); + if ($responseBody === false) { + $error = curl_error($ch) ?: 'Unknown cURL error'; + curl_close($ch); + return [ + 'success' => false, + 'error' => 'curl_error', + 'message' => $error, + ]; + } + + $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $decoded = null; + if ($responseBody !== '' && $responseBody !== null) { + $decoded = json_decode($responseBody, true); + if (json_last_error() !== JSON_ERROR_NONE) { + $decoded = null; + } + } + + if ($status >= 200 && $status < 300) { + return [ + 'success' => true, + 'status' => $status, + 'data' => $decoded ?? $responseBody, + ]; + } + + $errorMessage = 'AI proxy request failed'; + if (is_array($decoded)) { + $errorMessage = $decoded['error'] ?? $decoded['message'] ?? $errorMessage; + } elseif (is_string($responseBody) && $responseBody !== '') { + $errorMessage = $responseBody; + } + + return [ + 'success' => false, + 'status' => $status, + 'error' => $errorMessage, + 'response' => $decoded ?? $responseBody, + ]; + } + + /** + * Extract plain text from a Responses API payload. + * + * @param array $response Result of LocalAIApi::createResponse|request. + * @return string + */ + public static function extractText(array $response): string + { + $payload = $response['data'] ?? $response; + if (!is_array($payload)) { + return ''; + } + + if (!empty($payload['output']) && is_array($payload['output'])) { + $combined = ''; + foreach ($payload['output'] as $item) { + if (!isset($item['content']) || !is_array($item['content'])) { + continue; + } + foreach ($item['content'] as $block) { + if (is_array($block) && ($block['type'] ?? '') === 'output_text' && !empty($block['text'])) { + $combined .= $block['text']; + } + } + } + if ($combined !== '') { + return $combined; + } + } + + if (!empty($payload['choices'][0]['message']['content'])) { + return (string) $payload['choices'][0]['message']['content']; + } + + return ''; + } + + /** + * Attempt to decode JSON emitted by the model (handles markdown fences). + * + * @param array $response + * @return array|null + */ + public static function decodeJsonFromResponse(array $response): ?array + { + $text = self::extractText($response); + if ($text === '') { + return null; + } + + $decoded = json_decode($text, true); + if (is_array($decoded)) { + return $decoded; + } + + $stripped = preg_replace('/^```json|```$/m', '', trim($text)); + if ($stripped !== null && $stripped !== $text) { + $decoded = json_decode($stripped, true); + if (is_array($decoded)) { + return $decoded; + } + } + + return null; + } + + /** + * Load configuration from ai/config.php. + * + * @return array + */ + private static function config(): array + { + if (self::$configCache === null) { + $configPath = __DIR__ . '/config.php'; + if (!file_exists($configPath)) { + throw new RuntimeException('AI config file not found: ai/config.php'); + } + $cfg = require $configPath; + if (!is_array($cfg)) { + throw new RuntimeException('Invalid AI config format: expected array'); + } + self::$configCache = $cfg; + } + + return self::$configCache; + } + + /** + * Build an absolute URL from base_url and a path. + */ + private static function buildUrl(string $path, string $baseUrl): string + { + $trimmed = trim($path); + if ($trimmed === '') { + return $baseUrl; + } + if (str_starts_with($trimmed, 'http://') || str_starts_with($trimmed, 'https://')) { + return $trimmed; + } + if ($trimmed[0] === '/') { + return $baseUrl . $trimmed; + } + return $baseUrl . '/' . $trimmed; + } +} + +// Legacy alias for backward compatibility with the previous class name. +if (!class_exists('OpenAIService')) { + class_alias(LocalAIApi::class, 'OpenAIService'); +} diff --git a/ai/config.php b/ai/config.php new file mode 100644 index 0000000..1ba1596 --- /dev/null +++ b/ai/config.php @@ -0,0 +1,52 @@ + $baseUrl, + 'responses_path' => $responsesPath, + 'project_id' => $projectId, + 'project_uuid' => $projectUuid, + 'project_header' => 'project-uuid', + 'default_model' => 'gpt-5', + 'timeout' => 30, + 'verify_tls' => true, +]; diff --git a/assets/css/custom.css b/assets/css/custom.css new file mode 100644 index 0000000..eaac7b1 --- /dev/null +++ b/assets/css/custom.css @@ -0,0 +1,51 @@ + +body { + font-family: 'Inter', sans-serif; + background-color: #121212; + color: #E0E0E0; +} + +.navbar-dark .navbar-nav .nav-link { + color: rgba(255, 255, 255, .75); +} + +.navbar-dark .navbar-nav .nav-link.active, +.navbar-dark .navbar-nav .nav-link:hover { + color: #fff; +} + +.card { + background-color: #1E1E1E; + border: 1px solid rgba(255, 255, 255, 0.1); +} + +.form-control { + background-color: #2a2a2a; + color: #fff; + border: 1px solid #444; +} + +.form-control:focus { + background-color: #2a2a2a; + color: #fff; + border-color: #0D6EFD; + box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, .25); +} + +.form-control::placeholder { + color: #888; +} + +.btn-primary { + background-color: #0D6EFD; + border-color: #0D6EFD; +} + +.text-muted { + color: #A0A0A0 !important; +} + +.feature-icon { + font-size: 2.5rem; + color: #0D6EFD; +} diff --git a/dashboard.php b/dashboard.php new file mode 100644 index 0000000..baae238 --- /dev/null +++ b/dashboard.php @@ -0,0 +1,58 @@ + + +
+
+
+
+
+ Conversations +
+ +
+
+
+
+
+ Chat with User 1 +
+
+ +
+
+ Hello! +
+
+
+
+ Hi there! +
+
+
+ +
+
+
+
+ + diff --git a/db/config.php b/db/config.php index f12ebaf..d054803 100644 --- a/db/config.php +++ b/db/config.php @@ -14,4 +14,4 @@ function db() { ]); } return $pdo; -} +} \ No newline at end of file diff --git a/db/setup.php b/db/setup.php new file mode 100644 index 0000000..2fd5d6a --- /dev/null +++ b/db/setup.php @@ -0,0 +1,29 @@ + PDO::ERRMODE_EXCEPTION, + ]); + + // 2. Create the database if it doesn't exist + $pdo_admin->exec("CREATE DATABASE IF NOT EXISTS ".DB_NAME); + echo "Database '" . DB_NAME . "' created or already exists.\n"; + + // 3. Now, connect to the specific database and create the table + $pdo = db(); + $sql = " + CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + display_name VARCHAR(50) NOT NULL, + email VARCHAR(100) NOT NULL UNIQUE, + password_hash VARCHAR(255) NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + );"; + $pdo->exec($sql); + echo "Table 'users' created successfully (if it didn't exist).\n"; + +} catch (PDOException $e) { + die("DB SETUP ERROR: " . $e->getMessage()); +} \ No newline at end of file diff --git a/footer.php b/footer.php new file mode 100644 index 0000000..62c2604 --- /dev/null +++ b/footer.php @@ -0,0 +1,10 @@ + +
+
+ harmony3 © 2025 +
+
+ + + + diff --git a/header.php b/header.php new file mode 100644 index 0000000..7c5e038 --- /dev/null +++ b/header.php @@ -0,0 +1,54 @@ + + + + + + + harmony3 + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/index.php b/index.php index 7205f3d..63a7795 100644 --- a/index.php +++ b/index.php @@ -1,150 +1,43 @@ - -$phpVersion = PHP_VERSION; -$now = date('Y-m-d H:i:s'); -?> - - - - - - New Style - - - - - - - - - - - - - - - - - - - - - -
-
-

Analyzing your requirements and generating your website…

-
- Loading… -
-

AI is collecting your requirements and applying the first changes.

-

This page will update automatically as the plan is implemented.

-

Runtime: PHP — UTC

+
+
+
+

Connect Privately.

+

A simple, secure, and real-time messaging platform for personal conversations. Your space to talk, without the noise.

+ +
+
+ +
-
-
- Page updated: (UTC) -
- - + +
+
+
+ +
+

User Profiles

+

Create your profile, set your display name, and connect with others.

+
+
+
+ +
+

Direct Messaging

+

Start one-on-one conversations with any other registered user on the platform.

+
+
+
+ +
+

Real-Time Chat

+

Experience live messaging with WebSocket technology for instant communication.

+
+
+
+ + \ No newline at end of file diff --git a/login.php b/login.php new file mode 100644 index 0000000..861c80e --- /dev/null +++ b/login.php @@ -0,0 +1,106 @@ +window.top.location.href = "dashboard.php";'; + exit; +} + +$errors = []; +$registered_success = isset($_GET['registered']); + +// This block now only handles the STANDARD (non-fetch) form submission. +if ($_SERVER["REQUEST_METHOD"] == "POST") { + echo '
DEBUG: Standard POST handler reached.
'; + echo '
DEBUG: $_POST data: ' . htmlspecialchars(print_r($_POST, true)) . '
'; + + $email = trim($_POST['login_email']); + $password = $_POST['login_pass']; + + if (empty($email)) { + $errors[] = 'Email is required.'; + } + if (empty($password)) { + $errors[] = 'Password is required.'; + } + + if (empty($errors)) { + try { + $pdo = db(); + $stmt = $pdo->prepare("SELECT id, display_name, password_hash FROM users WHERE email = ?"); + $stmt->execute([$email]); + $user = $stmt->fetch(); + + if ($user && password_verify($password, $user['password_hash'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['display_name'] = $user['display_name']; + // Use JS redirect for consistency + echo ''; + exit; + } else { + $errors[] = 'Invalid email or password.'; + } + } catch (PDOException $e) { + error_log("Database error in login.php (Standard): " . $e->getMessage()); + $errors[] = 'A server error occurred. Please try again later.'; + } + } +} +?> +
+
+
+
+

Sign In

+ +
+ + + + + + + +
+ +
+
+ + +
+
+ + +
+
+ +
+
+
+ +
+
+
+ + diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..f83284d --- /dev/null +++ b/logout.php @@ -0,0 +1,6 @@ +prepare("SELECT id FROM users WHERE email = ?"); + $stmt->execute([$email]); + if ($stmt->fetch()) { + $errors[] = 'Email address is already registered.'; + } else { + $hashed_password = password_hash($password, PASSWORD_DEFAULT); + $stmt = $pdo->prepare("INSERT INTO users (display_name, email, password_hash) VALUES (?, ?, ?)"); + if ($stmt->execute([$display_name, $email, $hashed_password])) { + // Redirect to login page after successful registration + header("Location: login.php?registered=true"); + exit; + } else { + $errors[] = 'Failed to create account. Please try again later.'; + } + } + } catch (PDOException $e) { + error_log($e->getMessage()); + $errors[] = 'Database error. Please try again later.'; + } + } +} + +include 'header.php'; +?> + +
+
+
+
+

Create Account

+ + + + + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+ +
+
+
+ + \ No newline at end of file