<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

// PHP Settings Container
require("ext/php-settings-container/src/SettingsContainerInterface.php");
require("ext/php-settings-container/src/SettingsContainerAbstract.php");

// Exceptions
require("ext/php-qrcode/src/QRCodeException.php");
require("ext/php-qrcode/src/Data/QRCodeDataException.php");
require("ext/php-qrcode/src/Decoder/QRCodeDecoderException.php");
require("ext/php-qrcode/src/Detector/QRCodeDetectorException.php");
require("ext/php-qrcode/src/Output/QRCodeOutputException.php");

// Common
require("ext/php-qrcode/src/Common/BitBuffer.php");
require("ext/php-qrcode/src/Common/EccLevel.php");
require("ext/php-qrcode/src/Common/ECICharset.php");
require("ext/php-qrcode/src/Common/GenericGFPoly.php");
require("ext/php-qrcode/src/Common/GF256.php");
require("ext/php-qrcode/src/Common/MaskPattern.php");
require("ext/php-qrcode/src/Common/Mode.php");
require("ext/php-qrcode/src/Common/ReedSolomonDecoder.php");
require("ext/php-qrcode/src/Common/ReedSolomonEncoder.php");
require("ext/php-qrcode/src/Common/Version.php");

// Data
require("ext/php-qrcode/src/Data/QRDataModeInterface.php");
require("ext/php-qrcode/src/Data/QRDataModeAbstract.php");
require("ext/php-qrcode/src/Data/AlphaNum.php");
require("ext/php-qrcode/src/Data/Byte.php");
require("ext/php-qrcode/src/Data/ECI.php");
require("ext/php-qrcode/src/Data/Hanzi.php");
require("ext/php-qrcode/src/Data/Kanji.php");
require("ext/php-qrcode/src/Data/Number.php");
require("ext/php-qrcode/src/Data/QRData.php");
require("ext/php-qrcode/src/Data/QRMatrix.php");

// Decoder
require("ext/php-qrcode/src/Decoder/LuminanceSourceInterface.php");
require("ext/php-qrcode/src/Decoder/LuminanceSourceAbstract.php");
require("ext/php-qrcode/src/Decoder/Binarizer.php");
require("ext/php-qrcode/src/Decoder/BitMatrix.php");
require("ext/php-qrcode/src/Decoder/Decoder.php");
require("ext/php-qrcode/src/Decoder/DecoderResult.php");
require("ext/php-qrcode/src/Decoder/GDLuminanceSource.php");
require("ext/php-qrcode/src/Decoder/IMagickLuminanceSource.php");

// Detector
require("ext/php-qrcode/src/Detector/ResultPoint.php");
require("ext/php-qrcode/src/Detector/AlignmentPattern.php");
require("ext/php-qrcode/src/Detector/AlignmentPatternFinder.php");
require("ext/php-qrcode/src/Detector/Detector.php");
require("ext/php-qrcode/src/Detector/FinderPattern.php");
require("ext/php-qrcode/src/Detector/FinderPatternFinder.php");
require("ext/php-qrcode/src/Detector/GridSampler.php");
require("ext/php-qrcode/src/Detector/PerspectiveTransform.php");

// Output
require("ext/php-qrcode/src/Output/QROutputInterface.php");
require("ext/php-qrcode/src/Output/QROutputAbstract.php");
require("ext/php-qrcode/src/Output/QREps.php");
require("ext/php-qrcode/src/Output/QRFpdf.php");
require("ext/php-qrcode/src/Output/QRGdImage.php");
require("ext/php-qrcode/src/Output/QRImage.php");
require("ext/php-qrcode/src/Output/QRImagick.php");
require("ext/php-qrcode/src/Output/QRMarkup.php");
require("ext/php-qrcode/src/Output/QRMarkupHTML.php");
require("ext/php-qrcode/src/Output/QRMarkupSVG.php");
require("ext/php-qrcode/src/Output/QRString.php");

// Root
require("ext/php-qrcode/src/QROptionsTrait.php");
require("ext/php-qrcode/src/QROptions.php");
require("ext/php-qrcode/src/QRCode.php");

use \chillerlan\QRCode\{QRCode, QROptions};

$data = @$_SERVER['PATH_INFO'];

if (""!=$data && false===strpos($data, "..")) {
  http_response_code(200);
  header('Content-type: image/svg+xml');
  
  if (file_exists("qr".$data.".svg")) {
    $fp = fopen("qr".$data.".svg", 'rb');
    fpassthru($fp);
  } else {
    $posLastSlash = strrpos(@$_SERVER['PATH_INFO'], "/");
    $path = "qr".substr(@$_SERVER['PATH_INFO'], 0, $posLastSlash+1);
    if (!file_exists($path)) {
      mkdir($path, 0770, true);
    }
    
    $gzip = true;
  
    $options = new QROptions([
    	'outputType'   => QRCode::OUTPUT_MARKUP_SVG,
    	'imageBase64'  => false,
    	'eccLevel'     => QRCode::ECC_L,
    	'cssClass'     => 'my-css-class',
    	'svgOpacity'   => 1.0,
    	'addQuietzone' => false,
    	'svgDefs'      => 
    		'<linearGradient id="g2">
    			<stop offset="0%" stop-color="#39F" />
    			<stop offset="100%" stop-color="#F3F" />
    		</linearGradient>
    		<linearGradient id="g1">
    			<stop offset="0%" stop-color="#F3F" />
    			<stop offset="100%" stop-color="#39F" />
    		</linearGradient>
    		<style>rect{shape-rendering:crispEdges}</style>',
    ]);
    
    
    
    $qrcode = (new QRCode($options))->render($data, "qr".$data.".svg");
    
    /*if($gzip === true){
    	header('Vary: Accept-Encoding');
    	header('Content-Encoding: gzip');
    	$qrcode = gzencode($qrcode ,9);
    }*/
    echo $qrcode;
  }
} else {
  http_response_code(400);
}

