From 5cffb1dd17d7f1a6286f133b5ee5f736ab914f88 Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Sat, 1 Dec 2007 20:30:41 +0000
Subject: [PATCH] fixes #11  * the config file handling code has been moved to
 a separate file which is shared between normal and AJAX code  * the config
 file existence is checked and a well formatted error message is sent if it
 doesn't exist

---
 admin/common.inc.php | 15 ++----------
 admin/getgroups.php  | 14 ++----------
 admin/shared.inc.php | 54 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+), 25 deletions(-)
 create mode 100644 admin/shared.inc.php

diff --git a/admin/common.inc.php b/admin/common.inc.php
index 739a579..0c29f1b 100644
--- a/admin/common.inc.php
+++ b/admin/common.inc.php
@@ -27,19 +27,8 @@
  * 02110-1301 USA.
  */
 
-/** Include common internationalization code. */
-require_once("i18n.inc.php");
-
-if (!isset($_SERVER['DavAdminConfDir'])) {
-  header('HTTP/1.0 500 Internal Server Error');
-  header('Status: 500 Internal Server Error');
-  header('Content-Type: text/plain;charset=utf8');
-  print(_("The Server is not configured correctly. Please tell your Administrator to set the DavAdminConfDir environment variable."));
-  exit();
-}
-
-/** Include configuration information. */
-require_once($_SERVER['DavAdminConfDir'] . '/config.inc.php');
+/** Include the code shared between normal pages and AJAX. */
+require_once("shared.inc.php");
 
 /** DAV administrator group name. */
 define(ADMIN_GROUP, 'davadmin');
diff --git a/admin/getgroups.php b/admin/getgroups.php
index 59237f8..7ce865a 100644
--- a/admin/getgroups.php
+++ b/admin/getgroups.php
@@ -27,18 +27,8 @@
  * 02110-1301 USA.
  */
 
-/** Include common internationalization code. */
-require_once("i18n.inc.php");
-
-if (!isset($_SERVER['DavAdminConfDir'])) {
-  header('HTTP/1.0 500 Internal Server Error');
-  header('Status: 500 Internal Server Error');
-  header('Content-Type: text/plain;charset=utf8');
-  print(_("The Server is not configured correctly. Please tell your Administrator to set the DavAdminConfDir environment variable."));
-  exit();
-}
-/** Include configuration information. */
-require_once($_SERVER['DavAdminConfDir'] . '/config.inc.php');
+/** Include the code shared between normal pages and AJAX. */
+require_once("shared.inc.php");
 
 // output is plain text (JSON or an error message)
 header("Content-Type: text/plain; charset=UTF-8");
diff --git a/admin/shared.inc.php b/admin/shared.inc.php
new file mode 100644
index 0000000..39d7c99
--- /dev/null
+++ b/admin/shared.inc.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Code shared between normal pages and AJAX for DAVAdmin.
+ *
+ * @author Jan Dittberner <jan@dittberner.info>
+ * @version $Id$
+ * @license GPL
+ * @package DAVAdmin
+ *
+ * Copyright (c) 2007 Jan Dittberner
+ *
+ * This file is part of DAVAdmin.
+ *
+ * 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 Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+
+/** Include common internationalization code. */
+require_once("i18n.inc.php");
+
+function _server_error($message) {
+  header('HTTP/1.0 500 Internal Server Error');
+  header('Status: 500 Internal Server Error');
+  header('Content-Type: text/plain;charset=utf8');
+  print($message);
+  exit();
+}
+
+if (!isset($_SERVER['DavAdminConfDir'])) {
+  _server_error(_("The Server is not configured correctly. Please tell your Administrator to set the DavAdminConfDir environment variable."));
+}
+
+$confname = $_SERVER['DavAdminConfDir'] . DIRECTORY_SEPARATOR .
+'config.inc.php';
+
+if (!file_exists($confname)) {
+  _server_error(sprintf(_("The server configuration file '%s' doesn't exist. Please create the file with correct settings."), $confname));
+}
+
+/** Include configuration information. */
+require_once($confname);
+?>
\ No newline at end of file