1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class config {
25 /* XML parser */
26 var $parser;
27 var $config_found= FALSE;
28 var $tags= array();
29 var $level= 0;
30 var $gpc= 0;
31 var $section= "";
32 var $currentLocation= "";
34 /* Selected connection */
35 var $current= array();
37 /* Link to LDAP-server */
38 var $ldap= NULL;
39 var $referrals= array();
41 /* Configuration data */
42 var $data= array( 'TABS' => array(), 'LOCATIONS' => array(), 'SERVERS' => array(),
43 'MAIN' => array(),
44 'MENU' => array(), 'SERVICE' => array());
45 var $basedir= "";
46 var $config_version ="NOT SET";
48 /* Keep a copy of the current deparment list */
49 var $departments= array();
50 var $idepartments= array();
51 var $adepartments= array();
52 var $tdepartments= array();
53 var $filename = "";
54 var $last_modified = 0;
56 function config($filename, $basedir= "")
57 {
58 $this->parser = xml_parser_create();
59 $this->basedir= $basedir;
61 xml_set_object($this->parser, $this);
62 xml_set_element_handler($this->parser, "tag_open", "tag_close");
64 /* Parse config file directly? */
65 if ($filename != ""){
66 $this->parse($filename);
67 }
68 }
71 function check_and_reload()
72 {
73 global $ui;
75 /* Check if class_location.inc has changed, this is the case
76 if we have installed or removed plugins.
77 */
78 if(session::is_set("class_location.inc:timestamp")){
79 $tmp = stat("../include/class_location.inc");
80 if($tmp['mtime'] != session::get("class_location.inc:timestamp")){
81 session::un_set("plist");
82 }
83 }
84 $tmp = stat("../include/class_location.inc");
85 session::set("class_location.inc:timestamp",$tmp['mtime']);
89 if($this->filename != "" && filemtime($this->filename) != $this->last_modified){
91 $this->config_found= FALSE;
92 $this->tags= array();
93 $this->level= 0;
94 $this->gpc= 0;
95 $this->section= "";
96 $this->currentLocation= "";
98 $this->parser = xml_parser_create();
99 xml_set_object($this->parser, $this);
100 xml_set_element_handler($this->parser, "tag_open", "tag_close");
101 $this->parse($this->filename);
102 # if(session::is_set('plist')){
103 # session::un_set('plist');
104 # }
105 # if(session::is_set('plug')){
106 # session::un_set('plug');
107 # }
108 # if(isset($_GET['plug'])){
109 # unset($_GET['plug']);
110 # }
111 }
112 }
115 function parse($filename)
116 {
117 $this->last_modified = filemtime($filename);
118 $this->filename = $filename;
119 $fh= fopen($filename, "r");
120 $xmldata= fread($fh, 100000);
121 fclose($fh);
122 if(!xml_parse($this->parser, chop($xmldata))){
123 $msg = sprintf(_("XML error in gosa.conf: %s at line %d"),
124 xml_error_string(xml_get_error_code($this->parser)),
125 xml_get_current_line_number($this->parser));
126 msg_dialog::display(_("Configuration error"), $msg, FATAL_ERROR_DIALOG);
127 exit;
128 }
129 }
131 function tag_open($parser, $tag, $attrs)
132 {
133 /* Save last and current tag for reference */
134 $this->tags[$this->level]= $tag;
135 $this->level++;
137 /* Trigger on CONF section */
138 if ($tag == 'CONF'){
139 $this->config_found= TRUE;
140 if(isset($attrs['CONFIGVERSION'])){
141 $this->config_version = $attrs['CONFIGVERSION'];
142 }
143 }
145 /* Return if we're not in config section */
146 if (!$this->config_found){
147 return;
148 }
150 /* yes/no to true/false and upper case TRUE to true and so on*/
151 foreach($attrs as $name => $value){
152 if(preg_match("/^(true|yes)$/i",$value)){
153 $attrs[$name] = "true";
154 }elseif(preg_match("/^(false|no)$/i",$value)){
155 $attrs[$name] = "false";
156 }
157 }
159 /* Look through attributes */
160 switch ($this->tags[$this->level-1]){
163 /* Handle tab section */
164 case 'TAB': $name= $this->tags[$this->level-2];
166 /* Create new array? */
167 if (!isset($this->data['TABS'][$name])){
168 $this->data['TABS'][$name]= array();
169 }
171 /* Add elements */
172 $this->data['TABS'][$name][]= $attrs;
173 break;
175 /* Handle location */
176 case 'LOCATION':
177 if ($this->tags[$this->level-2] == 'MAIN'){
178 $name= $attrs['NAME'];
179 $name = preg_replace("/[<>\"']/","",$name);
180 $attrs['NAME'] = $name;
181 $this->currentLocation= $name;
183 /* Add location elements */
184 $this->data['LOCATIONS'][$name]= $attrs;
185 }
186 break;
188 /* Handle referral tags */
189 case 'REFERRAL':
190 if ($this->tags[$this->level-2] == 'LOCATION'){
191 $url= $attrs['URI'];
192 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
194 /* Add location elements */
195 if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
196 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
197 }
199 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
200 }
201 break;
203 /* Load main parameters */
204 case 'MAIN':
205 $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
206 break;
208 /* Load menu */
209 case 'SECTION':
210 if ($this->tags[$this->level-2] == 'MENU'){
211 $this->section= $attrs['NAME'];
212 $this->data['MENU'][$this->section]= array(); ;
213 }
214 break;
216 /* Inser plugins */
217 case 'PLUGIN':
218 if ($this->tags[$this->level-3] == 'MENU' &&
219 $this->tags[$this->level-2] == 'SECTION'){
221 $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
222 }
223 if ($this->tags[$this->level-2] == 'SERVICEMENU'){
224 $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
225 }
226 break;
227 }
228 }
230 function tag_close($parser, $tag)
231 {
232 /* Close config section */
233 if ($tag == 'CONF'){
234 $this->config_found= FALSE;
235 }
236 $this->level--;
237 }
240 function get_credentials($creds)
241 {
242 if (isset($_SERVER['HTTP_GOSA_KEY'])){
243 return (cred_decrypt($creds, $_SERVER['HTTP_GOSA_KEY']));
244 }
245 return ($creds);
246 }
249 function get_ldap_link($sizelimit= FALSE)
250 {
251 if($this->ldap === NULL || !is_resource($this->ldap->cid)){
253 /* Build new connection */
254 $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
255 $this->current['ADMINDN'], $this->get_credentials($this->current['ADMINPASSWORD']));
257 /* Check for connection */
258 if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
259 $smarty= get_smarty();
260 msg_dialog::display(_("LDAP error"), _("Cannot bind to LDAP. Please contact the system administrator."), FATAL_ERROR_DIALOG);
261 exit();
262 }
264 /* Move referrals */
265 if (!isset($this->current['REFERRAL'])){
266 $this->ldap->referrals= array();
267 } else {
268 $this->ldap->referrals= $this->current['REFERRAL'];
269 }
271 if (!session::is_set('size_limit')){
272 session::set('size_limit',$this->current['LDAPSIZELIMIT']);
273 session::set('size_ignore',$this->current['LDAPSIZEIGNORE']);
274 }
275 }
277 $obj = new ldapMultiplexer($this->ldap);
278 if ($sizelimit){
279 $obj->set_size_limit(session::get('size_limit'));
280 } else {
281 $obj->set_size_limit(0);
282 }
283 return($obj);
284 }
286 function set_current($name)
287 {
288 $this->current= $this->data['LOCATIONS'][$name];
290 if (!isset($this->current['SAMBAVERSION'])){
291 $this->current['SAMBAVERSION']= 3;
292 }
293 if (!isset($this->current['USERRDN'])){
294 $this->current['USERRDN']= "ou=people";
295 }
296 if (!isset($this->current['GROUPRDN'])){
297 $this->current['GROUPS']= "ou=groups";
298 }
300 if (isset($this->current['INITIAL_BASE'])){
301 session::set('CurrentMainBase',$this->current['INITIAL_BASE']);
302 }
304 /* Remove possibly added ',' from end of group and people ou */
305 $this->current['GROUPS'] = preg_replace("/,*$/","",$this->current['GROUPRDN']);
306 $this->current['USERRDN'] = preg_replace("/,*$/","",$this->current['USERRDN']);
308 if (!isset($this->current['SAMBAMACHINEACCOUNTRDN'])){
309 $this->current['SAMBAMACHINEACCOUNTRDN']= "ou=winstations,ou=systems";
310 }
311 if (!isset($this->current['HASH'])){
312 $this->current['HASH']= "crypt";
313 }
314 if (!isset($this->current['ACCOUNTPRIMARYATTRIBUTE'])){
315 $this->current['ACCOUNTPRIMARYATTRIBUTE']= "cn";
316 }
317 if (!isset($this->current['MINID'])){
318 $this->current['MINID']= 100;
319 }
320 if (!isset($this->current['LDAPSIZELIMIT'])){
321 $this->current['LDAPSIZELIMIT']= 200;
322 }
323 if (!isset($this->current['SIZEINGORE'])){
324 $this->current['LDAPSIZEIGNORE']= TRUE;
325 } else {
326 if (preg_match("/true/i", $this->current['LDAPSIZEIGNORE'])){
327 $this->current['LDAPSIZEIGNORE']= TRUE;
328 } else {
329 $this->current['LDAPSIZEIGNORE']= FALSE;
330 }
331 }
333 /* Sort referrals, if present */
334 if (isset ($this->current['REFERRAL'])){
335 $bases= array();
336 $servers= array();
337 foreach ($this->current['REFERRAL'] as $ref){
338 $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URI']);
339 $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URI']);
340 $bases[$base]= strlen($base);
341 $servers[$base]= $server;
342 }
343 asort($bases);
344 reset($bases);
345 }
347 /* SERVER not defined? Load the one with the shortest base */
348 if (!isset($this->current['SERVER'])){
349 $this->current['SERVER']= $servers[key($bases)];
350 }
352 /* BASE not defined? Load the one with the shortest base */
353 if (!isset($this->current['BASE'])){
354 $this->current['BASE']= key($bases);
355 }
357 /* Convert BASE to have escaped special characters */
358 $this->current['BASE']= @LDAP::convert($this->current['BASE']);
360 /* Parse LDAP referral informations */
361 if (!isset($this->current['ADMINDN']) || !isset($this->current['ADMINPASSWORD'])){
362 $url= $this->current['SERVER'];
363 $referral= $this->current['REFERRAL'][$url];
364 $this->current['ADMINDN']= $referral['ADMINDN'];
365 $this->current['ADMINPASSWORD']= $referral['ADMINPASSWORD'];
366 }
368 /* Load server informations */
369 $this->load_servers();
370 }
372 function load_servers ()
373 {
374 /* Only perform actions if current is set */
375 if ($this->current === NULL){
376 return;
377 }
379 /* Fill imap servers */
380 $ldap= $this->get_ldap_link();
381 $ldap->cd ($this->current['BASE']);
383 /* Search mailMethod konfiguration in main section too
384 */
385 $this->current['MAILMETHOD'] = $this->get_cfg_value("mailMethod","");
386 if (!isset($this->current['MAILMETHOD'])){
387 $this->current['MAILMETHOD']= "";
388 }
389 if ($this->current['MAILMETHOD'] == ""){
390 $ldap->search ("(objectClass=goMailServer)", array('cn'));
391 $this->data['SERVERS']['IMAP']= array();
392 while ($attrs= $ldap->fetch()){
393 $name= $attrs['cn'][0];
394 $this->data['SERVERS']['IMAP'][$name]=
395 array(
396 "server_dn" => $attrs['dn'],
397 "connect" => "",
398 "admin" => "",
399 "password" => "",
400 "sieve_server"=> "",
401 "sieve_option"=> "",
402 "sieve_port" => "");
403 }
404 } else {
405 $ldap->search ("(&(objectClass=goImapServer)(goImapSieveServer=*))",
406 array('goImapName', 'goImapConnect', 'goImapAdmin', 'goImapPassword',
407 'goImapSieveServer', 'goImapSievePort'));
409 $this->data['SERVERS']['IMAP']= array();
411 while ($attrs= $ldap->fetch()){
413 /* Check if the given goImapSieveServer is in the new style "{cn:port/option}"
414 or the old style just "cn".
415 */
416 if(preg_match("/\{/",$attrs['goImapSieveServer'][0])){
417 $sieve_server = preg_replace("/^\{([^:]*).*$/","\\1",$attrs['goImapSieveServer'][0]);
418 $sieve_option = preg_replace("/^[^:]*[^\/]*+\/(.*)\}$/","\\1",$attrs['goImapSieveServer'][0]);
419 }else{
420 $sieve_server = $attrs['goImapSieveServer'][0];
421 $sieve_option = "";
422 }
424 $pwd = $attrs['goImapPassword'][0];
425 $imap_admin = $attrs['goImapAdmin'][0];
426 $imap_connect = $attrs['goImapConnect'][0];
427 $imap_server = $attrs['goImapName'][0];
428 $sieve_port = $attrs['goImapSievePort'][0];
430 $this->data['SERVERS']['IMAP'][$imap_server]=
431 array(
432 "server_dn" => $attrs['dn'],
433 "connect" => $imap_connect,
434 "admin" => $imap_admin,
435 "password" => $pwd,
436 "sieve_server"=> $sieve_server,
437 "sieve_option"=> $sieve_option,
438 "sieve_port" => $sieve_port);
439 }
440 }
442 /* Get kerberos server. FIXME: only one is supported currently */
443 $ldap->cd ($this->current['BASE']);
444 $ldap->search ("(&(goKrbRealm=*)(goKrbAdmin=*)(objectClass=goKrbServer))");
445 if ($ldap->count()){
446 $attrs= $ldap->fetch();
447 $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
448 'REALM' => $attrs['goKrbRealm'][0],
449 'ADMIN' => $attrs['goKrbAdmin'][0]);
450 }
452 /* Get cups server. FIXME: only one is supported currently */
453 $ldap->cd ($this->current['BASE']);
454 $ldap->search ("(objectClass=goCupsServer)");
455 if ($ldap->count()){
456 $attrs= $ldap->fetch();
457 $this->data['SERVERS']['CUPS']= $attrs['cn'][0];
458 }
460 /* Get fax server. FIXME: only one is supported currently */
461 $ldap->cd ($this->current['BASE']);
462 $ldap->search ("(objectClass=goFaxServer)");
463 if ($ldap->count()){
464 $attrs= $ldap->fetch();
465 $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
466 'LOGIN' => $attrs['goFaxAdmin'][0],
467 'PASSWORD' => $attrs['goFaxPassword'][0]);
468 }
471 /* Get asterisk servers */
472 $ldap->cd ($this->current['BASE']);
473 $ldap->search ("(objectClass=goFonServer)");
474 $this->data['SERVERS']['FON']= array();
475 if ($ldap->count()){
476 while ($attrs= $ldap->fetch()){
478 /* Add 0 entry for development */
479 if(count($this->data['SERVERS']['FON']) == 0){
480 $this->data['SERVERS']['FON'][0]= array(
481 'DN' => $attrs['dn'],
482 'SERVER' => $attrs['cn'][0],
483 'LOGIN' => $attrs['goFonAdmin'][0],
484 'PASSWORD' => $attrs['goFonPassword'][0],
485 'DB' => "gophone",
486 'SIP_TABLE' => "sip_users",
487 'EXT_TABLE' => "extensions",
488 'VOICE_TABLE' => "voicemail_users",
489 'QUEUE_TABLE' => "queues",
490 'QUEUE_MEMBER_TABLE' => "queue_members");
491 }
493 /* Add entry with 'dn' as index */
494 $this->data['SERVERS']['FON'][$attrs['dn']]= array(
495 'DN' => $attrs['dn'],
496 'SERVER' => $attrs['cn'][0],
497 'LOGIN' => $attrs['goFonAdmin'][0],
498 'PASSWORD' => $attrs['goFonPassword'][0],
499 'DB' => "gophone",
500 'SIP_TABLE' => "sip_users",
501 'EXT_TABLE' => "extensions",
502 'VOICE_TABLE' => "voicemail_users",
503 'QUEUE_TABLE' => "queues",
504 'QUEUE_MEMBER_TABLE' => "queue_members");
505 }
506 }
509 /* Get glpi server */
510 $ldap->cd ($this->current['BASE']);
511 $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
512 if ($ldap->count()){
513 $attrs= $ldap->fetch();
514 if(!isset($attrs['goGlpiPassword'])){
515 $attrs['goGlpiPassword'][0] ="";
516 }
517 $this->data['SERVERS']['GLPI']= array(
518 'SERVER' => $attrs['cn'][0],
519 'LOGIN' => $attrs['goGlpiAdmin'][0],
520 'PASSWORD' => $attrs['goGlpiPassword'][0],
521 'DB' => $attrs['goGlpiDatabase'][0]);
522 }
525 /* Get logdb server */
526 $ldap->cd ($this->current['BASE']);
527 $ldap->search ("(objectClass=goLogDBServer)");
528 if ($ldap->count()){
529 $attrs= $ldap->fetch();
530 if(!isset($attrs['goLogDB'][0])){
531 $attrs['goLogDB'][0] = "gomon";
532 }
533 $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
534 'LOGIN' => $attrs['goLogAdmin'][0],
535 'DB' => $attrs['goLogDB'][0],
536 'PASSWORD' => $attrs['goLogPassword'][0]);
537 }
540 /* GOsa logging databases */
541 $ldap->cd ($this->current['BASE']);
542 $ldap->search ("(objectClass=gosaLogServer)");
543 if ($ldap->count()){
544 while($attrs= $ldap->fetch()){
545 $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]=
546 array(
547 'DN' => $attrs['dn'],
548 'USER' => $attrs['goLogDBUser'][0],
549 'DB' => $attrs['goLogDB'][0],
550 'PWD' => $attrs['goLogDBPassword'][0]);
551 }
552 }
555 /* Get NFS server lists */
556 $tmp= array("default");
557 $tmp2= array("default");
558 $ldap->cd ($this->current['BASE']);
559 $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
560 while ($attrs= $ldap->fetch()){
561 for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
562 if(preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
563 $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
564 $tmp[]= $attrs["cn"][0].":$path";
565 }
566 if(preg_match('/^[^|]+\|[^|]+\|NBD\|.*$/', $attrs["goExportEntry"][$i])){
567 $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
568 $tmp2[]= $attrs["cn"][0].":$path";
569 }
570 }
571 }
572 $this->data['SERVERS']['NFS']= $tmp;
573 $this->data['SERVERS']['NBD']= $tmp2;
575 /* Load Terminalservers */
576 $ldap->cd ($this->current['BASE']);
577 $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
578 $this->data['SERVERS']['TERMINAL']= array();
579 $this->data['SERVERS']['TERMINAL'][]= "default";
580 $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
583 while ($attrs= $ldap->fetch()){
584 $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
585 if(isset( $attrs["gotoSessionType"]['count'])){
586 for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
587 $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i];
588 }
589 }
590 }
592 /* Ldap Server
593 */
594 $this->data['SERVERS']['LDAP']= array();
595 $ldap->cd ($this->current['BASE']);
596 $ldap->search ("(&(objectClass=goLdapServer)(goLdapBase=*))");
597 while ($attrs= $ldap->fetch()){
598 $this->data['SERVERS']['LDAP'][$attrs['dn']] = $attrs;
599 }
601 /* Get misc server lists */
602 $this->data['SERVERS']['SYSLOG']= array("default");
603 $this->data['SERVERS']['NTP']= array("default");
604 $ldap->cd ($this->current['BASE']);
605 $ldap->search ("(objectClass=goNtpServer)");
606 while ($attrs= $ldap->fetch()){
607 $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
608 }
609 $ldap->cd ($this->current['BASE']);
610 $ldap->search ("(objectClass=goSyslogServer)");
611 while ($attrs= $ldap->fetch()){
612 $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
613 }
615 /* Get samba servers from LDAP, in case of samba3 */
616 if ($this->current['SAMBAVERSION'] == 3){
617 $this->data['SERVERS']['SAMBA']= array();
618 $ldap->cd ($this->current['BASE']);
619 $ldap->search ("(objectClass=sambaDomain)");
620 while ($attrs= $ldap->fetch()){
621 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
622 if(isset($attrs["sambaSID"][0])){
623 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"] = $attrs["sambaSID"][0];
624 }
625 if(isset($attrs["sambaAlgorithmicRidBase"][0])){
626 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
627 }
628 }
630 /* If no samba servers are found, look for configured sid/ridbase */
631 if (count($this->data['SERVERS']['SAMBA']) == 0){
632 if (!isset($this->current["SAMBASID"]) || !isset($this->current["SAMBARIDBASE"])){
633 msg_dialog::display(_("Configuration error"), _("sambaSID and/or sambaRidBase missing in the configuration!"), ERROR_DIALOG);
634 } else {
635 $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
636 "SID" => $this->current["SAMBASID"],
637 "RIDBASE" => $this->current["SAMBARIDBASE"]);
638 }
639 }
640 }
641 }
644 function get_departments($ignore_dn= "")
645 {
646 global $config;
648 /* Initialize result hash */
649 $result= array();
650 $administrative= array();
651 $result['/']= $this->current['BASE'];
652 $this->tdepartments= array();
654 /* Get all department types from department Management, to be able detect the department type.
655 -It is possible that differnty department types have the same name,
656 in this case we have to mark the department name to be able to differentiate.
657 (e.g l=Name or o=Name)
658 */
659 $types = departmentManagement::get_support_departments();
661 /* Create a list of attributes to fetch */
662 $ldap_values = array("objectClass","gosaUnitTag");
663 $filter = "";
664 foreach($types as $type){
665 $ldap_values[] = $type['ATTR'];
666 $filter .= "(objectClass=".$type['OC'].")";
667 }
668 $filter = "(&(objectClass=gosaDepartment)(|".$filter."))";
670 /* Get list of department objects */
671 $ldap= $this->get_ldap_link();
672 $ldap->cd ($this->current['BASE']);
673 $ldap->search ($filter, $ldap_values);
674 while ($attrs= $ldap->fetch()){
676 /* Detect department type */
677 $type_data = array();
678 foreach($types as $t => $data){
679 if(in_array($data['OC'],$attrs['objectClass'])){
680 $type_data = $data;
681 break;
682 }
683 }
685 /* Unknown department type -> skip
686 */
687 if(!count($type_data)) continue;
689 $dn= $ldap->getDN();
690 $this->tdepartments[$dn]= "";
692 /* Save administrative departments */
693 if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
694 isset($attrs['gosaUnitTag'][0])){
695 $administrative[$dn]= $attrs['gosaUnitTag'][0];
696 $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
697 }
699 if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
700 isset($attrs['gosaUnitTag'][0])){
701 $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
702 }
704 if ($dn == $ignore_dn){
705 continue;
706 }
708 $c_dn = convert_department_dn($dn)." (".$type_data['ATTR'].")";
710 /* Only assign non-root departments */
711 if ($dn != $result['/']){
712 $result[$c_dn]= $dn;
713 }
714 }
716 $this->adepartments= $administrative;
717 $this->departments= $result;
718 }
721 function make_idepartments($max_size= 28)
722 {
723 global $config;
724 $base = $config->current['BASE'];
725 $qbase = preg_quote($base, '/');
726 $utags= isset($config->current['HONOURUNITTAGS']) && preg_match('/true/i', $config->current['HONOURUNITTAGS']);
728 $arr = array();
729 $ui= get_userinfo();
731 $this->idepartments= array();
733 /* Create multidimensional array, with all departments. */
734 foreach ($this->departments as $key => $val){
736 /* When using strict_units, filter non relevant parts */
737 if ($utags){
738 if ($ui->gosaUnitTag != '' && isset($this->tdepartments[$val]) &&
739 $this->tdepartments[$val] != $ui->gosaUnitTag){
741 #TODO: link with strict*
742 #continue;
743 }
744 }
746 /* Split dn into single department pieces */
747 $elements = array_reverse(split(',',preg_replace("/$qbase$/",'',$val)));
749 /* Add last ou element of current dn to our array */
750 $last = &$arr;
751 foreach($elements as $key => $ele){
753 /* skip empty */
754 if(empty($ele)) continue;
756 /* Extract department name */
757 $elestr = trim(preg_replace('/^[^=]*+=/','', $ele),',');
758 $nameA = trim(preg_replace('/=.*$/','', $ele),',');
759 if($nameA != 'ou'){
760 $nameA = " ($nameA)";
761 }else{
762 $nameA = '';
763 }
765 /* Add to array */
766 if($key == (count($elements)-1)){
767 $last[$elestr.$nameA]['ENTRY'] = $val;
768 }
770 /* Set next array appending position */
771 $last = &$last[$elestr.$nameA]['SUB'];
772 }
773 }
776 /* Add base entry */
777 $ret['/']['ENTRY'] = $base;
778 $ret['/']['SUB'] = $arr;
779 $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
780 }
783 /* Creates display friendly output from make_idepartments */
784 function generateDepartmentArray($arr,$depth = -1,$max_size = 256)
785 {
786 $ret = array();
787 $depth ++;
789 /* Walk through array */
790 ksort($arr);
791 foreach($arr as $name => $entries){
793 /* If this department is the last in the current tree position
794 * remove it, to avoid generating output for it */
795 if(count($entries['SUB'])==0){
796 unset($entries['SUB']);
797 }
799 /* Fix name, if it contains a replace tag */
800 $name= preg_replace('/\\\\,/', ',', LDAP::fix($name));
802 /* Check if current name is too long, then cut it */
803 if(mb_strlen($name, 'UTF-8')> $max_size){
804 $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
805 }
807 /* Append the name to the list */
808 if(isset($entries['ENTRY'])){
809 $a = "";
810 for($i = 0 ; $i < $depth ; $i ++){
811 $a.=".";
812 }
813 $ret[$entries['ENTRY']]=$a." ".$name;
814 }
816 /* recursive add of subdepartments */
817 if(isset($entries['SUB'])){
818 $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
819 }
820 }
822 return($ret);
823 }
825 /* This function returns all available Shares defined in this ldap
826 * There are two ways to call this function, if listboxEntry is true
827 * only name and path are attached to the array, in it is false, the whole
828 * entry will be parsed an atached to the result.
829 */
830 function getShareList($listboxEntry = false)
831 {
832 $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","server",get_ou("serverRDN"),
833 $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
834 $return =array();
835 foreach($tmp as $entry){
837 if(isset($entry['goExportEntry']['count'])){
838 unset($entry['goExportEntry']['count']);
839 }
840 if(isset($entry['goExportEntry'])){
841 foreach($entry['goExportEntry'] as $export){
842 $shareAttrs = split("\|",$export);
843 if($listboxEntry) {
844 $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
845 }else{
846 $return[$shareAttrs[0]."|".$entry['cn'][0]]['server'] = $entry['cn'][0];
847 $return[$shareAttrs[0]."|".$entry['cn'][0]]['name'] = $shareAttrs[0];
848 $return[$shareAttrs[0]."|".$entry['cn'][0]]['description'] = $shareAttrs[1];
849 $return[$shareAttrs[0]."|".$entry['cn'][0]]['type'] = $shareAttrs[2];
850 $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset'] = $shareAttrs[3];
851 $return[$shareAttrs[0]."|".$entry['cn'][0]]['path'] = $shareAttrs[4];
852 $return[$shareAttrs[0]."|".$entry['cn'][0]]['option'] = $shareAttrs[5];
853 }
854 }
855 }
856 }
857 return($return);
858 }
861 /* This function returns all available ShareServer */
862 function getShareServerList()
863 {
864 global $config;
865 $return = array();
866 $ui = get_userinfo();
867 $base = $config->current['BASE'];
868 $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
869 get_ou("serverRDN"), $base,array("goExportEntry","cn"),GL_NONE | GL_NO_ACL_CHECK);
871 foreach($res as $entry){
873 $acl = $ui->get_permissions($entry['dn'],"server","");
874 if(isset($entry['goExportEntry']['count'])){
875 unset($entry['goExportEntry']['count']);
876 }
877 foreach($entry['goExportEntry'] as $share){
878 $a_share = split("\|",$share);
879 $sharename = $a_share[0];
880 $data= array();
881 $data['NAME'] = $sharename;
882 $data['ACL'] = $acl;
883 $data['SERVER'] = $entry['cn']['0'];
884 $data['SHARE'] = $sharename;
885 $data['DISPLAY']= $entry['cn'][0]." [".$sharename."]";
886 $return[$entry['cn'][0]."|".$sharename] = $data;
887 }
888 }
889 return($return);
890 }
893 /* Check if there's the specified bool value set in the configuration */
894 function boolValueIsTrue($section, $value)
895 {
896 $section= strtoupper($section);
897 $value= strtoupper($value);
898 if (isset($this->data[$section][$value])){
900 $data= $this->data[$section][$value];
901 if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
902 return TRUE;
903 }
905 }
907 return FALSE;
908 }
911 function __search(&$arr, $name, $return)
912 {
913 $return= strtoupper($return);
914 if (is_array($arr)){
915 foreach ($arr as &$a){
916 if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
917 return(isset($a[$return])?$a[$return]:"");
918 } else {
919 $res= $this->__search ($a, $name, $return);
920 if ($res != ""){
921 return $res;
922 }
923 }
924 }
925 }
926 return ("");
927 }
930 function search($class, $value, $categories= "")
931 {
932 if (is_array($categories)){
933 foreach ($categories as $category){
934 $res= $this->__search($this->data[strtoupper($category)], $class, $value);
935 if ($res != ""){
936 return $res;
937 }
938 }
939 } else {
940 if ($categories == "") {
941 return $this->__search($this->data, $class, $value);
942 } else {
943 return $this->__search($this->data[strtoupper($categories)], $class, $value);
944 }
945 }
947 return ("");
948 }
951 function get_cfg_value($name, $default= "") {
952 $name= strtoupper($name);
954 /* Check if we have a current value for $name */
955 if (isset($this->current[$name])){
956 return ($this->current[$name]);
957 }
959 /* Check if we have a global value for $name */
960 if (isset($this->data["MAIN"][$name])){
961 return ($this->data["MAIN"][$name]);
962 }
964 return ($default);
965 }
968 function check_config_version()
969 {
970 /* Skip check, if we've already mentioned the mismatch
971 */
972 if(session::is_set("LastChecked") && session::get("LastChecked") == $this->config_version) return;
974 /* Remember last checked version
975 */
976 session::set("LastChecked",$this->config_version);
978 $current = md5(file_get_contents(CONFIG_TEMPLATE_DIR."/gosa.conf"));
980 /* Check contributed config version and current config version.
981 */
982 if(($this->config_version == "NOT SET") || ($this->config_version != $current && !empty($this->config_version))){
983 msg_dialog::display(_("Configuration"),_("The configuration file you are using seems to be outdated. Please move the GOsa configuration file away to run the GOsa setup again."));
984 }
985 }
988 /* On debian systems the session files are deleted with
989 * a cronjob, which detects all files older than specified
990 * in php.ini:'session.gc_maxlifetime' and removes them.
991 * This function checks if the gosa.conf value matches the range
992 * defined by session.gc_maxlifetime.
993 */
994 function check_session_lifetime()
995 {
996 if(isset($this->data['MAIN']['SESSIONLIFETIME'])){
997 $cfg_lifetime = $this->data['MAIN']['SESSIONLIFETIME'];
998 $ini_lifetime = ini_get('session.gc_maxlifetime');
999 $deb_system = file_exists('/etc/debian_version');
1000 return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));
1001 }else{
1002 return(TRUE);
1003 }
1004 }
1005 }
1007 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1008 ?>