<?php
// inline_svg.php
// RJM Programming
// January, 2022
// Thanks to https://stackoverflow.com/questions/1623242/problem-with-dynamic-svg-image-generation

$asbkg=false;
$whati='image';

if (isset($_GET['asbackground']) || isset($_POST['asbackground'])) {
 $asbkg=true;
 $whati='youllneverfindthis';
} else {
 header('Content-type: image/svg+xml');
}
 

$h=0;

$w=0;

$lastcontent='';
$textcontent='';

$innards='';
$preinnards='';


$vbox='';
$pw='100%';
$ph='100%';
$blk='';

$svgmiddle='';

foreach ($_GET as $name=>$val) {
  if (strpos(strtolower('' . $name . '~'), 'css~') !== false) {
    if ($svgmiddle == '') {
      $svgmiddle="
            <style type=\"text/css\" >
                <![CDATA[
                    ." . strtolower($name) . " { " . str_replace(';;',';',trim(str_replace('+',' ', urldecode($_GET[$name]))) . ';') . " } 
                ]]>
            </style>

      ";
    } else {
      $svgmiddle=str_replace("<![CDATA[", "<![CDATA[ 
                    ." . strtolower($name) . " { " . str_replace(';;',';',trim(str_replace('+',' ', urldecode($_GET[$name]))) . ';') . " } ", $svgmiddle);
    } 
  }
}

foreach ($_POST as $name=>$val) {
  if (strpos(strtolower('' . $name . '~'), 'css~') !== false) {
    if ($svgmiddle == '') {
      $svgmiddle="
            <style type=\"text/css\" >
                <![CDATA[
                    ." . strtolower($name) . " { " . str_replace(';;',';',trim(str_replace('+',' ', urldecode($_POST[$name]))) . ';') . " } 
                ]]>
            </style>

      ";
    } else {
      $svgmiddle=str_replace("<![CDATA[", "<![CDATA[ 
                    ." . strtolower($name) . " { " . str_replace(';;',';',trim(str_replace('+',' ', urldecode($_POST[$name]))) . ';') . " } 
                    ", $svgmiddle);
    } 
  }
}

  //file_put_contents('xz.xz', $_SERVER['QUERY_STRING']);
//if ($svgmiddle != '') {
//  file_put_contents('xzxzxz.xzxzxz', $svgmiddle);
//}

function ingp($m) { // thanks to https://pretagteam.com/question/how-do-i-convert-unicode-special-characters-to-html-entities
 global $innards, $preinnards;
 if ($innards != '') {
$entity = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) {
    $char = current($m);
    $utf = iconv('UTF-8', 'UCS-4', $char);
    //return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0"));
    return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0"));
}, $innards);
//file_put_contents('xx.x',$entity);
$preinnards=htmlentities($entity);
//file_put_contents('x.x',$entity . ' ... ' . $preinnards);
$innards='';
 } 
}

if (isset($_GET['width'])) {

  $w=str_replace('+',' ',urldecode($_GET['width']));

} else if (isset($_POST['width'])) {

  $w=str_replace('+',' ',urldecode($_POST['width']));

}

if (isset($_GET['height'])) {

  $h=str_replace('+',' ',urldecode($_GET['height']));
  if ($w != 0) { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw='100%'; $ph='100%'; } 

} else if (isset($_POST['height'])) {

  $h=str_replace('+',' ',urldecode($_POST['height']));
  if ($w != 0) { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw='100%'; $ph='100%';  } 

}


