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