<?php
// signature_signature.php
// RJM Programming
// May, 2017
// Help out signature_signature.html with large data set signatures

$overridinghtml="";
$oursubj="Signature's Signature";
$htmlis="";
$divcont="";
$data=["",
";00000211.30.244.26;-148,176,148,175,148,173,146,168,138,152,133,140,130,125,128,112,128,102,128,92,128,85,132,77,137,71,141,66,146,63,152,60,161,58,165,57,170,56,173,56,176,56,178,55,180,55,187,62,191,72,195,83,196,90,196,95,196,97,196,100,193,102,187,106,178,111,168,116,161,120,153,123,147,123,145,125,142,125,143,125,145,125,147,125,151,125,157,123,166,123,175,123,186,126,193,128,196,128,202,132,206,136,208,140,211,143,213,147,216,150,216,151,217,152,-271,161,271,160,268,157,262,151,257,142,250,128,246,115,242,96,242,80,242,68,253,53,262,53,272,55,282,62,288,66,303,86,310,100,312,107,313,115,315,120,315,122,315,123,315,122,313,121,312,113,312,103,312,93,315,87,321,81,331,77,345,75,360,73,386,73,400,83,408,95,417,111,423,127,427,136,428,142,430,147,431,148,431,150,-446,138,448,138,465,130,475,123,486,113,496,103,501,96,506,90,507,86,508,82,508,80,506,78,501,78,497,78,492,78,491,80,488,81,487,86,486,93,485,101,485,110,485,116,485,121,488,123,506,130,526,131,546,126,553,111,557,96,561,76,563,55,565,32,565,12,565,3,565,6,565,13,563,28,563,60,565,112,568,135,571,155,575,170,577,177,578,178,580,178,582,177,585,175,588,168,593,160,598,150,603,141,608,133,613,125,618,121,622,118,630,115,633,115,637,113,638,113,641,112,640,112,638,111,633,110,628,107,623,105,620,105,618,105,616,105,613,106,612,111,612,117,612,123,615,130,622,141,625,142,640,148,650,150,655,150,665,146,676,140,686,132,693,125,698,117,701,111,703,107,703,106,705,105,706,105,706,103,707,103,706,103,705,103,702,105,700,106,697,110,695,113,693,118,693,123,692,130,690,135,688,140,687,146,686,147,686,148,686,150,687,150,688,150,691,150,693,150,697,150,698,146,703,135,703,132,705,126,705,123,705,122,705,121,705,120,705,118,705,115,705,112,705,111,705,112,705,121,705,127,705,131,705,135,705,136,706,138,706,140,707,141,707,142,707,143,708,143,710,142,711,138,712,133,715,128,716,125,717,121,718,116,720,108,720,92,720,68,721,35,721,23,721,15,721,11,721,10,721,16,721,31,721,53,720,75,720,112,720,122,721,127,-498,80,502,80,508,80,526,76,596,58,640,51,662,50,678,50,691,50,696,50,697,50,697,48",
";0211.30.244.26;-254,166,254,148,254,144,254,141,254,128,254,121,254,113,256,107,259,91,262,82,264,74,269,63,275,52,278,48,280,47,285,41,287,40,292,39,293,40,295,43,299,49,301,53,302,57,307,65,309,68,311,71,314,75,317,80,317,81,317,85,317,86,317,89,317,93,315,96,313,98,307,103,304,105,300,107,293,109,280,111,276,111,266,112,263,113,260,113,258,114,257,114,256,114,258,116,269,122,275,125,282,127,297,134,305,138,308,140,317,145,326,151,328,153,330,154,333,157,335,159,335,160,336,162,337,163,337,164,338,164,338,165,338,164,338,162,-387,158,387,156,386,152,385,149,383,141,382,138,381,134,378,114,378,86,378,77,378,62,378,58,380,54,382,51,385,50,387,50,392,50,398,55,403,61,407,70,410,76,416,89,418,94,421,99,424,105,426,107,427,109,429,111,430,113,432,113,433,111,442,95,446,87,450,78,458,59,461,55,465,48,472,36,476,32,481,28,486,28,488,30,498,45,501,51,509,64,521,81,540,106,550,122,556,131,558,134,565,147,566,148,567,151,568,151,568,146,568,147",
";00000144.139.112.210;-193,152,193,151,193,146,193,144,192,139,192,136,191,132,188,124,188,119,188,112,187,107,187,104,187,101,187,98,187,95,187,94,187,93,187,92,187,91,187,89,187,88,187,87,188,86,192,82,195,78,196,76,198,75,199,74,200,73,201,72,201,71,204,69,206,69,207,68,208,67,209,66,211,66,212,66,214,66,215,66,218,66,219,66,220,67,221,68,221,69,221,71,221,73,221,75,221,80,221,86,221,94,222,102,222,112,222,118,221,124,216,132,214,134,212,136,211,139,209,139,208,139,207,139,206,139,204,139,202,139,201,139,200,139,198,139,196,139,195,136,193,134,193,133,193,132,193,131,193,128,194,127,195,127,196,127,198,127,199,127,201,127,205,127,208,127,209,128,214,129,214,131,216,131,218,133,221,133,225,136,226,138,227,139,228,140,229,141,231,141,232,141,232,142,-247,144,246,142,246,141,247,136,248,129,249,124,253,113,253,106,253,98,255,91,256,84,256,80,256,76,258,75,258,73,260,71,261,69,262,67,262,65,265,62,265,60,265,58,266,55,267,54,267,52,268,52,268,54,268,55,269,60,272,66,274,73,275,79,276,87,276,89,278,93,279,95,279,96,280,98,281,99,282,99,284,99,285,99,286,98,292,92,298,85,304,79,309,73,316,66,322,60,329,53,334,47,340,41,345,38,347,34,349,32,352,31,352,32,352,34,351,38,348,42,347,51,346,68,345,78,341,91,340,102,339,114,335,126,334,136,333,146,332,155,331,159,331,161,329,164,329,165,332,166,333,166,-354,146,354,145,356,144,364,142,372,140,384,133,392,129,396,124,401,121,405,118,407,116,408,115,409,114,411,113,411,112,411,111,409,111,408,111,407,111,406,111,405,111,404,111,402,111,401,111,400,111,398,111,396,111,394,111,392,111,389,112,389,113,387,114,386,115,386,116,384,119,384,120,384,121,382,122,382,125,382,126,385,127,387,129,388,129,389,131,393,131,396,133,400,135,405,136,408,139,411,139,413,140,415,140,419,140,420,140,422,140,425,139,427,136,428,135,429,133,432,129,433,125,434,121,436,115,438,112,439,106,439,104,441,99,442,95,445,89,445,86,446,80,447,78,448,73,449,71,451,67,451,66,451,65,451,66,451,67,451,71,449,73,449,76,447,81,445,88,445,91,445,92,445,94,445,95,444,95,444,96,444,98,444,101,444,106,444,114,444,125,444,133,444,141,442,146,442,148,442,149,442,151,442,152,445,152,448,152,451,152,453,151,454,148,456,145,459,139,459,136,461,133,461,131,464,128,466,126,468,122,469,120,471,120,472,120,473,120,473,121,475,121,479,121,480,121,484,121,486,121,487,121,488,121,491,122,493,122,493,124,495,124,496,124,498,124,499,124,498,124,496,124,495,124,494,124,493,124,492,124,491,124,489,124,488,124,487,124,486,125,485,125,484,125,482,126,480,127,480,128,479,128,476,128,475,131,473,131,473,132,471,134,469,134,468,135,467,135,467,136,467,138,466,139,466,141,466,142,466,145,466,147,466,151,466,154,466,159,467,164,468,166,471,168,472,169,474,169,475,169,482,169,484,169,486,168,488,166,489,164,492,162,494,161,496,160,500,159,501,158,502,158,504,158,505,156,506,156,507,155"];

