<?php 

 $MAIL_HOST="imap.your_internet_supplyer.com"; 
 $USER_NAME="your_name@your_domain.com"; 
 $USER_PASS="your_password"; 

 
 
 
 $get_mails_for = htmlspecialchars($_GET["MailName"]);
 $send_mail_from = htmlspecialchars($_GET["SendMailFrom"]);
 $send_mail_to = htmlspecialchars($_GET["SendMailTo"]);
 $mailbody = htmlspecialchars($_GET["MailBody"]);
 
 $start_image = htmlspecialchars($_GET["SI"]); 
 $end_image = htmlspecialchars($_GET["EI"]); 
 $get_image_name = htmlspecialchars($_GET["IN"]); 
 $get_image = htmlspecialchars($_GET["I"]);
 $give_me_image = htmlspecialchars($_GET["GIVE"]);
 $check_file = htmlspecialchars($_GET["CHECKFILE"]);
 
 if( strlen($call_from_htp) > 0 )
 {
   $send_mail_from = $send_mail_from_C;
   $send_mail_to = $send_mail_to_C;
   $mailbody = $mailbody_C;
   $get_image_name = $get_image_name_C;
   $end_image = $end_image_C;
   $get_image = $get_image_C;
   $start_image = $start_image_C;
 }

 function SendMailWithPHPMailer( $mail_to, $mail_message, $mail_from, $file_in_server )
 {
  echo "<br>Sending Mail via PHPMailer....  ";
  require_once 'class.phpmailer.php';
  echo "PHPMailer found<br>";
  echo "Sending Mail from " . $mail_from . "<br>";
  echo "Sending Mail to " . $mail_to . "<br>";
  $mail = new PHPMailer(); // defaults to using php "mail()"
  $mail->IsSendmail(); // telling the class to use SendMail transport
  $body = $mail_message;
  $mail->AddReplyTo($mail_from,"First Last");
  $mail->SetFrom($mail_from.'@your_domain.com', $mail_from);
  $address = $mail_to;
  $mail->AddAddress($address, "www.your_domain.com");
  $mail->Subject    = "Bild " . $file_in_server;
  $mail->AltBody    = $mail_message; 
  $mail->MsgHTML($body);
  $mail->AddAttachment($file_in_server);      // attachment

  if(!$mail->Send()) 
  {
    echo "Mailer Error: " . $mail->ErrorInfo;
  } 
  else 
  {
    // @Message sent!  is a feedback string for Arduino
    echo "@Message sent!";
  }
    
 }

 function GetRGB( $color_val, &$r, &$g, &$b )
 {
   //echo "Color " . $color_var ;
   if( $color_val == 0 )  //WHITE
   {
     $r = 255;
     $g = 255;
     $b = 255;
   }
   else if( $color_val == 1 )  //BNACK
   {
     $r = 0;
     $g = 0;
     $b = 0;
   }
   else if( $color_val == 2 )  //RED
   {
     $r = 255;
     $g = 0;
     $b = 0;
   }
   else if( $color_val == 3 )  //BLUE
   {
     $r = 0;
     $g = 0;
     $b = 255;
   }
   else if( $color_val == 4 )  //GREEN
   {
     $r = 0;
     $g = 255;
     $b = 0;
   }
   else if( $color_val == 5 )  //FUCSIA
   {
     $r = 255;
     $g = 0;
     $b = 255;
   }
   else if( $color_val == 6 )  //YELLOW
   {
     $r = 255;
     $g = 255;
     $b = 0;
   }
   else //if( $color_val == 7 )  //AGUA
   {
     $r = 0;
     $g = 255;
     $b = 255;
   }
   //echo "R " . $r . " G " . $g . " B " . $b . "<br>";

 }
 
 function CheckFileExists( $check_file_name )
 {
   //$search_file= $_SERVER['DOCUMENT_ROOT']. "/scripts/Radioteleskop/" . $check_file_name;
   $search_file= $check_file_name;
   if( file_exists( $search_file) )
   {    
     echo "File " . $search_file . " exists";
   } 
   
   else
     echo "File " . $search_file . " does not exist";
 }

 function ConvertTextForInternet( $name_of_file )
 {
   //$name_of_file_abs = "/scripts/Radioteleskop/" . $name_of_file;
   //echo "Searching File " . $name_of_file_abs;
   if( !file_exists( $name_of_file) )
   {
     echo "SIZE0" . "@@@";
     return;
   }
   $req_file = fopen($name_of_file, "r");// or die("Unable to open file!");
   echo "SIZE" . filesize($name_of_file) . "@@@";
   usleep(50000);
   while(!feof($req_file)) 
   {     
     $new_char = fgetc($req_file);
     if( $new_char == 'F' )
     {   //we send %FX-Y-B-C%
       echo "%" . $new_value . "F";
       $new_value = "";       
       usleep(75000);
     }
     else
       $new_value = $new_value . $new_char;     
   }
   fclose($req_file);
   usleep(75000);
 }

 function ConvertTextToImage( $name_of_file )
 {   
   echo "Converting file from text to image <br>";
   echo "Opening File: " . $name_of_file . "<br>";
   $req_file = fopen($name_of_file, "r") or die("Unable to open file!");
   echo "Reading: <br>";

   $x = 319;
   $y = 239;
   $gd = imagecreatetruecolor($x, $y);   
   $new_value = "";
   $last_R = 0;
   $last_G = 0;
   $last_B = 0;
   $last_thickness = 4;
   $last_x = -1;
   $last_y = -1;
   $imageRGB = imagecolorallocate($gd, $last_R, $last_G, $last_B); 
   //imagesetthickness($gd, 5);
   while(!feof($req_file)) 
   {     
     $new_char = fgetc($req_file);
	 //echo $new_char;
     if( $new_char == 'F' )
     {
       //echo $new_value . "<br>";
       $last_thickness = 4;
       $teile = explode("-", $new_value);
       if( sizeof($teile) > 3 )
       {
         GetRGB( $teile[3], $last_R, $last_G, $last_B );
         $imageRGB = imagecolorallocate($gd, $last_R, $last_G, $last_B); 
       } 
       else if( sizeof($teile) > 2 )
         $last_thickness = intval($teile[2], 0);
       if( $last_thickness > 7 )
         $last_thickness = 7;

       if( strlen($teile[0]) > 0 )
         $last_x = $teile[0];
       if( strlen($teile[1]) > 0 )
         $last_y = $teile[1];     


       //echo "Last thickness " . $last_thickness . " in row " . $new_value ."<br>";
       imagesetpixel($gd, round($last_x),round($last_y), $imageRGB);
       imagefilledellipse ($gd, round($last_x), round($last_y), $last_thickness, $last_thickness, $imageRGB);
       $new_value = "";
     }
     else
       $new_value = $new_value . $new_char;     
   }
   echo "saving Image to PNG with name ";
   $name_only="temp";
   if( strstr( $name_of_file, ".TXT" ) )
   {
     $name_only = explode(".TXT", $name_of_file);
   }
   else
   {
     $name_only = explode(".txt", $name_of_file);
   }
   $image_name = $name_only[0] . ".png";
   echo $image_name;
   echo "<br>";
   header('Content-Type: image/png');
   //imagepng($gd);
   imagepng($gd, $image_name );
   fclose($req_file);   
 }


 
 
 
 /***********************************************************************************/
 /* ****************************         MAIN       *********************************/
 /***********************************************************************************/
 
 
 
 /*****************************************/
 /*               SEND MAIL               */
 /*****************************************/
 if( (strlen($send_mail_from) > 0 && strlen($send_mail_to) > 0) 
     || strlen($get_image) > 0 || strlen($_POST["MailImage"])>0 
	 || strlen($end_image) > 0 || strlen($get_image_name) > 0
     || strlen($give_me_image) > 0  )
 {
   $handy_light = 0;
   $to      = 'mama@your_domain.com';
   if( strcmp($send_mail_to, "Papa") == 0 )
     $to      = 'papa@your_domain.com';
   else if( strcmp($send_mail_to, "Keanu") == 0 )
   {
     $to      = 'keanu@your_domain.com';
     $handy_light = 1;
   }
   else if( strcmp($send_mail_to, "Kilian") == 0 )
   {
     $to      = 'kilian@your_domain.com';
     $handy_light = 1;
   }
   else if( strcmp($send_mail_to, "Samira") == 0 )
   {
     $to      = 'samira@your_domain.com';
     $handy_light = 1;
   }
   else if( strcmp($send_mail_to, "Mami") == 0 )
     $to      = 'gradnma@hotmail.com';
   else if( strcmp($send_mail_to, "Juanito") == 0 )
     $to      = 'oncle@hotmail.com';
   else if( strcmp($send_mail_to, "Javier") == 0 )
     $to      = 'myfriend@gmail.com';
  
   
   $subject = 'De: ' . $send_mail_from;
   $message = $mailbody;	  
   $headers = 'From: ' . $send_mail_from . '@your_domain.com' . "\r\n" .
              'Reply-To: ' . $send_mail_from . '@your_domain.com' . "\r\n" .
              'X-Mailer: PHP/' . phpversion();      

   
   //echo "\nIMAGE_XY $get_image";
   if( strlen($get_image_name ) > 0 && $start_image >0 && strlen($get_image) > 0)
   {
     $file_status = file_put_contents($get_image_name, $get_image); 
	 echo "Image file created\n";
   }
   elseif( strlen($get_image_name ) > 0 && strlen($get_image) > 0)
   {
     $file_status = file_put_contents($get_image_name, $get_image, FILE_APPEND); 
	 echo "Image file " . $get_image_name . " has now " . filesize($get_image_name) . ' Byte' . '<br>';
	 //echo filesize($get_image_name) . "<br>";
   }
   
   if( strlen($end_image) > 0 )
   {
        //echo $end_image . ': ' . filesize($end_image) . ' Byte' . '<br>';        
        echo "Converting image format<br>";
        echo 'Filesize: ' . filesize($get_image_name) . ' Byte' . '<br>';		
        ConvertTextToImage( $get_image_name );
        //ConvertBinToImage( $get_image_name );
   }
   
   if( strlen($message) > 0 || strlen($send_mail_from) > 0)  
   {
    /*****************************************************/
    /**************  SENDING MAILS   *********************/
    /*****************************************************/	 
     echo date(DATE_RFC822);
     echo "<br>";

     if( strlen($get_image_name ) > 0 )
     {
        echo "Mail with attachment:" . $get_image_name . "<br>";
        if( $handy_light == 1  )
        {
          echo "Destination Person with Light Handy<br>";
          $subject = "##" . $get_image_name;
          echo "Printing image name on Subject:     " . $subject;
          //echo "<br>Mail sent to $to <br>";
          echo "<br>Sending mail to $to with text <br>";
          echo $message . "<br>";
          mail($to, $subject, $message, $headers);	
          // @Message sent!  is a feedback string for Arduino
          echo "@Message sent!"; 
        }
        else
        {
          //Send Real Mail with attachment          
          $name_only = explode(".txt", $get_image_name);
          $image_name = $name_only[0] . ".png";          
          //SendMailWithAttachment( $to, $message, $headers, $image_name);  
          SendMailWithPHPMailer( $to, $message, $send_mail_from , $image_name);
        }        
     }
     else
     {
       //echo "<br>Mail sent to $to <br>";
       echo "<br>Sending mail to $to with text <br>";
       echo $message . "<br>";
       mail($to, $subject, $message, $headers);	
       // @Message sent!  is a feedback string for Arduino
       echo "@Message sent!"; 
     }
   }
   
 

   if( strlen($give_me_image) > 0 )
   {
     echo "Image required: " . $give_me_image . "<br>";
      //$req_file = fopen($give_me_image, "r") or die("Unable to open file!");
     $req_file = fopen($give_me_image, "r") ;
     echo "{";
     ConvertTextForInternet( $give_me_image );
     echo "}";
     //fclose($req_file);
   }


 }



 
 /*****************************************/
 /*               CHECK FILE              */
 /*****************************************/
 else if( strlen($check_file) > 0 )
   CheckFileExists( $check_file );  
   







 /*****************************************/
 /*              RECEIVE MAILS            */
 /*****************************************/   
 else
 {
   if( strcmp($get_mails_for,"Keanu") == 0 )
   {
     $USER_NAME="keanu@your_domain.com"; 
     $USER_PASS="A123456b"; 
   }
   else if( strcmp($get_mails_for,"Kilian")==0 )
   {
     $USER_NAME="kilian@your_domain.com"; 
     $USER_PASS="A123456b"; 
   }
   else if( strcmp($get_mails_for,"Samira") == 0 )
   {
     $USER_NAME="samira@your_domain.com"; 
     $USER_PASS="A123456b"; 
   }
   else 
     $USER_NAME="mama@your_domain.com"; 

   echo "Reading Mails for " . $get_mails_for . " from Server with PHP\n";
   $inbox = imap_open ("{". $MAIL_HOST . ":143}INBOX", $USER_NAME, $USER_PASS) or die("Can't connect IMAP-Server!");  
   $total = imap_num_msg($inbox)+1; 
   for($i=1; $i<$total; $i++) 
   { 
     // get message headers and structure 
     $headers = imap_header($inbox, $i); 
     $structure = imap_fetchstructure($inbox, $i); 
 
     if( $headers->Unseen == 'U' || $headers->Recent == 'N' ) 
     {   
     //echo "Von: ".htmlspecialchars($headers->fromaddress) . "   " ; 
     //echo "An: ".htmlspecialchars($headers->toaddress) . "<br>" ; 
     //echo "Datum: ".$headers->Date . "<br>" ;	 
       $from_field = htmlspecialchars($headers->fromaddress);
	   echo "_FROM: ". $from_field ."\n";
       flush();
       usleep(75000);
	   echo "_BODY: ";       
       echo nl2br( htmlspecialchars(stripslashes(trim(imap_body ($inbox, $i))))) ;
       flush();
       usleep(75000);
	   echo "_ATTACHMENT: ";
       flush();
       if ($headers->Subject)  
       {
         $subj_teile = explode("##", $headers->Subject);
         $imageN = $subj_teile[1];
         //echo "Picutre " . $imageN . " ";
         if( strlen($imageN) > 3 )
           ConvertTextForInternet( $imageN );
       }       
	   
     }
   }
   
   echo "Finnished#\n";
   // clean up 
   imap_close($inbox); 
 }
 ?>

















