]*>([^<]+)<\/t>/', $content, $matches);
return implode("\n", $matches[1] ?? []);
}
return '';
}
// Analyse image (OCR ou description)
private static function analyzeImage($filePath) {
$base64 = base64_encode(file_get_contents($filePath));
$mime = mime_content_type($filePath);
return [
'success' => true,
'type' => 'image',
'mime' => $mime,
'base64' => $base64,
'size' => filesize($filePath),
'dimensions' => getimagesize($filePath)
];
}
// ═══════════════════════════════════════════════════════════════════════════
// GÉNÉRATION DE SCHÉMAS
// ═══════════════════════════════════════════════════════════════════════════
// Générer un schéma Mermaid
public static function generateMermaid($type, $data) {
$mermaid = '';
switch ($type) {
case 'flowchart':
$mermaid = self::buildFlowchart($data);
break;
case 'sequence':
$mermaid = self::buildSequence($data);
break;
case 'er':
$mermaid = self::buildER($data);
break;
case 'class':
$mermaid = self::buildClassDiagram($data);
break;
case 'gantt':
$mermaid = self::buildGantt($data);
break;
default:
$mermaid = $data; // Raw mermaid code
}
return [
'success' => true,
'type' => 'mermaid',
'diagram_type' => $type,
'code' => $mermaid,
'html' => self::wrapMermaidHtml($mermaid)
];
}
private static function buildFlowchart($data) {
$lines = ["flowchart TD"];
if (is_array($data)) {
foreach ($data as $node) {
$lines[] = " " . $node;
}
} else {
$lines[] = $data;
}
return implode("\n", $lines);
}
private static function buildSequence($data) {
$lines = ["sequenceDiagram"];
if (is_array($data)) {
foreach ($data as $step) {
$lines[] = " " . $step;
}
}
return implode("\n", $lines);
}
private static function buildER($data) {
$lines = ["erDiagram"];
if (is_array($data)) {
foreach ($data as $relation) {
$lines[] = " " . $relation;
}
}
return implode("\n", $lines);
}
private static function buildClassDiagram($data) {
$lines = ["classDiagram"];
if (is_array($data)) {
foreach ($data as $class) {
$lines[] = " " . $class;
}
}
return implode("\n", $lines);
}
private static function buildGantt($data) {
$lines = ["gantt", " dateFormat YYYY-MM-DD"];
if (is_array($data)) {
foreach ($data as $task) {
$lines[] = " " . $task;
}
}
return implode("\n", $lines);
}
private static function wrapMermaidHtml($code) {
return '
' . htmlspecialchars($code) . '
';
}
// ═══════════════════════════════════════════════════════════════════════════
// GÉNÉRATION D'ARTIFACTS
// ═══════════════════════════════════════════════════════════════════════════
public static function createArtifact($type, $content, $title = 'artifact') {
$dir = '/opt/wevads/public/artifacts/';
if (!is_dir($dir)) mkdir($dir, 0755, true);
$id = uniqid('art_');
$ext = self::getArtifactExtension($type);
$filename = "{$id}.{$ext}";
$filepath = $dir . $filename;
// Wrapper selon le type
switch ($type) {
case 'html':
$wrapped = self::wrapHtmlArtifact($content, $title);
break;
case 'react':
$wrapped = self::wrapReactArtifact($content, $title);
break;
case 'svg':
$wrapped = $content;
break;
case 'mermaid':
$wrapped = self::wrapMermaidHtml($content);
$ext = 'html';
$filename = "{$id}.html";
$filepath = $dir . $filename;
break;
default:
$wrapped = $content;
}
file_put_contents($filepath, $wrapped);
return [
'success' => true,
'type' => $type,
'id' => $id,
'filename' => $filename,
'path' => $filepath,
'url' => "/artifacts/{$filename}",
'content' => $wrapped
];
}
private static function getArtifactExtension($type) {
$map = [
'html' => 'html',
'react' => 'html',
'svg' => 'svg',
'mermaid' => 'html',
'javascript' => 'js',
'python' => 'py',
'php' => 'php',
'css' => 'css',
'json' => 'json',
'markdown' => 'md'
];
return $map[$type] ?? 'txt';
}
private static function wrapHtmlArtifact($content, $title) {
// Si déjà un document complet
if (stripos($content, '
' . htmlspecialchars($title) . '
' . $content . '
';
}
private static function wrapReactArtifact($content, $title) {
return '
' . htmlspecialchars($title) . '
';
}
// ═══════════════════════════════════════════════════════════════════════════
// EXÉCUTION DE CODE
// ═══════════════════════════════════════════════════════════════════════════
public static function executeCode($language, $code, $timeout = 30) {
$result = ['success' => false, 'output' => '', 'error' => ''];
$tmpDir = '/tmp/hamid_exec/';
if (!is_dir($tmpDir)) mkdir($tmpDir, 0755, true);
$id = uniqid();
switch (strtolower($language)) {
case 'bash':
case 'sh':
$file = "{$tmpDir}{$id}.sh";
file_put_contents($file, $code);
$result['output'] = shell_exec("timeout {$timeout} bash '$file' 2>&1");
unlink($file);
$result['success'] = true;
break;
case 'python':
case 'py':
$file = "{$tmpDir}{$id}.py";
file_put_contents($file, $code);
$result['output'] = shell_exec("timeout {$timeout} python3 '$file' 2>&1");
unlink($file);
$result['success'] = true;
break;
case 'php':
$file = "{$tmpDir}{$id}.php";
file_put_contents($file, "&1");
unlink($file);
$result['success'] = true;
break;
case 'sql':
$result['output'] = shell_exec("timeout {$timeout} PGPASSWORD=admin123 psql -U admin -d adx_system -c " . escapeshellarg($code) . " 2>&1");
$result['success'] = true;
break;
case 'javascript':
case 'js':
$file = "{$tmpDir}{$id}.js";
file_put_contents($file, $code);
$result['output'] = shell_exec("timeout {$timeout} node '$file' 2>&1");
unlink($file);
$result['success'] = true;
break;
default:
$result['error'] = "Langage non supporté: $language";
}
return $result;
}
// ═══════════════════════════════════════════════════════════════════════════
// UTILITAIRES
// ═══════════════════════════════════════════════════════════════════════════
public static function listArtifacts() {
$dir = '/opt/wevads/public/artifacts/';
if (!is_dir($dir)) return [];
$files = [];
foreach (glob($dir . '*') as $file) {
$files[] = [
'name' => basename($file),
'path' => $file,
'url' => '/artifacts/' . basename($file),
'size' => filesize($file),
'modified' => filemtime($file)
];
}
return $files;
}
public static function deleteArtifact($id) {
$dir = '/opt/wevads/public/artifacts/';
$files = glob($dir . $id . '*');
foreach ($files as $file) {
unlink($file);
}
return ['success' => true];
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// API ENDPOINTS POUR CAPACITÉS AVANCÉES
// ═══════════════════════════════════════════════════════════════════════════════
if (basename($_SERVER['SCRIPT_NAME']) === 'hamid-brain.php') {
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
$action = $input['action'] ?? $_GET['action'] ?? '';
// Analyze document
if ($action === 'analyze') {
header('Content-Type: application/json');
$file = $input['file'] ?? '';
if ($file && file_exists($file)) {
echo json_encode(HamidCapabilities::analyzeDocument($file));
} else {
echo json_encode(['success' => false, 'error' => 'Fichier non trouvé']);
}
exit;
}
// Create artifact
if ($action === 'artifact') {
header('Content-Type: application/json');
$type = $input['type'] ?? 'html';
$content = $input['content'] ?? '';
$title = $input['title'] ?? 'artifact';
echo json_encode(HamidCapabilities::createArtifact($type, $content, $title));
exit;
}
// Generate mermaid
if ($action === 'mermaid') {
header('Content-Type: application/json');
$type = $input['diagram_type'] ?? 'flowchart';
$data = $input['data'] ?? [];
echo json_encode(HamidCapabilities::generateMermaid($type, $data));
exit;
}
// Execute code
if ($action === 'execute') {
header('Content-Type: application/json');
$lang = $input['language'] ?? 'bash';
$code = $input['code'] ?? '';
echo json_encode(HamidCapabilities::executeCode($lang, $code));
exit;
}
// List artifacts
if ($action === 'list_artifacts') {
header('Content-Type: application/json');
echo json_encode(['success' => true, 'artifacts' => HamidCapabilities::listArtifacts()]);
exit;
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// API ENDPOINTS POUR CAPACITÉS AVANCÉES
// ═══════════════════════════════════════════════════════════════════════════════
if (basename($_SERVER['SCRIPT_NAME']) === 'hamid-brain.php') {
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
$action = $input['action'] ?? $_GET['action'] ?? '';
// Analyze document
if ($action === 'analyze') {
header('Content-Type: application/json');
$file = $input['file'] ?? '';
if ($file && file_exists($file)) {
echo json_encode(HamidCapabilities::analyzeDocument($file));
} else {
echo json_encode(['success' => false, 'error' => 'Fichier non trouvé']);
}
exit;
}
// Create artifact
if ($action === 'artifact') {
header('Content-Type: application/json');
$type = $input['type'] ?? 'html';
$content = $input['content'] ?? '';
$title = $input['title'] ?? 'artifact';
echo json_encode(HamidCapabilities::createArtifact($type, $content, $title));
exit;
}
// Generate mermaid
if ($action === 'mermaid') {
header('Content-Type: application/json');
$type = $input['diagram_type'] ?? 'flowchart';
$data = $input['data'] ?? [];
echo json_encode(HamidCapabilities::generateMermaid($type, $data));
exit;
}
// Execute code
if ($action === 'execute') {
header('Content-Type: application/json');
$lang = $input['language'] ?? 'bash';
$code = $input['code'] ?? '';
echo json_encode(HamidCapabilities::executeCode($lang, $code));
exit;
}
// List artifacts
if ($action === 'list_artifacts') {
header('Content-Type: application/json');
echo json_encode(['success' => true, 'artifacts' => HamidCapabilities::listArtifacts()]);
exit;
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// API ENDPOINTS POUR CAPACITÉS AVANCÉES
// ═══════════════════════════════════════════════════════════════════════════════
if (basename($_SERVER['SCRIPT_NAME']) === 'hamid-brain.php') {
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
$action = $input['action'] ?? $_GET['action'] ?? '';
// Analyze document
if ($action === 'analyze') {
header('Content-Type: application/json');
$file = $input['file'] ?? '';
if ($file && file_exists($file)) {
echo json_encode(HamidCapabilities::analyzeDocument($file));
} else {
echo json_encode(['success' => false, 'error' => 'Fichier non trouvé']);
}
exit;
}
// Create artifact
if ($action === 'artifact') {
header('Content-Type: application/json');
$type = $input['type'] ?? 'html';
$content = $input['content'] ?? '';
$title = $input['title'] ?? 'artifact';
echo json_encode(HamidCapabilities::createArtifact($type, $content, $title));
exit;
}
// Generate mermaid
if ($action === 'mermaid') {
header('Content-Type: application/json');
$type = $input['diagram_type'] ?? 'flowchart';
$data = $input['data'] ?? [];
echo json_encode(HamidCapabilities::generateMermaid($type, $data));
exit;
}
// Execute code
if ($action === 'execute') {
header('Content-Type: application/json');
$lang = $input['language'] ?? 'bash';
$code = $input['code'] ?? '';
echo json_encode(HamidCapabilities::executeCode($lang, $code));
exit;
}
// List artifacts
if ($action === 'list_artifacts') {
header('Content-Type: application/json');
echo json_encode(['success' => true, 'artifacts' => HamidCapabilities::listArtifacts()]);
exit;
}
}
// ═══════════════════════════════════════════════════════════════════════════════
// GÉNÉRATION DE DOCUMENTS TÉLÉCHARGEABLES
// ═══════════════════════════════════════════════════════════════════════════════
class HamidDocuments {
private static $docsDir = '/opt/wevads/public/docs/';
public static function generateTxt($title, $content) {
$id = uniqid('doc_');
$filename = $id . '.txt';
$filepath = self::$docsDir . $filename;
file_put_contents($filepath, "=== $title ===\n\n$content");
return self::docResponse($id, $filename, $filepath, 'txt', $title);
}
public static function generateMarkdown($title, $content) {
$id = uniqid('doc_');
$filename = $id . '.md';
$filepath = self::$docsDir . $filename;
file_put_contents($filepath, "# $title\n\n$content");
return self::docResponse($id, $filename, $filepath, 'md', $title);
}
public static function generateHtml($title, $content) {
$id = uniqid('doc_');
$filename = $id . '.html';
$filepath = self::$docsDir . $filename;
$html = "$title
$title
$content