# WHATBOX - PHP Native Setup

Script PHP native (tanpa Laravel) untuk whatbox server.

## File Structure

```
whatbox/
├── api.php                    # API endpoint untuk receive file
├── config.php                 # Config loader
├── config.local.php          # Local config (buat sendiri, jangan commit)
├── generate_key.php          # Generate API key
├── download_file.php         # Script download single file
├── download_drama_native.php # Script download drama lengkap
└── README_NATIVE.md          # Dokumentasi ini
```

## Setup

### 1. Generate API Key

```bash
cd /var/www/whatbox/whatbox
php generate_key.php
```

Copy output key ke:
- `config.local.php` -> `api_key`
- Server utama `.env` -> `WHATBOX_API_KEY`

### 2. Buat config.local.php

```bash
cp config.local.php.example config.local.php
# Edit config.local.php, paste API key
```

Contoh `config.local.php`:
```php
<?php
$config = [
    'api_key' => 'your-generated-key-here',
    'path' => '/var/www/whatbox',
    'allowed_ips' => [], // Kosong = semua IP
    'max_file_size' => 1024 * 1024 * 1024, // 1GB
];
```

### 3. Set Permissions

```bash
chmod 755 api.php
chmod 755 download_file.php
chmod 755 download_drama_native.php
chmod 644 config.php
chmod 600 config.local.php  # Hanya owner bisa baca
```

## Usage

### 1. Download Single File

```bash
php download_file.php \
  --url="https://example.com/file.jpg" \
  --path="dramawave/123/cover.jpg"
```

### 2. Download Drama Lengkap

```bash
php download_drama_native.php \
  --drama-id=xuyr3DtXPt \
  --lang=id
```

### 3. API Endpoint (dari Server Utama)

**Upload File:**
```bash
curl -X POST https://your-whatbox-server.com/drama/api.php \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "file=@/path/to/file.jpg" \
  -F "path=dramawave/123/cover.jpg"
```

**Download File via API:**
```bash
curl -X POST "https://your-whatbox-server.com/drama/api.php?action=download" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d "url=https://example.com/file.jpg" \
  -d "path=dramawave/123/cover.jpg"
```

**Health Check:**
```bash
curl https://your-whatbox-server.com/whatbox/api.php?action=health
```

## Web Server Setup

### Nginx

```nginx
location /whatbox/api.php {
    fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    fastcgi_index api.php;
    fastcgi_param SCRIPT_FILENAME $document_root/whatbox/api.php;
    include fastcgi_params;
}
```

### Apache (.htaccess)

```apache
<Directory /var/www/whatbox/whatbox>
    Options -Indexes
    AllowOverride All
    Require all granted
</Directory>
```

## Security

1. **Jangan commit `config.local.php` ke git!**
2. **Set permission 600 untuk `config.local.php`**
3. **Gunakan HTTPS untuk API endpoint**
4. **Limit IP jika perlu** (edit `allowed_ips` di config)
5. **Rotate API key secara berkala**

## Troubleshooting

### Error: Permission denied

```bash
chown -R www-data:www-data /var/www/whatbox/whatbox
chmod -R 755 /var/www/whatbox/whatbox
```

### Error: API Key invalid

- Pastikan API key sama di `config.local.php` dan server utama `.env`
- Cek header Authorization: `Bearer YOUR_KEY`

### Error: Failed to download

- Cek koneksi internet
- Cek URL valid
- Cek disk space: `df -h`

