# Lizardman Pages (MVP)

Minimal PHP + Bootstrap implementation of the **frontend author site** described in `Design Docs/lizardman-pages.md`.

This is intentionally lightweight:
- DB connection scaffold exists, but public content still reads from JSON by default.
- Admin auth + template package manager is available.
- Theme + component structure is in place so it can grow into the full CMS.

## Requirements
- PHP 8.1+ (recommended)
- Composer (for Twig)

## Security Lockdown (Recommended for Production)

Runtime executable functions should be disabled in `php.ini`:

- `exec`
- `shell_exec`
- `system`
- `passthru`
- `proc_open`
- `popen`

Set:

```ini
disable_functions = exec,shell_exec,system,passthru,proc_open,popen
```

The app now enforces this by default at bootstrap via `LP_RUNTIME_LOCKDOWN=1`.
To bypass temporarily in local development only, set `LP_RUNTIME_LOCKDOWN=0` in `.env`.

## Install dependencies

From `web/`:

```bash
composer install
```

## Run locally
From the `web/` folder:

```bash
php -S localhost:8000 -t public public/router.php
```

Then open:
- http://localhost:8000/

## First-Run Installer

On a fresh deploy, the app redirects to:
- `/install`

Installer flow includes:
- Site bootstrap data (site name, author, domain, initial template)
- First administrator creation
- DB server connection test (MySQL/MariaDB)
- Schema existence check
- Optional schema creation if missing
- Table creation (users/roles/permissions + mailing integrations)
- Site initialization in **Coming Soon** mode
- Installer lock file creation at `storage/install.lock`

Installer output:
- Shows step-by-step progress logs and result screen (`/install/complete`).

To re-run installer:
- Remove `storage/install.lock` manually.

Admin UI:
- http://localhost:8000/admin/login

Admin credentials come from `.env`:
- `ADMIN_USER`
- `ADMIN_PASSWORD_HASH`

## Customize for a new author
Edit:
- `storage/site.json`
- `storage/content/*.json`

Useful keys in `storage/site.json`:
- `featured_book_slugs` (controls which books appear on the home page)
- `plugins` (turn sections on/off without breaking the theme)

To “roll out” a new site, copy the `web/` folder and adjust those JSON files.

## Pages
- `/` Home
- `/books` Books index
- `/books/{slug}` Book detail
- `/about` About / author bio
- `/follow` Social hub
- `/contact` Contact

## Themes
The active theme is `themes/default/`.

You can switch the active public theme from the admin dashboard. This updates `storage/site.json` (`theme_id`) and takes effect on the next request.

## Template Package Management (Admin)

The admin dashboard now supports:
- Creating a brand new template package from scratch (with starter Twig pages).
- Importing a template ZIP package with strict extension allow-list checks.
- Uploading assets into a template package with the same extension restrictions.
- Editing Twig pages in-browser.

Allowed upload/import file extensions:
- `js`, `css`, `twig`, `jpg`, `jpeg`, `gif`, `png`, `webp`, `webm`, `mp4`, `mp3`, `wav`

Security note:
- ZIP import rejects entries with path traversal (e.g. `../`).

## Starter Template Blueprints

The admin create form includes architecture-first blueprints:
- Showcase
- Bookfront
- Storyline
- Journal
- Portal
- Bibliography
- Spotlight
- Gallery
- Universe
- Monograph
- WebClassic

These define structure and composition intent; content remains data-driven.

## Twig Variables in Templates

Primary variables made available to template pages include:
- `site`
- `plugins`
- `books`
- `book`
- `featuredBooks`
- `appearances`
- `posts`
- `socialLinks`

Mailing provider objects:
- `mailingIntegrations` (enabled provider records configured in admin)

Each mailing integration object can include:
- `provider_id`
- `label`
- `action_url`
- `embed_html`
- `list_identifier`
- `enabled`

## Mailing List Systems

Admin now includes a provider catalog and per-provider config editor for common author-stack systems:
- Mailchimp
- ConvertKit
- Substack
- Beehiiv
- MailerLite
- ActiveCampaign
- AWeber
- SendFox

Integrations are saved in `storage/site.json` under:
- `newsletter.integrations`

Templates can render these objects directly for reusable embed behavior that survives template updates.

## Site Visibility Controls

Admin includes a toggle to switch between:
- **Live** (public homepage/routes visible)
- **Coming Soon** (public homepage replaced by Coming Soon page for non-admin visitors)

Additional mode:
- **Temporarily Closed** (non-admin visitors are redirected to a maintenance/closed page while authenticated admin users can still browse and manage the full site)

## Roles, Permission Keys, and Session Invalidation

Admin authentication now supports DB-backed users/roles/permissions.

Permission model:
- Every protected capability uses a permission key.
- Keys are action-oriented (for example: `templates.add`, `templates.edit`, `templates.delete`).
- Role changes are applied to users on next login.

Packaged roles currently included:
- Web Designer
- Assistant Administrator
- News Updater
- Inventory Administrator
- Analytics Viewer

Core behavior:
- Role packages can be re-synced from admin.
- Sync rewrites package permissions and invalidates sessions.
- A global “Force Logout All Users” control is available.
- Session invalidation uses a server-side session version; users must sign in again after changes.

Important:
- If a user has no effective permissions, admin login is denied.
- Super administrators receive wildcard permission `*`.

## Countdown Embeds

A reusable Countdown Embed system is included with a dedicated DB table and admin screen.

Key properties:
- Generic target linkage (`target_content_item_id`) for any publishable/schedulable item.
- Optional manual fallback URL (`target_url`).
- Configurable pre-countdown and post-countdown templates.
- Automatic swap to post-countdown template when target reaches zero.
- Optional configurable mailing invite text and CTA URL/label.

Admin route:
- `/admin/countdowns`

Public embed route:
- `/embeds/countdown/{slug}`

Template usage example in PHP theme pages:
- `<?= $__view->renderCountdownEmbed('home-launch'); ?>`

The admin editor includes a quick reference panel for these variables and helper usage patterns.

Theme structure:
- `themes/default/layouts/main.php`
- `themes/default/partials/*.php`
- `themes/default/pages/*.php`
- `themes/default/components/*.php`