if (isset($_GET['image'])) {
  list($width, $height) = getimagesize(str_replace('+',' ',urldecode($_GET['image'])));
  $lastcontent='<image class="css imagecss" href="' . str_replace('http:','',str_replace('https:','',str_replace('+',' ',urldecode($_GET['image'])) . '" x="20" y="20" height="' . $height . 'px" width="' . $width . 'px"/>'));
  if ($h == 0) {
    $h=$height;
  } 
  if ($w == 0) {
    $w=$width;
  }
  if (isset($_GET['insvg'])) {
  $lastcontent='<image class="css imagecss" href="' . str_replace('http:','',str_replace('https:','',str_replace('+',' ',urldecode($_GET['image'])) . '" x="20" y="20" height="' . $h . 'px" width="' . $w . 'px"/>'));
  $w+=40;
  //$w=(($w + 20) / 20) * 20;
  $h+=40;
  //$h=(($h + 20) / 20) * 20;
  if ($vbox != '') { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw=$w; $ph=$h; } 
  }

} else if (isset($_POST['image'])) {
  list($width, $height) = getimagesize(str_replace('+',' ',urldecode($_POST['image'])));
  $lastcontent='<image class="css imagecss" href="' . str_replace('http:','',str_replace('https:','',str_replace('+',' ',urldecode($_GET['image'])) . '" x="20" y="20" height="' . $height . 'px" width="' . $width . 'px"/>'));
  if ($h == 0) {
    $h=$height;
  } 
  if ($w == 0) {
    $w=$width;
  }
  if (isset($_POST['insvg'])) {
  $w+=40;
  //$w=(($w + 20) / 20) * 20;
  $h+=40;
  //$h=(($h + 20) / 20) * 20;
  if ($vbox != '') { $vbox=' viewBox="0 0 ' . $w . ' ' . $h . '" '; $pw=$w; $ph=$h;  } 
  }

}

if ($pw == '' && $h != 0) { $pw=$w; }
if ($ph == '' && $h != 0) { $ph=$h; }

if (isset($_GET['text'])) {
     $txt=str_replace("\n","<br>",str_replace('&nbsp;',' ',str_replace('+', ' ', urldecode($_GET['text']))));
     $minus30=($w / 2.0) - strlen(explode('<br>', $txt)[0]) * 3.1;
     $top30=($h / 2.0) + 25 - (sizeof(explode('<br>', $txt)) * 8);
     $lines=explode('<br>', $txt);
     for ($il=0; $il<sizeof($lines); $il++) {
       $textcontent.='<text class="css textcss" x="' . $minus30 . '" y="' . $top30 . '">' . $lines[$il] . '</text>';
       $top30+=15;
     }
} else if (isset($_POST['text'])) {
     $txt=str_replace("\n","<br>",str_replace('&nbsp;',' ',str_replace('+', ' ', urldecode($_POST['text']))));
     $minus30=($w / 2.0) - strlen(explode('<br>', $txt)[0]) * 3.1;
     $top30=($h / 2.0) + 25 - (sizeof(explode('<br>', $txt)) * 8);
     $lines=explode('<br>', $txt);
     for ($il=0; $il<sizeof($lines); $il++) {
       $textcontent.='<text class="css textcss" x="' . $minus30 . '" y="' . $top30 . '">' . $lines[$il] . '</text>';
       $top30+=15;
     }
}



