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