]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/common.php
make config kinda work
[quix0rs-gnu-social.git] / lib / common.php
1 <?php
2 /* 
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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 /* XXX: break up into separate modules (HTTP, HTML, user, files) */
21
22
23 if (!defined('LACONICA')) { exit(1) }
24
25 define('AVATAR_PROFILE_SIZE', 96);
26 define('AVATAR_STREAM_SIZE', 48);
27 define('AVATAR_MINI_SIZE', 24);
28 define('MAX_AVATAR_SIZE', 256 * 1024);
29
30 # global configuration object
31
32 require_once('PEAR.php');
33 require_once('DB_DataObject.php');
34
35 // default configuration, overwritten in config.php
36
37 $config =
38   array('site' =>
39                 array('name' => 'Just another Laconica microblog',
40                           'server' => 'localhost',
41                           'path' => '/'),
42                 'avatar' =>
43                 array('directory' => INSTALLDIR . 'files',
44                           'path' => '/files'),
45                 'db' => &PEAR::getStaticProperty('DB_DataObject','options'),
46 );
47
48 $config['db'] = 
49   array('database' => 'YOU HAVE TO SET THIS IN config.php',
50             'schema_location' => $INSTALLDIR . '/classes',
51                 'class_location' => $INSTALLDIR . '/classes',
52                 'require_prefix' => 'classes/',
53                 'class_prefix' => '',
54         'db_driver' => 'MDB2',
55                 'quote_identifiers' => false);
56
57 require_once(INSTALLDIR . '/config.php');
58
59 # Show a server error
60
61 function common_server_error($msg) {
62         header('Status: 500 Server Error');
63         header('Content-type: text/plain');
64
65         print $msg;
66         exit();
67 }
68
69 # Show a user error
70 function common_user_error($msg, $code=200) {
71         common_show_header('Error');
72         common_element('div', array('class' => 'error'), $msg);
73         common_show_footer();
74 }
75
76 # Start an HTML element
77 function common_element_start($tag, $attrs=NULL) {
78         print "<$tag";
79         if (is_array($attrs)) {
80                 foreach ($attrs as $name => $value) {
81                         print " $name='$value'";
82                 }
83         } else if (is_string($attrs)) {
84                 print " class='$attrs'";
85         }
86         print '>';
87 }
88
89 function common_element_end($tag) {
90         print "</$tag>";
91 }
92
93 function common_element($tag, $attrs=NULL, $content=NULL) {
94     common_element_start($tag, $attrs);
95         if ($content) print htmlspecialchars($content);
96         common_element_end($tag);
97 }
98
99 function common_show_header($pagetitle) {
100         global $config;
101         common_element_start('html');
102         common_element_start('head');
103         common_element('title', NULL, 
104                                    $pagetitle . " - " . $config['site']['name']);
105         common_element_end('head');
106         common_element_start('body');
107         common_head_menu();
108 }
109
110 function common_show_footer() {
111         common_foot_menu();
112         common_element_end('body');
113         common_element_end('html');
114 }
115
116 function common_head_menu() {
117         $user = common_current_user();
118         common_element_start('ul', 'headmenu');
119         common_menu_item(common_local_url('doc', array('title' => 'help')),
120                                          _t('Help'));
121         if ($user) {
122                 common_menu_item(common_local_url('all', array('nickname' => 
123                                                                                                            $user->nickname)),
124                                                  _t('Home'));
125                 common_menu_item(common_local_url('showstream', array('nickname' =>
126                                                                                                                           $user->nickname)),
127                                                  _t('Profile'),  $user->fullname || $user->nickname);
128                 common_menu_item(common_local_url('profilesettings'),
129                                                  _t('Settings'));
130                 common_menu_item(common_local_url('logout'),
131                                                  _t('Logout'));
132         } else {
133                 common_menu_item(common_local_url('login'),
134                                                  _t('Login'));
135                 common_menu_item(common_local_url('register'),
136                                                  _t('Register'));
137         }
138         common_element_end('ul');
139 }
140
141 function common_foot_menu() {
142         common_element_start('ul', 'footmenu');
143         common_menu_item(common_local_url('doc', array('title' => 'about')),
144                                          _t('About'));
145         common_menu_item(common_local_url('doc', array('title' => 'help')),
146                                          _t('Help'));
147         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
148                                          _t('Privacy'));
149 }
150
151 function common_menu_item($url, $text, $title=NULL) {
152         $attrs['href'] = $url;
153         if ($title) {
154                 $attrs['title'] = $title;
155         }
156         common_element_start('li', 'menuitem');
157         common_element('a', $attrs, $text);
158         common_element_end('li');
159 }
160
161 function common_input($id, $label) {
162         common_element('label', array('for' => $id), $label);
163         common_element('input', array('name' => $id,
164                                                                   'type' => 'text',
165                                                                   'id' => $id));
166 }
167
168 # salted, hashed passwords are stored in the DB
169
170 function common_munge_password($id, $password) {
171         return md5($id . $password);
172 }
173
174 # check if a username exists and has matching password
175 function common_check_user($nickname, $password) {
176         $user = User::staticGet('nickname', $nickname);
177         if (is_null($user)) {
178                 return false;
179         } else {
180                 return (0 == strcmp(common_munge_password($password, $user->id), 
181                                                         $user->password));
182         }
183 }
184
185 # is the current user logged in?
186 function common_logged_in() {
187         return (!is_null(common_current_user()));
188 }
189
190 function common_have_session() {
191         return (0 != strcmp(session_id(), ''));
192 }
193
194 function common_ensure_session() {
195         if (!common_have_session()) {
196                 @session_start();
197         }
198 }
199
200 function common_set_user($nickname) {
201         if (is_null($nickname) && common_have_session()) {
202                 unset($_SESSION['userid']);
203                 return true;
204         } else {
205                 $user = User::staticGet('nickname', $nickname);
206                 if ($user) {
207                         common_ensure_session();
208                         $_SESSION['userid'] = $user->id;
209                         return true;
210                 } else {
211                         return false;
212                 }
213         }
214         return false;
215 }
216
217 # who is the current user?
218 function common_current_user() {
219         static $user = NULL; # FIXME: global memcached
220         if (is_null($user)) {
221                 if (common_have_session()) {
222                         $id = $_SESSION['userid'];
223                         if ($id) {
224                                 $user = User::staticGet($id);
225                         }
226                 }
227         }
228         return $user;
229 }
230
231 # get canonical version of nickname for comparison
232 function common_canonical_nickname($nickname) {
233         # XXX: UTF-8 canonicalization (like combining chars)
234         return strtolower($nickname);
235 }
236
237 function common_render_content($text) {
238         # XXX: @ messages
239         # XXX: # tags
240         # XXX: machine tags
241         return htmlspecialchars($text);
242 }
243
244 // where should the avatar go for this user?
245
246 function common_avatar_filename($user, $extension, $size=NULL) {
247         global $config;
248
249         if ($size) {
250                 return $user->id . '-' . $size . $extension;
251         } else {
252                 return $user->id . '-original' . $extension;
253         }
254 }
255
256 function common_avatar_path($filename) {
257         global $config;
258         return $config['avatar']['directory'] . '/' . $filename;
259 }
260
261 function common_avatar_url($filename) {
262         global $config;
263         return $config['avatar']['path'] . '/' . $filename;
264 }
265
266 function common_local_url($action, $args) {
267         global $config;
268         /* XXX: pretty URLs */
269         $extra = '';
270         foreach ($args as $key => $value) {
271                 $extra .= "&${key}=${value}";
272         }
273         return "http://".$config['site']['server'].'/'.$config['site']['path']."/index.php?action=${action}${extra}";
274 }
275
276 function commmon_date_string($dt) {
277         // XXX: do some sexy date formatting
278         return date(DATE_RFC822);
279 }
280
281 function common_redirect($url, $code=307) {
282         static $status = (301 => "Moved Permanently",
283                                           302 => "Found",
284                                           303 => "See Other",
285                                           307 => "Temporary Redirect");
286         header("Status: ${code} $status[$code]");
287         header("Location: $url");
288         common_element('a', array('href' => $url), $url);
289 }
290
291 function common_broadcast_notices($id) {
292         // XXX: broadcast notices to remote subscribers
293         // XXX: broadcast notices to SMS
294         // XXX: broadcast notices to Jabber
295         // XXX: broadcast notices to other IM
296         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
297         return true;
298 }
299
300 // XXX: set up gettext
301
302 function _t($str) { $str }