| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_DEFENDER_VENDOR\Safe; |
| 4 |
|
| 5 |
use WP_DEFENDER_VENDOR\Safe\Exceptions\MailparseException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Extracts/decodes a message section from the supplied filename. |
| 9 |
* |
| 10 |
* The contents of the section will be decoded according to their transfer |
| 11 |
* encoding - base64, quoted-printable and uuencoded text are supported. |
| 12 |
* |
| 13 |
* @param resource $mimemail A valid MIME resource, created with |
| 14 |
* mailparse_msg_create. |
| 15 |
* @param mixed $filename Can be a file name or a valid stream resource. |
| 16 |
* @param callable|null $callbackfunc If set, this must be either a valid callback that will be passed the |
| 17 |
* extracted section, or NULL to make this function return the |
| 18 |
* extracted section. |
| 19 |
* |
| 20 |
* If not specified, the contents will be sent to "stdout". |
| 21 |
* |
| 22 |
* @return string If callbackfunc is not NULL returns TRUE on |
| 23 |
* success. |
| 24 |
* |
| 25 |
* If callbackfunc is set to NULL, returns the |
| 26 |
* extracted section as a string. |
| 27 |
* @throws MailparseException |
| 28 |
* |
| 29 |
*/ |
| 30 |
function mailparse_msg_extract_part_file($mimemail, $filename, ?callable $callbackfunc = null): string |
| 31 |
{ |
| 32 |
error_clear_last(); |
| 33 |
if ($callbackfunc !== null) { |
| 34 |
$result = \mailparse_msg_extract_part_file($mimemail, $filename, $callbackfunc); |
| 35 |
} else { |
| 36 |
$result = \mailparse_msg_extract_part_file($mimemail, $filename); |
| 37 |
} |
| 38 |
if ($result === false) { |
| 39 |
throw MailparseException::createFromPhpError(); |
| 40 |
} |
| 41 |
return $result; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Frees a MIME resource. |
| 47 |
* |
| 48 |
* @param resource $mimemail A valid MIME resource allocated by |
| 49 |
* mailparse_msg_create or |
| 50 |
* mailparse_msg_parse_file. |
| 51 |
* @throws MailparseException |
| 52 |
* |
| 53 |
*/ |
| 54 |
function mailparse_msg_free($mimemail): void |
| 55 |
{ |
| 56 |
error_clear_last(); |
| 57 |
$result = \mailparse_msg_free($mimemail); |
| 58 |
if ($result === false) { |
| 59 |
throw MailparseException::createFromPhpError(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Parses a file. |
| 66 |
* This is the optimal way of parsing a mail file that you have on disk. |
| 67 |
* |
| 68 |
* @param string $filename Path to the file holding the message. |
| 69 |
* The file is opened and streamed through the parser. |
| 70 |
* |
| 71 |
* The message contained in filename is supposed to end with a newline |
| 72 |
* (CRLF); otherwise the last line of the message will not be parsed. |
| 73 |
* @return resource Returns a MIME resource representing the structure. |
| 74 |
* @throws MailparseException |
| 75 |
* |
| 76 |
*/ |
| 77 |
function mailparse_msg_parse_file(string $filename) |
| 78 |
{ |
| 79 |
error_clear_last(); |
| 80 |
$result = \mailparse_msg_parse_file($filename); |
| 81 |
if ($result === false) { |
| 82 |
throw MailparseException::createFromPhpError(); |
| 83 |
} |
| 84 |
return $result; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Incrementally parse data into the supplied mime mail resource. |
| 90 |
* |
| 91 |
* This function allow you to stream portions of a file at a time, rather |
| 92 |
* than read and parse the whole thing. |
| 93 |
* |
| 94 |
* @param resource $mimemail A valid MIME resource. |
| 95 |
* @param string $data The final chunk of data is supposed to end with a newline |
| 96 |
* (CRLF); otherwise the last line of the message will not be parsed. |
| 97 |
* @throws MailparseException |
| 98 |
* |
| 99 |
*/ |
| 100 |
function mailparse_msg_parse($mimemail, string $data): void |
| 101 |
{ |
| 102 |
error_clear_last(); |
| 103 |
$result = \mailparse_msg_parse($mimemail, $data); |
| 104 |
if ($result === false) { |
| 105 |
throw MailparseException::createFromPhpError(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Streams data from the source file pointer, apply |
| 112 |
* encoding and write to the destination file pointer. |
| 113 |
* |
| 114 |
* @param resource $sourcefp A valid file handle. The file is streamed through the parser. |
| 115 |
* @param resource $destfp The destination file handle in which the encoded data will be written. |
| 116 |
* @param string $encoding One of the character encodings supported by the |
| 117 |
* mbstring module. |
| 118 |
* @throws MailparseException |
| 119 |
* |
| 120 |
*/ |
| 121 |
function mailparse_stream_encode($sourcefp, $destfp, string $encoding): void |
| 122 |
{ |
| 123 |
error_clear_last(); |
| 124 |
$result = \mailparse_stream_encode($sourcefp, $destfp, $encoding); |
| 125 |
if ($result === false) { |
| 126 |
throw MailparseException::createFromPhpError(); |
| 127 |
} |
| 128 |
} |
| 129 |
|