summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/Mailer.class.php294
1 files changed, 150 insertions, 144 deletions
diff --git a/inc/Mailer.class.php b/inc/Mailer.class.php
index 7661eaa75..fccf1dad9 100644
--- a/inc/Mailer.class.php
+++ b/inc/Mailer.class.php
@@ -11,20 +11,24 @@
// end of line for mail lines - RFC822 says CRLF but postfix (and other MTAs?)
// think different
-if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL',"\n");
+if(!defined('MAILHEADER_EOL')) define('MAILHEADER_EOL', "\n");
#define('MAILHEADER_ASCIIONLY',1);
+/**
+ * Mail Handling
+ */
class Mailer {
- protected $headers = array();
- protected $attach = array();
- protected $html = '';
- protected $text = '';
+ protected $headers = array();
+ protected $attach = array();
+ protected $html = '';
+ protected $text = '';
- protected $boundary = '';
- protected $partid = '';
- protected $sendparam= null;
+ protected $boundary = '';
+ protected $partid = '';
+ protected $sendparam = null;
+ /** @var EmailAddressValidator */
protected $validator = null;
protected $allowhtml = true;
@@ -33,38 +37,38 @@ class Mailer {
*
* Initializes the boundary strings and part counters
*/
- public function __construct(){
+ public function __construct() {
global $conf;
- $server = parse_url(DOKU_URL,PHP_URL_HOST);
+ $server = parse_url(DOKU_URL, PHP_URL_HOST);
- $this->partid = md5(uniqid(rand(),true)).'@'.$server;
- $this->boundary = '----------'.md5(uniqid(rand(),true));
+ $this->partid = md5(uniqid(rand(), true)).'@'.$server;
+ $this->boundary = '----------'.md5(uniqid(rand(), true));
- $listid = join('.',array_reverse(explode('/',DOKU_BASE))).$server;
- $listid = strtolower(trim($listid,'.'));
+ $listid = join('.', array_reverse(explode('/', DOKU_BASE))).$server;
+ $listid = strtolower(trim($listid, '.'));
- $this->allowhtml = (bool) $conf['htmlmail'];
+ $this->allowhtml = (bool)$conf['htmlmail'];
// add some default headers for mailfiltering FS#2247
- $this->setHeader('X-Mailer','DokuWiki '.getVersion());
+ $this->setHeader('X-Mailer', 'DokuWiki '.getVersion());
$this->setHeader('X-DokuWiki-User', $_SERVER['REMOTE_USER']);
$this->setHeader('X-DokuWiki-Title', $conf['title']);
$this->setHeader('X-DokuWiki-Server', $server);
$this->setHeader('X-Auto-Response-Suppress', 'OOF');
- $this->setHeader('List-Id',$conf['title'].' <'.$listid.'>');
+ $this->setHeader('List-Id', $conf['title'].' <'.$listid.'>');
}
/**
* Attach a file
*
- * @param $path Path to the file to attach
- * @param $mime Mimetype of the attached file
- * @param $name The filename to use
- * @param $embed Unique key to reference this file from the HTML part
+ * @param string $path Path to the file to attach
+ * @param string $mime Mimetype of the attached file
+ * @param string $name The filename to use
+ * @param string $embed Unique key to reference this file from the HTML part
*/
- public function attachFile($path,$mime,$name='',$embed=''){
- if(!$name){
+ public function attachFile($path, $mime, $name = '', $embed = '') {
+ if(!$name) {
$name = basename($path);
}
@@ -79,14 +83,14 @@ class Mailer {
/**
* Attach a file
*
- * @param $path The file contents to attach
- * @param $mime Mimetype of the attached file
- * @param $name The filename to use
- * @param $embed Unique key to reference this file from the HTML part
+ * @param string $data The file contents to attach
+ * @param string $mime Mimetype of the attached file
+ * @param string $name The filename to use
+ * @param string $embed Unique key to reference this file from the HTML part
*/
- public function attachContent($data,$mime,$name='',$embed=''){
- if(!$name){
- list($junk,$ext) = split('/',$mime);
+ public function attachContent($data, $mime, $name = '', $embed = '') {
+ if(!$name) {
+ list(, $ext) = explode('/', $mime);
$name = count($this->attach).".$ext";
}
@@ -101,18 +105,18 @@ class Mailer {
/**
* Callback function to automatically embed images referenced in HTML templates
*/
- protected function autoembed_cb($matches){
+ protected function autoembed_cb($matches) {
static $embeds = 0;
$embeds++;
// get file and mime type
$media = cleanID($matches[1]);
- list($ext, $mime) = mimetype($media);
- $file = mediaFN($media);
+ list(, $mime) = mimetype($media);
+ $file = mediaFN($media);
if(!file_exists($file)) return $matches[0]; //bad reference, keep as is
// attach it and set placeholder
- $this->attachFile($file,$mime,'','autoembed'.$embeds);
+ $this->attachFile($file, $mime, '', 'autoembed'.$embeds);
return '%%autoembed'.$embeds.'%%';
}
@@ -125,18 +129,18 @@ class Mailer {
* @param string $value the value of the header
* @param bool $clean remove all non-ASCII chars and line feeds?
*/
- public function setHeader($header,$value,$clean=true){
- $header = str_replace(' ','-',ucwords(strtolower(str_replace('-',' ',$header)))); // streamline casing
- if($clean){
- $header = preg_replace('/[^\w \-\.\+\@]+/','',$header);
- $value = preg_replace('/[^\w \-\.\+\@<>]+/','',$value);
+ public function setHeader($header, $value, $clean = true) {
+ $header = str_replace(' ', '-', ucwords(strtolower(str_replace('-', ' ', $header)))); // streamline casing
+ if($clean) {
+ $header = preg_replace('/[^\w \-\.\+\@]+/', '', $header);
+ $value = preg_replace('/[^\w \-\.\+\@<>]+/', '', $value);
}
// empty value deletes
$value = trim($value);
- if($value === ''){
+ if($value === '') {
if(isset($this->headers[$header])) unset($this->headers[$header]);
- }else{
+ } else {
$this->headers[$header] = $value;
}
}
@@ -147,7 +151,7 @@ class Mailer {
* Whatever is set here is directly passed to PHP's mail() command as last
* parameter. Depending on the PHP setup this might break mailing alltogether
*/
- public function setParameters($param){
+ public function setParameters($param) {
$this->sendparam = $param;
}
@@ -166,38 +170,40 @@ class Mailer {
* @param array $html the HTML body, leave null to create it from $text
* @param bool $wrap wrap the HTML in the default header/Footer
*/
- public function setBody($text, $textrep=null, $htmlrep=null, $html=null, $wrap=true){
+ public function setBody($text, $textrep = null, $htmlrep = null, $html = null, $wrap = true) {
global $INFO;
global $conf;
- $htmlrep = (array) $htmlrep;
- $textrep = (array) $textrep;
+ $htmlrep = (array)$htmlrep;
+ $textrep = (array)$textrep;
// create HTML from text if not given
- if(is_null($html)){
+ if(is_null($html)) {
$html = $text;
$html = hsc($html);
- $html = preg_replace('/^-----*$/m','<hr >',$html);
+ $html = preg_replace('/^-----*$/m', '<hr >', $html);
$html = nl2br($html);
}
- if($wrap){
- $wrap = rawLocale('mailwrap','html');
- $html = preg_replace('/\n-- <br \/>.*$/s','',$html); //strip signature
- $html = str_replace('@HTMLBODY@',$html,$wrap);
+ if($wrap) {
+ $wrap = rawLocale('mailwrap', 'html');
+ $html = preg_replace('/\n-- <br \/>.*$/s', '', $html); //strip signature
+ $html = str_replace('@HTMLBODY@', $html, $wrap);
}
// copy over all replacements missing for HTML (autolink URLs)
- foreach($textrep as $key => $value){
+ foreach($textrep as $key => $value) {
if(isset($htmlrep[$key])) continue;
- if(preg_match('/^https?:\/\//i',$value)){
+ if(preg_match('/^https?:\/\//i', $value)) {
$htmlrep[$key] = '<a href="'.hsc($value).'">'.hsc($value).'</a>';
- }else{
+ } else {
$htmlrep[$key] = hsc($value);
}
}
// embed media from templates
- $html = preg_replace_callback('/@MEDIA\(([^\)]+)\)@/',
- array($this,'autoembed_cb'),$html);
+ $html = preg_replace_callback(
+ '/@MEDIA\(([^\)]+)\)@/',
+ array($this, 'autoembed_cb'), $html
+ );
// prepare default replacements
$ip = clientIP();
@@ -213,7 +219,7 @@ class Mailer {
'NAME' => $INFO['userinfo']['name'],
'MAIL' => $INFO['userinfo']['mail'],
);
- $trep = array_merge($trep,(array) $textrep);
+ $trep = array_merge($trep, (array)$textrep);
$hrep = array(
'DATE' => '<i>'.hsc(dformat()).'</i>',
'BROWSER' => hsc($_SERVER['HTTP_USER_AGENT']),
@@ -224,16 +230,16 @@ class Mailer {
'USER' => hsc($_SERVER['REMOTE_USER']),
'NAME' => hsc($INFO['userinfo']['name']),
'MAIL' => '<a href="mailto:"'.hsc($INFO['userinfo']['mail']).'">'.
- hsc($INFO['userinfo']['mail']).'</a>',
+ hsc($INFO['userinfo']['mail']).'</a>',
);
- $hrep = array_merge($hrep,(array) $htmlrep);
+ $hrep = array_merge($hrep, (array)$htmlrep);
// Apply replacements
- foreach ($trep as $key => $substitution) {
- $text = str_replace('@'.strtoupper($key).'@',$substitution, $text);
+ foreach($trep as $key => $substitution) {
+ $text = str_replace('@'.strtoupper($key).'@', $substitution, $text);
}
- foreach ($hrep as $key => $substitution) {
- $html = str_replace('@'.strtoupper($key).'@',$substitution, $html);
+ foreach($hrep as $key => $substitution) {
+ $html = str_replace('@'.strtoupper($key).'@', $substitution, $html);
}
$this->setHTML($html);
@@ -247,7 +253,7 @@ class Mailer {
*
* You probably want to use setBody() instead
*/
- public function setHTML($html){
+ public function setHTML($html) {
$this->html = $html;
}
@@ -256,7 +262,7 @@ class Mailer {
*
* You probably want to use setBody() instead
*/
- public function setText($text){
+ public function setText($text) {
$this->text = $text;
}
@@ -266,7 +272,7 @@ class Mailer {
* @see setAddress
* @param string $address Multiple adresses separated by commas
*/
- public function to($address){
+ public function to($address) {
$this->setHeader('To', $address, false);
}
@@ -276,7 +282,7 @@ class Mailer {
* @see setAddress
* @param string $address Multiple adresses separated by commas
*/
- public function cc($address){
+ public function cc($address) {
$this->setHeader('Cc', $address, false);
}
@@ -286,7 +292,7 @@ class Mailer {
* @see setAddress
* @param string $address Multiple adresses separated by commas
*/
- public function bcc($address){
+ public function bcc($address) {
$this->setHeader('Bcc', $address, false);
}
@@ -299,7 +305,7 @@ class Mailer {
* @see setAddress
* @param string $address from address
*/
- public function from($address){
+ public function from($address) {
$this->setHeader('From', $address, false);
}
@@ -308,7 +314,7 @@ class Mailer {
*
* @param string $subject the mail subject
*/
- public function subject($subject){
+ public function subject($subject) {
$this->headers['Subject'] = $subject;
}
@@ -322,65 +328,65 @@ class Mailer {
* setAddress("föö <foo@bar.com>, me@somewhere.com","TBcc");
*
* @param string $address Multiple adresses separated by commas
- * @param string returns the prepared header (can contain multiple lines)
+ * @return bool|string the prepared header (can contain multiple lines)
*/
- public function cleanAddress($address){
+ public function cleanAddress($address) {
// No named recipients for To: in Windows (see FS#652)
$names = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? false : true;
- $address = preg_replace('/[\r\n\0]+/',' ',$address); // remove attack vectors
+ $address = preg_replace('/[\r\n\0]+/', ' ', $address); // remove attack vectors
$headers = '';
- $parts = explode(',',$address);
- foreach ($parts as $part){
+ $parts = explode(',', $address);
+ foreach($parts as $part) {
$part = trim($part);
// parse address
- if(preg_match('#(.*?)<(.*?)>#',$part,$matches)){
+ if(preg_match('#(.*?)<(.*?)>#', $part, $matches)) {
$text = trim($matches[1]);
$addr = $matches[2];
- }else{
+ } else {
$addr = $part;
}
// skip empty ones
- if(empty($addr)){
+ if(empty($addr)) {
continue;
}
// FIXME: is there a way to encode the localpart of a emailaddress?
- if(!utf8_isASCII($addr)){
- msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"),-1);
+ if(!utf8_isASCII($addr)) {
+ msg(htmlspecialchars("E-Mail address <$addr> is not ASCII"), -1);
continue;
}
- if(is_null($this->validator)){
- $this->validator = new EmailAddressValidator();
+ if(is_null($this->validator)) {
+ $this->validator = new EmailAddressValidator();
$this->validator->allowLocalAddresses = true;
}
- if(!$this->validator->check_email_address($addr)){
- msg(htmlspecialchars("E-Mail address <$addr> is not valid"),-1);
+ if(!$this->validator->check_email_address($addr)) {
+ msg(htmlspecialchars("E-Mail address <$addr> is not valid"), -1);
continue;
}
// text was given
- if(!empty($text) && $names){
+ if(!empty($text) && $names) {
// add address quotes
$addr = "<$addr>";
- if(defined('MAILHEADER_ASCIIONLY')){
+ if(defined('MAILHEADER_ASCIIONLY')) {
$text = utf8_deaccent($text);
$text = utf8_strip($text);
}
- if(!utf8_isASCII($text)){
+ if(!utf8_isASCII($text)) {
$text = '=?UTF-8?B?'.base64_encode($text).'?=';
}
- }else{
+ } else {
$text = '';
}
// add to header comma seperated
- if($headers != ''){
+ if($headers != '') {
$headers .= ', ';
}
$headers .= $text.' '.$addr;
@@ -397,30 +403,30 @@ class Mailer {
*
* Replaces placeholders in the HTML with the correct CIDs
*/
- protected function prepareAttachments(){
+ protected function prepareAttachments() {
$mime = '';
$part = 1;
// embedded attachments
- foreach($this->attach as $media){
+ foreach($this->attach as $media) {
// create content id
$cid = 'part'.$part.'.'.$this->partid;
// replace wildcards
- if($media['embed']){
- $this->html = str_replace('%%'.$media['embed'].'%%','cid:'.$cid,$this->html);
+ if($media['embed']) {
+ $this->html = str_replace('%%'.$media['embed'].'%%', 'cid:'.$cid, $this->html);
}
$mime .= '--'.$this->boundary.MAILHEADER_EOL;
$mime .= 'Content-Type: '.$media['mime'].';'.MAILHEADER_EOL;
$mime .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
$mime .= "Content-ID: <$cid>".MAILHEADER_EOL;
- if($media['embed']){
+ if($media['embed']) {
$mime .= 'Content-Disposition: inline; filename="'.$media['name'].'"'.MAILHEADER_EOL;
- }else{
+ } else {
$mime .= 'Content-Disposition: attachment; filename="'.$media['name'].'"'.MAILHEADER_EOL;
}
$mime .= MAILHEADER_EOL; //end of headers
- $mime .= chunk_split(base64_encode($media['data']),74,MAILHEADER_EOL);
+ $mime .= chunk_split(base64_encode($media['data']), 74, MAILHEADER_EOL);
$part++;
}
@@ -434,16 +440,15 @@ class Mailer {
*
* @return string the prepared mail body, false on errors
*/
- protected function prepareBody(){
- global $conf;
+ protected function prepareBody() {
// no HTML mails allowed? remove HTML body
- if(!$this->allowhtml){
+ if(!$this->allowhtml) {
$this->html = '';
}
// check for body
- if(!$this->text && !$this->html){
+ if(!$this->text && !$this->html) {
return false;
}
@@ -452,28 +457,28 @@ class Mailer {
$body = '';
- if(!$this->html && !count($this->attach)){ // we can send a simple single part message
- $this->headers['Content-Type'] = 'text/plain; charset=UTF-8';
+ if(!$this->html && !count($this->attach)) { // we can send a simple single part message
+ $this->headers['Content-Type'] = 'text/plain; charset=UTF-8';
$this->headers['Content-Transfer-Encoding'] = 'base64';
- $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL);
- }else{ // multi part it is
+ $body .= chunk_split(base64_encode($this->text), 74, MAILHEADER_EOL);
+ } else { // multi part it is
$body .= "This is a multi-part message in MIME format.".MAILHEADER_EOL;
// prepare the attachments
$attachments = $this->prepareAttachments();
// do we have alternative text content?
- if($this->text && $this->html){
+ if($this->text && $this->html) {
$this->headers['Content-Type'] = 'multipart/alternative;'.MAILHEADER_EOL.
- ' boundary="'.$this->boundary.'XX"';
+ ' boundary="'.$this->boundary.'XX"';
$body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
$body .= 'Content-Type: text/plain; charset=UTF-8'.MAILHEADER_EOL;
$body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
$body .= MAILHEADER_EOL;
- $body .= chunk_split(base64_encode($this->text),74,MAILHEADER_EOL);
+ $body .= chunk_split(base64_encode($this->text), 74, MAILHEADER_EOL);
$body .= '--'.$this->boundary.'XX'.MAILHEADER_EOL;
$body .= 'Content-Type: multipart/related;'.MAILHEADER_EOL.
- ' boundary="'.$this->boundary.'"'.MAILHEADER_EOL;
+ ' boundary="'.$this->boundary.'"'.MAILHEADER_EOL;
$body .= MAILHEADER_EOL;
}
@@ -481,13 +486,13 @@ class Mailer {
$body .= 'Content-Type: text/html; charset=UTF-8'.MAILHEADER_EOL;
$body .= 'Content-Transfer-Encoding: base64'.MAILHEADER_EOL;
$body .= MAILHEADER_EOL;
- $body .= chunk_split(base64_encode($this->html),74,MAILHEADER_EOL);
+ $body .= chunk_split(base64_encode($this->html), 74, MAILHEADER_EOL);
$body .= MAILHEADER_EOL;
$body .= $attachments;
$body .= '--'.$this->boundary.'--'.MAILHEADER_EOL;
// close open multipart/alternative boundary
- if($this->text && $this->html){
+ if($this->text && $this->html) {
$body .= '--'.$this->boundary.'XX--'.MAILHEADER_EOL;
}
}
@@ -498,47 +503,47 @@ class Mailer {
/**
* Cleanup and encode the headers array
*/
- protected function cleanHeaders(){
+ protected function cleanHeaders() {
global $conf;
// clean up addresses
if(empty($this->headers['From'])) $this->from($conf['mailfrom']);
- $addrs = array('To','From','Cc','Bcc');
- foreach($addrs as $addr){
- if(isset($this->headers[$addr])){
+ $addrs = array('To', 'From', 'Cc', 'Bcc');
+ foreach($addrs as $addr) {
+ if(isset($this->headers[$addr])) {
$this->headers[$addr] = $this->cleanAddress($this->headers[$addr]);
}
}
- if(isset($this->headers['Subject'])){
+ if(isset($this->headers['Subject'])) {
// add prefix to subject
- if(empty($conf['mailprefix'])){
+ if(empty($conf['mailprefix'])) {
if(utf8_strlen($conf['title']) < 20) {
$prefix = '['.$conf['title'].']';
- }else{
+ } else {
$prefix = '['.utf8_substr($conf['title'], 0, 20).'...]';
}
- }else{
+ } else {
$prefix = '['.$conf['mailprefix'].']';
}
$len = strlen($prefix);
- if(substr($this->headers['Subject'],0,$len) != $prefix){
+ if(substr($this->headers['Subject'], 0, $len) != $prefix) {
$this->headers['Subject'] = $prefix.' '.$this->headers['Subject'];
}
// encode subject
- if(defined('MAILHEADER_ASCIIONLY')){
+ if(defined('MAILHEADER_ASCIIONLY')) {
$this->headers['Subject'] = utf8_deaccent($this->headers['Subject']);
$this->headers['Subject'] = utf8_strip($this->headers['Subject']);
}
- if(!utf8_isASCII($this->headers['Subject'])){
+ if(!utf8_isASCII($this->headers['Subject'])) {
$this->headers['Subject'] = '=?UTF-8?B?'.base64_encode($this->headers['Subject']).'?=';
}
}
// wrap headers
- foreach($this->headers as $key => $val){
- $this->headers[$key] = wordwrap($val,78,MAILHEADER_EOL.' ');
+ foreach($this->headers as $key => $val) {
+ $this->headers[$key] = wordwrap($val, 78, MAILHEADER_EOL.' ');
}
}
@@ -547,9 +552,9 @@ class Mailer {
*
* @returns string the headers
*/
- protected function prepareHeaders(){
+ protected function prepareHeaders() {
$headers = '';
- foreach($this->headers as $key => $val){
+ foreach($this->headers as $key => $val) {
$headers .= "$key: $val".MAILHEADER_EOL;
}
return $headers;
@@ -563,9 +568,9 @@ class Mailer {
*
* @return string the mail, false on errors
*/
- public function dump(){
+ public function dump() {
$this->cleanHeaders();
- $body = $this->prepareBody();
+ $body = $this->prepareBody();
if($body === false) return false;
$headers = $this->prepareHeaders();
@@ -580,13 +585,13 @@ class Mailer {
* @triggers MAIL_MESSAGE_SEND
* @return bool true if the mail was successfully passed to the MTA
*/
- public function send(){
+ public function send() {
$success = false;
// prepare hook data
$data = array(
// pass the whole mail class to plugin
- 'mail' => $this,
+ 'mail' => $this,
// pass references for backward compatibility
'to' => &$this->headers['To'],
'cc' => &$this->headers['Cc'],
@@ -594,7 +599,7 @@ class Mailer {
'from' => &$this->headers['From'],
'subject' => &$this->headers['Subject'],
'body' => &$this->text,
- 'params' => &$this->sendparams,
+ 'params' => &$this->sendparam,
'headers' => '', // plugins shouldn't use this
// signal if we mailed successfully to AFTER event
'success' => &$success,
@@ -602,47 +607,48 @@ class Mailer {
// do our thing if BEFORE hook approves
$evt = new Doku_Event('MAIL_MESSAGE_SEND', $data);
- if ($evt->advise_before(true)) {
+ if($evt->advise_before(true)) {
// clean up before using the headers
$this->cleanHeaders();
// any recipients?
- if(trim($this->headers['To']) === '' &&
- trim($this->headers['Cc']) === '' &&
- trim($this->headers['Bcc']) === '') return false;
+ if(trim($this->headers['To']) === '' &&
+ trim($this->headers['Cc']) === '' &&
+ trim($this->headers['Bcc']) === ''
+ ) return false;
// The To: header is special
- if(isset($this->headers['To'])){
+ if(isset($this->headers['To'])) {
$to = $this->headers['To'];
unset($this->headers['To']);
- }else{
+ } else {
$to = '';
}
// so is the subject
- if(isset($this->headers['Subject'])){
+ if(isset($this->headers['Subject'])) {
$subject = $this->headers['Subject'];
unset($this->headers['Subject']);
- }else{
+ } else {
$subject = '';
}
// make the body
- $body = $this->prepareBody();
+ $body = $this->prepareBody();
if($body === false) return false;
// cook the headers
$headers = $this->prepareHeaders();
// add any headers set by legacy plugins
- if(trim($data['headers'])){
+ if(trim($data['headers'])) {
$headers .= MAILHEADER_EOL.trim($data['headers']);
}
// send the thing
- if(is_null($this->sendparam)){
- $success = @mail($to,$subject,$body,$headers);
- }else{
- $success = @mail($to,$subject,$body,$headers,$this->sendparam);
+ if(is_null($this->sendparam)) {
+ $success = @mail($to, $subject, $body, $headers);
+ } else {
+ $success = @mail($to, $subject, $body, $headers, $this->sendparam);
}
}
// any AFTER actions?