3d11f25d76902769bc729ca94b4bb2720a4b6034
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= "";
34 var $is_administrational_unit= false;
35 var $gosaUnitTag= "";
37 var $rec_dst=false; // Destination for recursive move
38 var $rec_src=false; // Source for recursive move
39 var $rec_cpy=false; // Is recursive move requested ?
41 /* Headpage attributes */
42 var $last_dep_sorting= "invalid";
43 var $departments= array();
45 /* attribute list for save action */
46 var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
47 "telephoneNumber", "facsimileTelephoneNumber", "gosaUnitTag");
48 var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
50 function department ($config, $dn)
51 {
53 plugin::plugin($config, $dn);
54 $this->is_account= TRUE;
55 $this->ui= get_userinfo();
56 $this->dn= $dn;
57 $this->orig_dn= $dn;
58 $this->config= $config;
60 /* Set base */
61 if ($this->dn == "new"){
62 $ui= get_userinfo();
63 if(isset($_SESSION['CurrentMainBase'])){
64 $this->base= $_SESSION['CurrentMainBase'];
65 }else{
66 $this->base= dn2base($ui->dn);
67 }
68 } else {
69 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
70 }
72 /* set permissions */
73 $ui= get_userinfo();
74 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
75 $this->acl= get_module_permission($acl, "department", $ui->dn);
77 /* Is administrational Unit? */
78 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
79 $this->is_administrational_unit= true;
80 }
81 }
83 function execute()
84 {
85 /* Call parent execute */
86 plugin::execute();
88 /* Reload departments */
89 $this->config->get_departments($this->dn);
90 $this->config->make_idepartments();
91 $smarty= get_smarty();
93 /* Base select dialog */
94 $once = true;
95 foreach($_POST as $name => $value){
96 if(preg_match("/^chooseBase/",$name) && $once){
97 $once = false;
98 $this->dialog = new baseSelectDialog($this->config);
99 $this->dialog->setCurrentBase($this->base);
100 }
101 }
103 /* Dialog handling */
104 if(is_object($this->dialog)){
105 /* Must be called before save_object */
106 $this->dialog->save_object();
108 if($this->dialog->isClosed()){
109 $this->dialog = false;
110 }elseif($this->dialog->isSelected()){
111 $this->base = $this->dialog->isSelected();
112 $this->dialog= false;
113 }else{
114 return($this->dialog->execute());
115 }
116 }
118 /* Hide all departments, that are subtrees of this department */
119 $bases = $this->config->idepartments;
120 if(($this->dn == "new")||($this->dn == "")){
121 $tmp = $bases;
122 }else{
123 $tmp = array();
124 foreach($bases as $dn=>$base){
125 $fixed = str_replace("/","\\",$this->dn);
126 /* Only attach departments which are not a subtree of this one */
127 if(!preg_match("/".$fixed."/",$dn)){
128 $tmp[$dn]=$base;
129 }
130 }
131 }
132 $smarty->assign("bases", $tmp);
134 foreach ($this->attributes as $val){
135 $smarty->assign("$val", $this->$val);
136 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
137 }
138 $smarty->assign("base_select", $this->base);
140 /* Set admin unit flag */
141 if ($this->is_administrational_unit) {
142 $smarty->assign("unitTag", "checked");
143 } else {
144 $smarty->assign("unitTag", "");
145 }
146 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
148 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
149 }
151 function clear_fields()
152 {
153 $this->dn= "";
154 $this->base= "";
155 $this->acl= "#none#";
157 foreach ($this->attributes as $val){
158 $this->$val= "";
159 }
160 }
163 function remove_from_parent()
164 {
165 $ldap= $this->config->get_ldap_link();
166 $ldap->cd ($this->dn);
167 $ldap->recursive_remove();
169 /* Optionally execute a command after we're done */
170 $this->handle_post_events('remove');
171 }
174 /* Save data to object */
175 function save_object()
176 {
177 if (isset($_POST['base'])){
178 plugin::save_object();
180 /* Save base, since this is no LDAP attribute */
181 if (chkacl($this->acl, "create") == ""){
182 $this->base= $_POST['base'];
183 }
185 /* Save tagging flag */
186 if (chkacl($this->acl, "unitTag") == ""){
187 if (isset($_POST['unitTag'])){
188 $this->is_administrational_unit= true;
189 } else {
190 $this->is_administrational_unit= false;
191 }
192 }
193 }
194 }
197 /* Check values */
198 function check()
199 {
200 /* Call common method to give check the hook */
201 $message= plugin::check();
203 /* Permissions for that base? */
204 // $this->dn= "ou=$this->ou,".$this->base;
205 if (chkacl($this->acl, "create") != ""){
206 $message[]= _("You have no permissions to create a department on this 'Base'.");
207 }
209 /* Check for presence of this department */
210 $ldap= $this->config->get_ldap_link();
211 $attrs= $ldap->cat ($this->dn, array('dn'));
212 if ($this->orig_dn == "new" && !($attrs === FALSE)){
213 $message[]= _("Department with that 'Name' already exists.");
214 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
215 $message[]= _("Department with that 'Name' already exists.");
216 }
218 /* All required fields are set? */
219 if ($this->ou == ""){
220 $message[]= _("Required field 'Name' is not set.");
221 }
222 if ($this->description == ""){
223 $message[]= _("Required field 'Description' is not set.");
224 }
226 /* Validate and modify - or: spaghetti rules! */
227 $SkipNames = array( "incoming","apps","systems","fai","config",
228 preg_replace("/ou=(.*),/","\\1",get_people_ou()),
229 preg_replace("/ou=(.*),/","\\1",get_groups_ou())
230 );
231 foreach($SkipNames as $name){
232 if ($this->ou == $name){
233 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$name);
234 }
235 }
238 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
239 $message[]= _("The field 'Name' contains invalid characters.");
240 }
241 if (!is_phone_nr($this->telephoneNumber)){
242 $message[]= _("The field 'Phone' contains an invalid phone number.");
243 }
244 if (!is_phone_nr($this->facsimileTelephoneNumber)){
245 $message[]= _("The field 'Fax' contains an invalid phone number.");
246 }
248 return $message;
249 }
252 /* Save to LDAP */
253 function save()
254 {
255 $ldap= $this->config->get_ldap_link();
257 /* Add tag objects if needed */
258 if ($this->is_administrational_unit){
259 $this->objectclasses[]= "gosaAdministrativeUnit";
260 if ($this->gosaUnitTag == ""){
262 /* It's unlikely, but check if already used... */
263 $try= 5;
264 $ldap->cd($this->config->current['BASE']);
265 while ($try--){
267 /* Generate microtime stamp as tag */
268 list($usec, $sec)= explode(" ", microtime());
269 $time_stamp= preg_replace("/\./", "", $sec.$usec);
271 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
272 if ($ldap->count() == 0){
273 break;
274 }
275 }
276 if($try == 0) {
277 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
278 return;
279 }
280 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
281 }
282 } else {
283 $this->gosaUnitTag= "";
284 }
286 plugin::save();
288 /* Remove tag information if needed */
289 if (!$this->is_administrational_unit){
290 $tmp= array();
292 /* Remove gosaAdministrativeUnit from this plugin */
293 foreach($this->attrs['objectClass'] as $oc){
294 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
295 $tmp[]= $oc;
296 }
297 }
298 $this->attrs['objectClass']= $tmp;
299 if(isset($this->attrs['gosaUnitTag'])){
300 $this->attrs['gosaUnitTag']= array();
301 }
302 }
304 /* Write back to ldap */
305 $ldap= $this->config->get_ldap_link();
306 $ldap->cat($this->dn, array('dn'));
307 $a= $ldap->fetch();
308 $ldap->cd($this->dn);
310 if (count($a)){
311 $this->cleanup();
312 $ldap->modify ($this->attrs);
314 $this->handle_post_events('modify');
315 } else {
316 $ldap->add($this->attrs);
317 $this->handle_post_events('add');
318 }
319 show_ldap_error($ldap->get_error());
320 $this->tag_objects();
322 /* Optionally execute a command after we're done */
323 $this->postcreate();
324 }
327 /* Tag objects to have the gosaAdministrativeUnitTag */
328 function tag_objects()
329 {
330 $add= $this->is_administrational_unit;
331 $len= strlen($this->dn);
332 $ldap= $this->config->get_ldap_link();
333 $ldap->cd($this->dn);
334 if ($add){
335 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
336 $this->gosaUnitTag.')))', array('dn'));
337 } else {
338 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
339 }
340 while ($attrs= $ldap->fetch()){
342 /* Skip self */
343 if ($attrs['dn'] == $this->dn){
344 continue;
345 }
347 /* Check for confilicting administrative units */
348 $fix= true;
349 foreach ($this->config->adepartments as $key => $tag){
350 /* This one is shorter than our dn, its not relevant... */
351 if ($len >= strlen($key)){
352 continue;
353 }
355 /* This one matches with the latter part. Break and don't fix this entry */
356 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
357 $fix= false;
358 break;
359 }
360 }
362 /* Fix entry if needed */
363 if ($fix){
364 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag);
365 }
366 }
367 }
370 /* Move/Rename complete trees */
371 function recursive_move($src_dn, $dst_dn,$force = false)
372 {
373 if(!$force){
375 $this->rec_cpy = true;
376 $this->rec_src = $src_dn;
377 $this->rec_dst = $dst_dn;
379 $smarty = get_smarty();
381 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
383 $display= $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
384 return($display);
385 exit();
386 }else{
387 if(!$this->rec_cpy){
388 return;
389 }
391 $src_dn = $this->rec_src;
392 $dst_dn = $this->rec_dst;
394 /* Print header to have styles included */
395 $smarty= get_smarty();
396 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
397 echo "<body style='background-image:none;margin:3px;color:black'>";
399 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
402 /* Check if the destination entry exists */
403 $ldap= $this->config->get_ldap_link();
405 /* Check if destination exists - abort */
406 $ldap->cat($dst_dn, array('dn'));
407 if ($ldap->fetch()){
408 trigger_error("Recursive_move $dst_dn already exists.",
409 E_USER_WARNING);
410 echo sprintf_("Recursive_move: '%s' already exists.")."<br>", $dst_dn);
411 return (FALSE);
412 }
414 /* Perform a search for all objects to be moved */
415 $objects= array();
416 $ldap->cd($src_dn);
417 $ldap->search("(objectClass=*)", array("dn"));
418 while($attrs= $ldap->fetch()){
419 $dn= $attrs['dn'];
420 $objects[$dn]= strlen($dn);
421 }
423 /* Sort objects by indent level */
424 asort($objects);
425 reset($objects);
427 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
428 foreach ($objects as $object => $len){
431 $src= str_replace("\\","\\\\",$object);
432 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
433 $dst= str_replace($src_dn,$dst_dn,$object);
435 echo "<b>"._("Object").":</b> $src<br>";
437 if (!$this->copy($src, $dst)){
438 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
439 return (FALSE);
440 }
442 flush();
443 }
445 /* Remove src_dn */
446 $ldap->cd($src_dn);
447 $ldap->recursive_remove();
448 $this->rec_src = $this->rec_dst = "";
449 $this->rec_cpy =false;
451 echo '<p class="seperator"> </p>';
453 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
454 <br><input type='submit' name='back' value='"._("Continue")."'>
455 </form></div>";
457 echo "</body></html>";
459 return (TRUE);
460 }
461 }
463 function am_i_moved()
464 {
465 return $this->rec_cpy;
466 }
468 }
470 ?>