<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(10);

function get_ext($f)
{
  $tmp = strrpos($f, '.'); /* Finds the last occurence of . */
  if ($tmp=='0')
  {
    return '';
  }
  else
  {
    return substr($f, $tmp+1);
  }
}

/* Inclusion of sendmail.php here */
/*********************************************
* Publish On : Jan 10th, 2004                *
* Edited By  : Maarten, Aug. 2006            *
* Scripter   : Hermawan Haryanto             *
* Version    : 1.0                           *
* License    : GPL (General Public License)  *
**********************************************/

function sendmail($from_name, $from_email, $to_name, $to_email, $subject, $text_message="", $html_message, $attachment, $attachmentName)
{
  $from = "$from_name <$from_email>";
  $to   = "$to_name <$to_email>";
  $main_boundary = "----=_NextPart_".md5(rand());
  $text_boundary = "----=_NextPart_".md5(rand());
  $html_boundary = "----=_NextPart_".md5(rand());
  $headers  = "From: $from\n";
  $headers .= "Reply-To: $from\n";
  $headers .= "X-Mailer: Mail-A-Friend script\n";
  $headers .= "MIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n\tboundary=\"$main_boundary\"\n";
  $message .= "\n--$main_boundary\n";
  $message .= "Content-Type: multipart/alternative;\n\tboundary=\"$text_boundary\"\n";
  $message .= "\n--$text_boundary\n";
  $message .= "Content-Type: text/plain; charset=\"ISO-8859-1\"\n";
  $message .= "Content-Transfer-Encoding: 7bit\n\n";
  $message .= ($text_message!="")?"$text_message":"Text portion of HTML E-mail";
  $message .= "\n--$text_boundary\n";
  $message .= "Content-Type: multipart/related;\n\tboundary=\"$html_boundary\"\n";
  $message .= "\n--$html_boundary\n";
  $message .= "Content-Type: text/html; charset=\"ISO-8859-1\"\n";
  $message .= "Content-Transfer-Encoding: 7bit\n\n";
  //$message .= str_replace ("=", "=3D", $html_message)."\n";
  $message .= "$html_message\n";
  if (isset ($attachment) && $attachment != "" && count ($attachment) >= 1)
  {
    for ($i=0; $i<count ($attachment); $i++)
    {
      $attfile = $attachment[$i];
  		$attfile = str_replace(" ", "%20", $attfile);
  		$file_name = basename ($attfile);
      $image_ext = get_ext($attachmentName);

  		$fp = fopen ($attfile, "r");
  		$fcontent = "";
  		while (!feof ($fp))
      {
        $fcontent .= fgets ($fp, 1024);
      }
      $fcontent = chunk_split(base64_encode($fcontent));
      @fclose ($fp);
      $message .= "\n--$html_boundary\n";
      if(in_array(strtolower($image_ext), array("jpg", "jpeg", "png", "gif")))
      {
        $message .= "Content-Type: image/".strtolower($image_ext).";\n";
      }
      else
      {
        $message .= "Content-Type: application/octetstream;\n";
      }
      $message .= " name=\"".$attachmentName."\"\n";
      $message .= "Content-Transfer-Encoding: base64\n";
      $message .= "Content-ID: <".$attachmentName.">\n";
      $message .= "Content-Disposition: inline; \n filename=\"".$attachmentName."\"\n\n";
      $message .= $fcontent;
    }
  }
  $message .= "\n--$html_boundary--\n";
  $message .= "\n--$text_boundary--\n";
  $message .= "\n--$main_boundary--\n";
  @mail($to, $subject, $message, $headers);
}
/* End of sendmail.php inclusion */

function stripcomment($filename, $divider)
{
  $commentTemp = explode($divider, $filename);
  $comment = substr($commentTemp[1], 0, (strlen((string) $commentTemp[1]) - 4));
  $comment = str_replace("<-", "</", $comment);
  $comment = str_replace("->", "/>", $comment);
  return $comment;
}


