Code

d5f68214266fc1497d9b34c2041c5534e833c108
[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(_("Configuration error"), $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   }
218   function get_credentials($creds)
219   {
220     if (isset($_SERVER['HTTP_GOSA_KEY'])){
221       return (cred_decrypt($creds, $_SERVER['HTTP_GOSA_KEY']));
222     }
223     return ($creds);
224   }
227   function get_ldap_link($sizelimit= FALSE)
228   {
229     if($this->ldap === NULL || !is_resource($this->ldap->cid)){
231       /* Build new connection */
232       $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
233           $this->current['ADMIN'], $this->get_credentials($this->current['PASSWORD']));
235       /* Check for connection */
236       if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
237         $smarty= get_smarty();
238         msg_dialog::display(_("LDAP error"), _("Cannot bind to LDAP. Please contact the system administrator."), FATAL_ERROR_DIALOG);
239         exit();
240       }
242       if (!session::is_set('size_limit')){
243         session::set('size_limit',$this->current['SIZELIMIT']);
244         session::set('size_ignore',$this->current['SIZEIGNORE']);
245       }
247       if ($sizelimit){
248         $this->ldap->set_size_limit(session::get('size_limit'));
249       } else {
250         $this->ldap->set_size_limit(0);
251       }
253       /* Move referrals */
254       if (!isset($this->current['REFERRAL'])){
255         $this->ldap->referrals= array();
256       } else {
257         $this->ldap->referrals= $this->current['REFERRAL'];
258       }
259     }
261     return new ldapMultiplexer($this->ldap);
262   }
264   function set_current($name)
265   {
266     $this->current= $this->data['LOCATIONS'][$name];
267     if (!isset($this->current['PEOPLE'])){
268       $this->current['PEOPLE']= "ou=people";
269     }
270     if (!isset($this->current['GROUPS'])){
271       $this->current['GROUPS']= "ou=groups";
272     }
274     if (isset($this->current['INITIAL_BASE'])){
275       session::set('CurrentMainBase',$this->current['INITIAL_BASE']);
276     }
277   
278     /* Remove possibly added ',' from end of group and people ou */
279     $this->current['GROUPS'] = preg_replace("/,*$/","",$this->current['GROUPS']);
280     $this->current['PEOPLE'] = preg_replace("/,*$/","",$this->current['PEOPLE']);
282     if (!isset($this->current['WINSTATIONS'])){
283       $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
284     }
285     if (!isset($this->current['HASH'])){
286       $this->current['HASH']= "crypt";
287     }
288     if (!isset($this->current['DNMODE'])){
289       $this->current['DNMODE']= "cn";
290     }
291     if (!isset($this->current['MINID'])){
292       $this->current['MINID']= 100;
293     }
294     if (!isset($this->current['SIZELIMIT'])){
295       $this->current['SIZELIMIT']= 200;
296     }
297     if (!isset($this->current['SIZEINGORE'])){
298       $this->current['SIZEIGNORE']= TRUE;
299     } else {
300       if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
301         $this->current['SIZEIGNORE']= TRUE;
302       } else {
303         $this->current['SIZEIGNORE']= FALSE;
304       }
305     }
307     /* Sort referrals, if present */
308     if (isset ($this->current['REFERRAL'])){
309       $bases= array();
310       $servers= array();
311       foreach ($this->current['REFERRAL'] as $ref){
312         $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
313         $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
314         $bases[$base]= strlen($base);
315         $servers[$base]= $server;
316       }
317       asort($bases);
318       reset($bases);
319     }
321     /* SERVER not defined? Load the one with the shortest base */
322     if (!isset($this->current['SERVER'])){
323       $this->current['SERVER']= $servers[key($bases)];
324     }
326     /* BASE not defined? Load the one with the shortest base */
327     if (!isset($this->current['BASE'])){
328       $this->current['BASE']= key($bases);
329     }
331     /* Convert BASE to have escaped special characters */
332     $this->current['BASE']= @LDAP::convert($this->current['BASE']);
334     /* Parse LDAP referral informations */
335     if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
336       $url= $this->current['SERVER'];
337       $referral= $this->current['REFERRAL'][$url];
338       $this->current['ADMIN']= $referral['ADMIN'];
339       $this->current['PASSWORD']= $referral['PASSWORD'];
340     }
342     /* Load server informations */
343     $this->load_servers();
344   }
346   function load_servers ()
347   {
348     /* Only perform actions if current is set */
349     if ($this->current === NULL){
350       return;
351     }
353     /* Fill imap servers */
354     $ldap= $this->get_ldap_link();
355     $ldap->cd ($this->current['BASE']);
356     if (!isset($this->current['MAILMETHOD'])){
357       $this->current['MAILMETHOD']= "";
358     }
359     if ($this->current['MAILMETHOD'] == ""){
360       $ldap->search ("(objectClass=goMailServer)", array('cn'));
361       $this->data['SERVERS']['IMAP']= array();
362       error_reporting(0);
363       while ($attrs= $ldap->fetch()){
364         $name= $attrs['cn'][0];
365         $this->data['SERVERS']['IMAP'][$name]= $name;
366       }
367       error_reporting(E_ALL);
368     } else {
369       $ldap->search ("(objectClass=goImapServer)", array('goImapName', 'goImapConnect', 'goImapAdmin', 'goImapPassword',
370             'goImapSieveServer', 'goImapSievePort'));
372       $this->data['SERVERS']['IMAP']= array();
373       error_reporting(0);
374       while ($attrs= $ldap->fetch()){
375         $name= $attrs['goImapName'][0];
376         $this->data['SERVERS']['IMAP'][$name]= array( "connect" => $attrs['goImapConnect'][0],
377             "admin" => $attrs['goImapAdmin'][0],
378             "password" => $attrs['goImapPassword'][0],
379             "sieve_server" => $attrs['goImapSieveServer'][0],
380             "sieve_port" => $attrs['goImapSievePort'][0]);
381       }
382       error_reporting(E_ALL);
383     }
385     /* Get kerberos server. FIXME: only one is supported currently */
386     $ldap->cd ($this->current['BASE']);
387     $ldap->search ("(objectClass=goKrbServer)");
388     if ($ldap->count()){
389       $attrs= $ldap->fetch();
390       $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
391           'REALM' => $attrs['goKrbRealm'][0],
392           'ADMIN' => $attrs['goKrbAdmin'][0]);
393     }
395     /* Get cups server. FIXME: only one is supported currently */
396     $ldap->cd ($this->current['BASE']);
397     $ldap->search ("(objectClass=goCupsServer)");
398     if ($ldap->count()){
399       $attrs= $ldap->fetch();
400       $this->data['SERVERS']['CUPS']= $attrs['cn'][0];  
401     }
403     /* Get fax server. FIXME: only one is supported currently */
404     $ldap->cd ($this->current['BASE']);
405     $ldap->search ("(objectClass=goFaxServer)");
406     if ($ldap->count()){
407       $attrs= $ldap->fetch();
408       $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
409           'LOGIN' => $attrs['goFaxAdmin'][0],
410           'PASSWORD' => $attrs['goFaxPassword'][0]);
411     }
414     /* Get asterisk servers */
415     $ldap->cd ($this->current['BASE']);
416     $ldap->search ("(objectClass=goFonServer)");
417     $this->data['SERVERS']['FON']= array();
418     if ($ldap->count()){
419       while ($attrs= $ldap->fetch()){
421         /* Add 0 entry for development */
422         if(count($this->data['SERVERS']['FON']) == 0){
423           $this->data['SERVERS']['FON'][0]= array(
424               'DN'      => $attrs['dn'],
425               'SERVER'  => $attrs['cn'][0],
426               'LOGIN'   => $attrs['goFonAdmin'][0],
427               'PASSWORD'  => $attrs['goFonPassword'][0],
428               'DB'    => "gophone",
429               'SIP_TABLE'   => "sip_users",
430               'EXT_TABLE'   => "extensions",
431               'VOICE_TABLE' => "voicemail_users",
432               'QUEUE_TABLE' => "queues",
433               'QUEUE_MEMBER_TABLE'  => "queue_members");
434         }
436         /* Add entry with 'dn' as index */
437         $this->data['SERVERS']['FON'][$attrs['dn']]= array(
438             'DN'      => $attrs['dn'],
439             'SERVER'  => $attrs['cn'][0],
440             'LOGIN'   => $attrs['goFonAdmin'][0],
441             'PASSWORD'  => $attrs['goFonPassword'][0],
442             'DB'    => "gophone",
443             'SIP_TABLE'   => "sip_users",
444             'EXT_TABLE'   => "extensions",
445             'VOICE_TABLE' => "voicemail_users",
446             'QUEUE_TABLE' => "queues",
447             'QUEUE_MEMBER_TABLE'  => "queue_members");
448       }
449     }
452     /* Get glpi server */
453     $ldap->cd ($this->current['BASE']);
454     $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
455     if ($ldap->count()){
456       $attrs= $ldap->fetch();
457       if(!isset($attrs['goGlpiPassword'])){
458         $attrs['goGlpiPassword'][0] ="";
459       }
460       $this->data['SERVERS']['GLPI']= array( 
461           'SERVER'      => $attrs['cn'][0],
462           'LOGIN'       => $attrs['goGlpiAdmin'][0],
463           'PASSWORD'    => $attrs['goGlpiPassword'][0],
464           'DB'          => $attrs['goGlpiDatabase'][0]);
465     }
468     /* Get logdb server */
469     $ldap->cd ($this->current['BASE']);
470     $ldap->search ("(objectClass=goLogDBServer)");
471     if ($ldap->count()){
472       $attrs= $ldap->fetch();
473       $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
474           'LOGIN' => $attrs['goLogAdmin'][0],
475           'PASSWORD' => $attrs['goLogPassword'][0]);
476     }
479     /* GOsa logging databases */
480     $ldap->cd ($this->current['BASE']);
481     $ldap->search ("(objectClass=gosaLogServer)");
482     if ($ldap->count()){
483       while($attrs= $ldap->fetch()){
484       $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]= 
485           array(
486           'DN'    => $attrs['dn'],
487           'USER'  => $attrs['goLogDBUser'][0],
488           'DB'    => $attrs['goLogDB'][0],
489           'PWD'   => $attrs['goLogDBPassword'][0]);
490       }
491     }
494     /* Get NFS server lists */
495     $tmp= array("default");
496     $ldap->cd ($this->current['BASE']);
497     $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
498     while ($attrs= $ldap->fetch()){
499       for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
500         if(!preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
501           continue;
502         }
503         $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
504         $tmp[]= $attrs["cn"][0].":$path";
505       }
506     }
507     $this->data['SERVERS']['NFS']= $tmp;
509     /* Load Terminalservers */
510     $ldap->cd ($this->current['BASE']);
511     $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
512     $this->data['SERVERS']['TERMINAL']= array();
513     $this->data['SERVERS']['TERMINAL'][]= "default";
514     $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
517     while ($attrs= $ldap->fetch()){
518       $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
519       if(isset( $attrs["gotoSessionType"]['count'])){
520         for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
521           $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i]; 
522         }
523       }
524     }
526     /* Ldap Server */
527     $this->data['SERVERS']['LDAP']= array();
528     $ldap->cd ($this->current['BASE']);
529     $ldap->search ("(objectClass=goLdapServer)");
530     while ($attrs= $ldap->fetch()){
531       if (isset($attrs["goLdapBase"])){
532         for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
533           $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
534         }
535       }
536     }
538     /* Get misc server lists */
539     $this->data['SERVERS']['SYSLOG']= array("default");
540     $this->data['SERVERS']['NTP']= array("default");
541     $ldap->cd ($this->current['BASE']);
542     $ldap->search ("(objectClass=goNtpServer)");
543     while ($attrs= $ldap->fetch()){
544       $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
545     }
546     $ldap->cd ($this->current['BASE']);
547     $ldap->search ("(objectClass=goSyslogServer)");
548     while ($attrs= $ldap->fetch()){
549       $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
550     }
552     /* Get samba servers from LDAP, in case of samba3 */
553     if ($this->current['SAMBAVERSION'] == 3){
554       $this->data['SERVERS']['SAMBA']= array();
555       $ldap->cd ($this->current['BASE']);
556       $ldap->search ("(objectClass=sambaDomain)");
557       while ($attrs= $ldap->fetch()){
558         $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
559         if(isset($attrs["sambaSID"][0])){
560           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"]  = $attrs["sambaSID"][0];
561         }
562         if(isset($attrs["sambaAlgorithmicRidBase"][0])){
563           $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
564         }
565       }
567       /* If no samba servers are found, look for configured sid/ridbase */
568       if (count($this->data['SERVERS']['SAMBA']) == 0){
569         if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
570           msg_dialog::display(_("Configuration error"), _("SID and/or RIDBASE missing in the configuration!"), FATAL_ERROR_DIALOG);
571           exit();
572         } else {
573           $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
574               "SID" => $this->current["SID"],
575               "RIDBASE" => $this->current["RIDBASE"]);
576         }
577       }
578     }
579   }
582   function get_departments($ignore_dn= "")
583   {
584     global $config;
586     /* Initialize result hash */
587     $result= array();
588     $administrative= array();
589     $result['/']= $this->current['BASE'];
590     $this->tdepartments= array();
592     /* Get list of department objects */
593     $ldap= $this->get_ldap_link();
594     $ldap->cd ($this->current['BASE']);
595     $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
596     while ($attrs= $ldap->fetch()){
597       $dn= $ldap->getDN();
598       $this->tdepartments[$dn]= "";
600       /* Save administrative departments */
601       if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
602           isset($attrs['gosaUnitTag'][0])){
603         $administrative[$dn]= $attrs['gosaUnitTag'][0];
604         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
605       }
606     
607       if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
608           isset($attrs['gosaUnitTag'][0])){
609         $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
610       }
611     
612       if ($dn == $ignore_dn){
613         continue;
614       }
616       /* Only assign non-root departments */
617       if ($dn != $result['/']){
618         $result[convert_department_dn($dn)]= $dn;
619       }
620     }
622     $this->adepartments= $administrative;
623     $this->departments= $result;
624   }
627   function make_idepartments($max_size= 28)
628   {
629     global $config;
630     $base = $config->current['BASE'];
632     $arr = array();
633     $ui= get_userinfo();
635     $this->idepartments= array();
637     /* Create multidimensional array, with all departments. */
638     foreach ($this->departments as $key => $val){
640       /* When using strict_units, filter non relevant parts */
641       if (isset($config->current['STRICT_UNITS']) && preg_match('/true/i', $config->current['STRICT_UNITS'])){
642         if ($ui->gosaUnitTag != "" && isset($this->tdepartments[$val]) &&
643             $this->tdepartments[$val] != $ui->gosaUnitTag){
644 #          continue;
645         }
646       }
648       /* remove base from dn */
649       $val2 = str_replace($base,"",$val);
651       /* Get every single ou */
652       $str = preg_replace("/ou=/","|ou=",$val2);        
653       $elements = array_reverse(split("\|",$str));              
655       /* Save last array position */
656       $last = &$arr;
658       /* Get array depth  */
659       $cnt = count($elements);
661       /* Add last ou element of current dn to our array */
662       foreach($elements as $key => $ele){
664         /* skip enpty */
665         if(empty($ele)) continue;
667         /* Extract department name */           
668         $elestr = preg_replace("/^ou=/","", $ele);
669         $elestr = preg_replace("/,$/","",$elestr);      
671         /* Add to array */      
672         if($key == ($cnt-2)){
673           $last[$elestr]['ENTRY'] = $val;
674         }
676         /* Set next array appending position */
677         $last = &$last[$elestr]['SUB'];
678       }
679     }
681     /* Add base entry */
682     $ret["/"]["ENTRY"]  = $base;
683     $ret["/"]["SUB"]    = $arr;
685     $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
686   }
689   /* Creates display friendly output from make_idepartments */
690   function generateDepartmentArray($arr,$depth = -1,$max_size){
691     $ret = array();
692     $depth ++;
694     /* Walk through array */    
695     ksort($arr);
696     foreach($arr as $name => $entries){
698       /* If this department is the last in the current tree position 
699        * remove it, to avoid generating output for it */
700       if(count($entries['SUB'])==0){
701         unset($entries['SUB']);
702       }
704       /* Fix name, if it contains a replace tag */
705       $name= @LDAP::fix($name);
707       /* Check if current name is too long, then cut it */
708       if(mb_strlen($name, 'UTF-8')> $max_size){
709         $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
710       }
712       /* Append the name to the list */ 
713       if(isset($entries['ENTRY'])){
714         $a = "";
715         for($i = 0 ; $i < $depth ; $i ++){
716           $a.=".";
717         }
718         $ret[$entries['ENTRY']]=$a."&nbsp;".$name;
719       } 
721       /* recursive add of subdepartments */
722       if(isset($entries['SUB'])){
723         $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
724       }
725     }
727     return($ret);
728   }
730   /* This function returns all available Shares defined in this ldap
731    * There are two ways to call this function, if listboxEntry is true
732    *  only name and path are attached to the array, in it is false, the whole
733    *  entry will be parsed an atached to the result.
734    */
735   function getShareList($listboxEntry = false)
736   {
737     $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","server",get_ou("serverou"),
738         $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
739     $return =array();
740     foreach($tmp as $entry){
742       if(isset($entry['goExportEntry']['count'])){
743         unset($entry['goExportEntry']['count']);
744       }
745       if(isset($entry['goExportEntry'])){
746         foreach($entry['goExportEntry'] as $export){
747           $shareAttrs = split("\|",$export);
748           if($listboxEntry) {
749             $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
750           }else{
751             $return[$shareAttrs[0]."|".$entry['cn'][0]]['server']       = $entry['cn'][0];
752             $return[$shareAttrs[0]."|".$entry['cn'][0]]['name']         = $shareAttrs[0];
753             $return[$shareAttrs[0]."|".$entry['cn'][0]]['description']  = $shareAttrs[1];
754             $return[$shareAttrs[0]."|".$entry['cn'][0]]['type']         = $shareAttrs[2];
755             $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset']      = $shareAttrs[3];
756             $return[$shareAttrs[0]."|".$entry['cn'][0]]['path']         = $shareAttrs[4];
757             $return[$shareAttrs[0]."|".$entry['cn'][0]]['option']       = $shareAttrs[5];
758           }
759         }
760       }
761     }
762     return($return);
763   }
766   /* This function returns all available ShareServer */
767   function getShareServerList()
768   {
769     global $config;
770     $return = array();
771     $base = $config->current['BASE'];
772     $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
773           get_ou("serverou"), $base,array("goExportEntry","cn"),GL_NONE);
775     foreach($res as $entry){
776         if(isset($entry['goExportEntry']['count'])){
777           unset($entry['goExportEntry']['count']);
778         }
779         foreach($entry['goExportEntry'] as $share){
780           $a_share = split("\|",$share);
781           $sharename = $a_share[0];
782           $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
783         }
784     }
785     return($return);
786   }
789   /* Check if there's the specified bool value set in the configuration */
790   function boolValueIsTrue($section, $value)
791   {
792     $section= strtoupper($section);
793     $value= strtoupper($value);
794     if (isset($this->data[$section][$value])){
795     
796       $data= $this->data[$section][$value];
797       if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
798         return TRUE;
799       }
801     }
803     return FALSE;
804   }
807   function __search(&$arr, $name, $return)
808   {
809     $return= strtoupper($return);
810     if (is_array($arr)){
811       foreach ($arr as &$a){
812         if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
813           return(isset($a[$return])?$a[$return]:"");
814         } else {
815           $res= $this->__search ($a, $name, $return);
816           if ($res != ""){
817             return $res;
818           }
819         }
820       }
821     }
822     return ("");
823   }
826   function search($class, $value, $categories= "")
827   {
828     if (is_array($categories)){
829       foreach ($categories as $category){
830         $res= $this->__search($this->data[strtoupper($category)], $class, $value);
831         if ($res != ""){
832           return $res;
833         }
834       }
835     } else {
836       if ($categories == "") {
837         return $this->__search($this->data, $class, $value);
838       } else {
839         return $this->__search($this->data[strtoupper($categories)], $class, $value);
840       }
841     } 
843     return ("");
844   }
847   /* On debian systems the session files are deleted with
848    *  a cronjob, which detects all files older than specified 
849    *  in php.ini:'session.gc_maxlifetime' and removes them.
850    * This function checks if the gosa.conf value matches the range
851    *  defined by session.gc_maxlifetime.
852    */
853   function check_session_lifetime()
854   {
855     $cfg_lifetime = $this->data['MAIN']['SESSION_LIFETIME'];
856     $ini_lifetime = ini_get('session.gc_maxlifetime');
857     $deb_system   = file_exists('/etc/debian_version');
858     return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));  
859   }
862 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
863 ?>