annotate plugins/enigma/lib/enigma_engine.php @ 0:1e000243b222

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:50:29 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1 <?php
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4 +-------------------------------------------------------------------------+
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
5 | Engine of the Enigma Plugin |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
6 | |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
7 | Copyright (C) 2010-2016 The Roundcube Dev Team |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
8 | |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
9 | Licensed under the GNU General Public License version 3 or |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
10 | any later version with exceptions for skins & plugins. |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
11 | See the README file for a full license statement. |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
12 | |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
13 +-------------------------------------------------------------------------+
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
14 | Author: Aleksander Machniak <alec@alec.pl> |
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
15 +-------------------------------------------------------------------------+
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
16 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
17
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
18 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
19 * Enigma plugin engine.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
20 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
21 * RFC2440: OpenPGP Message Format
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
22 * RFC3156: MIME Security with OpenPGP
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
23 * RFC3851: S/MIME
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
24 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
25 class enigma_engine
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
26 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
27 private $rc;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
28 private $enigma;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
29 private $pgp_driver;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
30 private $smime_driver;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
31 private $password_time;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
32
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
33 public $decryptions = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
34 public $signatures = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
35 public $encrypted_parts = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
36
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
37 const ENCRYPTED_PARTIALLY = 100;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
38
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
39 const SIGN_MODE_BODY = 1;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
40 const SIGN_MODE_SEPARATE = 2;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
41 const SIGN_MODE_MIME = 4;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
42
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
43 const ENCRYPT_MODE_BODY = 1;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
44 const ENCRYPT_MODE_MIME = 2;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
45 const ENCRYPT_MODE_SIGN = 4;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
46
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
47
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
48 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
49 * Plugin initialization.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
50 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
51 function __construct($enigma)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
52 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
53 $this->rc = rcmail::get_instance();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
54 $this->enigma = $enigma;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
55
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
56 $this->password_time = $this->rc->config->get('enigma_password_time') * 60;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
57
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
58 // this will remove passwords from session after some time
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
59 if ($this->password_time) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
60 $this->get_passwords();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
61 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
62 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
63
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
64 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
65 * PGP driver initialization.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
66 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
67 function load_pgp_driver()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
68 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
69 if ($this->pgp_driver) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
70 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
71 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
72
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
73 $driver = 'enigma_driver_' . $this->rc->config->get('enigma_pgp_driver', 'gnupg');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
74 $username = $this->rc->user->get_username();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
75
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
76 // Load driver
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
77 $this->pgp_driver = new $driver($username);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
78
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
79 if (!$this->pgp_driver) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
80 rcube::raise_error(array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
81 'code' => 600, 'type' => 'php',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
82 'file' => __FILE__, 'line' => __LINE__,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
83 'message' => "Enigma plugin: Unable to load PGP driver: $driver"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
84 ), true, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
85 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
86
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
87 // Initialise driver
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
88 $result = $this->pgp_driver->init();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
89
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
90 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
91 self::raise_error($result, __LINE__, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
92 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
93 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
94
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
95 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
96 * S/MIME driver initialization.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
97 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
98 function load_smime_driver()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
99 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
100 if ($this->smime_driver) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
101 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
102 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
103
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
104 $driver = 'enigma_driver_' . $this->rc->config->get('enigma_smime_driver', 'phpssl');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
105 $username = $this->rc->user->get_username();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
106
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
107 // Load driver
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
108 $this->smime_driver = new $driver($username);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
109
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
110 if (!$this->smime_driver) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
111 rcube::raise_error(array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
112 'code' => 600, 'type' => 'php',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
113 'file' => __FILE__, 'line' => __LINE__,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
114 'message' => "Enigma plugin: Unable to load S/MIME driver: $driver"
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
115 ), true, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
116 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
117
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
118 // Initialise driver
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
119 $result = $this->smime_driver->init();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
120
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
121 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
122 self::raise_error($result, __LINE__, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
123 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
124 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
125
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
126 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
127 * Handler for message signing
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
128 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
129 * @param Mail_mime Original message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
130 * @param int Encryption mode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
131 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
132 * @return enigma_error On error returns error object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
133 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
134 function sign_message(&$message, $mode = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
135 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
136 $mime = new enigma_mime_message($message, enigma_mime_message::PGP_SIGNED);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
137 $from = $mime->getFromAddress();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
138
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
139 // find private key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
140 $key = $this->find_key($from, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
141
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
142 if (empty($key)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
143 return new enigma_error(enigma_error::KEYNOTFOUND);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
144 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
145
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
146 // check if we have password for this key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
147 $passwords = $this->get_passwords();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
148 $pass = $passwords[$key->id];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
149
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
150 if ($pass === null) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
151 // ask for password
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
152 $error = array('missing' => array($key->id => $key->name));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
153 return new enigma_error(enigma_error::BADPASS, '', $error);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
154 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
155
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
156 $key->password = $pass;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
157
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
158 // select mode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
159 switch ($mode) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
160 case self::SIGN_MODE_BODY:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
161 $pgp_mode = Crypt_GPG::SIGN_MODE_CLEAR;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
162 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
163
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
164 case self::SIGN_MODE_MIME:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
165 $pgp_mode = Crypt_GPG::SIGN_MODE_DETACHED;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
166 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
167
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
168 default:
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
169 if ($mime->isMultipart()) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
170 $pgp_mode = Crypt_GPG::SIGN_MODE_DETACHED;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
171 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
172 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
173 $pgp_mode = Crypt_GPG::SIGN_MODE_CLEAR;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
174 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
175 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
176
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
177 // get message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
178 if ($pgp_mode == Crypt_GPG::SIGN_MODE_CLEAR) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
179 // in this mode we'll replace text part
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
180 // with the one containing signature
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
181 $body = $message->getTXTBody();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
182
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
183 $text_charset = $message->getParam('text_charset');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
184 $line_length = $this->rc->config->get('line_length', 72);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
185
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
186 // We can't use format=flowed for signed messages
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
187 if (strpos($text_charset, 'format=flowed')) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
188 list($charset, $params) = explode(';', $text_charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
189 $body = rcube_mime::unfold_flowed($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
190 $body = rcube_mime::wordwrap($body, $line_length, "\r\n", false, $charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
191
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
192 $text_charset = str_replace(";\r\n format=flowed", '', $text_charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
193 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
194 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
195 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
196 // here we'll build PGP/MIME message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
197 $body = $mime->getOrigBody();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
198 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
199
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
200 // sign the body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
201 $result = $this->pgp_sign($body, $key, $pgp_mode);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
202
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
203 if ($result !== true) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
204 if ($result->getCode() == enigma_error::BADPASS) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
205 // ask for password
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
206 $error = array('bad' => array($key->id => $key->name));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
207 return new enigma_error(enigma_error::BADPASS, '', $error);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
208 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
209
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
210 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
211 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
212
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
213 // replace message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
214 if ($pgp_mode == Crypt_GPG::SIGN_MODE_CLEAR) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
215 $message->setTXTBody($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
216 $message->setParam('text_charset', $text_charset);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
217 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
218 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
219 $mime->addPGPSignature($body, $this->pgp_driver->signature_algorithm());
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
220 $message = $mime;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
221 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
222 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
223
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
224 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
225 * Handler for message encryption
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
226 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
227 * @param Mail_mime Original message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
228 * @param int Encryption mode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
229 * @param bool Is draft-save action - use only sender's key for encryption
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
230 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
231 * @return enigma_error On error returns error object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
232 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
233 function encrypt_message(&$message, $mode = null, $is_draft = false)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
234 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
235 $mime = new enigma_mime_message($message, enigma_mime_message::PGP_ENCRYPTED);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
236
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
237 // always use sender's key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
238 $from = $mime->getFromAddress();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
239
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
240 // check senders key for signing
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
241 if ($mode & self::ENCRYPT_MODE_SIGN) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
242 $sign_key = $this->find_key($from, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
243
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
244 if (empty($sign_key)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
245 return new enigma_error(enigma_error::KEYNOTFOUND);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
246 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
247
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
248 // check if we have password for this key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
249 $passwords = $this->get_passwords();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
250 $sign_pass = $passwords[$sign_key->id];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
251
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
252 if ($sign_pass === null) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
253 // ask for password
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
254 $error = array('missing' => array($sign_key->id => $sign_key->name));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
255 return new enigma_error(enigma_error::BADPASS, '', $error);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
256 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
257
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
258 $sign_key->password = $sign_pass;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
259 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
260
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
261 $recipients = array($from);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
262
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
263 // if it's not a draft we add all recipients' keys
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
264 if (!$is_draft) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
265 $recipients = array_merge($recipients, $mime->getRecipients());
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
266 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
267
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
268 if (empty($recipients)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
269 return new enigma_error(enigma_error::KEYNOTFOUND);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
270 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
271
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
272 $recipients = array_unique($recipients);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
273
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
274 // find recipient public keys
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
275 foreach ((array) $recipients as $email) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
276 if ($email == $from && $sign_key) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
277 $key = $sign_key;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
278 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
279 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
280 $key = $this->find_key($email);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
281 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
282
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
283 if (empty($key)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
284 return new enigma_error(enigma_error::KEYNOTFOUND, '', array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
285 'missing' => $email
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
286 ));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
287 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
288
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
289 $keys[] = $key;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
290 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
291
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
292 // select mode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
293 if ($mode & self::ENCRYPT_MODE_BODY) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
294 $encrypt_mode = $mode;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
295 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
296 else if ($mode & self::ENCRYPT_MODE_MIME) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
297 $encrypt_mode = $mode;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
298 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
299 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
300 $encrypt_mode = $mime->isMultipart() ? self::ENCRYPT_MODE_MIME : self::ENCRYPT_MODE_BODY;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
301 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
302
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
303 // get message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
304 if ($encrypt_mode == self::ENCRYPT_MODE_BODY) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
305 // in this mode we'll replace text part
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
306 // with the one containing encrypted message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
307 $body = $message->getTXTBody();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
308 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
309 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
310 // here we'll build PGP/MIME message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
311 $body = $mime->getOrigBody();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
312 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
313
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
314 // sign the body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
315 $result = $this->pgp_encrypt($body, $keys, $sign_key);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
316
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
317 if ($result !== true) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
318 if ($result->getCode() == enigma_error::BADPASS) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
319 // ask for password
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
320 $error = array('bad' => array($sign_key->id => $sign_key->name));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
321 return new enigma_error(enigma_error::BADPASS, '', $error);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
322 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
323
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
324 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
325 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
326
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
327 // replace message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
328 if ($encrypt_mode == self::ENCRYPT_MODE_BODY) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
329 $message->setTXTBody($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
330 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
331 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
332 $mime->setPGPEncryptedBody($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
333 $message = $mime;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
334 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
335 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
336
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
337 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
338 * Handler for attaching public key to a message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
339 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
340 * @param Mail_mime Original message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
341 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
342 * @return bool True on success, False on failure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
343 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
344 function attach_public_key(&$message)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
345 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
346 $headers = $message->headers();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
347 $from = rcube_mime::decode_address_list($headers['From'], 1, false, null, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
348 $from = $from[1];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
349
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
350 // find my key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
351 if ($from && ($key = $this->find_key($from))) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
352 $pubkey_armor = $this->export_key($key->id);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
353
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
354 if (!$pubkey_armor instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
355 $pubkey_name = '0x' . enigma_key::format_id($key->id) . '.asc';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
356 $message->addAttachment($pubkey_armor, 'application/pgp-keys', $pubkey_name, false, '7bit');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
357 return true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
358 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
359 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
360
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
361 return false;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
362 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
363
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
364 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
365 * Handler for message_part_structure hook.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
366 * Called for every part of the message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
367 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
368 * @param array Original parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
369 * @param string Part body (will be set if used internally)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
370 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
371 * @return array Modified parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
372 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
373 function part_structure($p, $body = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
374 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
375 if ($p['mimetype'] == 'text/plain' || $p['mimetype'] == 'application/pgp') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
376 $this->parse_plain($p, $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
377 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
378 else if ($p['mimetype'] == 'multipart/signed') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
379 $this->parse_signed($p, $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
380 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
381 else if ($p['mimetype'] == 'multipart/encrypted') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
382 $this->parse_encrypted($p);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
383 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
384 else if ($p['mimetype'] == 'application/pkcs7-mime') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
385 $this->parse_encrypted($p);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
386 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
387
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
388 return $p;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
389 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
390
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
391 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
392 * Handler for message_part_body hook.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
393 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
394 * @param array Original parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
395 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
396 * @return array Modified parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
397 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
398 function part_body($p)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
399 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
400 // encrypted attachment, see parse_plain_encrypted()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
401 if ($p['part']->need_decryption && $p['part']->body === null) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
402 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
403
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
404 $storage = $this->rc->get_storage();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
405 $body = $storage->get_message_part($p['object']->uid, $p['part']->mime_id, $p['part'], null, null, true, 0, false);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
406 $result = $this->pgp_decrypt($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
407
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
408 // @TODO: what to do on error?
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
409 if ($result === true) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
410 $p['part']->body = $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
411 $p['part']->size = strlen($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
412 $p['part']->body_modified = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
413 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
414 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
415
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
416 return $p;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
417 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
418
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
419 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
420 * Handler for plain/text message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
421 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
422 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
423 * @param string Part body (will be set if used internally)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
424 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
425 function parse_plain(&$p, $body = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
426 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
427 $part = $p['structure'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
428
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
429 // Get message body from IMAP server
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
430 if ($body === null) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
431 $body = $this->get_part_body($p['object'], $part);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
432 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
433
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
434 // In this way we can use fgets on string as on file handle
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
435 // Don't use php://temp for security (body may come from an encrypted part)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
436 $fd = fopen('php://memory', 'r+');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
437 if (!$fd) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
438 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
439 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
440
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
441 fwrite($fd, $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
442 rewind($fd);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
443
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
444 $body = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
445 $prefix = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
446 $mode = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
447 $tokens = array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
448 'BEGIN PGP SIGNED MESSAGE' => 'signed-start',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
449 'END PGP SIGNATURE' => 'signed-end',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
450 'BEGIN PGP MESSAGE' => 'encrypted-start',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
451 'END PGP MESSAGE' => 'encrypted-end',
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
452 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
453 $regexp = '/^-----(' . implode('|', array_keys($tokens)) . ')-----[\r\n]*/';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
454
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
455 while (($line = fgets($fd)) !== false) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
456 if ($line[0] === '-' && $line[4] === '-' && preg_match($regexp, $line, $m)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
457 switch ($tokens[$m[1]]) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
458 case 'signed-start':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
459 $body = $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
460 $mode = 'signed';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
461 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
462
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
463 case 'signed-end':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
464 if ($mode === 'signed') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
465 $body .= $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
466 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
467 break 2; // ignore anything after this line
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
468
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
469 case 'encrypted-start':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
470 $body = $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
471 $mode = 'encrypted';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
472 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
473
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
474 case 'encrypted-end':
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
475 if ($mode === 'encrypted') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
476 $body .= $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
477 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
478 break 2; // ignore anything after this line
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
479 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
480
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
481 continue;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
482 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
483
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
484 if ($mode === 'signed') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
485 $body .= $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
486 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
487 else if ($mode === 'encrypted') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
488 $body .= $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
489 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
490 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
491 $prefix .= $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
492 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
493 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
494
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
495 fclose($fd);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
496
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
497 if ($mode === 'signed') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
498 $this->parse_plain_signed($p, $body, $prefix);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
499 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
500 else if ($mode === 'encrypted') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
501 $this->parse_plain_encrypted($p, $body, $prefix);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
502 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
503 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
504
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
505 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
506 * Handler for multipart/signed message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
507 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
508 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
509 * @param string Part body (will be set if used internally)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
510 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
511 function parse_signed(&$p, $body = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
512 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
513 $struct = $p['structure'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
514
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
515 // S/MIME
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
516 if ($struct->parts[1] && $struct->parts[1]->mimetype == 'application/pkcs7-signature') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
517 $this->parse_smime_signed($p, $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
518 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
519 // PGP/MIME: RFC3156
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
520 // The multipart/signed body MUST consist of exactly two parts.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
521 // The first part contains the signed data in MIME canonical format,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
522 // including a set of appropriate content headers describing the data.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
523 // The second body MUST contain the PGP digital signature. It MUST be
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
524 // labeled with a content type of "application/pgp-signature".
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
525 else if (count($struct->parts) == 2
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
526 && $struct->parts[1] && $struct->parts[1]->mimetype == 'application/pgp-signature'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
527 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
528 $this->parse_pgp_signed($p, $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
529 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
530 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
531
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
532 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
533 * Handler for multipart/encrypted message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
534 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
535 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
536 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
537 function parse_encrypted(&$p)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
538 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
539 $struct = $p['structure'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
540
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
541 // S/MIME
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
542 if ($p['mimetype'] == 'application/pkcs7-mime') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
543 $this->parse_smime_encrypted($p);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
544 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
545 // PGP/MIME: RFC3156
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
546 // The multipart/encrypted MUST consist of exactly two parts. The first
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
547 // MIME body part must have a content type of "application/pgp-encrypted".
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
548 // This body contains the control information.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
549 // The second MIME body part MUST contain the actual encrypted data. It
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
550 // must be labeled with a content type of "application/octet-stream".
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
551 else if (count($struct->parts) == 2
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
552 && $struct->parts[0] && $struct->parts[0]->mimetype == 'application/pgp-encrypted'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
553 && $struct->parts[1] && $struct->parts[1]->mimetype == 'application/octet-stream'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
554 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
555 $this->parse_pgp_encrypted($p);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
556 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
557 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
558
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
559 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
560 * Handler for plain signed message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
561 * Excludes message and signature bodies and verifies signature.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
562 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
563 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
564 * @param string Message (part) body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
565 * @param string Body prefix (additional text before the encrypted block)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
566 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
567 private function parse_plain_signed(&$p, $body, $prefix = '')
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
568 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
569 if (!$this->rc->config->get('enigma_signatures', true)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
570 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
571 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
572
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
573 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
574 $part = $p['structure'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
575
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
576 // Verify signature
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
577 if ($this->rc->action == 'show' || $this->rc->action == 'preview' || $this->rc->action == 'print') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
578 $sig = $this->pgp_verify($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
579 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
580
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
581 // In this way we can use fgets on string as on file handle
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
582 // Don't use php://temp for security (body may come from an encrypted part)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
583 $fd = fopen('php://memory', 'r+');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
584 if (!$fd) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
585 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
586 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
587
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
588 fwrite($fd, $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
589 rewind($fd);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
590
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
591 $body = $part->body = null;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
592 $part->body_modified = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
593
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
594 // Extract body (and signature?)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
595 while (($line = fgets($fd, 1024)) !== false) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
596 if ($part->body === null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
597 $part->body = '';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
598 else if (preg_match('/^-----BEGIN PGP SIGNATURE-----/', $line))
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
599 break;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
600 else
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
601 $part->body .= $line;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
602 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
603
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
604 fclose($fd);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
605
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
606 // Remove "Hash" Armor Headers
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
607 $part->body = preg_replace('/^.*\r*\n\r*\n/', '', $part->body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
608 // de-Dash-Escape (RFC2440)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
609 $part->body = preg_replace('/(^|\n)- -/', '\\1-', $part->body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
610
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
611 if ($prefix) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
612 $part->body = $prefix . $part->body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
613 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
614
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
615 // Store signature data for display
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
616 if (!empty($sig)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
617 $sig->partial = !empty($prefix);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
618 $this->signatures[$part->mime_id] = $sig;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
619 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
620 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
621
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
622 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
623 * Handler for PGP/MIME signed message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
624 * Verifies signature.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
625 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
626 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
627 * @param string Part body (will be set if used internally)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
628 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
629 private function parse_pgp_signed(&$p, $body = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
630 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
631 if (!$this->rc->config->get('enigma_signatures', true)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
632 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
633 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
634
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
635 if ($this->rc->action != 'show' && $this->rc->action != 'preview' && $this->rc->action != 'print') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
636 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
637 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
638
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
639 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
640 $struct = $p['structure'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
641
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
642 $msg_part = $struct->parts[0];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
643 $sig_part = $struct->parts[1];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
644
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
645 // Get bodies
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
646 if ($body === null) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
647 if (!$struct->body_modified) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
648 $body = $this->get_part_body($p['object'], $struct);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
649 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
650 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
651
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
652 $boundary = $struct->ctype_parameters['boundary'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
653
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
654 // when it is a signed message forwarded as attachment
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
655 // ctype_parameters property will not be set
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
656 if (!$boundary && $struct->headers['content-type']
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
657 && preg_match('/boundary="?([a-zA-Z0-9\'()+_,-.\/:=?]+)"?/', $struct->headers['content-type'], $m)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
658 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
659 $boundary = $m[1];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
660 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
661
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
662 // set signed part body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
663 list($msg_body, $sig_body) = $this->explode_signed_body($body, $boundary);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
664
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
665 // Verify
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
666 if ($sig_body && $msg_body) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
667 $sig = $this->pgp_verify($msg_body, $sig_body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
668
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
669 // Store signature data for display
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
670 $this->signatures[$struct->mime_id] = $sig;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
671 $this->signatures[$msg_part->mime_id] = $sig;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
672 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
673 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
674
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
675 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
676 * Handler for S/MIME signed message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
677 * Verifies signature.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
678 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
679 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
680 * @param string Part body (will be set if used internally)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
681 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
682 private function parse_smime_signed(&$p, $body = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
683 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
684 if (!$this->rc->config->get('enigma_signatures', true)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
685 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
686 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
687
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
688 // @TODO
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
689 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
690
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
691 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
692 * Handler for plain encrypted message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
693 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
694 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
695 * @param string Message (part) body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
696 * @param string Body prefix (additional text before the encrypted block)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
697 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
698 private function parse_plain_encrypted(&$p, $body, $prefix = '')
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
699 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
700 if (!$this->rc->config->get('enigma_decryption', true)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
701 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
702 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
703
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
704 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
705 $part = $p['structure'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
706
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
707 // Decrypt
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
708 $result = $this->pgp_decrypt($body, $signature);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
709
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
710 // Store decryption status
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
711 $this->decryptions[$part->mime_id] = $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
712
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
713 // Store signature data for display
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
714 if ($signature) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
715 $this->signatures[$part->mime_id] = $signature;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
716 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
717
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
718 // find parent part ID
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
719 if (strpos($part->mime_id, '.')) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
720 $items = explode('.', $part->mime_id);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
721 array_pop($items);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
722 $parent = implode('.', $items);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
723 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
724 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
725 $parent = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
726 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
727
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
728 // Parse decrypted message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
729 if ($result === true) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
730 $part->body = $prefix . $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
731 $part->body_modified = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
732
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
733 // it maybe PGP signed inside, verify signature
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
734 $this->parse_plain($p, $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
735
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
736 // Remember it was decrypted
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
737 $this->encrypted_parts[] = $part->mime_id;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
738
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
739 // Inform the user that only a part of the body was encrypted
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
740 if ($prefix) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
741 $this->decryptions[$part->mime_id] = self::ENCRYPTED_PARTIALLY;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
742 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
743
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
744 // Encrypted plain message may contain encrypted attachments
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
745 // in such case attachments have .pgp extension and type application/octet-stream.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
746 // This is what happens when you select "Encrypt each attachment separately
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
747 // and send the message using inline PGP" in Thunderbird's Enigmail.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
748
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
749 if ($p['object']->mime_parts[$parent]) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
750 foreach ((array)$p['object']->mime_parts[$parent]->parts as $p) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
751 if ($p->disposition == 'attachment' && $p->mimetype == 'application/octet-stream'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
752 && preg_match('/^(.*)\.pgp$/i', $p->filename, $m)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
753 ) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
754 // modify filename
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
755 $p->filename = $m[1];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
756 // flag the part, it will be decrypted when needed
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
757 $p->need_decryption = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
758 // disable caching
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
759 $p->body_modified = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
760 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
761 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
762 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
763 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
764 // decryption failed, but the message may have already
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
765 // been cached with the modified parts (see above),
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
766 // let's bring the original state back
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
767 else if ($p['object']->mime_parts[$parent]) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
768 foreach ((array)$p['object']->mime_parts[$parent]->parts as $p) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
769 if ($p->need_decryption && !preg_match('/^(.*)\.pgp$/i', $p->filename, $m)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
770 // modify filename
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
771 $p->filename .= '.pgp';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
772 // flag the part, it will be decrypted when needed
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
773 unset($p->need_decryption);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
774 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
775 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
776 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
777 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
778
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
779 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
780 * Handler for PGP/MIME encrypted message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
781 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
782 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
783 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
784 private function parse_pgp_encrypted(&$p)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
785 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
786 if (!$this->rc->config->get('enigma_decryption', true)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
787 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
788 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
789
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
790 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
791
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
792 $struct = $p['structure'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
793 $part = $struct->parts[1];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
794
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
795 // Get body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
796 $body = $this->get_part_body($p['object'], $part);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
797
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
798 // Decrypt
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
799 $result = $this->pgp_decrypt($body, $signature);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
800
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
801 if ($result === true) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
802 // Parse decrypted message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
803 $struct = $this->parse_body($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
804
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
805 // Modify original message structure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
806 $this->modify_structure($p, $struct, strlen($body));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
807
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
808 // Parse the structure (there may be encrypted/signed parts inside
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
809 $this->part_structure(array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
810 'object' => $p['object'],
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
811 'structure' => $struct,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
812 'mimetype' => $struct->mimetype
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
813 ), $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
814
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
815 // Attach the decryption message to all parts
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
816 $this->decryptions[$struct->mime_id] = $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
817 foreach ((array) $struct->parts as $sp) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
818 $this->decryptions[$sp->mime_id] = $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
819 if ($signature) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
820 $this->signatures[$sp->mime_id] = $signature;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
821 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
822 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
823 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
824 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
825 $this->decryptions[$part->mime_id] = $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
826
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
827 // Make sure decryption status message will be displayed
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
828 $part->type = 'content';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
829 $p['object']->parts[] = $part;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
830
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
831 // don't show encrypted part on attachments list
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
832 // don't show "cannot display encrypted message" text
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
833 $p['abort'] = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
834 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
835 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
836
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
837 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
838 * Handler for S/MIME encrypted message.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
839 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
840 * @param array Reference to hook's parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
841 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
842 private function parse_smime_encrypted(&$p)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
843 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
844 if (!$this->rc->config->get('enigma_decryption', true)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
845 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
846 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
847
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
848 // @TODO
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
849 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
850
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
851 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
852 * PGP signature verification.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
853 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
854 * @param mixed Message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
855 * @param mixed Signature body (for MIME messages)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
856 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
857 * @return mixed enigma_signature or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
858 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
859 private function pgp_verify(&$msg_body, $sig_body = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
860 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
861 // @TODO: Handle big bodies using (temp) files
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
862 $sig = $this->pgp_driver->verify($msg_body, $sig_body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
863
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
864 if (($sig instanceof enigma_error) && $sig->getCode() != enigma_error::KEYNOTFOUND) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
865 self::raise_error($sig, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
866 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
867
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
868 return $sig;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
869 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
870
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
871 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
872 * PGP message decryption.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
873 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
874 * @param mixed &$msg_body Message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
875 * @param enigma_signature &$signature Signature verification result
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
876 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
877 * @return mixed True or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
878 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
879 private function pgp_decrypt(&$msg_body, &$signature = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
880 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
881 // @TODO: Handle big bodies using (temp) files
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
882 $keys = $this->get_passwords();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
883 $result = $this->pgp_driver->decrypt($msg_body, $keys, $signature);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
884
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
885 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
886 if ($result->getCode() != enigma_error::KEYNOTFOUND) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
887 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
888 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
889
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
890 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
891 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
892
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
893 $msg_body = $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
894
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
895 return true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
896 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
897
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
898 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
899 * PGP message signing
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
900 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
901 * @param mixed Message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
902 * @param enigma_key The key (with passphrase)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
903 * @param int Signing mode
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
904 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
905 * @return mixed True or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
906 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
907 private function pgp_sign(&$msg_body, $key, $mode = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
908 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
909 // @TODO: Handle big bodies using (temp) files
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
910 $result = $this->pgp_driver->sign($msg_body, $key, $mode);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
911
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
912 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
913 if ($result->getCode() != enigma_error::KEYNOTFOUND) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
914 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
915 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
916
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
917 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
918 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
919
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
920 $msg_body = $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
921
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
922 return true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
923 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
924
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
925 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
926 * PGP message encrypting
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
927 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
928 * @param mixed Message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
929 * @param array Keys (array of enigma_key objects)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
930 * @param string Optional signing Key ID
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
931 * @param string Optional signing Key password
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
932 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
933 * @return mixed True or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
934 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
935 private function pgp_encrypt(&$msg_body, $keys, $sign_key = null, $sign_pass = null)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
936 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
937 // @TODO: Handle big bodies using (temp) files
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
938 $result = $this->pgp_driver->encrypt($msg_body, $keys, $sign_key, $sign_pass);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
939
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
940 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
941 if ($result->getCode() != enigma_error::KEYNOTFOUND) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
942 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
943 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
944
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
945 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
946 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
947
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
948 $msg_body = $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
949
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
950 return true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
951 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
952
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
953 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
954 * PGP keys listing.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
955 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
956 * @param mixed Key ID/Name pattern
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
957 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
958 * @return mixed Array of keys or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
959 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
960 function list_keys($pattern = '')
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
961 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
962 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
963 $result = $this->pgp_driver->list_keys($pattern);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
964
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
965 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
966 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
967 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
968
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
969 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
970 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
971
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
972 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
973 * Find PGP private/public key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
974 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
975 * @param string E-mail address
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
976 * @param bool Need a key for signing?
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
977 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
978 * @return enigma_key The key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
979 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
980 function find_key($email, $can_sign = false)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
981 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
982 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
983 $result = $this->pgp_driver->list_keys($email);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
984
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
985 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
986 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
987 return;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
988 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
989
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
990 $mode = $can_sign ? enigma_key::CAN_SIGN : enigma_key::CAN_ENCRYPT;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
991
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
992 // check key validity and type
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
993 foreach ($result as $key) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
994 if ($subkey = $key->find_subkey($email, $mode)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
995 return $key;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
996 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
997 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
998 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
999
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1000 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1001 * PGP key details.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1002 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1003 * @param mixed Key ID
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1004 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1005 * @return mixed enigma_key or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1006 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1007 function get_key($keyid)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1008 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1009 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1010 $result = $this->pgp_driver->get_key($keyid);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1011
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1012 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1013 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1014 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1015
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1016 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1017 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1018
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1019 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1020 * PGP key delete.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1021 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1022 * @param string Key ID
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1023 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1024 * @return enigma_error|bool True on success
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1025 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1026 function delete_key($keyid)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1027 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1028 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1029 $result = $this->pgp_driver->delete_key($keyid);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1030
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1031 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1032 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1033 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1034
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1035 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1036 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1037
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1038 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1039 * PGP keys pair generation.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1040 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1041 * @param array Key pair parameters
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1042 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1043 * @return mixed enigma_key or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1044 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1045 function generate_key($data)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1046 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1047 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1048 $result = $this->pgp_driver->gen_key($data);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1049
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1050 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1051 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1052 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1053
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1054 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1055 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1056
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1057 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1058 * PGP keys/certs import.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1059 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1060 * @param mixed Import file name or content
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1061 * @param boolean True if first argument is a filename
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1062 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1063 * @return mixed Import status data array or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1064 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1065 function import_key($content, $isfile = false)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1066 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1067 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1068 $result = $this->pgp_driver->import($content, $isfile, $this->get_passwords());
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1069
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1070 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1071 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1072 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1073 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1074 $result['imported'] = $result['public_imported'] + $result['private_imported'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1075 $result['unchanged'] = $result['public_unchanged'] + $result['private_unchanged'];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1076 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1077
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1078 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1079 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1080
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1081 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1082 * PGP keys/certs export.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1083 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1084 * @param string Key ID
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1085 * @param resource Optional output stream
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1086 * @param bool Include private key
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1087 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1088 * @return mixed Key content or enigma_error
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1089 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1090 function export_key($key, $fp = null, $include_private = false)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1091 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1092 $this->load_pgp_driver();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1093 $result = $this->pgp_driver->export($key, $include_private, $this->get_passwords());
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1094
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1095 if ($result instanceof enigma_error) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1096 self::raise_error($result, __LINE__);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1097 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1098 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1099
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1100 if ($fp) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1101 fwrite($fp, $result);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1102 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1103 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1104 return $result;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1105 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1106 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1107
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1108 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1109 * Registers password for specified key/cert sent by the password prompt.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1110 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1111 function password_handler()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1112 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1113 $keyid = rcube_utils::get_input_value('_keyid', rcube_utils::INPUT_POST);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1114 $passwd = rcube_utils::get_input_value('_passwd', rcube_utils::INPUT_POST, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1115
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1116 if ($keyid && $passwd !== null && strlen($passwd)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1117 $this->save_password(strtoupper($keyid), $passwd);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1118 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1119 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1120
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1121 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1122 * Saves key/cert password in user session
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1123 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1124 function save_password($keyid, $password)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1125 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1126 // we store passwords in session for specified time
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1127 if ($config = $_SESSION['enigma_pass']) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1128 $config = $this->rc->decrypt($config);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1129 $config = @unserialize($config);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1130 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1131
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1132 $config[$keyid] = array($password, time());
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1133
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1134 $_SESSION['enigma_pass'] = $this->rc->encrypt(serialize($config));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1135 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1136
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1137 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1138 * Returns currently stored passwords
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1139 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1140 function get_passwords()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1141 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1142 if ($config = $_SESSION['enigma_pass']) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1143 $config = $this->rc->decrypt($config);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1144 $config = @unserialize($config);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1145 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1146
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1147 $threshold = $this->password_time ? time() - $this->password_time : 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1148 $keys = array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1149
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1150 // delete expired passwords
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1151 foreach ((array) $config as $key => $value) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1152 if ($threshold && $value[1] < $threshold) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1153 unset($config[$key]);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1154 $modified = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1155 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1156 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1157 $keys[$key] = $value[0];
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1158 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1159 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1160
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1161 if ($modified) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1162 $_SESSION['enigma_pass'] = $this->rc->encrypt(serialize($config));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1163 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1164
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1165 return $keys;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1166 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1167
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1168 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1169 * Get message part body.
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1170 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1171 * @param rcube_message Message object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1172 * @param rcube_message_part Message part
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1173 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1174 private function get_part_body($msg, $part)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1175 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1176 // @TODO: Handle big bodies using file handles
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1177
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1178 // This is a special case when we want to get the whole body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1179 // using direct IMAP access, in other cases we prefer
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1180 // rcube_message::get_part_body() as the body may be already in memory
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1181 if (!$part->mime_id) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1182 // fake the size which may be empty for multipart/* parts
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1183 // otherwise get_message_part() below will fail
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1184 if (!$part->size) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1185 $reset = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1186 $part->size = 1;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1187 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1188
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1189 $storage = $this->rc->get_storage();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1190 $body = $storage->get_message_part($msg->uid, $part->mime_id, $part,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1191 null, null, true, 0, false);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1192
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1193 if ($reset) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1194 $part->size = 0;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1195 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1196 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1197 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1198 $body = $msg->get_part_body($part->mime_id, false);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1199
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1200 // Convert charset to get rid of possible non-ascii characters (#5962)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1201 if ($part->charset && stripos($part->charset, 'ASCII') === false) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1202 $body = rcube_charset::convert($body, $part->charset, 'US-ASCII');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1203 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1204 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1205
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1206 return $body;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1207 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1208
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1209 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1210 * Parse decrypted message body into structure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1211 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1212 * @param string Message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1213 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1214 * @return array Message structure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1215 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1216 private function parse_body(&$body)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1217 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1218 // Mail_mimeDecode need \r\n end-line, but gpg may return \n
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1219 $body = preg_replace('/\r?\n/', "\r\n", $body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1220
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1221 // parse the body into structure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1222 $struct = rcube_mime::parse_message($body);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1223
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1224 return $struct;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1225 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1226
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1227 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1228 * Replace message encrypted structure with decrypted message structure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1229 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1230 * @param array Hook arguments
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1231 * @param rcube_message_part Part structure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1232 * @param int Part size
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1233 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1234 private function modify_structure(&$p, $struct, $size = 0)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1235 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1236 // modify mime_parts property of the message object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1237 $old_id = $p['structure']->mime_id;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1238
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1239 foreach (array_keys($p['object']->mime_parts) as $idx) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1240 if (!$old_id || $idx == $old_id || strpos($idx, $old_id . '.') === 0) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1241 unset($p['object']->mime_parts[$idx]);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1242 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1243 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1244
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1245 // set some part params used by Roundcube core
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1246 $struct->headers = array_merge($p['structure']->headers, $struct->headers);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1247 $struct->size = $size;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1248 $struct->filename = $p['structure']->filename;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1249
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1250 // modify the new structure to be correctly handled by Roundcube
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1251 $this->modify_structure_part($struct, $p['object'], $old_id);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1252
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1253 // replace old structure with the new one
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1254 $p['structure'] = $struct;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1255 $p['mimetype'] = $struct->mimetype;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1256 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1257
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1258 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1259 * Modify decrypted message part
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1260 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1261 * @param rcube_message_part
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1262 * @param rcube_message
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1263 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1264 private function modify_structure_part($part, $msg, $old_id)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1265 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1266 // never cache the body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1267 $part->body_modified = true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1268 $part->encoding = 'stream';
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1269
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1270 // modify part identifier
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1271 if ($old_id) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1272 $part->mime_id = !$part->mime_id ? $old_id : ($old_id . '.' . $part->mime_id);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1273 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1274
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1275 // Cache the fact it was decrypted
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1276 $this->encrypted_parts[] = $part->mime_id;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1277 $msg->mime_parts[$part->mime_id] = $part;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1278
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1279 // modify sub-parts
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1280 foreach ((array) $part->parts as $p) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1281 $this->modify_structure_part($p, $msg, $old_id);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1282 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1283 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1284
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1285 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1286 * Extracts body and signature of multipart/signed message body
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1287 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1288 private function explode_signed_body($body, $boundary)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1289 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1290 if (!$body) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1291 return array();
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1292 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1293
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1294 $boundary = '--' . $boundary;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1295 $boundary_len = strlen($boundary) + 2;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1296
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1297 // Find boundaries
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1298 $start = strpos($body, $boundary) + $boundary_len;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1299 $end = strpos($body, $boundary, $start);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1300
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1301 // Get signed body and signature
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1302 $sig = substr($body, $end + $boundary_len);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1303 $body = substr($body, $start, $end - $start - 2);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1304
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1305 // Cleanup signature
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1306 $sig = substr($sig, strpos($sig, "\r\n\r\n") + 4);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1307 $sig = substr($sig, 0, strpos($sig, $boundary));
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1308
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1309 return array($body, $sig);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1310 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1311
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1312 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1313 * Checks if specified message part is a PGP-key or S/MIME cert data
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1314 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1315 * @param rcube_message_part Part object
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1316 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1317 * @return boolean True if part is a key/cert
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1318 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1319 public function is_keys_part($part)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1320 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1321 // @TODO: S/MIME
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1322 return (
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1323 // Content-Type: application/pgp-keys
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1324 $part->mimetype == 'application/pgp-keys'
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1325 );
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1326 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1327
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1328 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1329 * Removes all user keys and assigned data
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1330 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1331 * @param string Username
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1332 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1333 * @return bool True on success, False on failure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1334 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1335 public function delete_user_data($username)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1336 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1337 $homedir = $this->rc->config->get('enigma_pgp_homedir', INSTALL_PATH . 'plugins/enigma/home');
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1338 $homedir .= DIRECTORY_SEPARATOR . $username;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1339
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1340 return file_exists($homedir) ? self::delete_dir($homedir) : true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1341 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1342
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1343 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1344 * Recursive method to remove directory with its content
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1345 *
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1346 * @param string Directory
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1347 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1348 public static function delete_dir($dir)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1349 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1350 // This code can be executed from command line, make sure
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1351 // we have permissions to delete keys directory
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1352 if (!is_writable($dir)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1353 rcube::raise_error("Unable to delete $dir", false, true);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1354 return false;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1355 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1356
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1357 if ($content = scandir($dir)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1358 foreach ($content as $filename) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1359 if ($filename != '.' && $filename != '..') {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1360 $filename = $dir . DIRECTORY_SEPARATOR . $filename;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1361
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1362 if (is_dir($filename)) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1363 self::delete_dir($filename);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1364 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1365 else {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1366 unlink($filename);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1367 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1368 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1369 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1370
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1371 rmdir($dir);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1372 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1373
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1374 return true;
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1375 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1376
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1377 /**
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1378 * Raise/log (relevant) errors
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1379 */
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1380 protected static function raise_error($result, $line, $abort = false)
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1381 {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1382 if ($result->getCode() != enigma_error::BADPASS) {
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1383 rcube::raise_error(array(
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1384 'code' => 600,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1385 'file' => __FILE__,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1386 'line' => $line,
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1387 'message' => "Enigma plugin: " . $result->getMessage()
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1388 ), true, $abort);
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1389 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1390 }
1e000243b222 vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1391 }