Files
sld-blocknotes/functions/blocknotes.php
T
2026-08-25 16:42:26 +01:00

450 lines
15 KiB
PHP

<?php
/**
* Blocknotes JSON Storage Engine Functions
*/
/**
* Ensure storage directory exists.
*/
function ensure_blocknotes_directory() {
if (!is_dir(BLOCKNOTES_FOLD)) {
@mkdir(BLOCKNOTES_FOLD, 0777, true);
}
}
/**
* Scan BLOCKNOTES_FOLD and load all JSON files.
*/
function get_all_blocknotes() {
ensure_blocknotes_directory();
$files = glob(BLOCKNOTES_FOLD . "*.json");
$blocknotes = [];
if ($files) {
foreach ($files as $filepath) {
$content = file_get_contents($filepath);
$data = json_decode($content, true);
if (is_array($data) && isset($data['id'])) {
$blocknotes[] = $data;
}
}
}
// Sort by updated_at descending
usort($blocknotes, function($a, $b) {
return strtotime($b['updated_at'] ?? 0) - strtotime($a['updated_at'] ?? 0);
});
return $blocknotes;
}
/**
* Get blocknotes assigned to a specific user (or all if admin).
*/
function get_user_blocknotes($username, $is_admin = false) {
$all = get_all_blocknotes();
if ($is_admin) {
return $all;
}
return array_filter($all, function($bn) use ($username) {
return isset($bn['username']) && strtolower($bn['username']) === strtolower($username);
});
}
/**
* Find blocknote by ID.
*/
function get_blocknote_by_id($id) {
ensure_blocknotes_directory();
$filename = BLOCKNOTES_FOLD . preg_replace('/[^a-zA-Z0-9_\-]/', '', $id) . ".json";
if (file_exists($filename)) {
$content = file_get_contents($filename);
$data = json_decode($content, true);
if (is_array($data)) return $data;
}
// Fallback search across all files if ID is stored inside
$all = get_all_blocknotes();
foreach ($all as $bn) {
if (isset($bn['id']) && $bn['id'] === $id) {
return $bn;
}
}
return null;
}
/**
* Save blocknote to JSON file.
*/
function save_blocknote($data) {
ensure_blocknotes_directory();
if (empty($data['id'])) {
$data['id'] = 'bn_' . uniqid();
}
$data['updated_at'] = date('Y-m-d H:i:s');
if (empty($data['created_at'])) {
$data['created_at'] = $data['updated_at'];
}
if (!isset($data['notes'])) {
$data['notes'] = [];
}
$filepath = BLOCKNOTES_FOLD . preg_replace('/[^a-zA-Z0-9_\-]/', '', $data['id']) . ".json";
$json_string = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
return file_put_contents($filepath, $json_string) !== false;
}
/**
* Create a new blocknote.
*/
function create_blocknote($title, $username) {
$title = trim($title);
if (empty($title)) {
return ['success' => 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);
}