<?php
// use_attachment.php
// RJM Programming
// July, 2018
// PDF Attachments via fpdf and attachment.php ... thanks

// Thanks to http://www.fpdf.org/en/script/script95.php
require('attachment.php');

$httpc='';
$flist='';
$fdelim='';

function ourhtmlspecialchars($inv) {
  global $httpc;
  if ($httpc != '') {
    $xhttpc=$httpc;
    $httpc='';
    return $xhttpc;
  }
  return urldecode($inv);
}

function short($inf) {
  global $httpc;
  $inf=urlencode(str_replace("_",".",urldecode($inf)));
  //if (strpos(("~" . $inf), "~attach") !== false) echo urldecode($inf) . "\n";
  if (strpos(urldecode($inf), "/") !== false) {
    if (strpos(("~" . strtolower(urldecode($inf))), "~http") !== false) {
      $httpc=file_get_contents(urldecode($inf));
    }
    return explode("/", urldecode($inf))[-1 + sizeof(explode("/", urldecode($inf)))];
  } else if (strpos(urldecode($inf), substr("\\",0,1)) !== false) {
    if (strpos(("~" . strtolower(urldecode($inf))), "~http") !== false) {
      $httpc=file_get_contents(urldecode($inf));
    }
    return explode(substr("\\",0,1), urldecode($inf))[-1 + sizeof(explode(substr("\\",0,1), urldecode($inf)))];
  }
  //if (strpos(("~" . $inf), "~attach") !== false) echo ":" . urldecode($inf) . "\n";
  return urldecode($inf);
}

$pdf = null; //new PDF_Attachment();
$done=false;
$msg='This PDF contains an attached file.';
$cnt=0;
$pref='';
foreach ($_GET as $name => $val) {
     if (!$pdf) $pdf = new PDF_Attachment();
     //if (strpos(htmlspecialchars($val), ".php") === false) {
      if ($val != "" && strtolower($name) == "subject") {
       $httpc="";
       short($val);
       if ($httpc != "") {
         $msg=$httpc;
       } else {
         $msg=htmlspecialchars($val);
       }
      } else if ($val == "" && strtolower($name) == "subject") {
       $msg=$msg;
      } else if ($val == "" && file_exists($name)) {
       $pdf->Attach(getcwd() . "/" . $name);
       $done=true;
       $cnt++;
      } else if ($val == "" && !file_exists(short($name))) {
       file_put_contents(short($name), "");
       $pdf->Attach(getcwd() . "/" . short($name));
       $flist.=$fdelim . (short($name));
       $fdelim=',';
       $done=true;
       $cnt++;
      } else if ($val != "" && file_exists($name)) {
       $pref="1_";
       while (file_exists($pref . short($name))) {
         $pref.="1_";
       }
       file_put_contents($pref . short($name), ourhtmlspecialchars($val));
       $pdf->Attach(getcwd() . "/" . $pref . short($name));
       $flist.=$fdelim . ($pref . short($name));
       $fdelim=',';
       $done=true;
       $cnt++;
      } else if ($val != "") {
       file_put_contents(short($name), ourhtmlspecialchars($val));
       $pdf->Attach(getcwd() . "/" . short($name));
       $flist.=(short($name));
       $fdelim=',';
       $done=true;
       $cnt++;
      }
     //}
     $pref='';
}
if (!$done) {
foreach ($_POST as $name => $val) {
     if (!$pdf) $pdf = new PDF_Attachment();
     //if (strpos(htmlspecialchars($val), ".php") === false) {
      if ($val != "" && strtolower($name) == "subject") {
       $httpc="";
       short($val);
       if ($httpc != "") {
         $msg=$httpc;
       } else {
         $msg=htmlspecialchars($val);
       }
      } else if ($val == "" && strtolower($name) == "subject") {
       $msg=$msg;
      } else if ($val == "" && file_exists(short($name))) {
       echo "\n ! " . short($name) . "\n";
       if (file_exists($pref . short($name))) echo "\n !! " . $pref . short($name) . "\n";
       $pdf->Attach(getcwd() . "/" . short($name));
       $done=true;
       $cnt++;
      } else if ($val == "" && !file_exists(short($name))) {
       file_put_contents(short($name), "");
       //echo "\n ? " . short($name) . "\n";
       //if (file_exists($pref . short($name))) echo "\n ?? " . $pref . short($name) . "\n";
       $pdf->Attach(getcwd() . "/" . short($name));
       $flist.=(short($name));
       $fdelim=',';
       $done=true;
       $cnt++;
      } else if ($val != "" && file_exists(short($name))) {
       $pref="1_";
       while (file_exists($pref . short($name))) {
         $pref.="1_";
       }
       file_put_contents($pref . short($name), ourhtmlspecialchars($val));
       //echo "\n . " . $pref . short($name) . "\n";
       //if (file_exists($pref . short($name))) echo "\n .. " . $pref . short($name) . "\n";
       $pdf->Attach(getcwd() . "/" . $pref . short($name));
       $flist.=($pref . short($name));
       $fdelim=',';
       $done=true;
       $cnt++;
      } else if ($val != "") {
       file_put_contents(short($name), ourhtmlspecialchars($val));
       //echo "\n " . short($name) . "\n";
       //if (file_exists(short($name))) echo "\n ... " . short($name) . "\n";
       $pdf->Attach(getcwd() . "/" . short($name));
       $flist.=(short($name));
       $fdelim=',';
       $done=true;
       $cnt++;
      }
     //}
     $pref='';
}
}

