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