# 🏢 Condo Manager Suite (PHP + SQLite + Telegram)

Gestione completa di **utenti, rate condominiali e notifiche Telegram**,
con protezione tramite **token temporanei**.\
Il progetto è suddiviso in 4 moduli principali.

------------------------------------------------------------------------

## 📂 Struttura repository

    .
    ├── users_api.php        # Gestione utenti (CRUD)
    ├── telegram_bot.php     # Bot Telegram (notifiche + comandi)
    ├── rate_api.php         # API per sincronizzazione rate Danea + notifiche
    ├── accesso.php          # Accesso sicuro con token temporaneo
    ├── Condominio.db        # Database SQLite
    └── README.md            # Documentazione

------------------------------------------------------------------------

## 👥 Users API

Gestisce gli utenti (collegati a Telegram).

### 🔹 Endpoint

-   **GET** `/users_api.php?telegram_id=123` → ottieni utente
-   **GET** `/users_api.php` → lista utenti
-   **POST** `/users_api.php` → aggiungi utente
-   **DELETE** `/users_api.php` → rimuovi utente

### 🔹 Esempi `curl`

``` bash
# Leggi tutti gli utenti
curl -H "Authorization: Bearer TOKEN123" https://tuodominio.it/users_api.php

# Crea nuovo utente
curl -X POST -H "Authorization: Bearer TOKEN123"   -H "Content-Type: application/json"   -d '{"telegram_id":"123","nome":"Mario","cognome":"Rossi"}'   https://tuodominio.it/users_api.php
```

------------------------------------------------------------------------

## 🤖 Telegram Bot

Gestisce le notifiche e i comandi ricevuti via Telegram.

### 🔹 Funzioni principali

-   Riceve messaggi via webhook
-   Risponde con messaggi di stato e rate
-   Invio notifiche con pulsanti inline (✅❌)

### 🔹 Esempio `curl`

``` bash
# Invio messaggio test
curl -X POST https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage   -d "chat_id=<CHAT_ID>&text=Ciao dal bot!"
```

------------------------------------------------------------------------

## 💰 Rate API

Gestione delle **rate condominiali** importate da Danea.

### 🔹 Endpoint

-   **GET** `/rate_api.php` → elenco rate
-   **POST** `/rate_api.php?action=update&id=1` → aggiorna rata (pagata)
-   **POST** `/rate_api.php` → importazione da Danea + notifiche

### 🔹 Esempio `curl`

``` bash
# Elenco rate
curl https://tuodominio.it/rate_api.php
```

Risposta:

``` json
[
  {
    "id": 1,
    "num": 5,
    "data": "2025-01-31",
    "descrizione": "Rata gestione 2025",
    "dovuto": 150.00,
    "saldo": 150.00,
    "stato": "DA PAGARE",
    "tipo": "[Condominio Alfa]"
  }
]
```

------------------------------------------------------------------------

## 🔑 Accesso con Token

Protegge le pagine HTML statiche con **link temporanei** firmati
(HMAC-SHA256).

### 🔹 Formato token

    <USER_ID>:<SCADENZA>:<FIRMA>

### 🔹 Esempio generazione (PHP)

``` php
$chiaveSegreta = "supersegreta123";
$userId = "25357";
$scadenza = time() + 3600; // 1 ora
$firma = hash_hmac("sha256", "$userId:$scadenza", $chiaveSegreta);
$token = "$userId:$scadenza:$firma";
echo "https://tuodominio.it/accesso.php?token=" . urlencode($token);
```

### 🔹 Esempio `curl`

``` bash
curl "https://tuodominio.it/accesso.php?token=25357:1734770460:abcd1234..."
```

Se valido → ritorna il contenuto di `25357.html`.\
Se scaduto/non valido → errore ❌.

------------------------------------------------------------------------

## ⚠️ Note di sicurezza

-   Proteggi i file `.db` e non esporli pubblicamente.
-   Usa **HTTPS** per tutte le richieste API.
-   Custodisci chiavi segrete e token Telegram fuori dal repo.
-   Logga gli errori in file separati (`api_error.log`).

------------------------------------------------------------------------

✍️ Autore: *\[Il tuo nome qui\]*
