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