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