]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/common.php
license block for source code
[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 if (!defined('LACONICA')) { exit(1) }
21
22 # global configuration object
23
24 // default configuration, overwritten in config.php
25
26 $config =
27   array('site' =>
28                 array('name' => 'Just another µB'),
29                 'dsn' =>
30                 array('phptype' => 'mysql',
31                           'username' => 'stoica',
32                           'password' => 'apasswd',
33                           'hostspec' => 'localhost',
34                           'database' => 'thedb')
35                 'dboptions' =>
36                 array('debug' => 2,
37                           'portability' => DB_PORTABILITY_ALL));
38
39 require_once(INSTALLDIR . '/config.php');
40 require_once('DB.php');
41
42 # Show a server error
43
44 function common_server_error($msg) {
45         header('Status: 500 Server Error');
46         header('Content-type: text/plain');
47
48         print $msg;
49         exit();
50 }
51
52 # Show a user error
53 function common_user_error($msg, $code=200) {
54         common_show_header('Error');
55         common_element('div', array('class' => 'error'), $msg);
56         common_show_footer();
57 }
58
59 # Start an HTML element
60 function common_element_start($tag, $attrs=NULL) {
61         print "<$tag";
62         if (is_array($attrs)) {
63                 foreach ($attrs as $name => $value) {
64                         print " $name='$value'";
65                 }
66         } else if (is_string($attrs)) {
67                 print " class='$attrs'";
68         }
69         print '>';
70 }
71
72 function common_element_end($tag) {
73         print "</$tag>";
74 }
75
76 function common_element($tag, $attrs=NULL, $content=NULL) {
77     common_element_start($tag, $attrs);
78         if ($content) print htmlspecialchars($content);
79         common_element_end($tag);
80 }
81
82 function common_show_header($pagetitle) {
83         global $config;
84         common_element_start('html');
85         common_element_start('head');
86         common_element('title', NULL, 
87                                    $pagetitle . " - " . $config['site']['name']);
88         common_element_end('head');
89         common_element_start('body');
90         common_head_menu();
91 }
92
93 function common_show_footer() {
94         common_foot_menu();
95         common_element_end('body');
96         common_element_end('html');
97 }
98
99 function common_head_menu() {
100         $user = common_current_user();
101         common_element_start('ul', 'headmenu');
102         common_menu_item(common_local_url('doc', array('title' => 'help')),
103                                          _t('Help'));
104         if ($user) {
105                 common_menu_item(common_local_url('all', array('nickname' => 
106                                                                                                            $user->nickname)),
107                                                  _t('Home'));
108                 common_menu_item(common_local_url('showstream', array('nickname' =>
109                                                                                                                           $user->nickname)),
110                                                  _t('Profile'),  $user->fullname || $user->nickname);
111                 common_menu_item(common_local_url('settings'),
112                                                  _t('Settings'));
113                 common_menu_item(common_local_url('logout'),
114                                                  _t('Logout'));
115         } else {
116                 common_menu_item(common_local_url('login'),
117                                                  _t('Login'));
118                 common_menu_item(common_local_url('register'),
119                                                  _t('Register'));
120         }
121         common_element_end('ul');
122 }
123
124 function common_foot_menu() {
125         common_element_start('ul', 'footmenu');
126         common_menu_item(common_local_url('doc', array('title' => 'about')),
127                                          _t('About'));
128         common_menu_item(common_local_url('doc', array('title' => 'help')),
129                                          _t('Help'));
130         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
131                                          _t('Privacy'));
132 }
133
134 function common_menu_item($url, $text, $title=NULL) {
135         $attrs['href'] = $url;
136         if ($title) {
137                 $attrs['title'] = $title;
138         }
139         common_element_start('li', 'menuitem');
140         common_element('a', $attrs, $text);
141         common_element_end('li');
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                 if (common_have_session()) {
198                         $id = $_SESSION['userid'];
199                         if ($id) {
200                                 $user = User::staticGet($id);
201                         }
202                 }
203         }
204         return $user;
205 }
206
207 # get canonical version of nickname for comparison
208 function common_canonical_nickname($nickname) {
209         # XXX: UTF-8 canonicalization (like combining chars)
210         return strtolower($nickname);
211 }
212
213 function common_render_content($text) {
214         # XXX: @ messages
215         # XXX: # tags
216         # XXX: machine tags
217         return htmlspecialchars($text);
218 }
219
220 // XXX: set up gettext
221
222 function _t($str) { $str }