Code

Updated config object and convert_department dn.
[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                     $name = preg_replace("/[<>\"']/","",$name);
164                     $attrs['NAME'] = $name;
165                     $this->currentLocation= $name;
167                     /* Add location elements */
168                     $this->data['LOCATIONS'][$name]= $attrs;
169                   }
170                   break;
172                   /* Handle referral tags */
173       case 'REFERRAL':
174                   if ($this->tags[$this->level-2] == 'LOCATION'){
175                     $url= $attrs['URL'];
176                     $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
178                     /* Add location elements */
179                     if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
180                       $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
181                     }
183                     $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
184                   }
185                   break;
187                   /* Load main parameters */
188       case 'MAIN':
189                   $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
190                   break;
192                   /* Load menu */
193       case 'SECTION':
194                   if ($this->tags[$this->level-2] == 'MENU'){
195                     $this->section= $attrs['NAME'];
196                     $this->data['MENU'][$this->section]= array(); ;
197                   }
198                   break;
200                   /* Inser plugins */
201       case 'PLUGIN':
202                   if ($this->tags[$this->level-3] == 'MENU' &&
203                       $this->tags[$this->level-2] == 'SECTION'){
205                     $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
206                   }
207                   if ($this->tags[$this->level-2] == 'SERVICEMENU'){
208                     $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
209                   }
210                   break;
211     }
212   }
214   function tag_close($parser, $tag)
215   {
216     /* Close config section */
217     if ($tag == 'CONF'){
218       $this->config_found= FALSE;
219     }
220     $this->level--;
221   }
224   function get_credentials($creds)
225   {
226     if (isset($_SERVER['HTTP_GOSA_KEY'])){
227       return (cred_decrypt($creds, $_SERVER['HTTP_GOSA_KEY']));
228     }
229     return ($creds);
230   }
233   function get_ldap_link($sizelimit= FALSE)
234   {
235     if($this->ldap === NULL || !is_resource($this->ldap->cid)){
237       /* Build new connection */
238       $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
239           $this->current['ADMIN'], $this->get_credentials($this->current['PASSWORD']));
241       /* Check for connection */
242       if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
243         $smarty= get_smarty();
244         msg_dialog::display(_("LDAP error"), _("Cannot bind to LDAP. Please contact the system administrator."), FATAL_ERROR_DIALOG);
245         exit();
246       }
248       if (!session::is_set('size_limit')){
249         session::set('size_limit',$this->current['SIZELIMIT']);
250         session::set('size_ignore',$this->current['SIZEIGNORE']);
251       }
253       if ($sizelimit){
254         $this->ldap->set_size_limit(session::get('size_limit'));
255       } else {
256         $this->ldap->set_size_limit(0);
257       }
259       /* Move referrals */
260       if (!isset($this->current['REFERRAL'])){
261         $this->ldap->referrals= array();
262       } else {
263         $this->ldap->referrals= $this->current['REFERRAL'];
264       }
265     }
267     return new ldapMultiplexer($this->ldap);
268   }
270   function set_current($name)
271   {
272     $this->current= $this->data['LOCATIONS'][$name];
274     if (!isset($this->current['PEOPLE'])){
275       $this->current['PEOPLE']= "ou=people";
276     }
277     if (!isset($this->current['GROUPS'])){
278       $this->current['GROUPS']= "ou=groups";
279     }
281     if (isset($this->current['INITIAL_BASE'])){
282       session::set('CurrentMainBase',$this->current['INITIAL_BASE']);
283     }
284   
285     /* Remove possibly added ',' from end of group and people ou */
286     $this->current['GROUPS'] = preg_replace("/,*$/","",$this->current['GROUPS']);
287     $this->current['PEOPLE'] = preg_replace("/,*$/","",$this->current['PEOPLE']);
289     if (!isset($this->current['WINSTATIONS'])){
290       $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
291     }
292     if (!isset($this->current['HASH'])){
293       $this->current['HASH']= "crypt";
294     }
295     if (!isset($this->current['DNMODE'])){
296       $this->current['DNMODE']= "cn";
297     }
298     if (!isset($this->current['MINID'])){
299       $this->current['MINID']= 100;
300     }
301     if (!isset($this->current['SIZELIMIT'])){
302       $this->current['SIZELIMIT']= 200;
303     }
304     if (!isset($this->current['SIZEINGORE'])){
305       $this->current['SIZEIGNORE']= TRUE;
306     } else {
307       if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
308         $this->current['SIZEIGNORE']= TRUE;
309       } else {
310         $this->current['SIZEIGNORE']= FALSE;
311       }
312     }
314     /* Sort referrals, if present */
315     if (isset ($this->current['REFERRAL'])){
316       $bases= array();
317       $servers= array();
318       foreach ($this->current['REFERRAL'] as $ref){
319         $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
320         $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
321         $bases[$base]= strlen($base);
322         $servers[$base]= $server;
323       }
324       asort($bases);
325       reset($bases);
326     }
328     /* SERVER not defined? Load the one with the shortest base */
329     if (!isset($this->current['SERVER'])){
330       $this->current['SERVER']= $servers[key($bases)];
331     }
333     /* BASE not defined? Load the one with the shortest base */
334     if (!isset($this->current['BASE'])){
335       $this->current['BASE']= key($bases);
336     }
338     /* Convert BASE to have escaped special characters */
339     $this->current['BASE']= @LDAP::convert($this->current['BASE']);
341     /* Parse LDAP referral informations */
342     if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
343       $url= $this->current['SERVER'];
344       $referral= $this->current['REFERRAL'][$url];
345       $this->current['ADMIN']= $referral['ADMIN'];
346       $this->current['PASSWORD']= $referral['PASSWORD'];
347     }
349     /* Load server informations */
350     $this->load_servers();
351   }
353   function load_servers ()
354   {
355     /* Only perform actions if current is set */
356     if ($this->current === NULL){
357       return;
358     }
360     /* Fill imap servers */
361     $ldap= $this->get_ldap_link();
362     $ldap->cd ($this->current['BASE']);
363     if (!isset($this->current['MAILMETHOD'])){
364       $this->current['MAILMETHOD']= "";
365     }
366     if ($this->current['MAILMETHOD'] == ""){
367       $ldap->search ("(objectClass=goMailServer)", array('cn'));
368       $this->data['SERVERS']['IMAP']= array();
369       error_reporting(0);
370       while ($attrs= $ldap->fetch()){
371         $name= $attrs['cn'][0];
372         $this->data['SERVERS']['IMAP'][$name]= $name;
373       }
374       error_reporting(E_ALL);
375     } else {
376       $ldap->search ("(&(objectClass=goImapServer)(goImapSieveServer=*))", 
377                     array('goImapName', 'goImapConnect', 'goImapAdmin', 'goImapPassword',
378             'goImapSieveServer', 'goImapSievePort'));
380       $this->data['SERVERS']['IMAP']= array();
382       while ($attrs= $ldap->fetch()){
384         /* Check if the given goImapSieveServer is in the new style "{cn:port/option}"
385            or the old style just "cn".
386          */
387         if(preg_match("/\{/",$attrs['goImapSieveServer'][0])){
388           $sieve_server = preg_replace("/^\{([^:]*).*$/","\\1",$attrs['goImapSieveServer'][0]);
389           $sieve_option = preg_replace("/^[^:]*[^\/]*+\/(.*)\}$/","\\1",$attrs['goImapSieveServer'][0]);
390         }else{
391           $sieve_server = $attrs['goImapSieveServer'][0];
392           $sieve_option = "";
393         }
395         $pwd            = $attrs['goImapPassword'][0];
396         $imap_admin     = $attrs['goImapAdmin'][0];
397         $imap_connect   = $attrs['goImapConnect'][0];
398         $imap_server    = $attrs['goImapName'][0];
399         $sieve_port     = $attrs['goImapSievePort'][0];
400         
401         $this->data['SERVERS']['IMAP'][$imap_server]= 
402             array( 
403             "connect"     => $imap_connect,
404             "admin"       => $imap_admin,
405             "password"    => $pwd,
406             "sieve_server"=> $sieve_server,
407             "sieve_option"=> $sieve_option,
408             "sieve_port"  => $sieve_port);
409       }
410     }
412     /* Get kerberos server. FIXME: only one is supported currently */
413     $ldap->cd ($this->current['BASE']);
414     $ldap->search ("(&(goKrbRealm=*)(goKrbAdmin=*)(objectClass=goKrbServer))");
415     if ($ldap->count()){
416       $attrs= $ldap->fetch();
417       $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
418           'REALM' => $attrs['goKrbRealm'][0],
419           'ADMIN' => $attrs['goKrbAdmin'][0]);
420     }
422     /* Get cups server. FIXME: only one is supported currently */
423     $ldap->cd ($this->current['BASE']);
424     $ldap->search ("(objectClass=goCupsServer)");
425     if ($ldap->count()){
426       $attrs= $ldap->fetch();
427       $this->data['SERVERS']['CUPS']= $attrs['cn'][0];  
428     }
430     /* Get fax server. FIXME: only one is supported currently */
431     $ldap->cd ($this->current['BASE']);
432     $ldap->search ("(objectClass=goFaxServer)");
433     if ($ldap->count()){
434       $attrs= $ldap->fetch();
435       $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
436           'LOGIN' => $attrs['goFaxAdmin'][0],
437           'PASSWORD' => $attrs['goFaxPassword'][0]);
438     }
441     /* Get asterisk servers */
442     $ldap->cd ($this->current['BASE']);
443     $ldap->search ("(objectClass=goFonServer)");
444     $this->data['SERVERS']['FON']= array();
445     if ($ldap->count()){
446       while ($attrs= $ldap->fetch()){
448         /* Add 0 entry for development */
449         if(count($this->data['SERVERS']['FON']) == 0){
450           $this->data['SERVERS']['FON'][0]= array(
451               'DN'      => $attrs['dn'],
452               'SERVER'  => $attrs['cn'][0],
453               'LOGIN'   => $attrs['goFonAdmin'][0],
454               'PASSWORD'  => $attrs['goFonPassword'][0],
455               'DB'    => "gophone",
456               'SIP_TABLE'   => "sip_users",
457               'EXT_TABLE'   => "extensions",
458               'VOICE_TABLE' => "voicemail_users",
459               'QUEUE_TABLE' => "queues",
460               'QUEUE_MEMBER_TABLE'  => "queue_members");
461         }
463         /* Add entry with 'dn' as index */
464         $this->data['SERVERS']['FON'][$attrs['dn']]= array(
465             'DN'      => $attrs['dn'],
466             'SERVER'  => $attrs['cn'][0],
467             'LOGIN'   => $attrs['goFonAdmin'][0],
468             'PASSWORD'  => $attrs['goFonPassword'][0],
469             'DB'    => "gophone",
470             'SIP_TABLE'   => "sip_users",
471             'EXT_TABLE'   => "extensions",
472             'VOICE_TABLE' => "voicemail_users",
473             'QUEUE_TABLE' => "queues",
474             'QUEUE_MEMBER_TABLE'  => "queue_members");
475       }
476     }
479     /* Get glpi server */
480     $ldap->cd ($this->current['BASE']);
481     $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
482     if ($ldap->count()){
483       $attrs= $ldap->fetch();
484       if(!isset($attrs['goGlpiPassword'])){
485         $attrs['goGlpiPassword'][0] ="";
486       }
487       $this->data['SERVERS']['GLPI']= array( 
488           'SERVER'      => $attrs['cn'][0],
489           'LOGIN'       => $attrs['goGlpiAdmin'][0],
490           'PASSWORD'    => $attrs['goGlpiPassword'][0],
491           'DB'          => $attrs['goGlpiDatabase'][0]);
492     }
495     /* Get logdb server */
496     $ldap->cd ($this->current['BASE']);
497     $ldap->search ("(objectClass=goLogDBServer)");
498     if ($ldap->count()){
499       $attrs= $ldap->fetch();
500       $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
501           'LOGIN' => $attrs['goLogAdmin'][0],
502           'PASSWORD' => $attrs['goLogPassword'][0]);
503     }
506     /* GOsa logging databases */
507     $ldap->cd ($this->current['BASE']);
508     $ldap->search ("(objectClass=gosaLogServer)");
509     if ($ldap->count()){
510       while($attrs= $ldap->fetch()){
511       $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]= 
512           array(
513           'DN'    => $attrs['dn'],
514           'USER'  => $attrs['goLogDBUser'][0],
515           'DB'    => $attrs['goLogDB'][0],
516           'PWD'   => $attrs['goLogDBPassword'][0]);
517       }
518     }
521     /* Get NFS server lists */
522     $tmp= array("default");
523     $ldap->cd ($this->current['BASE']);
524     $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
525     while ($attrs= $ldap->fetch()){
526       for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
527         if(!preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
528           continue;
529         }
530         $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
531         $tmp[]= $attrs["cn"][0].":$path";
532       }
533     }
534     $this->data['SERVERS']['NFS']= $tmp;
536     /* Load Terminalservers */
537     $ldap->cd ($this->current['BASE']);
538     $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
539     $this->data['SERVERS']['TERMINAL']= array();
540     $this->data['SERVERS']['TERMINAL'][]= "default";
541     $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
544     while ($attrs= $ldap->fetch()){
545       $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
546       if(isset( $attrs["gotoSessionType"]['count'])){
547         for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
548           $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i]; 
549         }
550       }
551     }
553     /* Ldap Server */
554     $this->data['SERVERS']['LDAP']= array();
555     $ldap->cd ($this->current['BASE']);
556     $ldap->search ("(objectClass=goLdapServer)");
557     while ($attrs= $ldap->fetch()){
558       if (isset($attrs["goLdapBase"])){
559         for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
560           $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
561         }
562       }
563     }
565     /* Get misc server lists */
566     $this->data['SERVERS']['SYSLOG']= array("default");
567     $this->data['SERVERS']['NTP']= array("default");
568     $ldap->cd ($this->current['BASE']);
569     $ldap->search ("(objectClass=goNtpServer)");
570     while ($attrs= $ldap->fetch()){
571       $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
572     }
573     $ldap->cd ($this->current['BASE']);
574     $ldap->search ("(objectClass=goSyslogServer)");
575     while ($attrs= $ldap->fetch()){
576       $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
577     }
579     /* Get samba servers from LDAP, in case of samba3 */
580     if ($this->current['SAMBAVERSION'] == 3){
581       $this->data['SERVERS']['SAMBA']= array();
582       $ldap->cd ($this->current['BASE']);
583       $ldap->search ("(objectClass=sambaDomain)");
584       while ($attrs= $ldap->fetch()){
585         $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
586         if(isset($attrs["sambaSID"][0])){
587           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"]  = $attrs["sambaSID"][0];
588         }
589         if(isset($attrs["sambaAlgorithmicRidBase"][0])){
590           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
591         }
592       }
594       /* If no samba servers are found, look for configured sid/ridbase */
595       if (count($this->data['SERVERS']['SAMBA']) == 0){
596         if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
597           msg_dialog::display(_("Configuration error"), _("SID and/or RIDBASE missing in the configuration!"), FATAL_ERROR_DIALOG);
598           exit();
599         } else {
600           $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
601               "SID" => $this->current["SID"],
602               "RIDBASE" => $this->current["RIDBASE"]);
603         }
604       }
605     }
606   }
609   function get_departments($ignore_dn= "")
610   {
611     global $config;
613     /* Initialize result hash */
614     $result= array();
615     $administrative= array();
616     $result['/']= $this->current['BASE'];
617     $this->tdepartments= array();
619     /* Get list of department objects */
620     $ldap= $this->get_ldap_link();
621     $ldap->cd ($this->current['BASE']);
622     $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
623     while ($attrs= $ldap->fetch()){
624       $dn= $ldap->getDN();
625       $this->tdepartments[$dn]= "";
627       /* Save administrative departments */
628       if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
629           isset($attrs['gosaUnitTag'][0])){
630         $administrative[$dn]= $attrs['gosaUnitTag'][0];
631         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
632       }
633     
634       if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
635           isset($attrs['gosaUnitTag'][0])){
636         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
637       }
638     
639       if ($dn == $ignore_dn){
640         continue;
641       }
643       /* Only assign non-root departments */
644       if ($dn != $result['/']){
645         $result[convert_department_dn($dn)]= $dn;
646       }
647     }
649     $this->adepartments= $administrative;
650     $this->departments= $result;
651   }
654   function make_idepartments($max_size= 28)
655   {
656     global $config;
657     $base = $config->current['BASE'];
659     $arr = array();
660     $ui= get_userinfo();
662     $this->idepartments= array();
664     /* Create multidimensional array, with all departments. */
665     foreach ($this->departments as $key => $val){
667       /* When using strict_units, filter non relevant parts */
668       if (isset($config->current['STRICT_UNITS']) && preg_match('/true/i', $config->current['STRICT_UNITS'])){
669         if ($ui->gosaUnitTag != "" && isset($this->tdepartments[$val]) &&
670             $this->tdepartments[$val] != $ui->gosaUnitTag){
671 #          continue;
672         }
673       }
675       /* Split dn into single department pieces.
676        */
677       $elements = array_reverse(split(",",preg_replace("/".normalizePreg($base)."$/","",$val)));                
679       /* Add last ou element of current dn to our array */
680       $last = &$arr;
681       foreach($elements as $key => $ele){
683         /* skip empty */
684         if(empty($ele)) continue;
686         /* Extract department name */           
687         $elestr = trim(preg_replace("/^[^=]*+=/","", $ele),",");
689         /* Add to array */      
690         if($key == (count($elements)-1)){
691           $last[$elestr]['ENTRY'] = $val;
692         }
694         /* Set next array appending position */
695         $last = &$last[$elestr]['SUB'];
696       }
697     }
699     /* Add base entry */
700     $ret["/"]["ENTRY"]  = $base;
701     $ret["/"]["SUB"]    = $arr;
702     $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
703   }
706   /* Creates display friendly output from make_idepartments */
707   function generateDepartmentArray($arr,$depth = -1,$max_size)
708   {
709     $ret = array();
710     $depth ++;
712     /* Walk through array */    
713     ksort($arr);
714     foreach($arr as $name => $entries){
716       /* If this department is the last in the current tree position 
717        * remove it, to avoid generating output for it */
718       if(count($entries['SUB'])==0){
719         unset($entries['SUB']);
720       }
722       /* Fix name, if it contains a replace tag */
723       $name= @LDAP::fix($name);
725       /* Check if current name is too long, then cut it */
726       if(mb_strlen($name, 'UTF-8')> $max_size){
727         $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
728       }
730       /* Append the name to the list */ 
731       if(isset($entries['ENTRY'])){
732         $a = "";
733         for($i = 0 ; $i < $depth ; $i ++){
734           $a.=".";
735         }
736         $ret[$entries['ENTRY']]=$a."&nbsp;".$name;
737       } 
739       /* recursive add of subdepartments */
740       if(isset($entries['SUB'])){
741         $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
742       }
743     }
745     return($ret);
746   }
748   /* This function returns all available Shares defined in this ldap
749    * There are two ways to call this function, if listboxEntry is true
750    *  only name and path are attached to the array, in it is false, the whole
751    *  entry will be parsed an atached to the result.
752    */
753   function getShareList($listboxEntry = false)
754   {
755     $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","server",get_ou("serverou"),
756         $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
757     $return =array();
758     foreach($tmp as $entry){
760       if(isset($entry['goExportEntry']['count'])){
761         unset($entry['goExportEntry']['count']);
762       }
763       if(isset($entry['goExportEntry'])){
764         foreach($entry['goExportEntry'] as $export){
765           $shareAttrs = split("\|",$export);
766           if($listboxEntry) {
767             $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
768           }else{
769             $return[$shareAttrs[0]."|".$entry['cn'][0]]['server']       = $entry['cn'][0];
770             $return[$shareAttrs[0]."|".$entry['cn'][0]]['name']         = $shareAttrs[0];
771             $return[$shareAttrs[0]."|".$entry['cn'][0]]['description']  = $shareAttrs[1];
772             $return[$shareAttrs[0]."|".$entry['cn'][0]]['type']         = $shareAttrs[2];
773             $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset']      = $shareAttrs[3];
774             $return[$shareAttrs[0]."|".$entry['cn'][0]]['path']         = $shareAttrs[4];
775             $return[$shareAttrs[0]."|".$entry['cn'][0]]['option']       = $shareAttrs[5];
776           }
777         }
778       }
779     }
780     return($return);
781   }
784   /* This function returns all available ShareServer */
785   function getShareServerList()
786   {
787     global $config;
788     $return = array();
789     $base = $config->current['BASE'];
790     $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
791           get_ou("serverou"), $base,array("goExportEntry","cn"),GL_NONE);
793     foreach($res as $entry){
794         if(isset($entry['goExportEntry']['count'])){
795           unset($entry['goExportEntry']['count']);
796         }
797         foreach($entry['goExportEntry'] as $share){
798           $a_share = split("\|",$share);
799           $sharename = $a_share[0];
800           $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
801         }
802     }
803     return($return);
804   }
807   /* Check if there's the specified bool value set in the configuration */
808   function boolValueIsTrue($section, $value)
809   {
810     $section= strtoupper($section);
811     $value= strtoupper($value);
812     if (isset($this->data[$section][$value])){
813     
814       $data= $this->data[$section][$value];
815       if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
816         return TRUE;
817       }
819     }
821     return FALSE;
822   }
825   function __search(&$arr, $name, $return)
826   {
827     $return= strtoupper($return);
828     if (is_array($arr)){
829       foreach ($arr as &$a){
830         if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
831           return(isset($a[$return])?$a[$return]:"");
832         } else {
833           $res= $this->__search ($a, $name, $return);
834           if ($res != ""){
835             return $res;
836           }
837         }
838       }
839     }
840     return ("");
841   }
844   function search($class, $value, $categories= "")
845   {
846     if (is_array($categories)){
847       foreach ($categories as $category){
848         $res= $this->__search($this->data[strtoupper($category)], $class, $value);
849         if ($res != ""){
850           return $res;
851         }
852       }
853     } else {
854       if ($categories == "") {
855         return $this->__search($this->data, $class, $value);
856       } else {
857         return $this->__search($this->data[strtoupper($categories)], $class, $value);
858       }
859     } 
861     return ("");
862   }
865   function check_config_version()
866   {
867     /* Skip check, if we've already mentioned the mismatch 
868      */
869     if(session::is_set("LastChecked") && session::get("LastChecked") == $this->config_version) return;
870   
871     /* Remember last checked version 
872      */
873     session::set("LastChecked",$this->config_version);
875     $current = md5(file_get_contents(CONFIG_TEMPLATE_DIR."/gosa.conf"));
877     /* Check contributed config version and current config version.
878      */
879     if($this->config_version != $current && !empty($this->config_version)){
880       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."));
881     }
882   }
885   /* On debian systems the session files are deleted with
886    *  a cronjob, which detects all files older than specified 
887    *  in php.ini:'session.gc_maxlifetime' and removes them.
888    * This function checks if the gosa.conf value matches the range
889    *  defined by session.gc_maxlifetime.
890    */
891   function check_session_lifetime()
892   {
893     $cfg_lifetime = $this->data['MAIN']['SESSION_LIFETIME'];
894     $ini_lifetime = ini_get('session.gc_maxlifetime');
895     $deb_system   = file_exists('/etc/debian_version');
896     return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));  
897   }
900 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
901 ?>