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;
34 protected static $is_ajax;
35 protected static $plugins = array();
38 * Configure and instantiate a plugin into the current configuration.
39 * Class definitions will be loaded from standard paths if necessary.
40 * Note that initialization events won't be fired until later.
42 * @param string $name class name & plugin file/subdir name
43 * @param array $attrs key/value pairs of public attributes to set on plugin instance
45 * @throws ServerException if plugin can't be found
47 public static function addPlugin($name, $attrs = null)
49 $name = ucfirst($name);
50 $pluginclass = "{$name}Plugin";
52 if (!class_exists($pluginclass)) {
54 $files = array("local/plugins/{$pluginclass}.php",
55 "local/plugins/{$name}/{$pluginclass}.php",
56 "local/{$pluginclass}.php",
57 "local/{$name}/{$pluginclass}.php",
58 "plugins/{$pluginclass}.php",
59 "plugins/{$name}/{$pluginclass}.php");
61 foreach ($files as $file) {
62 $fullpath = INSTALLDIR.'/'.$file;
63 if (@file_exists($fullpath)) {
64 include_once($fullpath);
68 if (!class_exists($pluginclass)) {
69 throw new ServerException("Plugin $name not found.", 500);
73 $inst = new $pluginclass();
75 foreach ($attrs as $aname => $avalue) {
76 $inst->$aname = $avalue;
80 // Record activated plugins for later display/config dump
81 self::$plugins[] = array($name, $attrs);
87 * Get a list of activated plugins in this process.
88 * @return array of (string $name, array $args) pairs
90 public static function getActivePlugins()
92 return self::$plugins;
96 * Initialize, or re-initialize, StatusNet global configuration
99 * If switching site configurations during script execution, be
100 * careful when working with leftover objects -- global settings
101 * affect many things and they may not behave as you expected.
103 * @param $server optional web server hostname for picking config
104 * @param $path optional URL path for picking config
105 * @param $conffile optional configuration file path
107 * @throws NoConfigException if config file can't be found
109 public static function init($server=null, $path=null, $conffile=null)
113 StatusNet::initDefaults($server, $path);
114 StatusNet::loadConfigFile($conffile);
116 $sprofile = common_config('site', 'profile');
117 if (!empty($sprofile)) {
118 StatusNet::loadSiteProfile($sprofile);
120 // Load settings from database; note we need autoload for this
121 Config::loadSettings();
127 * Get identifier of the currently active site configuration
130 public static function currentSite()
132 return common_config('site', 'nickname');
136 * Change site configuration to site specified by nickname,
137 * if set up via Status_network. If not, sites other than
138 * the current will fail horribly.
140 * May throw exception or trigger a fatal error if the given
141 * site is missing or configured incorrectly.
143 * @param string $nickname
145 public static function switchSite($nickname)
147 if ($nickname == StatusNet::currentSite()) {
151 $sn = Status_network::getKV('nickname', $nickname);
154 throw new Exception("No such site nickname '$nickname'");
157 $server = $sn->getServerName();
158 StatusNet::init($server);
162 * Pull all local sites from status_network table.
164 * Behavior undefined if site is not configured via Status_network.
166 * @return array of nicknames
168 public static function findAllSites()
171 $sn = new Status_network();
173 while ($sn->fetch()) {
174 $sites[] = $sn->nickname;
180 * Fire initialization events for all instantiated plugins.
182 protected static function initPlugins()
185 foreach (common_config('plugins', 'core') as $name => $params) {
186 call_user_func('addPlugin', $name, $params);
189 // Load default plugins
190 foreach (common_config('plugins', 'default') as $name => $params) {
191 $key = 'disable-' . $name;
192 if (common_config('plugins', $key)) {
196 if (is_null($params)) {
198 } else if (is_array($params)) {
199 if (count($params) == 0) {
202 $keys = array_keys($params);
203 if (is_string($keys[0])) {
204 addPlugin($name, $params);
206 foreach ($params as $paramset) {
207 addPlugin($name, $paramset);
214 // XXX: if plugins should check the schema at runtime, do that here.
215 if (common_config('db', 'schemacheck') == 'runtime') {
216 Event::handle('CheckSchema');
219 // Give plugins a chance to initialize in a fully-prepared environment
220 Event::handle('InitializePlugin');
224 * Quick-check if configuration has been established.
225 * Useful for functions which may get used partway through
226 * initialization to back off from fancier things.
230 public static function haveConfig()
232 return self::$have_config;
235 public static function isApi()
237 return self::$is_api;
240 public static function setApi($mode)
242 self::$is_api = $mode;
245 public static function isAjax()
247 return self::$is_ajax;
250 public static function setAjax($mode)
252 self::$is_ajax = $mode;
256 * Build default configuration array
259 protected static function defaultConfig()
261 global $_server, $_path;
262 require(INSTALLDIR.'/lib/default.php');
267 * Establish default configuration based on given or default server and path
268 * Sets global $_server, $_path, and $config
270 public static function initDefaults($server, $path)
272 global $_server, $_path, $config, $_PEAR;
274 Event::clearHandlers();
275 self::$plugins = array();
277 // try to figure out where we are. $server and $path
278 // can be set by including module, else we guess based
281 if (isset($server)) {
284 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
285 strtolower($_SERVER['SERVER_NAME']) :
292 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
293 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
297 // Set config values initially to default values
298 $default = self::defaultConfig();
301 // default configuration, overwritten in config.php
302 // Keep DB_DataObject's db config synced to ours...
304 $config['db'] = &$_PEAR->getStaticProperty('DB_DataObject','options');
306 $config['db'] = $default['db'];
308 if (function_exists('date_default_timezone_set')) {
309 /* Work internally in UTC */
310 date_default_timezone_set('UTC');
314 public static function loadSiteProfile($name)
317 $settings = SiteProfile::getSettings($name);
318 $config = array_merge($config, $settings);
321 protected static function _sn_to_path($sn)
323 $past_root = substr($sn, 1);
324 $last_slash = strrpos($past_root, '/');
325 if ($last_slash > 0) {
326 $p = substr($past_root, 0, $last_slash);
334 * Load the default or specified configuration file.
335 * Modifies global $config and may establish plugins.
337 * @throws NoConfigException
339 protected static function loadConfigFile($conffile=null)
341 global $_server, $_path, $config;
343 // From most general to most specific:
344 // server-wide, then vhost-wide, then for a path,
345 // finally for a dir (usually only need one of the last two).
347 if (isset($conffile)) {
348 $config_files = array($conffile);
350 $config_files = array('/etc/statusnet/statusnet.php',
351 '/etc/statusnet/laconica.php',
352 '/etc/laconica/laconica.php',
353 '/etc/statusnet/'.$_server.'.php',
354 '/etc/laconica/'.$_server.'.php');
356 if (strlen($_path) > 0) {
357 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
358 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
361 $config_files[] = INSTALLDIR.'/config.php';
364 self::$have_config = false;
366 foreach ($config_files as $_config_file) {
367 if (@file_exists($_config_file)) {
368 // Ignore 0-byte config files
369 if (filesize($_config_file) > 0) {
370 common_log(LOG_INFO, "Including config file: " . $_config_file);
371 include($_config_file);
372 self::$have_config = true;
377 if (!self::$have_config) {
378 throw new NoConfigException("No configuration file found.",
382 // Backwards compatibility
383 if (array_key_exists('memcached', $config)) {
384 if ($config['memcached']['enabled']) {
385 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
388 if (!empty($config['memcached']['base'])) {
389 $config['cache']['base'] = $config['memcached']['base'];
393 if (array_key_exists('xmpp', $config)) {
394 if ($config['xmpp']['enabled']) {
395 addPlugin('xmpp', array(
396 'server' => $config['xmpp']['server'],
397 'port' => $config['xmpp']['port'],
398 'user' => $config['xmpp']['user'],
399 'resource' => $config['xmpp']['resource'],
400 'encryption' => $config['xmpp']['encryption'],
401 'password' => $config['xmpp']['password'],
402 'host' => $config['xmpp']['host'],
403 'debug' => $config['xmpp']['debug'],
404 'public' => $config['xmpp']['public']
409 // Check for database server; must exist!
411 if (empty($config['db']['database'])) {
412 throw new ServerException("No database server for this site.");
417 * Are we running from the web with HTTPS?
419 * @return boolean true if we're running with HTTPS; else false
422 static function isHTTPS()
424 // There are some exceptions to this; add them here!
425 if(empty($_SERVER['HTTPS'])) {
428 return $_SERVER['HTTPS'] !== 'off';
433 class NoConfigException extends Exception
437 function __construct($msg, $configFiles) {
438 parent::__construct($msg);
439 $this->configFiles = $configFiles;