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