if ($_GET['e'] != "") //Display image instead of form
{
  $viewCartoon = 1;
  $cartoonURLenc = $_GET['e'];
  $cartoonURL = base64_decode($_GET['e']);
  //die(basename($cartoonURL));
}
else
{
  $imageURL = "";
  $imageURLenc = "";

  if ($_GET['i'] != "")
  {
    $imageURLenc = $_GET['i'];
    $imageURL = base64_decode($_GET['i']);
  }
  else
  {
    $imageURLenc = $_POST['i'];
    $imageURL = base64_decode($_POST['i']);
  }
  if ($imageURL == "")
  {
    die("Error: No image file present!");
  }
  $pathArray = explode("/", $imageURL);
  $imageName = $pathArray[count($pathArray) - 1];

  $imageThumbName = $imageName.".thumb.jpg";
  $pathArray[count($pathArray) - 1] = "thumbs/".$imageThumbName;
  $imageThumbURL = implode("/", $pathArray);

  if (strstr($imageThumbName, "--"))
  {
    $image_ext = get_ext($imageThumbName);
    $attachmentName = explode("--", $imageThumbName);
    $attachmentName = $attachmentName[0].".".$image_ext;
  }
  else
  {
    $attachmentName = $imageThumbName;
  }
  $attachmentName = str_replace("%20", "_", $attachmentName);
}
/* end of if-else loop */
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>Mail-A-Friend</title>
  </head>
  <style>
  * {
    margin: 0;
    padding: 0;
    border: 0;
  }

  body {
    background-color: #000;
    font-family: Trebuchet MS, Verdana, Arial, Helvetica, sans-serif;
  	font-size: 16px;
  	color: #000;
    margin-left: 20px;
    margin-top: 20px;
    text-align: center;
  }

  a:link, a:visited {
   color: #CC3333;
   text-decoration: underline;
  }

  a:active {
    color: #CC6600;
    text-decoration: none;
  }

  a:hover {
    text-decoration: underline overline;
    color: #CC6600;
  }

  label {
  	margin-top: 5px;
  	float: left;
  	width: 200px;
  }

  input {
    margin-top: 5px;
    border: 1px brown solid;
    font-family: Trebuchet MS, Verdana, Arial, Helvetica, sans-serif;
  	font-size: 16px;
  	color: #000;
  }

  #formulier {
    width: 460px;
    background-color: #fff;
    filter: alpha(opacity=90);
    -moz-opacity: .90;
    opacity: .90;
    border: 2px brown solid;
    padding: 10px;
    _width: 480px;
    margin-right: auto;
    margin-left: auto;
    text-align: left;
  }

  .info {
    margin-top: 10px;
  	font: 12px/20px Trebuchet MS, Verdana, Arial, Helvetica, sans-serif;
    font-style: italic;
  }

  img.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    text-align: center;
  }

  #imagecont {
    filter: alpha(opacity=95);
    -moz-opacity: .95;
    opacity: .95;
    border: 2px brown solid;
    padding: 10px;
    margin-right: auto;
    margin-left: auto;
  }

  #imagecont img {
    display: block;
    margin-left: auto;
    margin-right: auto;
  }

  #meer_info {
    width: 460px;
    background-color: #fff;
    filter: alpha(opacity=90);
    -moz-opacity: .90;
    opacity: .90;
    border: 2px brown solid;
    padding: 10px;
    _width: 480px;
    margin-bottom: 10px;
    margin-right: auto;
    margin-left: auto;
  }

  #commentaar {
    width: 460px;
    background-color: #fff;
    filter: alpha(opacity=90);
    -moz-opacity: .90;
    opacity: .90;
    border: 2px brown solid;
    padding: 10px;
    _width: 480px;
    margin-top: 10px;
    margin-bottom: 10px;
    margin-right: auto;
    margin-left: auto;
  }

  .infotekst {
    line-height: 175%;
  }

  </style>
