<?php
// Image Plus Text PDF  ... RJM Programming ... August 2022
require('MarkItUp/fpdf17/fpdf.php');
//if (1 == 1) {
//echo "<html><body><p>gfdg nhffd</p></body></html>";
//exit;
//} else 

$user_agent = $_SERVER['HTTP_USER_AGENT']; 

function server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    if (1 == 2) {
    if(preg_match('/(chromium)[ \/]([\w.]+)/', $ua))
            $rma = '000000'.$rma;
    elseif(preg_match('/(chrome)[ \/]([\w.]+)/', $ua))
            $rma = '00000'.$rma;
    elseif(preg_match('/(safari)[ \/]([\w.]+)/', $ua))
            $rma = '0000'.$rma;
    elseif(preg_match('/(opera)[ \/]([\w.]+)/', $ua))
            $rma = '000'.$rma;
    elseif(preg_match('/(msie)[ \/]([\w.]+)/', $ua))
            $rma = '00'.$rma;
    elseif(preg_match('/(mozilla)[ \/]([\w.]+)/', $ua))
            $rma = '0'.$rma;
    }
    return "_" . str_replace(".", "_", str_replace(":", "_", $rma));
}

if (isset($_GET['pdffile'])) {
   $val=str_replace('+',' ',urldecode($_GET['pdffile']));
   if (strpos($val, ".") !== false) {
    if (strtolower(explode(".", basename($val))[-1 + sizeof(explode(".", basename($val)))]) == 'pdf') {
     $ourfname=str_replace('.pdf','',str_replace('.PDF','',basename($val)));
     if (1 == 1) {
   if (strpos( $user_agent, 'Safari') !== false && strpos( $user_agent, 'Chrome') === false && strpos( $user_agent, 'OPR') === false && strpos( $user_agent, 'Firefox') === false && strpos( $user_agent, 'Opera') === false) {
       $wobit="window.open('/" . $ourfname . ".pdf','_blank');";
       echo '<html><body onclick="' . $wobit . '" title="Click for PDF and/or scroll down to Sharing section" style="background:URL(' . 'data:image/jpeg;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.jpg')) . ');background-repeat:no-repeat;background-size:contain;"><img title="Click for PDF and/or scroll down to Sharing section" style=visibility:hidden; onclick="' . $wobit . '" src="' . 'data:image/jpeg;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.jpg')) . '"></img></body></html>';
   } else {
       echo '<html><body><div style="overflow:auto;-webkit-overflow-scrolling:touch;height:100%;"><object style="width:100%;height:900px;" type="application/pdf" data="' . 'data:application/pdf;base64,' . base64_encode(file_get_contents('./' . basename($val))) . '">' . "\n" . '<embed style="width:100%;height:900px;" type="application/pdf" src="' .'data:application/pdf;base64,' . base64_encode(file_get_contents('./' . basename($val))) . '"></embed></object></div></body></html>'; 
   }
     } else {
       header('Content-Type: application/pdf');
       echo file_get_contents('./' . basename($val));
     }
    } else {
     header('Content-Type: image/' . strtolower(explode(".", basename($val))[1]));
     echo file_get_contents('./' . basename($val));
    }
   }
   exit;   
}

$ourfname="filename" . server_remote_addr();
$dru="http://" . $_SERVER['SERVER_NAME'] . "" . str_replace("~","",str_replace(":443~","",str_replace(":80~","",(":" . $_SERVER['SERVER_PORT'] . "~")))) . "/";
$linkemailshare=$dru . $ourfname . ".pdf#Your Image and Text PDF#Via RJM Programming";
$sharehtmlbit="<br><br><iframe src='" . str_replace('http:','',str_replace("rjmprogramming.com.au/","rjmprogramming.com.au/HTMLCSS/",$dru)) . "web_share_api_test.html?suggestion=" . urlencode($linkemailshare) . "' style='width:100%;height:900px;'></iframe>";
$safarisharehtmlbit='open "' . str_replace("rjmprogramming.com.au/","rjmprogramming.com.au/HTMLCSS/",$dru) . 'web_share_api_test.html?suggestion=' . urlencode($linkemailshare) . '"';

