7cb312197e9497b900de27b526cf7545da1f098a
[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     );
42     
43     function getAuth()
44     {
45         $ff = HTML_FlexyFramework::get();
46         
47         if (!$ff->cli) {
48             die("cli only");
49         }
50     }
51     
52     function get($part = '', $opts=array()) {
53         $this->updateOrCreateEmail($part, $opts, false);
54     }
55
56     function updateOrCreateEmail($part='', $opts, $cm = false){
57         
58        // DB_DataObject::debugLevel(1);
59         
60         $template_name = preg_replace('/\.[a-z]+$/i', '', basename($opts['file']));
61         
62         if (!file_exists($opts['file'])) {
63             $this->jerr("file does not exist : " . $opts['file']);
64         }
65         
66         
67         if (!empty($opts['master']) && !file_exists($opts['master'])) {
68             $this->jerr("master file does not exist : " . $opts['master']);
69         }
70         
71         
72         if (empty($cm)) {
73             $cm = DB_dataObject::factory('core_email');
74             $ret = $cm->get('name',$template_name);
75             if($ret && empty($opts['update'])) {
76                 $this->jerr("use --update   to update the template..");
77             }
78         }
79         $mailtext = file_get_contents($opts['file']);
80         
81         if (!empty($opts['master'])) {
82             $body = $mailtext;
83             $mailtext = file_get_contents($opts['master']);
84             $mailtext = str_replace('{outputBody():h}', $body, $mailtext);
85         }
86         
87         require_once 'Mail/mimeDecode.php';
88         require_once 'Mail/RFC822.php';
89         
90         $decoder = new Mail_mimeDecode($mailtext);
91         $parts = $decoder->getSendArray();
92         if (is_a($parts,'PEAR_Error')) {
93             echo $parts->toString() . "\n";
94             exit;
95         }
96     
97         $headers = $parts[1];
98         $from = new Mail_RFC822();
99         $from_str = $from->parseAddressList($headers['From']);
100         
101         $from_name  = trim($from_str[0]->personal, '"');
102         
103         $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host;
104         
105         
106         if (!empty($opts['use-file'])) {
107             $parts[2] = '';
108         }
109         
110         
111         if ($cm->id) {
112             
113             $cc =clone($cm);
114             $cm->setFrom(array(
115                'bodytext'      => !empty($opts['use-file']) ? '' : $parts[2],
116                'updated_dt'     => date('Y-m-d H:i:s'),
117                'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
118             ));
119             
120             $cm->update($cc);
121         } else {
122             
123             $cm->setFrom(array(
124                 'from_name'     => $from_name,
125                 'from_email'    => $from_email,
126                 'subject'       => $headers['Subject'],
127                 'name'          => $template_name,
128                 'bodytext'      => !empty($opts['use-file']) ? '' : $parts[2],
129                 'updated_dt'     => date('Y-m-d H:i:s'),
130                 'created_dt'     => date('Y-m-d H:i:s'),
131                 'use_file' => !empty($opts['use-file']) ? realpath($opts['file']) : '',
132             ));
133             
134             $cm->insert();
135         }
136         return $cm;
137     }
138     function output() {
139         die("done\n");
140     }
141 }