function ourpremail($ris, $ssis, $tis, $his) {
    global $okay;
    $xsis = $ssis;
    try {
       //file_put_contents("aaaaa.00aaa", $ris . " " . $xsis);
       //mail("rmetcalfe15@gmail.com", $xsis, "My Body", $his);
       mail($ris, $xsis, $tis, $his);
    } catch (Exception $merr) {
      $okay = 0;
      if (file_exists(dirname(__FILE__) . "/mail.prob")) {
        file_put_contents(dirname(__FILE__) . "/mail.prob", file_get_contents(dirname(__FILE__) . "/mail.prob") . "\n\n" . date('l jS \of F Y h:i:s A') . " ... " . $merr->getMessage());
      } else {
        file_put_contents(dirname(__FILE__) . "/mail.prob", date('l jS \of F Y h:i:s A') . " ... " . $merr->getMessage());
      }
    }
}

function my_s_s_server_remote_addr() {
    $rma = $_SERVER['REMOTE_ADDR'];
    $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
    // you can add different browsers with the same way ..
    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 $rma;
}

$ournumc="1";
$eol = PHP_EOL;


        if (isset($_POST['htmlto'])) {
          if (strlen($_POST['htmlto']) > 0) {
          $xhtmlis=urldecode(urldecode($_POST['htmlto']));
          if (strpos($xhtmlis, "setTimeout(") !== false) {
            $overridinghtml=str_replace('#@#','+',str_replace('&lt;','<',str_replace('&gt;','>',$xhtmlis)));
          }
          }
        }


