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