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 // Load settings from database; note we need autoload for this
117 Config::loadSettings();
123 * Get identifier of the currently active site configuration
126 public static function currentSite()
128 return common_config('site', 'nickname');
132 * Change site configuration to site specified by nickname,
133 * if set up via Status_network. If not, sites other than
134 * the current will fail horribly.
136 * May throw exception or trigger a fatal error if the given
137 * site is missing or configured incorrectly.
139 * @param string $nickname
141 public static function switchSite($nickname)
143 if ($nickname == StatusNet::currentSite()) {
147 $sn = Status_network::staticGet('nickname', $nickname);
150 throw new Exception("No such site nickname '$nickname'");
153 $server = $sn->getServerName();
154 StatusNet::init($server);
158 * Pull all local sites from status_network table.
160 * Behavior undefined if site is not configured via Status_network.
162 * @return array of nicknames
164 public static function findAllSites()
167 $sn = new Status_network();
169 while ($sn->fetch()) {
170 $sites[] = $sn->nickname;
176 * Fire initialization events for all instantiated plugins.
178 protected static function initPlugins()
180 // Load default plugins
181 foreach (common_config('plugins', 'default') as $name => $params) {
182 $key = 'disable-' . $name;
183 if (common_config('plugins', $key)) {
187 if (is_null($params)) {
189 } else if (is_array($params)) {
190 if (count($params) == 0) {
193 $keys = array_keys($params);
194 if (is_string($keys[0])) {
195 addPlugin($name, $params);
197 foreach ($params as $paramset) {
198 addPlugin($name, $paramset);
205 // XXX: if plugins should check the schema at runtime, do that here.
206 if (common_config('db', 'schemacheck') == 'runtime') {
207 Event::handle('CheckSchema');
210 // Give plugins a chance to initialize in a fully-prepared environment
211 Event::handle('InitializePlugin');
215 * Quick-check if configuration has been established.
216 * Useful for functions which may get used partway through
217 * initialization to back off from fancier things.
221 public function haveConfig()
223 return self::$have_config;
226 public function isApi()
228 return self::$is_api;
231 public function setApi($mode)
233 self::$is_api = $mode;
236 public function isAjax()
238 return self::$is_ajax;
241 public function setAjax($mode)
243 self::$is_ajax = $mode;
247 * Build default configuration array
250 protected static function defaultConfig()
252 global $_server, $_path;
253 require(INSTALLDIR.'/lib/default.php');
258 * Establish default configuration based on given or default server and path
259 * Sets global $_server, $_path, and $config
261 public static function initDefaults($server, $path)
263 global $_server, $_path, $config;
265 Event::clearHandlers();
266 self::$plugins = array();
268 // try to figure out where we are. $server and $path
269 // can be set by including module, else we guess based
272 if (isset($server)) {
275 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
276 strtolower($_SERVER['SERVER_NAME']) :
283 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
284 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
288 // Set config values initially to default values
289 $default = self::defaultConfig();
292 // default configuration, overwritten in config.php
293 // Keep DB_DataObject's db config synced to ours...
295 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
297 $config['db'] = $default['db'];
299 // Backward compatibility
301 $config['site']['design'] =& $config['design'];
303 if (function_exists('date_default_timezone_set')) {
304 /* Work internally in UTC */
305 date_default_timezone_set('UTC');
309 protected function _sn_to_path($sn)
311 $past_root = substr($sn, 1);
312 $last_slash = strrpos($past_root, '/');
313 if ($last_slash > 0) {
314 $p = substr($past_root, 0, $last_slash);
322 * Load the default or specified configuration file.
323 * Modifies global $config and may establish plugins.
325 * @throws NoConfigException
327 protected function loadConfigFile($conffile=null)
329 global $_server, $_path, $config;
331 // From most general to most specific:
332 // server-wide, then vhost-wide, then for a path,
333 // finally for a dir (usually only need one of the last two).
335 if (isset($conffile)) {
336 $config_files = array($conffile);
338 $config_files = array('/etc/statusnet/statusnet.php',
339 '/etc/statusnet/laconica.php',
340 '/etc/laconica/laconica.php',
341 '/etc/statusnet/'.$_server.'.php',
342 '/etc/laconica/'.$_server.'.php');
344 if (strlen($_path) > 0) {
345 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
346 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
349 $config_files[] = INSTALLDIR.'/config.php';
352 self::$have_config = false;
354 foreach ($config_files as $_config_file) {
355 if (@file_exists($_config_file)) {
356 // Ignore 0-byte config files
357 if (filesize($_config_file) > 0) {
358 include($_config_file);
359 self::$have_config = true;
364 if (!self::$have_config) {
365 throw new NoConfigException("No configuration file found.",
369 // Fixup for statusnet.ini
370 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
372 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
373 $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
376 // Backwards compatibility
377 if (array_key_exists('memcached', $config)) {
378 if ($config['memcached']['enabled']) {
379 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
382 if (!empty($config['memcached']['base'])) {
383 $config['cache']['base'] = $config['memcached']['base'];
386 if (array_key_exists('xmpp', $config)) {
387 if ($config['xmpp']['enabled']) {
388 addPlugin('xmpp', array(
389 'server' => $config['xmpp']['server'],
390 'port' => $config['xmpp']['port'],
391 'user' => $config['xmpp']['user'],
392 'resource' => $config['xmpp']['resource'],
393 'encryption' => $config['xmpp']['encryption'],
394 'password' => $config['xmpp']['password'],
395 'host' => $config['xmpp']['host'],
396 'debug' => $config['xmpp']['debug'],
397 'public' => $config['xmpp']['public']
404 * Are we running from the web with HTTPS?
406 * @return boolean true if we're running with HTTPS; else false
409 static function isHTTPS()
411 // There are some exceptions to this; add them here!
412 if(empty($_SERVER['HTTPS'])) {
415 return $_SERVER['HTTPS'] !== 'off';
420 class NoConfigException extends Exception
424 function __construct($msg, $configFiles) {
425 parent::__construct($msg);
426 $this->configFiles = $configFiles;