if (isset($_POST['emailto']) && isset($_POST['contentto'])) {
        $pbit="";
        if (isset($_POST['pagebit'])) {
          if (strlen($_POST['pagebit']) > 0) {
            $pbit=urldecode($_POST['pagebit']);
          }
        }
        if (isset($_POST['htmlto'])) {
          if (strlen($_POST['htmlto']) > 0) {
          if (strlen($overridinghtml) > 0) {
          $htmlis=$overridinghtml;
          } else {
          $htmlis=urldecode($_POST['htmlto']);
          }
          if (strpos($htmlis, "setTimeout(") !== false && strlen($overridinghtml) == 0) {
            $overridinghtml=str_replace('&lt;','<',str_replace('&gt;','>',$htmlis));
            $htmlis=$overridinghtml;
          }
          //if (strpos($htmlis, "</head>") === false || 1 == 1) {
          //   $htmlis=str_replace("</body>", "<img style=\"position:absolute;top:0;left:0;\" src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img></body>", $htmlis);
          //} else {
          //   $htmlis=str_replace("</head>", "<style> body {  background: #fff url(" . urldecode($_POST['contentto']) . "); } </style>\n</head>", $htmlis);
          //}
          }
        } else if (sizeof(explode('<',urldecode($_POST['emailto']))) > 1) {
          $htmlis=str_replace(explode('<',urldecode($_POST['emailto']))[0] . '<','<',urldecode($_POST['emailto']));
          //if (strpos($htmlis, "</head>") === false || 1 == 1) {
             //$htmlis=str_replace("<body", "<head><style> body {  background: #fff url(" . urldecode($_POST['contentto']) . "); } </style>\n</head><body", $htmlis);
          //   $htmlis=str_replace("</body>", "<img style=\"position:absolute;top:0;left:0\" src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img></body>", $htmlis);
          //} else {
          //   $htmlis=str_replace("</head>", "<style> body {  background: #fff url(" . urldecode($_POST['contentto']) . "); } </style>\n</head>", $htmlis);
          //}
        }
        if (strlen($htmlis) > 0) {
           $oursubj="Congratulations. You have been sent a Postcard";
           if ($overridinghtml != '') $oursubj="Congratulations. You have been sent an animated Postcard";
        }
        if (isset($_POST['subject'])) {
           if (strlen($_POST['subject']) > 0) {
            $oursubj=urldecode($_POST['subject']);
            $bgt=explode(" made up of ", $oursubj);
            if (sizeof($bgt) > 1) {
              $ournumc=explode(" ", $bgt[1])[0];
            }
           }
        }
        if (isset($_POST['from'])) {
        if ($_POST['from'] != '') {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . urldecode($_POST['from']) . $eol;
        } else {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        }
        } else {
        $headers = 'From: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        $headers .= 'Reply-To: ' . 'rmetcalfe@rjmprogramming.com.au' . $eol;
        }
        if (isset($_POST['cc'])) {
        if ($_POST['CC'] != '') {
        $headers .= 'CC: ' . urldecode($_POST['cc']) . $eol;
        }
        }
        if (isset($_POST['bcc'])) {
        if ($_POST['BCC'] != '') {
        $headers .= 'BCC: ' . urldecode($_POST['bcc']) . $eol;
        }
        }
                   //     'X-Mailer: PHP/' . phpversion();
        $pdffn="";
        $th="text/html";
        if ($pbit != "") {   // Thanks to https://www.html5rocks.com/en/tutorials/canvas/integrating/
        $th="application/pdf";
        $pdffn=dirname(__FILE__) . "/bnimage." . explode(";", explode(",", explode("/", urldecode($_POST['contentto']))[1])[0])[0];
        $htmlis="";
   //$contents_split = explode(',', urldecode($_POST['contentto']));
   $contents_split = explode(',', ($_POST['contentto']));
   $encoded = $contents_split[count($contents_split)-1];
   $decoded = "";
   for ($ii=0; $ii < ceil(strlen($encoded)/256); $ii++) {
      $decoded = $decoded . base64_decode(substr($encoded,$ii*256,256)); 
   }
   $contents = $decoded; // output 
   $content="";          
        } else {
        $contents = str_replace('data:image/png;base64,', '', str_replace('data:image/jpg;base64,', '', str_replace('data:image/jpeg;base64,', '', urldecode($_POST['contentto']))));
            if ($overridinghtml != "") $content = $overridinghtml;
        }

        if ($contents != "") {
          if ($contents != '') {

    		date_default_timezone_set('Australia/Perth');

            //$mysubject .= ' ... ' . $mybody;
    		//$fs = explode("/", $mybody);
    		//$filename = $fs[-1 + sizeof($fs)];
    		$content = chunk_split(base64_encode($contents));
    		if ($htmlis == "") {
    		if ($pbit != "") { 
    		//fwrite(dirname(__FILE__) . "/animage.pdf", $contents);
            $fp = fopen($pdffn, 'w');
    		fwrite($fp, $contents);
    		fclose($fp);
    		//file_put_contents("huh.huh", "convert " . $pdffn . " " . str_replace("  repage"," +repage",$pbit) . " " . dirname(__FILE__) . "/animage.pdf");
            exec("convert " . str_replace("  repage"," +repage",$pbit) . " " . $pdffn . " " . dirname(__FILE__) . "/animage.pdf");
            if (file_exists($pdffn)) unlink($pdffn);
            if (file_exists(dirname(__FILE__) . "/animage.pdf")) {
    		  $content = file_get_contents(dirname(__FILE__) . "/animage.pdf"); //chunk_split(base64_encode(file_get_contents(dirname(__FILE__) . "/animage.pdf")));
              unlink(dirname(__FILE__) . "/animage.pdf");
            }
    		} else {
    		$content = "<!doctype html><html><body><img src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img></body></html>";
            if ($overridinghtml != "") $content = $overridinghtml;
            }
            } else {
            //$content=$htmlis;
            $hbits=explode("</head>", str_replace("<head>","</head>",$htmlis));
            $bbits=explode("</body", str_replace("<body","</body",$htmlis));
            $hdrbit="<head><style> img {  opacity:0.8; position:absolute; top:0; left:0; z-index:1;  } \n :not(img) { opacity:1.0; width:100; text-align:right; z-index:9; background-color: transparent; } </style></head>";
            $bodybit="<body>";
            $endbit="";
            if (sizeof($hbits) > 1) {
            $hdrbit=str_replace("<head><style>", "<head> " . $hbits[1] . "\n<style>", $hdrbit);
            }
            if (sizeof($bbits) > 1) {
            $bodybit="<body" . explode(">",$bbits[1])[0] . ">"; 
            $endbit = substr($bbits[1],strlen(explode(">",$bbits[1])[0] . ">"));
            }
    		$content = "<!doctype html><html>" . $hdrbit . $bodybit . "<img src=\"" . $_POST['contentto'] . "\" title=\"User of Signature Signature\"></img>" . $endbit . "</body></html>";
            if ($overridinghtml != "") $content = $overridinghtml;
            }
  //file_put_contents("user_of_s_s.html", $bbits[1] . "..." . $endbit . ":::" . $htmlis);
  //file_put_contents("user_of_s_s.htm", $content);
    		//$uid = md5(uniqid(time()));

    		// a random hash will be necessary to send mixed content
    		$separator = md5(time());

    		$headers .= "MIME-Version: 1.0" . $eol;
    		
    		if (1 == 1 || strpos(strtolower(explode('<',urldecode($_POST['emailto']))[0]), "rmetcalfe15@gmail.com") !== false) {
    		$cont = chunk_split(base64_encode($content));
    		
    		$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
    		$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
    		$headers .= "This is a MIME encoded message." . $eol . $eol;

    		// message
    		$headers .= "--" . $separator . $eol;
    		if ($pbit == "") {   // Thanks to https://www.html5rocks.com/en/tutorials/canvas/integrating/
    		if (1 == 1) {
    		$headers .= "Content-Type: " . $th . "; charset=utf-8" . $eol; //application/octet-stream; name=\"" . $filename . "\"" . $eol;
    		$headers .= $content . $eol . $eol;
            } else {
    		$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
    		$headers .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
    		}
            }
			//if ($cno != 0) {
    		//  $headers .= $defone . $eol . $eol;
    		//} else {
    		$headers .= "Please see attachment below:" . $eol . $eol;
    		//}
            //ourpremail($tem, $mysubject, "", $headers . "--" . $separator . "--");

    		// attachment
    		$ao="application/octet-stream";
    		$filename="user_of_signature_signature_today.html";
    		if ($pbit != "") {   // Thanks to https://www.html5rocks.com/en/tutorials/canvas/integrating/
    		$filename="user_of_signature_signature_today.pdf";
    		if (1 == 2) $ao="application/pdf";
    		//$cont=$content;
            } 
    		$headers .= "--" . $separator . $eol;
    		$headers .= "Content-Type: " . $ao . "; name=\"" . $filename . "\"" . $eol;
    		//$headers .= "Content-Type: application/html; name=\"" . $filename . "\"" . $eol;
    		$headers .= "Content-Transfer-Encoding: base64" . $eol;
    		$headers .= "Content-Disposition: attachment;filename=\"" . $filename . "\"" . $eol; //inline" . $eol; //attachment;filename=\"" . $filename . "\"" . $eol;
    		$headers .= $cont . $eol . $eol;
            $headers .= "--" . $separator . "--";
    		} else {
    		$headers .= "Content-Type: text/html; charset=utf-8" . $eol; //application/octet-stream; name=\"" . $filename . "\"" . $eol;
    		$headers .= $content . $eol . $eol;
    		}
          }
        }                      

        //$headers .= "--" . $separator . "--";
    
    		
    		//file_put_contents("aaa.aaa", $filename);
            
        if (strpos(urldecode($_POST['emailto']), "@") !== false) ourpremail(explode('<',urldecode($_POST['emailto']))[0], $oursubj, "", $headers);
    		if ($pbit == "") {   // Thanks to https://www.html5rocks.com/en/tutorials/canvas/integrating/
        //echo "<!doctype html><html><body><h1>Email sent to " . explode('<',urldecode($_POST['emailto']))[0] . "</h1></body></html>";
        //header("Location: ./signature_signature.html");        
        $thishcont=file_get_contents(dirname(__FILE__) . "/signature_signature.html");
        echo str_replace("signature_signature.js", "signature_signature.js?complexity=yes", str_replace("<body", "<body onload=\" document.getElementById('animage').src='" . $_POST['contentto'] . "'; \" ", $thishcont));
            } else if (strpos(urldecode($_POST['emailto']), "@") === false) {
    //file_put_contents("huh.html", "<!doctype html><html><head><script type='text/javascript'> top.window.open(\"data:application/pdf;base64,\" + \"" . base64_encode($content) . "\",\"_blank\",\"top=0,left=400,width=200,height=200\"); </script></head><body></body></html>");
    //echo "<!doctype html><html><head><script type='text/javascript'> window.open(\"data:application/pdf;base64,\" + \"" . base64_encode($content) . "\",\"_blank\",\"top=0,left=400,width=200,height=200\"); </script></head><body></body></html>";
        $thishcont=file_get_contents(dirname(__FILE__) . "/signature_signature.html");
        echo str_replace("<tr><td", "<tr id='myfirsttr'><td", str_replace("signature_signature.js", "signature_signature.js?numc=" . $ournumc, str_replace("<body", "<body onload='  document.getElementById(\"animage\").src=\"" . $_POST['contentto'] . "\";  window.open(\"data:application/pdf;base64,\" + \"" . base64_encode($content) . "\",\"_blank\",\"top=0,left=400,width=400,height=800\");   ' ", $thishcont)));
        //echo str_replace("<body", "<body onload=\" document.getElementById('animage').src='" . $_POST['contentto'] . "'; \" ", $thishcont);
            }
        exit;
}

