]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/common.php
Merge branch '0.9.x' into schema
[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
57 require_once(INSTALLDIR.'/lib/language.php');
58
59 // This gets included before the config file, so that admin code and plugins
60 // can use it
61
62 require_once(INSTALLDIR.'/lib/event.php');
63 require_once(INSTALLDIR.'/lib/plugin.php');
64
65 function _sn_to_path($sn)
66 {
67     $past_root = substr($sn, 1);
68     $last_slash = strrpos($past_root, '/');
69     if ($last_slash > 0) {
70         $p = substr($past_root, 0, $last_slash);
71     } else {
72         $p = '';
73     }
74     return $p;
75 }
76
77 // try to figure out where we are. $server and $path
78 // can be set by including module, else we guess based
79 // on HTTP info.
80
81 if (isset($server)) {
82     $_server = $server;
83 } else {
84     $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
85       strtolower($_SERVER['SERVER_NAME']) :
86     null;
87 }
88
89 if (isset($path)) {
90     $_path = $path;
91 } else {
92     $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
93       _sn_to_path($_SERVER['SCRIPT_NAME']) :
94     null;
95 }
96
97 require_once(INSTALLDIR.'/lib/default.php');
98
99 // Set config values initially to default values
100
101 $config = $default;
102
103 // default configuration, overwritten in config.php
104
105 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
106
107 $config['db'] = $default['db'];
108
109 // Backward compatibility
110
111 $config['site']['design'] =& $config['design'];
112
113 if (function_exists('date_default_timezone_set')) {
114     /* Work internally in UTC */
115     date_default_timezone_set('UTC');
116 }
117
118 function addPlugin($name, $attrs = null)
119 {
120     $name = ucfirst($name);
121     $pluginclass = "{$name}Plugin";
122
123     if (!class_exists($pluginclass)) {
124
125         $files = array("local/plugins/{$pluginclass}.php",
126                        "local/plugins/{$name}/{$pluginclass}.php",
127                        "local/{$pluginclass}.php",
128                        "local/{$name}/{$pluginclass}.php",
129                        "plugins/{$pluginclass}.php",
130                        "plugins/{$name}/{$pluginclass}.php");
131
132         foreach ($files as $file) {
133             $fullpath = INSTALLDIR.'/'.$file;
134             if (@file_exists($fullpath)) {
135                 include_once($fullpath);
136                 break;
137             }
138         }
139     }
140
141     $inst = new $pluginclass();
142
143     if (!empty($attrs)) {
144         foreach ($attrs as $aname => $avalue) {
145             $inst->$aname = $avalue;
146         }
147     }
148     return $inst;
149 }
150
151 // From most general to most specific:
152 // server-wide, then vhost-wide, then for a path,
153 // finally for a dir (usually only need one of the last two).
154
155 if (isset($conffile)) {
156     $_config_files = array($conffile);
157 } else {
158     $_config_files = array('/etc/statusnet/statusnet.php',
159                            '/etc/statusnet/laconica.php',
160                            '/etc/laconica/laconica.php',
161                            '/etc/statusnet/'.$_server.'.php',
162                            '/etc/laconica/'.$_server.'.php');
163
164     if (strlen($_path) > 0) {
165         $_config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
166         $_config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
167     }
168
169     $_config_files[] = INSTALLDIR.'/config.php';
170 }
171
172 $_have_a_config = false;
173
174 foreach ($_config_files as $_config_file) {
175     if (@file_exists($_config_file)) {
176         include_once($_config_file);
177         $_have_a_config = true;
178     }
179 }
180
181 function _have_config()
182 {
183     global $_have_a_config;
184     return $_have_a_config;
185 }
186
187 // XXX: Throw a conniption if database not installed
188
189 // Fixup for statusnet.ini
190
191 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
192
193 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
194     $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
195 }
196
197 function __autoload($cls)
198 {
199     if (file_exists(INSTALLDIR.'/classes/' . $cls . '.php')) {
200         require_once(INSTALLDIR.'/classes/' . $cls . '.php');
201     } else if (file_exists(INSTALLDIR.'/lib/' . strtolower($cls) . '.php')) {
202         require_once(INSTALLDIR.'/lib/' . strtolower($cls) . '.php');
203     } else if (mb_substr($cls, -6) == 'Action' &&
204                file_exists(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php')) {
205         require_once(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
206     } else if ($cls == 'OAuthRequest') {
207         require_once('OAuth.php');
208     } else {
209         Event::handle('Autoload', array(&$cls));
210     }
211 }
212
213 // XXX: how many of these could be auto-loaded on use?
214 // XXX: note that these files should not use config options
215 // at compile time since DB config options are not yet loaded.
216
217 require_once 'Validate.php';
218 require_once 'markdown.php';
219
220 require_once INSTALLDIR.'/lib/util.php';
221 require_once INSTALLDIR.'/lib/action.php';
222 require_once INSTALLDIR.'/lib/theme.php';
223 require_once INSTALLDIR.'/lib/mail.php';
224 require_once INSTALLDIR.'/lib/subs.php';
225 require_once INSTALLDIR.'/lib/Shorturl_api.php';
226 require_once INSTALLDIR.'/lib/twitter.php';
227
228 require_once INSTALLDIR.'/lib/clientexception.php';
229 require_once INSTALLDIR.'/lib/serverexception.php';
230
231 // Load settings from database; note we need autoload for this
232
233 Config::loadSettings();
234
235 // XXX: other formats here
236
237 define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER);
238
239 // Give plugins a chance to initialize in a fully-prepared environment
240
241 Event::handle('InitializePlugin');