Code

Added DN attribute to config -> SERVERS -> IMAP to be able to check acls later.
[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             "server_dn"   => $attrs['dn'],
404             "connect"     => $imap_connect,
405             "admin"       => $imap_admin,
406             "password"    => $pwd,
407             "sieve_server"=> $sieve_server,
408             "sieve_option"=> $sieve_option,
409             "sieve_port"  => $sieve_port);
410       }
411     }
413     /* Get kerberos server. FIXME: only one is supported currently */
414     $ldap->cd ($this->current['BASE']);
415     $ldap->search ("(&(goKrbRealm=*)(goKrbAdmin=*)(objectClass=goKrbServer))");
416     if ($ldap->count()){
417       $attrs= $ldap->fetch();
418       $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
419           'REALM' => $attrs['goKrbRealm'][0],
420           'ADMIN' => $attrs['goKrbAdmin'][0]);
421     }
423     /* Get cups server. FIXME: only one is supported currently */
424     $ldap->cd ($this->current['BASE']);
425     $ldap->search ("(objectClass=goCupsServer)");
426     if ($ldap->count()){
427       $attrs= $ldap->fetch();
428       $this->data['SERVERS']['CUPS']= $attrs['cn'][0];  
429     }
431     /* Get fax server. FIXME: only one is supported currently */
432     $ldap->cd ($this->current['BASE']);
433     $ldap->search ("(objectClass=goFaxServer)");
434     if ($ldap->count()){
435       $attrs= $ldap->fetch();
436       $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
437           'LOGIN' => $attrs['goFaxAdmin'][0],
438           'PASSWORD' => $attrs['goFaxPassword'][0]);
439     }
442     /* Get asterisk servers */
443     $ldap->cd ($this->current['BASE']);
444     $ldap->search ("(objectClass=goFonServer)");
445     $this->data['SERVERS']['FON']= array();
446     if ($ldap->count()){
447       while ($attrs= $ldap->fetch()){
449         /* Add 0 entry for development */
450         if(count($this->data['SERVERS']['FON']) == 0){
451           $this->data['SERVERS']['FON'][0]= array(
452               'DN'      => $attrs['dn'],
453               'SERVER'  => $attrs['cn'][0],
454               'LOGIN'   => $attrs['goFonAdmin'][0],
455               'PASSWORD'  => $attrs['goFonPassword'][0],
456               'DB'    => "gophone",
457               'SIP_TABLE'   => "sip_users",
458               'EXT_TABLE'   => "extensions",
459               'VOICE_TABLE' => "voicemail_users",
460               'QUEUE_TABLE' => "queues",
461               'QUEUE_MEMBER_TABLE'  => "queue_members");
462         }
464         /* Add entry with 'dn' as index */
465         $this->data['SERVERS']['FON'][$attrs['dn']]= array(
466             'DN'      => $attrs['dn'],
467             'SERVER'  => $attrs['cn'][0],
468             'LOGIN'   => $attrs['goFonAdmin'][0],
469             'PASSWORD'  => $attrs['goFonPassword'][0],
470             'DB'    => "gophone",
471             'SIP_TABLE'   => "sip_users",
472             'EXT_TABLE'   => "extensions",
473             'VOICE_TABLE' => "voicemail_users",
474             'QUEUE_TABLE' => "queues",
475             'QUEUE_MEMBER_TABLE'  => "queue_members");
476       }
477     }
480     /* Get glpi server */
481     $ldap->cd ($this->current['BASE']);
482     $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
483     if ($ldap->count()){
484       $attrs= $ldap->fetch();
485       if(!isset($attrs['goGlpiPassword'])){
486         $attrs['goGlpiPassword'][0] ="";
487       }
488       $this->data['SERVERS']['GLPI']= array( 
489           'SERVER'      => $attrs['cn'][0],
490           'LOGIN'       => $attrs['goGlpiAdmin'][0],
491           'PASSWORD'    => $attrs['goGlpiPassword'][0],
492           'DB'          => $attrs['goGlpiDatabase'][0]);
493     }
496     /* Get logdb server */
497     $ldap->cd ($this->current['BASE']);
498     $ldap->search ("(objectClass=goLogDBServer)");
499     if ($ldap->count()){
500       $attrs= $ldap->fetch();
501       $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
502           'LOGIN' => $attrs['goLogAdmin'][0],
503           'PASSWORD' => $attrs['goLogPassword'][0]);
504     }
507     /* GOsa logging databases */
508     $ldap->cd ($this->current['BASE']);
509     $ldap->search ("(objectClass=gosaLogServer)");
510     if ($ldap->count()){
511       while($attrs= $ldap->fetch()){
512       $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]= 
513           array(
514           'DN'    => $attrs['dn'],
515           'USER'  => $attrs['goLogDBUser'][0],
516           'DB'    => $attrs['goLogDB'][0],
517           'PWD'   => $attrs['goLogDBPassword'][0]);
518       }
519     }
522     /* Get NFS server lists */
523     $tmp= array("default");
524     $ldap->cd ($this->current['BASE']);
525     $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
526     while ($attrs= $ldap->fetch()){
527       for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
528         if(!preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
529           continue;
530         }
531         $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
532         $tmp[]= $attrs["cn"][0].":$path";
533       }
534     }
535     $this->data['SERVERS']['NFS']= $tmp;
537     /* Load Terminalservers */
538     $ldap->cd ($this->current['BASE']);
539     $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
540     $this->data['SERVERS']['TERMINAL']= array();
541     $this->data['SERVERS']['TERMINAL'][]= "default";
542     $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
545     while ($attrs= $ldap->fetch()){
546       $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
547       if(isset( $attrs["gotoSessionType"]['count'])){
548         for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
549           $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i]; 
550         }
551       }
552     }
554     /* Ldap Server */
555     $this->data['SERVERS']['LDAP']= array();
556     $ldap->cd ($this->current['BASE']);
557     $ldap->search ("(objectClass=goLdapServer)");
558     while ($attrs= $ldap->fetch()){
559       if (isset($attrs["goLdapBase"])){
560         for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
561           $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
562         }
563       }
564     }
566     /* Get misc server lists */
567     $this->data['SERVERS']['SYSLOG']= array("default");
568     $this->data['SERVERS']['NTP']= array("default");
569     $ldap->cd ($this->current['BASE']);
570     $ldap->search ("(objectClass=goNtpServer)");
571     while ($attrs= $ldap->fetch()){
572       $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
573     }
574     $ldap->cd ($this->current['BASE']);
575     $ldap->search ("(objectClass=goSyslogServer)");
576     while ($attrs= $ldap->fetch()){
577       $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
578     }
580     /* Get samba servers from LDAP, in case of samba3 */
581     if ($this->current['SAMBAVERSION'] == 3){
582       $this->data['SERVERS']['SAMBA']= array();
583       $ldap->cd ($this->current['BASE']);
584       $ldap->search ("(objectClass=sambaDomain)");
585       while ($attrs= $ldap->fetch()){
586         $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
587         if(isset($attrs["sambaSID"][0])){
588           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"]  = $attrs["sambaSID"][0];
589         }
590         if(isset($attrs["sambaAlgorithmicRidBase"][0])){
591           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
592         }
593       }
595       /* If no samba servers are found, look for configured sid/ridbase */
596       if (count($this->data['SERVERS']['SAMBA']) == 0){
597         if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
598           msg_dialog::display(_("Configuration error"), _("SID and/or RIDBASE missing in the configuration!"), FATAL_ERROR_DIALOG);
599           exit();
600         } else {
601           $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
602               "SID" => $this->current["SID"],
603               "RIDBASE" => $this->current["RIDBASE"]);
604         }
605       }
606     }
607   }
610   function get_departments($ignore_dn= "")
611   {
612     global $config;
614     /* Initialize result hash */
615     $result= array();
616     $administrative= array();
617     $result['/']= $this->current['BASE'];
618     $this->tdepartments= array();
620     /* Get all department types from department Management, to be able detect the department type.
621         -It is possible that differnty department types have the same name, 
622          in this case we have to mark the department name to be able to differentiate.
623           (e.g l=Name  or   o=Name)
624      */    
625     $types = departmentManagement::get_support_departments();
626     
627     /* Create a list of attributes to fetch */
628     $ldap_values = array("objectClass","gosaUnitTag");
629     $filter = "";
630     foreach($types as $type){
631       $ldap_values[] = $type['ATTR'];
632       $filter .= "(objectClass=".$type['OC'].")";
633     }
634     $filter = "(&(objectClass=gosaDepartment)(|".$filter."))";
636     /* Get list of department objects */
637     $ldap= $this->get_ldap_link();
638     $ldap->cd ($this->current['BASE']);
639     $ldap->search ($filter, $ldap_values);
640     while ($attrs= $ldap->fetch()){
642       /* Detect department type */
643       $type_data = array();
644       foreach($types as $t => $data){
645         if(in_array($data['OC'],$attrs['objectClass'])){
646           $type_data = $data;
647           break;    
648         }
649       }
651       /* Unknown department type -> skip 
652        */
653       if(!count($type_data)) continue;
655       $dn= $ldap->getDN();
656       $this->tdepartments[$dn]= "";
658       /* Save administrative departments */
659       if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
660           isset($attrs['gosaUnitTag'][0])){
661         $administrative[$dn]= $attrs['gosaUnitTag'][0];
662         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
663       }
664     
665       if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
666           isset($attrs['gosaUnitTag'][0])){
667         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
668       }
669     
670       if ($dn == $ignore_dn){
671         continue;
672       }
674       $c_dn = convert_department_dn($dn)." (".$type_data['ATTR'].")";
676       /* Only assign non-root departments */
677       if ($dn != $result['/']){
678         $result[$c_dn]= $dn;
679       }
680     }
682     $this->adepartments= $administrative;
683     $this->departments= $result;
684   }
687   function make_idepartments($max_size= 28)
688   {
689     global $config;
690     $base = $config->current['BASE'];
692     $arr = array();
693     $ui= get_userinfo();
695     $this->idepartments= array();
697     /* Create multidimensional array, with all departments. */
698     foreach ($this->departments as $key => $val){
700       /* When using strict_units, filter non relevant parts */
701       if (isset($config->current['STRICT_UNITS']) && preg_match('/true/i', $config->current['STRICT_UNITS'])){
702         if ($ui->gosaUnitTag != "" && isset($this->tdepartments[$val]) &&
703             $this->tdepartments[$val] != $ui->gosaUnitTag){
704 #          continue;
705         }
706       }
708       /* Split dn into single department pieces.
709        */
710       $elements = array_reverse(split(",",preg_replace("/".normalizePreg($base)."$/","",$val)));                
712       /* Add last ou element of current dn to our array */
713       $last = &$arr;
714       foreach($elements as $key => $ele){
716         /* skip empty */
717         if(empty($ele)) continue;
719         /* Extract department name */           
720         $elestr = trim(preg_replace("/^[^=]*+=/","", $ele),",");
721         $nameA  = trim(preg_replace("/=.*$/","", $ele),",");
722         if($nameA != "ou"){
723           $nameA = " (".$nameA.")";
724         }else{
725           $nameA = "";
726         }
727     
729         /* Add to array */      
730         if($key == (count($elements)-1)){
731           $last[$elestr.$nameA]['ENTRY'] = $val;
732         }
734         /* Set next array appending position */
735         $last = &$last[$elestr.$nameA]['SUB'];
736       }
737     }
740     /* Add base entry */
741     $ret["/"]["ENTRY"]  = $base;
742     $ret["/"]["SUB"]    = $arr;
743     $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
744   }
747   /* Creates display friendly output from make_idepartments */
748   function generateDepartmentArray($arr,$depth = -1,$max_size)
749   {
750     $ret = array();
751     $depth ++;
753     /* Walk through array */    
754     ksort($arr);
755     foreach($arr as $name => $entries){
757       /* If this department is the last in the current tree position 
758        * remove it, to avoid generating output for it */
759       if(count($entries['SUB'])==0){
760         unset($entries['SUB']);
761       }
763       /* Fix name, if it contains a replace tag */
764       $name= @LDAP::fix($name);
766       /* Check if current name is too long, then cut it */
767       if(mb_strlen($name, 'UTF-8')> $max_size){
768         $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
769       }
771       /* Append the name to the list */ 
772       if(isset($entries['ENTRY'])){
773         $a = "";
774         for($i = 0 ; $i < $depth ; $i ++){
775           $a.=".";
776         }
777         $ret[$entries['ENTRY']]=$a."&nbsp;".$name;
778       } 
780       /* recursive add of subdepartments */
781       if(isset($entries['SUB'])){
782         $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
783       }
784     }
786     return($ret);
787   }
789   /* This function returns all available Shares defined in this ldap
790    * There are two ways to call this function, if listboxEntry is true
791    *  only name and path are attached to the array, in it is false, the whole
792    *  entry will be parsed an atached to the result.
793    */
794   function getShareList($listboxEntry = false)
795   {
796     $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","server",get_ou("serverou"),
797         $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
798     $return =array();
799     foreach($tmp as $entry){
801       if(isset($entry['goExportEntry']['count'])){
802         unset($entry['goExportEntry']['count']);
803       }
804       if(isset($entry['goExportEntry'])){
805         foreach($entry['goExportEntry'] as $export){
806           $shareAttrs = split("\|",$export);
807           if($listboxEntry) {
808             $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
809           }else{
810             $return[$shareAttrs[0]."|".$entry['cn'][0]]['server']       = $entry['cn'][0];
811             $return[$shareAttrs[0]."|".$entry['cn'][0]]['name']         = $shareAttrs[0];
812             $return[$shareAttrs[0]."|".$entry['cn'][0]]['description']  = $shareAttrs[1];
813             $return[$shareAttrs[0]."|".$entry['cn'][0]]['type']         = $shareAttrs[2];
814             $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset']      = $shareAttrs[3];
815             $return[$shareAttrs[0]."|".$entry['cn'][0]]['path']         = $shareAttrs[4];
816             $return[$shareAttrs[0]."|".$entry['cn'][0]]['option']       = $shareAttrs[5];
817           }
818         }
819       }
820     }
821     return($return);
822   }
825   /* This function returns all available ShareServer */
826   function getShareServerList()
827   {
828     global $config;
829     $return = array();
830     $ui = get_userinfo();
831     $base = $config->current['BASE'];
832     $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
833           get_ou("serverou"), $base,array("goExportEntry","cn"),GL_NONE | GL_NO_ACL_CHECK);
835     foreach($res as $entry){
836         
837         $acl = $ui->get_permissions($entry['dn'],"server/goShareServer","");
838         if(isset($entry['goExportEntry']['count'])){
839           unset($entry['goExportEntry']['count']);
840         }
841         foreach($entry['goExportEntry'] as $share){
842           $a_share = split("\|",$share);
843           $sharename = $a_share[0];
844           $data= array();
845           $data['NAME']   = $sharename;
846           $data['ACL']    = $acl;
847           $data['SERVER'] = $entry['cn']['0'];
848           $data['SHARE']  = $sharename;
849           $data['DISPLAY']= $entry['cn'][0]." [".$sharename."]";
850           $return[$entry['cn'][0]."|".$sharename] = $data;
851         }
852     }
853     return($return);
854   }
857   /* Check if there's the specified bool value set in the configuration */
858   function boolValueIsTrue($section, $value)
859   {
860     $section= strtoupper($section);
861     $value= strtoupper($value);
862     if (isset($this->data[$section][$value])){
863     
864       $data= $this->data[$section][$value];
865       if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
866         return TRUE;
867       }
869     }
871     return FALSE;
872   }
875   function __search(&$arr, $name, $return)
876   {
877     $return= strtoupper($return);
878     if (is_array($arr)){
879       foreach ($arr as &$a){
880         if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
881           return(isset($a[$return])?$a[$return]:"");
882         } else {
883           $res= $this->__search ($a, $name, $return);
884           if ($res != ""){
885             return $res;
886           }
887         }
888       }
889     }
890     return ("");
891   }
894   function search($class, $value, $categories= "")
895   {
896     if (is_array($categories)){
897       foreach ($categories as $category){
898         $res= $this->__search($this->data[strtoupper($category)], $class, $value);
899         if ($res != ""){
900           return $res;
901         }
902       }
903     } else {
904       if ($categories == "") {
905         return $this->__search($this->data, $class, $value);
906       } else {
907         return $this->__search($this->data[strtoupper($categories)], $class, $value);
908       }
909     } 
911     return ("");
912   }
915   function check_config_version()
916   {
917     /* Skip check, if we've already mentioned the mismatch 
918      */
919     if(session::is_set("LastChecked") && session::get("LastChecked") == $this->config_version) return;
920   
921     /* Remember last checked version 
922      */
923     session::set("LastChecked",$this->config_version);
925     $current = md5(file_get_contents(CONFIG_TEMPLATE_DIR."/gosa.conf"));
927     /* Check contributed config version and current config version.
928      */
929     if($this->config_version != $current && !empty($this->config_version)){
930       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."));
931     }
932   }
935   /* On debian systems the session files are deleted with
936    *  a cronjob, which detects all files older than specified 
937    *  in php.ini:'session.gc_maxlifetime' and removes them.
938    * This function checks if the gosa.conf value matches the range
939    *  defined by session.gc_maxlifetime.
940    */
941   function check_session_lifetime()
942   {
943     $cfg_lifetime = $this->data['MAIN']['SESSION_LIFETIME'];
944     $ini_lifetime = ini_get('session.gc_maxlifetime');
945     $deb_system   = file_exists('/etc/debian_version');
946     return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));  
947   }
950 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
951 ?>