<?php

define('_IN_JOHNCMS', 1);
$server = $_SERVER['DOCUMENT_ROOT'];
require($server . '/system/bootstrap.php');
/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::class);

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);

/** @var Johncms\Api\EnvironmentInterface $env */
$env = $container->get(Johncms\Api\EnvironmentInterface::class);

/** @var Johncms\Api\BbcodeInterface $bbcode */
$bbcode = $container->get(Johncms\Api\BbcodeInterface::class);

/** @var Johncms\Api\ConfigInterface $config */
$config = $container->get(Johncms\Api\ConfigInterface::class);


$act = isset($_GET['act']) ? trim($_GET['act']) : '';
$headmod = 'img_encrypt';
 if (!$OpMin) {
echo $UploadProgress->Style('2'); 
}

if (!$systemUser->isValid()) {
    header('Location: /?err');
    exit;
}
 

switch ($act) {
    case 'uploads':
    $lokasi = 'img/';
    $name_file = 'encode_img';
    $me = "img/encode_img.png";
    if (isset($_POST['submit'])) {
        $handle = new upload($_FILES['my_field']);
        if ($handle->uploaded) {
            $handle->file_new_name_body = $name_file;
            $handle->allowed = array('image/jpeg', 'image/gif', 'image/png');
            $handle->file_max_size = 1024 * $config['flsz'];
            $handle->file_overwrite = true;
            $handle->image_convert = 'png';
            $handle->process($lokasi);
            if ($handle->processed){
            echo '<div class="gmenu" id="status"> Upload is done!</div>';
           $aku = base64_encode(file_get_contents($me));
            echo '<div class="gmenu" id="status"><input type="text" id="select2" value="data:image/png;base64,'.$aku.'"><span class="control-copyinput" onClick="return fieldtoclipboard.copyfield(event, \'select2\')">Copy</span></div>';
            echo '<div class="rmenu" id="status"><img src="data:image/png;base64,'.$aku.'"></div>';
             } else {
             echo '  <div class="rmenu" id="status">' . $handle->error . '</div>';
             }
             $handle-> Clean();
             } else {
             echo '<div class="rmenu" id="status">' . $handle->error . '</div>';
             }
             }
    break;
    case 'upload':
    require($server . '/system/head.php'); 
    echo '<div class="mainblok">';
echo '<div class="phdr">Images Encoder Online</div>';
    echo '<div class="gmenu"><form action="index.php?act=uploads" method="post" enctype="multipart/form-data">';
    echo 'Images<br/><input type="file" name="my_field"><br/>' .
         '<input type="submit" name="submit" value="Upload">' .
          '</div>';
          if (!$OpMin) { 
             echo $UploadProgress->Jquery();
             echo $UploadProgress->Show('1');
             echo $UploadProgress->Js();
          }
         $Siz = 1024 * $config['flsz'];
         echo '<div class="topmenu">*Type: JPEG, GIF, PNG<br/>*Size: '.$Siz.' KB</div>';
         echo '<div class="phdrblack"><a href="index.php"><b>Back</b></a></div>';
         echo '</div>';
    require($server . '/system/end.php'); 
    break;
    default:
    require($server . '/system/head.php'); 
    echo '<div class="mainblok">';
echo '<div class="phdr">Images Encoder Online</div>';
    $Siz = 1024 * $config['flsz'];
    echo '<div class="gmenu">This modules can encode image to base64 to put on css or another program so if you want click <a href="index.php?act=upload">here to upload image</a> to encode base64, image size only alowed by '.$Siz.' to upload</div>';
    echo '<div class="phdrblack"><a href="index.php"><b>Back</b></a></div>';
    echo '</div>';
    require($server . '/system/end.php'); 
    break;
}