false, 'message' => 'Il titolo del blocknote è obbligatorio.']; } $id = 'bn_' . uniqid(); $data = [ 'id' => $id, 'title' => $title, 'username' => $username, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), 'notes' => [] ]; if (save_blocknote($data)) { return ['success' => true, 'id' => $id, 'message' => 'Blocknote creato con successo!']; } return ['success' => false, 'message' => 'Errore nel salvataggio del file JSON.']; } /** * Import a JSON string (supports standard format and legacy codes format). */ function import_blocknote_json($json_string, $username, $custom_title = '') { $data = json_decode($json_string, true); if (!is_array($data)) { return ['success' => false, 'message' => 'File JSON non valido o malformato.']; } $id = 'bn_' . uniqid(); $title = !empty($custom_title) ? trim($custom_title) : 'Blocknote Importato'; // Case 1: Standard format with 'notes' array if (isset($data['notes']) && is_array($data['notes'])) { $bn = [ 'id' => $id, 'title' => !empty($data['title']) ? $data['title'] : $title, 'username' => $username, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), 'notes' => $data['notes'] ]; if (save_blocknote($bn)) { return ['success' => true, 'id' => $id, 'message' => 'Blocknote importato con successo!']; } } // Case 2: Legacy format (array of notes with 'title', 'description', 'codes') if (isset($data[0]) && is_array($data[0])) { $notes = []; foreach ($data as $n_idx => $legacy_note) { $note_id = 'note_' . ($n_idx + 1) . '_' . uniqid(); $subnotes = []; if (isset($legacy_note['codes']) && is_array($legacy_note['codes'])) { foreach ($legacy_note['codes'] as $s_idx => $legacy_code) { $subnotes[] = [ 'id' => 'sub_' . ($s_idx + 1) . '_' . uniqid(), 'title' => $legacy_code['sub_title'] ?? 'Comando', 'type' => 'command', 'content' => $legacy_code['code'] ?? '' ]; } } $notes[] = [ 'id' => $note_id, 'title' => $legacy_note['title'] ?? 'Nota ' . ($n_idx + 1), 'description' => $legacy_note['description'] ?? '', 'subnotes' => $subnotes ]; } $bn = [ 'id' => $id, 'title' => $title, 'username' => $username, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'), 'notes' => $notes ]; if (save_blocknote($bn)) { return ['success' => true, 'id' => $id, 'message' => 'Blocknote in formato legacy importato e convertito con successo!']; } } return ['success' => false, 'message' => 'Formato JSON non riconosciuto.']; } /** * Check permission on blocknote. */ function can_access_blocknote($bn, $username, $is_admin) { if (!$bn) return false; if ($is_admin) return true; return strtolower($bn['username'] ?? '') === strtolower($username); } /** * Rename a blocknote. */ function rename_blocknote($id, $new_title, $username, $is_admin) { $bn = get_blocknote_by_id($id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) { return ['success' => false, 'message' => 'Non hai i permessi per modificare questo blocknote.']; } $new_title = trim($new_title); if (empty($new_title)) return ['success' => false, 'message' => 'Il titolo non può essere vuoto.']; $bn['title'] = $new_title; if (save_blocknote($bn)) { return ['success' => true, 'message' => 'Blocknote rinominato con successo.']; } return ['success' => false, 'message' => 'Errore nel salvataggio del file JSON.']; } /** * Delete a blocknote. */ function delete_blocknote($id, $username, $is_admin) { $bn = get_blocknote_by_id($id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) { return ['success' => false, 'message' => 'Non hai i permessi per eliminare questo blocknote.']; } $filepath = BLOCKNOTES_FOLD . preg_replace('/[^a-zA-Z0-9_\-]/', '', $bn['id']) . ".json"; if (file_exists($filepath)) { @unlink($filepath); } return ['success' => true, 'message' => 'Blocknote eliminato con successo.']; } /** * Add a new Note block to a Blocknote. */ function add_note($blocknote_id, $title, $description, $username, $is_admin) { $bn = get_blocknote_by_id($blocknote_id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) return ['success' => false, 'message' => 'Permesso negato.']; $title = trim($title); if (empty($title)) return ['success' => false, 'message' => 'Il titolo della nota è obbligatorio.']; $note_id = 'note_' . uniqid(); $new_note = [ 'id' => $note_id, 'title' => $title, 'description' => trim($description), 'subnotes' => [] ]; $bn['notes'][] = $new_note; if (save_blocknote($bn)) { return ['success' => true, 'note_id' => $note_id, 'message' => 'Nota aggiunta con successo!']; } return ['success' => false, 'message' => 'Errore durante il salvataggio.']; } /** * Edit an existing Note block. */ function edit_note($blocknote_id, $note_id, $title, $description, $username, $is_admin) { $bn = get_blocknote_by_id($blocknote_id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) return ['success' => false, 'message' => 'Permesso negato.']; $title = trim($title); if (empty($title)) return ['success' => false, 'message' => 'Il titolo della nota è obbligatorio.']; $found = false; foreach ($bn['notes'] as &$note) { if ($note['id'] === $note_id) { $note['title'] = $title; $note['description'] = trim($description); $found = true; break; } } if (!$found) return ['success' => false, 'message' => 'Nota non trovata.']; if (save_blocknote($bn)) { return ['success' => true, 'message' => 'Nota modificata con successo!']; } return ['success' => false, 'message' => 'Errore nel salvataggio.']; } /** * Delete a Note block. */ function delete_note($blocknote_id, $note_id, $username, $is_admin) { $bn = get_blocknote_by_id($blocknote_id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) return ['success' => false, 'message' => 'Permesso negato.']; $initial_count = count($bn['notes']); $bn['notes'] = array_values(array_filter($bn['notes'], function($n) use ($note_id) { return $n['id'] !== $note_id; })); if (count($bn['notes']) === $initial_count) { return ['success' => false, 'message' => 'Nota non trovata.']; } if (save_blocknote($bn)) { return ['success' => true, 'message' => 'Nota eliminata con successo.']; } return ['success' => false, 'message' => 'Errore nel salvataggio.']; } /** * Add a Subnote to a Note block. */ function add_subnote($blocknote_id, $note_id, $sub_title, $type, $content, $username, $is_admin) { $bn = get_blocknote_by_id($blocknote_id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) return ['success' => false, 'message' => 'Permesso negato.']; $sub_title = trim($sub_title); if (empty($sub_title)) return ['success' => false, 'message' => 'Il titolo della sottonota è obbligatorio.']; $type = ($type === 'command') ? 'command' : 'text'; $sub_id = 'sub_' . uniqid(); $found = false; foreach ($bn['notes'] as &$note) { if ($note['id'] === $note_id) { if (!isset($note['subnotes'])) $note['subnotes'] = []; $note['subnotes'][] = [ 'id' => $sub_id, 'title' => $sub_title, 'type' => $type, 'content' => $content ]; $found = true; break; } } if (!$found) return ['success' => false, 'message' => 'Nota genitore non trovata.']; if (save_blocknote($bn)) { return ['success' => true, 'subnote_id' => $sub_id, 'message' => 'Sottonota aggiunta con successo!']; } return ['success' => false, 'message' => 'Errore nel salvataggio.']; } /** * Edit a Subnote. */ function edit_subnote($blocknote_id, $note_id, $sub_id, $sub_title, $type, $content, $username, $is_admin) { $bn = get_blocknote_by_id($blocknote_id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) return ['success' => false, 'message' => 'Permesso negato.']; $sub_title = trim($sub_title); if (empty($sub_title)) return ['success' => false, 'message' => 'Il titolo della sottonota è obbligatorio.']; $type = ($type === 'command') ? 'command' : 'text'; $found = false; foreach ($bn['notes'] as &$note) { if ($note['id'] === $note_id) { if (isset($note['subnotes'])) { foreach ($note['subnotes'] as &$sub) { if ($sub['id'] === $sub_id) { $sub['title'] = $sub_title; $sub['type'] = $type; $sub['content'] = $content; $found = true; break 2; } } } } } if (!$found) return ['success' => false, 'message' => 'Sottonota non trovata.']; if (save_blocknote($bn)) { return ['success' => true, 'message' => 'Sottonota modificata con successo!']; } return ['success' => false, 'message' => 'Errore nel salvataggio.']; } /** * Delete a Subnote. */ function delete_subnote($blocknote_id, $note_id, $sub_id, $username, $is_admin) { $bn = get_blocknote_by_id($blocknote_id); if (!$bn) return ['success' => false, 'message' => 'Blocknote non trovato.']; if (!can_access_blocknote($bn, $username, $is_admin)) return ['success' => false, 'message' => 'Permesso negato.']; $found = false; foreach ($bn['notes'] as &$note) { if ($note['id'] === $note_id && isset($note['subnotes'])) { $initial = count($note['subnotes']); $note['subnotes'] = array_values(array_filter($note['subnotes'], function($s) use ($sub_id) { return $s['id'] !== $sub_id; })); if (count($note['subnotes']) < $initial) { $found = true; break; } } } if (!$found) return ['success' => false, 'message' => 'Sottonota non trovata.']; if (save_blocknote($bn)) { return ['success' => true, 'message' => 'Sottonota eliminata con successo.']; } return ['success' => false, 'message' => 'Errore nel salvataggio.']; } /** * Compatibility Aliases */ function add_note_to_blocknote($blocknote_id, $title, $description, $username, $is_admin) { return add_note($blocknote_id, $title, $description, $username, $is_admin); } function edit_note_in_blocknote($blocknote_id, $note_id, $title, $description, $username, $is_admin) { return edit_note($blocknote_id, $note_id, $title, $description, $username, $is_admin); } function delete_note_from_blocknote($blocknote_id, $note_id, $username, $is_admin) { return delete_note($blocknote_id, $note_id, $username, $is_admin); } function add_subnote_to_note($blocknote_id, $note_id, $sub_title, $type, $content, $username, $is_admin) { return add_subnote($blocknote_id, $note_id, $sub_title, $type, $content, $username, $is_admin); } function edit_subnote_in_note($blocknote_id, $note_id, $sub_id, $sub_title, $type, $content, $username, $is_admin) { return edit_subnote($blocknote_id, $note_id, $sub_id, $sub_title, $type, $content, $username, $is_admin); } function delete_subnote_from_note($blocknote_id, $note_id, $sub_id, $username, $is_admin) { return delete_subnote($blocknote_id, $note_id, $sub_id, $username, $is_admin); }