Code

Updated gosaSupportDaemon
[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= "";
47   /* Keep a copy of the current deparment list */
48   var $departments= array();
49   var $idepartments= array();
50   var $adepartments= array();
51   var $tdepartments= array();
52   var $filename = "";
53   var $last_modified = 0;
55   function config($filename, $basedir= "")
56   {
57     $this->parser = xml_parser_create();
58     $this->basedir= $basedir;
60     xml_set_object($this->parser, $this);
61     xml_set_element_handler($this->parser, "tag_open", "tag_close");
63     /* Parse config file directly? */
64     if ($filename != ""){
65       $this->parse($filename);
66     }
67   }
69   
70   function check_and_reload()
71   {
72     if($this->filename != "" && filemtime($this->filename) != $this->last_modified){
74       $this->config_found= FALSE;
75       $this->tags= array();
76       $this->level= 0;
77       $this->gpc= 0;
78       $this->section= "";
79       $this->currentLocation= "";
81       $this->parser = xml_parser_create();
82       xml_set_object($this->parser, $this);
83       xml_set_element_handler($this->parser, "tag_open", "tag_close");
84       $this->parse($this->filename);
85       if(session::is_set('plist')){
86         session::un_set('plist');
87       }
88       if(session::is_set('plug')){
89         session::un_set('plug');
90       }
91       if(isset($_GET['plug'])){
92         unset($_GET['plug']);
93       }
94     }
95   }  
98   function parse($filename)
99   { 
100     $this->last_modified = filemtime($filename);
101     $this->filename = $filename;
102     $fh= fopen($filename, "r"); 
103     $xmldata= fread($fh, 100000);
104     fclose($fh); 
105     if(!xml_parse($this->parser, chop($xmldata))){
106       $msg = sprintf(_("XML error in gosa.conf: %s at line %d"),
107             xml_error_string(xml_get_error_code($this->parser)),
108             xml_get_current_line_number($this->parser));
109       msg_dialog::display(_("Config file parsing"), $msg, FATAL_ERROR_DIALOG);
110       exit;
111     }
112   }
114   function tag_open($parser, $tag, $attrs)
115   { 
116     /* Save last and current tag for reference */
117     $this->tags[$this->level]= $tag;
118     $this->level++;
120     /* Trigger on CONF section */
121     if ($tag == 'CONF'){
122       $this->config_found= TRUE;
123     }
125     /* Return if we're not in config section */
126     if (!$this->config_found){
127       return;
128     }
130     /* yes/no to true/false and upper case TRUE to true and so on*/
131     foreach($attrs as $name => $value){
132       if(preg_match("/^(true|yes)$/i",$value)){
133         $attrs[$name] = "true";
134       }elseif(preg_match("/^(false|no)$/i",$value)){
135         $attrs[$name] = "false";
136       } 
137     }
139     /* Look through attributes */
140     switch ($this->tags[$this->level-1]){
143       /* Handle tab section */
144       case 'TAB':       $name= $this->tags[$this->level-2];
146                   /* Create new array? */
147                   if (!isset($this->data['TABS'][$name])){
148                     $this->data['TABS'][$name]= array();
149                   }
151                   /* Add elements */
152                   $this->data['TABS'][$name][]= $attrs;
153                   break;
155                   /* Handle location */
156       case 'LOCATION':
157                   if ($this->tags[$this->level-2] == 'MAIN'){
158                     $name= $attrs['NAME'];
159                     $this->currentLocation= $name;
161                     /* Add location elements */
162                       $this->data['LOCATIONS'][$name]= $attrs;
163                     }
164                   break;
166                   /* Handle referral tags */
167       case 'REFERRAL':
168                   if ($this->tags[$this->level-2] == 'LOCATION'){
169                     $url= $attrs['URL'];
170                     $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
172                     /* Add location elements */
173                     if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
174                       $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
175                     }
177                     $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
178                   }
179                   break;
181                   /* Load main parameters */
182       case 'MAIN':
183                   $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
184                   break;
186                   /* Load menu */
187       case 'SECTION':
188                   if ($this->tags[$this->level-2] == 'MENU'){
189                     $this->section= $attrs['NAME'];
190                     $this->data['MENU'][$this->section]= array(); ;
191                   }
192                   break;
194                   /* Inser plugins */
195       case 'PLUGIN':
196                   if ($this->tags[$this->level-3] == 'MENU' &&
197                       $this->tags[$this->level-2] == 'SECTION'){
199                     $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
200                   }
201                   if ($this->tags[$this->level-2] == 'SERVICEMENU'){
202                     $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
203                   }
204                   break;
205     }
206   }
208   function tag_close($parser, $tag)
209   {
210     /* Close config section */
211     if ($tag == 'CONF'){
212       $this->config_found= FALSE;
213     }
214     $this->level--;
215   }
217   function get_ldap_link($sizelimit= FALSE)
218   {
220 # REuse last ldap valid handle again.
221 # DISABLED due to unpredictable results.
224 #    if($this->ldap === NULL || !is_resource($this->ldap->cid)){
226       /* Build new connection */
227       $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
228           $this->current['ADMIN'], $this->current['PASSWORD']);
230       /* Check for connection */
231       if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
232         $smarty= get_smarty();
233         msg_dialog::display(_("LDAP error"), _("Cannot bind to LDAP. Please contact the system administrator."), FATAL_ERROR_DIALOG);
234         exit();
235       }
237       if (!session::is_set('size_limit')){
238         session::set('size_limit',$this->current['SIZELIMIT']);
239         session::set('size_ignore',$this->current['SIZEIGNORE']);
240       }
242       if ($sizelimit){
243         $this->ldap->set_size_limit(session::get('size_limit'));
244       } else {
245         $this->ldap->set_size_limit(0);
246       }
248       /* Move referrals */
249       if (!isset($this->current['REFERRAL'])){
250         $this->ldap->referrals= array();
251       } else {
252         $this->ldap->referrals= $this->current['REFERRAL'];
253       }
254 #    }
255     return ($this->ldap);
256   }
258   function set_current($name)
259   {
260     $this->current= $this->data['LOCATIONS'][$name];
261     if (!isset($this->current['PEOPLE'])){
262       $this->current['PEOPLE']= "ou=people";
263     }
264     if (!isset($this->current['GROUPS'])){
265       $this->current['GROUPS']= "ou=groups";
266     }
268     if (isset($this->current['INITIAL_BASE'])){
269       session::set('CurrentMainBase',$this->current['INITIAL_BASE']);
270     }
271   
272     /* Remove possibly added ',' from end of group and people ou */
273     $this->current['GROUPS'] = preg_replace("/,*$/","",$this->current['GROUPS']);
274     $this->current['PEOPLE'] = preg_replace("/,*$/","",$this->current['PEOPLE']);
276     if (!isset($this->current['WINSTATIONS'])){
277       $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
278     }
279     if (!isset($this->current['HASH'])){
280       $this->current['HASH']= "crypt";
281     }
282     if (!isset($this->current['DNMODE'])){
283       $this->current['DNMODE']= "cn";
284     }
285     if (!isset($this->current['MINID'])){
286       $this->current['MINID']= 100;
287     }
288     if (!isset($this->current['SIZELIMIT'])){
289       $this->current['SIZELIMIT']= 200;
290     }
291     if (!isset($this->current['SIZEINGORE'])){
292       $this->current['SIZEIGNORE']= TRUE;
293     } else {
294       if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
295         $this->current['SIZEIGNORE']= TRUE;
296       } else {
297         $this->current['SIZEIGNORE']= FALSE;
298       }
299     }
301     /* Sort referrals, if present */
302     if (isset ($this->current['REFERRAL'])){
303       $bases= array();
304       $servers= array();
305       foreach ($this->current['REFERRAL'] as $ref){
306         $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
307         $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
308         $bases[$base]= strlen($base);
309         $servers[$base]= $server;
310       }
311       asort($bases);
312       reset($bases);
313     }
315     /* SERVER not defined? Load the one with the shortest base */
316     if (!isset($this->current['SERVER'])){
317       $this->current['SERVER']= $servers[key($bases)];
318     }
320     /* BASE not defined? Load the one with the shortest base */
321     if (!isset($this->current['BASE'])){
322       $this->current['BASE']= key($bases);
323     }
325     /* Convert BASE to have escaped special characters */
326     $this->current['BASE']= @LDAP::convert($this->current['BASE']);
328     /* Parse LDAP referral informations */
329     if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
330       $url= $this->current['SERVER'];
331       $referral= $this->current['REFERRAL'][$url];
332       $this->current['ADMIN']= $referral['ADMIN'];
333       $this->current['PASSWORD']= $referral['PASSWORD'];
334     }
336     /* Load server informations */
337     $this->load_servers();
338   }
340   function load_servers ()
341   {
342     /* Only perform actions if current is set */
343     if ($this->current === NULL){
344       return;
345     }
347     /* Fill imap servers */
348     $ldap= $this->get_ldap_link();
349     $ldap->cd ($this->current['BASE']);
350     if (!isset($this->current['MAILMETHOD'])){
351       $this->current['MAILMETHOD']= "";
352     }
353     if ($this->current['MAILMETHOD'] == ""){
354       $ldap->search ("(objectClass=goMailServer)", array('cn'));
355       $this->data['SERVERS']['IMAP']= array();
356       error_reporting(0);
357       while ($attrs= $ldap->fetch()){
358         $name= $attrs['cn'][0];
359         $this->data['SERVERS']['IMAP'][$name]= $name;
360       }
361       error_reporting(E_ALL);
362     } else {
363       $ldap->search ("(objectClass=goImapServer)", array('goImapName', 'goImapConnect', 'goImapAdmin', 'goImapPassword',
364             'goImapSieveServer', 'goImapSievePort'));
366       $this->data['SERVERS']['IMAP']= array();
367       error_reporting(0);
368       while ($attrs= $ldap->fetch()){
369         $name= $attrs['goImapName'][0];
370         $this->data['SERVERS']['IMAP'][$name]= array( "connect" => $attrs['goImapConnect'][0],
371             "admin" => $attrs['goImapAdmin'][0],
372             "password" => $attrs['goImapPassword'][0],
373             "sieve_server" => $attrs['goImapSieveServer'][0],
374             "sieve_port" => $attrs['goImapSievePort'][0]);
375       }
376       error_reporting(E_ALL);
377     }
379     /* Get kerberos server. FIXME: only one is supported currently */
380     $ldap->cd ($this->current['BASE']);
381     $ldap->search ("(objectClass=goKrbServer)");
382     if ($ldap->count()){
383       $attrs= $ldap->fetch();
384       $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
385           'REALM' => $attrs['goKrbRealm'][0],
386           'ADMIN' => $attrs['goKrbAdmin'][0],
387           'PASSWORD' => $attrs['goKrbPassword'][0]);
388     }
390     /* Get cups server. FIXME: only one is supported currently */
391     $ldap->cd ($this->current['BASE']);
392     $ldap->search ("(objectClass=goCupsServer)");
393     if ($ldap->count()){
394       $attrs= $ldap->fetch();
395       $this->data['SERVERS']['CUPS']= $attrs['cn'][0];  
396     }
398     /* Get fax server. FIXME: only one is supported currently */
399     $ldap->cd ($this->current['BASE']);
400     $ldap->search ("(objectClass=goFaxServer)");
401     if ($ldap->count()){
402       $attrs= $ldap->fetch();
403       $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
404           'LOGIN' => $attrs['goFaxAdmin'][0],
405           'PASSWORD' => $attrs['goFaxPassword'][0]);
406     }
409     /* Get asterisk servers */
410     $ldap->cd ($this->current['BASE']);
411     $ldap->search ("(objectClass=goFonServer)");
412     $this->data['SERVERS']['FON']= array();
413     if ($ldap->count()){
414       while ($attrs= $ldap->fetch()){
416         /* Add 0 entry for development */
417         if(count($this->data['SERVERS']['FON']) == 0){
418           $this->data['SERVERS']['FON'][0]= array(
419               'DN'      => $attrs['dn'],
420               'SERVER'  => $attrs['cn'][0],
421               'LOGIN'   => $attrs['goFonAdmin'][0],
422               'PASSWORD'  => $attrs['goFonPassword'][0],
423               'DB'    => "gophone",
424               'SIP_TABLE'   => "sip_users",
425               'EXT_TABLE'   => "extensions",
426               'VOICE_TABLE' => "voicemail_users",
427               'QUEUE_TABLE' => "queues",
428               'QUEUE_MEMBER_TABLE'  => "queue_members");
429         }
431         /* Add entry with 'dn' as index */
432         $this->data['SERVERS']['FON'][$attrs['dn']]= array(
433             'DN'      => $attrs['dn'],
434             'SERVER'  => $attrs['cn'][0],
435             'LOGIN'   => $attrs['goFonAdmin'][0],
436             'PASSWORD'  => $attrs['goFonPassword'][0],
437             'DB'    => "gophone",
438             'SIP_TABLE'   => "sip_users",
439             'EXT_TABLE'   => "extensions",
440             'VOICE_TABLE' => "voicemail_users",
441             'QUEUE_TABLE' => "queues",
442             'QUEUE_MEMBER_TABLE'  => "queue_members");
443       }
444     }
447     /* Get glpi server */
448     $ldap->cd ($this->current['BASE']);
449     $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
450     if ($ldap->count()){
451       $attrs= $ldap->fetch();
452       if(!isset($attrs['goGlpiPassword'])){
453         $attrs['goGlpiPassword'][0] ="";
454       }
455       $this->data['SERVERS']['GLPI']= array( 
456           'SERVER'      => $attrs['cn'][0],
457           'LOGIN'       => $attrs['goGlpiAdmin'][0],
458           'PASSWORD'    => $attrs['goGlpiPassword'][0],
459           'DB'          => $attrs['goGlpiDatabase'][0]);
460     }
463     /* Get logdb server */
464     $ldap->cd ($this->current['BASE']);
465     $ldap->search ("(objectClass=goLogDBServer)");
466     if ($ldap->count()){
467       $attrs= $ldap->fetch();
468       $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
469           'LOGIN' => $attrs['goLogAdmin'][0],
470           'PASSWORD' => $attrs['goLogPassword'][0]);
471     }
474     /* GOsa logging databases */
475     $ldap->cd ($this->current['BASE']);
476     $ldap->search ("(objectClass=gosaLogServer)");
477     if ($ldap->count()){
478       while($attrs= $ldap->fetch()){
479       $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]= 
480           array(
481           'DN'    => $attrs['dn'],
482           'USER'  => $attrs['goLogDBUser'][0],
483           'DB'    => $attrs['goLogDB'][0],
484           'PWD'   => $attrs['goLogDBPassword'][0]);
485       }
486     }
489     /* Get NFS server lists */
490     $tmp= array("default");
491     $ldap->cd ($this->current['BASE']);
492     $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
493     while ($attrs= $ldap->fetch()){
494       for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
495         if(!preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
496           continue;
497         }
498         $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
499         $tmp[]= $attrs["cn"][0].":$path";
500       }
501     }
502     $this->data['SERVERS']['NFS']= $tmp;
504     /* Load Terminalservers */
505     $ldap->cd ($this->current['BASE']);
506     $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
507     $this->data['SERVERS']['TERMINAL']= array();
508     $this->data['SERVERS']['TERMINAL'][]= "default";
509     $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
512     while ($attrs= $ldap->fetch()){
513       $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
514       if(isset( $attrs["gotoSessionType"]['count'])){
515         for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
516           $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i]; 
517         }
518       }
519     }
521     /* Ldap Server */
522     $this->data['SERVERS']['LDAP']= array();
523     $ldap->cd ($this->current['BASE']);
524     $ldap->search ("(objectClass=goLdapServer)");
525     while ($attrs= $ldap->fetch()){
526       if (isset($attrs["goLdapBase"])){
527         for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
528           $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
529         }
530       }
531     }
533     /* Get misc server lists */
534     $this->data['SERVERS']['SYSLOG']= array("default");
535     $this->data['SERVERS']['NTP']= array("default");
536     $ldap->cd ($this->current['BASE']);
537     $ldap->search ("(objectClass=goNtpServer)");
538     while ($attrs= $ldap->fetch()){
539       $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
540     }
541     $ldap->cd ($this->current['BASE']);
542     $ldap->search ("(objectClass=goSyslogServer)");
543     while ($attrs= $ldap->fetch()){
544       $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
545     }
547     /* Get samba servers from LDAP, in case of samba3 */
548     if ($this->current['SAMBAVERSION'] == 3){
549       $this->data['SERVERS']['SAMBA']= array();
550       $ldap->cd ($this->current['BASE']);
551       $ldap->search ("(objectClass=sambaDomain)");
552       while ($attrs= $ldap->fetch()){
553         $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
554         if(isset($attrs["sambaSID"][0])){
555           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"]  = $attrs["sambaSID"][0];
556         }
557         if(isset($attrs["sambaAlgorithmicRidBase"][0])){
558           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
559         }
560       }
562       /* If no samba servers are found, look for configured sid/ridbase */
563       if (count($this->data['SERVERS']['SAMBA']) == 0){
564         if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
565           msg_dialog::display(_("Configuration error"), _("SID and/or RIDBASE missing in the configuration!"), FATAL_ERROR_DIALOG);
566           exit();
567         } else {
568           $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
569               "SID" => $this->current["SID"],
570               "RIDBASE" => $this->current["RIDBASE"]);
571         }
572       }
573     }
574   }
577   function get_departments($ignore_dn= "")
578   {
579     global $config;
581     /* Initialize result hash */
582     $result= array();
583     $administrative= array();
584     $result['/']= $this->current['BASE'];
585     $this->tdepartments= array();
587     /* Get list of department objects */
588     $ldap= $this->get_ldap_link();
589     $ldap->cd ($this->current['BASE']);
590     $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
591     while ($attrs= $ldap->fetch()){
592       $dn= $ldap->getDN();
593       $this->tdepartments[$dn]= "";
595       /* Save administrative departments */
596       if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
597           isset($attrs['gosaUnitTag'][0])){
598         $administrative[$dn]= $attrs['gosaUnitTag'][0];
599         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
600       }
601     
602       if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
603           isset($attrs['gosaUnitTag'][0])){
604         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
605       }
606     
607       if ($dn == $ignore_dn){
608         continue;
609       }
611       /* Only assign non-root departments */
612       if ($dn != $result['/']){
613         $result[convert_department_dn($dn)]= $dn;
614       }
615     }
617     $this->adepartments= $administrative;
618     $this->departments= $result;
619   }
622   function make_idepartments($max_size= 28)
623   {
624     global $config;
625     $base = $config->current['BASE'];
627     $arr = array();
628     $ui= get_userinfo();
630     $this->idepartments= array();
632     /* Create multidimensional array, with all departments. */
633     foreach ($this->departments as $key => $val){
635       /* When using strict_units, filter non relevant parts */
636       if (isset($config->current['STRICT_UNITS']) && preg_match('/true/i', $config->current['STRICT_UNITS'])){
637         if ($ui->gosaUnitTag != "" && isset($this->tdepartments[$val]) &&
638             $this->tdepartments[$val] != $ui->gosaUnitTag){
639 #          continue;
640         }
641       }
643       /* remove base from dn */
644       $val2 = str_replace($base,"",$val);
646       /* Get every single ou */
647       $str = preg_replace("/ou=/","|ou=",$val2);        
648       $elements = array_reverse(split("\|",$str));              
650       /* Save last array position */
651       $last = &$arr;
653       /* Get array depth  */
654       $cnt = count($elements);
656       /* Add last ou element of current dn to our array */
657       foreach($elements as $key => $ele){
659         /* skip enpty */
660         if(empty($ele)) continue;
662         /* Extract department name */           
663         $elestr = preg_replace("/^ou=/","", $ele);
664         $elestr = preg_replace("/,$/","",$elestr);      
666         /* Add to array */      
667         if($key == ($cnt-2)){
668           $last[$elestr]['ENTRY'] = $val;
669         }
671         /* Set next array appending position */
672         $last = &$last[$elestr]['SUB'];
673       }
674     }
676     /* Add base entry */
677     $ret["/"]["ENTRY"]  = $base;
678     $ret["/"]["SUB"]    = $arr;
680     $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
681   }
684   /* Creates display friendly output from make_idepartments */
685   function generateDepartmentArray($arr,$depth = -1,$max_size){
686     $ret = array();
687     $depth ++;
689     /* Walk through array */    
690     ksort($arr);
691     foreach($arr as $name => $entries){
693       /* If this department is the last in the current tree position 
694        * remove it, to avoid generating output for it */
695       if(count($entries['SUB'])==0){
696         unset($entries['SUB']);
697       }
699       /* Fix name, if it contains a replace tag */
700       $name= @LDAP::fix($name);
702       /* Check if current name is too long, then cut it */
703       if(mb_strlen($name, 'UTF-8')> $max_size){
704         $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
705       }
707       /* Append the name to the list */ 
708       if(isset($entries['ENTRY'])){
709         $a = "";
710         for($i = 0 ; $i < $depth ; $i ++){
711           $a.=".";
712         }
713         $ret[$entries['ENTRY']]=$a."&nbsp;".$name;
714       } 
716       /* recursive add of subdepartments */
717       if(isset($entries['SUB'])){
718         $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
719       }
720     }
722     return($ret);
723   }
725   /* This function returns all available Shares defined in this ldap
726    * There are two ways to call this function, if listboxEntry is true
727    *  only name and path are attached to the array, in it is false, the whole
728    *  entry will be parsed an atached to the result.
729    */
730   function getShareList($listboxEntry = false)
731   {
732     $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","",get_ou("serverou"),
733         $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
734     $return =array();
735     foreach($tmp as $entry){
737       if(isset($entry['goExportEntry']['count'])){
738         unset($entry['goExportEntry']['count']);
739       }
740       if(isset($entry['goExportEntry'])){
741         foreach($entry['goExportEntry'] as $export){
742           $shareAttrs = split("\|",$export);
743           if($listboxEntry) {
744             $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
745           }else{
746             $return[$shareAttrs[0]."|".$entry['cn'][0]]['server']       = $entry['cn'][0];
747             $return[$shareAttrs[0]."|".$entry['cn'][0]]['name']         = $shareAttrs[0];
748             $return[$shareAttrs[0]."|".$entry['cn'][0]]['description']  = $shareAttrs[1];
749             $return[$shareAttrs[0]."|".$entry['cn'][0]]['type']         = $shareAttrs[2];
750             $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset']      = $shareAttrs[3];
751             $return[$shareAttrs[0]."|".$entry['cn'][0]]['path']         = $shareAttrs[4];
752             $return[$shareAttrs[0]."|".$entry['cn'][0]]['option']       = $shareAttrs[5];
753           }
754         }
755       }
756     }
757     return($return);
758   }
761   /* This function returns all available ShareServer */
762   function getShareServerList()
763   {
764     global $config;
765     $return = array();
766     $base = $config->current['BASE'];
767     $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
768           get_ou("serverou"), $base,array("goExportEntry","cn"),GL_NONE);
770     foreach($res as $entry){
771         if(isset($entry['goExportEntry']['count'])){
772           unset($entry['goExportEntry']['count']);
773         }
774         foreach($entry['goExportEntry'] as $share){
775           $a_share = split("\|",$share);
776           $sharename = $a_share[0];
777           $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
778         }
779     }
780     return($return);
781   }
784   /* Check if there's the specified bool value set in the configuration */
785   function boolValueIsTrue($section, $value)
786   {
787     $section= strtoupper($section);
788     $value= strtoupper($value);
789     if (isset($this->data[$section][$value])){
790     
791       $data= $this->data[$section][$value];
792       if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
793         return TRUE;
794       }
796     }
798     return FALSE;
799   }
802   function __search(&$arr, $name, $return)
803   {
804     $return= strtoupper($return);
805     if (is_array($arr)){
806       foreach ($arr as &$a){
807         if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
808           return(isset($a[$return])?$a[$return]:"");
809         } else {
810           $res= $this->__search ($a, $name, $return);
811           if ($res != ""){
812             return $res;
813           }
814         }
815       }
816     }
817     return ("");
818   }
821   function search($class, $value, $categories= "")
822   {
823     if (is_array($categories)){
824       foreach ($categories as $category){
825         $res= $this->__search($this->data[strtoupper($category)], $class, $value);
826         if ($res != ""){
827           return $res;
828         }
829       }
830     } else {
831       if ($categories == "") {
832         return $this->__search($this->data, $class, $value);
833       } else {
834         return $this->__search($this->data[strtoupper($categories)], $class, $value);
835       }
836     } 
838     return ("");
839   }
842   /* On debian systems the session files are deleted with
843    *  a cronjob, which detects all files older than specified 
844    *  in php.ini:'session.gc_maxlifetime' and removes them.
845    * This function checks if the gosa.conf value matches the range
846    *  defined by session.gc_maxlifetime.
847    */
848   function check_session_lifetime()
849   {
850     $cfg_lifetime = $this->data['MAIN']['SESSION_LIFETIME'];
851     $ini_lifetime = ini_get('session.gc_maxlifetime');
852     $deb_system   = file_exists('/etc/debian_version');
853     return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));  
854   }
857 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
858 ?>