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