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 /* Slip setting those attributes, we can't define class variables with - */
58 var $use_save_attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");
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 ("apple_user_share", "apple_user_homepath_raw");
66 /* Objectclasses */
67 var $objectclasses = array ("apple-user");
69 /* Checkboxes */
70 var $is_chk_box = array ();
73 /* The constructor just saves a copy of the config. You may add what ever you need. */
74 function netatalk($config, $dn = NULL) {
76 /* Include config object */
77 $this->config = $config;
78 plugin :: plugin($config, $dn);
80 /* Copy needed attributes */
81 foreach($this->use_save_attributes as $val) {
82 if (isset($this->attrs["$val"][0])) {
83 $name = str_replace('-', '_', $val);
84 $this->$name = $this->attrs["$val"][0];
85 }
86 }
88 if (strlen($this->apple_user_homeDirectory) >0) {
89 $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
90 }
92 /* Save initial account state */
93 $this->initially_was_account = $this->is_account;
94 }
96 /* Execute the plugin, produce the output. */
97 function execute() {
98 plugin :: execute();
100 /* Use the smarty templating engine here... */
101 $smarty = get_smarty();
102 $display = "";
104 /* Do we need to flip is_account state? */
105 if (isset ($_POST['modify_state'])) {
106 $this->is_account = !$this->is_account;
107 }
109 /* Do we represent a valid account? */
110 if (!$this->is_account && $this->parent == NULL) {
111 $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\"> <b>"._("This account has no netatalk extensions.")."</b>";
113 $display .= back_to_main();
114 return ($display);
115 }
118 /* Get netatalk shares */
119 $this->shares = array();
120 $ldap = $this->config->get_ldap_link();
122 if($this->dn === "new" || $this->dn == NULL) {
123 $ldap->cd($this->parent->by_object['user']->base);
124 } else {
125 $ldap->cd ($this->dn);
126 $ldap->cd ('..'); $ldap->cd ('..');
127 }
128 $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
131 /* Show tab dialog headers */
132 if ($this->parent != NULL) {
133 if ($this->is_account) {
134 $display = $this->show_disable_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
135 } else {
136 $errmsg="";
137 $obj = $this->parent->by_object['posixAccount'];
138 if (!($obj->is_account) ) {
139 $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
140 }
141 if ($ldap->count() == 0) {
142 $errmsg.="At least one share with netatalk or NFS mount entry needed.";
143 }
144 if($errmsg==""){
145 $display = $this->show_enable_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
146 } else {
147 $display = $this->show_enable_header(_("Create netatalk account"), _($errmsg), TRUE);
148 }
149 return ($display);
150 }
151 }
154 while ($attrs = $ldap->fetch()){
155 $tmp = split(":", $attrs["cn"][0]);
156 $host = trim($tmp[0]);
157 $dir = trim($tmp[1]);
158 $mountType = trim($attrs["mountType"][0]);
159 if ($mountType == "url") {
160 $mountTypeReal = "netatalk";
161 } else {
162 $mountTypeReal = $mountType;
163 }
164 $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
165 $this->shares[$share] = $share;
166 $this->shares_settings[$share]["mountType"]=$mountType;
167 $this->shares_settings[$share]["dir"]=$dir;
168 $this->shares_settings[$share]["host"]=$host;
170 $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
171 $newShare=($this->mountDirectory . "/". $host . $dir );
172 if (strcmp($oldShare, $newShare)==0) {
173 $this->selectedshare = $share;
174 }
175 }
176 asort($this->shares);
177 /* Assign attributes and ACL to smarty */
178 $SkipWrite = (!isset($this->parent) || !$this->parent) && !isset($_SESSION['edit']);
179 $smarty->assign("netatalkShareACL", $this->getacl("netatalkShare",$SkipWrite));
180 $smarty->assign("netatalkUserHomepathACL", $this->getacl("netatalkUserHomepath",$SkipWrite));
181 foreach ($this->smarty_attributes as $val) {
182 $smarty->assign("$val", $this-> $val);
183 if (in_array($val, $this->is_chk_box)) {
184 if ($this-> $val == "checked") {
185 $smarty->assign($val."CHK", " checked ");
186 } else {
187 $smarty->assign($val."CHK", "");
188 }
189 }
190 }
192 /* Let smarty fetch and process the page. */
193 $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
194 return ($display);
195 }
198 /* Check if we have correct data */
199 function check() {
200 $message = array ();
202 if (strlen($this->apple_user_share) == 0) {
203 $message[] = _("You must select a share to use.");
204 }
206 return ($message);
207 }
209 /* Save to LDAP */
210 function save() {
211 /* remove a / at the end of the homepath, we neither need it there nor
212 * do we want to check for it later.
213 */
214 if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
215 $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
216 }
218 $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
219 $dir=$this->shares_settings[$this->apple_user_share]["dir"];
220 $host=$this->shares_settings[$this->apple_user_share]["host"];
222 /* Convert raw data to wished format */
223 if ($this->is_account) {
224 if($mountType=="url") {
225 $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
226 $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
227 } else {
228 $this->apple_user_homeurl = "";
229 }
230 $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
231 } else {
232 $this->apple_user_homeurl = "";
233 $this->apple_user_homeDirectory = "";
234 }
236 $ldap = $this->config->get_ldap_link();
238 /* Call parents save to prepare $this->attrs */
239 plugin :: save();
241 /* Do attribute conversion */
242 foreach ($this->use_save_attributes as $val) {
243 $name = str_replace('-', '_', $val);
244 if ($this->$name != "") {
245 $this->attrs[$val] = $this->$name;
246 } else {
247 $this->attrs[$val] = array();
248 }
249 unset ($this->attrs[$name]);
250 }
251 /* Write back to ldap */
252 $ldap->cd($this->dn);
253 $this->cleanup();
254 $ldap->modify($this->attrs);
256 show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/netatalk account with dn '%s' failed."),$this->dn));
258 /* Optionally execute a command after we're done */
259 if ($this->initially_was_account == $this->is_account) {
260 if ($this->is_modified) {
261 $this->handle_post_events("modify");
262 }
263 } else {
264 $this->handle_post_events("add");
265 }
266 }
268 /* Use Save_object for every Post handling */
269 function save_object() {
270 if (isset ($_POST['netatalkTab'])) {
271 /* Save ldap attributes */
272 // plugin :: save_object();
274 foreach($this->post_attributes as $val) {
275 if (isset ($_POST[$val])) {
276 $this->$val = $_POST[$val];
277 } else {
278 $this->$val = "";
279 }
280 }
282 /* Specialhandling for checkboxes */
283 foreach ($this->is_chk_box as $val) {
284 if (isset ($_POST[$val])) {
285 $this-> $val = "checked";
286 } else {
287 $this-> $val = "unchecked";
288 }
289 }
291 $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
292 }
293 }
295 function remove_from_parent() {
296 /* Cancel if there's nothing to do here */
297 if (!$this->initially_was_account) {
298 return;
299 }
301 /* include global link_info */
302 $ldap = $this->config->get_ldap_link();
304 /* Remove and write to LDAP */
305 plugin :: remove_from_parent();
307 /* Adapt attributes if needed */
308 // $method= new $this->method($this->config);
309 // $method->fixAttributesOnRemove($this);
311 @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
312 $ldap->cd($this->dn);
313 $this->cleanup();
314 $ldap->modify($this->attrs);
316 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/netatalk account with dn '%s' failed."),$this->dn));
318 /* remove the entry from LDAP */
319 unset ($this->attrs['uid']);
321 /* Optionally execute a command after we're done */
322 $this->handle_post_events('remove');
323 }
326 /* Return plugin informations for acl handling*/
327 function plInfo()
328 {
329 return (array(
330 "plDescription" => _("Netatalk"),
331 "plSelfModify" => TRUE,
332 "plDepends" => array("user"),
333 "plPriority" => 1,
334 "plSection" => "personal",
335 "plCategory" => array("users"),
336 "plOptions" => array(),
338 "plProvidedAcls" => array(
339 "netatalkUserHomepath" => _("User home path"),
340 "netatalkShare" => _("Share"))
341 ));
342 }
343 }
345 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
346 ?>