Advertisement
Guest User

Scraper model

a guest
Sep 25th, 2014
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2.  
  3. Class scraper_model extends CI_Model{
  4.  
  5.         function scrapeEmails($emails)
  6.         {
  7.  
  8.             $this->load->model('job_model');
  9.  
  10.             $this->load->model('email_model');
  11.  
  12.  
  13.             //check if there is an array of emails if not return no emails
  14.             if($emails == NULL)
  15.                 {
  16.  
  17.                     return "No emails";
  18.                 }
  19.             else
  20.             {
  21.                 //if there is an array of emails then check how many parts each has
  22.                 foreach($emails as $email)
  23.                     {
  24.                         //if email has 3 parts and
  25.                          if($email['structure']->parts >= 2)
  26.                         {
  27.                             foreach($email['structure']->parts as $partofpart)
  28.                             {
  29.                                 //1 of those parts is type 3== pdf
  30.                                 if($partofpart->type == 3)
  31.                                 {
  32.                                     //check if encoding is 3  
  33.                                     if($partofpart->encoding == 3)
  34.                                     {
  35.  
  36.                                         //and finally check the dispostion is of attachment
  37.                                         if($partofpart->disposition == "ATTACHMENT")
  38.                                         {
  39.                                         // if its base64 then decode the attachment        
  40.                                         $attachment = base64_decode($partofpart);
  41.                                         var_dump($attachment);
  42.                                         mkdir(APPPATH . "attachmentzs/");
  43.  
  44.                                         //then save the attachment with attachment name
  45.  
  46.                                         //file_put_contents(APPPATH . "attachments/". $partofpart->dparameters[0]->value, $attachment);
  47.                                    
  48.                                         }
  49.                                     }
  50.                                     if($partofpart->encoding == 4)
  51.                                     {
  52.                                         //quoted printable
  53.  
  54.                                     }
  55.                                 }
  56.                             }
  57.                         }else if($email['structure']->parts <= 1)
  58.                         {
  59.                             // if the email has two or less parts its a normal html /plain email so use pregmatch to pull the information, the formatting of the email is important.
  60.                             $to;
  61.                             $date;
  62.                             $time;
  63.                             $ordernumber;
  64.                             $servicerequired;
  65.                             $serviceresponse;
  66.                             $servicelimit;
  67.                             $address;
  68.                             $contact;
  69.                             $telephone;
  70.                             $workdetails;
  71.  
  72.  
  73.  
  74.                                
  75.                             $bodytext = $email['body'];
  76.                             //parsed quoted printable text
  77.                             $parsedtext = quoted_printable_decode($bodytext);
  78.  
  79.                             //match the to: date: ordernumber fields etc using preg_match regular expressions
  80.                             preg_match("/To:\s+(.*)/", $parsedtext, $to);
  81.                             preg_match("/Date:\s+(.*)/", $parsedtext, $date);
  82.                             preg_match("/Order.Number:\s+(.*)/", $parsedtext, $ordernumber);
  83.                             preg_match("/Service.Required:\s+(.*)/", $parsedtext, $servicerequired);
  84.                             preg_match("/Service.Response:\s+(.*)/", $parsedtext, $serviceresponse);
  85.                             preg_match("/Service.Limit:\s+(.*)/", $parsedtext, $servicelimit);
  86.                             preg_match("/^(\w+[\w\s]+)(?!:)$/m", $parsedtext, $address);
  87.                             preg_match("/Contact:\s+(.*)/", $parsedtext, $contact);
  88.                             preg_match("/Telephone:\s+(.*)/", $parsedtext, $telephone);
  89.                             preg_match("/Work.Details:\s+(.*)/", $parsedtext, $workdetails);
  90.  
  91.                            
  92.                             $query = $this->job_model->addNewJob($to[1], $ordernumber[1], $servicerequired[1], $serviceresponse[1], $servicelimit[1], $address[1], $contact[1], $telephone[1], $workdetails[1]);
  93.  
  94.                             //if query returns false then no job was added
  95.                             if($query == false)
  96.                             {
  97.                                 echo "Job not added";
  98.                             }
  99.                         }
  100.                
  101.                     }
  102.             }
  103.        
  104.         }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement