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