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