Code

Applied in_array strict patches from trunk
[gosa.git] / gosa-core / setup / class_setupStep_Ldap.inc
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       = "";
30   var $append_base_to_admin_dn = FALSE;
31   var $admin_given = "";
33   var $connect_id = FALSE;
34   var $bind_id    = FALSE;
36   var $resolve_filter = "*";
37   var $resolve_user   = FALSE;
38   var $tls            = FALSE;
40   var $rfc2307bis             = FALSE;
41   var $attributes = array("connection","location","admin","password","base","admin_given","append_base_to_admin_dn","tls","rfc2307bis");
43   var $header_image= "images/setup/ldap.png";
45     var $b_displayCheckbutton = TRUE;
46   function Step_Ldap()
47   {
48     $this->update_strings();
49   }
51   
52   function update_strings()
53   {
54     $this->s_title      = _("LDAP setup");
55     $this->s_title_long = _("LDAP connection setup");
56     $this->s_info       = _("This dialog performs the basic configuration of the LDAP connectivity for GOsa.");
57   }
58   
59   
60   function execute()
61   {
62     $smarty = get_smarty();
63     foreach($this->attributes as $attr){
64       $smarty->assign($attr,htmlentities($this->$attr,ENT_QUOTES,"UTF-8"));
65     }
67     /* Assign connection status */
68     $smarty->assign("connection_status",$this->get_connection_status());
70     /* Handle namingContext detection */
71     $attr = @LDAP::get_naming_contexts($this->connection);
72     unset($attr['count']);
73     $smarty->assign("namingContexts",$attr);
74     $smarty->assign("namingContextsCount",count($attr));
75     $smarty->assign("bool",array(FALSE => _("No"), TRUE => _("Yes")));
77     /* Addign resolved users */
78     $smarty->assign("resolve_user",$this->resolve_user);
79     if($this->resolve_user){
80       $tmp = $this->resolve_user();
81       $smarty->assign("resolved_users",$tmp);
82       $smarty->assign("resolved_users_count",count($tmp));
83       $smarty->assign("resolve_filter",$this->resolve_filter);
84     }
86     $base_to_append = $this->base;
87     if(strlen($base_to_append) > 20){
88       $base_to_append = substr($base_to_append,0,17)."...";
89     }
90     $smarty->assign("base_to_append",$base_to_append);
91     return($smarty -> fetch (get_template_path("../setup/setup_ldap.tpl")));
92   }
94   function get_connection_status()
95   {
96     $this->connect_id = FALSE;
97     $this->bind_id    = FALSE;
99     @ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
100     $this->connect_id = ldap_connect($this->connection);
101       
102     if($this->tls){
103       if(@ldap_set_option($this->connect_id, LDAP_OPT_REFERRALS, 0))
104         if(@ldap_start_tls($this->connect_id))
105           $this->bind_id = @ldap_bind($this->connect_id, $this->admin, $this->password);
106       @ldap_set_option($this->connect_id, LDAP_OPT_PROTOCOL_VERSION, 3);
107     }else{
108       @ldap_set_option($this->connect_id, LDAP_OPT_PROTOCOL_VERSION, 3);
109       $this->bind_id = @ldap_bind($this->connect_id, $this->admin, $this->password);
110     }
111     
112     if(!$this->bind_id){
113       $str = sprintf(_("Anonymous bind to server '%s' failed!"),$this->connection); 
114       if(!empty($this->admin)){
115         $str = sprintf(_("Bind as user '%s' failed!"),$this->admin,$this->connection);
116       }      
117       return("<font color='red'>".$str."</font>");
118     }else{
119       if(empty($this->admin)){
120         $str = sprintf(_("Anonymous bind to server '%s' succeeded."), $this->connection);
121         return("<font color='blue'>".$str."</font> <font color='red'>"._("Please specify user and password!")."</font>");
122       }else{
123         $str = sprintf(_("Bind as user '%s' to server '%s' succeeded!"),$this->admin,$this->connection);
124         return("<font color='green'>".$str."</font>");
125       }      
126     }
127   }
129   
130   function resolve_user()
131   {
132     $filter  = $this->resolve_filter;
134     /* Establish ldap connection */
135     $cv = $this->parent->captured_values;
136     $ldap_l = new LDAP("","",$this->connection, FALSE, $this->tls);
137     $ldap = new ldapMultiplexer($ldap_l);
138     $ldap->cd($this->base);
139     $ldap->search("(&(objectClass=person)(|(uid=".$filter.")(cn=".$filter.")))");
140     $tmp = array();
141     while($attrs = $ldap->fetch()){
142       $tmp[base64_encode($attrs['dn'])]= LDAP::fix($attrs['dn']);
143       natcasesort($tmp);
144     }
145     return($tmp);
146   }   
149   function save_object()
150   {
151     $reset = FALSE;
152     foreach($this->attributes as $attr){
153       if(isset($_POST[$attr])){
154         if(in_array_strict($attr,array("base","connection")) && $this->$attr != get_post($attr)){
155           $reset = TRUE;
156         }
157         $this->$attr = get_post($attr);
158       }
159     }
161     if($reset){
162       $this->parent->disable_steps_from(($this->parent->step_name_to_id(get_class($this))) +1);
163       $attr = @LDAP::get_naming_contexts($this->connection);
164       if(is_array($attr) && !in_array_strict(get_post("base"),$attr)){
165         if(isset($attr[0])){
166           $this->base = $attr[0];
167         }
168       }
169     }
171     if(isset($_POST['resolve_user_x'])){
172       $this->resolve_user = !$this->resolve_user;
173     }
174     if(isset($_POST['resolve_user'])){
175       $this->resolve_user = !$this->resolve_user;
176     }
177   
178     /* Hide backward forward button*/
179     $this->dialog = $this->resolve_user;
180  
181     if(isset($_POST['resolve_filter'])){
182       $this->resolve_filter = get_post('resolve_filter');
183     }
185     if(isset($_POST['use_selected_user'])){
187       if(isset($_POST['admin_to_use'])){
188         $this->admin = base64_decode(get_post('admin_to_use'));
189         $this->resolve_user = false;
190       }
191     }
193     if(isset($_POST['append_base_to_admin_dn'])){
194       $this->append_base_to_admin_dn = TRUE;
195     }else{
196       $this->append_base_to_admin_dn = FALSE;
197     }
198  
199     if($this->append_base_to_admin_dn){
200       $base = $this->base;      
201       if(!preg_match("/,$/",$this->admin_given)){
202         $base = ",".$base;
203       }
204       $this->admin = $this->admin_given.$base;
205     }else{
206       $this->admin = $this->admin_given;
207     }
209     $this->get_connection_status();
210     if($this->bind_id && !empty($this->admin) && !empty($this->base)){
211       $this->is_completed =TRUE;
212     }else{
213       $this->is_completed =FALSE;
214     }
216   }
219 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
220 ?>