Code

Starting move
[gosa.git] / gosa-core / 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  */
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");
43   var $view_logged = FALSE;
45   /* Plugin specific values */
46   var $apple_user_homepath_raw   = "";
47   var $apple_user_homeurl_raw     = "";
48   var $apple_user_homeurl_xml     = "";
49   var $apple_user_homeurl       = "";
50   var $apple_user_homeDirectory   = "";
51   var $apple_user_share       = "";
52   var $shares             = array();
53   var $shares_settings        = array();
54   var $selectedshare        = "";
55   var $mountDirectory         = "/Network/Servers";
57   /* Attributes to save to LDAP */
58   var $attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");
59   var $CopyPasteVars= array("apple_user_homeurl", "apple_user_homeDirectory","apple_user_share","shares_settings","apple_user_homepath_raw",
60       "apple_user_homeurl_raw","apple_user_homeurl_xml","apple_user_homeurl","selectedshare","mountDirectory");
62   /* Attributes to use in smarty template */
63   var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
65   /* Attributes to save from $_POST */
66   var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");
68   /* Objectclasses */
69   var $objectclasses = array ("apple-user");
71   /* Checkboxes */
72   var $is_chk_box = array ();
74   var $uid ="";  
76   /* The constructor just saves a copy of the config. You may add what ever you need. */
77   function netatalk(&$config, $dn = NULL) {
79     /* Include config object */
80     $this->config = $config;
81     plugin::plugin($config, $dn);
83     /* set user id */    
84     if(isset($this->attrs['uid'])){
85       $this->uid = $this->attrs['uid'][0];
86     }
88     /* Copy needed attributes */
89     foreach($this->attributes as $val) {
90       if (isset($this->attrs["$val"][0])) {
91         $name = str_replace('-', '_', $val);
92         $this->$name = $this->attrs["$val"][0];
93       }
94     }
96     if (strlen($this->apple_user_homeDirectory) >0) {
97       $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
98     }
100     /* get share list an set default values */
101     $this->get_netatalk_shares(); 
102     $this->apple_user_share = $this->selectedshare;
104     /* Save initial account state */
105     $this->initially_was_account = $this->is_account;
106   }
110   /* Get netatalk shares */
111   function get_netatalk_shares()
112   {
113     /* Get netatalk shares */
114     $this->shares = array();
115     $ldap = $this->config->get_ldap_link();
117     if($this->dn === "new" || $this->dn === NULL) {
118       $base = $_SESSION['CurrentMainBase'];
119     } else {
120       $base = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/","",$this->dn);
121     }
123     $ldap->cd($base);
124     $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
126     while ($attrs = $ldap->fetch()){
127       $tmp  = split(":", $attrs["cn"][0]);
128       $host = trim($tmp[0]);
129       $dir  = trim($tmp[1]);
130       $mountType = trim($attrs["mountType"][0]);
131       if ($mountType == "url") {
132         $mountTypeReal = "netatalk";
133       } else {
134         $mountTypeReal = $mountType;
135       } 
136       $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
137       $this->shares[$share] = $share;
138       $this->shares_settings[$share]["mountType"]=$mountType;
139       $this->shares_settings[$share]["dir"]=$dir;
140       $this->shares_settings[$share]["host"]=$host;
142       $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
143       $newShare=($this->mountDirectory . "/". $host . $dir );
144       if (strcmp($oldShare, $newShare)==0) {
145         $this->selectedshare = $share;
146       }
147     }
148     asort($this->shares);
149   }
152   /* Execute the plugin, produce the output. */
153   function execute() 
154   {
155     plugin :: execute();
157     /* Log view */
158     if($this->is_account && !$this->view_logged){
159       $this->view_logged = TRUE;
160       new log("view","users/".get_class($this),$this->dn);
161     }
163     /* Use the smarty templating engine here... */
164     $smarty = get_smarty();
165     $display = "";
167     /* Do we need to flip is_account state? */
168     if (isset ($_POST['modify_state'])) {
169       $this->is_account = !$this->is_account;
170     }
172     /* Do we represent a valid account? */
173     if (!$this->is_account && $this->parent === NULL) {
174       $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\">&nbsp;<b>"._("This account has no netatalk extensions.")."</b>";
176       $display .= back_to_main();
177       return ($display);
178     }
180     /* Show tab dialog headers */
181     if ($this->parent !== NULL) {
182       if ($this->is_account) {
183         $display = $this->show_disable_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
184       } else {
185         $errmsg="";
186         $obj = $this->parent->by_object['posixAccount'];
187         if  (!($obj->is_account) ) {
188           $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
189         }
190         if (count($this->shares)== 0) {
191           $errmsg.="At least one share with netatalk or NFS mount entry needed.";
192         }
193         if($errmsg==""){
194           $display = $this->show_enable_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
195         } else {
196           $display = $this->show_enable_header(_("Create netatalk account"), _($errmsg), TRUE);
197         }
198         return ($display);
199       }
200     }
202     /* Assign attributes and ACL to smarty */
203     $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
204     $smarty->assign("netatalkShareACL", $this->getacl("netatalkShare",$SkipWrite));
205     $smarty->assign("netatalkUserHomepathACL", $this->getacl("netatalkUserHomepath",$SkipWrite));
207     foreach ($this->smarty_attributes as $val) {
208       $smarty->assign("$val", $this-> $val);
209       if (in_array($val, $this->is_chk_box)) {
210         if ($this-> $val == "checked") {
211           $smarty->assign($val."CHK", " checked ");
212         } else {
213           $smarty->assign($val."CHK", "");
214         }
215       }
216     }
218     /* Let smarty fetch and process the page. */
219     $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
220     return ($display);
221   }
224   /* Check if we have correct data */
225   function check() {
226     $message = array ();
228     if (strlen($this->apple_user_share) == 0) {
229       $message[] = _("You must select a share to use.");
230     }
232     return ($message);
233   }
235   /* Save to LDAP */
236   function save() {
237     /* remove a / at the end of the homepath, we neither need it there nor
238      * do we want to check for it later.
239      */
240     if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
241       $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
242     }
244     $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
245     $dir=$this->shares_settings[$this->apple_user_share]["dir"];
246     $host=$this->shares_settings[$this->apple_user_share]["host"];
248     /* Convert raw data to wished format */
249     if ($this->is_account) {
250       if($mountType=="url") {
251         $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
252         $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
253       } else {
254         $this->apple_user_homeurl = "";
255       }
256       $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
257     } else {
258       $this->apple_user_homeurl = "";
259       $this->apple_user_homeDirectory = "";
260     }
262     $ldap = $this->config->get_ldap_link();
264     /* Reset array of used attributes, because plugin::save() 
265        will not work with '-' in attributes names 
266        after calling save restore attributes array */
267     $attributes = $this->attributes;
268     $this->attributes = array();
269     plugin :: save();
270     $this->attributes = $attributes;
272     /* Do attribute conversion */
273     foreach ($this->attributes as $val) {
274       $name = str_replace('-', '_', $val);
275       if ($this->$name != "") {
276         $this->attrs[$val] = $this->$name;
277       } else {
278         $this->attrs[$val] = array();
279       }
280       unset ($this->attrs[$name]);
281     }
283     /* Write back to ldap */
284     $ldap->cd($this->dn);
285     $this->cleanup();
286     $ldap->modify($this->attrs);
288     if($this->initially_was_account){
289       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
290     }else{
291       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
292     }
294     show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/netatalk account with dn '%s' failed."),$this->dn));
296     /* Optionally execute a command after we're done */
297     if ($this->initially_was_account == $this->is_account) {
298       if ($this->is_modified) {
299         $this->handle_post_events("modify",array("uid" => $this->uid));
300       }
301     } else {
302       $this->handle_post_events("add",array("uid" => $this->uid));
303     }
304   }
306   /* Use Save_object for every Post handling */
307   function save_object() {
308     if (isset ($_POST['netatalkTab'])) {
309       /* Save ldap attributes */
310       plugin :: save_object();
312       foreach($this->post_attributes as $val) {
313         if (isset ($_POST[$val])) {
314           $this->$val = $_POST[$val];
315         } else {
316           $this->$val = "";
317         }
318       }
320       /* Specialhandling for checkboxes */
321       foreach ($this->is_chk_box as $val) {
322         if (isset ($_POST[$val])) {
323           $this-> $val = "checked";
324         } else {
325           $this-> $val = "unchecked";
326         }
327       }
329       $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
330     }
331   }
333   function remove_from_parent() {
334     /* Cancel if there's nothing to do here */
335     if (!$this->initially_was_account) {
336       return;
337     }
339     /* include global link_info */
340     $ldap = $this->config->get_ldap_link();
342     /* Remove and write to LDAP */
343     plugin :: remove_from_parent();
345     /* Adapt attributes if needed */
346     //     $method= new $this->method($this->config);
347     //     $method->fixAttributesOnRemove($this);
349     @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
350     $ldap->cd($this->dn);
351     $this->cleanup();
352     $ldap->modify($this->attrs);
354     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
356     show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/netatalk account with dn '%s' failed."),$this->dn));
358     /* remove the entry from LDAP */
359     unset ($this->attrs['uid']);
361     /* Optionally execute a command after we're done */
362     $this->handle_post_events('remove', array("uid" => $this->uid));
363   }
366   /* Return plugin informations for acl handling*/
367   static function plInfo()
368   {
369     return (array(
370           "plDescription"     => _("Netatalk"),
371           "plSelfModify"      => TRUE,
372           "plDepends"         => array("user"),
373           "plPriority"        => 6,
374           "plSection"     => array("personal" => _("My account")),
375           "plCategory"    => array("users"),
376           "plOptions"         => array(),
378           "plProvidedAcls"  => array(
379             "netatalkUserHomepath"   =>  _("User home path"),
380             "netatalkShare"          =>  _("Share"))
381           ));
382   }
386 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
387 ?>