3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
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 Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 global $config, $_server, $_path;
25 * Global configuration setup and management.
29 protected static $have_config;
32 * Configure and instantiate a plugin into the current configuration.
33 * Class definitions will be loaded from standard paths if necessary.
34 * Note that initialization events won't be fired until later.
36 * @param string $name class name & plugin file/subdir name
37 * @param array $attrs key/value pairs of public attributes to set on plugin instance
39 * @throws ServerException if plugin can't be found
41 public static function addPlugin($name, $attrs = null)
43 $name = ucfirst($name);
44 $pluginclass = "{$name}Plugin";
46 if (!class_exists($pluginclass)) {
48 $files = array("local/plugins/{$pluginclass}.php",
49 "local/plugins/{$name}/{$pluginclass}.php",
50 "local/{$pluginclass}.php",
51 "local/{$name}/{$pluginclass}.php",
52 "plugins/{$pluginclass}.php",
53 "plugins/{$name}/{$pluginclass}.php");
55 foreach ($files as $file) {
56 $fullpath = INSTALLDIR.'/'.$file;
57 if (@file_exists($fullpath)) {
58 include_once($fullpath);
62 if (!class_exists($pluginclass)) {
63 throw new ServerException(500, "Plugin $name not found.");
67 $inst = new $pluginclass();
69 foreach ($attrs as $aname => $avalue) {
70 $inst->$aname = $avalue;
77 * Initialize, or re-initialize, StatusNet global configuration
80 * If switching site configurations during script execution, be
81 * careful when working with leftover objects -- global settings
82 * affect many things and they may not behave as you expected.
84 * @param $server optional web server hostname for picking config
85 * @param $path optional URL path for picking config
86 * @param $conffile optional configuration file path
88 * @throws NoConfigException if config file can't be found
90 public static function init($server=null, $path=null, $conffile=null)
92 StatusNet::initDefaults($server, $path);
93 StatusNet::loadConfigFile($conffile);
95 // Load settings from database; note we need autoload for this
96 Config::loadSettings();
102 * Fire initialization events for all instantiated plugins.
104 protected static function initPlugins()
106 // Load default plugins
107 foreach (common_config('plugins', 'default') as $name => $params) {
108 if (is_null($params)) {
110 } else if (is_array($params)) {
111 if (count($params) == 0) {
114 $keys = array_keys($params);
115 if (is_string($keys[0])) {
116 addPlugin($name, $params);
118 foreach ($params as $paramset) {
119 addPlugin($name, $paramset);
126 // XXX: if plugins should check the schema at runtime, do that here.
127 if (common_config('db', 'schemacheck') == 'runtime') {
128 Event::handle('CheckSchema');
131 // Give plugins a chance to initialize in a fully-prepared environment
132 Event::handle('InitializePlugin');
136 * Quick-check if configuration has been established.
137 * Useful for functions which may get used partway through
138 * initialization to back off from fancier things.
142 public function haveConfig()
144 return self::$have_config;
148 * Build default configuration array
151 protected static function defaultConfig()
153 global $_server, $_path;
154 require(INSTALLDIR.'/lib/default.php');
159 * Establish default configuration based on given or default server and path
160 * Sets global $_server, $_path, and $config
162 protected static function initDefaults($server, $path)
164 global $_server, $_path, $config;
166 Event::clearHandlers();
168 // try to figure out where we are. $server and $path
169 // can be set by including module, else we guess based
172 if (isset($server)) {
175 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
176 strtolower($_SERVER['SERVER_NAME']) :
183 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
184 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
188 // Set config values initially to default values
189 $default = self::defaultConfig();
192 // default configuration, overwritten in config.php
193 // Keep DB_DataObject's db config synced to ours...
195 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
197 $config['db'] = $default['db'];
199 // Backward compatibility
201 $config['site']['design'] =& $config['design'];
203 if (function_exists('date_default_timezone_set')) {
204 /* Work internally in UTC */
205 date_default_timezone_set('UTC');
209 protected function _sn_to_path($sn)
211 $past_root = substr($sn, 1);
212 $last_slash = strrpos($past_root, '/');
213 if ($last_slash > 0) {
214 $p = substr($past_root, 0, $last_slash);
222 * Load the default or specified configuration file.
223 * Modifies global $config and may establish plugins.
225 * @throws NoConfigException
227 protected function loadConfigFile($conffile=null)
229 global $_server, $_path, $config;
231 // From most general to most specific:
232 // server-wide, then vhost-wide, then for a path,
233 // finally for a dir (usually only need one of the last two).
235 if (isset($conffile)) {
236 $config_files = array($conffile);
238 $config_files = array('/etc/statusnet/statusnet.php',
239 '/etc/statusnet/laconica.php',
240 '/etc/laconica/laconica.php',
241 '/etc/statusnet/'.$_server.'.php',
242 '/etc/laconica/'.$_server.'.php');
244 if (strlen($_path) > 0) {
245 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
246 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
249 $config_files[] = INSTALLDIR.'/config.php';
252 self::$have_config = false;
254 foreach ($config_files as $_config_file) {
255 if (@file_exists($_config_file)) {
256 include($_config_file);
257 self::$have_config = true;
261 if (!self::$have_config) {
262 throw new NoConfigException("No configuration file found.",
266 // Fixup for statusnet.ini
267 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
269 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
270 $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
273 // Backwards compatibility
275 if (array_key_exists('memcached', $config)) {
276 if ($config['memcached']['enabled']) {
277 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
280 if (!empty($config['memcached']['base'])) {
281 $config['cache']['base'] = $config['memcached']['base'];
287 class NoConfigException extends Exception
289 public $config_files;
291 function __construct($msg, $config_files) {
292 parent::__construct($msg);
293 $this->config_files = $config_files;