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);
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 if ($this->ou == "incoming"){
228 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
229 " Please choose another name.");
230 }
231 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
232 $message[]= _("The field 'Name' contains invalid characters.");
233 }
234 if (!is_phone_nr($this->telephoneNumber)){
235 $message[]= _("The field 'Phone' contains an invalid phone number.");
236 }
237 if (!is_phone_nr($this->facsimileTelephoneNumber)){
238 $message[]= _("The field 'Fax' contains an invalid phone number.");
239 }
241 return $message;
242 }
245 /* Save to LDAP */
246 function save()
247 {
248 $ldap= $this->config->get_ldap_link();
250 /* Add tag objects if needed */
251 if ($this->is_administrational_unit){
252 $this->objectclasses[]= "gosaAdministrativeUnit";
253 if ($this->gosaUnitTag == ""){
255 /* It's unlikely, but check if already used... */
256 $try= 5;
257 $ldap->cd($this->config->current['BASE']);
258 while ($try--){
260 /* Generate microtime stamp as tag */
261 list($usec, $sec)= explode(" ", microtime());
262 $time_stamp= preg_replace("/\./", "", $sec.$usec);
264 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
265 if ($ldap->count() == 0){
266 break;
267 }
268 }
269 if($try == 0) {
270 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
271 return;
272 }
273 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
274 }
275 }
277 plugin::save();
279 /* Remove tag information if needed */
280 if (!$this->is_administrational_unit){
281 $tmp= array();
283 /* Remove gosaAdministrativeUnit from this plugin */
284 foreach($this->attrs['objectClass'] as $oc){
285 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
286 $tmp[]= $oc;
287 }
288 }
289 $this->attrs['objectClass']= $tmp;
290 $this->attrs['gosaUnitTag']= array();
291 }
293 /* Write back to ldap */
294 $ldap= $this->config->get_ldap_link();
295 $ldap->cat($this->dn);
296 $a= $ldap->fetch();
297 $ldap->cd($this->dn);
299 if (count($a)){
300 $this->cleanup();
301 $ldap->modify ($this->attrs);
303 $this->handle_post_events('modify');
304 } else {
305 $ldap->add($this->attrs);
306 $this->handle_post_events('add');
307 }
308 show_ldap_error($ldap->get_error());
310 if ($this->is_administrational_unit){
311 $this->tag_objects();
312 } else {
313 $this->untag_objects();
314 }
316 /* Optionally execute a command after we're done */
317 $this->postcreate();
318 }
321 /* Tag objects to have the gosaAdministrativeUnitTag */
322 function tag_objects()
323 {
324 echo "<pre>Performing Tag:";
325 $ldap= $this->config->get_ldap_link();
326 $ldap->cd($this->dn);
327 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
328 $this->gosaUnitTag.')))', array('dn'));
329 while ($attrs= $ldap->fetch()){
330 //FIXME: check if this is below another administrative
331 // unit
332 echo "Fix: ".$attrs['dn']."\n";
333 }
334 echo "</pre>";
335 }
337 /* Remove the gosaAdministrativeUnitTag from objects */
338 function untag_objects()
339 {
340 echo "Performing Untag:";
341 }
343 /* Move/Rename complete trees */
344 function recursive_move($src_dn, $dst_dn,$force = false)
345 {
346 if(!$force){
348 $this->rec_cpy = true;
349 $this->rec_src = $src_dn;
350 $this->rec_dst = $dst_dn;
352 $smarty = get_smarty();
354 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
356 $display= $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
357 return($display);
358 exit();
359 }else{
360 if(!$this->rec_cpy){
361 return;
362 }
364 $src_dn = $this->rec_src;
365 $dst_dn = $this->rec_dst;
367 /* Print header to have styles included */
368 $smarty= get_smarty();
369 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
370 echo "<body style='background-image:none;margin:3px;color:black'>";
372 echo "<h3>".sprintf(_("Moving %s to %s"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
375 /* Check if the destination entry exists */
376 $ldap= $this->config->get_ldap_link();
378 /* Check if destination exists - abort */
379 $ldap->cat($dst_dn);
380 if ($ldap->fetch()){
381 trigger_error("Recursive_move $dst_dn already exists.",
382 E_USER_WARNING);
383 echo "Recursive_move :$dst_dn already exists.<br>";
384 return (FALSE);
385 }
387 /* Perform a search for all objects to be moved */
388 $objects= array();
389 $ldap->cd($src_dn);
390 $ldap->search("(objectClass=*)", array("dn"));
391 while($attrs= $ldap->fetch()){
392 $dn= $attrs['dn'];
393 $objects[$dn]= strlen($dn);
394 }
396 /* Sort objects by indent level */
397 asort($objects);
398 reset($objects);
400 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
401 foreach ($objects as $object => $len){
404 $src= str_replace("\\","\\\\",$object);
405 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
406 $dst= str_replace($src_dn,$dst_dn,$object);
408 echo "<b>"._("Object").":</b> $src<br>";
410 if (!$this->copy($src, $dst)){
411 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
412 return (FALSE);
413 }
415 flush();
416 }
418 /* Remove src_dn */
419 $ldap->cd($src_dn);
420 $ldap->recursive_remove();
421 $this->rec_src = $this->rec_dst = "";
422 $this->rec_cpy =false;
424 echo '<p class="seperator"> </p>';
426 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
427 <br><input type='submit' name='back' value='"._("Continue")."'>
428 </form></div>";
430 echo "</body></html>";
432 return (TRUE);
433 }
434 }
436 function am_i_moved()
437 {
438 return $this->rec_cpy;
439 }
441 }
443 ?>