bbb1ada80b4feacde94928f4fa6b9f93ce13b901
1 <?php
4 /* Sort multidimensional arrays for key 'text' */
5 function sort_list($val1, $val2)
6 {
7 $v1= strtolower($val1['text']);
8 $v2= strtolower($val2['text']);
9 if ($v1 > $v2){
10 return 1;
11 }
12 if ($v1 < $v2){
13 return -1;
14 }
15 return 0;
16 }
19 class ogroup extends plugin
20 {
21 /* Variables */
22 var $cn= "";
23 var $description= "";
24 var $base= "";
25 var $gosaGroupObjects= "";
26 var $department= "";
27 var $objects= array();
28 var $allobjects= array();
29 var $memberList= array();
30 var $member= array();
31 var $orig_dn= "";
32 var $group_dialog= FALSE;
34 /* attribute list for save action */
35 var $attributes= array("cn", "description", "gosaGroupObjects");
36 var $objectclasses= array("top", "gosaGroupOfNames");
38 function ogroup ($config, $dn= NULL)
39 {
40 plugin::plugin ($config, $dn);
41 $this->orig_dn= $dn;
43 /* Load member objects */
44 if (isset($this->attrs['member'])){
45 foreach ($this->attrs['member'] as $key => $value){
46 if ("$key" != "count"){
47 $this->member["$value"]= "$value";
48 }
49 }
50 }
51 $this->is_account= TRUE;
53 /* Get global filter config */
54 if (!is_global("ogfilter")){
55 $ui= get_userinfo();
56 $base= get_base_from_people($ui->dn);
57 $ogfilter= array( "dselect" => $base,
58 "regex" => "*");
59 register_global("ogfilter", $ogfilter);
60 }
61 $ogfilter= get_global('ogfilter');
63 /* Adjust flags */
64 foreach( array( "U" => "accounts",
65 "G" => "groups",
66 "A" => "applications",
67 "D" => "departments",
68 "S" => "servers",
69 "W" => "workstations",
70 "T" => "terminals",
71 "F" => "phones",
72 "P" => "printers") as $key => $val){
74 if (preg_match("/$key/", $this->gosaGroupObjects)){
75 $ogfilter[$val]= "checked";
76 } else {
77 $ogfilter[$val]= "";
78 }
79 }
80 register_global("ogfilter", $ogfilter);
82 if(isset($_SESSION['ogroupfilter']['depselect'])){
83 $this->base = $_SESSION['ogroupfilter']['depselect'];
84 }
86 /* Load member data */
87 $this->reload();
88 }
90 function execute()
91 {
92 /* Do we represent a valid group? */
93 if (!$this->is_account){
94 $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
95 _("This 'dn' is no object group.")."</b>";
96 return ($display);
97 }
101 /* Delete objects from group */
102 if (isset($_POST['delete_membership']) && isset($_POST['members'])){
103 foreach ($_POST['members'] as $value){
104 $this->objects["$value"]= $this->memberList[$value];
105 unset ($this->memberList["$value"]);
106 unset ($this->member["$value"]);
107 uasort ($this->objects, 'sort_list');
108 reset ($this->objects);
109 }
110 $this->reload();
111 }
113 /* Add objects to group */
114 if (isset($_POST['add_object_finish']) && isset($_POST['objects'])){
115 foreach ($_POST['objects'] as $value){
116 $this->memberList["$value"]= $this->objects[$value];
117 $this->member["$value"]= $value;
118 unset ($this->objects[$value]);
119 uasort ($this->memberList, 'sort_list');
120 reset ($this->memberList);
121 }
122 $this->reload();
123 }
125 /* Load templating engine */
126 $smarty= get_smarty();
128 /* Add objects? */
129 if (isset($_POST["edit_membership"])){
130 $this->group_dialog= TRUE;
131 $this->dialog= TRUE;
132 }
134 /* Add objects finished? */
135 if (isset($_POST["add_object_finish"]) || isset($_POST["add_object_cancel"])){
136 $this->group_dialog= FALSE;
137 $this->dialog= FALSE;
138 }
140 /* Manage object add dialog */
141 if ($this->group_dialog){
143 /* Save data */
144 $ogfilter= get_global("ogfilter");
145 foreach( array("dselect", "regex") as $type){
146 if (isset($_POST[$type])){
147 $ogfilter[$type]= $_POST[$type];
148 }
149 }
150 if (isset($_POST['dselect'])){
151 foreach( array("accounts", "groups", "applications", "departments",
152 "servers", "workstations", "terminals", "printers",
153 "phones") as $type){
155 if (isset($_POST[$type])) {
156 $ogfilter[$type]= "checked";
157 } else {
158 $ogfilter[$type]= "";
159 }
160 }
161 }
162 if (isset($_GET['search'])){
163 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
164 if ($s == "**"){
165 $s= "*";
166 }
167 $ogfilter['regex']= $s;
168 }
169 register_global("ogfilter", $ogfilter);
170 $this->reload();
172 /* Calculate actual groups */
173 $smarty->assign("objects", $this->convert_list($this->objects));
175 /* Show dialog */
176 $smarty->assign("search_image", get_template_path('images/search.png'));
177 $smarty->assign("launchimage", get_template_path('images/small_filter.png'));
178 $smarty->assign("tree_image", get_template_path('images/tree.png'));
179 $smarty->assign("deplist", $this->config->idepartments);
180 $smarty->assign("alphabet", generate_alphabet());
181 foreach( array("dselect", "regex", "accounts", "groups", "applications",
182 "departments", "servers", "workstations", "terminals",
183 "printers", "phones") as $type){
184 $smarty->assign("$type", $ogfilter[$type]);
185 }
186 $smarty->assign("hint", print_sizelimit_warning());
187 $smarty->assign("apply", apply_filter());
189 $display= $smarty->fetch (get_template_path('ogroup_objects.tpl', TRUE, dirname(__FILE__)));
190 return ($display);
191 }
193 /* Bases / Departments */
195 if (isset($_POST['base'])){
196 $this->base= $_POST['base'];
197 }
199 /* Assemble combine string */
200 if ($this->gosaGroupObjects == "[]"){
201 $smarty->assign("combinedObjects", _("none"));
202 } elseif (strlen($this->gosaGroupObjects) > 4){
203 $smarty->assign("combinedObjects", "<font color=red>"._("too many different objects!")."</font>");
204 } else {
205 $conv= array( "U" => _("users"),
206 "G" => _("groups"),
207 "A" => _("applications"),
208 "D" => _("departments"),
209 "S" => _("servers"),
210 "W" => _("workstations"),
211 "T" => _("terminals"),
212 "F" => _("phones"),
213 "P" => _("printers"));
215 $type= preg_replace('/[\[\]]/', '', $this->gosaGroupObjects);
216 $p1= $conv[$type[0]];
217 error_reporting(0);
218 if (isset($type[1]) && preg_match('/[UGADSFWTP]/', $type[1])){
219 $p2= $conv[$type[1]];
220 $smarty->assign("combinedObjects", "$p1 "._("and")." $p2");
221 } else {
222 $smarty->assign("combinedObjects", "$p1");
223 }
224 error_reporting(E_ALL);
225 }
227 /* Assign variables */
228 $smarty->assign("bases", $this->config->idepartments);
229 $smarty->assign("base_select", $this->base);
230 $smarty->assign("department", $this->department);
231 $smarty->assign("members", $this->convert_list($this->memberList));
233 /* Objects have to be tuned... */
234 $smarty->assign("objects", $this->convert_list($this->objects));
236 /* Fields */
237 foreach ($this->attributes as $val){
238 $smarty->assign("$val", $this->$val);
239 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
240 }
242 /* Assign ACL's */
243 foreach (array("base", "members") as $val){
244 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
245 }
247 return ($smarty->fetch (get_template_path('generic.tpl', TRUE)));
248 }
251 /* Save data to object */
252 function save_object()
253 {
254 /* Save additional values for possible next step */
255 if (isset($_POST['ogroupedit'])){
256 plugin::save_object();
258 if (chkacl ($this->acl, "base") == "" && isset($_POST["base"])){
259 $this->base= $_POST["base"];
260 }
262 }
263 }
266 /* (Re-)Load objects */
267 function reload()
268 {
269 /* Generate object list */
270 $this->objects= array();
271 $this->allobjects= array();
272 $ldap= $this->config->get_ldap_link();
274 /* Assemble filter */
275 $ogfilter= get_global("ogfilter");
277 $ldap->cd ($ogfilter['dselect']);
279 $filter= "";
280 if ($ogfilter['accounts'] == "checked"){
281 $filter.= "(objectClass=gosaAccount)";
282 }
283 if ($ogfilter['groups'] == "checked"){
284 $filter.= "(objectClass=posixGroup)";
285 }
286 if ($ogfilter['applications'] == "checked"){
287 $filter.= "(objectClass=gosaApplication)";
288 }
289 if ($ogfilter['departments'] == "checked"){
290 $filter.= "(objectClass=gosaDepartment)";
291 }
292 if ($ogfilter['servers'] == "checked"){
293 $filter.= "(objectClass=goServer)";
294 }
295 if ($ogfilter['workstations'] == "checked"){
296 $filter.= "(objectClass=gotoWorkstation)";
297 }
298 if ($ogfilter['terminals'] == "checked"){
299 $filter.= "(objectClass=gotoTerminal)";
300 }
301 if ($ogfilter['printers'] == "checked"){
302 $filter.= "(objectClass=gotoPrinter)";
303 }
304 if ($ogfilter['phones'] == "checked"){
305 $filter.= "(objectClass=goFonHardware)";
306 }
307 $regex= $ogfilter['regex'];
309 $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
310 while ($attrs= $ldap->fetch()){
312 /* Get type */
313 $type= $this->getObjectType($attrs);
314 $name= $this->getObjectName($attrs);
316 /* Fill array */
317 if (isset($attrs["description"][0])){
318 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
319 } elseif (isset($attrs["uid"][0])) {
320 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
321 } else {
322 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
323 }
324 }
325 uasort ($this->objects, 'sort_list');
326 reset ($this->objects);
328 $ldap->cd ($this->config->current['BASE']);
329 $filter= "(objectClass=gosaAccount)(objectClass=posixGroup)(objectClass=gosaApplication)(objectClass=gosaDepartment)(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal)(objectClass=gotoPrinter)(objectClass=goFonHardware)";
330 $regex= "*";
332 $ldap->search ("(&(|$filter)(|(uid=$regex)(cn=$regex)(ou=$regex)))", array("dn", "cn", "ou", "description", "objectClass", "sn", "givenName", "uid"));
333 while ($attrs= $ldap->fetch()){
335 /* Get type */
336 $type= $this->getObjectType($attrs);
337 $name= $this->getObjectName($attrs);
339 /* Fill array */
340 if (isset($attrs["description"][0])){
341 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
342 } elseif (isset($attrs["uid"][0])) {
343 $this->allobjects[$attrs["dn"]]= array("text" => "$name [".$attrs["uid"][0]."]", "type" => "$type");
344 } else {
345 $this->allobjects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
346 }
347 }
348 uasort ($this->allobjects, 'sort_list');
349 reset ($this->allobjects);
351 /* Build member list */
352 $this->memberList= array();
353 foreach($this->member as $dn){
355 /* Object in object list? */
356 if (isset($this->allobjects[$dn])){
357 $this->memberList[$dn]= $this->allobjects[$dn];
358 if (isset ($this->objects[$dn])){
359 unset ($this->objects[$dn]);
360 }
362 } else {
364 /* No, try to ge informations from LDAP */
365 $ldap->cat($dn, array("cn", "sn", "givenName", "ou", "description", "objectClass"));
366 if ($ldap->error != "success"){
367 $this->memberList[$dn]= array('text' => _("Non existing dn: ")."$dn",
368 "type" => "I");
369 } else {
370 $ldap->cat($dn);
371 $attrs= $ldap->fetch();
372 $type= $this->getObjectType($attrs);
373 $name= $this->getObjectName($attrs);
375 /* Fill array */
376 if (isset($attrs["description"][0])){
377 $this->objects[$attrs["dn"]]= array("text" => "$name [".$attrs["description"][0]."]", "type" => "$type");
378 } else {
379 $this->objects[$attrs["dn"]]= array("text" => "$name", "type" => "$type");
380 }
382 }
383 }
384 }
385 uasort ($this->memberList, 'sort_list');
386 reset ($this->memberList);
388 /* Assemble types of currently combined objects */
389 $objectTypes= "";
390 foreach ($this->memberList as $dn => $desc){
392 /* Invalid object? */
393 if ($desc['type'] == 'I'){
394 continue;
395 }
397 /* Fine. Add to list. */
398 if (!preg_match('/'.$desc['type'].'/', $objectTypes)){
399 $objectTypes.= $desc['type'];
400 }
401 }
402 $this->gosaGroupObjects= "[$objectTypes]";
403 }
406 function convert_list($input)
407 {
408 $temp= "";
409 $conv= array( "U" => "select_user.png",
410 "G" => "select_groups.png",
411 "A" => "select_application.png",
412 "D" => "select_department.png",
413 "S" => "select_server.png",
414 "W" => "select_workstation.png",
415 "T" => "select_terminal.png",
416 "F" => "select_phone.png",
417 "I" => "flag.png",
418 "P" => "select_printer.png");
420 foreach ($input as $key => $value){
421 /* Generate output */
422 $temp.= "<option value=\"$key\" class=\"select\" style=\"background-image:url('".get_template_path("images/".$conv[$value['type']])."');\">".$value['text']."</option>\n";
423 }
425 return ($temp);
426 }
429 function getObjectType($attrs)
430 {
431 $type= "I";
433 foreach(array( "U" => "gosaAccount",
434 "G" => "posixGroup",
435 "A" => "gosaApplication",
436 "D" => "gosaDepartment",
437 "S" => "goServer",
438 "W" => "gotoWorkstation",
439 "T" => "gotoTerminal",
440 "F" => "goFonHardware",
441 "P" => "gotoPrinter") as $index => $class){
442 if (in_array($class, $attrs['objectClass'])){
443 $type= $index;
444 break;
445 }
446 }
448 return ($type);
449 }
452 function getObjectName($attrs)
453 {
454 /* Person? */
455 if (in_array('gosaAccount', $attrs['objectClass'])){
456 if(isset($attrs['sn']) && isset($attrs['givenName'])){
457 $name= $attrs['sn'][0].", ".$attrs['givenName'][0];
458 } else {
459 $name= $attrs['uid'][0];
460 }
461 } else {
462 if(isset($attrs["cn"][0])) {
463 $name= $attrs['cn'][0];
464 } else {
465 $name= $attrs['ou'][0];
466 }
467 }
469 return ($name);
470 }
473 function check()
474 {
475 $message= array();
477 /* Permissions for that base? */
478 if ($this->base != ""){
479 $new_dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
480 } else {
481 $new_dn= $this->dn;
482 }
484 $ui= get_userinfo();
485 $acl= get_permissions ($new_dn, $ui->subtreeACL);
486 $acl= get_module_permission($acl, "group", $new_dn);
487 if (chkacl($acl, "create") != ""){
488 $message[]= _("You have no permissions to create a group on this 'Base'.");
489 }
491 /* must: cn */
492 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
493 $message[]= "The required field 'Name' is not set.";
494 }
496 /* Need at least one member */
497 if (count($this->member) == 0){
498 $message[]= _("Object groups need at least one member!");
499 }
501 /* To many different object types? */
502 if (strlen($this->gosaGroupObjects) > 4){
503 $message[]= _("You can combine two different object types at maximum only!");
504 }
506 return ($message);
507 }
510 /* Save to LDAP */
511 function save()
512 {
513 plugin::save();
515 /* Move members to target array */
516 foreach ($this->member as $key => $desc){
517 $this->attrs['member'][]= $key;
518 }
520 $ldap= $this->config->get_ldap_link();
522 /* New accounts need proper 'dn', propagate it to remaining objects */
523 if ($this->dn == 'new'){
524 $this->dn= 'cn='.$this->cn.','.get_groups_ou().$this->base;
525 }
527 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
528 new entries. So do a check first... */
529 $ldap->cat ($this->dn);
530 if ($ldap->fetch()){
531 /* Modify needs array() to remove values :-( */
532 if (!count ($this->member)){
533 $this->attrs['member']= array();
534 }
535 $mode= "modify";
536 } else {
537 $mode= "add";
538 $ldap->cd($this->config->current['BASE']);
539 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
540 }
542 /* Write back to ldap */
543 $ldap->cd($this->dn);
544 $ldap->$mode($this->attrs);
546 /* Trigger post signal */
547 $this->handle_post_events($mode);
549 $ret= 0;
550 if (show_ldap_error($ldap->get_error())){
551 $ret= 1;
552 }
554 return ($ret);
555 }
557 function remove_from_parent()
558 {
559 plugin::remove_from_parent();
561 $ldap= $this->config->get_ldap_link();
562 $ldap->rmdir($this->dn);
563 show_ldap_error($ldap->get_error());
565 /* Trigger remove signal */
566 $this->handle_post_events("remove");
567 }
569 }
571 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
572 ?>