]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/common.php
try to fix arghandling in action
[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 function common_render_content($text) {
240         # XXX: @ messages
241         # XXX: # tags
242         # XXX: machine tags
243         return htmlspecialchars($text);
244 }
245
246 // where should the avatar go for this user?
247
248 function common_avatar_filename($user, $extension, $size=NULL) {
249         global $config;
250
251         if ($size) {
252                 return $user->id . '-' . $size . $extension;
253         } else {
254                 return $user->id . '-original' . $extension;
255         }
256 }
257
258 function common_avatar_path($filename) {
259         global $config;
260         return $config['avatar']['directory'] . '/' . $filename;
261 }
262
263 function common_avatar_url($filename) {
264         global $config;
265         return $config['avatar']['path'] . '/' . $filename;
266 }
267
268 function common_local_url($action, $args=NULL) {
269         global $config;
270         /* XXX: pretty URLs */
271         $extra = '';
272         if ($args) {
273                 foreach ($args as $key => $value) {
274                         $extra .= "&${key}=${value}";
275                 }
276         }
277         $pathpart = ($config['site']['path']) ? $config['site']['path']."/" : '';
278         return "http://".$config['site']['server'].'/'.$pathpart."index.php?action=${action}${extra}";
279 }
280
281 function commmon_date_string($dt) {
282         // XXX: do some sexy date formatting
283         return date(DATE_RFC822);
284 }
285
286 function common_redirect($url, $code=307) {
287         static $status = array(301 => "Moved Permanently",
288                                                    302 => "Found",
289                                                    303 => "See Other",
290                                                    307 => "Temporary Redirect");
291         header("Status: ${code} $status[$code]");
292         header("Location: $url");
293         common_element('a', array('href' => $url), $url);
294 }
295
296 function common_broadcast_notices($id) {
297         // XXX: broadcast notices to remote subscribers
298         // XXX: broadcast notices to SMS
299         // XXX: broadcast notices to Jabber
300         // XXX: broadcast notices to other IM
301         // XXX: use a queue system like http://code.google.com/p/microapps/wiki/NQDQ
302         return true;
303 }
304
305 // XXX: set up gettext
306
307 function _t($str) { 
308         return $str;
309 }