Code

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