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