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 /*! \brief Configuration class
24 * \ingroup coreclasses
25 *
26 * The configuration class, responsible for parsing and querying the
27 * gosa configuration file.
28 */
30 class config {
32 /* XML parser */
33 var $parser;
34 var $config_found= FALSE;
35 var $tags= array();
36 var $level= 0;
37 var $gpc= 0;
38 var $section= "";
39 var $currentLocation= "";
41 /*! \brief Store configuration for current location */
42 var $current= array();
44 /* Link to LDAP-server */
45 var $ldap= NULL;
46 var $referrals= array();
48 /* \brief Configuration data
49 *
50 * - $data['SERVERS'] contains server informations.
51 * */
52 var $data= array( 'TABS' => array(), 'LOCATIONS' => array(), 'SERVERS' => array(),
53 'MAIN' => array(),
54 'MENU' => array(), 'SERVICE' => array());
55 var $basedir= "";
56 var $config_version ="NOT SET";
58 /* Keep a copy of the current deparment list */
59 var $departments= array();
60 var $idepartments= array();
61 var $adepartments= array();
62 var $tdepartments= array();
63 var $filename = "";
64 var $last_modified = 0;
66 /*! \brief Class constructor of the config class
67 *
68 * \param string 'filename' path to the configuration file
69 * \param string 'basedir' base directory
70 *
71 * */
72 function config($filename, $basedir= "")
73 {
74 $this->parser = xml_parser_create();
75 $this->basedir= $basedir;
77 xml_set_object($this->parser, $this);
78 xml_set_element_handler($this->parser, "tag_open", "tag_close");
80 /* Parse config file directly? */
81 if ($filename != ""){
82 $this->parse($filename);
83 }
84 }
87 /*! \brief Check and reload the configuration
88 *
89 * This function checks if the configuration has changed, since it was
90 * read the last time and reloads it. It uses the file mtime to check
91 * weither the file changed or not.
92 *
93 * */
94 function check_and_reload()
95 {
96 global $ui;
98 /* Check if class_location.inc has changed, this is the case
99 if we have installed or removed plugins.
100 */
101 if(session::global_is_set("class_location.inc:timestamp")){
102 $tmp = stat("../include/class_location.inc");
103 if($tmp['mtime'] != session::global_get("class_location.inc:timestamp")){
104 session::global_un_set("plist");
105 }
106 }
107 $tmp = stat("../include/class_location.inc");
108 session::global_set("class_location.inc:timestamp",$tmp['mtime']);
110 if($this->filename != "" && filemtime($this->filename) != $this->last_modified){
112 $this->config_found= FALSE;
113 $this->tags= array();
114 $this->level= 0;
115 $this->gpc= 0;
116 $this->section= "";
117 $this->currentLocation= "";
119 $this->parser = xml_parser_create();
120 xml_set_object($this->parser, $this);
121 xml_set_element_handler($this->parser, "tag_open", "tag_close");
122 $this->parse($this->filename);
123 $this->set_current($this->current['NAME']);
124 }
125 }
128 /*! \brief Parse the given configuration file
129 *
130 * Parses the configuration file and displays errors if there
131 * is something wrong with it.
132 *
133 * \param string 'filename' The filename of the configuration file.
134 * */
136 function parse($filename)
137 {
138 $this->data = array(
139 "TABS" => array(),
140 "LOCATIONS" => array(),
141 "MAIN" => array(),
142 "MENU" => array(),
143 "SERVICE" => array());
145 $this->last_modified = filemtime($filename);
146 $this->filename = $filename;
147 $fh= fopen($filename, "r");
148 $xmldata= fread($fh, 100000);
149 fclose($fh);
150 if(!xml_parse($this->parser, chop($xmldata))){
151 $msg = sprintf(_("XML error in gosa.conf: %s at line %d"),
152 xml_error_string(xml_get_error_code($this->parser)),
153 xml_get_current_line_number($this->parser));
154 msg_dialog::display(_("Configuration error"), $msg, FATAL_ERROR_DIALOG);
155 exit;
156 }
158 // Default schemacheck to "true"
159 if(!isset($this->data['MAIN']['SCHEMACHECK'])){
160 $this->data['MAIN']['SCHEMACHECK'] = "true";
161 }
162 }
164 function tag_open($parser, $tag, $attrs)
165 {
166 /* Save last and current tag for reference */
167 $this->tags[$this->level]= $tag;
168 $this->level++;
170 /* Trigger on CONF section */
171 if ($tag == 'CONF'){
172 $this->config_found= TRUE;
173 if(isset($attrs['CONFIGVERSION'])){
174 $this->config_version = $attrs['CONFIGVERSION'];
175 }
176 }
178 /* Return if we're not in config section */
179 if (!$this->config_found){
180 return;
181 }
183 /* yes/no to true/false and upper case TRUE to true and so on*/
184 foreach($attrs as $name => $value){
185 if(preg_match("/^(true|yes)$/i",$value)){
186 $attrs[$name] = "true";
187 }elseif(preg_match("/^(false|no)$/i",$value)){
188 $attrs[$name] = "false";
189 }
190 }
192 /* Look through attributes */
193 switch ($this->tags[$this->level-1]){
196 /* Handle tab section */
197 case 'TAB': $name= $this->tags[$this->level-2];
199 /* Create new array? */
200 if (!isset($this->data['TABS'][$name])){
201 $this->data['TABS'][$name]= array();
202 }
204 /* Add elements */
205 $this->data['TABS'][$name][]= $attrs;
206 break;
208 /* Handle location */
209 case 'LOCATION':
210 if ($this->tags[$this->level-2] == 'MAIN'){
211 $name= $attrs['NAME'];
212 $name = preg_replace("/[<>\"']/","",$name);
213 $attrs['NAME'] = $name;
214 $this->currentLocation= $name;
216 /* Add location elements */
217 $this->data['LOCATIONS'][$name]= $attrs;
218 }
219 break;
221 /* Handle referral tags */
222 case 'REFERRAL':
223 if ($this->tags[$this->level-2] == 'LOCATION'){
224 $url= $attrs['URI'];
225 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
227 /* Add location elements */
228 if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
229 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
230 }
232 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
233 }
234 break;
236 /* Load main parameters */
237 case 'MAIN':
238 $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
239 break;
241 /* Load menu */
242 case 'SECTION':
243 if ($this->tags[$this->level-2] == 'MENU'){
244 $this->section= $attrs['NAME'];
245 $this->data['MENU'][$this->section]= array(); ;
246 }
247 break;
249 /* Inser plugins */
250 case 'PLUGIN':
251 if ($this->tags[$this->level-3] == 'MENU' &&
252 $this->tags[$this->level-2] == 'SECTION'){
254 $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
255 }
256 if ($this->tags[$this->level-2] == 'SERVICEMENU'){
257 $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
258 }
259 break;
260 }
261 }
263 function tag_close($parser, $tag)
264 {
265 /* Close config section */
266 if ($tag == 'CONF'){
267 $this->config_found= FALSE;
268 }
269 $this->level--;
270 }
273 function get_credentials($creds)
274 {
275 if (isset($_SERVER['HTTP_GOSA_KEY'])){
276 if (!session::global_is_set('HTTP_GOSA_KEY_CACHE')){
277 session::global_set('HTTP_GOSA_KEY_CACHE',array());
278 }
279 $cache = session::global_get('HTTP_GOSA_KEY_CACHE');
280 if(!isset($cache[$creds])){
281 $cache[$creds] = cred_decrypt($creds, $_SERVER['HTTP_GOSA_KEY']);
282 session::global_set('HTTP_GOSA_KEY_CACHE',$cache);
283 }
284 return ($cache[$creds]);
285 }
286 return ($creds);
287 }
290 /*! \brief Get a LDAP link object
291 *
292 * This function can be used to get an ldap object, which in turn can
293 * be used to query the LDAP. See the LDAP class for more information
294 * on how to use it.
295 *
296 * Example usage:
297 * \code
298 * $ldap = $this->config->get_ldap_link();
299 * \endcode
300 *
301 * \param boolean sizelimit Weither to impose a sizelimit on the LDAP object or not.
302 * Defaults to false. If set to true, the size limit in the configuration
303 * file will be used to set the option LDAP_OPT_SIZELIMIT.
304 * \return ldapMultiplexer object
305 */
306 function get_ldap_link($sizelimit= FALSE)
307 {
308 if($this->ldap === NULL || !is_resource($this->ldap->cid)){
310 /* Build new connection */
311 $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
312 $this->current['ADMINDN'], $this->get_credentials($this->current['ADMINPASSWORD']));
314 /* Check for connection */
315 if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
316 $smarty= get_smarty();
317 msg_dialog::display(_("LDAP error"), _("Cannot bind to LDAP. Please contact the system administrator."), FATAL_ERROR_DIALOG);
318 exit();
319 }
321 /* Move referrals */
322 if (!isset($this->current['REFERRAL'])){
323 $this->ldap->referrals= array();
324 } else {
325 $this->ldap->referrals= $this->current['REFERRAL'];
326 }
328 if (!session::global_is_set('size_limit')){
329 session::global_set('size_limit',$this->current['LDAPSIZELIMIT']);
330 session::global_set('size_ignore',$this->current['LDAPSIZEIGNORE']);
331 }
332 }
334 $obj = new ldapMultiplexer($this->ldap);
335 if ($sizelimit){
336 $obj->set_size_limit(session::global_get('size_limit'));
337 } else {
338 $obj->set_size_limit(0);
339 }
340 return($obj);
341 }
343 /*! \brief Set the current location
344 *
345 * \param string name the name of the location
346 */
347 function set_current($name)
348 {
349 $this->current= $this->data['LOCATIONS'][$name];
351 if (!isset($this->current['USERRDN'])){
352 $this->current['USERRDN']= "ou=people";
353 }
354 if (!isset($this->current['GROUPRDN'])){
355 $this->current['GROUPS']= "ou=groups";
356 }
358 if (isset($this->current['INITIAL_BASE'])){
359 session::global_set('CurrentMainBase',$this->current['INITIAL_BASE']);
360 }
362 /* Remove possibly added ',' from end of group and people ou */
363 $this->current['GROUPS'] = preg_replace("/,*$/","",$this->current['GROUPRDN']);
364 $this->current['USERRDN'] = preg_replace("/,*$/","",$this->current['USERRDN']);
366 if (!isset($this->current['SAMBAMACHINEACCOUNTRDN'])){
367 $this->current['SAMBAMACHINEACCOUNTRDN']= "ou=winstations,ou=systems";
368 }
369 if (!isset($this->current['ACCOUNTPRIMARYATTRIBUTE'])){
370 $this->current['ACCOUNTPRIMARYATTRIBUTE']= "cn";
371 }
372 if (!isset($this->current['MINID'])){
373 $this->current['MINID']= 100;
374 }
375 if (!isset($this->current['LDAPSIZELIMIT'])){
376 $this->current['LDAPSIZELIMIT']= 200;
377 }
378 if (!isset($this->current['SIZEINGORE'])){
379 $this->current['LDAPSIZEIGNORE']= TRUE;
380 } else {
381 if (preg_match("/true/i", $this->current['LDAPSIZEIGNORE'])){
382 $this->current['LDAPSIZEIGNORE']= TRUE;
383 } else {
384 $this->current['LDAPSIZEIGNORE']= FALSE;
385 }
386 }
388 /* Sort referrals, if present */
389 if (isset ($this->current['REFERRAL'])){
390 $bases= array();
391 $servers= array();
392 foreach ($this->current['REFERRAL'] as $ref){
393 $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URI']);
394 $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URI']);
395 $bases[$base]= strlen($base);
396 $servers[$base]= $server;
397 }
398 asort($bases);
399 reset($bases);
400 }
402 /* SERVER not defined? Load the one with the shortest base */
403 if (!isset($this->current['SERVER'])){
404 $this->current['SERVER']= $servers[key($bases)];
405 }
407 /* BASE not defined? Load the one with the shortest base */
408 if (!isset($this->current['BASE'])){
409 $this->current['BASE']= key($bases);
410 }
412 /* Convert BASE to have escaped special characters */
413 $this->current['BASE']= @LDAP::convert($this->current['BASE']);
415 /* Parse LDAP referral informations */
416 if (!isset($this->current['ADMINDN']) || !isset($this->current['ADMINPASSWORD'])){
417 $url= $this->current['SERVER'];
418 $referral= $this->current['REFERRAL'][$url];
419 $this->current['ADMINDN']= $referral['ADMINDN'];
420 $this->current['ADMINPASSWORD']= $referral['ADMINPASSWORD'];
421 }
423 /* Load server informations */
424 $this->load_servers();
425 }
428 /*! \brief Load server information from config/LDAP
429 *
430 * This function searches the LDAP for servers (e.g. goImapServer, goMailServer etc.)
431 * and stores information about them $this->data['SERVERS']. In the case of mailservers
432 * the main section of the configuration file is searched, too.
433 */
434 function load_servers ()
435 {
436 /* Only perform actions if current is set */
437 if ($this->current === NULL){
438 return;
439 }
441 /* Fill imap servers */
442 $ldap= $this->get_ldap_link();
443 $ldap->cd ($this->current['BASE']);
445 /* Search mailMethod konfiguration in main section too
446 */
447 $this->current['MAILMETHOD'] = $this->get_cfg_value("mailMethod","");
448 if (!isset($this->current['MAILMETHOD'])){
449 $this->current['MAILMETHOD']= "";
450 }
451 if ($this->current['MAILMETHOD'] == ""){
452 $ldap->search ("(objectClass=goMailServer)", array('cn'));
453 $this->data['SERVERS']['IMAP']= array();
454 while ($attrs= $ldap->fetch()){
455 $name= $attrs['cn'][0];
456 $this->data['SERVERS']['IMAP'][$name]=
457 array(
458 "server_dn" => $attrs['dn'],
459 "connect" => "",
460 "admin" => "",
461 "password" => "",
462 "sieve_server"=> "",
463 "sieve_option"=> "",
464 "sieve_port" => "");
465 }
466 } else {
467 $ldap->search ("(&(objectClass=goImapServer)(goImapSieveServer=*))",
468 array('goImapName', 'goImapConnect', 'goImapAdmin', 'goImapPassword',
469 'goImapSieveServer', 'goImapSievePort'));
471 $this->data['SERVERS']['IMAP']= array();
473 while ($attrs= $ldap->fetch()){
475 /* Check if the given goImapSieveServer is in the new style "{cn:port/option}"
476 or the old style just "cn".
477 */
478 if(preg_match("/\{/",$attrs['goImapSieveServer'][0])){
479 $sieve_server = preg_replace("/^\{([^:]*).*$/","\\1",$attrs['goImapSieveServer'][0]);
480 $sieve_option = preg_replace("/^[^:]*[^\/]*+\/(.*)\}$/","\\1",$attrs['goImapSieveServer'][0]);
481 }else{
482 $sieve_server = $attrs['goImapSieveServer'][0];
483 $sieve_option = "";
484 }
486 $pwd = $attrs['goImapPassword'][0];
487 $imap_admin = $attrs['goImapAdmin'][0];
488 $imap_connect = $attrs['goImapConnect'][0];
489 $imap_server = $attrs['goImapName'][0];
490 $sieve_port = $attrs['goImapSievePort'][0];
492 $this->data['SERVERS']['IMAP'][$imap_server]=
493 array(
494 "server_dn" => $attrs['dn'],
495 "connect" => $imap_connect,
496 "admin" => $imap_admin,
497 "password" => $pwd,
498 "sieve_server"=> $sieve_server,
499 "sieve_option"=> $sieve_option,
500 "sieve_port" => $sieve_port);
501 }
502 }
504 /* Get kerberos server. FIXME: only one is supported currently */
505 $ldap->cd ($this->current['BASE']);
506 $ldap->search ("(&(goKrbRealm=*)(goKrbAdmin=*)(objectClass=goKrbServer))");
507 if ($ldap->count()){
508 $attrs= $ldap->fetch();
509 $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
510 'REALM' => $attrs['goKrbRealm'][0],
511 'ADMIN' => $attrs['goKrbAdmin'][0]);
512 }
514 /* Get cups server. FIXME: only one is supported currently */
515 $ldap->cd ($this->current['BASE']);
516 $ldap->search ("(objectClass=goCupsServer)");
517 if ($ldap->count()){
518 $attrs= $ldap->fetch();
519 $this->data['SERVERS']['CUPS']= $attrs['cn'][0];
520 }
522 /* Get fax server. FIXME: only one is supported currently */
523 $ldap->cd ($this->current['BASE']);
524 $ldap->search ("(objectClass=goFaxServer)");
525 if ($ldap->count()){
526 $attrs= $ldap->fetch();
527 $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
528 'LOGIN' => $attrs['goFaxAdmin'][0],
529 'PASSWORD' => $attrs['goFaxPassword'][0]);
530 }
533 /* Get asterisk servers */
534 $ldap->cd ($this->current['BASE']);
535 $ldap->search ("(objectClass=goFonServer)");
536 $this->data['SERVERS']['FON']= array();
537 if ($ldap->count()){
538 while ($attrs= $ldap->fetch()){
540 /* Add 0 entry for development */
541 if(count($this->data['SERVERS']['FON']) == 0){
542 $this->data['SERVERS']['FON'][0]= array(
543 'DN' => $attrs['dn'],
544 'SERVER' => $attrs['cn'][0],
545 'LOGIN' => $attrs['goFonAdmin'][0],
546 'PASSWORD' => $attrs['goFonPassword'][0],
547 'DB' => "gophone",
548 'SIP_TABLE' => "sip_users",
549 'EXT_TABLE' => "extensions",
550 'VOICE_TABLE' => "voicemail_users",
551 'QUEUE_TABLE' => "queues",
552 'QUEUE_MEMBER_TABLE' => "queue_members");
553 }
555 /* Add entry with 'dn' as index */
556 $this->data['SERVERS']['FON'][$attrs['dn']]= array(
557 'DN' => $attrs['dn'],
558 'SERVER' => $attrs['cn'][0],
559 'LOGIN' => $attrs['goFonAdmin'][0],
560 'PASSWORD' => $attrs['goFonPassword'][0],
561 'DB' => "gophone",
562 'SIP_TABLE' => "sip_users",
563 'EXT_TABLE' => "extensions",
564 'VOICE_TABLE' => "voicemail_users",
565 'QUEUE_TABLE' => "queues",
566 'QUEUE_MEMBER_TABLE' => "queue_members");
567 }
568 }
571 /* Get glpi server */
572 $ldap->cd ($this->current['BASE']);
573 $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
574 if ($ldap->count()){
575 $attrs= $ldap->fetch();
576 if(!isset($attrs['goGlpiPassword'])){
577 $attrs['goGlpiPassword'][0] ="";
578 }
579 $this->data['SERVERS']['GLPI']= array(
580 'SERVER' => $attrs['cn'][0],
581 'LOGIN' => $attrs['goGlpiAdmin'][0],
582 'PASSWORD' => $attrs['goGlpiPassword'][0],
583 'DB' => $attrs['goGlpiDatabase'][0]);
584 }
587 /* Get logdb server */
588 $ldap->cd ($this->current['BASE']);
589 $ldap->search ("(objectClass=goLogDBServer)");
590 if ($ldap->count()){
591 $attrs= $ldap->fetch();
592 if(!isset($attrs['gosaLogDB'][0])){
593 $attrs['gosaLogDB'][0] = "gomon";
594 }
595 $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
596 'LOGIN' => $attrs['goLogAdmin'][0],
597 'DB' => $attrs['gosaLogDB'][0],
598 'PASSWORD' => $attrs['goLogPassword'][0]);
599 }
602 /* GOsa logging databases */
603 $ldap->cd ($this->current['BASE']);
604 $ldap->search ("(objectClass=gosaLogServer)");
605 if ($ldap->count()){
606 while($attrs= $ldap->fetch()){
607 $this->data['SERVERS']['LOGGING'][$attrs['cn'][0]]=
608 array(
609 'DN' => $attrs['dn'],
610 'USER' => $attrs['goLogDBUser'][0],
611 'DB' => $attrs['goLogDB'][0],
612 'PWD' => $attrs['goLogDBPassword'][0]);
613 }
614 }
617 /* Get NFS server lists */
618 $tmp= array("default");
619 $tmp2= array("default");
620 $ldap->cd ($this->current['BASE']);
621 $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
622 while ($attrs= $ldap->fetch()){
623 for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
624 if(preg_match('/^[^|]+\|[^|]+\|NFS\|.*$/', $attrs["goExportEntry"][$i])){
625 $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
626 $tmp[]= $attrs["cn"][0].":$path";
627 }
628 if(preg_match('/^[^|]+\|[^|]+\|NBD\|.*$/', $attrs["goExportEntry"][$i])){
629 $path= preg_replace ("/^[^|]+\|[^|]+\|[^|]+\|[^|]+\|([^|]+).*$/", '\1', $attrs["goExportEntry"][$i]);
630 $tmp2[]= $attrs["cn"][0].":$path";
631 }
632 }
633 }
634 $this->data['SERVERS']['NFS']= $tmp;
635 $this->data['SERVERS']['NBD']= $tmp2;
637 /* Load Terminalservers */
638 $ldap->cd ($this->current['BASE']);
639 $ldap->search ("(objectClass=goTerminalServer)",array("cn","gotoSessionType"));
640 $this->data['SERVERS']['TERMINAL']= array();
641 $this->data['SERVERS']['TERMINAL'][]= "default";
642 $this->data['SERVERS']['TERMINAL_SESSION_TYPES'] = array();
645 while ($attrs= $ldap->fetch()){
646 $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
647 if(isset( $attrs["gotoSessionType"]['count'])){
648 for($i =0 ; $i < $attrs["gotoSessionType"]['count'] ; $i++){
649 $this->data['SERVERS']['TERMINAL_SESSION_TYPES'][$attrs["cn"][0]][] = $attrs["gotoSessionType"][$i];
650 }
651 }
652 }
654 /* Ldap Server
655 */
656 $this->data['SERVERS']['LDAP']= array();
657 $ldap->cd ($this->current['BASE']);
658 $ldap->search ("(&(objectClass=goLdapServer)(goLdapBase=*))");
659 while ($attrs= $ldap->fetch()){
660 $this->data['SERVERS']['LDAP'][$attrs['dn']] = $attrs;
661 }
663 /* Get misc server lists */
664 $this->data['SERVERS']['SYSLOG']= array("default");
665 $this->data['SERVERS']['NTP']= array("default");
666 $ldap->cd ($this->current['BASE']);
667 $ldap->search ("(objectClass=goNtpServer)");
668 while ($attrs= $ldap->fetch()){
669 $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
670 }
671 $ldap->cd ($this->current['BASE']);
672 $ldap->search ("(objectClass=goSyslogServer)");
673 while ($attrs= $ldap->fetch()){
674 $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
675 }
677 /* Get samba servers from LDAP, in case of samba3 */
678 $this->data['SERVERS']['SAMBA']= array();
679 $ldap->cd ($this->current['BASE']);
680 $ldap->search ("(objectClass=sambaDomain)");
681 while ($attrs= $ldap->fetch()){
682 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array( "SID" =>"","RIDBASE" =>"");
683 if(isset($attrs["sambaSID"][0])){
684 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["SID"] = $attrs["sambaSID"][0];
685 }
686 if(isset($attrs["sambaAlgorithmicRidBase"][0])){
687 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]["RIDBASE"] = $attrs["sambaAlgorithmicRidBase"][0];
688 }
689 }
691 /* If no samba servers are found, look for configured sid/ridbase */
692 if (count($this->data['SERVERS']['SAMBA']) == 0){
693 if (!isset($this->current["SAMBASID"]) || !isset($this->current["SAMBARIDBASE"])){
694 msg_dialog::display(_("Configuration error"), _("sambaSID and/or sambaRidBase missing in the configuration!"), ERROR_DIALOG);
695 } else {
696 $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
697 "SID" => $this->current["SAMBASID"],
698 "RIDBASE" => $this->current["SAMBARIDBASE"]);
699 }
700 }
702 }
705 function get_departments($ignore_dn= "")
706 {
707 global $config;
709 /* Initialize result hash */
710 $result= array();
711 $administrative= array();
712 $result['/']= $this->current['BASE'];
713 $this->tdepartments= array();
715 /* Get all department types from department Management, to be able detect the department type.
716 -It is possible that differnty department types have the same name,
717 in this case we have to mark the department name to be able to differentiate.
718 (e.g l=Name or o=Name)
719 */
720 $types = departmentManagement::get_support_departments();
722 /* Create a list of attributes to fetch */
723 $ldap_values = array("objectClass","gosaUnitTag");
724 $filter = "";
725 foreach($types as $type){
726 $ldap_values[] = $type['ATTR'];
727 $filter .= "(objectClass=".$type['OC'].")";
728 }
729 $filter = "(&(objectClass=gosaDepartment)(|".$filter."))";
731 /* Get list of department objects */
732 $ldap= $this->get_ldap_link();
733 $ldap->cd ($this->current['BASE']);
734 $ldap->search ($filter, $ldap_values);
735 while ($attrs= $ldap->fetch()){
737 /* Detect department type */
738 $type_data = array();
739 foreach($types as $t => $data){
740 if(in_array($data['OC'],$attrs['objectClass'])){
741 $type_data = $data;
742 break;
743 }
744 }
746 /* Unknown department type -> skip
747 */
748 if(!count($type_data)) continue;
750 $dn= $ldap->getDN();
751 $this->tdepartments[$dn]= "";
753 /* Save administrative departments */
754 if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
755 isset($attrs['gosaUnitTag'][0])){
756 $administrative[$dn]= $attrs['gosaUnitTag'][0];
757 $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
758 }
760 if (in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass']) &&
761 isset($attrs['gosaUnitTag'][0])){
762 $this->tdepartments[$dn]= $attrs['gosaUnitTag'][0];
763 }
765 if ($dn == $ignore_dn){
766 continue;
767 }
769 $c_dn = convert_department_dn($dn)." (".$type_data['ATTR'].")";
771 /* Only assign non-root departments */
772 if ($dn != $result['/']){
773 $result[$c_dn]= $dn;
774 }
775 }
777 $this->adepartments= $administrative;
778 $this->departments= $result;
779 }
782 function make_idepartments($max_size= 28)
783 {
784 global $config;
785 $base = $config->current['BASE'];
786 $qbase = preg_quote($base, '/');
787 $utags= isset($config->current['HONOURUNITTAGS']) && preg_match('/true/i', $config->current['HONOURUNITTAGS']);
789 $arr = array();
790 $ui= get_userinfo();
792 $this->idepartments= array();
794 /* Create multidimensional array, with all departments. */
795 foreach ($this->departments as $key => $val){
797 /* When using strict_units, filter non relevant parts */
798 if ($utags){
799 if ($ui->gosaUnitTag != '' && isset($this->tdepartments[$val]) &&
800 $this->tdepartments[$val] != $ui->gosaUnitTag){
802 #TODO: link with strict*
803 #continue;
804 }
805 }
807 /* Split dn into single department pieces */
808 $elements = array_reverse(split(',',preg_replace("/$qbase$/",'',$val)));
810 /* Add last ou element of current dn to our array */
811 $last = &$arr;
812 foreach($elements as $key => $ele){
814 /* skip empty */
815 if(empty($ele)) continue;
817 /* Extract department name */
818 $elestr = trim(preg_replace('/^[^=]*+=/','', $ele),',');
819 $nameA = trim(preg_replace('/=.*$/','', $ele),',');
820 if($nameA != 'ou'){
821 $nameA = " ($nameA)";
822 }else{
823 $nameA = '';
824 }
826 /* Add to array */
827 if($key == (count($elements)-1)){
828 $last[$elestr.$nameA]['ENTRY'] = $val;
829 }
831 /* Set next array appending position */
832 $last = &$last[$elestr.$nameA]['SUB'];
833 }
834 }
837 /* Add base entry */
838 $ret['/']['ENTRY'] = $base;
839 $ret['/']['SUB'] = $arr;
840 $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
841 }
844 /* Creates display friendly output from make_idepartments */
845 function generateDepartmentArray($arr,$depth = -1,$max_size = 256)
846 {
847 $ret = array();
848 $depth ++;
850 /* Walk through array */
851 ksort($arr);
852 foreach($arr as $name => $entries){
854 /* If this department is the last in the current tree position
855 * remove it, to avoid generating output for it */
856 if(count($entries['SUB'])==0){
857 unset($entries['SUB']);
858 }
860 /* Fix name, if it contains a replace tag */
861 $name= preg_replace('/\\\\,/', ',', LDAP::fix($name));
863 /* Check if current name is too long, then cut it */
864 if(mb_strlen($name, 'UTF-8')> $max_size){
865 $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
866 }
868 /* Append the name to the list */
869 if(isset($entries['ENTRY'])){
870 $a = "";
871 for($i = 0 ; $i < $depth ; $i ++){
872 $a.=".";
873 }
874 $ret[$entries['ENTRY']]=$a." ".$name;
875 }
877 /* recursive add of subdepartments */
878 if(isset($entries['SUB'])){
879 $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
880 }
881 }
883 return($ret);
884 }
886 /*! \brief Get all available shares defined in the current LDAP
887 *
888 * This function returns all available Shares defined in this ldap
889 *
890 * \param boolean listboxEntry If set to TRUE, only name and path are
891 * attached to the array. If FALSE, the whole entry will be parsed an atached to the result.
892 * \return array
893 */
894 function getShareList($listboxEntry = false)
895 {
896 $tmp = get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))","server",get_ou("serverRDN"),
897 $this->current['BASE'],array("goExportEntry","cn"), GL_NONE);
898 $return =array();
899 foreach($tmp as $entry){
901 if(isset($entry['goExportEntry']['count'])){
902 unset($entry['goExportEntry']['count']);
903 }
904 if(isset($entry['goExportEntry'])){
905 foreach($entry['goExportEntry'] as $export){
906 $shareAttrs = split("\|",$export);
907 if($listboxEntry) {
908 $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
909 }else{
910 $return[$shareAttrs[0]."|".$entry['cn'][0]]['server'] = $entry['cn'][0];
911 $return[$shareAttrs[0]."|".$entry['cn'][0]]['name'] = $shareAttrs[0];
912 $return[$shareAttrs[0]."|".$entry['cn'][0]]['description'] = $shareAttrs[1];
913 $return[$shareAttrs[0]."|".$entry['cn'][0]]['type'] = $shareAttrs[2];
914 $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset'] = $shareAttrs[3];
915 $return[$shareAttrs[0]."|".$entry['cn'][0]]['path'] = $shareAttrs[4];
916 $return[$shareAttrs[0]."|".$entry['cn'][0]]['option'] = $shareAttrs[5];
917 }
918 }
919 }
920 }
921 return($return);
922 }
925 /*! \brief Return al available share servers
926 *
927 * This function returns all available ShareServers.
928 *
929 * \return array
930 * */
931 function getShareServerList()
932 {
933 global $config;
934 $return = array();
935 $ui = get_userinfo();
936 $base = $config->current['BASE'];
937 $res= get_sub_list("(&(objectClass=goShareServer)(goExportEntry=*))", "server",
938 get_ou("serverRDN"), $base,array("goExportEntry","cn"),GL_NONE | GL_NO_ACL_CHECK);
940 foreach($res as $entry){
942 $acl = $ui->get_permissions($entry['dn'],"server","");
943 if(isset($entry['goExportEntry']['count'])){
944 unset($entry['goExportEntry']['count']);
945 }
946 foreach($entry['goExportEntry'] as $share){
947 $a_share = split("\|",$share);
948 $sharename = $a_share[0];
949 $data= array();
950 $data['NAME'] = $sharename;
951 $data['ACL'] = $acl;
952 $data['SERVER'] = $entry['cn']['0'];
953 $data['SHARE'] = $sharename;
954 $data['DISPLAY']= $entry['cn'][0]." [".$sharename."]";
955 $return[$entry['cn'][0]."|".$sharename] = $data;
956 }
957 }
958 return($return);
959 }
962 /*! \brief Check if there's the specified bool value set in the configuration
963 *
964 * The function checks, weither the specified bool value is set to a true
965 * value in the configuration file. Considered true are either true or yes,
966 * case-insensitive.
967 *
968 * Example usage:
969 * \code
970 * if ($this->config->boolValueIsTrue("main", "copyPaste")) {
971 * echo "Copy Paste Handling is enabled";
972 * }
973 * \endcode
974 *
975 * \param string 'section' Section in the configuration file.
976 * \param string 'value' Key in the given section, which is subject to check
977 *
978 *
979 * */
980 function boolValueIsTrue($section, $value)
981 {
982 $section= strtoupper($section);
983 $value= strtoupper($value);
984 if (isset($this->data[$section][$value])){
986 $data= $this->data[$section][$value];
987 if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
988 return TRUE;
989 }
991 }
993 return FALSE;
994 }
997 function __search(&$arr, $name, $return)
998 {
999 $return= strtoupper($return);
1000 if (is_array($arr)){
1001 foreach ($arr as &$a){
1002 if (isset($a['CLASS']) && strcasecmp($name, $a['CLASS']) == 0){
1003 return(isset($a[$return])?$a[$return]:"");
1004 } else {
1005 $res= $this->__search ($a, $name, $return);
1006 if ($res != ""){
1007 return $res;
1008 }
1009 }
1010 }
1011 }
1012 return ("");
1013 }
1016 /*! Search for a configuration setting in different categories
1017 *
1018 * Searches for the value of a given key in the configuration data.
1019 * Optionally the list of categories to search (tabs, main, locations) can
1020 * be specified. The first value that matches is returned.
1021 *
1022 * Example usage:
1023 * \code
1024 * $postcmd = $this->config->search(get_class($this), "POSTCOMMAND", array("menu", "tabs"));
1025 * \endcode
1026 *
1027 * */
1028 function search($class, $value, $categories= "")
1029 {
1030 if (is_array($categories)){
1031 foreach ($categories as $category){
1032 $res= $this->__search($this->data[strtoupper($category)], $class, $value);
1033 if ($res != ""){
1034 return $res;
1035 }
1036 }
1037 } else {
1038 if ($categories == "") {
1039 return $this->__search($this->data, $class, $value);
1040 } else {
1041 return $this->__search($this->data[strtoupper($categories)], $class, $value);
1042 }
1043 }
1045 return ("");
1046 }
1049 /*! \brief Get a configuration value from the config
1050 *
1051 * This returns a configuration value from the config. It either
1052 * uses the data of the current location ($this->current),
1053 * if it contains the value (e.g. current['BASE']) or otherwise
1054 * uses the data from the main configuration section.
1055 *
1056 * If no value is found and an optional default has been specified,
1057 * then the default is returned.
1058 *
1059 * \param string 'name' the configuration key (case-insensitive)
1060 * \param string 'default' a default that is returned, if no value is found
1061 *
1062 *
1063 */
1064 function get_cfg_value($name, $default= "") {
1065 $name= strtoupper($name);
1067 /* Check if we have a current value for $name */
1068 if (isset($this->current[$name])){
1069 return ($this->current[$name]);
1070 }
1072 /* Check if we have a global value for $name */
1073 if (isset($this->data["MAIN"][$name])){
1074 return ($this->data["MAIN"][$name]);
1075 }
1077 return ($default);
1078 }
1081 /*! \brief Check if current configuration version matches the GOsa version
1082 *
1083 * This function checks if the configuration file version matches the
1084 * version of the gosa version, by comparing it with the configuration
1085 * file version of the example gosa.conf that comes with GOsa.
1086 * If a version mismatch occurs an error is triggered.
1087 * */
1088 function check_config_version()
1089 {
1090 /* Skip check, if we've already mentioned the mismatch
1091 */
1092 if(session::global_is_set("LastChecked") && session::global_get("LastChecked") == $this->config_version) return;
1094 /* Remember last checked version
1095 */
1096 session::global_set("LastChecked",$this->config_version);
1098 $current = md5(file_get_contents(CONFIG_TEMPLATE_DIR."/gosa.conf"));
1100 /* Check contributed config version and current config version.
1101 */
1102 if(($this->config_version == "NOT SET") || ($this->config_version != $current && !empty($this->config_version))){
1103 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."));
1104 }
1105 }
1108 /*! \brief Check if session lifetime matches session.gc_maxlifetime
1109 *
1110 * On debian systems the session files are deleted with
1111 * a cronjob, which detects all files older than specified
1112 * in php.ini:'session.gc_maxlifetime' and removes them.
1113 * This function checks if the gosa.conf value matches the range
1114 * defined by session.gc_maxlifetime.
1115 *
1116 * \return boolean TRUE or FALSE depending on weither the settings match
1117 * or not. If SESSIONLIFETIME is not configured in GOsa it always returns
1118 * TRUE.
1119 */
1120 function check_session_lifetime()
1121 {
1122 if(isset($this->data['MAIN']['SESSIONLIFETIME'])){
1123 $cfg_lifetime = $this->data['MAIN']['SESSIONLIFETIME'];
1124 $ini_lifetime = ini_get('session.gc_maxlifetime');
1125 $deb_system = file_exists('/etc/debian_version');
1126 return(!($deb_system && ($ini_lifetime < $cfg_lifetime)));
1127 }else{
1128 return(TRUE);
1129 }
1130 }
1132 /* Returns true if snapshots are enabled, and false if it is disalbed
1133 There will also be some errors psoted, if the configuration failed */
1134 function snapshotEnabled()
1135 {
1136 if($this->get_cfg_value("enableSnapshots") == "true"){
1138 /* Check if the snapshot_base is defined */
1139 if ($this->get_cfg_value("snapshotBase") == ""){
1141 /* Send message if not done already */
1142 if(!session::is_set("snapshotFailMessageSend")){
1143 session::set("snapshotFailMessageSend",TRUE);
1144 msg_dialog::display(_("Configuration error"),
1145 sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),
1146 "snapshotBase"), ERROR_DIALOG);
1147 }
1148 return(FALSE);
1149 }
1151 /* Check if the snapshot_base is defined */
1152 if (!is_callable("gzcompress")){
1154 /* Send message if not done already */
1155 if(!session::is_set("snapshotFailMessageSend")){
1156 session::set("snapshotFailMessageSend",TRUE);
1157 msg_dialog::display(_("Configuration error"),
1158 sprintf(_("The snapshot functionality is enabled, but the required compression module is missing. Please install '%s'."),"php5-zip / php5-gzip"), ERROR_DIALOG);
1159 }
1160 return(FALSE);
1161 }
1163 /* check if there are special server configurations for snapshots */
1164 if ($this->get_cfg_value("snapshotURI") != ""){
1166 /* check if all required vars are available to create a new ldap connection */
1167 $missing = "";
1168 foreach(array("snapshotURI","snapshotAdminDn","snapshotAdminPassword","snapshotBase") as $var){
1169 if($this->get_cfg_value($var) == ""){
1170 $missing .= $var." ";
1172 /* Send message if not done already */
1173 if(!session::is_set("snapshotFailMessageSend")){
1174 session::set("snapshotFailMessageSend",TRUE);
1175 msg_dialog::display(_("Configuration error"),
1176 sprintf(_("The snapshot functionality is enabled, but the required variable '%s' is not set."),
1177 $missing), ERROR_DIALOG);
1178 }
1179 return(FALSE);
1180 }
1181 }
1182 }
1183 return(TRUE);
1184 }
1185 return(FALSE);
1186 }
1188 }
1190 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1191 ?>