1<?php
2	/***************************************************************************\
3	* phpGroupWare - QMailLDAP                                                  *
4	* http://www.phpgroupware.org                                               *
5	* Written by: Lars Kneschke [lkneschke@phpgw.de]                            *
6	* ----------------------------------------------                            *
7	* This program is free software; you can redistribute it and/or modify it   *
8	* under the terms of the GNU General Public License as published by the     *
9	* Free Software Foundation; either version 2 of the License, or (at your    *
10	* option) any later version.                                                *
11	\***************************************************************************/
12	/* $Id: class.uiqmailldap.inc.php 15765 2005-03-02 22:51:49Z ceb $ */
13
14	class uiqmailldap
15	{
16		var $public_functions = array
17		(
18			'listServers'	=> True,
19			'addServer'		=> True,
20			'deleteServer'	=> True,
21			'editServer'	=> True,
22			'editSettings'	=> True,
23			'addSmtpRoute'	=> True,
24			'save'			=> True
25		);
26
27		function uiqmailldap()
28		{
29			//$this->cats		= CreateObject('phpgwapi.categories');
30			$this->nextmatchs	= CreateObject('phpgwapi.nextmatchs');
31			$this->boqmailldap	= CreateObject('qmailldap.boqmailldap');
32
33			$this->rowColor[0]	= $GLOBALS['phpgw_info']['theme']['row_on'];
34			$this->rowColor[1]	= $GLOBALS['phpgw_info']['theme']['row_off'];
35		}
36
37		function addServer()
38		{
39			$this->display_app_header();
40
41			$GLOBALS['phpgw']->template->set_file(array('body' => 'ldapsettings.tpl'));
42			$GLOBALS['phpgw']->template->set_block('body','main');
43			$GLOBALS['phpgw']->template->set_block('body','menu_row');
44			$GLOBALS['phpgw']->template->set_block('body','menu_row_bold');
45			$GLOBALS['phpgw']->template->set_block('body','activation_row');
46
47			$this->translate();
48			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('qmailldap') . ': ' . lang('add server');
49
50			$GLOBALS['phpgw']->template->set_var('done_row_color',$this->rowColor[($i)%2]);
51			$linkData = array
52			(
53				'menuaction'	=> 'qmailldap.uiqmailldap.listServers'
54			);
55			$GLOBALS['phpgw']->template->set_var('done_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
56
57			$linkData = array
58			(
59				'menuaction' => 'qmailldap.uiqmailldap.save'
60			);
61			$GLOBALS['phpgw']->template->set_var('form_action',$GLOBALS['phpgw']->link('/index.php',$linkData));
62			$GLOBALS['phpgw']->template->pfp('out','main');
63		}
64
65		function addSmtpRoute()
66		{
67			$this->display_app_header();
68			$this->translate();
69		}
70
71		function createMenu($_serverid, $_pagenumber, $_ldapData)
72		{
73			$menu = array
74			(
75				'0'	=> array
76					   (
77					   	'name'		=> lang('domain names'),
78					   	'template'	=> 'domainnames.tpl'
79					   ),
80				'10'	=> array
81					   (
82					   	'name'		=> lang('virtual domains'),
83					   	'template'	=> 'defaultpage.tpl'
84					   ),
85				'15'	=> array
86					   (
87					   	'name'		=> lang('smtp routing'),
88					   	'template'	=> 'smtprouting.tpl'
89					   ),
90				'20'	=> array
91					   (
92					   	'name'		=> lang('options'),
93					   	'template'	=> 'options.tpl'
94					   )
95			);
96
97			$GLOBALS['phpgw']->template->set_file(array('body' => $menu[$_pagenumber]['template']));
98			$GLOBALS['phpgw']->template->set_block('body','menu_row');
99			$GLOBALS['phpgw']->template->set_block('body','menu_row_bold');
100			$GLOBALS['phpgw']->template->set_block('body','activation_row');
101
102			reset($menu);
103			$i=0;
104			foreach($menu as $key => $value)
105			{
106				$GLOBALS['phpgw']->template->set_var('menu_description',$value['name']);
107				$linkData = array
108				(
109					'menuaction'	=> 'qmailldap.uiqmailldap.editServer',
110					'pagenumber'	=> $key,
111					'serverid'		=> $_serverid
112				);
113				$GLOBALS['phpgw']->template->set_var('menu_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
114				$GLOBALS['phpgw']->template->set_var('menu_row_color',$this->rowColor[$i%2]);
115				if ($_pagenumber == $key)
116				{
117					$GLOBALS['phpgw']->template->parse('menu_rows','menu_row_bold',True);
118				}
119				else
120				{
121					$GLOBALS['phpgw']->template->parse('menu_rows','menu_row',True);
122				}
123				++$i;
124			}
125
126			if ($_ldapData['needActivation'] == 1)
127			{
128				$linkData = array
129				(
130					'menuaction'	=> 'qmailldap.uiqmailldap.save',
131					'pagenumber'	=> $_pagenumber,
132					'serverid'		=> $_serverid,
133					'bo_action'		=> 'write_to_ldap'
134				);
135				$GLOBALS['phpgw']->template->set_var('activation_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
136				$GLOBALS['phpgw']->template->parse('activation_rows','activation_row');
137			}
138
139			$GLOBALS['phpgw']->template->set_var('done_row_color',$this->rowColor[($i)%2]);
140			$linkData = array
141			(
142				'menuaction' => 'qmailldap.uiqmailldap.listServers',
143			);
144			$GLOBALS['phpgw']->template->set_var('done_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
145
146		}
147
148		function deleteServer()
149		{
150			$this->boqmailldap->deleteServer($_GET['serverid']);
151			$this->listServers();
152		}
153
154		function display_app_header()
155		{
156			$GLOBALS['phpgw']->common->phpgw_header();
157			echo parse_navbar();
158		}
159
160		function editServer($_serverid='', $_pagenumber='')
161		{
162			$serverid		= get_var('serverid',array('POST','GET'));
163			$pagenumber		= get_var('pagenumber',array('POST','GET'));
164
165			if(!empty($_serverid))
166			{
167				$serverid = $_serverid;
168			}
169
170			if(!empty($_pagenumber))
171			{
172				$pagenumber = $_pagenumber;
173			}
174
175			$ldapData = $this->boqmailldap->getLDAPData($serverid);
176
177			$this->display_app_header();
178
179			$this->createMenu($serverid, $pagenumber, $ldapData);
180
181			$GLOBALS['phpgw']->template->set_block('body','main');
182			$this->translate();
183			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('qmailldap') . ': ' . lang('edit server');
184
185			$linkData = array
186			(
187				'menuaction'	=> 'qmailldap.uiqmailldap.save',
188				'pagenumber'	=> $pagenumber,
189				'serverid'		=> $serverid
190			);
191			$GLOBALS['phpgw']->template->set_var('form_action',$GLOBALS['phpgw']->link('/index.php',$linkData));
192
193			switch($pagenumber)
194			{
195				case '0':
196					if (count($ldapData['rcpthosts']) > 0)
197					{
198						$selectBox  = '<select size="10" name="values[rcpthosts]">' . "\n";
199						for ($i=0;$i < count($ldapData['rcpthosts']);++$i)
200						{
201							$selectBox .= '<option value="' . $i .'">' . $ldapData['rcpthosts'][$i] . '</option>' . "\n";
202						}
203						$selectBox .= '</select>' . "\n";
204						$GLOBALS['phpgw']->template->set_var('rcpt_selectbox',$selectBox);
205					}
206					else
207					{
208						$GLOBALS['phpgw']->template->set_var('rcpt_selectbox','<b>' . lang('We do not accept any email') . '</b>');
209					}
210
211					if (count($ldapData['locals']) > 0)
212					{
213						$selectBox  = '<select size="10" name="values[locals]">' . "\n";
214						for ($i=0;$i < count($ldapData['locals']);++$i)
215						{
216							$selectBox .= '<option value="' . $i . '">' . $ldapData['locals'][$i] . '</option>' . "\n";
217						}
218						$selectBox .= '</select>' . "\n";
219						$GLOBALS['phpgw']->template->set_var('locals_selectbox',$selectBox);
220					}
221					else
222					{
223						$GLOBALS['phpgw']->template->set_var('locals_selectbox','<b>' . lang('We do not deliver any email locally') . '</b>');
224					}
225					break;
226				case '15':
227					$GLOBALS['phpgw']->template->set_block('body','smtproute_row');
228
229					if (count($ldapData['smtproutes']) > 0)
230					{
231						for ($i=0;$i<count($ldapData['smtproutes']);++$i)
232						{
233							$smtproute = explode(':',$ldapData['smtproutes'][$i]);
234							$GLOBALS['phpgw']->template->set_var('domain_name',$smtproute[0]);
235							$GLOBALS['phpgw']->template->set_var('remote_server',$smtproute[1]);
236							$GLOBALS['phpgw']->template->set_var('remote_port',$smtproute[2]);
237
238							$linkData = array
239							(
240								'menuaction'	=> 'qmailldap.uiqmailldap.save',
241								'bo_action'		=> 'remove_smtproute',
242								'smtproute_id'	=> $i,
243								'pagenumber'	=> 15,
244								'serverid'		=> $serverid
245							);
246							$this->nextmatchs->template_alternate_row_color(&$GLOBALS['phpgw']->template);
247							$GLOBALS['phpgw']->template->set_var('delete_route_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
248							$GLOBALS['phpgw']->template->parse('smtproute_rows','smtproute_row',True);
249						}
250					}
251
252					$linkData = array
253					(
254						'menuaction'	=> 'qmailldap.uiqmailldap.addSmtpRoute',
255						'pagenumber'	=> 15,
256						'serverid'		=> $serverid
257					);
258					$GLOBALS['phpgw']->template->set_var('add_route_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
259					break;
260				case '20':
261					$GLOBALS['phpgw']->template->set_var('ldaplocaldelivery_'.$ldapData['ldaplocaldelivery'],'selected');
262					$GLOBALS['phpgw']->template->set_var('ldapdefaultdotmode_'.$ldapData['ldapdefaultdotmode'],'selected');
263					$GLOBALS['phpgw']->template->set_var('ldapbasedn',$ldapData['ldapbasedn']);
264					break;
265				case '99':
266					if ($storageData = $this->boqmailldap->getLDAPStorageData($serverid))
267					{
268						$GLOBALS['phpgw']->template->set_var('qmail_servername',$storageData['qmail_servername']);
269						$GLOBALS['phpgw']->template->set_var('description',$storageData['description']);
270						$GLOBALS['phpgw']->template->set_var('ldap_basedn',$storageData['ldap_basedn']);
271					}
272					break;
273			}
274			$GLOBALS['phpgw']->template->pfp('out','main');
275		}
276
277		function editSettings($_serverid='')
278		{
279			$serverid = get_var('serverid',array('POST','GET'));
280
281			if(!empty($_serverid))
282			{
283				$serverid = $_serverid;
284			}
285
286			$ldapData = $this->boqmailldap->getLDAPData($serverid);
287
288			$this->display_app_header();
289
290			$GLOBALS['phpgw']->template->set_file(array('body' => 'ldapsettings.tpl'));
291			$GLOBALS['phpgw']->template->set_block('body','main');
292			$GLOBALS['phpgw']->template->set_block('body','menu_row');
293			$GLOBALS['phpgw']->template->set_block('body','menu_row_bold');
294			$GLOBALS['phpgw']->template->set_block('body','activation_row');
295
296			$this->translate();
297			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('qmailldap') . ': ' . lang('edit settings');
298
299			if ($storageData = $this->boqmailldap->getLDAPStorageData($serverid))
300			{
301				$GLOBALS['phpgw']->template->set_var('qmail_servername',$storageData['qmail_servername']);
302				$GLOBALS['phpgw']->template->set_var('description',$storageData['description']);
303				$GLOBALS['phpgw']->template->set_var('ldap_basedn',$storageData['ldap_basedn']);
304			}
305
306			$GLOBALS['phpgw']->template->set_var('done_row_color',$this->rowColor[($i)%2]);
307			$linkData = array
308			(
309				'menuaction' => 'qmailldap.uiqmailldap.listServers'
310			);
311			$GLOBALS['phpgw']->template->set_var('done_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
312
313			$linkData = array
314			(
315				'menuaction'    => 'qmailldap.uiqmailldap.save',
316				'pagenumber'    => $pagenumber,
317				'serverid'      => $serverid
318			);
319			$GLOBALS['phpgw']->template->set_var('form_action',$GLOBALS['phpgw']->link('/index.php',$linkData));
320
321			$GLOBALS['phpgw']->template->pfp('out','main');
322		}
323
324		function listServers()
325		{
326			$this->display_app_header();
327
328			$GLOBALS['phpgw']->template->set_file(array('body' => 'listservers.tpl'));
329			$GLOBALS['phpgw']->template->set_block('body','main','main');
330			$GLOBALS['phpgw']->template->set_block('body','row','row');
331
332			$this->translate();
333			$GLOBALS['phpgw_info']['flags']['app_header'] = lang('qmailldap') . ': ' . lang('list servers');
334
335			$serverList = $this->boqmailldap->getServerList();
336
337			if ($serverList)
338			{
339				for ($i=0; $i < count($serverList);++$i)
340				{
341					$GLOBALS['phpgw']->template->set_var('server_name',$serverList[$i]['qmail_servername']);
342					$GLOBALS['phpgw']->template->set_var('server_description',$serverList[$i]['description']);
343					$linkData = array
344					(
345						'menuaction'	=> 'qmailldap.uiqmailldap.editServer',
346						'nocache'		=> 1,
347						'pagenumber'	=> 0,
348						'serverid'		=> $serverList[$i]['id']
349					);
350					$GLOBALS['phpgw']->template->set_var('edit_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
351					$linkData = array
352					(
353						'menuaction'	=> 'qmailldap.uiqmailldap.editSettings',
354						'nocache'		=> 1,
355						'serverid'		=> $serverList[$i]['id']
356					);
357					$GLOBALS['phpgw']->template->set_var('settings_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
358					$linkData = array
359					(
360						'menuaction'	=> 'qmailldap.uiqmailldap.deleteServer',
361						'serverid'		=> $serverList[$i]['id']
362					);
363					$GLOBALS['phpgw']->template->set_var('delete_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
364					$this->nextmatchs->template_alternate_row_color(&$GLOBALS['phpgw']->template);
365					$GLOBALS['phpgw']->template->parse('rows','row',True);
366				}
367			}
368
369			$linkData = array
370			(
371				'menuaction' => 'qmailldap.uiqmailldap.addServer'
372			);
373			$GLOBALS['phpgw']->template->set_var('add_link',$GLOBALS['phpgw']->link('/index.php',$linkData));
374			$GLOBALS['phpgw']->template->pfp('out','main');
375		}
376
377		function save()
378		{
379			$bo_action	= get_var('bo_action',array('POST','GET'));
380			$serverid	= get_var('serverid',array('POST','GET'));
381			$pagenumber	= get_var('pagenumber',array('POST','GET'));
382			$smtproute_id = $_GET['smtproute_id'];
383
384			$values = get_var('values',array('POST','GET'));
385
386			$values['bo_action']	= $bo_action;
387			$values['serverid']		= $serverid;
388			$values['smtproute_id']	= $_GET['smtproute_id'];
389
390			$this->boqmailldap->save($values);
391			if ($bo_action == 'save_ldap')
392			{
393				$this->listServers();
394			}
395			else
396			{
397				$this->editServer($serverid,$pagenumber);
398			}
399		}
400
401		function translate()
402		{
403			$GLOBALS['phpgw']->template->set_var('th_bg',$GLOBALS['phpgw_info']['theme']['th_bg']);
404			$GLOBALS['phpgw']->template->set_var('bg_01',$GLOBALS['phpgw_info']['theme']['bg01']);
405			$GLOBALS['phpgw']->template->set_var('bg_02',$GLOBALS['phpgw_info']['theme']['bg02']);
406
407			$GLOBALS['phpgw']->template->set_var('lang_server_list',lang('server list'));
408			$GLOBALS['phpgw']->template->set_var('lang_server_name',lang('server name'));
409			$GLOBALS['phpgw']->template->set_var('lang_server_description',lang('description'));
410			$GLOBALS['phpgw']->template->set_var('lang_activate',lang('Activate'));
411			$GLOBALS['phpgw']->template->set_var('lang_edit',lang('edit'));
412			$GLOBALS['phpgw']->template->set_var('lang_save',lang('save'));
413			$GLOBALS['phpgw']->template->set_var('lang_delete',lang('delete'));
414			$GLOBALS['phpgw']->template->set_var('lang_disabled',lang('disabled'));
415			$GLOBALS['phpgw']->template->set_var('lang_enabled',lang('enabled'));
416			$GLOBALS['phpgw']->template->set_var('lang_add',lang('add'));
417			$GLOBALS['phpgw']->template->set_var('lang_done',lang('Done'));
418			$GLOBALS['phpgw']->template->set_var('lang_back',lang('back'));
419			$GLOBALS['phpgw']->template->set_var('lang_remove',lang('remove'));
420			$GLOBALS['phpgw']->template->set_var('lang_add_to_local',lang('add also to local domains'));
421			$GLOBALS['phpgw']->template->set_var('lang_ldap_server',lang('LDAP server'));
422			$GLOBALS['phpgw']->template->set_var('lang_qmail_dn',lang('qmail dn'));
423			$GLOBALS['phpgw']->template->set_var('lang_ldap_server_admin',lang('admin dn'));
424			$GLOBALS['phpgw']->template->set_var('lang_ldap_server_password',lang('admin password'));
425			$GLOBALS['phpgw']->template->set_var('lang_add_server',lang('add server'));
426			$GLOBALS['phpgw']->template->set_var('lang_domain_name',lang('domainname'));
427			$GLOBALS['phpgw']->template->set_var('lang_remote_server',lang('remote server'));
428			$GLOBALS['phpgw']->template->set_var('lang_remote_port',lang('remote port'));
429
430			$GLOBALS['phpgw']->template->set_var('desc_ldaplocaldelivery',lang('To lookup the local passwd file if the LDAP lookup finds no match. This affects qmail-lspawn and auth_* if the LDAP lookup returns nothing.'));
431			$GLOBALS['phpgw']->template->set_var('desc_ldapdefaultdotmode',lang('The default interpretation of .qmail files.<br><b>Note:</b> Works only for deliveries based on LDAP lookups. Local mails use dotonly like in normal qmail.'));
432			$GLOBALS['phpgw']->template->set_var('desc_ldapbasedn',lang('The base DN from where the search in the LDAP tree begins.'));
433		}
434	}
435?>
436