]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/statusnet.php
Merge branch '0.9.x' into inblob
[quix0rs-gnu-social.git] / lib / statusnet.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 global $config, $_server, $_path;
23
24 /**
25  * Global configuration setup and management.
26  */
27 class StatusNet
28 {
29     protected static $have_config;
30
31     /**
32      * Configure and instantiate a plugin into the current configuration.
33      * Class definitions will be loaded from standard paths if necessary.
34      * Note that initialization events won't be fired until later.
35      *
36      * @param string $name class name & plugin file/subdir name
37      * @param array $attrs key/value pairs of public attributes to set on plugin instance
38      *
39      * @throws ServerException if plugin can't be found
40      */
41     public static function addPlugin($name, $attrs = null)
42     {
43         $name = ucfirst($name);
44         $pluginclass = "{$name}Plugin";
45
46         if (!class_exists($pluginclass)) {
47
48             $files = array("local/plugins/{$pluginclass}.php",
49                            "local/plugins/{$name}/{$pluginclass}.php",
50                            "local/{$pluginclass}.php",
51                            "local/{$name}/{$pluginclass}.php",
52                            "plugins/{$pluginclass}.php",
53                            "plugins/{$name}/{$pluginclass}.php");
54
55             foreach ($files as $file) {
56                 $fullpath = INSTALLDIR.'/'.$file;
57                 if (@file_exists($fullpath)) {
58                     include_once($fullpath);
59                     break;
60                 }
61             }
62             if (!class_exists($pluginclass)) {
63                 throw new ServerException(500, "Plugin $name not found.");
64             }
65         }
66
67         $inst = new $pluginclass();
68         if (!empty($attrs)) {
69             foreach ($attrs as $aname => $avalue) {
70                 $inst->$aname = $avalue;
71             }
72         }
73         return true;
74     }
75
76     /**
77      * Initialize, or re-initialize, StatusNet global configuration
78      * and plugins.
79      *
80      * If switching site configurations during script execution, be
81      * careful when working with leftover objects -- global settings
82      * affect many things and they may not behave as you expected.
83      *
84      * @param $server optional web server hostname for picking config
85      * @param $path optional URL path for picking config
86      * @param $conffile optional configuration file path
87      *
88      * @throws NoConfigException if config file can't be found
89      */
90     public static function init($server=null, $path=null, $conffile=null)
91     {
92         StatusNet::initDefaults($server, $path);
93         StatusNet::loadConfigFile($conffile);
94
95         // Load settings from database; note we need autoload for this
96         Config::loadSettings();
97
98         self::initPlugins();
99     }
100
101     /**
102      * Fire initialization events for all instantiated plugins.
103      */
104     protected static function initPlugins()
105     {
106         // Load default plugins
107         foreach (common_config('plugins', 'default') as $name => $params) {
108             if (is_null($params)) {
109                 addPlugin($name);
110             } else if (is_array($params)) {
111                 if (count($params) == 0) {
112                     addPlugin($name);
113                 } else {
114                     $keys = array_keys($params);
115                     if (is_string($keys[0])) {
116                         addPlugin($name, $params);
117                     } else {
118                         foreach ($params as $paramset) {
119                             addPlugin($name, $paramset);
120                         }
121                     }
122                 }
123             }
124         }
125
126         // XXX: if plugins should check the schema at runtime, do that here.
127         if (common_config('db', 'schemacheck') == 'runtime') {
128             Event::handle('CheckSchema');
129         }
130
131         // Give plugins a chance to initialize in a fully-prepared environment
132         Event::handle('InitializePlugin');
133     }
134
135     /**
136      * Quick-check if configuration has been established.
137      * Useful for functions which may get used partway through
138      * initialization to back off from fancier things.
139      *
140      * @return bool
141      */
142     public function haveConfig()
143     {
144         return self::$have_config;
145     }
146
147     /**
148      * Build default configuration array
149      * @return array
150      */
151     protected static function defaultConfig()
152     {
153         global $_server, $_path;
154         require(INSTALLDIR.'/lib/default.php');
155         return $default;
156     }
157
158     /**
159      * Establish default configuration based on given or default server and path
160      * Sets global $_server, $_path, and $config
161      */
162     protected static function initDefaults($server, $path)
163     {
164         global $_server, $_path, $config;
165
166         Event::clearHandlers();
167
168         // try to figure out where we are. $server and $path
169         // can be set by including module, else we guess based
170         // on HTTP info.
171
172         if (isset($server)) {
173             $_server = $server;
174         } else {
175             $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
176               strtolower($_SERVER['SERVER_NAME']) :
177             null;
178         }
179
180         if (isset($path)) {
181             $_path = $path;
182         } else {
183             $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
184               self::_sn_to_path($_SERVER['SCRIPT_NAME']) :
185             null;
186         }
187
188         // Set config values initially to default values
189         $default = self::defaultConfig();
190         $config = $default;
191
192         // default configuration, overwritten in config.php
193         // Keep DB_DataObject's db config synced to ours...
194
195         $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
196
197         $config['db'] = $default['db'];
198
199         // Backward compatibility
200
201         $config['site']['design'] =& $config['design'];
202
203         if (function_exists('date_default_timezone_set')) {
204             /* Work internally in UTC */
205             date_default_timezone_set('UTC');
206         }
207     }
208
209     protected function _sn_to_path($sn)
210     {
211         $past_root = substr($sn, 1);
212         $last_slash = strrpos($past_root, '/');
213         if ($last_slash > 0) {
214             $p = substr($past_root, 0, $last_slash);
215         } else {
216             $p = '';
217         }
218         return $p;
219     }
220
221     /**
222      * Load the default or specified configuration file.
223      * Modifies global $config and may establish plugins.
224      *
225      * @throws NoConfigException
226      */
227     protected function loadConfigFile($conffile=null)
228     {
229         global $_server, $_path, $config;
230         
231         // From most general to most specific:
232         // server-wide, then vhost-wide, then for a path,
233         // finally for a dir (usually only need one of the last two).
234         
235         if (isset($conffile)) {
236             $config_files = array($conffile);
237         } else {
238             $config_files = array('/etc/statusnet/statusnet.php',
239                                         '/etc/statusnet/laconica.php',
240                                         '/etc/laconica/laconica.php',
241                                         '/etc/statusnet/'.$_server.'.php',
242                                         '/etc/laconica/'.$_server.'.php');
243
244             if (strlen($_path) > 0) {
245                 $config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
246                 $config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
247             }
248
249             $config_files[] = INSTALLDIR.'/config.php';
250         }
251
252         self::$have_config = false;
253
254         foreach ($config_files as $_config_file) {
255             if (@file_exists($_config_file)) {
256                 include($_config_file);
257                 self::$have_config = true;
258             }
259         }
260
261         if (!self::$have_config) {
262             throw new NoConfigException("No configuration file found.",
263                 $config_files);
264         }
265
266         // Fixup for statusnet.ini
267         $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
268
269         if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
270             $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
271         }
272
273         // Backwards compatibility
274
275         if (array_key_exists('memcached', $config)) {
276             if ($config['memcached']['enabled']) {
277                 addPlugin('Memcache', array('servers' => $config['memcached']['server']));
278             }
279
280             if (!empty($config['memcached']['base'])) {
281                 $config['cache']['base'] = $config['memcached']['base'];
282             }
283         }
284     }
285 }
286
287 class NoConfigException extends Exception
288 {
289     public $config_files;
290
291     function __construct($msg, $config_files) {
292         parent::__construct($msg);
293         $this->config_files = $config_files;
294     }
295 }