Code

Removed show_ldap_error() calls
[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>".msgPool::noValidExtension(_("netatalk"))."</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(msgPool::removeFeaturesButton(_("netatalk")), msgPool::featuresEnabled(_("netatalk")));
180       } else {
181         $errmsg="";
182         $obj = $this->parent->by_object['posixAccount'];
183         if  (!($obj->is_account) ) {
184           $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")), msgPool::featuresDisabled(_("netatalk"), _("POSIX")), TRUE);
185         } elseif (count($this->shares)== 0) {
186           $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")), msgPool::featuresDisabled(_("netatalk"), _("netatalk or NFS share")), TRUE);
187         } else {
188           $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")), msgPool::featuresDisabled(_("netatalk")), TRUE);
189         } 
190         }
191         return ($display);
192       }
193     }
195     /* Assign attributes and ACL to smarty */
196     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
197     $smarty->assign("netatalkShareACL", $this->getacl("netatalkShare",$SkipWrite));
198     $smarty->assign("netatalkUserHomepathACL", $this->getacl("netatalkUserHomepath",$SkipWrite));
200     foreach ($this->smarty_attributes as $val) {
201       $smarty->assign("$val", $this-> $val);
202       if (in_array($val, $this->is_chk_box)) {
203         if ($this-> $val == "checked") {
204           $smarty->assign($val."CHK", " checked ");
205         } else {
206           $smarty->assign($val."CHK", "");
207         }
208       }
209     }
211     /* Let smarty fetch and process the page. */
212     $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
213     return ($display);
214   }
217   /* Check if we have correct data */
218   function check() {
219     $message = array ();
221     if (strlen($this->apple_user_share) == 0) {
222       $message[] = msgPool::required(_("Share"));
223     }
225     return ($message);
226   }
228   /* Save to LDAP */
229   function save() {
230     /* remove a / at the end of the homepath, we neither need it there nor
231      * do we want to check for it later.
232      */
233     if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
234       $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
235     }
237     $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
238     $dir=$this->shares_settings[$this->apple_user_share]["dir"];
239     $host=$this->shares_settings[$this->apple_user_share]["host"];
241     /* Convert raw data to wished format */
242     if ($this->is_account) {
243       if($mountType=="url") {
244         $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
245         $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
246       } else {
247         $this->apple_user_homeurl = "";
248       }
249       $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
250     } else {
251       $this->apple_user_homeurl = "";
252       $this->apple_user_homeDirectory = "";
253     }
255     $ldap = $this->config->get_ldap_link();
257     /* Reset array of used attributes, because plugin::save() 
258        will not work with '-' in attributes names 
259        after calling save restore attributes array */
260     $attributes = $this->attributes;
261     $this->attributes = array();
262     plugin :: save();
263     $this->attributes = $attributes;
265     /* Do attribute conversion */
266     foreach ($this->attributes as $val) {
267       $name = str_replace('-', '_', $val);
268       if ($this->$name != "") {
269         $this->attrs[$val] = $this->$name;
270       } else {
271         $this->attrs[$val] = array();
272       }
273       unset ($this->attrs[$name]);
274     }
276     /* Write back to ldap */
277     $ldap->cd($this->dn);
278     $this->cleanup();
279     $ldap->modify($this->attrs);
281     if($this->initially_was_account){
282       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
283     }else{
284       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
285     }
287     if (!$ldap->success()){
288       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
289     }
291     /* Optionally execute a command after we're done */
292     if ($this->initially_was_account == $this->is_account) {
293       if ($this->is_modified) {
294         $this->handle_post_events("modify",array("uid" => $this->uid));
295       }
296     } else {
297       $this->handle_post_events("add",array("uid" => $this->uid));
298     }
299   }
301   /* Use Save_object for every Post handling */
302   function save_object() {
303     if (isset ($_POST['netatalkTab'])) {
304       /* Save ldap attributes */
305       plugin :: save_object();
307       foreach($this->post_attributes as $val) {
308         if (isset ($_POST[$val])) {
309           $this->$val = $_POST[$val];
310         } else {
311           $this->$val = "";
312         }
313       }
315       /* Specialhandling for checkboxes */
316       foreach ($this->is_chk_box as $val) {
317         if (isset ($_POST[$val])) {
318           $this-> $val = "checked";
319         } else {
320           $this-> $val = "unchecked";
321         }
322       }
324       $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
325     }
326   }
328   function remove_from_parent() {
329     /* Cancel if there's nothing to do here */
330     if (!$this->initially_was_account) {
331       return;
332     }
334     /* include global link_info */
335     $ldap = $this->config->get_ldap_link();
337     /* Remove and write to LDAP */
338     plugin :: remove_from_parent();
340     /* Adapt attributes if needed */
341     //     $method= new $this->method($this->config);
342     //     $method->fixAttributesOnRemove($this);
344     @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
345     $ldap->cd($this->dn);
346     $this->cleanup();
347     $ldap->modify($this->attrs);
349     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
351     if (!$ldap->success()){
352       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
353     }
355     /* remove the entry from LDAP */
356     unset ($this->attrs['uid']);
358     /* Optionally execute a command after we're done */
359     $this->handle_post_events('remove', array("uid" => $this->uid));
360   }
363   /* Return plugin informations for acl handling*/
364   static function plInfo()
365   {
366     return (array(
367           "plDescription"     => _("Netatalk"),
368           "plSelfModify"      => TRUE,
369           "plDepends"         => array("user"),
370           "plPriority"        => 6,
371           "plSection"     => array("personal" => _("My account")),
372           "plCategory"    => array("users"),
373           "plOptions"         => array(),
375           "plProvidedAcls"  => array(
376             "netatalkUserHomepath"   =>  _("User home path"),
377             "netatalkShare"          =>  _("Share"))
378           ));
379   }
383 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
384 ?>