//if (!isset($_POST['emailto']) && !isset($_POST['contentto'])) {
$remove=0;
if (isset($_GET['pdf'])) {
    // Thanks to https://www.imagemagick.org/discourse-server/viewtopic.php?t=27974
    if (file_exists(dirname(__FILE__) . "/animage.wait")) {
     while (file_exists(dirname(__FILE__) . "/animage.wait")) {
       sleep(5);
     }
    }
    file_put_contents(dirname(__FILE__) . "/animage.wait", " ");
    $presufa="&pdfnickname=" . urlencode(explode("/", urldecode($_GET['pdf']))[-1 + sizeof(explode("/", urldecode($_GET['pdf'])))]);
    $presufa.="&pdfname=" . $_GET['pdf'];
    $sufa="&pno=1";
    $cpg="[0,-1]";
    if (isset($_GET['page'])) {
     if (strpos(urldecode($_GET['page']), "-") !== false || strpos(urldecode($_GET['page']), ",") !== false) {
      $sufa="";
      $cpg="[" . urldecode($_GET['page']) . "]";
     } else {
      $sufa="&pno=" . $_GET['page'];
      $cpg="[" . (-1 + $_GET['page']) . "]";
     }
    }
    if (file_exists(dirname(__FILE__) . "/animage.jpeg")) unlink(dirname(__FILE__) . "/animage.jpeg");
    foreach (glob(dirname(__FILE__) . "/animage-*.jpeg") as $filename) {
      unlink($filename);
    }
    exec("convert  " . explode('?',explode('#',urldecode($_GET['pdf']))[0])[0] . $cpg . " " . dirname(__FILE__) . "/animage.jpeg");
    //sleep(38);
    //$thishcont=file_get_contents(dirname(__FILE__) . "/signature_signature.html");
    //echo str_replace("<body", "<body onload=\" parent.document.location.href=parent.document.URL.split('#')[0].split('?')[0] + '?urlimage=' + encodeURIComponent('./animage.jpeg'); \" ", $thishcont);
    if (file_exists(dirname(__FILE__) . "/animage.jpeg")) {
    if (strpos($sufa, "&lno=") === false && isset($_GET['lastpage']))  $sufa .= "&lno=" . urldecode($_GET['lastpage']);
    if (file_exists(dirname(__FILE__) . "/animage.wait")) {
     unlink(dirname(__FILE__) . "/animage.wait");
    }
    if (isset($_GET['nameaimage'])) { // nametopcontext  nametopconvas
    echo "<!doctype html><html><head><script type='text/javascript'> parent.document.getElementById('" . $_GET['nameaimage'] . "').src='./animage.jpeg?rn=' + Math.floor(Math.random() * 23467894); </script></head><body></body></html>";
    } else {
    echo "<!doctype html><html><head><script type='text/javascript'> top.window.location = top.document.URL.split('#')[0].split('?')[0] + '?urlimage=' + encodeURIComponent('./animage.jpeg') + '" . $presufa . $sufa . "&rn=" . rand() . "'; </script></head><body></body></html>";
    }
    die;
    exit;
    } else {
    $sor=-1;
    $lor=-1;
    $sufa="";
    foreach (glob(dirname(__FILE__) . "/animage-*.jpeg") as $filename) {
      if ($sor < 0) {
        $sor=(1 + explode(".",explode("nimage-", $filename)[1])[0]);
        $sufa="&pno=" . $sor;
        $lor=$sor;
        //copy($filename, str_replace("-" . explode(".",explode("nimage-", $filename)[1])[0] . ".jpeg", ".jpeg", $filename));
        //unlink($filename);
        rename($filename, str_replace("-" . explode(".",explode("nimage-", $filename)[1])[0] . ".jpeg", ".jpeg", $filename));
      } else if ((1 + explode(".",explode("nimage-", $filename)[1])[0]) > $sor && (1 + explode(".",explode("nimage-", $filename)[1])[0]) > $lor) {
        $lor=(1 + explode(".",explode("nimage-", $filename)[1])[0]);
        unlink($filename);
      } else {
        unlink($filename);
      }
    }
    if ($sufa != "" && $lor > 0) $sufa .= "&lno=" . $lor;
    if (strpos($sufa, "&lno=") === false && isset($_GET['lastpage']))  $sufa .= "&lno=" . urldecode($_GET['lastpage']);
    if (file_exists(dirname(__FILE__) . "/animage.wait")) {
     unlink(dirname(__FILE__) . "/animage.wait");
    }
    if (isset($_GET['nameaimage'])) { // nametopcontext  nametopconvas
    echo "<!doctype html><html><head><script type='text/javascript'> parent.document.getElementById('" . $_GET['nameaimage'] . "').src='./animage.jpeg?rn=' + Math.floor(Math.random() * 23467894); </script></head><body></body></html>";
    } else {
    echo "<!doctype html><html><head><script type='text/javascript'> top.window.location = top.document.URL.split('#')[0].split('?')[0] + '?urlimage=' + encodeURIComponent('./animage.jpeg') + '" . $presufa . $sufa . "&rn=" . rand() . "'; </script></head><body></body></html>";
    }
    die;
    exit;
    }
} else if (isset($_GET['sscoords']) && isset($_GET['nonblankremove'])) {
    $pc=str_replace("+"," ",str_replace("@!@","",urldecode($_GET['sscoords'])));
    $remove=1;
    $thiscont=file_get_contents(dirname(__FILE__) . "/signature_signature.php");
    if (strpos($thiscont, ";" . my_s_s_server_remote_addr() . ";") !== false) {
      $predivcont=explode('|',explode(";" . my_s_s_server_remote_addr() . ";", $thiscont)[1])[0];
      $divcont=explode('"',explode(";" . my_s_s_server_remote_addr() . ";", $thiscont)[1])[0];
      //file_put_contents("xxx.z",substr(($pc . " "),0,1));
      if (substr(($pc . " "),0,1) == "|") {
      $ifthere="|" . explode("`", substr($pc,1))[0] . "`";
      //file_put_contents("xxx.zx",$remove . " " . $_GET['nonblankremove'] . "??" . $_SERVER['QUERY_STRING']);
      if ($remove > 0) {
      $jjustc=str_replace("``","`",str_replace($ifthere, "`", $pc)); 
      $justc=str_replace("``","`",str_replace($jjustc, "`", $divcont));
      //file_put_contents("xxx.zxc","\nifthere=" . $ifthere . "\njjustc=" . $jjustc . "\ndivcont=" . $divcont);
      if ($justc != $divcont) {
      //file_put_contents("xxx.zxcok","\nifthere=" . $ifthere . "\njjustc=" . $jjustc . "\ndivcont=" . $divcont);
      if (strpos(($justc . "|"), ($ifthere . "|")) !== false && strpos($divcont, ("|" . explode("`", substr($pc,1))[0] . $jjustc)) !== false) $jjustc="|" . explode("`", substr($pc,1))[0] . $jjustc;
      $thiscont=str_replace($divcont, str_replace($jjustc, "", $divcont), $thiscont);
      //file_put_contents("xxx.zxcokay","\nifthere=" . $ifthere . "\njjustc=" . $jjustc . "\ndivcont=" . str_replace($jjustc, "", $divcont));
      file_put_contents(dirname(__FILE__) . "/signature_signature.php", $thiscont);
      }
      }
      }
    }
} else if (isset($_POST['sscoords'])) {
    $pc=str_replace("@!@","",$_POST['sscoords']);
    $thiscont=file_get_contents(dirname(__FILE__) . "/signature_signature.php");
    if (strpos($thiscont, ";" . my_s_s_server_remote_addr() . ";") !== false) {
      $predivcont=explode('|',explode(";" . my_s_s_server_remote_addr() . ";", $thiscont)[1])[0];
      $divcont=explode('"',explode(";" . my_s_s_server_remote_addr() . ";", $thiscont)[1])[0];
      //file_put_contents("xx.z",substr(($pc . " "),0,1));
      if (substr(($pc . " "),0,1) == "|") {
      $ifthere="|" . explode("`", substr($pc,1))[0] . "`";

      if (strpos($divcont, $pc) === false) {
      if (strpos($divcont, $ifthere) !== false) {
      $thiscont=str_replace(";" . my_s_s_server_remote_addr() . ";" . $divcont, ";" . my_s_s_server_remote_addr() . ";" . str_replace($ifthere,$pc . "`",$divcont), $thiscont);
      } else {
      $thiscont=str_replace(";" . my_s_s_server_remote_addr() . ";" . $divcont, ";" . my_s_s_server_remote_addr() . ";" . $divcont . $pc, $thiscont);
      }
      } else if (strpos($divcont, $ifthere) !== false) {
      $thiscont=str_replace(";" . my_s_s_server_remote_addr() . ";" . $divcont, ";" . my_s_s_server_remote_addr() . ";" . str_replace($ifthere,$pc . "`",$divcont), $thiscont);
      }
      
      } else if (strpos($divcont, "|") !== false && $predivcont != "") {
      if ($pc != $predivcont) {
      $thiscont=str_replace(";" . my_s_s_server_remote_addr() . ";" . $predivcont, ";" . my_s_s_server_remote_addr() . ";" . $pc, $thiscont);
      }
      } else if (strpos($divcont, "|") !== false) {
      $thiscont=str_replace(";" . my_s_s_server_remote_addr() . ";", ";" . my_s_s_server_remote_addr() . ";" . $pc, $thiscont);
      } else {
      $thiscont=str_replace(";" . my_s_s_server_remote_addr() . ";" . $divcont, ";" . my_s_s_server_remote_addr() . ";" . $pc, $thiscont);
      }
      
    } else {
      $thiscont=str_replace('"' . ']' . ';', '",' . "\n" . '"' . ";" . my_s_s_server_remote_addr() . ";" . $pc . '"' . ']' . ';', $thiscont);
    }
    file_put_contents(dirname(__FILE__) . "/signature_signature.php", $thiscont);
    $plusurl="";
    if (isset($_POST['url'])) $plusurl="?url=" . urldecode($_POST['url']);
    if (substr(($pc . " "),0,1) != "|") header("Location: ./signature_signature.html" . $plusurl);
} else {
    $thiscont=file_get_contents(dirname(__FILE__) . "/signature_signature.php");
    if (strpos($thiscont, ";" . my_s_s_server_remote_addr() . ";") !== false) {
      $divcont=explode('"',explode(";" . my_s_s_server_remote_addr() . ";", $thiscont)[1])[0];
      echo "<!doctype html><html><body onload=\" if (parent.document.getElementById('longsignature')) {  parent.document.getElementById('longsignature').innerHTML='" . $divcont . "'; }  \"></body></html>";
    }
}
//}
?>
