Guest User

Untitled

a guest
May 10th, 2017
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.18 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  <html>
  3.     <head>
  4.         <title>Outlook Pasto Sistema</title>
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
  6.         <style>
  7.             *{
  8.                 margin:0;
  9.                 padding:0;
  10.             }
  11.             html{
  12.                 height: 100%;
  13.                 width: 100%;
  14.             }
  15.             body{
  16.                 height: 100%;
  17.                 width: 100%
  18.             }
  19.             table{
  20.                 table-layout: fixed;
  21.                 width: 100%;
  22.             }
  23.             td{
  24.                 border : 1px solid black;
  25.                 text-align: center;
  26.                 width: 10%;
  27.                 word-wrap:break-word
  28.             }
  29.             th {
  30.                 border: 1px solid black;
  31.                 word-wrap:break-word
  32.                 }
  33.         </style>
  34.     </head>
  35.     <body>
  36.        
  37.  
  38.  
  39.        
  40. <?php
  41. header('Content-Type: text/html; charset=UTF-8');
  42. //Prijungiam duomenu baze
  43. include ('config/DbConnect.php');
  44.  
  45.  
  46. //Prisijungimai i el.pasta
  47. $user = 'tautvydas.ulevicius@vilnius.lt';
  48. $password = 'Taul320k';
  49.  
  50.  //Prisijungimo funkcija...Nustatom tls ir pridedam , kad be sertifikato...
  51. function pop3_login($host,$port,$user,$pass,$folder="INBOX",$tls=false)
  52. {
  53.     $tls = ($tls==false) ? '/novalidate-cert' : '';
  54.  
  55.     return (imap_open('{'.$host.':'.$port.'/pop3/tls}'.$folder,$user,$pass));
  56. }
  57.  
  58. //Isgaunam prisijungimo statusa....
  59. function pop3_stat($imap){
  60.     $check = imap_mailboxmsginfo($imap);
  61.  
  62.     return ((array)$check);
  63. }
  64.  
  65. //Informacija, prie kur jungtis
  66. $inbox = pop3_login("mail.vilnius.lt","110",$user,$password,$folder="INBOX",$tls=true);
  67. $emails = imap_search($inbox,'UNSEEN');
  68.  
  69. //Paziurim el-pasto statusa...
  70. $check = imap_mailboxmsginfo($inbox);
  71. if ($check) {
  72.     echo '<table><tr>';
  73.     echo "<th>"."Date: "."</th>"."<th>"."Driver: "."</th>"."<th>"."Mailbox: "."<th>"."Messages: "."<th>"."Recent: "."<th>"."Unread: "."<th>"."Deleted: "."<th>"."Size: "."</tr>";
  74.     echo "<tr>"."<td>".$check->Date."</td>"."<td>".$check->Driver."</td>"."<td>".$check->Mailbox."</td>"."<td>".$check->Nmsgs."</td>"."<td>".$check->Recent."</td>"."<td>".$check->Unread."</td>"."<td>".$check->Deleted."</td>"."<td>".$check->Size."</td>"."</tr>";
  75. } else {
  76.     echo "imap_mailboxmsginfo() failed: " . imap_last_error() . "<br />\n";
  77. }?>
  78.      <!--Pradedam lentele kurioje bus dedama informacija laisku-->  
  79.        
  80.   <table width="100%" border="0" cellspacing="0" cellpadding="0">  
  81.       <br>
  82.   <tr>
  83.       <td><h1>ID</h1></td>
  84.       <td><h1>Siuntėjas</h1></td>
  85.       <td><h1>Gavimo laikas</h1></td>
  86.       <td><h1>Tema</h1></td>
  87.       <td><h1>Turinys</h1></td>
  88.       <td><h1>Pridetas failas</h1></td>
  89.   </tr>
  90.  
  91.  
  92.  
  93.  <?php
  94. if($emails) {
  95.   //apsirasom kintamuosius, kurie bus reikalingi, kad i duomenu baze irasyt info
  96.   $siuntejas = '';
  97.   $data='';
  98.   $tema='';
  99.   $turinys='';
  100.   $output='';
  101.  
  102.  
  103.   /* Kiekvienam laiskui... */
  104.  foreach($emails as $email_number) {
  105.  
  106.     /* Laisko informacijos gavimas */
  107.    $overview = imap_fetch_overview($inbox,$email_number,0);
  108.        $message =imap_fetchbody($inbox,$email_number,1);
  109.      
  110.    
  111.     /* Gauname informacija */
  112.        
  113.     $siuntejas= $overview[0]->from;
  114.     $sena_data=$overview[0]->date; // Data blogu formatu
  115.     $tema=$overview[0]->subject;
  116.     $message;
  117.     $convert = strtotime($sena_data)+3600; // Konvertuojam data i normalu formata
  118.     $data =date('Y-m-d G:i:s', $convert);// Isgaunam geresnio formato data
  119.    
  120.     //Naikinam base64 encodinga...
  121.     //$turinys = imap_fetchstructure($inbox,$email_number);
  122.     //if ($turinys->encoding==3) //3 = BASE64 encoding
  123.       //  $message = base64_decode($message);
  124.    
  125.    
  126.     $turinys= $message;
  127.  
  128.  
  129.     $turinys=strip_tags(html_entity_decode($turinys));//Reikalingas, kad
  130.    //istrintu html,xml is gauto emailo
  131.    
  132.     $turinys = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $turinys);//reikalingas, kad pasalintu
  133.    //visus tarpus
  134.     echo $turinys;
  135.     $siuntejas = preg_replace("/[^@. \w]+/", "", $siuntejas); //Dabartinis budas
  136.     $siuntejas=str_replace('windows1257Q', '', $siuntejas);//sutvarkyti gaunamu
  137.     $siuntejas=str_replace('UTF8Q', '', $siuntejas);//adresatu vardus
  138.     $siuntejas=str_replace('*lt', '', $siuntejas);  
  139.    
  140.    
  141.     $data2=date('Y-m-d-H-i-s',$convert);
  142.         //Issaugom kiekviena e-mail'a i folderi , nes po to istrinsim
  143.     $headers = imap_fetchheader($inbox, $email_number, FT_PREFETCHTEXT);
  144.    //$headers = strstr($headers,"From:");
  145.    $headers = preg_replace('/^(?!From:)(?!To:)(?!Subject:)(?!Date:)(?!X-MS-Has-Attach:).*?\n/m','',$headers);
  146.     $body = imap_body($inbox, $email_number);
  147.    file_put_contents('./test/'.($siuntejas.'-'.$data2), $headers . "\n" . $body);
  148.    
  149.      
  150.  
  151.    //Pradedam isgauti pridetus failus
  152.  $structure = imap_fetchstructure($inbox, $email_number);
  153.  
  154.         $attachments = array();
  155.  
  156.         /* Jei rastas pridetas failas.. */
  157.         if(isset($structure->parts) && count($structure->parts))
  158.         {
  159.             for($i = 0; $i < count($structure->parts); $i++)
  160.             {
  161.                 $attachments[$i] = array(
  162.                     'is_attachment' => false,
  163.                     'filename' => '',
  164.                     'name' => '',
  165.                     'attachment' => ''
  166.                 );
  167.  
  168.                 if($structure->parts[$i]->ifdparameters)
  169.                 {
  170.                     foreach($structure->parts[$i]->dparameters as $object)
  171.                     {
  172.                         if(strtolower($object->attribute) == 'filename')
  173.                         {
  174.                             $attachments[$i]['is_attachment'] = true;
  175.                             $attachments[$i]['filename'] = $object->value;
  176.                         }
  177.                     }
  178.                 }
  179.  
  180.                 if($structure->parts[$i]->ifparameters)
  181.                 {
  182.                     foreach($structure->parts[$i]->parameters as $object)
  183.                     {
  184.                         if(strtolower($object->attribute) == 'name')
  185.                         {
  186.                             $attachments[$i]['is_attachment'] = true;
  187.                             $attachments[$i]['name'] = $object->value;
  188.                         }
  189.                     }
  190.                 }
  191.  
  192.                 if($attachments[$i]['is_attachment'])
  193.                 {
  194.                     $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
  195.  
  196.                     /* 3 = BASE64 encoding */
  197.                     if($structure->parts[$i]->encoding == 3)
  198.                     {
  199.                         $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  200.                     }
  201.                     /* 4 = QUOTED-PRINTABLE encoding */
  202.                     elseif($structure->parts[$i]->encoding == 4)
  203.                     {
  204.                         $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
  205.                     }
  206.                 }
  207.             }
  208.         }else{$attlink='Nera prideto failo';}
  209.  
  210.         /* Pereinam per kiekviena prideta faila ir ji issaugom */
  211.         foreach($attachments as $attachment)
  212.         {
  213.             if($attachment['is_attachment'] == 1)
  214.             {
  215.                 $filename = $attachment['name'];
  216.                 if(empty($filename)) $filename = $attachment['filename'];
  217.  
  218.                 if(empty($filename)) $filename = time() . ".dat";
  219.                 $folder = "test";
  220.                 if(!is_dir($folder))
  221.                 {
  222.                      mkdir($folder);
  223.                 }
  224.                
  225.                 $fp = fopen("./". $folder ."/". $email_number . "-" . $filename, "w+");
  226.                 fwrite($fp, $attachment['attachment']);
  227.                 fclose($fp);
  228.                 $attlink=("./".$folder."/".$email_number."-".$filename);
  229.                
  230.             }
  231.            
  232.         }
  233.  
  234.  
  235.      //Pradedam irasyneti i duomenu baze
  236.   $sql = "INSERT INTO Laiskai (Siuntejas,Gavimo_laikas,Tema,Turinys,Pridetas_failas)
  237.              VALUES ('$siuntejas','$data','$tema','$turinys','$attlink')";
  238.      if (mysqli_query($conn, $sql)) {
  239.          //echo "New record created successfully";
  240.               } else {
  241.          echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  242.          }
  243.     imap_delete($inbox, $email_number); //Nustatom , kad istrintu emailus visus
  244. }
  245. }else{echo 'Nera nauju laisku';}
  246.  
  247.  
  248.  
  249.  
  250.  
  251. //Traukiam informacija is duomenu bazes
  252.     $sql = "SELECT * FROM Laiskai ORDER BY Gavimo_laikas DESC";
  253. $result = $conn->query($sql);
  254.  
  255. if ($result->num_rows > 0) {
  256.     // output data of each row
  257.     while($row = $result->fetch_assoc()){  
  258.         $url = $row["Pridetas_failas"];
  259.         echo '<tr>'.'<th>'.$row["ID"].'</th>'.'<th>'.$row["Siuntejas"].'</th>'.'<th>'.$row["Gavimo_laikas"].'</th>'.'<th>'.$row["Tema"].'</th>'.'<th>'.$row["Turinys"].'</th>'.'<th>'."<a href='".$url."'>".$url."<a/>".'<a/>'.'</th>'.'</tr>';                      
  260.     }
  261.    
  262. } else {
  263.     echo '<h1>'."Nėra laišku".'</h1>'.'<br/>'; //Jei nera emailu, arba nauju
  264. }
  265. /* Uzdarom prisijungimus */
  266. mysqli_close($conn);
  267. //imap_expunge($inbox); //Trinam visus emailus
  268. imap_close($inbox);
  269.  
  270. ?>
  271.  
  272. </table>
  273. </body>
  274. </html>
Add Comment
Please, Sign In to add comment