<body>
<?php
if ($viewCartoon)
{ //Display div with the image in it, instead of the mail-a-friend form.
  $imageInfo = getimagesize(str_replace(" ", "%20", $cartoonURL));
?>
  <div id="meer_info">
    <p class="infotekst"><strong><a href="<?=$_SERVER['PHP_SELF'];?>?i=<?=$cartoonURLenc;?>" target="_self"> > Also send this image to a friend! < </a><br />
    <a href="http://<?=$_SERVER['SERVER_NAME'];?>" target="_self"> > Go to <?=$_SERVER['SERVER_NAME'];?> for more interesting images! < </a></strong></p>
  </div>
  <div id="imagecont" style="height: <?=$imageInfo[1]?>; width: <?=$imageInfo[0]?>; background: #fff url('<?=addslashes($cartoonURL);?>') center center no-repeat;"></div>
<?php
  $comment = trim(stripcomment(basename($cartoonURL), "--"));
  if ($comment !== "")
  {
  ?>
    <div id="commentaar"><p class="infotekst"><?=$comment;?></p></div>
  <?php
  }
}
else
{
?>
  <div id="formulier">
  <h3>Mail this image to a friend!</h3>
  <img class="center" src="<?=$imageThumbURL;?>" />
  <?php
  if ($HTTP_SERVER_VARS['REQUEST_METHOD'] == 'POST') // Formulier is gepost
  {
    foreach($HTTP_POST_VARS as $name => $value)
  	{
  		$$name = trim(stripslashes($value));
  	}
    if (($from_name != "") && ($from_email != "") && ($to_name != "") && ($to_email != ""))
    {
      //All relevant information has been entered, so send the email and display a confirmation.
      $subject = "Take a look at this image at ".$_SERVER['SERVER_NAME']."!";
      // Text Portion
      $text_message = "Dear ".$to_name.",\n\n";
      $text_message.= $from_name." would like to share this image with you. \n";
      $text_message.= "Click on the link below (or paste this link in your browsers \n";
      $text_message.= "address bar in case this doesn't work) to view it. \n";
      $text_message.= "http://".$_SERVER['SERVER_NAME']."/mailafriend.php?e=".$imageURLenc." \n\n";
      $text_message.= "(This message has been sent using the Mail-A-Friend service at ".$_SERVER['SERVER_NAME'].", designed and realised by MetaMinds.)";
      $text_message.= "";
      // HTML Portion
      $html_message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
      $html_message.= "<html><head><title></title>\n";
      $html_message.= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
      $html_message.= "<style type=\"text/css\">\n";
      $html_message.= "body, td {\nfont-family: Trebuchet MS, Arial, Verdana, sans-serif;\nfont-size: 14px;\n}\np.note {font-size: 12px;\nfont-style: italic;}\n";
      $html_message.= "</style>\n";
      $html_message.= "</head>\n";
      $html_message.= "<body><table width=\"450\" cellpadding=\"0\" cellspacing=\"0\"><tr valign=\"top\" align=\"left\">";
      $html_message.= "<td width=\"450\">Dear ".$to_name.",<br /><br />\n";
      $html_message.= $from_name." would like to share this image with you:<br />\n";
      $html_message.= "<a href=\"http://".$_SERVER['SERVER_NAME']."/mailafriend.php?e=".$imageURLenc."\"><img src=\"cid:".$attachmentName."\" border=\"0\"></a><br />\n";
      $html_message.= "Click on the image above or the link below to view this image in all of its glory.<br />\n";
      $html_message.= "(If you're unable to click on a link, copy and paste the address below in your browsers address bar.)<br />\n";
      $html_message.= "<a href=\"http://".$_SERVER['SERVER_NAME']."/mailafriend.php?e=".$imageURLenc."\">http://".$_SERVER['SERVER_NAME']."/mailafriend.php?e=".$imageURLenc."</a><br /><br />\n";
      $html_message.= "<p class=\"note\">This message was sent using the Mail-A-Friend service at <a href=\"http://".$_SERVER['SERVER_NAME']."\">".$_SERVER['SERVER_NAME']."</a>, designed and created by <a href=\"http://www.metaminds.nl\">MetaMinds</a> &copy;.</p>\n";
      $html_message.= "</td>";
      $html_message.= "</tr></table>";
      $html_message.= "</body></html>";

      # Attachment Location
      $attachment = array($imageThumbURL);
      # Execute SendMail Function
      sendmail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $attachment, $attachmentName);
      //sendmail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message);
      //Code below will make sure that name and address will be kept, in case the image is sent again.
      $from_name = "value=\"".$from_name."\"";
      $from_email = "value=\"".$from_email."\"";
      //Email has been sent, show a confirmation.
      echo "<strong>Thank you for your e-mail.<br />You can close this window or mail this image to someone else as well.</strong>";
    }
    else
    {
      //Not all data was entered correctly.
      echo "<strong>You didn't enter all of the necessary data!</strong> ";
      $form_style = "style=\"background-color: #FFCCCC;\"";
      reset($HTTP_POST_VARS); //reset the pointer to the beginning of the array
      foreach($HTTP_POST_VARS as $name => $value)
    	{
        if ($value == "") //var is empty, so attach code to make it appear in red.
        {
          $$name = $form_style;
        }
        else //var has been entered, so paste some code in front of it so the value is already displayed and doesn't have to be entered again.
        {
          $$name = "value=\"".trim(stripslashes($value))."\"";
        }
    	}
    }
  }
  ?>
  <form name="mailafriend" action="<?=$_SERVER['PHP_SELF'];?>" method="POST" />
  <label>Name recipient:</label> <input name="to_name" size="30" tabindex="1" maxlength="256" <?=$to_name;?>" />
  <br clear="all">
  <label>E-mail address recipient:</label><input name="to_email" size="30" tabindex="2" maxlength="256" <?=$to_email;?>" />
  <br clear="all">
  <label>Your name:</label> <input name="from_name" size="30" tabindex="3" maxlength="256" <?=$from_name;?>" />
  <br clear="all">
  <label>Your e-mail address:</label> <input name="from_email" size="30" tabindex="4" maxlength="256" <?=$from_email;?>" />
  <br clear="all">
  <input type="submit" name="submit" value="Send"> <input type="reset" name="reset" value="Reset" />
  <p class="info">The information entered above will <b>not</b> be saved, nor used in any way other than for the purpose of this script.</p>
  <input type="hidden" name="i" value="<?=$imageURLenc;?>">
  </form>
  </div>
<?php
}//end else
?>
</body>
</html>