summaryrefslogtreecommitdiff
path: root/www/admin/user/vacation.php
blob: 789f28e433e8306e3191f2ac5d5860173ecdd0b0 (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
135
136
<?php
/*
 (c) 2004 Klarälvdalens Datakonsult AB

 This program is Free Software under the GNU General Public License (>=v2).
 Read the file COPYING that comes with this packages for details.
*/

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 ***/
$scriptname = 'kmail-vacation.siv';
if( !$errors ) {
  $obj = $ldap->read( $auth->dn() );
  $sieve =& new Net_Sieve( $auth->uid(), $auth->password(), $obj['kolabHomeServer'][0] );
  
  // Update sieve script on server in case we have submit data
  if( $_REQUEST['submit'] ) {
	$addresses = array_unique( array_filter( array_map( 'trim', preg_split( '/\n/', $_POST['addresses'] ) ), 'strlen') );

	$script = 
	  "require \"vacation\";\r\n\r\n".
	  "vacation :addresses [ \"".join('", "', $addresses )."\" ] :days ".
	  $_REQUEST['days']." text:\r\n".
	  dotstuff($_REQUEST['text'])."\r\n.\r\n;";
	$active = isset($_REQUEST['active']);

	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[] = _('Vacation message successfully activated');
	  else $messages[] = _('Vacation message successfully deactivated');
	}
  }

  // Fetch script data from server
  $scripts = $sieve->listScripts();
  if( in_array( $scriptname, $scripts ) ) {
	$script = $sieve->getScript( $scriptname );
	//debug("<pre>".htmlentities($script)."</pre>");
	if( ereg(':addresses \[(.*)\]', $script, $regs ) ) {
	  $tmp = split(',', $regs[1] );
	  $addresses = array();
	  foreach( $tmp as $a ) {
		if( ereg('^ *"(.*)" *$', $a, $regs ) ) $addresses[] = $regs[1];
		else $addresses[] = $a;
	  }
	}
	if( ereg(':days ([0-9]+)', $script, $regs ) ) {
	  $days = $regs[1];
	}
	if( ereg("text:\r\n(.*)\r\n\\.\r\n", $script, $regs ) ) {
	  $text = $regs[1];
	  $text = str_replace( '\n', "\r\n", $text );
	  $text = undotstuff($text);
	}
  }
  if( !$addresses ) {
	$object = $ldap->read( $auth->dn() );
	$addresses = array_merge( $object['mail'], $object['alias'] );
  }
  if( !$days ) $days = 7;
  if( !$text ) {
	$date = strftime(_('%x'));
	$text = _("I am out of office till $date.\r\n".
	  "In urgent cases, please contact Mrs. <vacation replacement>\r\n\r\n".
	  "email: <email address of vacation replacement>\r\n".
	  "phone: +49 711 1111 11\r\n".
	  "fax.:  +49 711 1111 12\r\n\r\n".
	  "Yours sincerely,\r\n".
	  "-- \r\n".
	  "<enter your name and email address here>");
  }
  $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( 'text', $text );
$smarty->assign( 'addresses', $addresses );
$smarty->assign( 'days', $days );
$smarty->assign( 'maincontent', 'vacation.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:
 */
?>