]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/statusnet.php
Followup to IIS installer tweaks in [9bb48c36]:
[quix0rs-gnu-social.git] / lib / statusnet.php
index 0c5807d7b984099e175c66ec8798d7ec84f40609..7cb831696bc02ac512a69f6190469242227371c4 100644 (file)
@@ -1,7 +1,7 @@
 <?php
-/*
+/**
  * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2009, StatusNet, Inc.
+ * Copyright (C) 2009-2010 StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  *
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
  */
 
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
+if (!defined('STATUSNET') && !defined('LACONICA')) {
+    exit(1);
+}
 
 global $config, $_server, $_path;
 
@@ -27,6 +30,8 @@ global $config, $_server, $_path;
 class StatusNet
 {
     protected static $have_config;
+    protected static $is_api;
+    protected static $plugins = array();
 
     /**
      * Configure and instantiate a plugin into the current configuration.
@@ -60,7 +65,7 @@ class StatusNet
                 }
             }
             if (!class_exists($pluginclass)) {
-                throw new ServerException(500, "Plugin $name not found.");
+                throw new ServerException("Plugin $name not found.", 500);
             }
         }
 
@@ -70,9 +75,22 @@ class StatusNet
                 $inst->$aname = $avalue;
             }
         }
+
+        // Record activated plugins for later display/config dump
+        self::$plugins[] = array($name, $attrs);
+
         return true;
     }
 
+    /**
+     * Get a list of activated plugins in this process.
+     * @return array of (string $name, array $args) pairs
+     */
+    public static function getActivePlugins()
+    {
+        return self::$plugins;
+    }
+
     /**
      * Initialize, or re-initialize, StatusNet global configuration
      * and plugins.
@@ -98,6 +116,60 @@ class StatusNet
         self::initPlugins();
     }
 
+    /**
+     * Get identifier of the currently active site configuration
+     * @return string
+     */
+    public static function currentSite()
+    {
+        return common_config('site', 'nickname');
+    }
+
+    /**
+     * Change site configuration to site specified by nickname,
+     * if set up via Status_network. If not, sites other than
+     * the current will fail horribly.
+     *
+     * May throw exception or trigger a fatal error if the given
+     * site is missing or configured incorrectly.
+     *
+     * @param string $nickname
+     */
+    public static function switchSite($nickname)
+    {
+        if ($nickname == StatusNet::currentSite()) {
+            return true;
+        }
+
+        $sn = Status_network::staticGet('nickname', $nickname);
+        if (empty($sn)) {
+            return false;
+            throw new Exception("No such site nickname '$nickname'");
+        }
+
+        $server = $sn->getServerName();
+        StatusNet::init($server);
+    }
+
+    /**
+     * Pull all local sites from status_network table.
+     *
+     * Behavior undefined if site is not configured via Status_network.
+     *
+     * @return array of nicknames
+     */
+    public static function findAllSites()
+    {
+        $sites = array();
+        $sn = new Status_network();
+        $sn->find();
+        while ($sn->fetch()) {
+            $sites[] = $sn->nickname;
+        }
+        return $sites;
+    }
+
+
     /**
      * Fire initialization events for all instantiated plugins.
      */
@@ -144,6 +216,16 @@ class StatusNet
         return self::$have_config;
     }
 
+    public function isApi()
+    {
+        return self::$is_api;
+    }
+    
+    public function setApi($mode)
+    {
+        self::$is_api = $mode;
+    }
+
     /**
      * Build default configuration array
      * @return array
@@ -164,6 +246,7 @@ class StatusNet
         global $_server, $_path, $config;
 
         Event::clearHandlers();
+        self::$plugins = array();
 
         // try to figure out where we are. $server and $path
         // can be set by including module, else we guess based
@@ -227,19 +310,19 @@ class StatusNet
     protected function loadConfigFile($conffile=null)
     {
         global $_server, $_path, $config;
-        
+
         // From most general to most specific:
         // server-wide, then vhost-wide, then for a path,
         // finally for a dir (usually only need one of the last two).
-        
+
         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');
+                                  '/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';
@@ -253,14 +336,17 @@ class StatusNet
 
         foreach ($config_files as $_config_file) {
             if (@file_exists($_config_file)) {
-                include($_config_file);
-                self::$have_config = true;
+                // Ignore 0-byte config files
+                if (filesize($_config_file) > 0) {
+                    include($_config_file);
+                    self::$have_config = true;
+                }
             }
         }
 
         if (!self::$have_config) {
             throw new NoConfigException("No configuration file found.",
-                $config_files);
+                                        $config_files);
         }
 
         // Fixup for statusnet.ini
@@ -286,10 +372,10 @@ class StatusNet
 
 class NoConfigException extends Exception
 {
-    public $config_files;
+    public $configFiles;
 
-    function __construct($msg, $config_files) {
+    function __construct($msg, $configFiles) {
         parent::__construct($msg);
-        $this->config_files = $config_files;
+        $this->configFiles = $configFiles;
     }
 }