<?php
/*
 * Super Simple QR Code Wrapper untuk Google Chart API
 * ---------------------------------------------------
 * Tidak pakai GD, tidak pakai matrix.
 * Hanya:
 *   - Bangun URL QR
 *   - Download PNG pakai file_get_contents atau cURL
 *   - Simpan ke file (atau echo langsung)
 *
 * Cara pakai:
 *   QRcode::png($text, $outfile, $level, $size, $margin);
 */

class QRcode {

    public static function png($text, $outfile = false, $level = 'L', $size = 4, $margin = 4) {
        // Sanitize & encode teks
        $data = rawurlencode($text);

        // Tentukan ukuran pixel berdasarkan $size
        // (ini bukan skala super presisi, tapi cukup untuk web & print sederhana)
        $px = 200; // default
        if ($size >= 10) {
            $px = 800;
        } elseif ($size >= 6) {
            $px = 400;
        } elseif ($size >= 5) {
            $px = 300;
        }

        // level error correction: L, M, Q, H
        $level = strtoupper($level);
        if (!in_array($level, ['L','M','Q','H'])) {
            $level = 'L';
        }

        // margin dalam modul QR (hampir sama dengan parameter API "chld")
        // contoh: chld=L|4
        $margin = (int)$margin;
        if ($margin < 0) $margin = 0;

        $url = "https://chart.googleapis.com/chart?cht=qr&chs={$px}x{$px}&chl={$data}&choe=UTF-8&chld={$level}|{$margin}";

        // Ambil data PNG
        $raw = self::fetchUrl($url);
        if ($raw === false || strlen($raw) === 0) {
            // gagal ambil QR, jangan fatal error, cukup return
            return;
        }

        if ($outfile) {
            // Simpan ke file
            @file_put_contents($outfile, $raw);
        } else {
            // Tampilkan langsung
            header('Content-Type: image/png');
            echo $raw;
        }
    }

    /**
     * fetchUrl: coba file_get_contents dulu, kalau gagal pakai cURL.
     */
    private static function fetchUrl($url) {
        // 1) file_get_contents jika allow_url_fopen ON
        if (ini_get('allow_url_fopen')) {
            $ctx = stream_context_create([
                'http' => [
                    'timeout' => 10,
                ],
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ]);
            $raw = @file_get_contents($url, false, $ctx);
            if ($raw !== false && strlen($raw) > 0) {
                return $raw;
            }
        }

        // 2) fallback ke cURL
        if (function_exists('curl_version')) {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            $raw = curl_exec($ch);
            curl_close($ch);

            if ($raw !== false && strlen($raw) > 0) {
                return $raw;
            }
        }

        return false;
    }
}
