c9ee2746292c2b40155663b73ffd4c075ad65cce
[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, $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         if (is_a($parts,'PEAR_Error')) {
114             echo $parts->toString() . "\n";
115             exit;
116         }
117     
118         $headers = $parts[1];
119         $from = new Mail_RFC822();
120         $from_str = $from->parseAddressList($headers['From']);
121         if (is_a($from_str,'PEAR_Error')) {
122             echo $from_str->toString() . "\n";
123             exit;
124         }
125
126         
127         $from_name  = trim($from_str[0]->personal, '"');
128         
129         $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host;
130         
131         
132         if (!empty($opts['use-file'])) {
133             $parts[2] = '';
134         }
135         
136         
137         if ($cm->id) {
138             
139             $cc =clone($cm);
140             $cm->setFrom(array(
141                'bodytext'      => !empty($opts['use-file']) ? '' : $parts[2],
142                'updated_dt'     => date('Y-m-d H:i:s'),
143                'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
144             ));
145             
146             $cm->update($cc);
147         } else {
148             
149             $cm->setFrom(array(
150                 'from_name'     => $from_name,
151                 'from_email'    => $from_email,
152                 'subject'       => $headers['Subject'],
153                 'name'          => $template_name,
154                 'bodytext'      => !empty($opts['use-file']) ? '' : $parts[2],
155                 'updated_dt'     => date('Y-m-d H:i:s'),
156                 'created_dt'     => date('Y-m-d H:i:s'),
157                 'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
158             ));
159             
160             $cm->insert();
161         }
162         return $cm;
163     }
164     function output() {
165         die("done\n");
166     }
167 }