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