123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) {
- exit(1);
- }
- global $config, $_server, $_path;
- class StatusNet
- {
- protected static $have_config;
- protected static $is_api;
- protected static $is_ajax;
- protected static $plugins = array();
-
- public static function addPlugin($name, array $attrs=array())
- {
- $name = ucfirst($name);
- if (isset(self::$plugins[$name])) {
-
-
-
- return true;
- }
- $pluginclass = "{$name}Plugin";
- if (!class_exists($pluginclass)) {
- $files = array("local/plugins/{$pluginclass}.php",
- "local/plugins/{$name}/{$pluginclass}.php",
- "local/{$pluginclass}.php",
- "local/{$name}/{$pluginclass}.php",
- "plugins/{$pluginclass}.php",
- "plugins/{$name}/{$pluginclass}.php");
- foreach ($files as $file) {
- $fullpath = INSTALLDIR.'/'.$file;
- if (@file_exists($fullpath)) {
- include_once($fullpath);
- break;
- }
- }
- if (!class_exists($pluginclass)) {
- throw new ServerException("Plugin $name not found.", 500);
- }
- }
-
-
- $inst = new $pluginclass();
- foreach ($attrs as $aname => $avalue) {
- $inst->$aname = $avalue;
- }
-
- self::$plugins[$name] = $attrs;
- return true;
- }
- public static function delPlugin($name)
- {
-
- $name = ucfirst($name);
- if (isset(self::$plugins[$name])) {
- unset(self::$plugins[$name]);
- }
-
- common_config_set('plugins', 'disable-'.$name, true);
- return true;
- }
-
- public static function getActivePlugins()
- {
- return self::$plugins;
- }
-
- public static function init($server=null, $path=null, $conffile=null)
- {
- Router::clear();
- self::initDefaults($server, $path);
- self::loadConfigFile($conffile);
- $sprofile = common_config('site', 'profile');
- if (!empty($sprofile)) {
- self::loadSiteProfile($sprofile);
- }
-
- Config::loadSettings();
- self::initPlugins();
- }
-
- public static function currentSite()
- {
- return common_config('site', 'nickname');
- }
-
- public static function switchSite($nickname)
- {
- if ($nickname == StatusNet::currentSite()) {
- return true;
- }
- $sn = Status_network::getKV('nickname', $nickname);
- if (empty($sn)) {
- return false;
- throw new Exception("No such site nickname '$nickname'");
- }
- $server = $sn->getServerName();
- StatusNet::init($server);
- }
-
- public static function findAllSites()
- {
- $sites = array();
- $sn = new Status_network();
- $sn->find();
- while ($sn->fetch()) {
- $sites[] = $sn->nickname;
- }
- return $sites;
- }
-
- protected static function initPlugins()
- {
-
-
-
-
- foreach (common_config('plugins', 'core') as $name => $params) {
- call_user_func('self::addPlugin', $name, $params);
- }
-
- foreach (common_config('plugins', 'default') as $name => $params) {
- $key = 'disable-' . $name;
- if (common_config('plugins', $key)) {
- continue;
- }
-
-
- if (is_null($params)) {
- self::addPlugin($name);
- } else if (is_array($params)) {
- if (count($params) == 0) {
- self::addPlugin($name);
- } else {
- $keys = array_keys($params);
- if (is_string($keys[0])) {
- self::addPlugin($name, $params);
- } else {
- foreach ($params as $paramset) {
- self::addPlugin($name, $paramset);
- }
- }
- }
- }
- }
-
- if (common_config('db', 'schemacheck') == 'runtime') {
- Event::handle('CheckSchema');
- }
-
- Event::handle('InitializePlugin');
- }
-
- public static function haveConfig()
- {
- return self::$have_config;
- }
- public static function isApi()
- {
- return self::$is_api;
- }
- public static function setApi($mode)
- {
- self::$is_api = $mode;
- }
- public static function isAjax()
- {
- return self::$is_ajax;
- }
- public static function setAjax($mode)
- {
- self::$is_ajax = $mode;
- }
-
- protected static function defaultConfig()
- {
- global $_server, $_path;
- require(INSTALLDIR.'/lib/default.php');
- return $default;
- }
-
- public static function initDefaults($server, $path)
- {
- global $_server, $_path, $config, $_PEAR;
- Event::clearHandlers();
- self::$plugins = array();
-
-
-
- if (isset($server)) {
- $_server = $server;
- } else {
- $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
- strtolower($_SERVER['SERVER_NAME']) :
- null;
- }
- if (isset($path)) {
- $_path = $path;
- } else {
- $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
- self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
- null;
- }
-
- $default = self::defaultConfig();
- $config = $default;
-
-
- $config['db'] = &$_PEAR->getStaticProperty('DB_DataObject','options');
- $config['db'] = $default['db'];
- if (function_exists('date_default_timezone_set')) {
-
- date_default_timezone_set('UTC');
- }
- }
- public static function loadSiteProfile($name)
- {
- global $config;
- $settings = SiteProfile::getSettings($name);
- $config = array_replace_recursive($config, $settings);
- }
- protected static function _sn_to_path($sn)
- {
- $past_root = substr($sn, 1);
- $last_slash = strrpos($past_root, '/');
- if ($last_slash > 0) {
- $p = substr($past_root, 0, $last_slash);
- } else {
- $p = '';
- }
- return $p;
- }
-
- protected static function loadConfigFile($conffile=null)
- {
- global $_server, $_path, $config;
-
-
-
- if (isset($conffile)) {
- $config_files = array($conffile);
- } else {
- $config_files = array('/etc/statusnet/statusnet.php',
- '/etc/statusnet/laconica.php',
- '/etc/laconica/laconica.php',
- '/etc/statusnet/'.$_server.'.php',
- '/etc/laconica/'.$_server.'.php');
- if (strlen($_path) > 0) {
- $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
- $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
- }
- $config_files[] = INSTALLDIR.'/config.php';
- }
- self::$have_config = false;
- foreach ($config_files as $_config_file) {
- if (@file_exists($_config_file)) {
-
- if (filesize($_config_file) > 0) {
- common_log(LOG_INFO, "Including config file: " . $_config_file);
- include($_config_file);
- self::$have_config = true;
- }
- }
- }
- if (!self::$have_config) {
- throw new NoConfigException("No configuration file found.",
- $config_files);
- }
-
- if (empty($config['db']['database'])) {
- throw new ServerException("No database server for this site.");
- }
- }
-
- static function isHTTPS()
- {
-
- if (empty($_SERVER['HTTPS'])) {
- return false;
- }
-
- return strtolower($_SERVER['HTTPS']) !== 'off';
- }
-
- static function useHTTPS()
- {
- return self::isHTTPS() || common_config('site', 'ssl') != 'never';
- }
- }
- class NoConfigException extends Exception
- {
- public $configFiles;
- function __construct($msg, $configFiles) {
- parent::__construct($msg);
- $this->configFiles = $configFiles;
- }
- }
|