fix #8131 - chinese translations
[Pman.Core] / Import / Core_email.php
1 <?php
2
3
4
5 require_once 'Pman.php';
6
7 class Pman_Core_Import_Core_email extends Pman 
8 {
9     static $cli_desc = "Import an email into core_email template"; 
10     
11     static $cli_opts = array(
12         'file' => array(
13             'desc' => 'File to import',
14             'short' => 'f',
15             'min' => 1,
16             'max' => 1,
17             
18         ),
19         'master' => array(
20             'desc' => 'Master template (wrapper to body)',
21             'short' => 'm',
22             'default' => '',
23             'min' => 1,
24             'max' => 1,
25             
26         ),
27         'update' => array(
28             'desc' => 'Update template (deletes old version?)',
29             'short' => 'u',
30             'default' => '',
31             'min' => 0,
32             'max' => 0,  
33         ),
34          'use-file' => array(
35             'desc' => 'Force usage of file (so content is not editable in Management system)',
36             'short' => 'F',
37             'default' => '',
38             'min' => 0,
39             'max' => 0,  
40         ),
41         'raw_content' => array(
42             'desc' => 'Raw contents of email (used by API) - not by Command line',
43             'short' => 'R',
44             'default' => '',
45             'min' => 0,
46             'max' => 0,  
47         )
48          
49     );
50     
51     function getAuth()
52     {
53         $ff = HTML_FlexyFramework::get();
54         
55         if (!$ff->cli) {
56             die("cli only");
57         }
58     }
59     
60     function get($part = '', $opts=array())
61     {
62         $this->updateOrCreateEmail($part, $opts, false);
63     }
64
65     function updateOrCreateEmail($part='', $opts= array(), $cm = false, $mapping = false){
66         
67        // DB_DataObject::debugLevel(1);
68         
69         
70         if (empty($opts['raw_content'])) {
71             $template_name = preg_replace('/\.[a-z]+$/i', '', basename($opts['file']));
72
73             if (!file_exists($opts['file'])) {
74                 $this->jerr("file does not exist : " . $opts['file']);
75             }
76             
77             
78             if (!empty($opts['master']) && !file_exists($opts['master'])) {
79                 $this->jerr("master file does not exist : " . $opts['master']);
80             }
81             
82             
83             if (empty($cm)) {
84                 $cm = DB_dataObject::factory('core_email');
85                 $ret = $cm->get('name',$template_name);
86                 if($ret && empty($opts['update'])) {
87                     $this->jerr("use --update   to update the template..");
88                 }
89             }
90             $mailtext = file_get_contents($opts['file']);
91         } else {
92             $template_name = $opts['name'];
93             $mailtext =  $opts['raw_content'];
94         }
95         
96         if (!empty($opts['master'])) {
97             $body = $mailtext;
98             $mailtext = file_get_contents($opts['master']);
99             $mailtext = str_replace('{outputBody():h}', $body, $mailtext);
100         }
101         
102         if($mapping) {
103             foreach ($mapping as $k => $v) {
104                 $mailtext = str_replace($k, $v, $mailtext);
105             }
106         }
107         
108         require_once 'Mail/mimeDecode.php';
109         require_once 'Mail/RFC822.php';
110         
111         $decoder = new Mail_mimeDecode($mailtext);
112         $parts = $decoder->getSendArray();
113         $structure = $decoder->decode(array(
114             'include_bodies' => true,
115             'decode_bodies' => true
116         ));
117         if (is_a($parts,'PEAR_Error')) {
118             echo $parts->toString() . "\n";
119             exit;
120         }
121     
122         $headers = $parts[1];
123         $from = new Mail_RFC822();
124         $from_str = $from->parseAddressList($headers['From']);
125         if (is_a($from_str,'PEAR_Error')) {
126             echo $from_str->toString() . "\n";
127             exit;
128         }
129
130         
131         $from_name  = trim($from_str[0]->personal, '"');
132         
133         $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host;
134         
135         
136         $bodyhtml  = '';
137         $bodytext  = '';
138         if (empty($opts['use-file'])) {
139             
140             switch($structure->ctype_primary .'/'. $structure->ctype_secondary ) {
141                 case 'multipart/alternative':
142                     foreach($structure->parts as $p) {
143                         switch($p->ctype_primary .'/'. $p->ctype_secondary ) {
144                             case 'text/plain':
145                                 $bodytext = $p->body;
146                                 break;
147                         
148                             case 'text/html':
149                                 $bodyhtml = $p->body;
150                                 break;
151                                  // no default...
152                         }
153                             
154                     }
155                     break;
156                 case 'text/plain':
157                     $bodytext = $parts[2];
158                     break;
159                 
160                 case 'text/html':
161                 
162                     $bodyhtml = $parts[2];
163                     break;
164                 default:
165                     var_dump($structure->ctype_primary .'/'. $structure->ctype_secondary );
166                     die("UNKNOWN TYPE");
167             }
168             
169           
170         }
171         
172         
173         
174         
175         if ($cm->id) {
176             
177             $cc =clone($cm);
178             $cm->setFrom(array(
179                'bodytext'      => $bodyhtml,
180                'plaintext' => $bodytext,
181                'updated_dt'     => date('Y-m-d H:i:s'),
182                'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
183             ));
184             
185             $cm->update($cc);
186         } else {
187             
188             $cm->setFrom(array(
189                 'from_name'     => $from_name,
190                 'from_email'    => $from_email,
191                 'subject'       => $headers['Subject'],
192                 'name'          => $template_name,
193                 'bodytext'      => $bodyhtml,
194                 'plaintext'     => $bodytext,
195                 'updated_dt'     => date('Y-m-d H:i:s'),
196                 'created_dt'     => date('Y-m-d H:i:s'),
197                 'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
198             ));
199             
200             $cm->insert();
201         }
202         return $cm;
203     }
204     function output() {
205         die("done\n");
206     }
207 }