3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009-2010 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/>.
21 if (!defined('STATUSNET') && !defined('LACONICA')) {
25 global $config, $_server, $_path;
28 * Global configuration setup and management.
32 protected static $have_config;
33 protected static $is_api;
36 * Configure and instantiate a plugin into the current configuration.
37 * Class definitions will be loaded from standard paths if necessary.
38 * Note that initialization events won't be fired until later.
40 * @param string $name class name & plugin file/subdir name
41 * @param array $attrs key/value pairs of public attributes to set on plugin instance
43 * @throws ServerException if plugin can't be found
45 public static function addPlugin($name, $attrs = null)
47 $name = ucfirst($name);
48 $pluginclass = "{$name}Plugin";
50 if (!class_exists($pluginclass)) {
52 $files = array("local/plugins/{$pluginclass}.php",
53 "local/plugins/{$name}/{$pluginclass}.php",
54 "local/{$pluginclass}.php",
55 "local/{$name}/{$pluginclass}.php",
56 "plugins/{$pluginclass}.php",
57 "plugins/{$name}/{$pluginclass}.php");
59 foreach ($files as $file) {
60 $fullpath = INSTALLDIR.'/'.$file;
61 if (@file_exists($fullpath)) {
62 include_once($fullpath);
66 if (!class_exists($pluginclass)) {
67 throw new ServerException("Plugin $name not found.", 500);
71 $inst = new $pluginclass();
73 foreach ($attrs as $aname => $avalue) {
74 $inst->$aname = $avalue;
81 * Initialize, or re-initialize, StatusNet global configuration
84 * If switching site configurations during script execution, be
85 * careful when working with leftover objects -- global settings
86 * affect many things and they may not behave as you expected.
88 * @param $server optional web server hostname for picking config
89 * @param $path optional URL path for picking config
90 * @param $conffile optional configuration file path
92 * @throws NoConfigException if config file can't be found
94 public static function init($server=null, $path=null, $conffile=null)
96 StatusNet::initDefaults($server, $path);
97 StatusNet::loadConfigFile($conffile);
99 // Load settings from database; note we need autoload for this
100 Config::loadSettings();
106 * Get identifier of the currently active site configuration
109 public static function currentSite()
111 return common_config('site', 'nickname');
115 * Change site configuration to site specified by nickname,
116 * if set up via Status_network. If not, sites other than
117 * the current will fail horribly.
119 * May throw exception or trigger a fatal error if the given
120 * site is missing or configured incorrectly.
122 * @param string $nickname
124 public static function switchSite($nickname)
126 if ($nickname == StatusNet::currentSite()) {
130 $sn = Status_network::staticGet($nickname);
133 throw new Exception("No such site nickname '$nickname'");
136 $server = $sn->getServerName();
137 StatusNet::init($server);
141 * Pull all local sites from status_network table.
143 * Behavior undefined if site is not configured via Status_network.
145 * @return array of nicknames
147 public static function findAllSites()
150 $sn = new Status_network();
152 while ($sn->fetch()) {
153 $sites[] = $sn->nickname;
160 * Fire initialization events for all instantiated plugins.
162 protected static function initPlugins()
164 // Load default plugins
165 foreach (common_config('plugins', 'default') as $name => $params) {
166 $key = 'disable-' . $name;
167 if (common_config('plugins', $key)) {
171 if (is_null($params)) {
173 } else if (is_array($params)) {
174 if (count($params) == 0) {
177 $keys = array_keys($params);
178 if (is_string($keys[0])) {
179 addPlugin($name, $params);
181 foreach ($params as $paramset) {
182 addPlugin($name, $paramset);
189 // XXX: if plugins should check the schema at runtime, do that here.
190 if (common_config('db', 'schemacheck') == 'runtime') {
191 Event::handle('CheckSchema');
194 // Give plugins a chance to initialize in a fully-prepared environment
195 Event::handle('InitializePlugin');
199 * Quick-check if configuration has been established.
200 * Useful for functions which may get used partway through
201 * initialization to back off from fancier things.
205 public function haveConfig()
207 return self::$have_config;
210 public function isApi()
212 return self::$is_api;
215 public function setApi($mode)
217 self::$is_api = $mode;
221 * Build default configuration array
224 protected static function defaultConfig()
226 global $_server, $_path;
227 require(INSTALLDIR.'/lib/default.php');
232 * Establish default configuration based on given or default server and path
233 * Sets global $_server, $_path, and $config
235 protected static function initDefaults($server, $path)
237 global $_server, $_path, $config;
239 Event::clearHandlers();
241 // try to figure out where we are. $server and $path
242 // can be set by including module, else we guess based
245 if (isset($server)) {
248 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
249 strtolower($_SERVER['SERVER_NAME']) :
256 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
257 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
261 // Set config values initially to default values
262 $default = self::defaultConfig();
265 // default configuration, overwritten in config.php
266 // Keep DB_DataObject's db config synced to ours...
268 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
270 $config['db'] = $default['db'];
272 // Backward compatibility
274 $config['site']['design'] =& $config['design'];
276 if (function_exists('date_default_timezone_set')) {
277 /* Work internally in UTC */
278 date_default_timezone_set('UTC');
282 protected function _sn_to_path($sn)
284 $past_root = substr($sn, 1);
285 $last_slash = strrpos($past_root, '/');
286 if ($last_slash > 0) {
287 $p = substr($past_root, 0, $last_slash);
295 * Load the default or specified configuration file.
296 * Modifies global $config and may establish plugins.
298 * @throws NoConfigException
300 protected function loadConfigFile($conffile=null)
302 global $_server, $_path, $config;
304 // From most general to most specific:
305 // server-wide, then vhost-wide, then for a path,
306 // finally for a dir (usually only need one of the last two).
308 if (isset($conffile)) {
309 $config_files = array($conffile);
311 $config_files = array('/etc/statusnet/statusnet.php',
312 '/etc/statusnet/laconica.php',
313 '/etc/laconica/laconica.php',
314 '/etc/statusnet/'.$_server.'.php',
315 '/etc/laconica/'.$_server.'.php');
317 if (strlen($_path) > 0) {
318 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
319 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
322 $config_files[] = INSTALLDIR.'/config.php';
325 self::$have_config = false;
327 foreach ($config_files as $_config_file) {
328 if (@file_exists($_config_file)) {
329 include($_config_file);
330 self::$have_config = true;
334 if (!self::$have_config) {
335 throw new NoConfigException("No configuration file found.",
339 // Fixup for statusnet.ini
340 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
342 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
343 $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
346 // Backwards compatibility
347 if (array_key_exists('memcached', $config)) {
348 if ($config['memcached']['enabled']) {
349 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
352 if (!empty($config['memcached']['base'])) {
353 $config['cache']['base'] = $config['memcached']['base'];
356 if (array_key_exists('xmpp', $config)) {
357 if ($config['xmpp']['enabled']) {
358 addPlugin('xmpp', array(
359 'server' => $config['xmpp']['server'],
360 'port' => $config['xmpp']['port'],
361 'user' => $config['xmpp']['user'],
362 'resource' => $config['xmpp']['resource'],
363 'encryption' => $config['xmpp']['encryption'],
364 'password' => $config['xmpp']['password'],
365 'host' => $config['xmpp']['host'],
366 'debug' => $config['xmpp']['debug'],
367 'public' => $config['xmpp']['public']
374 class NoConfigException extends Exception
378 function __construct($msg, $configFiles) {
379 parent::__construct($msg);
380 $this->configFiles = $configFiles;