2007-08-17 17:14:16 +02:00
< ? php
2018-01-26 15:45:57 +01:00
/**
* Postfix Admin
*
* LICENSE
* This source file is subject to the GPL license that is bundled with
* this package in the file LICENSE . TXT .
*
* Further details on the project are available at http :// postfixadmin . sf . net
*
* @ version $Id $
* @ license GNU GPL v2 or later .
*
2007-09-30 17:53:54 +02:00
* File : broadcast - message . php
* Used to send a message to _ALL_ users with mailboxes on this server .
*
2009-12-24 15:58:01 +01:00
* Template File : broadcast - message . tpl
2007-09-30 17:53:54 +02:00
*
* Template Variables : - none -
*
* Form POST \ GET Variables :
*
* name
* subject
* message
*/
2007-08-17 17:14:16 +02:00
2007-10-31 19:24:40 +01:00
require_once ( 'common.php' );
2007-09-22 12:54:00 +02:00
2017-02-13 16:15:15 +01:00
if ( Config :: bool ( 'sendmail_all_admins' )) {
2018-01-26 15:45:57 +01:00
authentication_require_role ( 'admin' );
2017-02-12 20:39:37 +01:00
} else {
2018-01-26 15:45:57 +01:00
authentication_require_role ( 'global-admin' );
2017-02-11 23:16:43 +01:00
}
2007-09-22 12:54:00 +02:00
2020-09-27 12:02:58 +02:00
$CONF = Config :: getInstance () -> getAll ();
$smarty = PFASmarty :: getInstance ();
2007-11-18 22:46:20 +01:00
if ( $CONF [ 'sendmail' ] != 'YES' ) {
2018-01-26 15:45:57 +01:00
header ( " Location: main.php " );
exit ;
2007-11-18 22:46:20 +01:00
}
2017-04-10 20:54:34 +02:00
$error = 0 ;
2011-07-19 23:41:37 +02:00
$smtp_from_email = smtp_get_admin_email ();
2017-02-11 23:16:43 +01:00
$allowed_domains = list_domains_for_admin ( authentication_get_username ());
2007-08-17 17:14:16 +02:00
2018-01-26 15:45:57 +01:00
if ( $_SERVER [ 'REQUEST_METHOD' ] == " POST " ) {
if ( safepost ( 'token' ) != $_SESSION [ 'PFA_token' ]) {
die ( 'Invalid token!' );
}
2021-03-02 22:25:49 +01:00
if ( empty ( $_POST [ 'subject' ]) || empty ( $_POST [ 'message' ]) || empty ( $_POST [ 'name' ]) || empty ( $_POST [ 'domains' ]) || ! is_array ( $_POST [ 'domains' ])) {
2018-01-26 15:45:57 +01:00
$error = 1 ;
flash_error ( $PALANG [ 'pBroadcast_error_empty' ]);
} else {
$wanted_domains = array_intersect ( $allowed_domains , $_POST [ 'domains' ]);
$table_mailbox = table_by_key ( 'mailbox' );
$table_alias = table_by_key ( 'alias' );
$recipients = array ();
$q = " SELECT username from $table_mailbox WHERE active=' " . db_get_boolean ( true ) . " ' AND " . db_in_clause ( " domain " , $wanted_domains );
if ( intval ( safepost ( 'mailboxes_only' )) == 0 ) {
2021-03-02 22:25:49 +01:00
$q .= " UNION SELECT goto FROM $table_alias WHERE active=' " . db_get_boolean ( true ) . " ' AND " . db_in_clause ( " domain " , $wanted_domains ) . " AND goto NOT IN ( $q ) " ;
2018-01-26 15:45:57 +01:00
}
2019-01-06 22:27:08 +01:00
$result = db_query_all ( $q );
2019-01-06 21:25:24 +01:00
$recipients = array_column ( $result , 'username' );
2018-01-26 15:45:57 +01:00
$recipients = array_unique ( $recipients );
if ( count ( $recipients ) > 0 ) {
mb_internal_encoding ( " UTF-8 " );
$b_name = mb_encode_mimeheader ( $_POST [ 'name' ], 'UTF-8' , 'Q' );
$b_subject = mb_encode_mimeheader ( $_POST [ 'subject' ], 'UTF-8' , 'Q' );
2018-02-08 17:11:57 +01:00
$b_message = chunk_split ( base64_encode ( $_POST [ 'message' ]));
$serverName = isset ( $_SERVER [ 'SERVER_NAME' ]) ? $_SERVER [ 'SERVER_NAME' ] : php_uname ( 'n' ); // ??
2018-01-26 15:45:57 +01:00
$i = 0 ;
foreach ( $recipients as $rcpt ) {
$fTo = $rcpt ;
$fHeaders = 'To: ' . $fTo . " \n " ;
$fHeaders .= 'From: ' . $b_name . ' <' . $smtp_from_email . " > \n " ;
$fHeaders .= 'Subject: ' . $b_subject . " \n " ;
$fHeaders .= 'MIME-Version: 1.0' . " \n " ;
$fHeaders .= 'Content-Type: text/plain; charset=UTF-8' . " \n " ;
$fHeaders .= 'Content-Transfer-Encoding: base64' . " \n " ;
2018-02-08 17:11:57 +01:00
$fHeaders .= 'Date: ' . date ( 'r' , time ()) . " \n " ;
$fHeaders .= 'Message-ID: <' . microtime ( true ) . '-' . md5 ( $smtp_from_email . $fTo ) . " @ { $serverName } > \n \n " ;
2018-01-26 15:45:57 +01:00
$fHeaders .= $b_message ;
2019-05-31 11:42:42 +02:00
if ( ! smtp_mail ( $fTo , $smtp_from_email , $fHeaders , smtp_get_admin_password ())) {
2018-01-26 15:45:57 +01:00
flash_error ( Config :: lang_f ( 'pSendmail_result_error' , $fTo ));
} else {
flash_info ( Config :: lang_f ( 'pSendmail_result_success' , $fTo ));
}
2007-08-17 17:14:16 +02:00
}
2018-01-26 15:45:57 +01:00
}
flash_info ( $PALANG [ 'pBroadcast_success' ]);
2021-01-29 22:24:51 +01:00
$smarty -> assign ( 'smarty_template' , 'broadcast-message' );
2018-01-26 15:45:57 +01:00
$smarty -> display ( 'index.tpl' );
// echo '<p>'.$PALANG['pBroadcast_success'].'</p>';
}
2007-08-17 17:14:16 +02:00
}
2018-01-26 15:45:57 +01:00
if ( $_SERVER [ 'REQUEST_METHOD' ] == " GET " || $error == 1 ) {
$smarty -> assign ( 'allowed_domains' , $allowed_domains );
$smarty -> assign ( 'smtp_from_email' , $smtp_from_email );
$smarty -> assign ( 'error' , $error );
$smarty -> assign ( 'smarty_template' , 'broadcast-message' );
$smarty -> display ( 'index.tpl' );
2009-11-05 15:55:39 +01:00
2018-01-26 15:45:57 +01:00
// include ("templates/broadcast-message.tpl");
2007-08-17 17:14:16 +02:00
}
/* vim: set expandtab softtabstop=3 tabstop=3 shiftwidth=3: */