Code

Updating spanish translation for gosa 2.5
[gosa.git] / 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/proxy.png";
45   function Step_Ldap()
46   {
47     $this->update_strings();
48   }
50   
51   function update_strings()
52   {
53     $this->s_title      = _("LDAP setup");
54     $this->s_title_long = _("LDAP connection setup");
55     $this->s_info       = _("This dialog performs the basic configuration of the LDAP connectivity for GOsa.");
56   }
57   
58   
59   function execute()
60   {
61     $smarty = get_smarty();
62     foreach($this->attributes as $attr){
63       $smarty->assign($attr,$this->$attr);
64     }
66     /* Assign connection status */
67     $smarty->assign("connection_status",$this->get_connection_status());
69     /* Handle namingContext detection */
70     $attr = @LDAP::get_naming_contexts($this->connection);
71     unset($attr['count']);
72     $smarty->assign("namingContexts",$attr);
73     $smarty->assign("namingContextsCount",count($attr));
74     $smarty->assign("bool",array(FALSE => _("No"), TRUE => _("Yes")));
76     /* Addign resolved users */
77     $smarty->assign("resolve_user",$this->resolve_user);
78     if($this->resolve_user){
79       $tmp = $this->resolve_user();
80       $smarty->assign("resolved_users",$tmp);
81       $smarty->assign("resolved_users_count",count($tmp));
82       $smarty->assign("resolve_filter",$this->resolve_filter);
83     }
85     $base_to_append = $this->base;
86     if(strlen($base_to_append) > 20){
87       $base_to_append = substr($base_to_append,0,17)."...";
88     }
89     $smarty->assign("base_to_append",$base_to_append);
90     return($smarty -> fetch (get_template_path("../setup/setup_ldap.tpl")));
91   }
93   function get_connection_status()
94   {
95     $this->connect_id = FALSE;
96     $this->bind_id    = FALSE;
98     @ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
99     $this->connect_id = @ldap_connect($this->connection);
100       
101     @ldap_set_option($this->connect_id, LDAP_OPT_PROTOCOL_VERSION, 3);
102     $this->bind_id = @ldap_bind($this->connect_id, $this->admin, $this->password);
103     
104     if(!$this->bind_id){
105       $str = sprintf(_("Anonymous bind failed on server '%s'."),$this->connection); 
106       if(!empty($this->admin)){
107         $str = sprintf(_("Bind as user '%s' failed on server '%s'."),$this->admin,$this->connection);
108       }      
109       return("<font color='red'>".$str."</font>");
110     }else{
111       if(empty($this->admin)){
112         $str = sprintf(_("Anonymous bind on server '%s' succeeded."), $this->connection);
113         return("<font color='blue'>".$str."</font> <font color='red'>"._("Please specify user and password.")."</font>");
114       }else{
115         $str = sprintf(_("Bind as user '%s' on server '%s' succeeded."),$this->admin,$this->connection);
116         return("<font color='green'>".$str."</font>");
117       }      
118     }
119   }
121   
122   function resolve_user()
123   {
124     $filter  = $this->resolve_filter;
125     $ldap = new LDAP("","",$this->connection);
126     $ldap->cd($this->base);
127     $ldap->search("(&(objectClass=person)(|(uid=".$filter.")(cn=".$filter.")))");
128     $tmp = array();
129     while($attrs = $ldap->fetch()){
130       $tmp[base64_encode($attrs['dn'])]= @LDAP::fix($attrs['dn']);
131       natcasesort($tmp);
132     }
133     return($tmp);
134   }   
137   function save_object()
138   {
139     $reset = FALSE;
140     foreach($this->attributes as $attr){
141       if(isset($_POST[$attr])){
142         if(in_array($attr,array("base","connection")) && $this->$attr != get_post($attr)){
143           $reset = TRUE;
144         }
145         $this->$attr = get_post($attr);
146       }
147     }
149     if($reset){
150       $this->parent->disable_steps_from(($this->parent->step_name_to_id(get_class($this))) +1);
151       $attr = @LDAP::get_naming_contexts($this->connection);
152       if(is_array($attr) && !in_array(get_post("base"),$attr)){
153         if(isset($attr[0])){
154           $this->base = $attr[0];
155         }
156       }
157     }
160     if(isset($_POST['resolve_user_x'])){
161       $this->resolve_user = !$this->resolve_user;
162     }
163     if(isset($_POST['resolve_user'])){
164       $this->resolve_user = !$this->resolve_user;
165     }
166   
167     /* Hide backward forward button*/
168     $this->dialog = $this->resolve_user;
169  
170     if(isset($_POST['resolve_filter'])){
171       $this->resolve_filter = get_post('resolve_filter');
172     }
174     if(isset($_POST['use_selected_user'])){
176       if(isset($_POST['admin_to_use'])){
177         $this->admin = base64_decode(get_post('admin_to_use'));
178         $this->resolve_user = false;
179       }
180     }
182     if(isset($_POST['append_base_to_admin_dn'])){
183       $this->append_base_to_admin_dn = TRUE;
184     }else{
185       $this->append_base_to_admin_dn = FALSE;
186     }
187  
188     if($this->append_base_to_admin_dn){
189       $base = $this->base;      
190       if(!preg_match("/,$/",$this->admin_given)){
191         $base = ",".$base;
192       }
193       $this->admin = $this->admin_given.$base;
194     }else{
195       $this->admin = $this->admin_given;
196     }
198     $this->get_connection_status();
199     if($this->bind_id && !empty($this->admin) && !empty($this->base)){
200       $this->is_completed =TRUE;
201     }else{
202       $this->is_completed =FALSE;
203     }
205   }
208 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
209 ?>