-
dockx thibault authored
Full code style as been reworked to match 1.4
Verified76299fb1
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2003-2010 Cajus Pollmeier
Copyright (C) 2011-2018 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \file class_config.inc
* Source code for the class config
*/
/*!
* \brief This class is responsible for parsing and querying the
* fusiondirectory configuration file.
*/
class config
{
/* XML parser */
var $parser;
var $config_found = FALSE;
var $tags = [];
var $level = 0;
var $gpc = 0;
var $section = '';
var $currentLocation = '';
/*!
* \brief Store configuration for current location
*/
var $current = [];
/* Link to LDAP-server */
var $ldap = NULL;
var $referrals = [];
/*
* \brief Configuration data
*
* - $data['SERVERS'] contains server informations.
*/
var $data = [
'LOCATIONS' => [],
'SERVERS' => [],
'MAIN' => [],
];
var $basedir = '';
/* Keep a copy of the current department list */
var $departments = [];
var $idepartments = [];
var $department_info = [];
var $filename = '';
var $last_modified = 0;
/*!
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
* \brief Class constructor of the config class
*
* \param string $filename path to the configuration file
*
* \param string $basedir base directory
*/
function __construct ($filename, $basedir = '')
{
$this->basedir = $basedir;
/* Parse config file directly? */
if ($filename != '') {
$this->parse($filename);
}
}
/*!
* \brief Check and reload the configuration
*
* This function checks if the configuration has changed, since it was
* read the last time and reloads it. It uses the file mtime to check
* weither the file changed or not.
*/
function check_and_reload ($force = FALSE)
{
/* Check if class_location.inc has changed, this is the case
if we have installed or removed plugins. */
$tmp = stat(CACHE_DIR.'/'.CLASS_CACHE);
if (session::is_set('class_location.inc:timestamp')) {
if ($tmp['mtime'] != session::get('class_location.inc:timestamp')) {
session::un_set('plist');
}
}
session::set('class_location.inc:timestamp', $tmp['mtime']);
if (($this->filename != '') && ((filemtime($this->filename) != $this->last_modified) || $force)) {
$this->config_found = FALSE;
$this->tags = [];
$this->level = 0;
$this->gpc = 0;
$this->section = '';
$this->currentLocation = '';
$this->parse($this->filename);
$this->set_current($this->current['NAME']);
}
}
/*!
* \brief Parse the given configuration file
*
* Parses the configuration file and displays errors if there
* is something wrong with it.
*
* \param string $filename The filename of the configuration file.
*/
function parse ($filename)
{
$this->last_modified = filemtime($filename);
$this->filename = $filename;
$fh = fopen($filename, 'r');
$xmldata = fread($fh, 100000);
fclose($fh);
$this->parse_data($xmldata);
}
function parse_data ($xmldata)
{
$this->data = [
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
'LOCATIONS' => [],
'SERVERS' => [],
'MAIN' => [],
];
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "tag_open", "tag_close");
if (!xml_parse($this->parser, chop($xmldata))) {
$msg = sprintf(_("XML error in fusiondirectory.conf: %s at line %d"),
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser));
msg_dialog::display(_("Configuration error"), $msg, FATAL_ERROR_DIALOG);
exit;
}
xml_parser_free($this->parser);
}
/*!
* \brief Open xml tag when parsing the xml config
*
* \param string $parser
*
* \param string $tag
*
* \param string $attrs
*/
function tag_open ($parser, $tag, $attrs)
{
/* Save last and current tag for reference */
$this->tags[$this->level] = $tag;
$this->level++;
/* Trigger on CONF section */
if ($tag == 'CONF') {
$this->config_found = TRUE;
}
/* Return if we're not in config section */
if (!$this->config_found) {
return;
}
/* yes/no to true/false and upper case TRUE to true and so on*/
foreach ($attrs as $name => $value) {
if (preg_match("/^(true|yes)$/i", $value)) {
$attrs[$name] = "TRUE";
} elseif (preg_match("/^(false|no)$/i", $value)) {
$attrs[$name] = "FALSE";
}
}
/* Look through attributes */
switch ($this->tags[$this->level - 1]) {
/* Handle location */
case 'LOCATION':
if ($this->tags[$this->level - 2] == 'MAIN') {
$attrs['NAME'] = preg_replace('/[<>"\']/', '', $attrs['NAME']);
$this->currentLocation = $attrs['NAME'];
/* Add location elements */
$this->data['LOCATIONS'][$attrs['NAME']] = $attrs;
}
break;
/* Handle referral tags */
case 'REFERRAL':
if ($this->tags[$this->level - 2] == 'LOCATION') {