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;
35 * Configure and instantiate a plugin into the current configuration.
36 * Class definitions will be loaded from standard paths if necessary.
37 * Note that initialization events won't be fired until later.
39 * @param string $name class name & plugin file/subdir name
40 * @param array $attrs key/value pairs of public attributes to set on plugin instance
42 * @throws ServerException if plugin can't be found
44 public static function addPlugin($name, $attrs = null)
46 $name = ucfirst($name);
47 $pluginclass = "{$name}Plugin";
49 if (!class_exists($pluginclass)) {
51 $files = array("local/plugins/{$pluginclass}.php",
52 "local/plugins/{$name}/{$pluginclass}.php",
53 "local/{$pluginclass}.php",
54 "local/{$name}/{$pluginclass}.php",
55 "plugins/{$pluginclass}.php",
56 "plugins/{$name}/{$pluginclass}.php");
58 foreach ($files as $file) {
59 $fullpath = INSTALLDIR.'/'.$file;
60 if (@file_exists($fullpath)) {
61 include_once($fullpath);
65 if (!class_exists($pluginclass)) {
66 throw new ServerException("Plugin $name not found.", 500);
70 $inst = new $pluginclass();
72 foreach ($attrs as $aname => $avalue) {
73 $inst->$aname = $avalue;
80 * Initialize, or re-initialize, StatusNet global configuration
83 * If switching site configurations during script execution, be
84 * careful when working with leftover objects -- global settings
85 * affect many things and they may not behave as you expected.
87 * @param $server optional web server hostname for picking config
88 * @param $path optional URL path for picking config
89 * @param $conffile optional configuration file path
91 * @throws NoConfigException if config file can't be found
93 public static function init($server=null, $path=null, $conffile=null)
95 StatusNet::initDefaults($server, $path);
96 StatusNet::loadConfigFile($conffile);
98 // Load settings from database; note we need autoload for this
99 Config::loadSettings();
105 * Fire initialization events for all instantiated plugins.
107 protected static function initPlugins()
109 // Load default plugins
110 foreach (common_config('plugins', 'default') as $name => $params) {
111 if (is_null($params)) {
113 } else if (is_array($params)) {
114 if (count($params) == 0) {
117 $keys = array_keys($params);
118 if (is_string($keys[0])) {
119 addPlugin($name, $params);
121 foreach ($params as $paramset) {
122 addPlugin($name, $paramset);
129 // XXX: if plugins should check the schema at runtime, do that here.
130 if (common_config('db', 'schemacheck') == 'runtime') {
131 Event::handle('CheckSchema');
134 // Give plugins a chance to initialize in a fully-prepared environment
135 Event::handle('InitializePlugin');
139 * Quick-check if configuration has been established.
140 * Useful for functions which may get used partway through
141 * initialization to back off from fancier things.
145 public function haveConfig()
147 return self::$have_config;
151 * Build default configuration array
154 protected static function defaultConfig()
156 global $_server, $_path;
157 require(INSTALLDIR.'/lib/default.php');
162 * Establish default configuration based on given or default server and path
163 * Sets global $_server, $_path, and $config
165 protected static function initDefaults($server, $path)
167 global $_server, $_path, $config;
169 Event::clearHandlers();
171 // try to figure out where we are. $server and $path
172 // can be set by including module, else we guess based
175 if (isset($server)) {
178 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
179 strtolower($_SERVER['SERVER_NAME']) :
186 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
187 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
191 // Set config values initially to default values
192 $default = self::defaultConfig();
195 // default configuration, overwritten in config.php
196 // Keep DB_DataObject's db config synced to ours...
198 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
200 $config['db'] = $default['db'];
202 // Backward compatibility
204 $config['site']['design'] =& $config['design'];
206 if (function_exists('date_default_timezone_set')) {
207 /* Work internally in UTC */
208 date_default_timezone_set('UTC');
212 protected function _sn_to_path($sn)
214 $past_root = substr($sn, 1);
215 $last_slash = strrpos($past_root, '/');
216 if ($last_slash > 0) {
217 $p = substr($past_root, 0, $last_slash);
225 * Load the default or specified configuration file.
226 * Modifies global $config and may establish plugins.
228 * @throws NoConfigException
230 protected function loadConfigFile($conffile=null)
232 global $_server, $_path, $config;
234 // From most general to most specific:
235 // server-wide, then vhost-wide, then for a path,
236 // finally for a dir (usually only need one of the last two).
238 if (isset($conffile)) {
239 $config_files = array($conffile);
241 $config_files = array('/etc/statusnet/statusnet.php',
242 '/etc/statusnet/laconica.php',
243 '/etc/laconica/laconica.php',
244 '/etc/statusnet/'.$_server.'.php',
245 '/etc/laconica/'.$_server.'.php');
247 if (strlen($_path) > 0) {
248 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
249 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
252 $config_files[] = INSTALLDIR.'/config.php';
255 self::$have_config = false;
257 foreach ($config_files as $_config_file) {
258 if (@file_exists($_config_file)) {
259 include($_config_file);
260 self::$have_config = true;
264 if (!self::$have_config) {
265 throw new NoConfigException("No configuration file found.",
269 // Fixup for statusnet.ini
270 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
272 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
273 $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
276 // Backwards compatibility
277 if (array_key_exists('memcached', $config)) {
278 if ($config['memcached']['enabled']) {
279 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
282 if (!empty($config['memcached']['base'])) {
283 $config['cache']['base'] = $config['memcached']['base'];
286 if (array_key_exists('xmpp', $config)) {
287 if ($config['xmpp']['enabled']) {
288 addPlugin('xmpp', array(
289 'server' => $config['xmpp']['server'],
290 'port' => $config['xmpp']['port'],
291 'user' => $config['xmpp']['user'],
292 'resource' => $config['xmpp']['resource'],
293 'encryption' => $config['xmpp']['encryption'],
294 'password' => $config['xmpp']['password'],
295 'host' => $config['xmpp']['host'],
296 'debug' => $config['xmpp']['debug'],
297 'public' => $config['xmpp']['public']
304 class NoConfigException extends Exception
306 public $config_files;
308 function __construct($msg, $config_files) {
309 parent::__construct($msg);
310 $this->config_files = $config_files;