1 <?php
3 /*
4 This code is part of GOsa (https://gosa.gonicus.de)
5 Copyright (C) 2007 Fabian Hickert
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
23 class Step_Ldap extends setup_step
24 {
25 var $connection = "ldap://localhost:389";
26 var $location = "default";
27 var $admin = "";
28 var $password = "";
29 var $base = "";
31 var $connect_id = FALSE;
32 var $bind_id = FALSE;
34 var $resolve_filter = "*";
35 var $resolve_user = FALSE;
36 var $tls = FALSE;
38 var $attributes = array("connection","location","admin","password","base","tls");
39 var $header_image= "images/proxy.png";
42 function Step_Ldap()
43 {
44 $this->update_strings();
45 }
48 function update_strings()
49 {
50 $this->s_title = _("LDAP setup");
51 $this->s_title_long = _("LDAP connection setup");
52 $this->s_info = _("This dialog performs the basic configuration of the LDAP connectivity for GOsa.");
53 }
56 function execute()
57 {
58 $smarty = get_smarty();
59 foreach($this->attributes as $attr){
60 $smarty->assign($attr,$this->$attr);
61 }
63 /* Assign connection status */
64 $smarty->assign("connection_status",$this->get_connection_status());
66 /* Handle namingContext detection */
67 $attr = @LDAP::get_naming_contexts($this->connection);
68 unset($attr['count']);
69 $smarty->assign("namingContexts",$attr);
70 $smarty->assign("namingContextsCount",count($attr));
71 $smarty->assign("bool",array(FALSE => _("No"), TRUE => _("Yes")));
73 /* Addign resolved users */
74 $smarty->assign("resolve_user",$this->resolve_user);
75 if($this->resolve_user){
76 $tmp = $this->resolve_user();
77 $smarty->assign("resolved_users",$tmp);
78 $smarty->assign("resolved_users_count",count($tmp));
79 $smarty->assign("resolve_filter",$this->resolve_filter);
80 }
81 return($smarty -> fetch (get_template_path("../setup/setup_ldap.tpl")));
82 }
84 function get_connection_status()
85 {
86 $this->connect_id = FALSE;
87 $this->bind_id = FALSE;
89 @ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
90 $this->connect_id = @ldap_connect($this->connection);
92 @ldap_set_option($this->connect_id, LDAP_OPT_PROTOCOL_VERSION, 3);
93 $this->bind_id = @ldap_bind($this->connect_id, $this->admin, $this->password);
95 if(!$this->bind_id){
96 $str = sprintf(_("Anonymous bind failed on server '%s'."),$this->connection);
97 if(!empty($this->admin)){
98 $str = sprintf(_("Bind as user '%s' failed on server '%s'."),$this->admin,$this->connection);
99 }
100 return("<font color='red'>".$str."</font>");
101 }else{
102 if(empty($this->admin)){
103 $str = sprintf(_("Anonymous bind on server '%s' succeeded. Please specify user and password."),$this->connection);
104 return("<font color='blue'>".$str."</font>");
105 }else{
106 $str = sprintf(_("Bind as user '%s' on server '%s' succeeded."),$this->admin,$this->connection);
107 return("<font color='green'>".$str."</font>");
108 }
109 }
110 }
113 function resolve_user()
114 {
115 $filter = $this->resolve_filter;
116 $ldap = new LDAP("","",$this->connection);
117 $ldap->cd($this->base);
118 $ldap->search("(&(objectClass=person)(|(uid=".$filter.")(cn=".$filter.")))");
119 $tmp = array();
120 while($attrs = $ldap->fetch()){
121 $tmp[base64_encode($attrs['dn'])]= @LDAP::fix($attrs['dn']);
122 natcasesort($tmp);
123 }
124 return($tmp);
125 }
128 function save_object()
129 {
130 foreach($this->attributes as $attr){
131 if(isset($_POST[$attr])){
132 $this->$attr = $_POST[$attr];
133 }
134 }
136 if(isset($_POST['resolve_user_x'])){
137 $this->resolve_user = !$this->resolve_user;
138 }
139 if(isset($_POST['resolve_user'])){
140 $this->resolve_user = !$this->resolve_user;
141 }
143 /* Hide backward forward button*/
144 $this->dialog = $this->resolve_user;
146 if(isset($_POST['resolve_filter'])){
147 $this->resolve_filter = $_POST['resolve_filter'];
148 }
150 if(isset($_POST['use_selected_user'])){
152 if(isset($_POST['admin_to_use'])){
153 $this->admin = base64_decode($_POST['admin_to_use']);
154 $this->resolve_user = false;
155 }
156 }
158 $this->get_connection_status();
159 if($this->bind_id){
160 $this->is_completed =TRUE;
161 }else{
162 $this->is_completed =FALSE;
163 }
164 }
165 }
167 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
168 ?>