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