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");
44 /* Plugin specific values */
45 var $apple_user_homepath_raw = "";
46 var $apple_user_homeurl_raw = "";
47 var $apple_user_homeurl_xml = "";
48 var $apple_user_homeurl = "";
49 var $apple_user_homeDirectory = "";
50 var $apple_user_share = "";
51 var $shares = array();
52 var $shares_settings = array();
53 var $selectedshare = "";
54 var $mountDirectory = "/Network/Servers";
56 /* Attributes to save to LDAP */
57 var $attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");
58 var $CopyPasteVars= array("apple_user_homeurl", "apple_user_homeDirectory","apple_user_share","shares_settings","apple_user_homepath_raw",
59 "apple_user_homeurl_raw","apple_user_homeurl_xml","apple_user_homeurl","selectedshare","mountDirectory");
61 /* Attributes to use in smarty template */
62 var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
64 /* Attributes to save from $_POST */
65 var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");
67 /* Objectclasses */
68 var $objectclasses = array ("apple-user");
70 /* Checkboxes */
71 var $is_chk_box = array ();
73 var $uid ="";
75 /* The constructor just saves a copy of the config. You may add what ever you need. */
76 function netatalk($config, $dn = NULL) {
78 /* Include config object */
79 $this->config = $config;
80 plugin::plugin($config, $dn);
82 /* set user id */
83 if(isset($this->attrs['uid'])){
84 $this->uid = $this->attrs['uid'][0];
85 }
87 /* Copy needed attributes */
88 foreach($this->attributes as $val) {
89 if (isset($this->attrs["$val"][0])) {
90 $name = str_replace('-', '_', $val);
91 $this->$name = $this->attrs["$val"][0];
92 }
93 }
95 if (strlen($this->apple_user_homeDirectory) >0) {
96 $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
97 }
99 /* get share list an set default values */
100 $this->get_netatalk_shares();
101 $this->apple_user_share = $this->selectedshare;
103 /* Save initial account state */
104 $this->initially_was_account = $this->is_account;
105 }
109 /* Get netatalk shares */
110 function get_netatalk_shares()
111 {
112 /* Get netatalk shares */
113 $this->shares = array();
114 $ldap = $this->config->get_ldap_link();
116 if($this->dn === "new" || $this->dn == NULL) {
117 $base = $_SESSION['CurrentMainBase'];
118 } else {
119 $base = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/","",$this->dn);
120 }
122 $ldap->cd($base);
123 $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
125 while ($attrs = $ldap->fetch()){
126 $tmp = split(":", $attrs["cn"][0]);
127 $host = trim($tmp[0]);
128 $dir = trim($tmp[1]);
129 $mountType = trim($attrs["mountType"][0]);
130 if ($mountType == "url") {
131 $mountTypeReal = "netatalk";
132 } else {
133 $mountTypeReal = $mountType;
134 }
135 $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
136 $this->shares[$share] = $share;
137 $this->shares_settings[$share]["mountType"]=$mountType;
138 $this->shares_settings[$share]["dir"]=$dir;
139 $this->shares_settings[$share]["host"]=$host;
141 $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
142 $newShare=($this->mountDirectory . "/". $host . $dir );
143 if (strcmp($oldShare, $newShare)==0) {
144 $this->selectedshare = $share;
145 }
146 }
147 asort($this->shares);
148 }
151 /* Execute the plugin, produce the output. */
152 function execute()
153 {
154 plugin :: execute();
156 /* Use the smarty templating engine here... */
157 $smarty = get_smarty();
158 $display = "";
160 /* Do we need to flip is_account state? */
161 if (isset ($_POST['modify_state'])) {
162 $this->is_account = !$this->is_account;
163 }
165 /* Do we represent a valid account? */
166 if (!$this->is_account && $this->parent == NULL) {
167 $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\"> <b>"._("This account has no netatalk extensions.")."</b>";
169 $display .= back_to_main();
170 return ($display);
171 }
173 /* Show tab dialog headers */
174 if ($this->parent != NULL) {
175 if ($this->is_account) {
176 $display = $this->show_disable_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
177 } else {
178 $errmsg="";
179 $obj = $this->parent->by_object['posixAccount'];
180 if (!($obj->is_account) ) {
181 $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
182 }
183 if (count($this->shares)== 0) {
184 $errmsg.="At least one share with netatalk or NFS mount entry needed.";
185 }
186 if($errmsg==""){
187 $display = $this->show_enable_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
188 } else {
189 $display = $this->show_enable_header(_("Create netatalk account"), _($errmsg), TRUE);
190 }
191 return ($display);
192 }
193 }
195 /* Assign attributes and ACL to smarty */
196 $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['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[] = _("You must select a share to use.");
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 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/netatalk account with dn '%s' failed."),$this->dn));
283 /* Optionally execute a command after we're done */
284 if ($this->initially_was_account == $this->is_account) {
285 if ($this->is_modified) {
286 $this->handle_post_events("modify",array("uid" => $this->uid));
287 }
288 } else {
289 $this->handle_post_events("add",array("uid" => $this->uid));
290 }
291 }
293 /* Use Save_object for every Post handling */
294 function save_object() {
295 if (isset ($_POST['netatalkTab'])) {
296 /* Save ldap attributes */
297 plugin :: save_object();
299 foreach($this->post_attributes as $val) {
300 if (isset ($_POST[$val])) {
301 $this->$val = $_POST[$val];
302 } else {
303 $this->$val = "";
304 }
305 }
307 /* Specialhandling for checkboxes */
308 foreach ($this->is_chk_box as $val) {
309 if (isset ($_POST[$val])) {
310 $this-> $val = "checked";
311 } else {
312 $this-> $val = "unchecked";
313 }
314 }
316 $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
317 }
318 }
320 function remove_from_parent() {
321 /* Cancel if there's nothing to do here */
322 if (!$this->initially_was_account) {
323 return;
324 }
326 /* include global link_info */
327 $ldap = $this->config->get_ldap_link();
329 /* Remove and write to LDAP */
330 plugin :: remove_from_parent();
332 /* Adapt attributes if needed */
333 // $method= new $this->method($this->config);
334 // $method->fixAttributesOnRemove($this);
336 @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
337 $ldap->cd($this->dn);
338 $this->cleanup();
339 $ldap->modify($this->attrs);
341 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/netatalk account with dn '%s' failed."),$this->dn));
343 /* remove the entry from LDAP */
344 unset ($this->attrs['uid']);
346 /* Optionally execute a command after we're done */
347 $this->handle_post_events('remove', array("uid" => $this->uid));
348 }
351 /* Return plugin informations for acl handling*/
352 function plInfo()
353 {
354 return (array(
355 "plDescription" => _("Netatalk"),
356 "plSelfModify" => TRUE,
357 "plDepends" => array("user"),
358 "plPriority" => 6,
359 "plSection" => array("personal" => _("My account")),
360 "plCategory" => array("users" => array("description" => _("Netatalk account"),
361 "objectClass" => "apple-user")),
362 "plOptions" => array(),
364 "plProvidedAcls" => array(
365 "netatalkUserHomepath" => _("User home path"),
366 "netatalkShare" => _("Share"))
367 ));
368 }
370 }
372 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
373 ?>