summaryrefslogtreecommitdiff
path: root/www/admin/user/forward.php
blob: f183f7e4395b6e85a0c71bda9ae907f1a64cef68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/*
 *  Copyright (c) 2004 Klarälvdalens Datakonsult AB
 *
 *    Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
 *
 *  This  program is free  software; you can redistribute  it and/or
 *  modify it  under the terms of the GNU  General Public License as
 *  published by the  Free Software Foundation; either version 2, or
 *  (at your option) any later version.
 *
 *  This program is  distributed in the hope that it will be useful,
 *  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

require_once 'admin/include/mysmarty.php';
require_once 'admin/include/headers.php';
require_once 'admin/include/authenticate.php';

// Funny multiline string escaping in Sieve
function dotstuff( $str ) {
  return str_replace( "\n.", "\n..", $str );
}

function undotstuff( $str ) {
  return str_replace( "\n..", "\n.", $str );
}

$errors = array();
if( (@include_once 'admin/include/Sieve.php' ) === false ) {
  $errors[] = _('Net/Sieve.php is missing. Without that, vacation settings are not available');
  $errors[] = _("Suggest your system administrator to run \"$kolab_prefix/bin/pear install http://pear.php.net/get/Net_Sieve\" on the server");
}

/**** Authentication etc. ***/
$sidx = 'user';

require_once('admin/include/menu.php');

/**** Submenu for current page ***/
$menuitems[$sidx]['selected'] = 'selected';

/**** Sieve handling ***/
// this is the name KDE client stores - so we can also change the setting from KDE client
$scriptname = "kmail-forward.siv";
if( !$errors ) {
  $obj = $ldap->read( $auth->dn() );
  $sieve =& new Net_Sieve( $auth->uid(), $auth->password(), $obj['kolabHomeServer'][0] );

  // Fetch script data from server
  $scripts = $sieve->listScripts();
  if( in_array( "kolab-deliver.siv", $scripts ) ) {
	$deliverscript = $sieve->getScript( $scriptname );
	
	if( ereg('fileinto "INBOX/(.*)";', $deliverscript, $regs ) ) {
	  $inbox = $regs[1];
	}
  }
  
  // Update sieve script on server in case we have submit data
  if( $_REQUEST['submit'] ) {
	$address = trim($_REQUEST['address']);
	$keep = isset($_REQUEST['keep']);
	$active = isset($_REQUEST['active']);

	if( empty( $address ) ) {
	  $errors[] = _('Please enter an email address');
	} else {
	  $script = 
		"require \"fileinto\";\r\nredirect \"".addslashes($address)."\";".($keep?" keep;":"");
	  if( $inbox ) {
		$script .= "\r\nif header :contains [\"X-Kolab-Scheduling-Message\"] [\"FALSE\"] {\r\nfileinto \"INBOX/$inbox\";\r\n}\r\n";
	  }
	  if( PEAR::isError( $res = $sieve->installScript( $scriptname, $script, $active ) ) ) {
		$errors[] = $res->getMessage();
		$errors[] = _('Script was:');
		$errors[] = '<pre>'.htmlentities($script).'</pre>';
	  }
	  if( !$active && $sieve->getActive() === $scriptname ) {
		$sieve->setActive( '' );
	  }

	  if( !$errors ) {
		if( $active ) $messages[] = _("Forwarding to '$address' successfully activated");
		else $messages[] =  _("Forwarding to '$address' successfully deactivated");
	  }
	}
  }

  if( in_array( $scriptname, $scripts ) ) {
	$script = $sieve->getScript( $scriptname );
	if( ereg('redirect "(.*)";', $script, $regs ) ) {
	  $address = $regs[1];
	}
	$keep = ereg('"; keep;', $script, $regs );
  } else {
	$address = '';
	$keep = true;
  }
  $active = ( $sieve->getActive() === $scriptname );  
}

/**** Insert into template and output ***/
$smarty = new MySmarty();
$smarty->assign( 'errors', $errors );
$smarty->assign( 'messages', $messages );
$smarty->assign( 'uid', $auth->uid() );
$smarty->assign( 'group', $auth->group() );
$smarty->assign( 'page_title', $menuitems[$sidx]['title'] );
$smarty->assign( 'menuitems', $menuitems );
$smarty->assign( 'submenuitems', 
				 array_key_exists('submenu', 
								  $menuitems[$sidx])?$menuitems[$sidx]['submenu']:array() );
$smarty->assign( 'active', $active );
$smarty->assign( 'keep', $keep );
$smarty->assign( 'address', $address );
$smarty->assign( 'maincontent', 'forward.tpl' );
$smarty->display('page.tpl');

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
  vim:encoding=utf-8:
 */
?>