Code

Fixed errors
[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   var $shares_loaded= false;
56   
57   /* Attributes to save to LDAP */
58   /* Slip setting those attributes, we can't define class variables with - */
59   var $use_save_attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");
61   /* Attributes to use in smarty template */
62   var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
63   
64   /* Attributes to save from $_POST */
65   var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");
67   /* Objectclasses */
68   var $objectclasses = array ("apple-user");
70   /* Checkboxes */
71   var $is_chk_box = array ();
72   
73   var $uid = "";
74   function get_shares()
75   {
76     $this->shares_loaded= TRUE;
78     /* Get netatalk shares */
79     $this->shares = array();
80     $ldap = $this->config->get_ldap_link();
82     if(($this->dn != "new" && $this->dn != NULL)) {
83       $ldap->cd($this->parent->by_object['user']->base);
84     } else {
85       $ldap->cd ($this->dn);
86       $ldap->cd ('..'); $ldap->cd ('..');
87     }
88     $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
89     
90     while ($attrs = $ldap->fetch()){
91       $tmp = split(":", $attrs["cn"][0]);
92       $host = trim($tmp[0]);
93       $dir = trim($tmp[1]);
94       $mountType = trim($attrs["mountType"][0]);
95       if ($mountType == "url") {
96         $mountTypeReal = "netatalk";
97       } else {
98         $mountTypeReal = $mountType;
99       } 
100       $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
101       $this->shares[$share] = $share;
102       $this->shares_settings[$share]["mountType"]=$mountType;
103       $this->shares_settings[$share]["dir"]=$dir;
104       $this->shares_settings[$share]["host"]=$host;
106       $test = $host.$dir;
107       if(preg_match("#".$test."#",$this->apple_user_homeDirectory)){
108         $this->apple_user_share = $this->selectedshare = $share;
109       }
110     }
111     asort($this->shares);
112   }
114   /* The constructor just saves a copy of the config. You may add what ever you need. */
115   function netatalk($config, $dn = NULL) {
117     /* Include config object */
118     $this->config = $config;
119     plugin :: plugin($config, $dn);
121     /* Setting uid to default */
122     if(isset($this->attrs['uid'][0])){
123       $this->uid = $this->attrs['uid'][0];
124     }
126     /* Copy needed attributes */
127     foreach($this->use_save_attributes as $val) {
128       if (isset($this->attrs["$val"][0])) {
129         $name = str_replace('-', '_', $val);
130         $this->$name = $this->attrs["$val"][0];
131       }
132     }
133       
134     if (strlen($this->apple_user_homeDirectory) >0) {
135       $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
136     }
137  
138     /* Save initial account state */
139     $this->initially_was_account = $this->is_account;
140   }
142   /* Execute the plugin, produce the output. */
143   function execute() {
144     if (!$this->shares_loaded){
145       $this->get_shares();
146     }
148     plugin::execute();
150     /* Use the smarty templating engine here... */
151     $smarty = get_smarty();
152     $display = "";
154     /* Do we need to flip is_account state? */
155     if (isset ($_POST['modify_state'])) {
156       $this->is_account = !$this->is_account;
157     }
159     /* Do we represent a valid account? */
160     if (!$this->is_account && $this->parent == NULL) {
161       $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>"._("This account has no netatalk extensions.")."</b>";
163       $display .= back_to_main();
164       return ($display);
165     }
166     
167     /* Show tab dialog headers */
168     if ($this->parent != NULL) {
169       if ($this->is_account) {
170         $display = $this->show_disable_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
171       } else {
172         $errmsg="";
173         $obj = $this->parent->by_object['posixAccount'];
174         if  (!($obj->is_account) ) {
175           $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
176         }
177         if (count($this->shares)== 0) {
178           $errmsg.="At least one share with netatalk or NFS mount entry needed.";
179         }
180         if($errmsg==""){
181           $display = $this->show_enable_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
182         } else {
183           $display = $this->show_enable_header(_("Create netatalk account"), _($errmsg), TRUE);  
184         }
185         return ($display);
186       }
187     }
188    
189     /* Assign attributes and ACL to smarty */
190     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']); 
191     $smarty->assign("netatalkShareACL", $this->getacl("netatalkShare",$SkipWrite));
192     $smarty->assign("netatalkUserHomepathACL", $this->getacl("netatalkUserHomepath",$SkipWrite));
193     foreach ($this->smarty_attributes as $val) {
194       $smarty->assign("$val", $this-> $val);
195       if (in_array($val, $this->is_chk_box)) {
196         if ($this-> $val == "checked") {
197           $smarty->assign($val."CHK", " checked ");
198         } else {
199           $smarty->assign($val."CHK", "");
200         }
201       }
202     }
204     /* Let smarty fetch and process the page. */
205     $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
206     return ($display);
207   }
208   
209   
210   /* Check if we have correct data */
211   function check() {
212     $message = array ();
214     if (strlen($this->apple_user_share) == 0) {
215       $message[] = _("You must select a share to use.");
216     }
218     return ($message);
219   }
221   /* Save to LDAP */
222   function save() {
223     /* remove a / at the end of the homepath, we neither need it there nor
224       * do we want to check for it later.
225       */
226     if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
227       $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
228     }
230     $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
231     $dir=$this->shares_settings[$this->apple_user_share]["dir"];
232     $host=$this->shares_settings[$this->apple_user_share]["host"];
233     
234     /* Convert raw data to wished format */
235     if ($this->is_account) {
236       if($mountType=="url") {
237         $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
238         $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
239       } else {
240         $this->apple_user_homeurl = "";
241       }
242       $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
243     } else {
244       $this->apple_user_homeurl = "";
245       $this->apple_user_homeDirectory = "";
246     }
248     $ldap = $this->config->get_ldap_link();
250     /* Call parents save to prepare $this->attrs */
251     plugin :: save();
253     /* Do attribute conversion */
254     foreach ($this->use_save_attributes as $val) {
255       $name = str_replace('-', '_', $val);
256       if ($this->$name != "") {
257         $this->attrs[$val] = $this->$name;
258       } else {
259         $this->attrs[$val] = array();
260       }
261       unset ($this->attrs[$name]);
262     }
263     /* Write back to ldap */
264     $ldap->cd($this->dn);
265     $this->cleanup();
266     $ldap->modify($this->attrs);
268     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/netatalk account with dn '%s' failed."),$this->dn));
270     /* Optionally execute a command after we're done */
271     if ($this->initially_was_account == $this->is_account) {
272       if ($this->is_modified) {
273         $this->handle_post_events("modify", array("uid" => $this->uid));
274       }
275     } else {
276       $this->handle_post_events("add", array("uid" => $this->uid));
277     }
278   }
280   /* Use Save_object for every Post handling */
281   function save_object() {
282     if (isset ($_POST['netatalkTab'])) {
283       /* Save ldap attributes */
284 //      plugin :: save_object();
285       
286       foreach($this->post_attributes as $val) {
287         if (isset ($_POST[$val])) {
288           $this->$val = $_POST[$val];
289         } else {
290           $this->$val = "";
291         }
292       }
294       /* Specialhandling for checkboxes */
295       foreach ($this->is_chk_box as $val) {
296         if (isset ($_POST[$val])) {
297           $this-> $val = "checked";
298         } else {
299           $this-> $val = "unchecked";
300         }
301       }
302       
303       $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
304     }
305   }
307   function remove_from_parent() {
308     /* Cancel if there's nothing to do here */
309     if (!$this->initially_was_account) {
310       return;
311     }
313     /* include global link_info */
314     $ldap = $this->config->get_ldap_link();
316     /* Remove and write to LDAP */
317     plugin :: remove_from_parent();
319     /* Adapt attributes if needed */
320     //     $method= new $this->method($this->config);
321     //     $method->fixAttributesOnRemove($this);
323     @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
324     $ldap->cd($this->dn);
325     $this->cleanup();
326     $ldap->modify($this->attrs);
328     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/netatalk account with dn '%s' failed."),$this->dn));
330     /* remove the entry from LDAP */
331     unset ($this->attrs['uid']);
333     /* Optionally execute a command after we're done */
334     $this->handle_post_events('remove',array("uid" => $this->uid));
335   }
337   
338   /* Return plugin informations for acl handling*/
339   function plInfo()
340   {
341     return (array(
342           "plDescription"     => _("Netatalk"),
343           "plSelfModify"      => TRUE,
344           "plDepends"         => array("user"),
345           "plPriority"        => 1,
346           "plSection"         => "personal",
347           "plCategory"        => array("users"),
348           "plOptions"         => array(),
350           "plProvidedAcls"  => array(
351             "netatalkUserHomepath"   =>  _("User home path"),
352             "netatalkShare"          =>  _("Share"))
353           ));
354   }
357 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
358 ?>