Textpattern | PHP Cross Reference | Content Management Systems |
Description: Collection of email related encoders.
1 <?php 2 3 /* 4 * Textpattern Content Management System 5 * https://textpattern.com/ 6 * 7 * Copyright (C) 2020 The Textpattern Development Team 8 * 9 * This file is part of Textpattern. 10 * 11 * Textpattern is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License 13 * as published by the Free Software Foundation, version 2. 14 * 15 * Textpattern is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with Textpattern. If not, see <https://www.gnu.org/licenses/>. 22 */ 23 24 /** 25 * Collection of email related encoders. 26 * 27 * @since 4.6.0 28 * @package Mail 29 */ 30 31 namespace Textpattern\Mail; 32 33 class Encode 34 { 35 /** 36 * Wished character encoding. 37 * 38 * @var string 39 */ 40 41 protected $charset = 'UTF-8'; 42 43 /** 44 * Constructor. 45 */ 46 47 public function __construct() 48 { 49 if (get_pref('override_emailcharset') && is_callable('utf8_decode')) { 50 $this->charset = 'ISO-8859-1'; 51 } 52 } 53 54 /** 55 * Encodes an address list to a valid email header value. 56 * 57 * @param array $value The address list 58 * @return string 59 */ 60 61 public function addressList($value) 62 { 63 if (!$value) { 64 return ''; 65 } 66 67 $out = array(); 68 69 foreach ($value as $email => $name) { 70 if ($this->charset != 'UTF-8') { 71 $name = utf8_decode($name); 72 } 73 74 $out[] = trim($this->header($this->escapeHeader($name), 'phrase').' <'.$this->escapeHeader($email).'>'); 75 } 76 77 return join(', ', $out); 78 } 79 80 /** 81 * Encodes a string for use in an email header. 82 * 83 * @param string $string The string 84 * @param string $type The type of header, either "text" or "phrase" 85 * @return string 86 * @throws \Textpattern\Mail\Exception 87 */ 88 89 public function header($string, $type) 90 { 91 if (strpos($string, '=?') === false && !preg_match('/[\x00-\x1F\x7F-\xFF]/', $string)) { 92 if ($type == 'phrase') { 93 if (preg_match('/[][()<>@,;:".\x5C]/', $string)) { 94 $string = '"'.strtr($string, array("\\" => "\\\\", '"' => '\"')).'"'; 95 } 96 } elseif ($type != 'text') { 97 throw new \Textpattern\Mail\Exception(gTxt('invalid_argument', array('{name}' => 'type'))); 98 } 99 100 return $string; 101 } 102 103 if ($this->charset == 'ISO-8859-1') { 104 $start = '=?ISO-8859-1?B?'; 105 $pcre = '/.{1,42}/s'; 106 } else { 107 $start = '=?UTF-8?B?'; 108 $pcre = '/.{1,45}(?=[\x00-\x7F\xC0-\xFF]|$)/s'; 109 } 110 111 $end = '?='; 112 $sep = IS_WIN ? "\r\n" : "\n"; 113 preg_match_all($pcre, $string, $matches); 114 115 return $start.join($end.$sep.' '.$start, array_map('base64_encode', $matches[0])).$end; 116 } 117 118 /** 119 * Converts an email address into unicode entities. 120 * 121 * <code> 122 * echo Txp::get('\Textpattern\Mail\Encode')->entityObfuscateAddress('john.doe@example.com'); 123 * </code> 124 * 125 * @param string $address The email address 126 * @return string Encoded email address 127 */ 128 129 public function entityObfuscateAddress($address) 130 { 131 if (extension_loaded('mbstring') && mb_detect_encoding($address) != 'ASCII') { 132 return mb_encode_numericentity ($address, array (0x0, 0xfffff, 0, 0xfffff), 'UTF-8'); 133 } 134 135 $ent = array(); 136 $length = strlen($address); 137 138 for ($i = 0; $i < $length; $i++) { 139 $ent[] = "&#".ord(substr($address, $i, 1)).";"; 140 } 141 142 return join('', $ent); 143 } 144 145 /** 146 * Removes new lines and NULL bytes from header lines, preventing 147 * header injections. 148 * 149 * @param string $string The string 150 * @return string Escaped header value 151 */ 152 153 public function escapeHeader($string) 154 { 155 return str_replace(array("\r\n", "\r", "\n", "\0"), array(' ', ' ', ' ', ''), (string)$string); 156 } 157 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title