Code

Accidentally added debug code.
[gosa.git] / gosa-core / include / class_config.inc
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   }
70   
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::global_is_set("class_location.inc:timestamp")){
79       $tmp = stat("../include/class_location.inc");
80       if($tmp['mtime'] != session::global_get("class_location.inc:timestamp")){
81         session::global_un_set("plist");
82       }
83     }
84     $tmp = stat("../include/class_location.inc");
85     session::global_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     }
103   }  
106   function parse($filename)
107   {
109     $this->data = array(
110         "TABS"      => array(), 
111         "LOCATIONS" => array(), 
112         "SERVERS"   => array(), 
113         "MAIN"      => array(), 
114         "MENU"      => array(), 
115         "SERVICE"   => array());
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::global_is_set('size_limit')){
272         session::global_set('size_limit',$this->current['LDAPSIZELIMIT']);
273         session::global_set('size_ignore',$this->current['LDAPSIZEIGNORE']);
274       }
275     }
277     $obj  = new ldapMultiplexer($this->ldap);
278     if ($sizelimit){
279       $obj->set_size_limit(session::global_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::global_set('CurrentMainBase',$this->current['INITIAL_BASE']);
302     }
303   
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['ACCOUNTPRIMARYATTRIBUTE'])){
312       $this->current['ACCOUNTPRIMARYATTRIBUTE']= "cn";
313     }
314     if (!isset($this->current['MINID'])){
315       $this->current['MINID']= 100;
316     }
317     if (!isset($this->current['LDAPSIZELIMIT'])){
318       $this->current['LDAPSIZELIMIT']= 200;
319     }
320     if (!isset($this->current['SIZEINGORE'])){
321       $this->current['LDAPSIZEIGNORE']= TRUE;
322     } else {
323       if (preg_match("/true/i", $this->current['LDAPSIZEIGNORE'])){
324         $this->current['LDAPSIZEIGNORE']= TRUE;
325       } else {
326         $this->current['LDAPSIZEIGNORE']= FALSE;
327       }
328     }
330     /* Sort referrals, if present */
331     if (isset ($this->current['REFERRAL'])){
332       $bases= array();
333       $servers= array();
334       foreach ($this->current['REFERRAL'] as $ref){
335         $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URI']);
336         $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URI']);
337         $bases[$base]= strlen($base);
338         $servers[$base]= $server;
339       }
340       asort($bases);
341       reset($bases);
342     }
344     /* SERVER not defined? Load the one with the shortest base */
345     if (!isset($this->current['SERVER'])){
346       $this->current['SERVER']= $servers[key($bases)];
347     }
349     /* BASE not defined? Load the one with the shortest base */
350     if (!isset($this->current['BASE'])){
351       $this->current['BASE']= key($bases);
352     }
354     /* Convert BASE to have escaped special characters */
355     $this->current['BASE']= @LDAP::convert($this->current['BASE']);
357     /* Parse LDAP referral informations */
358     if (!isset($this->current['ADMINDN']) || !isset($this->current['ADMINPASSWORD'])){
359       $url= $this->current['SERVER'];
360       $referral= $this->current['REFERRAL'][$url];
361       $this->current['ADMINDN']= $referral['ADMINDN'];
362       $this->current['ADMINPASSWORD']= $referral['ADMINPASSWORD'];
363     }
365     /* Load server informations */
366     $this->load_servers();
367   }
369   function load_servers ()
370   {
371     /* Only perform actions if current is set */
372     if ($this->current === NULL){
373       return;
374     }
376     /* Fill imap servers */
377     $ldap= $this->get_ldap_link();
378     $ldap->cd ($this->current['BASE']);
380     /* Search mailMethod konfiguration in main section too 
381      */
382     $this->current['MAILMETHOD'] = $this->get_cfg_value("mailMethod","");
383     if (!isset($this->current['MAILMETHOD'])){
384       $this->current['MAILMETHOD']= "";
385     }
386     if ($this->current['MAILMETHOD'] == ""){
387       $ldap->search ("(objectClass=goMailServer)", array('cn'));
388       $this->data['SERVERS']['IMAP']= array();
389       while ($attrs= $ldap->fetch()){
390         $name= $attrs['cn'][0];
391         $this->data['SERVERS']['IMAP'][$name]= 
392           array( 
393               "server_dn"   => $attrs['dn'],
394               "connect"     => "",
395               "admin"       => "",
396               "password"    => "",
397               "sieve_server"=> "",
398               "sieve_option"=> "",
399               "sieve_port"  => "");
400       }
401     } else {
402       $ldap->search ("(&(objectClass=goImapServer)(goImapSieveServer=*))", 
403                     array('goImapName', 'goImapConnect', 'goImapAdmin', 'goImapPassword',
404             'goImapSieveServer', 'goImapSievePort'));
406       $this->data['SERVERS']['IMAP']= array();
408       while ($attrs= $ldap->fetch()){
410         /* Check if the given goImapSieveServer is in the new style "{cn:port/option}"
411            or the old style just "cn".
412          */
413         if(preg_match("/\{/",$attrs['goImapSieveServer'][0])){
414           $sieve_server = preg_replace("/^\{([^:]*).*$/","\\1",$attrs['goImapSieveServer'][0]);
415           $sieve_option = preg_replace("/^[^:]*[^\/]*+\/(.*)\}$/","\\1",$attrs['goImapSieveServer'][0]);
416         }else{
417           $sieve_server = $attrs['goImapSieveServer'][0];
418           $sieve_option = "";
419         }
421         $pwd            = $attrs['goImapPassword'][0];
422         $imap_admin     = $attrs['goImapAdmin'][0];
423         $imap_connect   = $attrs['goImapConnect'][0];
424         $imap_server    = $attrs['goImapName'][0];
425         $sieve_port     = $attrs['goImapSievePort'][0];
426         
427         $this->data['SERVERS']['IMAP'][$imap_server]= 
428             array( 
429             "server_dn"   => $attrs['dn'],
430             "connect"     => $imap_connect,
431             "admin"       => $imap_admin,
432             "password"    => $pwd,
433             "sieve_server"=> $sieve_server,
434             "sieve_option"=> $sieve_option,
435             "sieve_port"  => $sieve_port);
436       }
437     }
439     /* Get kerberos server. FIXME: only one is supported currently */
440     $ldap->cd ($this->current['BASE']);
441     $ldap->search ("(&(goKrbRealm=*)(goKrbAdmin=*)(objectClass=goKrbServer))");
442     if ($ldap->count()){
443       $attrs= $ldap->fetch();
444       $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
445           'REALM' => $attrs['goKrbRealm'][0],
446           'ADMIN' => $attrs['goKrbAdmin'][0]);
447     }
449     /* Get cups server. FIXME: only one is supported currently */
450     $ldap->cd ($this->current['BASE']);
451     $ldap->search ("(objectClass=goCupsServer)");
452     if ($ldap->count()){
453       $attrs= $ldap->fetch();
454       $this->data['SERVERS']['CUPS']= $attrs['cn'][0];  
455     }
457     /* Get fax server. FIXME: only one is supported currently */
458     $ldap->cd ($this->current['BASE']);
459     $ldap->search ("(objectClass=goFaxServer)");
460     if ($ldap->count()){
461       $attrs= $ldap->fetch();
462       $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
463           'LOGIN' => $attrs['goFaxAdmin'][0],
464           'PASSWORD' => $attrs['goFaxPassword'][0]);
465     }
468     /* Get asterisk servers */
469     $ldap->cd ($this->current['BASE']);
470     $ldap->search ("(objectClass=goFonServer)");
471     $this->data['SERVERS']['FON']= array();
472     if ($ldap->count()){
473       while ($attrs= $ldap->fetch()){
475         /* Add 0 entry for development */
476         if(count($this->data['SERVERS']['FON']) == 0){
477           $this->data['SERVERS']['FON'][0]= array(
478               'DN'      => $attrs['dn'],
479               'SERVER'  => $attrs['cn'][0],
480               'LOGIN'   => $attrs['goFonAdmin'][0],
481               'PASSWORD'  => $attrs['goFonPassword'][0],
482               'DB'    => "gophone",
483               'SIP_TABLE'   => "sip_users",
484               'EXT_TABLE'   => "extensions",
485               'VOICE_TABLE' => "voicemail_users",
486               'QUEUE_TABLE' => "queues",
487               'QUEUE_MEMBER_TABLE'  => "queue_members");
488         }
490         /* Add entry with 'dn' as index */
491         $this->data['SERVERS']['FON'][$attrs['dn']]= array(
492             'DN'      => $attrs['dn'],
493             'SERVER'  => $attrs['cn'][0],
494             'LOGIN'   => $attrs['goFonAdmin'][0],
495             'PASSWORD'  => $attrs['goFonPassword'][0],
496             'DB'    => "gophone",
497             'SIP_TABLE'   => "sip_users",
498             'EXT_TABLE'   => "extensions",
499             'VOICE_TABLE' => "voicemail_users",
500             'QUEUE_TABLE' => "queues",
501             'QUEUE_MEMBER_TABLE'  => "queue_members");
502       }
503     }
506     /* Get glpi server */
507     $ldap->cd ($this->current['BASE']);
508     $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
509     if ($ldap->count()){
510       $attrs= $ldap->fetch();
511       if(!isset($attrs['goGlpiPassword'])){
512         $attrs['goGlpiPassword'][0] ="";
513       }
514       $this->data['SERVERS']['GLPI']= array( 
515           'SERVER'      => $attrs['cn'][0],
516           'LOGIN'       => $attrs['goGlpiAdmin'][0],
517           'PASSWORD'    => $attrs['goGlpiPassword'][0],
518           'DB'          => $attrs['goGlpiDatabase'][0]);
519     }
522     /* Get logdb server */
523     $ldap->cd ($this->current['BASE']);
524     $ldap->search ("(objectClass=goLogDBServer)");
525     if ($ldap->count()){
526       $attrs= $ldap->fetch();
527       if(!isset($attrs['goLogDB'][0])){
528         $attrs['goLogDB'][0] = "gomon";
529       }
530       $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
531           'LOGIN' => $attrs['goLogAdmin'][0],
532           'DB' => $attrs['goLogDB'][0],
533           'PASSWORD' => $attrs['goLogPassword'][0]);
534     }
537     /* GOsa logging databases */
538     $ldap->cd ($this->current['BASE']);
539     $ldap->search ("(objectClass=gosaLogServer)");
540     if ($ldap->count()){
541       while($attrs= $ldap->fetch()){
542       $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]= 
543           array(
544           'DN'    => $attrs['dn'],
545           'USER'  => $attrs['goLogDBUser'][0],
546           'DB'    => $attrs['goLogDB'][0],
547           'PWD'   => $attrs['goLogDBPassword'][0]);
548       }
549     }
552     /* Get NFS server lists */
553     $tmp= array("default");
554     $tmp2= array("default");
555     $ldap->cd ($this->current['BASE']);
556     $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
557     while ($attrs= $ldap->fetch()){
558       for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
559         if(preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
560           $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
561           $tmp[]= $attrs["cn"][0].":$path";
562         }
563         if(preg_match('/^[^|]+\|[^|]+\|NBD\|.*$/', $attrs["goExportEntry"][$i])){
564           $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
565           $tmp2[]= $attrs["cn"][0].":$path";
566         }
567       }
568     }
569     $this->data['SERVERS']['NFS']= $tmp;
570     $this->data['SERVERS']['NBD']= $tmp2;
572     /* Load Terminalservers */
573     $ldap->cd ($this->current['BASE']);
574     $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
575     $this->data['SERVERS']['TERMINAL']= array();
576     $this->data['SERVERS']['TERMINAL'][]= "default";
577     $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
580     while ($attrs= $ldap->fetch()){
581       $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
582       if(isset( $attrs["gotoSessionType"]['count'])){
583         for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
584           $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i]; 
585         }
586       }
587     }
589     /* Ldap Server 
590      */
591     $this->data['SERVERS']['LDAP']= array();
592     $ldap->cd ($this->current['BASE']);
593     $ldap->search ("(&(objectClass=goLdapServer)(goLdapBase=*))");
594     while ($attrs= $ldap->fetch()){
595       $this->data['SERVERS']['LDAP'][$attrs['dn']] = $attrs;
596     }
598     /* Get misc server lists */
599     $this->data['SERVERS']['SYSLOG']= array("default");
600     $this->data['SERVERS']['NTP']= array("default");
601     $ldap->cd ($this->current['BASE']);
602     $ldap->search ("(objectClass=goNtpServer)");
603     while ($attrs= $ldap->fetch()){
604       $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
605     }
606     $ldap->cd ($this->current['BASE']);
607     $ldap->search ("(objectClass=goSyslogServer)");
608     while ($attrs= $ldap->fetch()){
609       $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
610     }
612     /* Get samba servers from LDAP, in case of samba3 */
613     if ($this->current['SAMBAVERSION'] == 3){
614       $this->data['SERVERS']['SAMBA']= array();
615       $ldap->cd ($this->current['BASE']);
616       $ldap->search ("(objectClass=sambaDomain)");
617       while ($attrs= $ldap->fetch()){
618         $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
619         if(isset($attrs["sambaSID"][0])){
620           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"]  = $attrs["sambaSID"][0];
621         }
622         if(isset($attrs["sambaAlgorithmicRidBase"][0])){
623           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
624         }
625       }
627       /* If no samba servers are found, look for configured sid/ridbase */
628       if (count($this->data['SERVERS']['SAMBA']) == 0){
629         if (!isset($this->current["SAMBASID"]) || !isset($this->current["SAMBARIDBASE"])){
630           msg_dialog::display(_("Configuration error"), _("sambaSID and/or sambaRidBase missing in the configuration!"), ERROR_DIALOG);
631         } else {
632           $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
633               "SID" => $this->current["SAMBASID"],
634               "RIDBASE" => $this->current["SAMBARIDBASE"]);
635         }
636       }
637     }
638   }
641   function get_departments($ignore_dn= "")
642   {
643     global $config;
645     /* Initialize result hash */
646     $result= array();
647     $administrative= array();
648     $result['/']= $this->current['BASE'];
649     $this->tdepartments= array();
651     /* Get all department types from department Management, to be able detect the department type.
652         -It is possible that differnty department types have the same name, 
653          in this case we have to mark the department name to be able to differentiate.
654           (e.g l=Name  or   o=Name)
655      */    
656     $types = departmentManagement::get_support_departments();
657     
658     /* Create a list of attributes to fetch */
659     $ldap_values = array("objectClass","gosaUnitTag");
660     $filter = "";
661     foreach($types as $type){
662       $ldap_values[] = $type['ATTR'];
663       $filter .= "(objectClass=".$type['OC'].")";
664     }
665     $filter = "(&(objectClass=gosaDepartment)(|".$filter."))";
667     /* Get list of department objects */
668     $ldap= $this->get_ldap_link();
669     $ldap->cd ($this->current['BASE']);
670     $ldap->search ($filter, $ldap_values);
671     while ($attrs= $ldap->fetch()){
673       /* Detect department type */
674       $type_data = array();
675       foreach($types as $t => $data){
676         if(in_array($data['OC'],$attrs['objectClass'])){
677           $type_data = $data;
678           break;    
679         }
680       }
682       /* Unknown department type -> skip 
683        */
684       if(!count($type_data)) continue;
686       $dn= $ldap->getDN();
687       $this->tdepartments[$dn]= "";
689       /* Save administrative departments */
690       if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
691           isset($attrs['gosaUnitTag'][0])){
692         $administrative[$dn]= $attrs['gosaUnitTag'][0];
693         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
694       }
695     
696       if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
697           isset($attrs['gosaUnitTag'][0])){
698         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
699       }
700     
701       if ($dn == $ignore_dn){
702         continue;
703       }
705       $c_dn = convert_department_dn($dn)." (".$type_data['ATTR'].")";
707       /* Only assign non-root departments */
708       if ($dn != $result['/']){
709         $result[$c_dn]= $dn;
710       }
711     }
713     $this->adepartments= $administrative;
714     $this->departments= $result;
715   }
718   function make_idepartments($max_size= 28)
719   {
720     global $config;
721     $base = $config->current['BASE'];
722                 $qbase = preg_quote($base, '/');
723     $utags= isset($config->current['HONOURUNITTAGS']) && preg_match('/true/i', $config->current['HONOURUNITTAGS']);
725     $arr = array();
726     $ui= get_userinfo();
728     $this->idepartments= array();
730     /* Create multidimensional array, with all departments. */
731     foreach ($this->departments as $key => $val){
733       /* When using strict_units, filter non relevant parts */
734       if ($utags){
735         if ($ui->gosaUnitTag != '' && isset($this->tdepartments[$val]) &&
736             $this->tdepartments[$val] != $ui->gosaUnitTag){
738                                                 #TODO: link with strict*
739                                                 #continue;
740         }
741       }
743       /* Split dn into single department pieces */
744       $elements = array_reverse(split(',',preg_replace("/$qbase$/",'',$val)));          
746       /* Add last ou element of current dn to our array */
747       $last = &$arr;
748       foreach($elements as $key => $ele){
750         /* skip empty */
751         if(empty($ele)) continue;
753         /* Extract department name */           
754         $elestr = trim(preg_replace('/^[^=]*+=/','', $ele),',');
755         $nameA  = trim(preg_replace('/=.*$/','', $ele),',');
756         if($nameA != 'ou'){
757           $nameA = " ($nameA)";
758         }else{
759           $nameA = '';
760         }
761     
762         /* Add to array */      
763         if($key == (count($elements)-1)){
764           $last[$elestr.$nameA]['ENTRY'] = $val;
765         }
767         /* Set next array appending position */
768         $last = &$last[$elestr.$nameA]['SUB'];
769       }
770     }
773     /* Add base entry */
774     $ret['/']['ENTRY']  = $base;
775     $ret['/']['SUB']    = $arr;
776     $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
777   }
780   /* Creates display friendly output from make_idepartments */
781   function generateDepartmentArray($arr,$depth = -1,$max_size = 256)
782   {
783     $ret = array();
784     $depth ++;
786     /* Walk through array */    
787     ksort($arr);
788     foreach($arr as $name => $entries){
790       /* If this department is the last in the current tree position 
791        * remove it, to avoid generating output for it */
792       if(count($entries['SUB'])==0){
793         unset($entries['SUB']);
794       }
796       /* Fix name, if it contains a replace tag */
797       $name= preg_replace('/\\\\,/', ',', LDAP::fix($name));
799       /* Check if current name is too long, then cut it */
800       if(mb_strlen($name, 'UTF-8')> $max_size){
801         $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
802       }
804       /* Append the name to the list */ 
805       if(isset($entries['ENTRY'])){
806         $a = "";
807         for($i = 0 ; $i < $depth ; $i ++){
808           $a.=".";
809         }
810         $ret[$entries['ENTRY']]=$a."&nbsp;".$name;
811       } 
813       /* recursive add of subdepartments */
814       if(isset($entries['SUB'])){
815         $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
816       }
817     }
819     return($ret);
820   }
822   /* This function returns all available Shares defined in this ldap
823    * There are two ways to call this function, if listboxEntry is true
824    *  only name and path are attached to the array, in it is false, the whole
825    *  entry will be parsed an atached to the result.
826    */
827   function getShareList($listboxEntry = false)
828   {
829     $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","server",get_ou("serverRDN"),
830         $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
831     $return =array();
832     foreach($tmp as $entry){
834       if(isset($entry['goExportEntry']['count'])){
835         unset($entry['goExportEntry']['count']);
836       }
837       if(isset($entry['goExportEntry'])){
838         foreach($entry['goExportEntry'] as $export){
839           $shareAttrs = split("\|",$export);
840           if($listboxEntry) {
841             $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
842           }else{
843             $return[$shareAttrs[0]."|".$entry['cn'][0]]['server']       = $entry['cn'][0];
844             $return[$shareAttrs[0]."|".$entry['cn'][0]]['name']         = $shareAttrs[0];
845             $return[$shareAttrs[0]."|".$entry['cn'][0]]['description']  = $shareAttrs[1];
846             $return[$shareAttrs[0]."|".$entry['cn'][0]]['type']         = $shareAttrs[2];
847             $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset']      = $shareAttrs[3];
848             $return[$shareAttrs[0]."|".$entry['cn'][0]]['path']         = $shareAttrs[4];
849             $return[$shareAttrs[0]."|".$entry['cn'][0]]['option']       = $shareAttrs[5];
850           }
851         }
852       }
853     }
854     return($return);
855   }
858   /* This function returns all available ShareServer */
859   function getShareServerList()
860   {
861     global $config;
862     $return = array();
863     $ui = get_userinfo();
864     $base = $config->current['BASE'];
865     $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
866           get_ou("serverRDN"), $base,array("goExportEntry","cn"),GL_NONE | GL_NO_ACL_CHECK);
868     foreach($res as $entry){
869         
870         $acl = $ui->get_permissions($entry['dn'],"server","");
871         if(isset($entry['goExportEntry']['count'])){
872           unset($entry['goExportEntry']['count']);
873         }
874         foreach($entry['goExportEntry'] as $share){
875           $a_share = split("\|",$share);
876           $sharename = $a_share[0];
877           $data= array();
878           $data['NAME']   = $sharename;
879           $data['ACL']    = $acl;
880           $data['SERVER'] = $entry['cn']['0'];
881           $data['SHARE']  = $sharename;
882           $data['DISPLAY']= $entry['cn'][0]." [".$sharename."]";
883           $return[$entry['cn'][0]."|".$sharename] = $data;
884         }
885     }
886     return($return);
887   }
890   /* Check if there's the specified bool value set in the configuration */
891   function boolValueIsTrue($section, $value)
892   {
893     $section= strtoupper($section);
894     $value= strtoupper($value);
895     if (isset($this->data[$section][$value])){
896     
897       $data= $this->data[$section][$value];
898       if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
899         return TRUE;
900       }
902     }
904     return FALSE;
905   }
908   function __search(&$arr, $name, $return)
909   {
910     $return= strtoupper($return);
911     if (is_array($arr)){
912       foreach ($arr as &$a){
913         if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
914           return(isset($a[$return])?$a[$return]:"");
915         } else {
916           $res= $this->__search ($a, $name, $return);
917           if ($res != ""){
918             return $res;
919           }
920         }
921       }
922     }
923     return ("");
924   }
927   function search($class, $value, $categories= "")
928   {
929     if (is_array($categories)){
930       foreach ($categories as $category){
931         $res= $this->__search($this->data[strtoupper($category)], $class, $value);
932         if ($res != ""){
933           return $res;
934         }
935       }
936     } else {
937       if ($categories == "") {
938         return $this->__search($this->data, $class, $value);
939       } else {
940         return $this->__search($this->data[strtoupper($categories)], $class, $value);
941       }
942     } 
944     return ("");
945   }
948   function get_cfg_value($name, $default= "") {
949     $name= strtoupper($name);
951     /* Check if we have a current value for $name */
952     if (isset($this->current[$name])){
953       return ($this->current[$name]);
954     }
956     /* Check if we have a global value for $name */
957     if (isset($this->data["MAIN"][$name])){
958       return ($this->data["MAIN"][$name]);
959     }
961     return ($default);
962   }
965   function check_config_version()
966   {
967     /* Skip check, if we've already mentioned the mismatch 
968      */
969     if(session::global_is_set("LastChecked") && session::global_get("LastChecked") == $this->config_version) return;
970   
971     /* Remember last checked version 
972      */
973     session::global_set("LastChecked",$this->config_version);
975     $current = md5(file_get_contents(CONFIG_TEMPLATE_DIR."/gosa.conf"));
977     /* Check contributed config version and current config version.
978      */
979     if(($this->config_version == "NOT SET") || ($this->config_version != $current && !empty($this->config_version))){
980       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."));
981     }
982   }
985   /* On debian systems the session files are deleted with
986    *  a cronjob, which detects all files older than specified 
987    *  in php.ini:'session.gc_maxlifetime' and removes them.
988    * This function checks if the gosa.conf value matches the range
989    *  defined by session.gc_maxlifetime.
990    */
991   function check_session_lifetime()
992   {
993     if(isset($this->data['MAIN']['SESSIONLIFETIME'])){
994       $cfg_lifetime = $this->data['MAIN']['SESSIONLIFETIME'];
995       $ini_lifetime = ini_get('session.gc_maxlifetime');
996       $deb_system   = file_exists('/etc/debian_version');
997       return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));  
998     }else{
999       return(TRUE);
1000     }
1001   }
1004 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1005 ?>