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