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