#!/bin/bash
# Get cPanel security token and test json-api
OUT=/home/john/public_html/i5365_token_results.txt
> "$OUT"

# Login
curl -sk -c /tmp/i5365c.txt https://cpanel.linlab.dk:2083/login/ -o /dev/null 2>&1
curl -sk -b /tmp/i5365c.txt -c /tmp/i5365c.txt \
  -d 'user=john&pass=Ip7*sunfO0#P5xxg' \
  https://cpanel.linlab.dk:2083/login/ -o /dev/null 2>&1

echo "=== Get cPanel index page to extract token ===" >> "$OUT"
INDEX=$(curl -sk -b /tmp/i5365c.txt 'https://cpanel.linlab.dk:2083/' 2>&1)
# Look for token in the page
echo "$INDEX" | grep -o 'cpanelapi_token[^"]*"[^"]*"' | head -3 >> "$OUT"
echo "$INDEX" | grep -o 'token[^"]*"[^"]*"' | head -5 >> "$OUT"
echo "$INDEX" | grep -o 'security_token[^"]*"[^"]*"' | head -3 >> "$OUT"
echo "$INDEX" | grep -o 'api_token[^"]*"[^"]*"' | head -3 >> "$OUT"
# Also look for the token in JS variables
echo "$INDEX" | grep -o 'var.*token.*=' | head -5 >> "$OUT"
echo "$INDEX" | grep -o 'token.*=.*"' | head -5 >> "$OUT"

echo "" >> "$OUT"
echo "=== Try /execute/ with full error ===" >> "$OUT"
EXEC_RESP=$(curl -sk -b /tmp/i5365c.txt 'https://cpanel.linlab.dk:2083/execute/uapi/cron/list_cron_jobs?u=root' 2>&1)
echo "$EXEC_RESP" | grep -i 'error\|denied\|illegal\|function\|token\|access' | head -10 >> "$OUT"
echo "--- Full /execute/ response (first 500 chars) ---" >> "$OUT"
echo "$EXEC_RESP" | head -c 500 >> "$OUT"

echo "" >> "$OUT"
echo "=== Try /execute/ without u=root ===" >> "$OUT"
EXEC_RESP2=$(curl -sk -b /tmp/i5365c.txt 'https://cpanel.linlab.dk:2083/execute/uapi/cron/list_cron_jobs' 2>&1)
echo "$EXEC_RESP2" | grep -i 'error\|denied\|illegal\|function\|token\|access' | head -10 >> "$OUT"
echo "--- Full /execute/ no-u response (first 500 chars) ---" >> "$OUT"
echo "$EXEC_RESP2" | head -c 500 >> "$OUT"

echo "" >> "$OUT"
echo "=== Try /json-api/ with cpanelapi_token from session ===" >> "$OUT"
# Try to get token via the json-api itself
TOKEN_RESP=$(curl -sk -b /tmp/i5365c.txt 'https://cpanel.linlab.dk:2083/json-api/cpanel/...?cpanel_jsonapi_module=CPANEL' 2>&1)
echo "Token probe: $TOKEN_RESP" | head -c 300 >> "$OUT"

echo "" >> "$OUT"
echo "=== Try /json-api/ with token in header ===" >> "$OUT"
# cPanel sometimes accepts the token as a header
curl -sk -b /tmp/i5365c.txt -H 'X-CPANEL-API-TOKEN: test' 'https://cpanel.linlab.dk:2083/json-api/cpanel/cron?cpanel_jsonapi_module=cron&u=root' 2>&1 | head -c 300 >> "$OUT"

echo "" >> "$OUT"
echo "=== Check if /execute/ works for a simple call ===" >> "$OUT"
# Try a simple execute call that should work
SIMPLE=$(curl -sk -b /tmp/i5365c.txt 'https://cpanel.linlab.dk:2083/execute/api1/cpmodlogs/list_logs' 2>&1)
echo "$SIMPLE" | head -c 500 >> "$OUT"

echo "" >> "$OUT"
echo "=== Try /json-api/ with session token from login ===" >> "$OUT"
# The login response might contain a token
LOGIN2=$(curl -sk -c /tmp/i5365d.txt https://cpanel.linlab.dk:2083/login/ -o /dev/null 2>&1)
LOGIN3=$(curl -sk -b /tmp/i5365d.txt -c /tmp/i5365d.txt \
  -d 'user=john&pass=Ip7*sunfO0#P5xxg' \
  https://cpanel.linlab.dk:2083/login/ 2>&1)
echo "Login JSON: $LOGIN3" | head -c 500 >> "$OUT"

echo "" >> "$OUT"
echo "=== DONE ===" >> "$OUT"
echo "DONE"
