#!/usr/bin/env python3
import requests
import time
import io

PROXY = {
    'http': 'http://b04dd480860a80d0:VJvorQIeLmBSK1G9@res.proxy-seller.com:10004',
    'https': 'http://b04dd480860a80d0:VJvorQIeLmBSK1G9@res.proxy-seller.com:10004'
}

TARGET = "https://pohon.disperkim.semarangkota.go.id/main/laporan_input"

# Create minimal valid PNG (no PHP)
def create_valid_png():
    png_header = b'\x89PNG\r\n\x1a\n'
    ihdr = b'\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde'
    idat = b'\x00\x00\x00\x0cIDAT\x08\xd7c\xf8\xff\xff?\x00\x05\xfe\x02\xfeT\xfe\x00'  
    iend = b'\x00\x00\x00\x00IEND\xaeB`\x82'
    return png_header + ihdr + idat + iend

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
}

session = requests.Session()

# Get the main page first to get cookies
print("[*] Getting main page first...")
resp = session.get("https://pohon.disperkim.semarangkota.go.id/main", headers=headers, proxies=PROXY, verify=False, timeout=30)
print(f"[*] Main page status: {resp.status_code}")
print(f"[*] Cookies: {session.cookies.get_dict()}")

time.sleep(2)

# Now try upload with valid PNG
print("\n[*] Testing upload with valid PNG...")
png_content = create_valid_png()
print(f"[*] PNG size: {len(png_content)} bytes")

files = {
    'userfile': ('test.png', io.BytesIO(png_content), 'image/png')
}

data = {
    'pelapor_nama': 'Test User',
    'pelapor_alamat': 'Test Alamat',
    'pelapor_phone': '081234567890',
    'pohon_lokasi': 'Test Lokasi',
    'pohon_keterangan': 'Test Keterangan',
}

try:
    resp = session.post(TARGET, files=files, data=data, headers=headers, 
                       proxies=PROXY, verify=False, timeout=60, allow_redirects=True)
    print(f"[*] Status: {resp.status_code}")
    print(f"[*] Final URL: {resp.url}")
    # Check for flash message
    if 'Berhasil' in resp.text or 'success' in resp.text.lower():
        print("[+] Upload appears successful!")
    if 'error' in resp.text.lower() or 'gagal' in resp.text.lower():
        print("[-] Upload failed")
    # Show relevant part of response
    if 'flashdata' in resp.text:
        print(f"[*] Flashdata found in response")
except Exception as e:
    print(f"[-] Error: {e}")

