]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/common.php
Merge branch '0.9.x' into pluginize-twitter-bridge
[quix0rs-gnu-social.git] / lib / common.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 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 define('STATUSNET_VERSION', '0.9.0dev');
23 define('LACONICA_VERSION', STATUSNET_VERSION); // compatibility
24
25 define('STATUSNET_CODENAME', 'Stand');
26
27 define('AVATAR_PROFILE_SIZE', 96);
28 define('AVATAR_STREAM_SIZE', 48);
29 define('AVATAR_MINI_SIZE', 24);
30
31 define('NOTICES_PER_PAGE', 20);
32 define('PROFILES_PER_PAGE', 20);
33
34 define('FOREIGN_NOTICE_SEND', 1);
35 define('FOREIGN_NOTICE_RECV', 2);
36 define('FOREIGN_NOTICE_SEND_REPLY', 4);
37
38 define('FOREIGN_FRIEND_SEND', 1);
39 define('FOREIGN_FRIEND_RECV', 2);
40
41 define_syslog_variables();
42
43 # append our extlib dir as the last-resort place to find libs
44
45 set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib/');
46
47 # global configuration object
48
49 require_once('PEAR.php');
50 require_once('DB/DataObject.php');
51 require_once('DB/DataObject/Cast.php'); # for dates
52
53 if (!function_exists('gettext')) {
54     require_once("php-gettext/gettext.inc");
55 }
56 require_once(INSTALLDIR.'/lib/language.php');
57
58 // This gets included before the config file, so that admin code and plugins
59 // can use it
60
61 require_once(INSTALLDIR.'/lib/event.php');
62 require_once(INSTALLDIR.'/lib/plugin.php');
63
64 function _sn_to_path($sn)
65 {
66     $past_root = substr($sn, 1);
67     $last_slash = strrpos($past_root, '/');
68     if ($last_slash > 0) {
69         $p = substr($past_root, 0, $last_slash);
70     } else {
71         $p = '';
72     }
73     return $p;
74 }
75
76 // try to figure out where we are. $server and $path
77 // can be set by including module, else we guess based
78 // on HTTP info.
79
80 if (isset($server)) {
81     $_server = $server;
82 } else {
83     $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
84       strtolower($_SERVER['SERVER_NAME']) :
85     null;
86 }
87
88 if (isset($path)) {
89     $_path = $path;
90 } else {
91     $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
92       _sn_to_path($_SERVER['SCRIPT_NAME']) :
93     null;
94 }
95
96 // default configuration, overwritten in config.php
97
98 $config =
99   array('site' =>
100         array('name' => 'Just another StatusNet microblog',
101               'server' => $_server,
102               'theme' => 'default',
103               'path' => $_path,
104               'logfile' => null,
105               'logo' => null,
106               'logdebug' => false,
107               'fancy' => false,
108               'locale_path' => INSTALLDIR.'/locale',
109               'language' => 'en_US',
110               'languages' => get_all_languages(),
111               'email' =>
112               array_key_exists('SERVER_ADMIN', $_SERVER) ? $_SERVER['SERVER_ADMIN'] : null,
113               'broughtby' => null,
114               'timezone' => 'UTC',
115               'broughtbyurl' => null,
116               'closed' => false,
117               'inviteonly' => false,
118               'private' => false,
119               'ssl' => 'never',
120               'sslserver' => null,
121               'shorturllength' => 30,
122               'dupelimit' => 60, # default for same person saying the same thing
123               'textlimit' => 140,
124               ),
125         'syslog' =>
126         array('appname' => 'statusnet', # for syslog
127               'priority' => 'debug', # XXX: currently ignored
128               'facility' => LOG_USER),
129         'queue' =>
130         array('enabled' => false,
131               'subsystem' => 'db', # default to database, or 'stomp'
132               'stomp_server' => null,
133               'queue_basename' => 'statusnet',
134               'stomp_username' => null,
135               'stomp_password' => null,
136               ),
137         'license' =>
138         array('url' => 'http://creativecommons.org/licenses/by/3.0/',
139               'title' => 'Creative Commons Attribution 3.0',
140               'image' => 'http://i.creativecommons.org/l/by/3.0/80x15.png'),
141         'mail' =>
142         array('backend' => 'mail',
143               'params' => null),
144         'nickname' =>
145         array('blacklist' => array(),
146               'featured' => array()),
147         'profile' =>
148         array('banned' => array(),
149               'biolimit' => null),
150         'avatar' =>
151         array('server' => null,
152               'dir' => INSTALLDIR . '/avatar/',
153               'path' => $_path . '/avatar/'),
154         'background' =>
155         array('server' => null,
156               'dir' => INSTALLDIR . '/background/',
157               'path' => $_path . '/background/'),
158         'public' =>
159         array('localonly' => true,
160               'blacklist' => array(),
161               'autosource' => array()),
162         'theme' =>
163         array('server' => null,
164               'dir' => null,
165               'path'=> null),
166         'throttle' =>
167         array('enabled' => false, // whether to throttle edits; false by default
168               'count' => 20, // number of allowed messages in timespan
169               'timespan' => 600), // timespan for throttling
170         'xmpp' =>
171         array('enabled' => false,
172               'server' => 'INVALID SERVER',
173               'port' => 5222,
174               'user' => 'update',
175               'encryption' => true,
176               'resource' => 'uniquename',
177               'password' => 'blahblahblah',
178               'host' => null, # only set if != server
179               'debug' => false, # print extra debug info
180               'public' => array()), # JIDs of users who want to receive the public stream
181         'invite' =>
182         array('enabled' => true),
183         'sphinx' =>
184         array('enabled' => false,
185               'server' => 'localhost',
186               'port' => 3312),
187         'tag' =>
188         array('dropoff' => 864000.0),
189         'popular' =>
190         array('dropoff' => 864000.0),
191         'daemon' =>
192         array('piddir' => '/var/run',
193               'user' => false,
194               'group' => false),
195         'emailpost' =>
196         array('enabled' => true),
197         'sms' =>
198         array('enabled' => true),
199         'twitter' =>
200         array('enabled' => true),
201         'twitterbridge' =>
202         array('enabled' => false),
203         'integration' =>
204         array('source' => 'StatusNet', # source attribute for Twitter
205               'taguri' => $_server.',2009'), # base for tag URIs
206         'twitter' =>
207         array('consumer_key'    => null,
208               'consumer_secret' => null),
209         'memcached' =>
210         array('enabled' => false,
211               'server' => 'localhost',
212               'base' => null,
213               'port' => 11211),
214                 'ping' =>
215         array('notify' => array()),
216         'inboxes' =>
217         array('enabled' => true), # on by default for new sites
218         'newuser' =>
219         array('default' => null,
220               'welcome' => null),
221         'snapshot' =>
222         array('run' => 'web',
223               'frequency' => 10000,
224               'reporturl' => 'http://status.net/stats/report'),
225         'attachments' =>
226         array('server' => null,
227               'dir' => INSTALLDIR . '/file/',
228               'path' => $_path . '/file/',
229               'supported' => array('image/png',
230                                    'image/jpeg',
231                                    'image/gif',
232                                    'image/svg+xml',
233                                    'audio/mpeg',
234                                    'audio/x-speex',
235                                    'application/ogg',
236                                    'application/pdf',
237                                    'application/vnd.oasis.opendocument.text',
238                                    'application/vnd.oasis.opendocument.text-template',
239                                    'application/vnd.oasis.opendocument.graphics',
240                                    'application/vnd.oasis.opendocument.graphics-template',
241                                    'application/vnd.oasis.opendocument.presentation',
242                                    'application/vnd.oasis.opendocument.presentation-template',
243                                    'application/vnd.oasis.opendocument.spreadsheet',
244                                    'application/vnd.oasis.opendocument.spreadsheet-template',
245                                    'application/vnd.oasis.opendocument.chart',
246                                    'application/vnd.oasis.opendocument.chart-template',
247                                    'application/vnd.oasis.opendocument.image',
248                                    'application/vnd.oasis.opendocument.image-template',
249                                    'application/vnd.oasis.opendocument.formula',
250                                    'application/vnd.oasis.opendocument.formula-template',
251                                    'application/vnd.oasis.opendocument.text-master',
252                                    'application/vnd.oasis.opendocument.text-web',
253                                    'application/x-zip',
254                                    'application/zip',
255                                    'text/plain',
256                                    'video/mpeg',
257                                    'video/mp4',
258                                    'video/quicktime',
259                                    'video/mpeg'),
260         'file_quota' => 5000000,
261         'user_quota' => 50000000,
262         'monthly_quota' => 15000000,
263         'uploads' => true,
264         'filecommand' => '/usr/bin/file',
265         ),
266         'group' =>
267         array('maxaliases' => 3,
268               'desclimit' => null),
269         'oohembed' => array('endpoint' => 'http://oohembed.com/oohembed/'),
270         'search' =>
271         array('type' => 'fulltext'),
272         'sessions' =>
273         array('handle' => false, // whether to handle sessions ourselves
274               'debug' => false), // debugging output for sessions
275         'design' =>
276         array('backgroundcolor' => null, // null -> 'use theme default'
277               'contentcolor' => null,
278               'sidebarcolor' => null,
279               'textcolor' => null,
280               'linkcolor' => null,
281               'backgroundimage' => null,
282               'disposition' => null),
283         'notice' =>
284         array('contentlimit' => null),
285         'message' =>
286         array('contentlimit' => null),
287         'http' =>
288         array('client' => 'curl'), // XXX: should this be the default?
289         );
290
291 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
292
293 $config['db'] =
294   array('database' => 'YOU HAVE TO SET THIS IN config.php',
295         'schema_location' => INSTALLDIR . '/classes',
296         'class_location' => INSTALLDIR . '/classes',
297         'require_prefix' => 'classes/',
298         'class_prefix' => '',
299         'mirror' => null,
300         'utf8' => true,
301         'db_driver' => 'DB', # XXX: JanRain libs only work with DB
302         'quote_identifiers' => false,
303         'type' => 'mysql' );
304
305 // Backward compatibility
306
307 $config['site']['design'] =& $config['design'];
308
309 if (function_exists('date_default_timezone_set')) {
310     /* Work internally in UTC */
311     date_default_timezone_set('UTC');
312 }
313
314 function addPlugin($name, $attrs = null)
315 {
316     $name = ucfirst($name);
317     $pluginclass = "{$name}Plugin";
318
319     if (!class_exists($pluginclass)) {
320
321         $files = array("local/plugins/{$pluginclass}.php",
322                        "local/plugins/{$name}/{$pluginclass}.php",
323                        "local/{$pluginclass}.php",
324                        "local/{$name}/{$pluginclass}.php",
325                        "plugins/{$pluginclass}.php",
326                        "plugins/{$name}/{$pluginclass}.php");
327
328         foreach ($files as $file) {
329             $fullpath = INSTALLDIR.'/'.$file;
330             if (@file_exists($fullpath)) {
331                 include_once($fullpath);
332                 break;
333             }
334         }
335     }
336
337     $inst = new $pluginclass();
338
339     if (!empty($attrs)) {
340         foreach ($attrs as $aname => $avalue) {
341             $inst->$aname = $avalue;
342         }
343     }
344     return $inst;
345 }
346
347 // From most general to most specific:
348 // server-wide, then vhost-wide, then for a path,
349 // finally for a dir (usually only need one of the last two).
350
351 if (isset($conffile)) {
352     $_config_files = array($conffile);
353 } else {
354     $_config_files = array('/etc/statusnet/statusnet.php',
355                            '/etc/statusnet/laconica.php',
356                            '/etc/laconica/laconica.php',
357                            '/etc/statusnet/'.$_server.'.php',
358                            '/etc/laconica/'.$_server.'.php');
359
360     if (strlen($_path) > 0) {
361         $_config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
362         $_config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
363     }
364
365     $_config_files[] = INSTALLDIR.'/config.php';
366 }
367
368 $_have_a_config = false;
369
370 foreach ($_config_files as $_config_file) {
371     if (@file_exists($_config_file)) {
372         include_once($_config_file);
373         $_have_a_config = true;
374     }
375 }
376
377 function _have_config()
378 {
379     global $_have_a_config;
380     return $_have_a_config;
381 }
382
383 // XXX: Throw a conniption if database not installed
384
385 // Fixup for statusnet.ini
386
387 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
388
389 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
390     $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
391 }
392
393 function __autoload($cls)
394 {
395     if (file_exists(INSTALLDIR.'/classes/' . $cls . '.php')) {
396         require_once(INSTALLDIR.'/classes/' . $cls . '.php');
397     } else if (file_exists(INSTALLDIR.'/lib/' . strtolower($cls) . '.php')) {
398         require_once(INSTALLDIR.'/lib/' . strtolower($cls) . '.php');
399     } else if (mb_substr($cls, -6) == 'Action' &&
400                file_exists(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php')) {
401         require_once(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
402     } else if ($cls == 'OAuthRequest') {
403         require_once('OAuth.php');
404     } else {
405         Event::handle('Autoload', array(&$cls));
406     }
407 }
408
409 // XXX: how many of these could be auto-loaded on use?
410 // XXX: note that these files should not use config options
411 // at compile time since DB config options are not yet loaded.
412
413 require_once 'Validate.php';
414 require_once 'markdown.php';
415
416 require_once INSTALLDIR.'/lib/util.php';
417 require_once INSTALLDIR.'/lib/action.php';
418 require_once INSTALLDIR.'/lib/theme.php';
419 require_once INSTALLDIR.'/lib/mail.php';
420 require_once INSTALLDIR.'/lib/subs.php';
421 require_once INSTALLDIR.'/lib/Shorturl_api.php';
422
423 require_once INSTALLDIR.'/lib/clientexception.php';
424 require_once INSTALLDIR.'/lib/serverexception.php';
425
426 // Load settings from database; note we need autoload for this
427
428 Config::loadSettings();
429
430 // XXX: other formats here
431
432 define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER);
433
434 // Give plugins a chance to initialize in a fully-prepared environment
435
436 Event::handle('InitializePlugin');