# Site Builder - WHP Integration Guide
## Overview
This site builder integrates with WHP (Web Hosting Panel) to provide users with a visual site building interface. Users can create HTML pages using a drag-and-drop editor and save them directly to their web hosting account.
## Architecture
### Components
1. **Frontend (GrapesJS)**
- Location: `/home/jknapp/code/site-builder/`
- Visual drag-and-drop editor
- Real-time preview
- Device responsive design tools
2. **Backend API**
- Location: `/home/jknapp/code/whp/web-files/api/site-builder.php`
- Handles save/load/list/delete operations
- Integrates with WHP authentication system
- Stores sites in user's home directory
3. **WHP Integration Layer**
- File: `js/whp-integration.js`
- Connects frontend to backend API
- Provides save/load UI
- Auto-save functionality
## Installation
### 1. Deploy Backend API
Copy the site builder API to the WHP web files:
```bash
cp /home/jknapp/code/whp/web-files/api/site-builder.php /docker/whp/web/api/
```
Or run WHP update script to sync:
```bash
cd /home/jknapp/code/whp
./update.sh
```
### 2. Deploy Frontend
Copy the site builder files to the WHP web directory:
```bash
# Create site-builder directory in WHP
mkdir -p /docker/whp/web/site-builder
# Copy all files
cp -r /home/jknapp/code/site-builder/* /docker/whp/web/site-builder/
```
### 3. Configure Access
The site builder should be accessible to authenticated WHP users at:
```
https://your-whp-domain.com/site-builder/
```
## User Directory Structure
When a user saves a site, it creates the following structure:
```
/home/{username}/public_html/site-builder/
├── sites/
│ ├── site_abc123.json # GrapesJS project data
│ ├── site_abc123.html # Compiled HTML output
│ ├── site_xyz789.json
│ └── site_xyz789.html
```
### File Formats
**JSON File** (`.json`):
```json
{
"id": "site_abc123",
"name": "My Awesome Site",
"created": 1708531200,
"modified": 1708617600,
"html": "
...
",
"css": "body { ... }",
"grapesjs": { ... }
}
```
**HTML File** (`.html`):
- Complete, standalone HTML file
- Includes embedded CSS
- Ready to publish/share
- Accessible via: `https://your-domain.com/~username/site-builder/sites/site_abc123.html`
## API Endpoints
All endpoints require WHP authentication (session-based).
### List Sites
```
GET /api/site-builder.php?action=list
```
Response:
```json
{
"success": true,
"sites": [
{
"id": "site_abc123",
"name": "My Site",
"created": 1708531200,
"modified": 1708617600,
"url": "/~username/site-builder/sites/site_abc123.html"
}
]
}
```
### Load Site
```
GET /api/site-builder.php?action=load&id=site_abc123
```
Response:
```json
{
"success": true,
"site": {
"id": "site_abc123",
"name": "My Site",
"html": "...
",
"css": "body { ... }",
"grapesjs": { ... }
}
}
```
### Save Site
```
POST /api/site-builder.php?action=save
Content-Type: application/json
{
"id": "site_abc123",
"name": "My Site",
"html": "...
",
"css": "body { ... }",
"grapesjs": { ... }
}
```
Response:
```json
{
"success": true,
"site": {
"id": "site_abc123",
"name": "My Site",
"url": "/~username/site-builder/sites/site_abc123.html"
}
}
```
### Delete Site
```
GET /api/site-builder.php?action=delete&id=site_abc123
```
Response:
```json
{
"success": true,
"message": "Site deleted successfully"
}
```
## Security
### Authentication
- Uses WHP's existing authentication system
- `AUTH_USER` constant provides current username
- `HOME_DIR` constant provides user's home directory path
- Session-based authentication (handled by `auto-prepend.php`)
### Path Security
- All user paths are validated and sanitized
- `basename()` used to prevent path traversal
- Files stored only in user's home directory
- No cross-user access possible
### File Permissions
- Site directories created with `0755` permissions
- Files inherit user's ownership
- Web server can read/serve files
- Users can only access their own sites
## Frontend Integration
### Adding WHP Integration to Index.html
Add this script tag before the closing `