1 <?php
2 /*
3 * This code is part of GOsa (https://gosa.gonicus.de)
4 * Copyright (C) 2003-2006 - Cajus Pollmeier <pollmeier@gonicus.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class config {
23 /* XML parser */
24 var $parser;
25 var $config_found= FALSE;
26 var $tags= array();
27 var $level= 0;
28 var $gpc= 0;
29 var $section= "";
30 var $currentLocation= "";
32 /* Selected connection */
33 var $current= array();
35 /* Link to LDAP-server */
36 var $ldap= NULL;
37 var $referrals= array();
39 /* Configuration data */
40 var $data= array( 'TABS' => array(), 'LOCATIONS' => array(), 'SERVERS' => array(),
41 'MAIN' => array( 'LANGUAGES' => array(), 'FAXFORMATS' => array() ),
42 'MENU' => array(), 'SERVICE' => array());
43 var $basedir= "";
45 /* Keep a copy of the current deparment list */
46 var $departments= array();
47 var $idepartments= array();
48 var $adepartments= array();
50 function config($filename, $basedir= "")
51 {
52 $this->parser = xml_parser_create();
53 $this->basedir= $basedir;
55 xml_set_object($this->parser, $this);
56 xml_set_element_handler($this->parser, "tag_open", "tag_close");
58 /* Parse config file directly? */
59 if ($filename != ""){
60 $this->parse($filename);
61 }
62 }
64 function parse($filename)
65 {
66 $fh= fopen($filename, "r");
67 $xmldata= fread($fh, 100000);
68 fclose($fh);
69 if(!xml_parse($this->parser, chop($xmldata))){
70 print_red(sprintf(_("XML error in gosa.conf: %s at line %d"),
71 xml_error_string(xml_get_error_code($this->parser)),
72 xml_get_current_line_number($this->parser)));
73 echo $_SESSION['errors'];
74 exit;
75 }
76 }
78 function tag_open($parser, $tag, $attrs)
79 {
80 /* Save last and current tag for reference */
81 $this->tags[$this->level]= $tag;
82 $this->level++;
84 /* Trigger on CONF section */
85 if ($tag == 'CONF'){
86 $this->config_found= TRUE;
87 }
89 /* Return if we're not in config section */
90 if (!$this->config_found){
91 return;
92 }
94 /* Look through attributes */
95 switch ($this->tags[$this->level-1]){
97 /* Handle tab section */
98 case 'TAB': $name= $this->tags[$this->level-2];
100 /* Create new array? */
101 if (!isset($this->data['TABS'][$name])){
102 $this->data['TABS'][$name]= array();
103 }
105 /* Add elements */
106 $this->data['TABS'][$name][]= $attrs;
107 break;
109 /* Handle location */
110 case 'LOCATION':
111 if ($this->tags[$this->level-2] == 'MAIN'){
112 $name= $attrs['NAME'];
113 $this->currentLocation= $name;
115 /* Add location elements */
116 $this->data['LOCATIONS'][$name]= $attrs;
117 }
118 break;
120 /* Handle referral tags */
121 case 'REFERRAL':
122 if ($this->tags[$this->level-2] == 'LOCATION'){
123 $url= $attrs['URL'];
124 $server= preg_replace('!^([^:]+://[^/]+)/.*$!', '\\1', $url);
126 /* Add location elements */
127 if (!isset($this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'])){
128 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL']= array();
129 }
131 $this->data['LOCATIONS'][$this->currentLocation]['REFERRAL'][$server]= $attrs;
132 }
133 break;
135 /* Handle language */
136 case 'LANGUAGE':
137 if ($this->tags[$this->level-2] == 'MAIN'){
138 /* Add languages */
139 $this->data['MAIN']['LANGUAGES'][$attrs['NAME']]=
140 $attrs['TAG'];
141 }
142 break;
144 /* Handle faxformat */
145 case 'FAXFORMAT':
146 if ($this->tags[$this->level-2] == 'MAIN'){
147 /* Add fax formats */
148 $this->data['MAIN']['FAXFORMATS'][]= $attrs['TYPE'];
149 }
150 break;
152 /* Load main parameters */
153 case 'MAIN':
154 $this->data['MAIN']= array_merge ($this->data['MAIN'], $attrs);
155 break;
157 /* Load menu */
158 case 'SECTION':
159 if ($this->tags[$this->level-2] == 'MENU'){
160 $this->section= $attrs['NAME'];
161 $this->data['MENU'][$this->section]= array(); ;
162 }
163 break;
165 /* Inser plugins */
166 case 'PLUGIN':
167 if ($this->tags[$this->level-3] == 'MENU' &&
168 $this->tags[$this->level-2] == 'SECTION'){
170 $this->data['MENU'][$this->section][$this->gpc++]= $attrs;
171 }
172 if ($this->tags[$this->level-2] == 'SERVICEMENU'){
173 $this->data['SERVICE'][$attrs['CLASS']]= $attrs;
174 }
175 break;
176 }
177 }
179 function tag_close($parser, $tag)
180 {
181 /* Close config section */
182 if ($tag == 'CONF'){
183 $this->config_found= FALSE;
184 }
185 $this->level--;
186 }
188 function get_ldap_link($sizelimit= FALSE)
189 {
190 /* Build new connection */
191 $this->ldap= ldap_init ($this->current['SERVER'], $this->current['BASE'],
192 $this->current['ADMIN'], $this->current['PASSWORD']);
194 /* Check for connection */
195 if (is_null($this->ldap) || (is_int($this->ldap) && $this->ldap == 0)){
196 $smarty= get_smarty();
197 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
198 $smarty->display (get_template_path('headers.tpl'));
199 echo '<body style="background-image:none">'.$_SESSION['errors'].'</body></html>';
200 exit();
201 }
203 if (!isset($_SESSION['size_limit'])){
204 $_SESSION['size_limit']= $this->current['SIZELIMIT'];
205 $_SESSION['size_ignore']= $this->current['SIZEIGNORE'];
206 }
208 if ($sizelimit){
209 $this->ldap->set_size_limit($_SESSION['size_limit']);
210 } else {
211 $this->ldap->set_size_limit(0);
212 }
214 /* Move referrals */
215 if (!isset($this->current['REFERRAL'])){
216 $this->ldap->referrals= array();
217 } else {
218 $this->ldap->referrals= $this->current['REFERRAL'];
219 }
221 return ($this->ldap);
222 }
224 function set_current($name)
225 {
226 $this->current= $this->data['LOCATIONS'][$name];
227 if (!isset($this->current['PEOPLE'])){
228 $this->current['PEOPLE']= "ou=people";
229 }
230 if (!isset($this->current['GROUPS'])){
231 $this->current['GROUPS']= "ou=groups";
232 }
233 if (!isset($this->current['WINSTATIONS'])){
234 $this->current['WINSTATIONS']= "ou=winstations,ou=systems";
235 }
236 if (!isset($this->current['HASH'])){
237 $this->current['HASH']= "crypt";
238 }
239 if (!isset($this->current['DNMODE'])){
240 $this->current['DNMODE']= "cn";
241 }
242 if (!isset($this->current['MINID'])){
243 $this->current['MINID']= 100;
244 }
245 if (!isset($this->current['SIZELIMIT'])){
246 $this->current['SIZELIMIT']= 200;
247 }
248 if (!isset($this->current['SIZEINGORE'])){
249 $this->current['SIZEIGNORE']= TRUE;
250 } else {
251 if (preg_match("/true/i", $this->current['SIZEIGNORE'])){
252 $this->current['SIZEIGNORE']= TRUE;
253 } else {
254 $this->current['SIZEIGNORE']= FALSE;
255 }
256 }
258 /* Sort referrals, if present */
259 if (isset ($this->current['REFERRAL'])){
260 $bases= array();
261 $servers= array();
262 foreach ($this->current['REFERRAL'] as $ref){
263 $server= preg_replace('%^(.*)/[^/]+$%', '\\1', $ref['URL']);
264 $base= preg_replace('%^.*/([^/]+)$%', '\\1', $ref['URL']);
265 $bases[$base]= strlen($base);
266 $servers[$base]= $server;
267 }
268 asort($bases);
269 reset($bases);
270 }
272 /* SERVER not defined? Load the one with the shortest base */
273 if (!isset($this->current['SERVER'])){
274 $this->current['SERVER']= $servers[key($bases)];
275 }
277 /* BASE not defined? Load the one with the shortest base */
278 if (!isset($this->current['BASE'])){
279 $this->current['BASE']= key($bases);
280 }
282 /* Convert BASE to have escaped special characters */
283 $this->current['BASE']= @LDAP::convert($this->current['BASE']);
285 /* Parse LDAP referral informations */
286 if (!isset($this->current['ADMIN']) || !isset($this->current['PASSWORD'])){
287 $url= $this->current['SERVER'];
288 $referral= $this->current['REFERRAL'][$url];
289 $this->current['ADMIN']= $referral['ADMIN'];
290 $this->current['PASSWORD']= $referral['PASSWORD'];
291 }
293 /* Load server informations */
294 $this->load_servers();
295 }
297 function load_servers ()
298 {
299 /* Only perform actions if current is set */
300 if ($this->current == NULL){
301 return;
302 }
304 /* Fill imap servers */
305 $ldap= $this->get_ldap_link();
306 $ldap->cd ($this->current['BASE']);
307 $ldap->search ("(objectClass=goImapServer)");
309 $this->data['SERVERS']['IMAP']= array();
310 error_reporting(0);
311 while ($attrs= $ldap->fetch()){
312 $name= $attrs['goImapName'][0];
313 $this->data['SERVERS']['IMAP'][$name]= array( "connect" => $attrs['goImapConnect'][0],
314 "admin" => $attrs['goImapAdmin'][0],
315 "password" => $attrs['goImapPassword'][0],
316 "sieve_server" => $attrs['goImapSieveServer'][0],
317 "sieve_port" => $attrs['goImapSievePort'][0]);
318 }
319 error_reporting(E_ALL);
321 /* Get kerberos server. FIXME: only one is supported currently */
322 $ldap->cd ($this->current['BASE']);
323 $ldap->search ("(objectClass=goKrbServer)");
324 if ($ldap->count()){
325 $attrs= $ldap->fetch();
326 $this->data['SERVERS']['KERBEROS']= array( 'SERVER' => $attrs['cn'][0],
327 'REALM' => $attrs['goKrbRealm'][0],
328 'ADMIN' => $attrs['goKrbAdmin'][0],
329 'PASSWORD' => $attrs['goKrbPassword'][0]);
330 }
332 /* Get cups server. FIXME: only one is supported currently */
333 $ldap->cd ($this->current['BASE']);
334 $ldap->search ("(objectClass=goCupsServer)");
335 if ($ldap->count()){
336 $attrs= $ldap->fetch();
337 $this->data['SERVERS']['CUPS']= $attrs['cn'][0];
338 }
340 /* Get fax server. FIXME: only one is supported currently */
341 $ldap->cd ($this->current['BASE']);
342 $ldap->search ("(objectClass=goFaxServer)");
343 if ($ldap->count()){
344 $attrs= $ldap->fetch();
345 $this->data['SERVERS']['FAX']= array( 'SERVER' => $attrs['cn'][0],
346 'LOGIN' => $attrs['goFaxAdmin'][0],
347 'PASSWORD' => $attrs['goFaxPassword'][0]);
348 }
350 /* Get asterisk servers */
351 $ldap->cd ($this->current['BASE']);
352 $ldap->search ("(objectClass=goFonServer)");
353 if ($ldap->count()){
354 $attrs= $ldap->fetch();
355 $this->data['SERVERS']['FON']= array(
356 'SERVER' => $attrs['cn'][0],
357 'LOGIN' => $attrs['goFonAdmin'][0],
358 'PASSWORD' => $attrs['goFonPassword'][0],
359 'DB' => "gophone",
360 'SIP_TABLE' => "sip_users",
361 'EXT_TABLE' => "extensions",
362 'VOICE_TABLE' => "voicemail_users",
363 'QUEUE_TABLE' => "queues",
364 'QUEUE_MEMBER_TABLE' => "queue_members");
365 }
367 /* Get glpi servers */
368 $ldap->cd ($this->current['BASE']);
369 $ldap->search ("(&(objectClass=goGlpiServer)(cn=*)(goGlpiAdmin=*)(goGlpiDatabase=*))",array("cn","goGlpiPassword","goGlpiAdmin","goGlpiDatabase"));
370 if ($ldap->count()){
371 $attrs= $ldap->fetch();
372 if(!isset($attrs['goGlpiPassword'])){
373 $attrs['goGlpiPassword'][0] ="";
374 }
375 $this->data['SERVERS']['GLPI']= array(
376 'SERVER' => $attrs['cn'][0],
377 'LOGIN' => $attrs['goGlpiAdmin'][0],
378 'PASSWORD' => $attrs['goGlpiPassword'][0],
379 'DB' => $attrs['goGlpiDatabase'][0]);
380 }
381 /* Get logdb server */
382 $ldap->cd ($this->current['BASE']);
383 $ldap->search ("(objectClass=goLogDBServer)");
384 if ($ldap->count()){
385 $attrs= $ldap->fetch();
386 $this->data['SERVERS']['LOG']= array( 'SERVER' => $attrs['cn'][0],
387 'LOGIN' => $attrs['goLogAdmin'][0],
388 'PASSWORD' => $attrs['goLogPassword'][0]);
389 }
391 /* Get NFS server lists */
392 $tmp= array("default");
393 $ldap->cd ($this->current['BASE']);
394 $ldap->search ("(&(objectClass=goShareServer)(goExportEntry=*))");
395 while ($attrs= $ldap->fetch()){
396 for ($i= 0; $i<$attrs["goExportEntry"]["count"]; $i++){
397 $path= preg_replace ("/\s.*$/", "", $attrs["goExportEntry"][$i]);
398 $tmp[]= $attrs["cn"][0].":$path";
399 }
400 }
401 $this->data['SERVERS']['NFS']= $tmp;
404 /* Load Terminalservers */
405 $ldap->cd ($this->current['BASE']);
406 $ldap->search ("(objectClass=goTerminalServer)");
407 $this->data['SERVERS']['TERMINAL']= array();
408 $this->data['SERVERS']['TERMINAL'][]= "default";
410 $this->data['SERVERS']['FONT']= array();
411 $this->data['SERVERS']['FONT'][]= "default";
412 while ($attrs= $ldap->fetch()){
413 $this->data['SERVERS']['TERMINAL'][]= $attrs["cn"][0];
414 for ($i= 0; $i<$attrs["goFontPath"]["count"]; $i++){
415 $this->data['SERVERS']['FONT'][]= $attrs["goFontPath"][$i];
416 }
417 }
419 /* Ldap Server */
420 $this->data['SERVERS']['LDAP']= array();
421 $ldap->cd ($this->current['BASE']);
422 $ldap->search ("(objectClass=goLdapServer)");
423 while ($attrs= $ldap->fetch()){
424 if (isset($attrs["goLdapBase"])){
425 for ($i= 0; $i<$attrs["goLdapBase"]["count"]; $i++){
426 $this->data['SERVERS']['LDAP'][]= $attrs["cn"][0].":".$attrs["goLdapBase"][$i];
427 }
428 }
429 }
431 /* Get misc server lists */
432 $this->data['SERVERS']['SYSLOG']= array("default");
433 $this->data['SERVERS']['NTP']= array("default");
434 $ldap->cd ($this->current['BASE']);
435 $ldap->search ("(objectClass=goNtpServer)");
436 while ($attrs= $ldap->fetch()){
437 $this->data['SERVERS']['NTP'][]= $attrs["cn"][0];
438 }
439 $ldap->cd ($this->current['BASE']);
440 $ldap->search ("(objectClass=goSyslogServer)");
441 while ($attrs= $ldap->fetch()){
442 $this->data['SERVERS']['SYSLOG'][]= $attrs["cn"][0];
443 }
445 /* Get samba servers from LDAP, in case of samba3 */
446 if ($this->current['SAMBAVERSION'] == 3){
447 $this->data['SERVERS']['SAMBA']= array();
448 $ldap->cd ($this->current['BASE']);
449 $ldap->search ("(objectClass=sambaDomain)");
450 while ($attrs= $ldap->fetch()){
451 $this->data['SERVERS']['SAMBA'][$attrs['sambaDomainName'][0]]= array(
452 "SID" => $attrs["sambaSID"][0],
453 "RIDBASE" => $attrs["sambaAlgorithmicRidBase"][0]);
454 }
456 /* If no samba servers are found, look for configured sid/ridbase */
457 if (count($this->data['SERVERS']['SAMBA']) == 0){
458 if (!isset($this->current["SID"]) || !isset($this->current["RIDBASE"])){
459 print_red(_("SID and/or RIDBASE missing in your configuration!"));
460 echo $_SESSION['errors'];
461 exit;
462 } else {
463 $this->data['SERVERS']['SAMBA']['DEFAULT']= array(
464 "SID" => $this->current["SID"],
465 "RIDBASE" => $this->current["RIDBASE"]);
466 }
467 }
468 }
469 }
472 function get_departments($ignore_dn= "")
473 {
474 global $config;
476 /* Initialize result hash */
477 $result= array();
478 $administrative= array();
479 $result['/']= $this->current['BASE'];
481 /* Get list of department objects */
482 $ldap= $this->get_ldap_link();
483 $ldap->cd ($this->current['BASE']);
484 $ldap->search ("(objectClass=gosaDepartment)", array("ou", "objectClass", "gosaUnitTag"));
485 while ($attrs= $ldap->fetch()){
486 $dn= $ldap->getDN();
488 /* Save administrative departments */
489 if (in_array_ics("gosaAdministrativeUnit", $attrs['objectClass']) &&
490 isset($attrs['gosaUnitTag'][0])){
491 $administrative[$dn]= $attrs['gosaUnitTag'][0];
492 }
494 if ($dn == $ignore_dn){
495 continue;
496 }
498 /* Only assign non-root departments */
499 if ($dn != $result['/']){
500 $result[convert_department_dn($dn)]= $dn;
501 }
502 }
504 $this->adepartments= $administrative;
505 $this->departments= $result;
506 }
509 function make_idepartments($max_size= 28)
510 {
511 global $config;
512 $base = $config->current['BASE'];
514 $arr = array();
516 $this->idepartments= array();
518 /* Create multidimensional array, with all departments. */
519 foreach ($this->departments as $key => $val){
521 /* remove base from dn */
522 $val2 = str_replace($base,"",$val);
524 /* Get every single ou */
525 $str = preg_replace("/ou=/","|ou=",$val2);
526 $elements = array_reverse(split("\|",$str));
528 /* Save last array position */
529 $last = &$arr;
531 /* Get array depth */
532 $cnt = count($elements);
534 /* Add last ou element of current dn to our array */
535 foreach($elements as $key => $ele){
537 /* skip enpty */
538 if(empty($ele)) continue;
540 /* Extract department name */
541 $elestr = preg_replace("/^ou=/","", $ele);
542 $elestr = preg_replace("/,$/","",$elestr);
544 /* Add to array */
545 if($key == ($cnt-2)){
546 $last[$elestr]['ENTRY'] = $val;
547 }
549 /* Set next array appending position */
550 $last = &$last[$elestr]['SUB'];
551 }
552 }
554 /* Add base entry */
555 $ret["/"]["ENTRY"] = $base;
556 $ret["/"]["SUB"] = $arr;
558 $this->idepartments= $this->generateDepartmentArray($ret,-1,$max_size);
559 }
562 /* Creates display friendly output from make_idepartments */
563 function generateDepartmentArray($arr,$depth = -1,$max_size){
564 $ret = array();
565 $depth ++;
567 /* Walk through array */
568 foreach($arr as $name => $entries){
570 /* If this department is the last in the current tree position
571 * remove it, to avoid generating output for it */
572 if(count($entries['SUB'])==0){
573 unset($entries['SUB']);
574 }
576 /* Fix name, if it contains a replace tag */
577 $name= @LDAP::fix($name);
579 /* Check if current name is too long, then cut it */
580 if(mb_strlen($name, 'UTF-8')> $max_size){
581 $name = mb_substr($name,0,($max_size-3), 'UTF-8')." ...";
582 }
584 /* Append the name to the list */
585 if(isset($entries['ENTRY'])){
586 $a = "";
587 for($i = 0 ; $i < $depth ; $i ++){
588 $a.=" ";
589 }
590 $ret[$entries['ENTRY']]=$a." ".$name;
591 }
593 /* recursive add of subdepartments */
594 if(isset($entries['SUB'])){
595 $ret = array_merge($ret,$this->generateDepartmentArray($entries['SUB'],$depth,$max_size));
596 }
597 }
599 return($ret);
600 }
602 /* This function returns all available Shares defined in this ldap
603 * There are two ways to call this function, if listboxEntry is true
604 * only name and path are attached to the array, in it is false, the whole
605 * entry will be parsed an atached to the result.
606 */
607 function getShareList($listboxEntry = false)
608 {
609 $ldap= $this->get_ldap_link();
610 $a_res = $ldap->search("(objectClass=goShareServer)",array("goExportEntry","cn"));
611 $return= array();
612 while($entry = $ldap->fetch($a_res)){
613 if(isset($entry['goExportEntry']['count'])){
614 unset($entry['goExportEntry']['count']);
615 }
616 if(isset($entry['goExportEntry'])){
617 foreach($entry['goExportEntry'] as $export){
618 $shareAttrs = split("\|",$export);
619 if($listboxEntry) {
620 $return[$shareAttrs[0]."|".$entry['cn'][0]] = $shareAttrs[0]." - ".$entry['cn'][0];
621 }else{
622 $return[$shareAttrs[0]."|".$entry['cn'][0]]['server'] = $entry['cn'][0];
623 $return[$shareAttrs[0]."|".$entry['cn'][0]]['name'] = $shareAttrs[0];
624 $return[$shareAttrs[0]."|".$entry['cn'][0]]['description'] = $shareAttrs[1];
625 $return[$shareAttrs[0]."|".$entry['cn'][0]]['type'] = $shareAttrs[2];
626 $return[$shareAttrs[0]."|".$entry['cn'][0]]['charset'] = $shareAttrs[3];
627 $return[$shareAttrs[0]."|".$entry['cn'][0]]['path'] = $shareAttrs[4];
628 $return[$shareAttrs[0]."|".$entry['cn'][0]]['option'] = $shareAttrs[5];
629 }
630 }
631 }
632 }
633 return($return);
634 }
636 /* This function returns all available ShareServer */
637 function getShareServerList()
638 {
639 $ldap= $this->get_ldap_link();
640 $a_res = $ldap->search("(&(objectClass=goShareServer)(goExportEntry=*))",array("goExportEntry","cn"));
641 $return= array();
642 while($entry = $ldap->fetch($a_res)){
643 if(isset($entry['goExportEntry']['count'])){
644 unset($entry['goExportEntry']['count']);
645 }
646 foreach($entry['goExportEntry'] as $share){
647 $a_share = split("\|",$share);
648 $sharename = $a_share[0];
649 $return[$entry['cn'][0]."|".$sharename] = $entry['cn'][0]." [".$sharename."]";
650 }
651 }
652 return($return);
653 }
655 /* Check if there's the specified bool value set in the configuration */
656 function boolValueIsTrue($section, $value)
657 {
658 $section= strtoupper($section);
659 $value= strtoupper($value);
660 if (isset($this->data[$section][$value])){
662 $data= $this->data[$section][$value];
663 if (preg_match("/^true$/i", $data) || preg_match("/yes/i", $data)){
664 return TRUE;
665 }
667 }
669 return FALSE;
670 }
672 }
674 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
675 ?>