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