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();
118 if($this->dn === "new" || $this->dn === NULL) {
119 $base = session::get('CurrentMainBase');
120 } else {
121 $base = preg_replace("/^[^,]+,".preg_quote(get_people_ou(), '/')."/","",$this->dn);
122 }
124 $ldap->cd($base);
125 $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
127 while ($attrs = $ldap->fetch()){
128 $tmp = split(":", $attrs["cn"][0]);
129 $host = trim($tmp[0]);
130 $dir = trim($tmp[1]);
131 $mountType = trim($attrs["mountType"][0]);
132 if ($mountType == "url") {
133 $mountTypeReal = "netatalk";
134 } else {
135 $mountTypeReal = $mountType;
136 }
137 $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
138 $this->shares[$share] = $share;
139 $this->shares_settings[$share]["mountType"]=$mountType;
140 $this->shares_settings[$share]["dir"]=$dir;
141 $this->shares_settings[$share]["host"]=$host;
143 $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
144 $newShare=($this->mountDirectory . "/". $host . $dir );
145 if (strcmp($oldShare, $newShare)==0) {
146 $this->selectedshare = $share;
147 }
148 }
149 asort($this->shares);
150 }
153 /* Execute the plugin, produce the output. */
154 function execute()
155 {
156 plugin :: execute();
158 /* Log view */
159 if($this->is_account && !$this->view_logged){
160 $this->view_logged = TRUE;
161 new log("view","users/".get_class($this),$this->dn);
162 }
164 /* Use the smarty templating engine here... */
165 $smarty = get_smarty();
166 $display = "";
168 /* Do we need to flip is_account state? */
169 if (isset ($_POST['modify_state'])) {
170 $this->is_account = !$this->is_account;
171 }
173 /* Do we represent a valid account? */
174 if (!$this->is_account && $this->parent === NULL) {
175 $display = "<img alt=\"\"src=\"images/small-error.png\" align=\"middle\"> <b>".
176 msgPool::noValidExtension(_("netatalk"))."</b>";
177 $display .= back_to_main();
178 return ($display);
179 }
181 /* Show tab dialog headers */
182 if ($this->parent !== NULL) {
183 if ($this->is_account) {
184 $display = $this->show_disable_header(msgPool::removeFeaturesButton(_("netatalk")),
185 msgPool::featuresEnabled(_("netatalk")));
186 } else {
187 $errmsg="";
188 $obj = $this->parent->by_object['posixAccount'];
189 if (!($obj->is_account) ) {
190 $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")),
191 msgPool::featuresDisabled(_("netatalk"), _("POSIX")), TRUE);
192 } elseif (count($this->shares)== 0) {
193 $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")),
194 msgPool::featuresDisabled(_("netatalk"), _("netatalk or NFS share")), TRUE);
195 } else {
196 $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")),
197 msgPool::featuresDisabled(_("netatalk")));
198 }
199 return ($display);
200 }
201 }
203 /* Assign attributes
204 */
205 foreach ($this->smarty_attributes as $val) {
206 $smarty->assign("$val", $this-> $val);
207 }
209 /* Assign ACLs
210 */
211 $tmp = $this->plInfo();
212 $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
213 foreach($tmp['plProvidedAcls'] as $name => $desc){
214 $smarty->assign($name."ACL",$this->getacl($name,$SkipWrite));
215 }
217 /* Let smarty fetch and process the page. */
218 $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
219 return ($display);
220 }
223 /* Check if we have correct data */
224 function check()
225 {
226 $message = array ();
227 if (strlen($this->apple_user_share) == 0) {
228 $message[] = msgPool::required(_("Share"));
229 }
230 return ($message);
231 }
234 /* Save to LDAP */
235 function save()
236 {
238 /* remove a / at the end of the homepath, we neither need it there nor
239 * do we want to check for it later.
240 */
241 if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
242 $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
243 }
245 $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
246 $dir=$this->shares_settings[$this->apple_user_share]["dir"];
247 $host=$this->shares_settings[$this->apple_user_share]["host"];
249 /* Convert raw data to wished format */
250 if ($this->is_account) {
251 if($mountType=="url") {
252 $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.
253 $this->apple_user_homepath_raw.'</path></home_dir>';
254 $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
255 } else {
256 $this->apple_user_homeurl = "";
257 }
258 $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .
259 $dir . '/' . $this->apple_user_homepath_raw;
260 } else {
261 $this->apple_user_homeurl = "";
262 $this->apple_user_homeDirectory = "";
263 }
265 $ldap = $this->config->get_ldap_link();
266 plugin :: save();
268 /* Transform variable names from '_' to '-'.
269 */
270 foreach ($this->attributes as $val) {
271 unset($this->attrs[$val]);
272 $name = str_replace('_', '-', $val);
273 if ($this->$val != "") {
274 $this->attrs[$name] = $this->$val;
275 } else {
276 $this->attrs[$name] = array();
277 }
278 }
280 /* Write back to ldap */
281 $ldap->cd($this->dn);
282 $this->cleanup();
283 $ldap->modify($this->attrs);
285 if($this->initially_was_account){
286 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
287 }else{
288 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
289 }
291 if (!$ldap->success()){
292 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
293 }
295 /* Optionally execute a command after we're done */
296 if ($this->initially_was_account == $this->is_account) {
297 if ($this->is_modified) {
298 $this->handle_post_events("modify",array("uid" => $this->uid));
299 }
300 } else {
301 $this->handle_post_events("add",array("uid" => $this->uid));
302 }
303 }
306 /* Use Save_object for every Post handling
307 */
308 function save_object()
309 {
310 if (isset ($_POST['netatalkTab'])) {
311 /* Save ldap attributes */
312 plugin :: save_object();
314 foreach($this->post_attributes as $acl => $val) {
315 if(!preg_match("/w/",$this->getacl($acl))) continue;
316 if (isset ($_POST[$val])) {
317 $this->$val = $_POST[$val];
318 } else {
319 $this->$val = "";
320 }
321 }
322 $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
323 }
324 }
327 function remove_from_parent()
328 {
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();
340 $this->cleanup();
342 /* Attribute name conversion "_" to "-" */
343 foreach($this->attributes as $val){
344 unset($this->attrs[$val]);
345 $name = preg_replace("/_/","-",$val);
346 $this->attrs[$name] = array();
347 }
349 @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
350 $ldap->cd($this->dn);
352 $ldap->modify($this->attrs);
354 new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
356 if (!$ldap->success()){
357 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
358 }
360 /* remove the entry from LDAP */
361 unset ($this->attrs['uid']);
363 /* Optionally execute a command after we're done */
364 $this->handle_post_events('remove', array("uid" => $this->uid));
365 }
368 /* Return plugin informations for acl handling*/
369 static function plInfo()
370 {
371 return (array(
372 "plDescription" => _("Netatalk"),
373 "plSelfModify" => TRUE,
374 "plDepends" => array("user"),
375 "plPriority" => 6,
376 "plSection" => array("personal" => _("My account")),
377 "plCategory" => array("users"),
378 "plOptions" => array(),
380 "plProvidedAcls" => array(
381 "netatalkUserHomepath" => _("User home path"),
382 "netatalkShare" => _("Share"))
383 ));
384 }
386 }
388 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
389 ?>