$furl=((isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . str_replace("HTTP:","http:",$_SERVER[REQUEST_URI]));
$dpath=str_replace("$#$","//", explode("/",str_replace("//","$#$",$furl))[0] ) . "/";
$frest=explode(((isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . "/"),   ((isset($_SERVER['HTTPS']) ? "https" : "http") . "://" . $_SERVER[HTTP_HOST] . str_replace("HTTP:","http:",$_SERVER[REQUEST_URI]))   )[1];
if (strpos(($frest . '*'), '=*') === false && strpos($frest, "=") !== false) {
  $frest=explode("=", $frest)[0] . "=";
}
//echo $furl . " ... " . $dpath . " ... " . $frest;
//exit;
$sincebefore="";
$prefix="";
$notprefix="";
for ($i=0; $i<(-1 + sizeof(explode("/",$frest))); $i++) {
  $notprefix=explode($frest,$furl)[0];
  $prefix.="../";
}
if ($prefix == "") $notprefix="";
$first=true;
$outputto="";
$outputsofar="";
$outputpdf=null;
$top=1;
$title="";
$wastitle="";
$istitle="";
$cpn=1;
$emailto="";
$pdfis="";
$outputpdf_hgt=100.0;
$lh=6;
$loffset=0;
$fpdfs=",Arial,Courier,Helvetica,Symbol,Times,ZapfDingbats,";

$scale=floatval(1.0);
$scalep=floatval(100.0);

if (isset($_POST['pdfimageplustext'])) {
   $w=0;
   $h=0;
   $outputpdf = new FPDF('P','pt');
   $outputpdf->SetTopMargin(0.0);
   //$outputpdf->AddPage('L');
   if (isset($_POST['pdfimage'])) {
    if (strpos(urldecode($_POST['pdfimage']), "data:image/") !== false && strpos(urldecode($_POST['pdfimage']), ";base64,") !== false) {
    $fname='./' . $ourfname . '.' . strtolower(explode(";", explode("data:image/", urldecode($_POST['pdfimage']))[1])[0]);
    file_put_contents($fname, base64_encode(explode(";base64,", urldecode($_POST['pdfimage']))[1]));
    list($w, $h) = getimagesize($fname);
    if ($w > $h) {
    $outputpdf->AddPage('L', 'A4');
    if ($w > 830) {
    $scale=floatval(830.0 / $w);
    $scalep=floatval($scale * 100.0);
    //$outputpdf->_out('q');
    //Scale(floatval($scalep), floatval($scalep));
    $newh=($h * (830.0 / $w));
    $outputpdf->Image($fname, 0, 0, 830, floatval($newh)); //, 33.78);
    //$outputpdf->_out('Q');
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    } else {
    $outputpdf->AddPage('P', 'A4');
    if ($h > 830) {
    $scale=floatval(830.0 / $h);
    $scalep=floatval($scale * 100.0);
    //Scale(floatval($scalep), floatval($scalep));
    $neww=($w * (830.0 / $h));
    $outputpdf->Image($fname, 0, 0, floatval($neww), 830); //, 33.78);
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    }
    } else if (strpos(urldecode($_POST['pdfimage']), "rjmprogramming.com.au/") !== false) {
    $fname="./" . explode("rjmprogramming.com.au/", urldecode($_POST['pdfimage']))[1];
    //$outputpdf->Image("./" . explode("rjmprogramming.com.au/", urldecode($_POST['pdfimage']))[1], 0, 0); //, 33.78);
    list($w, $h) = getimagesize($fname);
    if ($w > $h) {
    $outputpdf->AddPage('L', 'A4');
    if ($w > 830) {
    $scale=floatval(830.0 / $w);
    $scalep=floatval($scale * 100.0);
    //$outputpdf->_out('q');
    //Scale(floatval($scalep), floatval($scalep));
    $newh=($h * (830.0 / $w));
    $outputpdf->Image($fname, 0, 0, 830, floatval($newh)); //, 33.78);
    //$outputpdf->_out('Q');
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    } else {
    $outputpdf->AddPage('P', 'A4');
    if ($h > 830) {
    $scale=floatval(830.0 / $h);
    $scalep=floatval($scale * 100.0);
    //Scale(floatval($scalep), floatval($scalep));
    $neww=($w * (830.0 / $h));
    $outputpdf->Image($fname, 0, 0, floatval($neww), 830); //, 33.78);
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    }
    } else {
    $fname=urldecode($_POST['pdfimage']);
    //$outputpdf->Image(urldecode($_POST['pdfimage']), 0, 0); //, 33.78);
    list($w, $h) = getimagesize($fname);
    if ($w > $h) {
    $outputpdf->AddPage('L', 'A4');
    if ($w > 830) {
    $scale=floatval(830.0 / $w);
    $scalep=floatval($scale * 100.0);
    //$outputpdf->_out('q');
    //Scale(floatval($scalep), floatval($scalep));
    $newh=($h * (830.0 / $w));
    $outputpdf->Image($fname, 0, 0, 830, floatval($newh)); //, 33.78);
    //$outputpdf->_out('Q');
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    } else {
    $outputpdf->AddPage('P', 'A4');
    if ($h > 830) {
    $scale=floatval(830.0 / $h);
    $scalep=floatval($scale * 100.0);
    //Scale(floatval($scalep), floatval($scalep));
    $neww=($w * (830.0 / $h));
    $outputpdf->Image($fname, 0, 0, floatval($neww), 830); //, 33.78);
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    }
    }
   } else {
    $outputpdf->AddPage('L','A4');
   }
   
   if (isset($_POST['pdftext1'])) {
   $texti=1;
   $ffy='Arial';
   $fst='B';
   $fsz=16;
   if (isset($_POST['pdffamily'])) {
     $ffy=str_replace('+',' ',urldecode($_POST['pdffamily']));
   }
   if (isset($_POST['pdffontstyle'])) {
     $ffy=str_replace('N',' ',substr(strtoupper(str_replace('+',' ',urldecode($_POST['pdffontstyle']))) . ' ',0,1));
   }
   if (isset($_POST['pdffontsize'])) {
     if (strlen(trim(str_replace('+',' ',urldecode($_POST['pdffontsize'])))) > 0) {
       $fsz=str_replace('+',' ',urldecode($_POST['pdffontsize']));
     }
   }
   $ofsz=$fsz;
   while (isset($_POST['pdftext' . $texti]) && isset($_POST['pdfx' . $texti]) && isset($_POST['pdfy' . $texti])) {
     $exc="~";
     if (strpos(urldecode($_POST['pdfx' . $texti]), '.') !== false && strpos(urldecode($_POST['pdfy' . $texti]), '.') === false) {
       $fsz=explode('.', urldecode($_POST['pdfx' . $texti]))[1];
       $exc=".";
     }
     if (strlen(trim(urldecode($_POST['pdftext' . $texti]))) > 0) {
     //echo "yesooo";
     //exit;
       $outputpdf->SetFont($ffy,$fst,$fsz);
       $ourx=floatval($scale * floatval(explode($exc, urldecode($_POST['pdfx' . $texti]))[0]));
       $oury=floatval($scale * floatval(urldecode($_POST['pdfy' . $texti])));
       $stris=str_replace('^%&',' + ',str_replace('+',' ',str_replace('+++','^%&',urldecode($_POST['pdftext' . $texti]))));
       $outputpdf->Text($ourx,$oury,$stris);     
     }
     $texti++;
     $fsz=$ofsz;
   }
   }
   if (file_exists('./' . $ourfname . '.pdf')) {    unlink('./' . $ourfname . '.pdf');   }
   $outputpdf->Output("./" . $ourfname . ".pdf", "F");
   $result = 0;
   if (strpos($_SERVER['SERVER_NAME'], 'rjmprogramming.com.au') !== false) {
   exec( "/usr/local/cpanel/3rdparty/bin/convert -density 300 " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".pdf " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".jpg"); //, null, $result ); // Thanks to https://stackoverflow.com/questions/8624886/pdf-to-jpg-conversion-using-php
   } else {
   exec( "/usr/local/bin/convert -density 300 " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".pdf " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".jpg"); //, null, $result ); // Thanks to https://stackoverflow.com/questions/8624886/pdf-to-jpg-conversion-using-php
   }
   if (strpos( $user_agent, 'Safari') !== false && strpos( $user_agent, 'Chrome') === false && strpos( $user_agent, 'OPR') === false && strpos( $user_agent, 'Firefox') === false && strpos( $user_agent, 'Opera') === false) {
   if (1 == 1) {
   $wobit="window.open('/" . $ourfname . ".pdf','_blank');";
   echo '<html><body onclick="' . $wobit . '" title="Click for PDF and/or scroll down to Sharing section" style="background:URL(' . 'data:image/jpeg;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.jpg')) . ');background-repeat:no-repeat;background-size:contain;"><img title="Click for PDF and/or scroll down to Sharing section" style=visibility:hidden; src="' . 'data:image/jpeg;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.jpg')) . '"></img></body></html>';
   } else {
   header('Content-Type: application/pdf');
   echo file_get_contents('./' . $ourfname . '.pdf');
   exec($safarisharehtmlbit);
   }
   } else {
   echo '<html><body><div style="overflow:auto;-webkit-overflow-scrolling:touch;height:100%;"><object style="width:100%;height:900px;" type="application/pdf" data="' . 'data:application/pdf;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.pdf')) . '">' . "\n" . '<embed style="width:100%;height:900px;" type="application/pdf" src="' .'data:application/pdf;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.pdf')) . '"></embed></object></div>' . $sharehtmlbit . '</body></html>'; 
   }
   exit;
} else if (isset($_GET['pdfimageplustext'])) {
   $w=0;
   $h=0;
   $outputpdf = new FPDF('P','pt');
   $outputpdf->SetTopMargin(0.0);
   //$outputpdf->AddPage('L');
   if (isset($_GET['pdfimage'])) {
    if (strpos(urldecode($_GET['pdfimage']), "data:image/") !== false && strpos(urldecode($_GET['pdfimage']), ";base64,") !== false) {
    $fname='./' . $ourfname . '.' . strtolower(explode(";", explode("data:image/", urldecode($_GET['pdfimage']))[1])[0]);
    file_put_contents($fname, base64_encode(explode(";base64,", urldecode($_GET['pdfimage']))[1]));
    list($w, $h) = getimagesize($fname);
    if ($w > $h) {
    $outputpdf->AddPage('L', 'A4');
    if ($w > 830) {
    $scale=floatval(830.0 / $w);
    $scalep=floatval($scale * 100.0);
    //$outputpdf->_out('q');
    //Scale(floatval($scalep), floatval($scalep));
    $newh=($h * (830.0 / $w));
    $outputpdf->Image($fname, 0, 0, 830, floatval($newh)); //, 33.78);
    //$outputpdf->_out('Q');
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    } else {
    $outputpdf->AddPage('P', 'A4');
    if ($h > 830) {
    $scale=floatval(830.0 / $h);
    $scalep=floatval($scale * 100.0);
    //Scale(floatval($scalep), floatval($scalep));
    $neww=($w * (830.0 / $h));
    $outputpdf->Image($fname, 0, 0, floatval($neww), 830); //, 33.78);
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    }
    } else if (strpos(urldecode($_GET['pdfimage']), "rjmprogramming.com.au/") !== false) {
    $fname="./" . explode("rjmprogramming.com.au/", urldecode($_GET['pdfimage']))[1];
    //$outputpdf->Image("./" . explode("rjmprogramming.com.au/", urldecode($_GET['pdfimage']))[1], 0, 0); //, 33.78);
    list($w, $h) = getimagesize($fname);
    if ($w > $h) {
    $outputpdf->AddPage('L', 'A4');
    if ($w > 830) {
    $scale=floatval(830.0 / $w);
    $scalep=floatval($scale * 100.0);
    //$outputpdf->_out('q');
    //Scale(floatval($scalep), floatval($scalep));
    $newh=($h * (830.0 / $w));
    $outputpdf->Image($fname, 0, 0, 830, floatval($newh)); //, 33.78);
    //$outputpdf->_out('Q');
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    } else {
    $outputpdf->AddPage('P', 'A4');
    if ($h > 830) {
    $scale=floatval(830.0 / $h);
    $scalep=floatval($scale * 100.0);
    //Scale(floatval($scalep), floatval($scalep));
    $neww=($w * (830.0 / $h));
    $outputpdf->Image($fname, 0, 0, floatval($neww), 830); //, 33.78);
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    }
    } else {
    $fname=urldecode($_GET['pdfimage']);
    //$outputpdf->Image(urldecode($_GET['pdfimage']), 0, 0); //, 33.78);
    list($w, $h) = getimagesize($fname);
    if ($w > $h) {
    $outputpdf->AddPage('L', 'A4');
    if ($w > 830) {
    $scale=floatval(830.0 / $w);
    $scalep=floatval($scale * 100.0);
    //$outputpdf->_out('q');
    //Scale(floatval($scalep), floatval($scalep));
    $newh=($h * (830.0 / $w));
    $outputpdf->Image($fname, 0, 0, 830, floatval($newh)); //, 33.78);
    //$outputpdf->_out('Q');
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    } else {
    $outputpdf->AddPage('P', 'A4');
    if ($h > 830) {
    $scale=floatval(830.0 / $h);
    $scalep=floatval($scale * 100.0);
    //Scale(floatval($scalep), floatval($scalep));
    $neww=($w * (830.0 / $h));
    $outputpdf->Image($fname, 0, 0, floatval($neww), 830); //, 33.78);
    } else {
    $outputpdf->Image($fname, 0, 0); //, 33.78);
    }
    }
    }
   } else {
    $outputpdf->AddPage('L','A4');
   }
   
   if (isset($_GET['pdftext1'])) {
   $texti=1;
   $ffy='Arial';
   $fst='B';
   $fsz=16;
   if (isset($_GET['pdffamily'])) {
     $ffy=str_replace('+',' ',urldecode($_GET['pdffamily']));
   }
   if (isset($_GET['pdffontstyle'])) {
     $ffy=str_replace('N',' ',substr(strtoupper(str_replace('+',' ',urldecode($_GET['pdffontstyle']))) . ' ',0,1));
   }
   if (isset($_GET['pdffontsize'])) {
     if (strlen(trim(str_replace('+',' ',urldecode($_GET['pdffontsize'])))) > 0) {
       $fsz=str_replace('+',' ',urldecode($_GET['pdffontsize']));
     }
   }
   $ofsz=$fsz;
   while (isset($_GET['pdftext' . $texti]) && isset($_GET['pdfx' . $texti]) && isset($_GET['pdfy' . $texti])) {
     $exc="~";
     if (strpos(urldecode($_GET['pdfx' . $texti]), '.') !== false && strpos(urldecode($_GET['pdfy' . $texti]), '.') === false) {
       $fsz=explode('.', urldecode($_GET['pdfx' . $texti]))[1];
       $exc=".";
     }
     if (strlen(trim(urldecode($_GET['pdftext' . $texti]))) > 0) {
     //echo "yesooo";
     //exit;
       $outputpdf->SetFont($ffy,$fst,$fsz);
       $ourx=floatval($scale * floatval(explode($exc, urldecode($_GET['pdfx' . $texti]))[0]));
       $oury=floatval($scale * floatval(urldecode($_GET['pdfy' . $texti])));
       $stris=str_replace('^%&',' + ',str_replace('+',' ',str_replace('+++','^%&',urldecode($_GET['pdftext' . $texti]))));
       $outputpdf->Text($ourx,$oury,$stris);     
     }
     $texti++;
     $fsz=$ofsz;
   }
   }
   if (file_exists('./' . $ourfname . '.pdf')) {    unlink('./' . $ourfname . '.pdf');   }
   $outputpdf->Output("./" . $ourfname . ".pdf", "F");
   $result = 0;
   if (strpos($_SERVER['SERVER_NAME'], 'rjmprogramming.com.au') !== false) {
   exec( "/usr/local/cpanel/3rdparty/bin/convert -density 300 " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".pdf " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".jpg"); //, null, $result ); // Thanks to https://stackoverflow.com/questions/8624886/pdf-to-jpg-conversion-using-php
   } else {
   exec( "/usr/local/bin/convert -density 300 " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".pdf " . dirname(__FILE__) . DIRECTORY_SEPARATOR . $ourfname . ".jpg"); //, null, $result ); // Thanks to https://stackoverflow.com/questions/8624886/pdf-to-jpg-conversion-using-php
   }
   if (strpos( $user_agent, 'Safari') !== false && strpos( $user_agent, 'Chrome') === false && strpos( $user_agent, 'OPR') === false && strpos( $user_agent, 'Firefox') === false && strpos( $user_agent, 'Opera') === false) {
   if (1 == 1) {
   $wobit="window.open('/" . $ourfname . ".pdf','_blank');";
   echo '<html><body onclick="' . $wobit . '" title="Click for PDF and/or scroll down to Sharing section" style="background:URL(' . 'data:image/jpeg;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.jpg')) . ');background-repeat:no-repeat;background-size:contain;"><img title="Click for PDF and/or scroll down to Sharing section" style=visibility:hidden; onclick="' . $wobit . '" src="' . 'data:image/jpeg;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.jpg')) . '"></img>' . $sharehtmlbit . '</body></html>';
   } else {
   header('Content-Type: application/pdf');
   echo file_get_contents('./' . $ourfname . '.pdf');
   exec($safarisharehtmlbit);
   }
   } else {
   echo '<html><body><div style="overflow:auto;-webkit-overflow-scrolling:touch;height:100%;"><object style="width:100%;height:900px;" type="application/pdf" data="' . 'data:application/pdf;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.pdf')) . '">' . "\n" . '<embed style="width:100%;height:900px;" type="application/pdf" src="' .'data:application/pdf;base64,' . base64_encode(file_get_contents('./' . $ourfname . '.pdf')) . '"></embed></object></div>' . $sharehtmlbit . '</body></html>'; 
   }
   exit;
}

?>
