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