7e66f6ce8584dc699110c6ff801019152cd59be9
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 ();
72 /* The constructor just saves a copy of the config. You may add what ever you need. */
73 function netatalk($config, $dn = NULL) {
75 /* Include config object */
76 $this->config = $config;
77 plugin :: plugin($config, $dn);
79 /* Copy needed attributes */
80 foreach($this->attributes as $val) {
81 if (isset($this->attrs["$val"][0])) {
82 $name = str_replace('-', '_', $val);
83 $this->$name = $this->attrs["$val"][0];
84 }
85 }
87 if (strlen($this->apple_user_homeDirectory) >0) {
88 $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
89 }
91 /* Save initial account state */
92 $this->initially_was_account = $this->is_account;
93 }
95 /* Execute the plugin, produce the output. */
96 function execute() {
97 plugin :: execute();
99 /* Use the smarty templating engine here... */
100 $smarty = get_smarty();
101 $display = "";
103 /* Do we need to flip is_account state? */
104 if (isset ($_POST['modify_state'])) {
105 $this->is_account = !$this->is_account;
106 }
108 /* Do we represent a valid account? */
109 if (!$this->is_account && $this->parent == NULL) {
110 $display = "<img alt=\"\"src=\"images/stop.png\" align=\"middle\"> <b>"._("This account has no netatalk extensions.")."</b>";
112 $display .= back_to_main();
113 return ($display);
114 }
117 /* Get netatalk shares */
118 $this->shares = array();
119 $ldap = $this->config->get_ldap_link();
121 if($this->dn === "new" || $this->dn == NULL) {
122 $ldap->cd($this->parent->by_object['user']->base);
123 } else {
124 $ldap->cd ($this->dn);
125 $ldap->cd ('..'); $ldap->cd ('..');
126 }
127 $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
130 /* Show tab dialog headers */
131 if ($this->parent != NULL) {
132 if ($this->is_account) {
133 $display = $this->show_header(_("Remove netatalk account"), _("This account has netatalk features enabled. You can disable them by clicking below."));
134 } else {
135 $errmsg="";
136 $obj = $this->parent->by_object['posixAccount'];
137 if (!($obj->is_account) ) {
138 $errmsg.="Posix features are needed for netatalk accounts, enable them first. ";
139 }
140 if ($ldap->count() == 0) {
141 $errmsg.="At least one share with netatalk or NFS mount entry needed.";
142 }
143 if($errmsg==""){
144 $display = $this->show_header(_("Create netatalk account"), _("This account has netatalk features disabled. You can enable them by clicking below."));
145 } else {
146 $display = $this->show_header(_("Create netatalk account"), _($errmsg), TRUE);
147 }
148 return ($display);
149 }
150 }
153 while ($attrs = $ldap->fetch()){
154 $tmp = split(":", $attrs["cn"][0]);
155 $host = trim($tmp[0]);
156 $dir = trim($tmp[1]);
157 $mountType = trim($attrs["mountType"][0]);
158 if ($mountType == "url") {
159 $mountTypeReal = "netatalk";
160 } else {
161 $mountTypeReal = $mountType;
162 }
163 $share = $attrs["cn"][0]. " (" . $mountTypeReal . ")";
164 $this->shares[$share] = $share;
165 $this->shares_settings[$share]["mountType"]=$mountType;
166 $this->shares_settings[$share]["dir"]=$dir;
167 $this->shares_settings[$share]["host"]=$host;
169 $oldShare=substr($this->apple_user_homeDirectory, 0, strrpos($this->apple_user_homeDirectory, '/'));
170 $newShare=($this->mountDirectory . "/". $host . $dir );
171 if (strcmp($oldShare, $newShare)==0) {
172 $this->selectedshare = $share;
173 }
174 }
175 asort($this->shares);
176 /* Assign attributes and ACL to smarty */
178 $smarty->assign("netatalkShareACL", chkacl($this->acl, "netatalkShare"));
179 $smarty->assign("netatalkUserHomepathACL", chkacl($this->acl, "netatalkUserHomepath"));
180 foreach ($this->smarty_attributes as $val) {
181 $smarty->assign("$val", $this-> $val);
182 if (in_array($val, $this->is_chk_box)) {
183 if ($this-> $val == "checked") {
184 $smarty->assign($val."CHK", " checked ");
185 } else {
186 $smarty->assign($val."CHK", "");
187 }
188 }
189 }
191 /* Let smarty fetch and process the page. */
192 $display .= ($smarty->fetch(get_template_path('netatalk.tpl', TRUE, dirname(__FILE__))));
193 return ($display);
194 }
197 /* Check if we have correct data */
198 function check() {
199 $message = array ();
201 if (strlen($this->apple_user_share) == 0) {
202 $message[] = _("You must select a share to use.");
203 }
205 return ($message);
206 }
208 /* Save to LDAP */
209 function save() {
210 /* remove a / at the end of the homepath, we neither need it there nor
211 * do we want to check for it later.
212 */
213 if(substr($this->apple_user_homepath_raw, -1, 1) === '/') {
214 $this->apple_user_homepath_raw=substr($this->apple_user_homepath_raw, 0, -1);
215 }
217 $mountType=$this->shares_settings[$this->apple_user_share]["mountType"];
218 $dir=$this->shares_settings[$this->apple_user_share]["dir"];
219 $host=$this->shares_settings[$this->apple_user_share]["host"];
221 /* Convert raw data to wished format */
222 if ($this->is_account) {
223 if($mountType=="url") {
224 $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
225 $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
226 } else {
227 $this->apple_user_homeurl = "";
228 }
229 $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
230 } else {
231 $this->apple_user_homeurl = "";
232 $this->apple_user_homeDirectory = "";
233 }
235 $ldap = $this->config->get_ldap_link();
237 /* Call parents save to prepare $this->attrs */
238 plugin :: save();
240 /* Do attribute conversion */
241 foreach ($this->attributes as $val) {
242 $name = str_replace('-', '_', $val);
243 if ($this->$name != "") {
244 $this->attrs[$val] = $this->$name;
245 } else {
246 $this->attrs[$val] = array();
247 }
248 unset ($this->attrs[$name]);
249 }
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(), _("Saving Netatalk account failed"));
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(), _("Removing Netatalk account failed"));
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 }
325 }
327 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
328 ?>