Code

Updated configuration file up to date check.
[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 ="";
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     if($this->filename != "" && filemtime($this->filename) != $this->last_modified){
75       $this->config_found= FALSE;
76       $this->tags= array();
77       $this->level= 0;
78       $this->gpc= 0;
79       $this->section= "";
80       $this->currentLocation= "";
82       $this->parser = xml_parser_create();
83       xml_set_object($this->parser, $this);
84       xml_set_element_handler($this->parser, "tag_open", "tag_close");
85       $this->parse($this->filename);
86 #     if(session::is_set('plist')){
87 #       session::un_set('plist');
88 #     }
89 #     if(session::is_set('plug')){
90 #       session::un_set('plug');
91 #     }
92 #     if(isset($_GET['plug'])){
93 #       unset($_GET['plug']);
94 #     }
95     }
96   }  
99   function parse($filename)
100   { 
101     $this->last_modified = filemtime($filename);
102     $this->filename = $filename;
103     $fh= fopen($filename, "r"); 
104     $xmldata= fread($fh, 100000);
105     fclose($fh); 
106     if(!xml_parse($this->parser, chop($xmldata))){
107       $msg = sprintf(_("XML error in gosa.conf: %s at line %d"),
108             xml_error_string(xml_get_error_code($this->parser)),
109             xml_get_current_line_number($this->parser));
110       msg_dialog::display(_("Configuration error"), $msg, FATAL_ERROR_DIALOG);
111       exit;
112     }
113   }
115   function tag_open($parser, $tag, $attrs)
116   {
117     /* Save last and current tag for reference */
118     $this->tags[$this->level]= $tag;
119     $this->level++;
121     /* Trigger on CONF section */
122     if ($tag == 'CONF'){
123       $this->config_found= TRUE;
124       if(isset($attrs['CONFIG_VERSION'])){
125         $this->config_version = $attrs['CONFIG_VERSION'];
126       }
127     }
129     /* Return if we're not in config section */
130     if (!$this->config_found){
131       return;
132     }
134     /* yes/no to true/false and upper case TRUE to true and so on*/
135     foreach($attrs as $name => $value){
136       if(preg_match("/^(true|yes)$/i",$value)){
137         $attrs[$name] = "true";
138       }elseif(preg_match("/^(false|no)$/i",$value)){
139         $attrs[$name] = "false";
140       } 
141     }
143     /* Look through attributes */
144     switch ($this->tags[$this->level-1]){
147       /* Handle tab section */
148       case 'TAB':       $name= $this->tags[$this->level-2];
150                   /* Create new array? */
151                   if (!isset($this->data['TABS'][$name])){
152                     $this->data['TABS'][$name]= array();
153                   }
155                   /* Add elements */
156                   $this->data['TABS'][$name][]= $attrs;
157                   break;
159                   /* Handle location */
160       case 'LOCATION':
161                   if ($this->tags[$this->level-2] == 'MAIN'){
162                     $name= $attrs['NAME'];
163                     $this->currentLocation= $name;
165                     /* Add location elements */
166                       $this->data['LOCATIONS'][$name]= $attrs;
167                     }
168                   break;
170                   /* Handle referral tags */
171       case 'REFERRAL':
172                   if ($this->tags[$this->level-2] == 'LOCATION'){
173                     $url= $attrs['URL'];
174                     $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
176                     /* Add location elements */
177                     if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
178                       $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
179                     }
181                     $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
182                   }
183                   break;
185                   /* Load main parameters */
186       case 'MAIN':
187                   $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
188                   break;
190                   /* Load menu */
191       case 'SECTION':
192                   if ($this->tags[$this->level-2] == 'MENU'){
193                     $this->section= $attrs['NAME'];
194                     $this->data['MENU'][$this->section]= array(); ;
195                   }
196                   break;
198                   /* Inser plugins */
199       case 'PLUGIN':
200                   if ($this->tags[$this->level-3] == 'MENU' &&
201                       $this->tags[$this->level-2] == 'SECTION'){
203                     $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
204                   }
205                   if ($this->tags[$this->level-2] == 'SERVICEMENU'){
206                     $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
207                   }
208                   break;
209     }
210   }
212   function tag_close($parser, $tag)
213   {
214     /* Close config section */
215     if ($tag == 'CONF'){
216       $this->config_found= FALSE;
217     }
218     $this->level--;
219   }
222   function get_credentials($creds)
223   {
224     if (isset($_SERVER['HTTP_GOSA_KEY'])){
225       return (cred_decrypt($creds, $_SERVER['HTTP_GOSA_KEY']));
226     }
227     return ($creds);
228   }
231   function get_ldap_link($sizelimit= FALSE)
232   {
233     if($this->ldap === NULL || !is_resource($this->ldap->cid)){
235       /* Build new connection */
236       $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
237           $this->current['ADMIN'], $this->get_credentials($this->current['PASSWORD']));
239       /* Check for connection */
240       if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
241         $smarty= get_smarty();
242         msg_dialog::display(_("LDAP error"), _("Cannot bind to LDAP. Please contact the system administrator."), FATAL_ERROR_DIALOG);
243         exit();
244       }
246       if (!session::is_set('size_limit')){
247         session::set('size_limit',$this->current['SIZELIMIT']);
248         session::set('size_ignore',$this->current['SIZEIGNORE']);
249       }
251       if ($sizelimit){
252         $this->ldap->set_size_limit(session::get('size_limit'));
253       } else {
254         $this->ldap->set_size_limit(0);
255       }
257       /* Move referrals */
258       if (!isset($this->current['REFERRAL'])){
259         $this->ldap->referrals= array();
260       } else {
261         $this->ldap->referrals= $this->current['REFERRAL'];
262       }
263     }
265     return new ldapMultiplexer($this->ldap);
266   }
268   function set_current($name)
269   {
270     $this->current= $this->data['LOCATIONS'][$name];
271     if (!isset($this->current['PEOPLE'])){
272       $this->current['PEOPLE']= "ou=people";
273     }
274     if (!isset($this->current['GROUPS'])){
275       $this->current['GROUPS']= "ou=groups";
276     }
278     if (isset($this->current['INITIAL_BASE'])){
279       session::set('CurrentMainBase',$this->current['INITIAL_BASE']);
280     }
281   
282     /* Remove possibly added ',' from end of group and people ou */
283     $this->current['GROUPS'] = preg_replace("/,*$/","",$this->current['GROUPS']);
284     $this->current['PEOPLE'] = preg_replace("/,*$/","",$this->current['PEOPLE']);
286     if (!isset($this->current['WINSTATIONS'])){
287       $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
288     }
289     if (!isset($this->current['HASH'])){
290       $this->current['HASH']= "crypt";
291     }
292     if (!isset($this->current['DNMODE'])){
293       $this->current['DNMODE']= "cn";
294     }
295     if (!isset($this->current['MINID'])){
296       $this->current['MINID']= 100;
297     }
298     if (!isset($this->current['SIZELIMIT'])){
299       $this->current['SIZELIMIT']= 200;
300     }
301     if (!isset($this->current['SIZEINGORE'])){
302       $this->current['SIZEIGNORE']= TRUE;
303     } else {
304       if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
305         $this->current['SIZEIGNORE']= TRUE;
306       } else {
307         $this->current['SIZEIGNORE']= FALSE;
308       }
309     }
311     /* Sort referrals, if present */
312     if (isset ($this->current['REFERRAL'])){
313       $bases= array();
314       $servers= array();
315       foreach ($this->current['REFERRAL'] as $ref){
316         $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
317         $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
318         $bases[$base]= strlen($base);
319         $servers[$base]= $server;
320       }
321       asort($bases);
322       reset($bases);
323     }
325     /* SERVER not defined? Load the one with the shortest base */
326     if (!isset($this->current['SERVER'])){
327       $this->current['SERVER']= $servers[key($bases)];
328     }
330     /* BASE not defined? Load the one with the shortest base */
331     if (!isset($this->current['BASE'])){
332       $this->current['BASE']= key($bases);
333     }
335     /* Convert BASE to have escaped special characters */
336     $this->current['BASE']= @LDAP::convert($this->current['BASE']);
338     /* Parse LDAP referral informations */
339     if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
340       $url= $this->current['SERVER'];
341       $referral= $this->current['REFERRAL'][$url];
342       $this->current['ADMIN']= $referral['ADMIN'];
343       $this->current['PASSWORD']= $referral['PASSWORD'];
344     }
346     /* Load server informations */
347     $this->load_servers();
348   }
350   function load_servers ()
351   {
352     /* Only perform actions if current is set */
353     if ($this->current === NULL){
354       return;
355     }
357     /* Fill imap servers */
358     $ldap= $this->get_ldap_link();
359     $ldap->cd ($this->current['BASE']);
360     if (!isset($this->current['MAILMETHOD'])){
361       $this->current['MAILMETHOD']= "";
362     }
363     if ($this->current['MAILMETHOD'] == ""){
364       $ldap->search ("(objectClass=goMailServer)", array('cn'));
365       $this->data['SERVERS']['IMAP']= array();
366       error_reporting(0);
367       while ($attrs= $ldap->fetch()){
368         $name= $attrs['cn'][0];
369         $this->data['SERVERS']['IMAP'][$name]= $name;
370       }
371       error_reporting(E_ALL);
372     } else {
373       $ldap->search ("(&(objectClass=goImapServer)(goImapSieveServer=*))", 
374                     array('goImapName', 'goImapConnect', 'goImapAdmin', 'goImapPassword',
375             'goImapSieveServer', 'goImapSievePort'));
377       $this->data['SERVERS']['IMAP']= array();
379       while ($attrs= $ldap->fetch()){
381         /* Check if the given goImapSieveServer is in the new style "{cn:port/option}"
382            or the old style just "cn".
383          */
384         if(preg_match("/\{/",$attrs['goImapSieveServer'][0])){
385           $sieve_server = preg_replace("/^\{([^:]*).*$/","\\1",$attrs['goImapSieveServer'][0]);
386           $sieve_option = preg_replace("/^[^:]*[^\/]*+\/(.*)\}$/","\\1",$attrs['goImapSieveServer'][0]);
387         }else{
388           $sieve_server = $attrs['goImapSieveServer'][0];
389           $sieve_option = "";
390         }
392         $pwd            = $attrs['goImapPassword'][0];
393         $imap_admin     = $attrs['goImapAdmin'][0];
394         $imap_connect   = $attrs['goImapConnect'][0];
395         $imap_server    = $attrs['goImapName'][0];
396         $sieve_port     = $attrs['goImapSievePort'][0];
397         
398         $this->data['SERVERS']['IMAP'][$imap_server]= 
399             array( 
400             "connect"     => $imap_connect,
401             "admin"       => $imap_admin,
402             "password"    => $pwd,
403             "sieve_server"=> $sieve_server,
404             "sieve_option"=> $sieve_option,
405             "sieve_port"  => $sieve_port);
406       }
407     }
409     /* Get kerberos server. FIXME: only one is supported currently */
410     $ldap->cd ($this->current['BASE']);
411     $ldap->search ("(&(goKrbRealm=*)(goKrbAdmin=*)(objectClass=goKrbServer))");
412     if ($ldap->count()){
413       $attrs= $ldap->fetch();
414       $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
415           'REALM' => $attrs['goKrbRealm'][0],
416           'ADMIN' => $attrs['goKrbAdmin'][0]);
417     }
419     /* Get cups server. FIXME: only one is supported currently */
420     $ldap->cd ($this->current['BASE']);
421     $ldap->search ("(objectClass=goCupsServer)");
422     if ($ldap->count()){
423       $attrs= $ldap->fetch();
424       $this->data['SERVERS']['CUPS']= $attrs['cn'][0];  
425     }
427     /* Get fax server. FIXME: only one is supported currently */
428     $ldap->cd ($this->current['BASE']);
429     $ldap->search ("(objectClass=goFaxServer)");
430     if ($ldap->count()){
431       $attrs= $ldap->fetch();
432       $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
433           'LOGIN' => $attrs['goFaxAdmin'][0],
434           'PASSWORD' => $attrs['goFaxPassword'][0]);
435     }
438     /* Get asterisk servers */
439     $ldap->cd ($this->current['BASE']);
440     $ldap->search ("(objectClass=goFonServer)");
441     $this->data['SERVERS']['FON']= array();
442     if ($ldap->count()){
443       while ($attrs= $ldap->fetch()){
445         /* Add 0 entry for development */
446         if(count($this->data['SERVERS']['FON']) == 0){
447           $this->data['SERVERS']['FON'][0]= array(
448               'DN'      => $attrs['dn'],
449               'SERVER'  => $attrs['cn'][0],
450               'LOGIN'   => $attrs['goFonAdmin'][0],
451               'PASSWORD'  => $attrs['goFonPassword'][0],
452               'DB'    => "gophone",
453               'SIP_TABLE'   => "sip_users",
454               'EXT_TABLE'   => "extensions",
455               'VOICE_TABLE' => "voicemail_users",
456               'QUEUE_TABLE' => "queues",
457               'QUEUE_MEMBER_TABLE'  => "queue_members");
458         }
460         /* Add entry with 'dn' as index */
461         $this->data['SERVERS']['FON'][$attrs['dn']]= array(
462             'DN'      => $attrs['dn'],
463             'SERVER'  => $attrs['cn'][0],
464             'LOGIN'   => $attrs['goFonAdmin'][0],
465             'PASSWORD'  => $attrs['goFonPassword'][0],
466             'DB'    => "gophone",
467             'SIP_TABLE'   => "sip_users",
468             'EXT_TABLE'   => "extensions",
469             'VOICE_TABLE' => "voicemail_users",
470             'QUEUE_TABLE' => "queues",
471             'QUEUE_MEMBER_TABLE'  => "queue_members");
472       }
473     }
476     /* Get glpi server */
477     $ldap->cd ($this->current['BASE']);
478     $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
479     if ($ldap->count()){
480       $attrs= $ldap->fetch();
481       if(!isset($attrs['goGlpiPassword'])){
482         $attrs['goGlpiPassword'][0] ="";
483       }
484       $this->data['SERVERS']['GLPI']= array( 
485           'SERVER'      => $attrs['cn'][0],
486           'LOGIN'       => $attrs['goGlpiAdmin'][0],
487           'PASSWORD'    => $attrs['goGlpiPassword'][0],
488           'DB'          => $attrs['goGlpiDatabase'][0]);
489     }
492     /* Get logdb server */
493     $ldap->cd ($this->current['BASE']);
494     $ldap->search ("(objectClass=goLogDBServer)");
495     if ($ldap->count()){
496       $attrs= $ldap->fetch();
497       $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
498           'LOGIN' => $attrs['goLogAdmin'][0],
499           'PASSWORD' => $attrs['goLogPassword'][0]);
500     }
503     /* GOsa logging databases */
504     $ldap->cd ($this->current['BASE']);
505     $ldap->search ("(objectClass=gosaLogServer)");
506     if ($ldap->count()){
507       while($attrs= $ldap->fetch()){
508       $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]= 
509           array(
510           'DN'    => $attrs['dn'],
511           'USER'  => $attrs['goLogDBUser'][0],
512           'DB'    => $attrs['goLogDB'][0],
513           'PWD'   => $attrs['goLogDBPassword'][0]);
514       }
515     }
518     /* Get NFS server lists */
519     $tmp= array("default");
520     $ldap->cd ($this->current['BASE']);
521     $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
522     while ($attrs= $ldap->fetch()){
523       for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
524         if(!preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
525           continue;
526         }
527         $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
528         $tmp[]= $attrs["cn"][0].":$path";
529       }
530     }
531     $this->data['SERVERS']['NFS']= $tmp;
533     /* Load Terminalservers */
534     $ldap->cd ($this->current['BASE']);
535     $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
536     $this->data['SERVERS']['TERMINAL']= array();
537     $this->data['SERVERS']['TERMINAL'][]= "default";
538     $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
541     while ($attrs= $ldap->fetch()){
542       $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
543       if(isset( $attrs["gotoSessionType"]['count'])){
544         for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
545           $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i]; 
546         }
547       }
548     }
550     /* Ldap Server */
551     $this->data['SERVERS']['LDAP']= array();
552     $ldap->cd ($this->current['BASE']);
553     $ldap->search ("(objectClass=goLdapServer)");
554     while ($attrs= $ldap->fetch()){
555       if (isset($attrs["goLdapBase"])){
556         for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
557           $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
558         }
559       }
560     }
562     /* Get misc server lists */
563     $this->data['SERVERS']['SYSLOG']= array("default");
564     $this->data['SERVERS']['NTP']= array("default");
565     $ldap->cd ($this->current['BASE']);
566     $ldap->search ("(objectClass=goNtpServer)");
567     while ($attrs= $ldap->fetch()){
568       $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
569     }
570     $ldap->cd ($this->current['BASE']);
571     $ldap->search ("(objectClass=goSyslogServer)");
572     while ($attrs= $ldap->fetch()){
573       $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
574     }
576     /* Get samba servers from LDAP, in case of samba3 */
577     if ($this->current['SAMBAVERSION'] == 3){
578       $this->data['SERVERS']['SAMBA']= array();
579       $ldap->cd ($this->current['BASE']);
580       $ldap->search ("(objectClass=sambaDomain)");
581       while ($attrs= $ldap->fetch()){
582         $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
583         if(isset($attrs["sambaSID"][0])){
584           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"]  = $attrs["sambaSID"][0];
585         }
586         if(isset($attrs["sambaAlgorithmicRidBase"][0])){
587           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
588         }
589       }
591       /* If no samba servers are found, look for configured sid/ridbase */
592       if (count($this->data['SERVERS']['SAMBA']) == 0){
593         if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
594           msg_dialog::display(_("Configuration error"), _("SID and/or RIDBASE missing in the configuration!"), FATAL_ERROR_DIALOG);
595           exit();
596         } else {
597           $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
598               "SID" => $this->current["SID"],
599               "RIDBASE" => $this->current["RIDBASE"]);
600         }
601       }
602     }
603   }
606   function get_departments($ignore_dn= "")
607   {
608     global $config;
610     /* Initialize result hash */
611     $result= array();
612     $administrative= array();
613     $result['/']= $this->current['BASE'];
614     $this->tdepartments= array();
616     /* Get list of department objects */
617     $ldap= $this->get_ldap_link();
618     $ldap->cd ($this->current['BASE']);
619     $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
620     while ($attrs= $ldap->fetch()){
621       $dn= $ldap->getDN();
622       $this->tdepartments[$dn]= "";
624       /* Save administrative departments */
625       if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
626           isset($attrs['gosaUnitTag'][0])){
627         $administrative[$dn]= $attrs['gosaUnitTag'][0];
628         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
629       }
630     
631       if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
632           isset($attrs['gosaUnitTag'][0])){
633         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
634       }
635     
636       if ($dn == $ignore_dn){
637         continue;
638       }
640       /* Only assign non-root departments */
641       if ($dn != $result['/']){
642         $result[convert_department_dn($dn)]= $dn;
643       }
644     }
646     $this->adepartments= $administrative;
647     $this->departments= $result;
648   }
651   function make_idepartments($max_size= 28)
652   {
653     global $config;
654     $base = $config->current['BASE'];
656     $arr = array();
657     $ui= get_userinfo();
659     $this->idepartments= array();
661     /* Create multidimensional array, with all departments. */
662     foreach ($this->departments as $key => $val){
664       /* When using strict_units, filter non relevant parts */
665       if (isset($config->current['STRICT_UNITS']) && preg_match('/true/i', $config->current['STRICT_UNITS'])){
666         if ($ui->gosaUnitTag != "" && isset($this->tdepartments[$val]) &&
667             $this->tdepartments[$val] != $ui->gosaUnitTag){
668 #          continue;
669         }
670       }
672       /* remove base from dn */
673       $val2 = str_replace($base,"",$val);
675       /* Get every single ou */
676       $str = preg_replace("/ou=/","|ou=",$val2);        
677       $elements = array_reverse(split("\|",$str));              
679       /* Save last array position */
680       $last = &$arr;
682       /* Get array depth  */
683       $cnt = count($elements);
685       /* Add last ou element of current dn to our array */
686       foreach($elements as $key => $ele){
688         /* skip enpty */
689         if(empty($ele)) continue;
691         /* Extract department name */           
692         $elestr = preg_replace("/^ou=/","", $ele);
693         $elestr = preg_replace("/,$/","",$elestr);      
695         /* Add to array */      
696         if($key == ($cnt-2)){
697           $last[$elestr]['ENTRY'] = $val;
698         }
700         /* Set next array appending position */
701         $last = &$last[$elestr]['SUB'];
702       }
703     }
705     /* Add base entry */
706     $ret["/"]["ENTRY"]  = $base;
707     $ret["/"]["SUB"]    = $arr;
709     $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
710   }
713   /* Creates display friendly output from make_idepartments */
714   function generateDepartmentArray($arr,$depth = -1,$max_size){
715     $ret = array();
716     $depth ++;
718     /* Walk through array */    
719     ksort($arr);
720     foreach($arr as $name => $entries){
722       /* If this department is the last in the current tree position 
723        * remove it, to avoid generating output for it */
724       if(count($entries['SUB'])==0){
725         unset($entries['SUB']);
726       }
728       /* Fix name, if it contains a replace tag */
729       $name= @LDAP::fix($name);
731       /* Check if current name is too long, then cut it */
732       if(mb_strlen($name, 'UTF-8')> $max_size){
733         $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
734       }
736       /* Append the name to the list */ 
737       if(isset($entries['ENTRY'])){
738         $a = "";
739         for($i = 0 ; $i < $depth ; $i ++){
740           $a.=".";
741         }
742         $ret[$entries['ENTRY']]=$a."&nbsp;".$name;
743       } 
745       /* recursive add of subdepartments */
746       if(isset($entries['SUB'])){
747         $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
748       }
749     }
751     return($ret);
752   }
754   /* This function returns all available Shares defined in this ldap
755    * There are two ways to call this function, if listboxEntry is true
756    *  only name and path are attached to the array, in it is false, the whole
757    *  entry will be parsed an atached to the result.
758    */
759   function getShareList($listboxEntry = false)
760   {
761     $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","server",get_ou("serverou"),
762         $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
763     $return =array();
764     foreach($tmp as $entry){
766       if(isset($entry['goExportEntry']['count'])){
767         unset($entry['goExportEntry']['count']);
768       }
769       if(isset($entry['goExportEntry'])){
770         foreach($entry['goExportEntry'] as $export){
771           $shareAttrs = split("\|",$export);
772           if($listboxEntry) {
773             $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
774           }else{
775             $return[$shareAttrs[0]."|".$entry['cn'][0]]['server']       = $entry['cn'][0];
776             $return[$shareAttrs[0]."|".$entry['cn'][0]]['name']         = $shareAttrs[0];
777             $return[$shareAttrs[0]."|".$entry['cn'][0]]['description']  = $shareAttrs[1];
778             $return[$shareAttrs[0]."|".$entry['cn'][0]]['type']         = $shareAttrs[2];
779             $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset']      = $shareAttrs[3];
780             $return[$shareAttrs[0]."|".$entry['cn'][0]]['path']         = $shareAttrs[4];
781             $return[$shareAttrs[0]."|".$entry['cn'][0]]['option']       = $shareAttrs[5];
782           }
783         }
784       }
785     }
786     return($return);
787   }
790   /* This function returns all available ShareServer */
791   function getShareServerList()
792   {
793     global $config;
794     $return = array();
795     $base = $config->current['BASE'];
796     $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
797           get_ou("serverou"), $base,array("goExportEntry","cn"),GL_NONE);
799     foreach($res as $entry){
800         if(isset($entry['goExportEntry']['count'])){
801           unset($entry['goExportEntry']['count']);
802         }
803         foreach($entry['goExportEntry'] as $share){
804           $a_share = split("\|",$share);
805           $sharename = $a_share[0];
806           $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
807         }
808     }
809     return($return);
810   }
813   /* Check if there's the specified bool value set in the configuration */
814   function boolValueIsTrue($section, $value)
815   {
816     $section= strtoupper($section);
817     $value= strtoupper($value);
818     if (isset($this->data[$section][$value])){
819     
820       $data= $this->data[$section][$value];
821       if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
822         return TRUE;
823       }
825     }
827     return FALSE;
828   }
831   function __search(&$arr, $name, $return)
832   {
833     $return= strtoupper($return);
834     if (is_array($arr)){
835       foreach ($arr as &$a){
836         if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
837           return(isset($a[$return])?$a[$return]:"");
838         } else {
839           $res= $this->__search ($a, $name, $return);
840           if ($res != ""){
841             return $res;
842           }
843         }
844       }
845     }
846     return ("");
847   }
850   function search($class, $value, $categories= "")
851   {
852     if (is_array($categories)){
853       foreach ($categories as $category){
854         $res= $this->__search($this->data[strtoupper($category)], $class, $value);
855         if ($res != ""){
856           return $res;
857         }
858       }
859     } else {
860       if ($categories == "") {
861         return $this->__search($this->data, $class, $value);
862       } else {
863         return $this->__search($this->data[strtoupper($categories)], $class, $value);
864       }
865     } 
867     return ("");
868   }
871   function check_config_version()
872   {
873     /* Skip check, if we've already mentioned the mismatch 
874      */
875     if(session::is_set("LastChecked") && session::get("LastChecked") == $this->config_version) return;
876   
877     /* Remember last checked version 
878      */
879     session::set("LastChecked",$this->config_version);
881     $current = md5(file_get_contents(CONFIG_TEMPLATE_DIR."/gosa.conf"));
883     /* Check contributed config version and current config version.
884      */
885     if($this->config_version != $current && !empty($this->config_version)){
886       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."));
887     }
888   }
891   /* On debian systems the session files are deleted with
892    *  a cronjob, which detects all files older than specified 
893    *  in php.ini:'session.gc_maxlifetime' and removes them.
894    * This function checks if the gosa.conf value matches the range
895    *  defined by session.gc_maxlifetime.
896    */
897   function check_session_lifetime()
898   {
899     $cfg_lifetime = $this->data['MAIN']['SESSION_LIFETIME'];
900     $ini_lifetime = ini_get('session.gc_maxlifetime');
901     $deb_system   = file_exists('/etc/debian_version');
902     return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));  
903   }
906 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
907 ?>