ImportMailMessage.php
[Pman.Core] / ImportMailMessage.php
1 <?php
2
3 // should be in import folder... need to know where this is used though...
4
5 require_once 'ConvertStyle.php';
6
7 class Pman_Core_ImportMailMessage extends Pman_Core_ConvertStyle 
8 {
9     function getAuth()
10     {
11         
12         if (HTML_FlexyFramework::get()->cli) {
13             return true;
14         }
15         
16         
17         $this->authUser = $this->getAuthUser();
18         if (!$this->authUser) {
19             return false;
20         }
21         
22         var_dump('die');exit;
23         
24         return true;
25         
26     }
27     
28     function get($v, $opts=array())
29     {
30         $this->post();
31         
32         return $this->jerr("not allowed");
33     }
34     
35     function post($v)
36     {   
37         if(isset($_REQUEST['_convertToPlain']))
38         {
39             require_once 'System.php';
40             $tmpdir  = System::mktemp("-d convertPlain");
41             $path = $tmpdir . '/' . time() . '.html';
42             
43             if(isset($_REQUEST['_check_unsubscribe'])){
44                 libxml_use_internal_errors (true);
45                 $doc = new DOMDocument('1.0', 'UTF-8');
46                 $doc->loadHTML($_REQUEST['bodytext']);
47                 $xpath = new DOMXpath($doc);
48                 foreach ($xpath->query('//a[@href]') as $a) { 
49                     $href = $a->getAttribute('href');
50                     
51                     if(!preg_match('/^#unsubscribe/', $href)){
52                         continue;
53                     }
54                     $a->parentNode->replaceChild($doc->createTextNode($a->nodeValue . ' {unsubscribe_link}'), $a);
55                 }
56                 
57                 $_REQUEST['bodytext'] = $doc->saveHTML();
58                 libxml_use_internal_errors (false);
59             }
60             
61             if(!file_exists($path)){
62                file_put_contents($path, $_REQUEST['bodytext']); 
63             }
64             require_once 'File/Convert.php';
65             $fc = new File_Convert($path, 'text/html');
66             
67             $plain = $fc->convert('text/plain');
68             $this->jok(file_get_contents($plain));
69         }
70         
71         // Import from URL
72         if(isset($_REQUEST['importUrl']))
73         {
74             $this->checkHeader($_REQUEST['importUrl']);
75             $data = $this->convertStyle($_REQUEST['importUrl'], '', true);
76          
77             $this->jok($data);
78             
79         }
80      
81         // Import from file
82         $htmlFile = DB_DataObject::factory('images');
83         $htmlFile->setFrom(array(
84                'onid' => 0,
85                'ontable' =>'crm_mailing_list_message'
86         ));
87         $htmlFile->onUpload(false);
88        
89         if($htmlFile->mimetype != 'text/html')
90         {
91             $this->jerr('accept html file only!');
92         }
93         if(!file_exists($htmlFile->getStoreName()))
94         {
95             $this->jerr('update failed!');
96         }
97         
98         $data = $this->convertStyle('', $htmlFile->getStoreName(), false);
99         
100         $htmlFile->delete();
101         
102         unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
103         
104         $this->jok($data);
105     }
106     
107 }