if (!$pdf) {
echo "<!doctype html>
<html>
<head>
<title>PDF Attachments - RJM Programming - July, 2018</title>
<script type='text/javascript'>
function pd(oi) {
 if (oi.value != '') {
 if (oi.id.indexOf('content') != -1) {
  //alert('ii' + oi.id.replace('content','') + ' ... ' + oi.value);
  document.getElementById('ii' + oi.id.replace('content','')).value=oi.value;
 } else {
  //alert('d' + oi.id.replace('i','') + ' .. ' + '<input id=ii' + oi.id.replace('i','') + ' type=hidden style=display:none; value=\"\" name=' + oi.value + '></input>');
  document.getElementById('d' + oi.id.replace('i','')).innerHTML='<input id=ii' + oi.id.replace('i','') + ' type=hidden style=display:none; value=\"\" name=' + oi.value + '></input>';
 }
 }
}
</script>
</head>
<body>
<h1>PDF Attachments</h1>
<h3>RJM Programming</h3>
<h3>July, 2018</h3>
<form onsubmit='return true;' action='./use_attachment.php' method='POST'>
<table style='width:100%;' border=5>
<tr style=background-color:pink;><th>Subject</th><th><textarea name=subject cols=80 rows=5></textarea></th></tr>
<tr><td id=td1>Attachment name (or HTTP url content) 1: <input onblur='pd(this);' type=text id=i1 value=''></input><div id=d1></div></td><td>Non HTTP URL Content 1: <textarea onchange='pd(this);' id=content1 cols=80 rows=5></textarea></td></tr>
<tr><td id=td2>Attachment name (or HTTP url content) 2: <input onblur='pd(this);' type=text id=i2 value=''></input><div id=d2></div></td><td>Non HTTP URL Content 2: <textarea onchange='pd(this);' id=content2 cols=80 rows=5></textarea></td></tr>
<tr><td id=td3>Attachment name (or HTTP url content) 3: <input onblur='pd(this);' type=text id=i3 value=''></input><div id=d3></div></td><td>Non HTTP URL Content 3: <textarea onchange='pd(this);' id=content3 cols=80 rows=5></textarea></td></tr>
<tr><td id=td4>Attachment name (or HTTP url content) 4: <input onblur='pd(this);' type=text id=i4 value=''></input><div id=d4></div></td><td>Non HTTP URL Content 4: <textarea onchange='pd(this);' id=content4 cols=80 rows=5></textarea></td></tr>
</table>
<br>
<input type='submit' value='Create PDF' style=background-color:yellow;></input>
</form>
</body>
</html>";
} else {
if (!$done && strpos(("~" . $msg), "~This PDF contain") !== false) $pdf->Attach('attached.txt');
if ($cnt > 1 && strpos(("~" . $msg), "~This PDF contain") !== false) $msg='This PDF contains ' . $cnt . ' attached files.';
$pdf->OpenAttachmentPane();
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->Write(5,$msg);
$pdf->Output();
if ($flist != '') {
  $fs=explode($fdelim, $flist);
  for ($ifs=0; $ifs<sizeof($fs); $ifs++) {
    unlink($fs[$ifs]);
  }
}
}
?>
