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            // $host = parse_url($_REQUEST['importUrl']);
64 //            if($host['host'] != 'localhost' && $host['host'] != 'roojs-edward.com' && $host['host'] != $_SERVER['HTTP_HOST'])
65 //            {
66 //                $this->jerr('Invalid URL!');
67 //            }
68             $this->checkHeader($_REQUEST['importUrl']);
69             $data = $this->convertStyle($_REQUEST['importUrl'], '', true);
70          //   print_r($data);exit;
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        // print_r($htmlFile);
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         unlink($htmlFile->getStoreName()) or die('Unable to delete the file');
96         
97         $this->jok($data);
98     }
99     
100 }