06

สำหรับ System Owner

การตั้งค่าระบบ + Cron + APIs + Migrations

⚙️ Role: system_owner

6.1 ภาพรวมหน้าที่ของ System Owner

ไฟล์สำคัญที่ควรรู้

ไฟล์หน้าที่
app_config.phpconfig public (อยู่ใน webroot)
app_secret.phpsecrets — นอก webroot
db.phpเชื่อมต่อ MySQL
smtp_config.phpSMTP server credentials
line_config.phpLINE Bot channel access token
lib/bootstrap.phpโหลด wide ทุก session

6.2 ตั้งค่า SMTP (Email)

ระบบใช้ PHPMailer ส่ง email สำหรับ: OTP, reset password, error alerts, broadcast

ตั้งค่าใน smtp_config.php

<?php
return [
  'host' => 'smtp.gmail.com',
  'port' => 587,
  'username' => '[email protected]',
  'password' => 'xxxx xxxx xxxx xxxx', // App password
  'encryption' => 'tls',
  'from_email' => '[email protected]',
  'from_name' => 'ThaibadTournaments',
];
⚠️ Gmail App Password

หากใช้ Gmail ต้อง enable 2FA ก่อน → สร้าง App Password ที่ myaccount.google.com/apppasswords

ทดสอบ

เปิด test_error_monitor.php → จะส่ง test email ไป admin email

6.3 ตั้งค่า LINE Bot

อ่านเอกสาร: Line_Admin_Setup.md, Line_Noti.md, Line_User_Guide.md

ขั้นตอน

  1. สร้าง LINE Official Account ที่ developers.line.biz
  2. เปิด Messaging API
  3. คัดลอก: Channel Access Token + Channel Secret
  4. กรอกใน line_config.php:
    return [
      'channel_access_token' => 'xxxxx...',
      'channel_secret' => 'xxxxx...',
      'liff_id' => 'xxxxx-xxxxx',
    ];
  5. ตั้ง Webhook URL: https://thaibadtournaments.com/badminton/line_webhook.php
  6. เปิด "Use webhook" + ปิด "Auto-reply messages"
  7. เพิ่มเพื่อนเอง → ทดสอบ "สถานะ"

LIFF (LINE Front-end Framework)

สำหรับ login ผ่าน LINE ในเว็บ — สร้าง LIFF app type=Full → URL ชี้ไปที่ auth_line.php

6.4 ตั้ง Cron Jobs

ระบบมี 2 cron jobs หลักที่ต้องตั้งใน server

1. Match Reminder (ทุก 2-5 นาที)

*/3 * * * * curl -s -H "X-Cron-Secret: YOUR_SECRET" \
  https://thaibadtournaments.com/badminton/cron_notify_matches.php \
  >> /var/log/thaibad_cron.log 2>&1

2. Morning Broadcast (06:00 ทุกวัน)

0 6 * * * curl -s -H "X-Cron-Secret: YOUR_SECRET" \
  "https://thaibadtournaments.com/badminton/admin_morning_broadcast.php?auto=1&date=TODAY"

3. Cleanup เก่า (รายสัปดาห์)

0 3 * * 0 php /path/to/cleanup_old_logs.php
🔒 X-Cron-Secret

ตั้ง CRON_SECRET ใน app_secret.php → ใช้เป็น header X-Cron-Secret เพื่อกัน cron URL ถูกเรียกจากภายนอก

6.5 ตั้งค่า SlipOK API

สำหรับการตรวจสลิปอัตโนมัติ

ขั้นตอน

  1. สมัครที่ slipok.com → เลือกแพ็กเกจ
  2. เพิ่มสาขา (Branch) → ได้ branch_id
  3. สร้าง api_key
  4. ตั้งบัญชีรับโอนของแต่ละ tournament ใน edit_match.php → ฟิลด์:
    • slipok_branch_id
    • slipok_api_key
    • payment_account_name — ต้องตรงกับใน SlipOK

API Call

lib/slipok.php ส่ง POST ไปยัง https://api.slipok.com/api/line/apikey/{branch_id} พร้อมรูปสลิป

Response Codes

6.6 ตั้งค่า Gemini TTS API

ขั้นตอน

  1. ไปที่ ai.google.dev → สร้าง API Key
  2. เก็บใน app_secret.php:
    'GEMINI_API_KEY' => 'AIzaSy...',
  3. Default model: gemini-2.5-flash-preview-tts + voice Kore (Thai)

Quota

การจัดการ Cache

ระบบเก็บไฟล์เสียงใน /uploads/tts_cache/SHA256(text+voice).wav

6.7 Database Migrations

เมื่ออัปเดตระบบ — รัน migration scripts ใน root ของ project

17 Migration Scripts

Scriptเพิ่มอะไร
migrate_2fa_tables.php2FA + backup codes
migrate_payment_system.phppayment_mode, slipok, slip status
migrate_clubs.phpclubs, members, banner_designs
migrate_lockout.phpfailed_login_attempts, lockout_until
migrate_add_gender.phpusers.gender (M/F)
migrate_age_categories.phpavailable_age_categories JSON
migrate_identity_verification.phpidentity_verified + id_card_image
migrate_errors_log.phperrors_log table
migrate_line_chat_states.phpstate machine ของ webhook
migrate_line_link_pins.phpPIN 6 หลักผูก LINE
migrate_notify_flags.phpnotify_* flags ต่อ tournament
migrate_calendar_province.phpfilter จังหวัด
migrate_calendar_map_url.phpGoogle Maps URL
migrate_match_tab_visibility.phptab_*_enabled flags
migrate_match_permissions_role.phpper-tournament role
migrate_tts_cache.phptts_quota_daily + cache table
migrate_cleanup_legacy_roles.phpcleanup เก่า ๆ
⚠️ Backup ก่อนรัน Migration

ใช้ mysqldump backup ก่อนรันทุกครั้ง — ไม่มี rollback automatic

6.8 ระบบ Real-time Co-work

อ่านเอกสาร Real_time_Co_work.md

Editing Presence

ตาราง editing_presence:

API ที่เกี่ยวข้อง

Polling Strategy

Frontend poll ทุก 2 วินาที — ไม่ใช้ WebSocket (เพื่อง่ายในการ host บน shared hosting)