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 $plugins = array();
37 * Configure and instantiate a plugin into the current configuration.
38 * Class definitions will be loaded from standard paths if necessary.
39 * Note that initialization events won't be fired until later.
41 * @param string $name class name & plugin file/subdir name
42 * @param array $attrs key/value pairs of public attributes to set on plugin instance
44 * @throws ServerException if plugin can't be found
46 public static function addPlugin($name, $attrs = null)
48 $name = ucfirst($name);
49 $pluginclass = "{$name}Plugin";
51 if (!class_exists($pluginclass)) {
53 $files = array("local/plugins/{$pluginclass}.php",
54 "local/plugins/{$name}/{$pluginclass}.php",
55 "local/{$pluginclass}.php",
56 "local/{$name}/{$pluginclass}.php",
57 "plugins/{$pluginclass}.php",
58 "plugins/{$name}/{$pluginclass}.php");
60 foreach ($files as $file) {
61 $fullpath = INSTALLDIR.'/'.$file;
62 if (@file_exists($fullpath)) {
63 include_once($fullpath);
67 if (!class_exists($pluginclass)) {
68 throw new ServerException("Plugin $name not found.", 500);
72 $inst = new $pluginclass();
74 foreach ($attrs as $aname => $avalue) {
75 $inst->$aname = $avalue;
79 // Record activated plugins for later display/config dump
80 self::$plugins[] = array($name, $attrs);
86 * Get a list of activated plugins in this process.
87 * @return array of (string $name, array $args) pairs
89 public static function getActivePlugins()
91 return self::$plugins;
95 * Initialize, or re-initialize, StatusNet global configuration
98 * If switching site configurations during script execution, be
99 * careful when working with leftover objects -- global settings
100 * affect many things and they may not behave as you expected.
102 * @param $server optional web server hostname for picking config
103 * @param $path optional URL path for picking config
104 * @param $conffile optional configuration file path
106 * @throws NoConfigException if config file can't be found
108 public static function init($server=null, $path=null, $conffile=null)
110 StatusNet::initDefaults($server, $path);
111 StatusNet::loadConfigFile($conffile);
113 // Load settings from database; note we need autoload for this
114 Config::loadSettings();
120 * Get identifier of the currently active site configuration
123 public static function currentSite()
125 return common_config('site', 'nickname');
129 * Change site configuration to site specified by nickname,
130 * if set up via Status_network. If not, sites other than
131 * the current will fail horribly.
133 * May throw exception or trigger a fatal error if the given
134 * site is missing or configured incorrectly.
136 * @param string $nickname
138 public static function switchSite($nickname)
140 if ($nickname == StatusNet::currentSite()) {
144 $sn = Status_network::staticGet('nickname', $nickname);
147 throw new Exception("No such site nickname '$nickname'");
150 $server = $sn->getServerName();
151 StatusNet::init($server);
155 * Pull all local sites from status_network table.
157 * Behavior undefined if site is not configured via Status_network.
159 * @return array of nicknames
161 public static function findAllSites()
164 $sn = new Status_network();
166 while ($sn->fetch()) {
167 $sites[] = $sn->nickname;
173 * Fire initialization events for all instantiated plugins.
175 protected static function initPlugins()
177 // Load default plugins
178 foreach (common_config('plugins', 'default') as $name => $params) {
179 $key = 'disable-' . $name;
180 if (common_config('plugins', $key)) {
184 if (is_null($params)) {
186 } else if (is_array($params)) {
187 if (count($params) == 0) {
190 $keys = array_keys($params);
191 if (is_string($keys[0])) {
192 addPlugin($name, $params);
194 foreach ($params as $paramset) {
195 addPlugin($name, $paramset);
202 // XXX: if plugins should check the schema at runtime, do that here.
203 if (common_config('db', 'schemacheck') == 'runtime') {
204 Event::handle('CheckSchema');
207 // Give plugins a chance to initialize in a fully-prepared environment
208 Event::handle('InitializePlugin');
212 * Quick-check if configuration has been established.
213 * Useful for functions which may get used partway through
214 * initialization to back off from fancier things.
218 public function haveConfig()
220 return self::$have_config;
223 public function isApi()
225 return self::$is_api;
228 public function setApi($mode)
230 self::$is_api = $mode;
234 * Build default configuration array
237 protected static function defaultConfig()
239 global $_server, $_path;
240 require(INSTALLDIR.'/lib/default.php');
245 * Establish default configuration based on given or default server and path
246 * Sets global $_server, $_path, and $config
248 public static function initDefaults($server, $path)
250 global $_server, $_path, $config;
252 Event::clearHandlers();
253 self::$plugins = array();
255 // try to figure out where we are. $server and $path
256 // can be set by including module, else we guess based
259 if (isset($server)) {
262 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
263 strtolower($_SERVER['SERVER_NAME']) :
270 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
271 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
275 // Set config values initially to default values
276 $default = self::defaultConfig();
279 // default configuration, overwritten in config.php
280 // Keep DB_DataObject's db config synced to ours...
282 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
284 $config['db'] = $default['db'];
286 // Backward compatibility
288 $config['site']['design'] =& $config['design'];
290 if (function_exists('date_default_timezone_set')) {
291 /* Work internally in UTC */
292 date_default_timezone_set('UTC');
296 protected function _sn_to_path($sn)
298 $past_root = substr($sn, 1);
299 $last_slash = strrpos($past_root, '/');
300 if ($last_slash > 0) {
301 $p = substr($past_root, 0, $last_slash);
309 * Load the default or specified configuration file.
310 * Modifies global $config and may establish plugins.
312 * @throws NoConfigException
314 protected function loadConfigFile($conffile=null)
316 global $_server, $_path, $config;
318 // From most general to most specific:
319 // server-wide, then vhost-wide, then for a path,
320 // finally for a dir (usually only need one of the last two).
322 if (isset($conffile)) {
323 $config_files = array($conffile);
325 $config_files = array('/etc/statusnet/statusnet.php',
326 '/etc/statusnet/laconica.php',
327 '/etc/laconica/laconica.php',
328 '/etc/statusnet/'.$_server.'.php',
329 '/etc/laconica/'.$_server.'.php');
331 if (strlen($_path) > 0) {
332 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
333 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
336 $config_files[] = INSTALLDIR.'/config.php';
339 self::$have_config = false;
341 foreach ($config_files as $_config_file) {
342 if (@file_exists($_config_file)) {
343 // Ignore 0-byte config files
344 if (filesize($_config_file) > 0) {
345 include($_config_file);
346 self::$have_config = true;
351 if (!self::$have_config) {
352 throw new NoConfigException("No configuration file found.",
356 // Fixup for statusnet.ini
357 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
359 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
360 $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
363 // Backwards compatibility
364 if (array_key_exists('memcached', $config)) {
365 if ($config['memcached']['enabled']) {
366 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
369 if (!empty($config['memcached']['base'])) {
370 $config['cache']['base'] = $config['memcached']['base'];
373 if (array_key_exists('xmpp', $config)) {
374 if ($config['xmpp']['enabled']) {
375 addPlugin('xmpp', array(
376 'server' => $config['xmpp']['server'],
377 'port' => $config['xmpp']['port'],
378 'user' => $config['xmpp']['user'],
379 'resource' => $config['xmpp']['resource'],
380 'encryption' => $config['xmpp']['encryption'],
381 'password' => $config['xmpp']['password'],
382 'host' => $config['xmpp']['host'],
383 'debug' => $config['xmpp']['debug'],
384 'public' => $config['xmpp']['public']
391 * Are we running from the web with HTTPS?
393 * @return boolean true if we're running with HTTPS; else false
396 static function isHTTPS()
398 // There are some exceptions to this; add them here!
399 return !empty($_SERVER['HTTPS']);
403 class NoConfigException extends Exception
407 function __construct($msg, $configFiles) {
408 parent::__construct($msg);
409 $this->configFiles = $configFiles;