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");
59 /* Attributes to use in smarty template */
60 var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
62 /* Attributes to save from $_POST */
63 var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");
65 /* Objectclasses */
66 var $objectclasses = array ("apple-user");
68 /* Checkboxes */
69 var $is_chk_box = array ();
71 var $uid ="";
73 /* The constructor just saves a copy of the config. You may add what ever you need. */
74 function netatalk($config, $dn = NULL, $parent= NULL) {
76 /* Include config object */
77 $this->config = $config;
78 plugin::plugin($config, $dn, $parent);
80 /* set user id */
81 if(isset($this->attrs['uid'])){
82 $this->uid = $this->attrs['uid'][0];
83 }
85 /* Copy needed attributes */
86 foreach($this->attributes as $val) {
87 if (isset($this->attrs["$val"][0])) {
88 $name = str_replace('-', '_', $val);
89 $this->$name = $this->attrs["$val"][0];
90 }
91 }
93 if (strlen($this->apple_user_homeDirectory) >0) {
94 $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
95 }
97 /* Save initial account state */
98 $this->initially_was_account = $this->is_account;
99 }
101 /* Execute the plugin, produce the output. */
102 function execute() {
103 plugin :: execute();
105 /* Use the smarty templating engine here... */
106 $smarty = get_smarty();
107 $display = "";
109 /* Do we need to flip is_account state? */
110 if (isset ($_POST['modify_state'])) {
111 $this->is_account = !$this->is_account;
112 }
114 /* Do we represent a valid account? */
115 if (!$this->is_account && $this->parent == NULL) {
116 $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\"> <b>"._("This account has no netatalk extensions.")."</b>";
118 $display .= back_to_main();
119 return ($display);
120 }
123 /* Get netatalk shares */
124 $this->shares = array();
125 $ldap = $this->config->get_ldap_link();
127 if($this->dn === "new" || $this->dn == NULL) {
128 $ldap->cd($this->parent->by_object['user']->base);
129 } else {
130 $ldap->cd ($this->dn);
131 $ldap->cd ('..'); $ldap->cd ('..');
132 }
133 $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
136 /* Show tab dialog headers */
137 if ($this->parent != NULL) {
138 if ($this->is_account) {
139 $display = $this->show_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
140 } else {
141 $errmsg="";
142 $obj = $this->parent->by_object['posixAccount'];
143 if (!($obj->is_account) ) {
144 $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
145 }
146 if ($ldap->count() == 0) {
147 $errmsg.="At least one share with netatalk or NFS mount entry needed.";
148 }
149 if($errmsg==""){
150 $display = $this->show_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
151 } else {
152 $display = $this->show_header(_("Create netatalk account"), _($errmsg), TRUE);
153 }
154 return ($display);
155 }
156 }
159 while ($attrs = $ldap->fetch()){
160 $tmp = split(":", $attrs["cn"][0]);
161 $host = trim($tmp[0]);
162 $dir = trim($tmp[1]);
163 $mountType = trim($attrs["mountType"][0]);
164 if ($mountType == "url") {
165 $mountTypeReal = "netatalk";
166 } else {
167 $mountTypeReal = $mountType;
168 }
169 $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
170 $this->shares[$share] = $share;
171 $this->shares_settings[$share]["mountType"]=$mountType;
172 $this->shares_settings[$share]["dir"]=$dir;
173 $this->shares_settings[$share]["host"]=$host;
175 $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
176 $newShare=($this->mountDirectory . "/". $host . $dir );
177 if (strcmp($oldShare, $newShare)==0) {
178 $this->selectedshare = $share;
179 }
180 }
181 asort($this->shares);
182 /* Assign attributes and ACL to smarty */
184 $smarty->assign("netatalkShareACL", chkacl($this->acl, "netatalkShare"));
185 $smarty->assign("netatalkUserHomepathACL", chkacl($this->acl, "netatalkUserHomepath"));
186 foreach ($this->smarty_attributes as $val) {
187 $smarty->assign("$val", $this-> $val);
188 if (in_array($val, $this->is_chk_box)) {
189 if ($this-> $val == "checked") {
190 $smarty->assign($val."CHK", " checked ");
191 } else {
192 $smarty->assign($val."CHK", "");
193 }
194 }
195 }
197 /* Let smarty fetch and process the page. */
198 $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
199 return ($display);
200 }
203 /* Check if we have correct data */
204 function check() {
205 $message = array ();
207 if (strlen($this->apple_user_share) == 0) {
208 $message[] = _("You must select a share to use.");
209 }
211 return ($message);
212 }
214 /* Save to LDAP */
215 function save() {
216 /* remove a / at the end of the homepath, we neither need it there nor
217 * do we want to check for it later.
218 */
219 if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
220 $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
221 }
223 $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
224 $dir=$this->shares_settings[$this->apple_user_share]["dir"];
225 $host=$this->shares_settings[$this->apple_user_share]["host"];
227 /* Convert raw data to wished format */
228 if ($this->is_account) {
229 if($mountType=="url") {
230 $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
231 $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
232 } else {
233 $this->apple_user_homeurl = "";
234 }
235 $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
236 } else {
237 $this->apple_user_homeurl = "";
238 $this->apple_user_homeDirectory = "";
239 }
241 $ldap = $this->config->get_ldap_link();
243 /* Call parents save to prepare $this->attrs */
244 plugin :: save();
246 /* Do attribute conversion */
247 foreach ($this->attributes as $val) {
248 $name = str_replace('-', '_', $val);
249 if ($this->$name != "") {
250 $this->attrs[$val] = $this->$name;
251 } else {
252 $this->attrs[$val] = array();
253 }
254 unset ($this->attrs[$name]);
255 }
257 /* Write back to ldap */
258 $ldap->cd($this->dn);
259 $this->cleanup();
260 $ldap->modify($this->attrs);
262 show_ldap_error($ldap->get_error(), _("Saving Netatalk account failed"));
264 /* Optionally execute a command after we're done */
265 if ($this->initially_was_account == $this->is_account) {
266 if ($this->is_modified) {
267 $this->handle_post_events("modify",array("uid" => $this->uid));
268 }
269 } else {
270 $this->handle_post_events("add",array("uid" => $this->uid));
271 }
272 }
274 /* Use Save_object for every Post handling */
275 function save_object() {
276 if (isset ($_POST['netatalkTab'])) {
277 /* Save ldap attributes */
278 plugin :: save_object();
280 foreach($this->post_attributes as $val) {
281 if (isset ($_POST[$val])) {
282 $this->$val = $_POST[$val];
283 } else {
284 $this->$val = "";
285 }
286 }
288 /* Specialhandling for checkboxes */
289 foreach ($this->is_chk_box as $val) {
290 if (isset ($_POST[$val])) {
291 $this-> $val = "checked";
292 } else {
293 $this-> $val = "unchecked";
294 }
295 }
297 $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
298 }
299 }
301 function remove_from_parent() {
302 /* Cancel if there's nothing to do here */
303 if (!$this->initially_was_account) {
304 return;
305 }
307 /* include global link_info */
308 $ldap = $this->config->get_ldap_link();
310 /* Remove and write to LDAP */
311 plugin :: remove_from_parent();
313 /* Adapt attributes if needed */
314 // $method= new $this->method($this->config);
315 // $method->fixAttributesOnRemove($this);
317 @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
318 $ldap->cd($this->dn);
319 $this->cleanup();
320 $ldap->modify($this->attrs);
322 show_ldap_error($ldap->get_error(), _("Removing Netatalk account failed"));
324 /* remove the entry from LDAP */
325 unset ($this->attrs['uid']);
327 /* Optionally execute a command after we're done */
328 $this->handle_post_events('remove', array("uid" => $this->uid));
329 }
331 }
333 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
334 ?>