1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class department extends plugin
22 {
23 /* department attributes */
24 var $ou= "";
25 var $description= "";
26 var $base= "";
27 var $st= "";
28 var $l= "";
29 var $postalAddress= "";
30 var $businessCategory= "";
31 var $telephoneNumber= "";
32 var $facsimileTelephoneNumber= "";
33 var $orig_dn= "";
35 var $rec_dst=false; // Destination for recursive move
36 var $rec_src=false; // Source for recursive move
37 var $rec_cpy=false; // Is recursive move requested ?
39 function am_i_moved()
40 {
41 return $this->rec_cpy;
42 }
44 /* Headpage attributes */
45 var $last_dep_sorting= "invalid";
46 var $departments= array();
48 /* attribute list for save action */
49 var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
50 "telephoneNumber", "facsimileTelephoneNumber");
51 var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
53 function department ($config, $dn)
54 {
56 plugin::plugin($config, $dn);
57 $this->is_account= TRUE;
58 $this->ui= get_userinfo();
59 $this->dn= $dn;
60 $this->orig_dn= $dn;
61 $this->config= $config;
63 /* Set base */
64 if ($this->dn == "new"){
65 $ui= get_userinfo();
66 if(isset($_SESSION['depfilter']['depselect'])){
67 $this->base = $_SESSION['depfilter']['depselect'];
68 }else{
69 $this->base= dn2base($ui->dn);
70 }
71 } else {
72 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
73 }
74 /* set permissions */
75 $ui= get_userinfo();
76 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
77 $this->acl= get_module_permission($acl, "department", $ui->dn);
78 }
80 function execute()
81 {
82 /* Call parent execute */
83 plugin::execute();
85 /* Reload departments */
86 $this->config->departments= get_departments($this->dn);
87 $this->config->make_idepartments();
88 $smarty= get_smarty();
91 /* Base select dialog */
92 $once = true;
93 foreach($_POST as $name => $value){
94 if(preg_match("/^chooseBase/",$name) && $once){
95 $once = false;
96 $this->dialog = new baseSelectDialog($this->config);
97 $this->dialog->setCurrentBase($this->base);
98 }
99 }
101 /* Dialog handling */
102 if(is_object($this->dialog)){
103 /* Must be called before save_object */
104 $this->dialog->save_object();
106 if($this->dialog->isClosed()){
107 $this->dialog = false;
108 }elseif($this->dialog->isSelected()){
109 $this->base = $this->dialog->isSelected();
110 $this->dialog= false;
111 }else{
112 return($this->dialog->execute());
113 }
114 }
116 /* Hide all departments, that are subtrees of this department */
117 $bases = $this->config->idepartments;
118 if(($this->dn == "new")||($this->dn == "")){
119 $tmp = $bases;
120 }else{
121 $tmp = array();
122 foreach($bases as $dn=>$base){
123 $fixed = str_replace("/","\\",$this->dn);
124 /* Only attach departments which are not a subtree of this one */
125 if(!preg_match("/".$fixed."/",$dn)){
126 $tmp[$dn]=$base;
127 }
128 }
129 }
130 $smarty->assign("bases", $tmp);
132 foreach ($this->attributes as $val){
133 $smarty->assign("$val", $this->$val);
134 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
135 }
136 $smarty->assign("base_select", $this->base);
137 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
138 }
140 function clear_fields()
141 {
142 $this->dn= "";
143 $this->base= "";
144 $this->acl= "#none#";
146 foreach ($this->attributes as $val){
147 $this->$val= "";
148 }
149 }
152 function remove_from_parent()
153 {
154 $ldap= $this->config->get_ldap_link();
155 $ldap->cd ($this->dn);
156 $ldap->recursive_remove();
158 /* Optionally execute a command after we're done */
159 $this->handle_post_events('remove');
160 }
163 /* Save data to object */
164 function save_object()
165 {
166 if (isset($_POST['base'])){
167 plugin::save_object();
169 /* Save base, since this is no LDAP attribute */
170 if (chkacl($this->acl, "create") == ""){
171 $this->base= $_POST['base'];
172 }
173 }
174 }
177 /* Check values */
178 function check()
179 {
180 $message= array();
182 /* Permissions for that base? */
183 // $this->dn= "ou=$this->ou,".$this->base;
184 if (chkacl($this->acl, "create") != ""){
185 $message[]= _("You have no permissions to create a department on this 'Base'.");
186 }
188 /* Check for presence of this department */
189 $ldap= $this->config->get_ldap_link();
190 $attrs= $ldap->cat ($this->dn);
191 if ($this->orig_dn == "new" && !($attrs === FALSE)){
192 $message[]= _("Department with that 'Name' already exists.");
193 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
194 $message[]= _("Department with that 'Name' already exists.");
195 }
197 /* All required fields are set? */
198 if ($this->ou == ""){
199 $message[]= _("Required field 'Name' is not set.");
200 }
201 if ($this->description == ""){
202 $message[]= _("Required field 'Description' is not set.");
203 }
205 /* Validate and modify - or: spaghetti rules! */
206 if ($this->ou == "incoming"){
207 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
208 " Please choose another name.");
209 }
210 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
211 $message[]= _("The field 'Name' contains invalid characters.");
212 }
213 if (!is_phone_nr($this->telephoneNumber)){
214 $message[]= _("The field 'Phone' contains an invalid phone number.");
215 }
216 if (!is_phone_nr($this->facsimileTelephoneNumber)){
217 $message[]= _("The field 'Fax' contains an invalid phone number.");
218 }
220 return $message;
221 }
224 /* Save to LDAP */
225 function save()
226 {
227 plugin::save();
229 /* Write back to ldap */
230 $ldap= $this->config->get_ldap_link();
231 $ldap->cat($this->dn);
232 $a= $ldap->fetch();
233 $ldap->cd($this->dn);
234 if (count($a)){
235 $this->cleanup();
236 $ldap->modify ($this->attrs);
238 $this->handle_post_events('modify');
239 } else {
240 $ldap->add($this->attrs);
241 $this->handle_post_events('add');
242 }
243 show_ldap_error($ldap->get_error());
245 /* Optionally execute a command after we're done */
246 $this->postcreate();
247 }
249 /* Move/Rename complete trees */
250 function recursive_move($src_dn, $dst_dn,$force = false)
251 {
252 if(!$force){
254 $this->rec_cpy = true;
255 $this->rec_src = $src_dn;
256 $this->rec_dst = $dst_dn;
258 $smarty = get_smarty();
260 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
262 $display= $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
263 return($display);
264 exit();
265 }else{
266 if(!$this->rec_cpy){
267 return;
268 }
270 $src_dn = $this->rec_src;
271 $dst_dn = $this->rec_dst;
273 /* Print header to have styles included */
274 $smarty= get_smarty();
275 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
276 echo "<body style='background-image:none;margin:3px;color:black'>";
278 echo "<h3>".sprintf(_("Moving %s to %s"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
281 /* Check if the destination entry exists */
282 $ldap= $this->config->get_ldap_link();
284 /* Check if destination exists - abort */
285 $ldap->cat($dst_dn);
286 if ($ldap->fetch()){
287 trigger_error("Recursive_move $dst_dn already exists.",
288 E_USER_WARNING);
289 echo "Recursive_move :$dst_dn already exists.<br>";
290 return (FALSE);
291 }
293 /* Perform a search for all objects to be moved */
294 $objects= array();
295 $ldap->cd($src_dn);
296 $ldap->search("(objectClass=*)", array("dn"));
297 while($attrs= $ldap->fetch()){
298 $dn= $attrs['dn'];
299 $objects[$dn]= strlen($dn);
300 }
302 /* Sort objects by indent level */
303 asort($objects);
304 reset($objects);
306 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
307 foreach ($objects as $object => $len){
310 $src= str_replace("\\","\\\\",$object);
311 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
312 $dst= str_replace($src_dn,$dst_dn,$object);
314 echo "<b>"._("Object").":</b> $src<br>";
316 if (!$this->copy($src, $dst)){
317 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
318 return (FALSE);
319 }
321 flush();
322 }
324 /* Remove src_dn */
325 $ldap->cd($src_dn);
326 $ldap->recursive_remove();
327 $this->rec_src = $this->rec_dst = "";
328 $this->rec_cpy =false;
330 echo '<p class="seperator"> </p>';
332 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
333 <br><input type='submit' name='back' value='"._("Continue")."'>
334 </form></div>";
336 echo "</body></html>";
338 return (TRUE);
339 }
340 }
343 }
345 ?>