summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Config.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2011-07-21 21:32:48 +0200
committerChristian Weiske <cweiske@cweiske.de>2011-07-21 21:32:48 +0200
commit82ee59779ea9a5d2d9234e622f56cfcc4c22ff3a (patch)
tree8dbaba53a257464942d71c66a559944b949fdb9b /src/SemanticScuttle/Config.php
parent846e3a38cf049266a33cb3e553fcacbcbdc0bfa3 (diff)
downloadsemanticscuttle-82ee59779ea9a5d2d9234e622f56cfcc4c22ff3a.tar.gz
semanticscuttle-82ee59779ea9a5d2d9234e622f56cfcc4c22ff3a.tar.bz2
support global and per-host configuration files
Diffstat (limited to 'src/SemanticScuttle/Config.php')
-rw-r--r--src/SemanticScuttle/Config.php109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/SemanticScuttle/Config.php b/src/SemanticScuttle/Config.php
new file mode 100644
index 0000000..0773310
--- /dev/null
+++ b/src/SemanticScuttle/Config.php
@@ -0,0 +1,109 @@
+<?php
+/**
+ * SemanticScuttle - your social bookmark manager.
+ *
+ * PHP version 5.
+ *
+ * @category Bookmarking
+ * @package SemanticScuttle
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license AGPL http://www.gnu.org/licenses/agpl.html
+ * @link http://sourceforge.net/projects/semanticscuttle
+ */
+
+/**
+ * Configuration handling
+ *
+ * @category Bookmarking
+ * @package SemanticScuttle
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @license AGPL http://www.gnu.org/licenses/agpl.html
+ * @link http://sourceforge.net/projects/semanticscuttle
+ */
+class SemanticScuttle_Config
+{
+ /**
+ * Prefix for configuration files.
+ * Used to inject stream wrapper protocol for unit testing
+ *
+ * @var string
+ */
+ public $filePrefix = '';
+
+
+
+ /**
+ * Finds the correct data directory
+ *
+ * @return string Full path to the data directory with a trailing slash
+ */
+ protected function getDataDir()
+ {
+ if ('@data_dir@' == '@' . 'data_dir@') {
+ //non pear-install
+ $datadir = dirname(__FILE__) . '/../../data/';
+ } else {
+ //pear installation; files are in include path
+ $datadir = '@data_dir@/SemanticScuttle/';
+ }
+
+ return $datadir;
+ }
+
+
+
+ /**
+ * Tries to find a configuration file by looking in different
+ * places:
+ * - pear data_dir/SemanticScuttle/config-$hostname.php
+ * - pear data_dir/SemanticScuttle/config.php
+ * - /etc/semanticscuttle/config-$hostname.php
+ * - /etc/semanticscuttle/config.php
+ *
+ * Paths with host name have priority.
+ *
+ * @return array Array with config file path as first value
+ * and default config file path as second value.
+ * Any may be NULL if not found
+ */
+ public function findFiles()
+ {
+ //use basename to prevent path injection
+ $host = basename($_SERVER['HTTP_HOST']);
+ $datadir = $this->getDataDir();
+
+ $arFiles = array(
+ $datadir . 'config.' . $host . '.php',
+ '/etc/semanticscuttle/config.' . $host . '.php',
+ $datadir . 'config.php',
+ '/etc/semanticscuttle/config.php',
+ );
+
+ $configfile = null;
+ foreach ($arFiles as $file) {
+ if (file_exists($this->filePrefix . $file)) {
+ $configfile = $file;
+ break;
+ }
+ }
+
+ //find default file
+ $arDefaultFiles = array_unique(
+ array(
+ substr($configfile, 0, -3) . 'default.php',
+ $datadir . 'config.default.php',
+ '/etc/semanticscuttle/config.default.php',
+ )
+ );
+ $defaultfile = null;
+ foreach ($arDefaultFiles as $file) {
+ if (file_exists($this->filePrefix . $file)) {
+ $defaultfile = $file;
+ break;
+ }
+ }
+ return array($configfile, $defaultfile);
+ }
+}
+
+?> \ No newline at end of file