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)
112 StatusNet::initDefaults($server, $path);
113 StatusNet::loadConfigFile($conffile);
115 // Load settings from database; note we need autoload for this
116 Config::loadSettings();
122 * Get identifier of the currently active site configuration
125 public static function currentSite()
127 return common_config('site', 'nickname');
131 * Change site configuration to site specified by nickname,
132 * if set up via Status_network. If not, sites other than
133 * the current will fail horribly.
135 * May throw exception or trigger a fatal error if the given
136 * site is missing or configured incorrectly.
138 * @param string $nickname
140 public static function switchSite($nickname)
142 if ($nickname == StatusNet::currentSite()) {
146 $sn = Status_network::staticGet('nickname', $nickname);
149 throw new Exception("No such site nickname '$nickname'");
152 $server = $sn->getServerName();
153 StatusNet::init($server);
157 * Pull all local sites from status_network table.
159 * Behavior undefined if site is not configured via Status_network.
161 * @return array of nicknames
163 public static function findAllSites()
166 $sn = new Status_network();
168 while ($sn->fetch()) {
169 $sites[] = $sn->nickname;
175 * Fire initialization events for all instantiated plugins.
177 protected static function initPlugins()
179 // Load default plugins
180 foreach (common_config('plugins', 'default') as $name => $params) {
181 if (is_null($params)) {
183 } else if (is_array($params)) {
184 if (count($params) == 0) {
187 $keys = array_keys($params);
188 if (is_string($keys[0])) {
189 addPlugin($name, $params);
191 foreach ($params as $paramset) {
192 addPlugin($name, $paramset);
199 // XXX: if plugins should check the schema at runtime, do that here.
200 if (common_config('db', 'schemacheck') == 'runtime') {
201 Event::handle('CheckSchema');
204 // Give plugins a chance to initialize in a fully-prepared environment
205 Event::handle('InitializePlugin');
209 * Quick-check if configuration has been established.
210 * Useful for functions which may get used partway through
211 * initialization to back off from fancier things.
215 public function haveConfig()
217 return self::$have_config;
220 public function isApi()
222 return self::$is_api;
225 public function setApi($mode)
227 self::$is_api = $mode;
231 * Build default configuration array
234 protected static function defaultConfig()
236 global $_server, $_path;
237 require(INSTALLDIR.'/lib/default.php');
242 * Establish default configuration based on given or default server and path
243 * Sets global $_server, $_path, and $config
245 protected static function initDefaults($server, $path)
247 global $_server, $_path, $config;
249 Event::clearHandlers();
250 self::$plugins = array();
252 // try to figure out where we are. $server and $path
253 // can be set by including module, else we guess based
256 if (isset($server)) {
259 $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
260 strtolower($_SERVER['SERVER_NAME']) :
267 $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
268 self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
272 // Set config values initially to default values
273 $default = self::defaultConfig();
276 // default configuration, overwritten in config.php
277 // Keep DB_DataObject's db config synced to ours...
279 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
281 $config['db'] = $default['db'];
283 // Backward compatibility
285 $config['site']['design'] =& $config['design'];
287 if (function_exists('date_default_timezone_set')) {
288 /* Work internally in UTC */
289 date_default_timezone_set('UTC');
293 protected function _sn_to_path($sn)
295 $past_root = substr($sn, 1);
296 $last_slash = strrpos($past_root, '/');
297 if ($last_slash > 0) {
298 $p = substr($past_root, 0, $last_slash);
306 * Load the default or specified configuration file.
307 * Modifies global $config and may establish plugins.
309 * @throws NoConfigException
311 protected function loadConfigFile($conffile=null)
313 global $_server, $_path, $config;
315 // From most general to most specific:
316 // server-wide, then vhost-wide, then for a path,
317 // finally for a dir (usually only need one of the last two).
319 if (isset($conffile)) {
320 $config_files = array($conffile);
322 $config_files = array('/etc/statusnet/statusnet.php',
323 '/etc/statusnet/laconica.php',
324 '/etc/laconica/laconica.php',
325 '/etc/statusnet/'.$_server.'.php',
326 '/etc/laconica/'.$_server.'.php');
328 if (strlen($_path) > 0) {
329 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
330 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
333 $config_files[] = INSTALLDIR.'/config.php';
336 self::$have_config = false;
338 foreach ($config_files as $_config_file) {
339 if (@file_exists($_config_file)) {
340 // Ignore 0-byte config files
341 if (filesize($_config_file) > 0) {
342 include($_config_file);
343 self::$have_config = true;
348 if (!self::$have_config) {
349 throw new NoConfigException("No configuration file found.",
353 // Fixup for statusnet.ini
354 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
356 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
357 $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
360 // Backwards compatibility
362 if (array_key_exists('memcached', $config)) {
363 if ($config['memcached']['enabled']) {
364 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
367 if (!empty($config['memcached']['base'])) {
368 $config['cache']['base'] = $config['memcached']['base'];
374 * Are we running from the web with HTTPS?
376 * @return boolean true if we're running with HTTPS; else false
379 static function isHTTPS()
381 // There are some exceptions to this; add them here!
382 if(empty($_SERVER['HTTPS'])) {
385 return $_SERVER['HTTPS'] !== 'off';
390 class NoConfigException extends Exception
394 function __construct($msg, $configFiles) {
395 parent::__construct($msg);
396 $this->configFiles = $configFiles;