if (isset($_GET['insvg'])) {

  $innards=str_replace('+','',urldecode($_GET['insvg']));
  if (trim(str_replace(',','',str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$innards)))))))))))) != '') {
  ingp($innards);
  }
  if ($preinnards == '') {
  $preinnards='&amp;#' . str_replace(',',';&amp;#',$innards) . ';';
  }
  
  $svgprefix='';
  $svgsuffix='';

  if (strpos($innards, '<svg') === false) {

    if (isset($_GET['image'])) {
    $svgprefix='<svg width="' . $pw . '" height="' . $ph . '"' . $blk . ' version="1.1" xmlns="http://www.w3.org/2000/svg">';
    } else {
    $svgprefix='<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">';
    }
    

    $svgsuffix='</svg>';

    if (trim(str_replace(',','',str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$innards)))))))))))) == '') {

     for ($x=0; $x<=$w; $x+=20) {

      for ($y=0; $y<=(0 + $h); $y+=20) {

       if ($x == 0 && $y == 0) {

         $innards='<text class="css textcss" x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         if ($h > 0) { $innards.='<text class="css textcss" x="' . $w . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';  }

       } else if (isset($_GET['background']) || isset($_GET[$whati]) || $x == 0 || $y == 0 || ($x + 20) > $w || ($y + 20) > (0 + $h)) {

         $innards.='<text class="css textcss" x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         $innards.='<text class="css textcss" x="' . ($w - $x) . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';

       }

      }

     }

    }

  }

  ourecho ('<?xml version="1.0" standalone="no"?>' . html_entity_decode('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . $svgprefix . $svgmiddle . $innards . $lastcontent . $textcontent . $svgsuffix, ENT_QUOTES, "utf-8"));
} else if (isset($_POST['insvg'])) {

  $innards=str_replace('+','',urldecode($_POST['insvg']));
  if (trim(str_replace(',','',str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$innards)))))))))))) != '') {
  ingp($innards);
  }
  if ($preinnards == '') {
  $preinnards='&#' . str_replace(',',';&#',$innards) . ';';
  }

  $svgprefix='';
  $svgsuffix='';

  if (strpos($innards, '<svg') === false) {

    if (isset($_POST['image'])) {
    $svgprefix='<svg width="' . $pw . '" height="' . $ph . '"' . $blk . ' version="1.1" xmlns="http://www.w3.org/2000/svg">';
    } else {
    $svgprefix='<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">';
    }

    $svgsuffix='</svg>';

    if (trim(str_replace(',','',str_replace('0','',str_replace('1','',str_replace('2','',str_replace('3','',str_replace('4','',str_replace('5','',str_replace('6','',str_replace('7','',str_replace('8','',str_replace('9','',$innards)))))))))))) == '') {

     for ($x=0; $x<=$w; $x+=20) {

      for ($y=0; $y<=(0 + $h); $y+=20) {

       if ($x == 0 && $y == 0) {

         $innards='<text class="css textcss" x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         if ($h > 0) { $innards.='<text class="css textcss" x="' . $w . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';  }

       } else if (isset($_GET['background']) || isset($_GET[$whati]) || $x == 0 || $y == 0 || ($x + 20) > $w || ($y + 20) > (0 + $h)) {

         $innards.='<text class="css textcss" x="' . $x . '" y="' . (20 + $y) . '">' . $preinnards . '</text>';

         $innards.='<text class="css textcss" x="' . ($w - $x) . '" y="' . (20 + $h - $y) . '">' . $preinnards . '</text>';

       }

      }

     }

    }

  }

  ourecho ('<?xml version="1.0" encoding="UTF-16" standalone="no"?>' . html_entity_decode('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . $svgprefix . $svgmiddle . $innards . $lastcontent . $textcontent . $svgsuffix, ENT_QUOTES, "utf-8"));
} else if (isset($_POST['image']) || isset($_GET['image'])) {
  ourecho ('<?xml version="1.0" encoding="UTF-16" standalone="no"?>' . html_entity_decode('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg width="' . $w . '" height="' . $h . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . $svgmiddle . $lastcontent . '</svg>', ENT_QUOTES, "utf-8"));

} else {
  ourecho ('<?xml version="1.0" encoding="UTF-16" standalone="no"?>' . html_entity_decode('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">' . $svgmiddle . '<circle class="css circlecss" cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/></svg>', ENT_QUOTES, "utf-8"));
}

function ourecho($inh) {
  global $asbkg;
  if ($asbkg && (isset($_GET['midbit']) || isset($_POST['midbit']))) {
    echo '<svg' . explode('<svg', explode('</svg>', $inh)[0])[1] . '</svg>';
  } else if ($asbkg) {
    if (isset($_GET['nobase64']) || isset($_POST['nobase64'])) {
    echo "data:image/svg+xml;utf8," . ('<svg' . explode('<svg', explode('</svg>', $inh)[0])[1] . '</svg>');
    } else {
    echo "data:image/svg+xml;base64," . base64_encode('<svg' . explode('<svg', explode('</svg>', $inh)[0])[1] . '</svg>');
    }
  } else {
    echo $inh;
  }
}

?>