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('GNUSOCIAL')) { exit(1); }
23 global $config, $_server, $_path;
26 * Global configuration setup and management.
30 protected static $config_files = array();
31 protected static $have_config;
32 protected static $is_api;
33 protected static $is_ajax;
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, array $attrs=array())
48 $name = ucfirst($name);
50 if (isset(self::$plugins[$name])) {
51 // We have already loaded this plugin. Don't try to
52 // do it again with (possibly) different values.
53 // Försten till kvarn får mala.
57 $pluginclass = "{$name}Plugin";
59 if (!class_exists($pluginclass)) {
61 $files = array("local/plugins/{$pluginclass}.php",
62 "local/plugins/{$name}/{$pluginclass}.php",
63 "local/{$pluginclass}.php",
64 "local/{$name}/{$pluginclass}.php",
65 "plugins/{$pluginclass}.php",
66 "plugins/{$name}/{$pluginclass}.php");
68 foreach ($files as $file) {
69 $fullpath = INSTALLDIR.'/'.$file;
70 if (@file_exists($fullpath)) {
71 include_once($fullpath);
75 if (!class_exists($pluginclass)) {
76 throw new ServerException("Plugin $name not found.", 500);
80 // Doesn't this $inst risk being garbage collected or something?
81 // TODO: put into a static array that makes sure $inst isn't lost.
82 $inst = new $pluginclass();
83 foreach ($attrs as $aname => $avalue) {
84 $inst->$aname = $avalue;
87 // Record activated plugins for later display/config dump
88 self::$plugins[$name] = $attrs;
93 public static function delPlugin($name)
95 // Remove our plugin if it was previously loaded
96 $name = ucfirst($name);
97 if (isset(self::$plugins[$name])) {
98 unset(self::$plugins[$name]);
101 // make sure initPlugins will avoid this
102 common_config_set('plugins', 'disable-'.$name, true);
108 * Get a list of activated plugins in this process.
109 * @return array of (string $name, array $args) pairs
111 public static function getActivePlugins()
113 return self::$plugins;
117 * Initialize, or re-initialize, GNU social global configuration
120 * If switching site configurations during script execution, be
121 * careful when working with leftover objects -- global settings
122 * affect many things and they may not behave as you expected.
124 * @param $server optional web server hostname for picking config
125 * @param $path optional URL path for picking config
126 * @param $conffile optional configuration file path
128 * @throws NoConfigException if config file can't be found
130 public static function init($server=null, $path=null, $conffile=null)
134 self::initDefaults($server, $path);
135 self::loadConfigFile($conffile);
137 $sprofile = common_config('site', 'profile');
138 if (!empty($sprofile)) {
139 self::loadSiteProfile($sprofile);
141 // Load settings from database; note we need autoload for this
142 Config::loadSettings();
144 self::verifyLoadedConfig();
150 * Get identifier of the currently active site configuration
153 public static function currentSite()
155 return common_config('site', 'nickname');
159 * Change site configuration to site specified by nickname,
160 * if set up via Status_network. If not, sites other than
161 * the current will fail horribly.
163 * May throw exception or trigger a fatal error if the given
164 * site is missing or configured incorrectly.
166 * @param string $nickname
168 public static function switchSite($nickname)
170 if ($nickname == self::currentSite()) {
174 $sn = Status_network::getKV('nickname', $nickname);
177 throw new Exception("No such site nickname '$nickname'");
180 $server = $sn->getServerName();
185 * Pull all local sites from status_network table.
187 * Behavior undefined if site is not configured via Status_network.
189 * @return array of nicknames
191 public static function findAllSites()
194 $sn = new Status_network();
196 while ($sn->fetch()) {
197 $sites[] = $sn->nickname;
203 * Fire initialization events for all instantiated plugins.
205 protected static function initPlugins()
207 // User config may have already added some of these plugins, with
208 // maybe configured parameters. The self::addPlugin function will
209 // ignore the new call if it has already been instantiated.
212 foreach (common_config('plugins', 'core') as $name => $params) {
213 call_user_func('self::addPlugin', $name, $params);
216 // Load default plugins
217 foreach (common_config('plugins', 'default') as $name => $params) {
218 $key = 'disable-' . $name;
219 if (common_config('plugins', $key)) {
223 // TODO: We should be able to avoid this is_null and assume $params
224 // is an array, since that's how it is typed in addPlugin
225 if (is_null($params)) {
226 self::addPlugin($name);
227 } else if (is_array($params)) {
228 if (count($params) == 0) {
229 self::addPlugin($name);
231 $keys = array_keys($params);
232 if (is_string($keys[0])) {
233 self::addPlugin($name, $params);
235 foreach ($params as $paramset) {
236 self::addPlugin($name, $paramset);
243 // XXX: if plugins should check the schema at runtime, do that here.
244 if (common_config('db', 'schemacheck') == 'runtime') {
245 Event::handle('CheckSchema');
248 // Give plugins a chance to initialize in a fully-prepared environment
249 Event::handle('InitializePlugin');
253 * Quick-check if configuration has been established.
254 * Useful for functions which may get used partway through
255 * initialization to back off from fancier things.
259 public static function haveConfig()
261 return self::$have_config;
265 * Returns a list of configuration files that have
266 * been loaded for this instance of GNU social.
268 public static function configFiles()
270 return self::$config_files;
273 public static function isApi()
275 return self::$is_api;
278 public static function setApi($mode)
280 self::$is_api = $mode;
283 public static function isAjax()
285 return self::$is_ajax;
288 public static function setAjax($mode)
290 self::$is_ajax = $mode;
294 * Build default configuration array
297 protected static function defaultConfig()
299 global $_server, $_path;
300 require(INSTALLDIR.'/lib/default.php');
305 * Establish default configuration based on given or default server and path
306 * Sets global $_server, $_path, and $config
308 public static function initDefaults($server, $path)
310 global $_server, $_path, $config, $_PEAR;
312 Event::clearHandlers();
313 self::$plugins = array();
315 // try to figure out where we are. $server and $path
316 // can be set by including module, else we guess based
319 if (isset($server)) {
322 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
323 strtolower($_SERVER['SERVER_NAME']) :
330 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
331 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
335 // Set config values initially to default values
336 $default = self::defaultConfig();
339 // default configuration, overwritten in config.php
340 // Keep DB_DataObject's db config synced to ours...
342 $config['db'] = &$_PEAR->getStaticProperty('DB_DataObject','options');
344 $config['db'] = $default['db'];
346 if (function_exists('date_default_timezone_set')) {
347 /* Work internally in UTC */
348 date_default_timezone_set('UTC');
352 public static function loadSiteProfile($name)
355 $settings = SiteProfile::getSettings($name);
356 $config = array_replace_recursive($config, $settings);
359 protected static function _sn_to_path($sn)
361 $past_root = substr($sn, 1);
362 $last_slash = strrpos($past_root, '/');
363 if ($last_slash > 0) {
364 $p = substr($past_root, 0, $last_slash);
372 * Load the default or specified configuration file.
373 * Modifies global $config and may establish plugins.
375 * @throws NoConfigException
377 protected static function loadConfigFile($conffile=null)
379 global $_server, $_path, $config;
381 // From most general to most specific:
382 // server-wide, then vhost-wide, then for a path,
383 // finally for a dir (usually only need one of the last two).
385 if (isset($conffile)) {
386 $config_files = array($conffile);
388 $config_files = array('/etc/gnusocial/config.php',
389 '/etc/gnusocial/config.d/'.$_server.'.php');
391 if (strlen($_path) > 0) {
392 $config_files[] = '/etc/gnusocial/config.d/'.$_server.'_'.$_path.'.php';
395 $config_files[] = INSTALLDIR.'/config.php';
398 self::$have_config = false;
400 foreach ($config_files as $_config_file) {
401 if (@file_exists($_config_file)) {
402 // Ignore 0-byte config files
403 if (filesize($_config_file) > 0) {
404 include($_config_file);
405 self::$config_files[] = $_config_file;
406 self::$have_config = true;
411 if (!self::$have_config) {
412 throw new NoConfigException("No configuration file found.",
416 // Check for database server; must exist!
418 if (empty($config['db']['database'])) {
419 throw new ServerException("No database server for this site.");
424 * Verify that the loaded config is good. Not complete, but will
425 * throw exceptions on common configuration problems I hope.
427 * Might make changes to the filesystem, to created dirs, but will
428 * not make database changes.
430 static function verifyLoadedConfig()
432 if (common_config('htmlpurifier', 'Cache.DefinitionImpl') === 'Serializer'
433 && !is_dir(common_config('htmlpurifier', 'Cache.SerializerPath'))) {
434 if (!mkdir(common_config('htmlpurifier', 'Cache.SerializerPath'))) {
435 throw new ServerException('Could not create HTMLPurifier cache dir: '._ve(common_config('htmlpurifier', 'Cache.SerializerPath')));
441 * Are we running from the web with HTTPS?
443 * @return boolean true if we're running with HTTPS; else false
446 static function isHTTPS()
448 if (common_config('site', 'sslproxy')) {
452 // There are some exceptions to this; add them here!
453 if (empty($_SERVER['HTTPS'])) {
457 // If it is _not_ "off", it is on, so "true".
458 return strtolower($_SERVER['HTTPS']) !== 'off';
462 * Can we use HTTPS? Then do! Only return false if it's not configured ("never").
464 static function useHTTPS()
466 return self::isHTTPS() || common_config('site', 'ssl') != 'never';
470 class NoConfigException extends Exception
474 function __construct($msg, $configFiles) {
475 parent::__construct($msg);
476 $this->configFiles = $configFiles;