2017-05-23 09:36:59 +02:00
< ? php
function policy ( $_action , $_scope , $_data = null ) {
2021-08-09 08:28:22 +02:00
global $pdo ;
global $redis ;
global $lang ;
$_data_log = $_data ;
2017-05-23 09:36:59 +02:00
switch ( $_action ) {
case 'add' :
2018-09-09 21:17:59 +02:00
if ( ! isset ( $_SESSION [ 'acl' ][ 'spam_policy' ]) || $_SESSION [ 'acl' ][ 'spam_policy' ] != " 1 " ) {
$_SESSION [ 'return' ][] = array (
'type' => 'danger' ,
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
);
return false ;
}
2017-05-23 09:36:59 +02:00
switch ( $_scope ) {
case 'domain' :
$object = $_data [ 'domain' ];
if ( is_valid_domain_name ( $object )) {
if ( ! hasDomainAccess ( $_SESSION [ 'mailcow_cc_username' ], $_SESSION [ 'mailcow_cc_role' ], $object )) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
return false ;
}
2019-02-05 00:10:21 +01:00
$object = idn_to_ascii ( strtolower ( trim ( $object )), 0 , INTL_IDNA_VARIANT_UTS46 );
2017-05-23 09:36:59 +02:00
}
else {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
return false ;
}
if ( $_data [ 'object_list' ] == " bl " ) {
$object_list = " blacklist_from " ;
}
elseif ( $_data [ 'object_list' ] == " wl " ) {
$object_list = " whitelist_from " ;
}
2019-01-08 13:00:34 +01:00
$object_from = trim ( strtolower ( $_data [ 'object_from' ]));
2017-10-04 19:01:46 +02:00
if ( ! ctype_alnum ( str_replace ( array ( '@' , '_' , '.' , '-' , '*' ), '' , $object_from ))) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'policy_list_from_invalid'
2017-05-23 09:36:59 +02:00
);
return false ;
}
if ( $object_list != " blacklist_from " && $object_list != " whitelist_from " ) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
return false ;
}
2018-08-13 23:20:40 +02:00
$stmt = $pdo -> prepare ( " SELECT `object` FROM `filterconf`
WHERE ( `option` = 'whitelist_from' OR `option` = 'blacklist_from' )
AND `object` = : object
AND `value` = : object_from " );
$stmt -> execute ( array ( ':object' => $object , ':object_from' => $object_from ));
$num_results = count ( $stmt -> fetchAll ( PDO :: FETCH_ASSOC ));
if ( $num_results != 0 ) {
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
2018-08-13 23:20:40 +02:00
'msg' => 'policy_list_from_exists'
2017-05-23 09:36:59 +02:00
);
return false ;
}
2018-08-13 23:20:40 +02:00
$stmt = $pdo -> prepare ( " INSERT INTO `filterconf` (`object`, `option` ,`value`)
VALUES ( : object , : object_list , : object_from ) " );
$stmt -> execute ( array (
':object' => $object ,
':object_list' => $object_list ,
':object_from' => $object_from
));
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'success' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => array ( 'domain_modified' , $object )
2017-05-23 09:36:59 +02:00
);
break ;
case 'mailbox' :
$object = $_data [ 'username' ];
if ( ! hasMailboxObjectAccess ( $_SESSION [ 'mailcow_cc_username' ], $_SESSION [ 'mailcow_cc_role' ], $object )) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
return false ;
}
if ( $_data [ 'object_list' ] == " bl " ) {
$object_list = " blacklist_from " ;
}
elseif ( $_data [ 'object_list' ] == " wl " ) {
$object_list = " whitelist_from " ;
}
2019-01-08 13:00:34 +01:00
$object_from = trim ( strtolower ( $_data [ 'object_from' ]));
2017-10-04 19:01:46 +02:00
if ( ! ctype_alnum ( str_replace ( array ( '@' , '_' , '.' , '-' , '*' ), '' , $object_from ))) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'policy_list_from_invalid'
2017-05-23 09:36:59 +02:00
);
return false ;
}
if ( $object_list != " blacklist_from " && $object_list != " whitelist_from " ) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
return false ;
}
2018-08-13 23:20:40 +02:00
$stmt = $pdo -> prepare ( " SELECT `object` FROM `filterconf`
WHERE ( `option` = 'whitelist_from' OR `option` = 'blacklist_from' )
AND `object` = : object
AND `value` = : object_from " );
$stmt -> execute ( array ( ':object' => $object , ':object_from' => $object_from ));
$num_results = count ( $stmt -> fetchAll ( PDO :: FETCH_ASSOC ));
if ( $num_results != 0 ) {
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
2018-08-13 23:20:40 +02:00
'msg' => 'policy_list_from_exists'
2017-05-23 09:36:59 +02:00
);
return false ;
}
2018-08-13 23:20:40 +02:00
$stmt = $pdo -> prepare ( " INSERT INTO `filterconf` (`object`, `option` ,`value`)
VALUES ( : object , : object_list , : object_from ) " );
$stmt -> execute ( array (
':object' => $object ,
':object_list' => $object_list ,
':object_from' => $object_from
));
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'success' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => array ( 'mailbox_modified' , $object )
2017-05-23 09:36:59 +02:00
);
break ;
}
break ;
case 'delete' :
2018-09-09 21:17:59 +02:00
if ( ! isset ( $_SESSION [ 'acl' ][ 'spam_policy' ]) || $_SESSION [ 'acl' ][ 'spam_policy' ] != " 1 " ) {
$_SESSION [ 'return' ][] = array (
'type' => 'danger' ,
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
);
return false ;
}
2017-05-23 09:36:59 +02:00
switch ( $_scope ) {
case 'domain' :
2017-05-26 23:02:04 +02:00
( array ) $prefids = $_data [ 'prefid' ];
2017-05-23 09:36:59 +02:00
foreach ( $prefids as $prefid ) {
if ( ! is_numeric ( $prefid )) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
2018-08-13 23:20:40 +02:00
continue ;
2017-05-23 09:36:59 +02:00
}
2018-08-13 23:20:40 +02:00
$stmt = $pdo -> prepare ( " SELECT `object` FROM `filterconf` WHERE `prefid` = :prefid " );
$stmt -> execute ( array ( ':prefid' => $prefid ));
$object = $stmt -> fetch ( PDO :: FETCH_ASSOC )[ 'object' ];
2017-05-23 09:36:59 +02:00
if ( is_valid_domain_name ( $object )) {
if ( ! hasDomainAccess ( $_SESSION [ 'mailcow_cc_username' ], $_SESSION [ 'mailcow_cc_role' ], $object )) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
2018-08-13 23:20:40 +02:00
continue ;
2017-05-23 09:36:59 +02:00
}
2019-02-05 00:10:21 +01:00
$object = idn_to_ascii ( strtolower ( trim ( $object )), 0 , INTL_IDNA_VARIANT_UTS46 );
2017-05-23 09:36:59 +02:00
}
else {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
2018-08-13 23:20:40 +02:00
continue ;
2017-05-23 09:36:59 +02:00
}
try {
$stmt = $pdo -> prepare ( " DELETE FROM `filterconf` WHERE `object` = :object AND `prefid` = :prefid " );
$stmt -> execute ( array (
':object' => $object ,
':prefid' => $prefid
));
}
catch ( PDOException $e ) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => array ( 'mysql_error' , $e )
2017-05-23 09:36:59 +02:00
);
2018-08-13 23:20:40 +02:00
continue ;
2017-05-23 09:36:59 +02:00
}
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
'type' => 'success' ,
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => array ( 'item_deleted' , $prefid )
);
2017-05-23 09:36:59 +02:00
}
break ;
case 'mailbox' :
if ( ! is_array ( $_data [ 'prefid' ])) {
$prefids = array ();
$prefids [] = $_data [ 'prefid' ];
}
else {
$prefids = $_data [ 'prefid' ];
}
foreach ( $prefids as $prefid ) {
if ( ! is_numeric ( $prefid )) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
2018-08-13 23:20:40 +02:00
continue ;
2017-05-23 09:36:59 +02:00
}
2018-08-13 23:20:40 +02:00
$stmt = $pdo -> prepare ( " SELECT `object` FROM `filterconf` WHERE `prefid` = :prefid " );
$stmt -> execute ( array ( ':prefid' => $prefid ));
$object = $stmt -> fetch ( PDO :: FETCH_ASSOC )[ 'object' ];
2017-05-23 09:36:59 +02:00
if ( ! hasMailboxObjectAccess ( $_SESSION [ 'mailcow_cc_username' ], $_SESSION [ 'mailcow_cc_role' ], $object )) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => 'access_denied'
2017-05-23 09:36:59 +02:00
);
2018-08-13 23:20:40 +02:00
continue ;
2017-05-23 09:36:59 +02:00
}
try {
$stmt = $pdo -> prepare ( " DELETE FROM `filterconf` WHERE `object` = :object AND `prefid` = :prefid " );
$stmt -> execute ( array (
':object' => $object ,
':prefid' => $prefid
));
}
catch ( PDOException $e ) {
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
2017-05-23 09:36:59 +02:00
'type' => 'danger' ,
2018-08-03 20:31:33 +02:00
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
'msg' => array ( 'mysql_error' , $e )
2017-05-23 09:36:59 +02:00
);
2018-08-13 23:20:40 +02:00
continue ;
2017-05-23 09:36:59 +02:00
}
2018-08-13 23:20:40 +02:00
$_SESSION [ 'return' ][] = array (
'type' => 'success' ,
'log' => array ( __FUNCTION__ , $_action , $_scope , $_data_log ),
2021-08-09 08:28:22 +02:00
'msg' => array ( 'items_deleted' , implode ( ', ' , ( array ) $prefids ))
2018-08-13 23:20:40 +02:00
);
2017-05-23 09:36:59 +02:00
}
break ;
}
break ;
case 'get' :
switch ( $_scope ) {
case 'domain' :
if ( ! is_valid_domain_name ( $_data )) {
return false ;
}
else {
if ( ! hasDomainAccess ( $_SESSION [ 'mailcow_cc_username' ], $_SESSION [ 'mailcow_cc_role' ], $_data )) {
return false ;
}
2019-02-05 00:10:21 +01:00
$_data = idn_to_ascii ( strtolower ( trim ( $_data )), 0 , INTL_IDNA_VARIANT_UTS46 );
2017-05-23 09:36:59 +02:00
}
2018-08-13 23:20:40 +02:00
// WHITELIST
$stmt = $pdo -> prepare ( " SELECT `object`, `value`, `prefid` FROM `filterconf` WHERE `option`='whitelist_from' AND (`object` LIKE :object_mail OR `object` = :object_domain) " );
$stmt -> execute ( array ( ':object_mail' => '%@' . $_data , ':object_domain' => $_data ));
$rows [ 'whitelist' ] = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
// BLACKLIST
$stmt = $pdo -> prepare ( " SELECT `object`, `value`, `prefid` FROM `filterconf` WHERE `option`='blacklist_from' AND (`object` LIKE :object_mail OR `object` = :object_domain) " );
$stmt -> execute ( array ( ':object_mail' => '%@' . $_data , ':object_domain' => $_data ));
$rows [ 'blacklist' ] = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
2017-05-23 09:36:59 +02:00
return $rows ;
break ;
case 'mailbox' :
if ( isset ( $_data ) && filter_var ( $_data , FILTER_VALIDATE_EMAIL )) {
if ( ! hasMailboxObjectAccess ( $_SESSION [ 'mailcow_cc_username' ], $_SESSION [ 'mailcow_cc_role' ], $_data )) {
return false ;
}
}
else {
$_data = $_SESSION [ 'mailcow_cc_username' ];
}
$domain = mailbox ( 'get' , 'mailbox_details' , $_data )[ 'domain' ];
if ( empty ( $domain )) {
return false ;
}
2018-08-13 23:20:40 +02:00
// WHITELIST
$stmt = $pdo -> prepare ( " SELECT `object`, `value`, `prefid` FROM `filterconf` WHERE `option`='whitelist_from' AND (`object` = :username OR `object` = :domain) " );
$stmt -> execute ( array ( ':username' => $_data , ':domain' => $domain ));
$rows [ 'whitelist' ] = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
// BLACKLIST
$stmt = $pdo -> prepare ( " SELECT `object`, `value`, `prefid` FROM `filterconf` WHERE `option`='blacklist_from' AND (`object` = :username OR `object` = :domain) " );
$stmt -> execute ( array ( ':username' => $_data , ':domain' => $domain ));
$rows [ 'blacklist' ] = $stmt -> fetchAll ( PDO :: FETCH_ASSOC );
2017-05-23 09:36:59 +02:00
return $rows ;
break ;
}
break ;
}
2021-08-09 08:28:22 +02:00
}