Code

8620a262f1424e874672e28fe8bebfb002b620cf
[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",
56       "apple_user_share","shares_settings","apple_user_homepath_raw",
57       "apple_user_homeurl_raw","apple_user_homeurl_xml","apple_user_homeurl",
58       "selectedshare","mountDirectory");
60   /* Attributes to use in smarty template */
61   var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
63   /* Attributes to save from $_POST */
64   var $post_attributes = array ("netatalkShare"         => "apple_user_share", 
65                                 "netatalkUserHomepath"  => "apple_user_homepath_raw");
67   /* Objectclasses */
68   var $objectclasses = array ("apple-user");
69   var $uid ="";  
71   /* The constructor just saves a copy of the config. You may add what ever you need. */
72   function netatalk(&$config, $dn = NULL) 
73   {
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     /* Netatalk attribute include '-' and we can't handle thos attribute names.
85        Copy all thos attribute into a useable name.
86      */
87     foreach($this->attributes as $val) {
88       $name = str_replace('_', '-', $val);
89       if (isset($this->attrs[$name][0])) {
90         $this->$val = $this->attrs[$name][0];
91       }
92     }
94     /* Extract homepath value 
95      */
96     if (strlen($this->apple_user_homeDirectory) >0) {
97       $this->apple_user_homepath_raw = 
98         substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
99     }
101     /* get share list an set default values */
102     $this->get_netatalk_shares(); 
103     $this->apple_user_share = $this->selectedshare;
105     /* Save initial account state */
106     $this->initially_was_account = $this->is_account;
107   }
111   /* Get netatalk shares */
112   function get_netatalk_shares()
113   {
114     /* Get netatalk shares */
115     $this->shares = array();
116     $ldap = $this->config->get_ldap_link();
117     $ldap->cd($this->config->current['BASE']);
118     $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
120     while ($attrs = $ldap->fetch()){
121       $tmp  = explode(":", $attrs["cn"][0]);
122       $host = trim($tmp[0]);
123       $dir  = trim($tmp[1]);
124       $mountType = trim($attrs["mountType"][0]);
125       if ($mountType == "url") {
126         $mountTypeReal = "netatalk";
127       } else {
128         $mountTypeReal = $mountType;
129       } 
130       $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
131       $this->shares[$share] = $share;
132       $this->shares_settings[$share]["mountType"]=$mountType;
133       $this->shares_settings[$share]["dir"]=$dir;
134       $this->shares_settings[$share]["host"]=$host;
136       $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
137       $newShare=($this->mountDirectory . "/". $host . $dir );
138       if (strcmp($oldShare, $newShare)==0) {
139         $this->selectedshare = $share;
140       }
141     }
142     asort($this->shares);
143   }
146   /* Execute the plugin, produce the output. */
147   function execute() 
148   {
149     plugin :: execute();
151     /* Log view */
152     if($this->is_account && !$this->view_logged){
153       $this->view_logged = TRUE;
154       new log("view","users/".get_class($this),$this->dn);
155     }
157     /* Use the smarty templating engine here... */
158     $smarty = get_smarty();
159     $display = "";
161     /* Do we need to flip is_account state? */
162     if (isset ($_POST['modify_state'])) {
163       $this->is_account = !$this->is_account;
164     }
166     /* Do we represent a valid account? */
167     if (!$this->is_account && $this->parent === NULL) {
168       $display = "<img alt=\"\"src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
169         msgPool::noValidExtension(_("Netatalk"))."</b>";
170       $display .= back_to_main();
171       return ($display);
172     }
174     /* Show tab dialog headers */
175     if ($this->parent !== NULL) {
176       if ($this->is_account) {
177         $display = $this->show_disable_header(msgPool::removeFeaturesButton(_("Netatalk")), 
178             msgPool::featuresEnabled(_("Netatalk")));
179       } else {
180         $errmsg="";
181         $obj = $this->parent->by_object['posixAccount'];
182         if  (!($obj->is_account) ) {
183           $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Netatalk")), 
184               msgPool::featuresDisabled(_("Netatalk"), _("POSIX")), TRUE);
185         } elseif (count($this->shares)== 0) {
186           $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Netatalk")), 
187               msgPool::featuresDisabled(_("Netatalk"), _("Netatalk or NFS share")), TRUE);
188         } else {
189           $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Netatalk")), 
190               msgPool::featuresDisabled(_("Netatalk")));
191         } 
192         return ($display);
193       }
194     }
196     /* Assign attributes 
197      */
198     foreach ($this->smarty_attributes as $val) {
199       $smarty->assign("$val", set_post($this-> $val));
200     }
202     /* Assign ACLs 
203      */
204     $tmp = $this->plInfo();
205     foreach($tmp['plProvidedAcls'] as $name => $desc){
206       $smarty->assign($name."ACL",$this->getacl($name));
207     }
209     /* Let smarty fetch and process the page. */
210     $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
211     return ($display);
212   }
215   /* Check if we have correct data */
216   function check() 
217   {
218     $message = array ();
219     if (strlen($this->apple_user_share) == 0) {
220       $message[] = msgPool::required(_("Share"));
221     }
222     return ($message);
223   }
226   /* Save to LDAP */
227   function save() 
228   {
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>'.
245           $this->apple_user_homepath_raw.'</path></home_dir>';
246         $this->apple_user_homeurl = $this->apple_user_homeurl_xml;
247       } else {
248         $this->apple_user_homeurl = "";
249       }
250       $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .
251         $dir . '/' . $this->apple_user_homepath_raw;
252     } else {
253       $this->apple_user_homeurl = "";
254       $this->apple_user_homeDirectory = "";
255     }
257     $ldap = $this->config->get_ldap_link();
258     plugin :: save();
260     /* Transform variable names from '_' to '-'.
261      */
262     foreach ($this->attributes as $val) {
263       unset($this->attrs[$val]);
264       $name = str_replace('_', '-', $val);
265       if ($this->$val != "") {
266         $this->attrs[$name] = $this->$val;
267       } else {
268         $this->attrs[$name] = array();
269       }
270     }
272     /* Write back to ldap */
273     $ldap->cd($this->dn);
274     $this->cleanup();
275     $ldap->modify($this->attrs);
277     if($this->initially_was_account){
278       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
279     }else{
280       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
281     }
283     if (!$ldap->success()){
284       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
285     }
287     /* Optionally execute a command after we're done */
288     if ($this->initially_was_account == $this->is_account) {
289       if ($this->is_modified) {
290         $this->handle_post_events("modify",array("uid" => $this->uid));
291       }
292     } else {
293       $this->handle_post_events("add",array("uid" => $this->uid));
294     }
295   }
298   /* Use Save_object for every Post handling 
299    */
300   function save_object() 
301   {
302     if (isset ($_POST['netatalkTab'])) {
303       /* Save ldap attributes */
304       plugin :: save_object();
306       foreach($this->post_attributes as $acl => $val) {
307         if(!preg_match("/w/",$this->getacl($acl))) continue;
308         if (isset ($_POST[$val])) {
309           $this->$val = get_post($val);
310         } else {
311           $this->$val = "";
312         }
313       }
314       $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
315     }
316   }
319   function remove_from_parent() 
320   {
322     /* Cancel if there's nothing to do here */
323     if (!$this->initially_was_account) {
324       return;
325     }
327     /* include global link_info */
328     $ldap = $this->config->get_ldap_link();
330     /* Remove and write to LDAP */
331     plugin :: remove_from_parent();
332     $this->cleanup();
334     /* Attribute name conversion "_" to "-" */
335     foreach($this->attributes as $val){
336       unset($this->attrs[$val]);
337       $name = preg_replace("/_/","-",$val);
338       $this->attrs[$name] = array();
339     }
341     @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
342     $ldap->cd($this->dn);
344     $ldap->modify($this->attrs);
346     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
348     if (!$ldap->success()){
349       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
350     }
352     /* remove the entry from LDAP */
353     unset ($this->attrs['uid']);
355     /* Optionally execute a command after we're done */
356     $this->handle_post_events('remove', array("uid" => $this->uid));
357   }
360   /* Return plugin informations for acl handling*/
361   static function plInfo()
362   {
363     return (array(
364           "plDescription"     => _("Netatalk"),
365           "plSelfModify"      => TRUE,
366           "plDepends"         => array("user"),
367           "plPriority"        => 6,
368           "plSection"     => array("personal" => _("My account")),
369           "plCategory"    => array("users"),
370           "plOptions"         => array(),
371           "plRequirements"=> array(
372               'ldapSchema' => array('apple-user' => ''),
373               'onFailureDisablePlugin' => array(get_class())),
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 ?>