]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/common.php
b280afec02319d09fe1ff52a7d2e3e544b583c9d
[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 //exit with 200 response, if this is checking fancy from the installer
23 if (isset($_REQUEST['p']) && $_REQUEST['p'] == 'check-fancy') {  exit; }
24
25 define('STATUSNET_VERSION', '0.9.0rc2');
26 define('LACONICA_VERSION', STATUSNET_VERSION); // compatibility
27
28 define('STATUSNET_CODENAME', 'Stand');
29
30 define('AVATAR_PROFILE_SIZE', 96);
31 define('AVATAR_STREAM_SIZE', 48);
32 define('AVATAR_MINI_SIZE', 24);
33
34 define('NOTICES_PER_PAGE', 20);
35 define('PROFILES_PER_PAGE', 20);
36
37 define('FOREIGN_NOTICE_SEND', 1);
38 define('FOREIGN_NOTICE_RECV', 2);
39 define('FOREIGN_NOTICE_SEND_REPLY', 4);
40
41 define('FOREIGN_FRIEND_SEND', 1);
42 define('FOREIGN_FRIEND_RECV', 2);
43
44 # append our extlib dir as the last-resort place to find libs
45
46 set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/extlib/');
47
48 // To protect against upstream libraries which haven't updated
49 // for PHP 5.3 where dl() function may not be present...
50 if (!function_exists('dl')) {
51     // function_exists() returns false for things in disable_functions,
52     // but they still exist and we'll die if we try to redefine them.
53     //
54     // Fortunately trying to call the disabled one will only trigger
55     // a warning, not a fatal, so it's safe to leave it for our case.
56     // Callers will be suppressing warnings anyway.
57     $disabled = array_filter(array_map('trim', explode(',', ini_get('disable_functions'))));
58     if (!in_array('dl', $disabled)) {
59         function dl($library) {
60             return false;
61         }
62     }
63 }
64
65 # global configuration object
66
67 require_once('PEAR.php');
68 require_once('DB/DataObject.php');
69 require_once('DB/DataObject/Cast.php'); # for dates
70
71 require_once(INSTALLDIR.'/lib/language.php');
72
73 // This gets included before the config file, so that admin code and plugins
74 // can use it
75
76 require_once(INSTALLDIR.'/lib/event.php');
77 require_once(INSTALLDIR.'/lib/plugin.php');
78
79 function _sn_to_path($sn)
80 {
81     $past_root = substr($sn, 1);
82     $last_slash = strrpos($past_root, '/');
83     if ($last_slash > 0) {
84         $p = substr($past_root, 0, $last_slash);
85     } else {
86         $p = '';
87     }
88     return $p;
89 }
90
91 // Save our sanity when code gets loaded through subroutines such as PHPUnit tests
92 global $default, $config, $_server, $_path;
93
94 // try to figure out where we are. $server and $path
95 // can be set by including module, else we guess based
96 // on HTTP info.
97
98 if (isset($server)) {
99     $_server = $server;
100 } else {
101     $_server = array_key_exists('SERVER_NAME', $_SERVER) ?
102       strtolower($_SERVER['SERVER_NAME']) :
103     null;
104 }
105
106 if (isset($path)) {
107     $_path = $path;
108 } else {
109     $_path = (array_key_exists('SERVER_NAME', $_SERVER) && array_key_exists('SCRIPT_NAME', $_SERVER)) ?
110       _sn_to_path($_SERVER['SCRIPT_NAME']) :
111     null;
112 }
113
114 require_once(INSTALLDIR.'/lib/default.php');
115
116 // Set config values initially to default values
117
118 $config = $default;
119
120 // default configuration, overwritten in config.php
121
122 $config['db'] = &PEAR::getStaticProperty('DB_DataObject','options');
123
124 $config['db'] = $default['db'];
125
126 // Backward compatibility
127
128 $config['site']['design'] =& $config['design'];
129
130 if (function_exists('date_default_timezone_set')) {
131     /* Work internally in UTC */
132     date_default_timezone_set('UTC');
133 }
134
135 function addPlugin($name, $attrs = null)
136 {
137     $name = ucfirst($name);
138     $pluginclass = "{$name}Plugin";
139
140     if (!class_exists($pluginclass)) {
141
142         $files = array("local/plugins/{$pluginclass}.php",
143                        "local/plugins/{$name}/{$pluginclass}.php",
144                        "local/{$pluginclass}.php",
145                        "local/{$name}/{$pluginclass}.php",
146                        "plugins/{$pluginclass}.php",
147                        "plugins/{$name}/{$pluginclass}.php");
148
149         foreach ($files as $file) {
150             $fullpath = INSTALLDIR.'/'.$file;
151             if (@file_exists($fullpath)) {
152                 include_once($fullpath);
153                 break;
154             }
155         }
156     }
157
158     $inst = new $pluginclass();
159
160     if (!empty($attrs)) {
161         foreach ($attrs as $aname => $avalue) {
162             $inst->$aname = $avalue;
163         }
164     }
165     return $inst;
166 }
167
168 // From most general to most specific:
169 // server-wide, then vhost-wide, then for a path,
170 // finally for a dir (usually only need one of the last two).
171
172 if (isset($conffile)) {
173     $_config_files = array($conffile);
174 } else {
175     $_config_files = array('/etc/statusnet/statusnet.php',
176                            '/etc/statusnet/laconica.php',
177                            '/etc/laconica/laconica.php',
178                            '/etc/statusnet/'.$_server.'.php',
179                            '/etc/laconica/'.$_server.'.php');
180
181     if (strlen($_path) > 0) {
182         $_config_files[] = '/etc/statusnet/'.$_server.'_'.$_path.'.php';
183         $_config_files[] = '/etc/laconica/'.$_server.'_'.$_path.'.php';
184     }
185
186     $_config_files[] = INSTALLDIR.'/config.php';
187 }
188
189 global $_have_a_config;
190 $_have_a_config = false;
191
192 foreach ($_config_files as $_config_file) {
193     if (@file_exists($_config_file)) {
194         include_once($_config_file);
195         $_have_a_config = true;
196     }
197 }
198
199 function _have_config()
200 {
201     global $_have_a_config;
202     return $_have_a_config;
203 }
204
205 // XXX: Throw a conniption if database not installed
206 // XXX: Find a way to use htmlwriter for this instead of handcoded markup
207 if (!_have_config()) {
208   echo '<p>'. _('No configuration file found. ') .'</p>';
209   echo '<p>'. _('I looked for configuration files in the following places: ') .'<br /> '. implode($_config_files, '<br />');
210   echo '<p>'. _('You may wish to run the installer to fix this.') .'</p>';
211   echo '<a href="install.php">'. _('Go to the installer.') .'</a>';
212   exit;
213 }
214 // Fixup for statusnet.ini
215
216 $_db_name = substr($config['db']['database'], strrpos($config['db']['database'], '/') + 1);
217
218 if ($_db_name != 'statusnet' && !array_key_exists('ini_'.$_db_name, $config['db'])) {
219     $config['db']['ini_'.$_db_name] = INSTALLDIR.'/classes/statusnet.ini';
220 }
221
222 // Backwards compatibility
223
224 if (array_key_exists('memcached', $config)) {
225     if ($config['memcached']['enabled']) {
226         addPlugin('Memcache', array('servers' => $config['memcached']['server']));
227     }
228
229     if (!empty($config['memcached']['base'])) {
230         $config['cache']['base'] = $config['memcached']['base'];
231     }
232 }
233
234 function __autoload($cls)
235 {
236     if (file_exists(INSTALLDIR.'/classes/' . $cls . '.php')) {
237         require_once(INSTALLDIR.'/classes/' . $cls . '.php');
238     } else if (file_exists(INSTALLDIR.'/lib/' . strtolower($cls) . '.php')) {
239         require_once(INSTALLDIR.'/lib/' . strtolower($cls) . '.php');
240     } else if (mb_substr($cls, -6) == 'Action' &&
241                file_exists(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php')) {
242         require_once(INSTALLDIR.'/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
243     } else if ($cls == 'OAuthRequest') {
244         require_once('OAuth.php');
245     } else {
246         Event::handle('Autoload', array(&$cls));
247     }
248 }
249
250 // Load default plugins
251
252 foreach ($config['plugins']['default'] as $name => $params) {
253     if (is_null($params)) {
254         addPlugin($name);
255     } else if (is_array($params)) {
256         if (count($params) == 0) {
257             addPlugin($name);
258         } else {
259             $keys = array_keys($params);
260             if (is_string($keys[0])) {
261                 addPlugin($name, $params);
262             } else {
263                 foreach ($params as $paramset) {
264                     addPlugin($name, $paramset);
265                 }
266             }
267         }
268     }
269 }
270
271 // XXX: how many of these could be auto-loaded on use?
272 // XXX: note that these files should not use config options
273 // at compile time since DB config options are not yet loaded.
274
275 require_once 'Validate.php';
276 require_once 'markdown.php';
277
278 require_once INSTALLDIR.'/lib/util.php';
279 require_once INSTALLDIR.'/lib/action.php';
280 require_once INSTALLDIR.'/lib/mail.php';
281 require_once INSTALLDIR.'/lib/subs.php';
282
283 require_once INSTALLDIR.'/lib/clientexception.php';
284 require_once INSTALLDIR.'/lib/serverexception.php';
285
286 // Load settings from database; note we need autoload for this
287
288 Config::loadSettings();
289
290 // XXX: if plugins should check the schema at runtime, do that here.
291
292 if ($config['db']['schemacheck'] == 'runtime') {
293     Event::handle('CheckSchema');
294 }
295
296 // XXX: other formats here
297
298 define('NICKNAME_FMT', VALIDATE_NUM.VALIDATE_ALPHA_LOWER);
299
300 // Give plugins a chance to initialize in a fully-prepared environment
301
302 Event::handle('InitializePlugin');