Code

Fixed error messages for pseudo attributes
[gosa.git] / plugins / personal / netatalk / class_netatalk.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2006  Gina Haeussge <osd@foosel.net>
5    Copyright (C) 2006  Bernd Zeimetz <bernd@zeimetz.de>
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  */
21  
22 /*! \brief   netatalk plugin
23   \author  Gina Haeussge <osd@foosel.net>
24   \author  Bernd Zeimetz <bernd@zeimetz.de>
25   \version 0.1
26   \date    21.3.2006
28   This class provides the functionality to read and write all attributes
29   relevant for netatalk from/to the LDAP. It does syntax checking
30   and displays the formulars required.
31  */
33 class netatalk extends plugin {
35   /* Definitions */
36   var $plHeadline = "Netatalk";
37   var $plDescription = "Manage netatalk account";
39   /* CLI vars */
40   var $cli_summary = "Manage netatalk account";
41   var $cli_description = "Manage Account \nfor netatalk";
42   var $cli_parameters = array ("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
44   /* Plugin specific values */
45   var $apple_user_homepath_raw   = "";
46   var $apple_user_homeurl_raw     = "";
47   var $apple_user_homeurl_xml     = "";
48   var $apple_user_homeurl       = "";
49   var $apple_user_homeDirectory   = "";
50   var $apple_user_share       = "";
51   var $shares             = array();
52   var $shares_settings        = array();
53   var $selectedshare        = "";
54   var $mountDirectory         = "/Network/Servers";
55   
56   /* Attributes to save to LDAP */
57   var $attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");
58   var $CopyPasteVars= array("apple_user_homeurl", "apple_user_homeDirectory");
60   /* Attributes to use in smarty template */
61   var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
62   
63   /* Attributes to save from $_POST */
64   var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");
66   /* Objectclasses */
67   var $objectclasses = array ("apple-user");
69   /* Checkboxes */
70   var $is_chk_box = array ();
72   var $uid ="";  
74   /* The constructor just saves a copy of the config. You may add what ever you need. */
75   function netatalk($config, $dn = NULL, $parent= NULL) {
77     /* Include config object */
78     $this->config = $config;
79     plugin::plugin($config, $dn, $parent);
81     /* set user id */    
82     if(isset($this->attrs['uid'])){
83       $this->uid = $this->attrs['uid'][0];
84     }
86     /* Copy needed attributes */
87     foreach($this->attributes as $val) {
88       if (isset($this->attrs["$val"][0])) {
89         $name = str_replace('-', '_', $val);
90         $this->$name = $this->attrs["$val"][0];
91       }
92     }
93       
94     if (strlen($this->apple_user_homeDirectory) >0) {
95       $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
96     }
97     
98     /* Save initial account state */
99     $this->initially_was_account = $this->is_account;
100   }
102   /* Execute the plugin, produce the output. */
103   function execute() {
104     plugin :: execute();
106     /* Use the smarty templating engine here... */
107     $smarty = get_smarty();
108     $display = "";
110     /* Do we need to flip is_account state? */
111     if (isset ($_POST['modify_state'])) {
112       $this->is_account = !$this->is_account;
113     }
115     /* Do we represent a valid account? */
116     if (!$this->is_account && $this->parent == NULL) {
117       $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>"._("This account has no netatalk extensions.")."</b>";
119       $display .= back_to_main();
120       return ($display);
121     }
122     
123     
124     /* Get netatalk shares */
125     $this->shares = array();
126     $ldap = $this->config->get_ldap_link();
128     if($this->dn === "new" || $this->dn == NULL) {
129       $ldap->cd($this->parent->by_object['user']->base);
130     } else {
131       $ldap->cd ($this->dn);
132       $ldap->cd ('..'); $ldap->cd ('..');
133     }
134     $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
137     /* Show tab dialog headers */
138     if ($this->parent != NULL) {
139       if ($this->is_account) {
140         $display = $this->show_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
141       } else {
142         $errmsg="";
143         $obj = $this->parent->by_object['posixAccount'];
144         if  (!($obj->is_account) ) {
145           $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
146         }
147         if ($ldap->count() == 0) {
148           $errmsg.="At least one share with netatalk or NFS mount entry needed.";
149         }
150         if($errmsg==""){
151           $display = $this->show_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
152         } else {
153           $display = $this->show_header(_("Create netatalk account"), _($errmsg), TRUE);  
154         }
155         return ($display);
156       }
157     }
158     
159     
160     while ($attrs = $ldap->fetch()){
161       $tmp = split(":", $attrs["cn"][0]);
162       $host = trim($tmp[0]);
163       $dir = trim($tmp[1]);
164       $mountType = trim($attrs["mountType"][0]);
165       if ($mountType == "url") {
166         $mountTypeReal = "netatalk";
167       } else {
168         $mountTypeReal = $mountType;
169       } 
170       $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
171       $this->shares[$share] = $share;
172       $this->shares_settings[$share]["mountType"]=$mountType;
173       $this->shares_settings[$share]["dir"]=$dir;
174       $this->shares_settings[$share]["host"]=$host;
176       $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
177       $newShare=($this->mountDirectory . "/". $host . $dir );
178       if (strcmp($oldShare, $newShare)==0) {
179             $this->selectedshare = $share;
180       }
181     }
182     asort($this->shares);
183     /* Assign attributes and ACL to smarty */
184     
185     $smarty->assign("netatalkShareACL", chkacl($this->acl, "netatalkShare"));
186     $smarty->assign("netatalkUserHomepathACL", chkacl($this->acl, "netatalkUserHomepath"));
187     foreach ($this->smarty_attributes as $val) {
188       $smarty->assign("$val", $this-> $val);
189       if (in_array($val, $this->is_chk_box)) {
190         if ($this-> $val == "checked") {
191           $smarty->assign($val."CHK", " checked ");
192         } else {
193           $smarty->assign($val."CHK", "");
194         }
195       }
196     }
198     /* Let smarty fetch and process the page. */
199     $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
200     return ($display);
201   }
202   
203   
204   /* Check if we have correct data */
205   function check() {
206     $message = array ();
208     if (strlen($this->apple_user_share) == 0) {
209       $message[] = _("You must select a share to use.");
210     }
212     return ($message);
213   }
215   /* Save to LDAP */
216   function save() {
217     /* remove a / at the end of the homepath, we neither need it there nor
218       * do we want to check for it later.
219       */
220     if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
221       $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
222     }
224     $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
225     $dir=$this->shares_settings[$this->apple_user_share]["dir"];
226     $host=$this->shares_settings[$this->apple_user_share]["host"];
227     
228     /* Convert raw data to wished format */
229     if ($this->is_account) {
230       if($mountType=="url") {
231         $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
232         $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
233       } else {
234         $this->apple_user_homeurl = "";
235       }
236       $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
237     } else {
238       $this->apple_user_homeurl = "";
239       $this->apple_user_homeDirectory = "";
240     }
242     $ldap = $this->config->get_ldap_link();
244     /* Call parents save to prepare $this->attrs */
245     plugin :: save();
247     /* Do attribute conversion */
248     foreach ($this->attributes as $val) {
249       $name = str_replace('-', '_', $val);
250       if ($this->$name != "") {
251         $this->attrs[$val] = $this->$name;
252       } else {
253         $this->attrs[$val] = array();
254       }
255       unset ($this->attrs[$name]);
256     }
257       
258     /* Write back to ldap */
259     $ldap->cd($this->dn);
260     $this->cleanup();
261     $ldap->modify($this->attrs);
263     show_ldap_error($ldap->get_error(), _("Saving Netatalk account failed"));
265     /* Optionally execute a command after we're done */
266     if ($this->initially_was_account == $this->is_account) {
267       if ($this->is_modified) {
268         $this->handle_post_events("modify",array("uid" => $this->uid));
269       }
270     } else {
271       $this->handle_post_events("add",array("uid" => $this->uid));
272     }
273   }
275   /* Use Save_object for every Post handling */
276   function save_object() {
277     if (isset ($_POST['netatalkTab'])) {
278       /* Save ldap attributes */
279       plugin :: save_object();
280       
281       foreach($this->post_attributes as $val) {
282         if (isset ($_POST[$val])) {
283           $this->$val = $_POST[$val];
284         } else {
285           $this->$val = "";
286         }
287       }
289       /* Specialhandling for checkboxes */
290       foreach ($this->is_chk_box as $val) {
291         if (isset ($_POST[$val])) {
292           $this-> $val = "checked";
293         } else {
294           $this-> $val = "unchecked";
295         }
296       }
297       
298       $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
299     }
300   }
302   function remove_from_parent() {
303     /* Cancel if there's nothing to do here */
304     if (!$this->initially_was_account) {
305       return;
306     }
308     /* include global link_info */
309     $ldap = $this->config->get_ldap_link();
311     /* Remove and write to LDAP */
312     plugin :: remove_from_parent();
314     /* Adapt attributes if needed */
315     //     $method= new $this->method($this->config);
316     //     $method->fixAttributesOnRemove($this);
318     @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
319     $ldap->cd($this->dn);
320     $this->cleanup();
321     $ldap->modify($this->attrs);
323     show_ldap_error($ldap->get_error(), _("Removing Netatalk account failed"));
325     /* remove the entry from LDAP */
326     unset ($this->attrs['uid']);
328     /* Optionally execute a command after we're done */
329     $this->handle_post_events('remove', array("uid" => $this->uid));
330   }
334 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
335 ?>