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