]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/common.php
move library files to lib
[quix0rs-gnu-social.git] / lib / common.php
1 <?php
2
3 if (!defined('MICROBLOG')) { exit(1) }
4
5 # global configuration object
6
7 // default configuration, overwritten in config.php
8
9 $config =
10   array('site' =>
11                 array('name' => 'Just another µB'),
12                 'dsn' =>
13                 array('phptype' => 'mysql',
14                           'username' => 'stoica',
15                           'password' => 'apasswd',
16                           'hostspec' => 'localhost',
17                           'database' => 'thedb')
18                 'dboptions' =>
19                 array('debug' => 2,
20                           'portability' => DB_PORTABILITY_ALL));
21
22 require_once(INSTALLDIR . '/config.php');
23 require_once('DB.php');
24
25 # Show a server error
26
27 function common_server_error($msg) {
28         header('Status: 500 Server Error');
29         header('Content-type: text/plain');
30
31         print $msg;
32         exit();
33 }
34
35 # Show a user error
36 function common_user_error($msg, $code=200) {
37         common_show_header('Error');
38         common_element('div', array('class' => 'error'), $msg);
39         common_show_footer();
40 }
41
42 # Start an HTML element
43 function common_element_start($tag, $attrs=NULL) {
44         print "<$tag";
45         if (is_array($attrs)) {
46                 foreach ($attrs as $name => $value) {
47                         print " $name='$value'";
48                 }
49         } else if (is_string($attrs)) {
50                 print " class='$attrs'";
51         }
52         print '>';
53 }
54
55 function common_element_end($tag) {
56         print "</$tag>";
57 }
58
59 function common_element($tag, $attrs=NULL, $content=NULL) {
60     common_element_start($tag, $attrs);
61         if ($content) print htmlspecialchars($content);
62         common_element_end($tag);
63 }
64
65 function common_show_header($pagetitle) {
66         global $config;
67         common_element_start('html');
68         common_element_start('head');
69         common_element('title', NULL, 
70                                    $pagetitle . " - " . $config['site']['name']);
71         common_element_end('head');
72         common_element_start('body');
73         common_head_menu();
74 }
75
76 function common_show_footer() {
77         common_foot_menu();
78         common_element_end('body');
79         common_element_end('html');
80 }
81
82 function common_head_menu() {
83         $user = common_current_user();
84         common_element_start('ul', 'headmenu');
85         common_menu_item(common_local_url('doc', array('title' => 'help')),
86                                          _t('Help'));
87         if ($user) {
88                 common_menu_item(common_local_url('all', array('nickname' => 
89                                                                                                            $user->nickname)),
90                                                  _t('Home'));
91                 common_menu_item(common_local_url('showstream', array('nickname' =>
92                                                                                                                           $user->nickname)),
93                                                  _t('Profile'),  $user->fullname || $user->nickname);
94                 common_menu_item(common_local_url('settings'),
95                                                  _t('Settings'));
96                 common_menu_item(common_local_url('logout'),
97                                                  _t('Logout'));
98         } else {
99                 common_menu_item(common_local_url('login'),
100                                                  _t('Login'));
101                 common_menu_item(common_local_url('register'),
102                                                  _t('Register'));
103         }
104         common_element_end('ul');
105 }
106
107 function common_foot_menu() {
108         common_element_start('ul', 'footmenu');
109         common_menu_item(common_local_url('doc', array('title' => 'about')),
110                                          _t('About'));
111         common_menu_item(common_local_url('doc', array('title' => 'help')),
112                                          _t('Help'));
113         common_menu_item(common_local_url('doc', array('title' => 'privacy')),
114                                          _t('Privacy'));
115 }
116
117 function common_menu_item($url, $text, $title=NULL) {
118         $attrs['href'] = $url;
119         if ($title) {
120                 $attrs['title'] = $title;
121         }
122         common_element_start('li', 'menuitem');
123         common_element('a', $attrs, $text);
124         common_element_end('li');
125 }
126
127 # salted, hashed passwords are stored in the DB
128
129 function common_munge_password($id, $password) {
130         return md5($id . $password);
131 }
132
133 # check if a username exists and has matching password
134 function common_check_user($nickname, $password) {
135         $user = User::staticGet('nickname', $nickname);
136         if (is_null($user)) {
137                 return false;
138         } else {
139                 return (0 == strcmp(common_munge_password($password, $user->id), 
140                                                         $user->password));
141         }
142 }
143
144 # is the current user logged in?
145 function common_logged_in() {
146         return (!is_null(common_current_user()));
147 }
148
149 function common_have_session() {
150         return (0 != strcmp(session_id(), ''));
151 }
152
153 function common_ensure_session() {
154         if (!common_have_session()) {
155                 @session_start();
156         }
157 }
158
159 function common_set_user($nickname) {
160         if (is_null($nickname) && common_have_session()) {
161                 unset($_SESSION['userid']);
162                 return true;
163         } else {
164                 $user = User::staticGet('nickname', $nickname);
165                 if ($user) {
166                         common_ensure_session();
167                         $_SESSION['userid'] = $user->id;
168                         return true;
169                 } else {
170                         return false;
171                 }
172         }
173         return false;
174 }
175
176 # who is the current user?
177 function common_current_user() {
178         static $user = NULL; # FIXME: global memcached
179         if (is_null($user)) {
180                 if (common_have_session()) {
181                         $id = $_SESSION['userid'];
182                         if ($id) {
183                                 $user = User::staticGet($id);
184                         }
185                 }
186         }
187         return $user;
188 }
189
190 # get canonical version of nickname for comparison
191 function common_canonical_nickname($nickname) {
192         # XXX: UTF-8 canonicalization (like combining chars)
193         return strtolower($nickname);
194 }
195
196 function common_render_content($text) {
197         # XXX: @ messages
198         # XXX: # tags
199         # XXX: machine tags
200         return htmlspecialchars($text);
201 }
202
203 // XXX: set up gettext
204
205 function _t($str) { $str }