# LMCC YouTube Sermon Archive — Rebuilt Module

This replaces the old YouTube add-on with a module built against the
**actual** LMCC COMP codebase (audited directly from the uploaded files),
not the generic flat-file site the old add-on assumed.

## 1. Files in this package

```
LifeMissionCMS/
  config/
    youtube_config.php     NEW - non-secret settings (channel handle, cache/paging)
    youtube_secrets.php    NEW - API key ONLY (edit this before anything works)
  youtube/
    YouTubeClient.php       NEW - YouTube Data API v3 wrapper
    YouTubeSync.php         NEW - discovery + sync + upsert logic
  includes/
    youtube_helpers.php     NEW - read-only DB helpers (used by public + admin pages)
    sidebar.php             MODIFIED - added a "Sermons" admin nav link (system_admin/
                             church_admin/general_overseer only). Nothing else changed.
  admin/
    youtube.php             NEW - admin dashboard (uses your real header/sidebar/
                             footer/auth/CSRF-per-page pattern/audit_log)
  cron/
    youtube_sync.php        NEW - CLI-safe sync entry point
  database/
    migration_youtube_module.sql   NEW - creates 3 tables, touches nothing else
  sermons.php                NEW - public sermon archive (no login required)
  sermon.php                 NEW - public single-sermon page
  assets/css/sermons.css     NEW
  assets/js/sermons.js       NEW
```

Nothing else in your LifeMissionCMS installation was modified. The only
existing file touched is `includes/sidebar.php`, and only to add one
gated `<li>` link — no existing lines were removed or altered.

## 2. Deploy via cPanel File Manager (Android-friendly)

1. Open **File Manager** → navigate to the folder that contains your live
   `LifeMissionCMS` directory.
2. Upload this zip anywhere temporary (e.g. `public_html/_upload/`), then
   use File Manager's **Extract** action on it.
3. Drag/move the extracted `LifeMissionCMS/config/youtube_config.php`,
   `youtube_secrets.php`, the whole `youtube/` folder, `includes/
   youtube_helpers.php`, `admin/youtube.php`, `cron/youtube_sync.php`,
   `database/migration_youtube_module.sql`, `sermons.php`, `sermon.php`,
   and `assets/css/sermons.css` + `assets/js/sermons.js` into your real
   `LifeMissionCMS` folder, matching the paths above exactly.
4. For `includes/sidebar.php`: don't just overwrite it blindly — open the
   one in this package and copy only the new `<!-- Sermons (YouTube
   Archive) -->` block into your live `includes/sidebar.php`, right
   before the `<!-- Communication Dropdown -->` comment. (It's a ~6-line
   block — safer to paste than overwrite, in case your live sidebar has
   moved on since this audit.)
5. Delete the temporary `_upload` folder once done.

## 3. Google Cloud setup

1. console.cloud.google.com → **APIs & Services → Library** → enable
   **YouTube Data API v3**.
2. **APIs & Services → Credentials → Create Credentials → API key.**
3. Restrict it: **API restriction → YouTube Data API v3 only.** (This key
   is only ever called from your server via cURL, never from a browser,
   so there's no referrer to restrict to.)
4. Edit `LifeMissionCMS/config/youtube_secrets.php` on the server and
   paste the key in place of the placeholder.

## 4. Database migration

phpMyAdmin → your LMCC database → **SQL** tab → paste the contents of
`database/migration_youtube_module.sql` → **Go**.

Creates `youtube_channel`, `youtube_videos`, `youtube_sync_log`. Nothing
existing is touched. Safe to re-run.

## 5. Cron job (cPanel → Cron Jobs)

First confirm your PHP CLI binary: cPanel → **Select PHP Version** →
note the "PHP Binary (CLI)" path.

```
*/30 * * * * /usr/local/bin/php83 /home/ACCOUNT/public_html/LifeMissionCMS/cron/youtube_sync.php >> /home/ACCOUNT/logs/youtube_sync.log 2>&1
```

Adjust `/home/ACCOUNT/...` to your real path. If your plan genuinely has
no cron access, set `cron_http_token` in `config/youtube_config.php` and
use an external "URL pinger" hitting
`https://yourdomain/LifeMissionCMS/cron/youtube_sync.php?token=YOUR_TOKEN`
instead — otherwise leave `cron_http_token` as `null` (recommended).

## 6. First run

1. Log in as `system_admin` (or `church_admin` / `general_overseer`) →
   sidebar → **Sermons**.
2. Click **Test Connection** — confirms the API key and channel resolve.
3. Click **Sync Now** — pulls the channel's uploads (up to 300 on the
   first run; run Sync again for older backfill on very large channels).
4. Visit `/LifeMissionCMS/sermons.php` — should show cached videos
   immediately, no API call involved.

## 7. Testing checklist

1. Log in to the CMS as an admin role.
2. Open sidebar → Sermons (`admin/youtube.php`).
3. Click **Test Connection** → should report the channel name.
4. Click **Sync Now** → video count should increase from 0.
5. Confirm cached videos appear in the "Cached Videos" table.
6. Open `/LifeMissionCMS/sermons.php` in a private/incognito window (no
   login) → sermons should display.
7. Click a sermon → plays in an embedded, cookie-less YouTube player.
8. Use the search box on `sermons.php` → results should filter.
9. Load `sermons.php` on a phone → grid should collapse to one column,
   featured sermon should stack.
10. Temporarily rename `config/youtube_secrets.php` (or blank the key)
    and click **Sync Now** → dashboard should show a clear error, while
    `/LifeMissionCMS/sermons.php` keeps showing the last cached videos.

## 8. Security confirmation

- API key lives only in `config/youtube_secrets.php`, read only by
  `YouTubeClient`, never echoed, never in HTML/JS, redacted out of any
  error message before it's logged or displayed.
- Database credentials are never duplicated — every new file reuses your
  existing `config/db_config.php` connection.
- `sermons.php`/`sermon.php` never call the YouTube API — MySQL reads
  only. The API is called exclusively from `admin/youtube.php` (manual
  actions) and `cron/youtube_sync.php` (scheduled).
- `admin/youtube.php` is gated by session role check
  (`system_admin`/`church_admin`/`general_overseer`) and every state-
  changing action requires a per-page CSRF token (same pattern as your
  existing `setup_2fa.php`/`verify_2fa.php`), verified with
  `hash_equals()`.
- Every admin action (test connection, sync, clear cache, feature/hide a
  video) is written to your existing tamper-evident `audit_logs` table
  via `audit_log()`.
- `cron/youtube_sync.php` refuses to run over HTTP unless a pre-shared
  token is configured (off by default) — a real cPanel Cron Job (CLI)
  never needs one and is not affected.
- No raw exceptions, stack traces, DB errors, or file paths are ever
  shown to a visitor; failures are logged server-side via `error_log()`
  and (for sync attempts) `youtube_sync_log`, with only a friendly
  message surfaced.
