# Central License System - Setup Guide

## Prerequisites
- cPanel hosting with PHP 7.4+ and MySQL
- 2FA authenticator app for cPanel

---

## Step 1: Database Setup

1. Go to **cPanel → phpMyAdmin** (or MySQL Databases)
2. Click **Import** tab
3. Upload `schema.sql` file

**OR** run in MySQL CLI:
```sql
CREATE DATABASE silverwo_licenses;
USE silverwo_licenses;
-- (paste contents of schema.sql)
```

---

## Step 2: Create MySQL User

In **cPanel → MySQL Databases**:

1. Create new user:
   - Username: `silverwo_licenseadm`
   - Password: `YOUR_DB_PASSWORD_HERE`

2. Add user to database: `silverwo_licenses`

---

## Step 3: Edit config/db.php

Update these values in `config/db.php`:

```php
define('DB_HOST', 'localhost');
define('DB_NAME', 'silverwo_licenses');
define('DB_USER', 'silverwo_licenseadm');
define('DB_PASS', 'YOUR_DB_PASSWORD_HERE');
define('API_KEY', 'YOUR_SECRET_API_KEY_HERE');
define('ADMIN_USER', 'admin');
define('ADMIN_PASS', 'YOUR_ADMIN_PASSWORD_HERE');
```

**Important:**
- Use a strong random string for `API_KEY` (min 32 chars)
- Change `ADMIN_PASS` from default!

---

## Step 4: Upload Files

Upload the folder structure to `public_html/`:

```
public_html/
├── api/
│   ├── verify.php
│   ├── activate.php
│   └── status.php
├── admin/
│   ├── login.php
│   ├── dashboard.php
│   ├── create_license.php
│   ├── manage_licenses.php
│   └── logs.php
└── config/
    └── db.php
```

**Recommended:** Put `api/` and `config/` outside `public_html/` for security:
```
/home/silverwo/
├── license_api/          (NOT web accessible)
│   ├── api/
│   └── config/
└── public_html/          (web root)
    └── license_admin/    (admin panel)
```

---

## Step 5: Test API

Test with curl (replace values):
```bash
curl -X POST https://yourdomain.com/api/verify.php \
  -H "Content-Type: application/json" \
  -H "X-API-KEY: YOUR_SECRET_API_KEY_HERE" \
  -d '{"license":"XXXX-YYYY-ZZZZ","product":"openclaw","hwid":"TEST-HWID"}'
```

Expected response:
```json
{"valid":false,"message":"License not found"}
```
(Means API is working, just need to create a license first)

---

## Step 6: Create First License

1. Go to `https://yourdomain.com/admin/`
2. Login with admin credentials
3. Go to **Create License**
4. Enter product name (e.g. `openclaw`)
5. Set expiry date
6. Generate

---

## Step 7: Protect Admin Panel

Add `.htaccess` protection for admin folder:

```apache
# In admin/.htaccess
<IfModule mod_rewrite.c>
    RewriteEngine On
    # Allow only specific IPs (optional)
    # Require ip 123.456.789.012
</IfModule>
```

---

## API Usage Examples

### Verify License (client app):
```php
$response = post_json("https://yourdomain.com/api/verify.php", [
    "license" => "XXXX-YYYY-ZZZZ",
    "product" => "openclaw",
    "hwid" => get_hwid()
]);

if ($response['valid']) {
    echo "License OK, expires: " . $response['expire_date'];
} else {
    echo "License error: " . $response['message'];
}
```

### Activate License:
```php
$response = post_json("https://yourdomain.com/api/activate.php", [
    "license" => "XXXX-YYYY-ZZZZ",
    "product" => "openclaw",
    "hwid" => get_hwid()
]);
```

---

## Troubleshooting

**"Database connection failed":**
- Check db.php credentials match cPanel MySQL settings

**"Invalid API key":**
- Make sure `X-API-KEY` header matches exactly what's in config/db.php

**"Rate limit exceeded":**
- Wait 60 seconds, or adjust `$rate_limit` in verify.php

**502/500 errors:**
- Check PHP error logs in cPanel
- Make sure PHP version is 7.4+

---

## Security Checklist

- [ ] Changed default admin password
- [ ] Used strong random API_KEY
- [ ] Placed config files outside public_html (recommended)
- [ ] Enabled HTTPS (Let's Encrypt in cPanel)
- [ ] Added .htaccess password protection to admin folder (optional)
- [ ] Regular backups of licenses table