Updating functions_datastore.php
[bugdar.git] / userctrl.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'userctrl',
24 'userctrl_column',
25 'userctrl_search'
26 );
27
28 define('SVN', '$Id$');
29
30 $focus['user'] = 'focus';
31
32 require_once('./global.php');
33 require_once('./includes/api_user.php');
34
35 if (!bugdar::$userinfo['userid'])
36 {
37 $message->errorPermission();
38 }
39
40 $userapi = new UserAPI();
41 $userapi->set('userid', bugdar::$userinfo['userid']);
42 $userapi->setCondition();
43
44 $userinfo = bugdar::$userinfo;
45
46 // ###################################################################
47
48 if (empty($_REQUEST['do']))
49 {
50 $_REQUEST['do'] = 'modify';
51 }
52
53 // ###################################################################
54
55 if ($_POST['do'] == 'killsearch')
56 {
57 $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid']);
58 if (!$search)
59 {
60 $message->errorPermission();
61 }
62
63 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->in['searchid']);
64
65 $message->redirect(T('This saved search has been removed from your list.'), 'userctrl.php');
66 }
67
68 // ###################################################################
69
70 if ($_REQUEST['do'] == 'deletesearch')
71 {
72 $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid']);
73 if (!$search)
74 {
75 $message->errorPermission();
76 }
77
78 $message->confirm(T('Are you sure you want to delete this saved search?'), 'userctrl.php', 'killsearch', T('Delete'), T('Cancel'), array('searchid' => $input->in['searchid']));
79 }
80
81 // ###################################################################
82
83 if ($_POST['do'] == 'update')
84 {
85 // -------------------------------------------------------------------
86 // authentication
87 if (!empty($input->in['password_change']) OR !empty($input->in['email_change']))
88 {
89 if (empty($input->in['validate']))
90 {
91 $message->addError(T('You need to enter your current password to change your email or password'));
92 }
93 else
94 {
95 if (md5(md5($input->in['validate']) . md5(bugdar::$userinfo['salt'])) != bugdar::$userinfo['password'])
96 {
97 $message->addError(T('Your authentication password does not match the one in our records'));
98 }
99 }
100 }
101
102 // -------------------------------------------------------------------
103 // email validation
104 if (!empty($input->in['email_change']))
105 {
106 if (!empty($input->in['email_change']) AND empty($input->in['email_confirm']))
107 {
108 $message->addError(T('You need to enter both the email and confirm email fields to change your address'));
109 }
110
111 if ($input->in['email_change'] != $input->in['email_confirm'])
112 {
113 $message->addError(T('Your email and confirm email addresses do not match'));
114 }
115
116 $userapi->set('email', $input->in['email_change']);
117 }
118
119 // -------------------------------------------------------------------
120 // password validation
121 if (!empty($input->in['password_change']))
122 {
123 if (!empty($input->in['password_change']) AND empty($input->in['password_confirm']))
124 {
125 $message->addError(T('You need to enter both the password and confirm password fields to change your password'));
126 }
127 else
128 {
129 if ($input->in['password_change'] != $input->in['password_confirm'])
130 {
131 $message->addError(T('Your password and confirm password do not match'));
132 }
133 }
134
135 $userapi->set('password', $input->in['password_change']);
136 }
137
138 $userapi->set('displayname', $input->in['displayname']);
139 $userapi->set('showemail', $input->in['showemail']);
140 $userapi->set('showcolors', $input->in['showcolors']);
141 $userapi->set('languageid', $input->in['languageid']);
142 $userapi->set('timezone', $input->in['timezone']);
143 $userapi->set('usedst', $input->in['usedst']);
144 $userapi->set('hidestatuses', $input->in['hidestatuses']);
145 $userapi->set('defaultsortkey', $input->in['defaultsortkey']);
146 $userapi->set('defaultsortas', $input->in['defaultsortas']);
147 $userapi->set('columnoptions', $input->inputClean('columnoptions', TYPE_UINT));
148
149 // -------------------------------------------------------------------
150 // copy fields
151 $userinfo['displayname'] = $input->in['displayname'];
152 $userinfo['showemail'] = $input->in['showemail'];
153 $userinfo['showcolors'] = $input->in['showcolors'];
154 $userinfo['languageid'] = $input->in['languageid'];
155 $userinfo['timezone'] = $input->in['timezone'];
156 $userinfo['usedst'] = $input->in['usedst'];
157 $userinfo['hidestatuses'] = $input->in['hidestatuses'];
158 $userinfo['defaultsorkey'] = $input->in['defaultsorkey'];
159 $userinfo['defaultsortas'] = $input->in['defaultsortas'];
160 $userinfo['columnoptions'] = $input->in['columnoptions'];
161
162 $email = $input->in['email_change'];
163 $email_confirm = $input->in['email_confirm'];
164
165 // -------------------------------------------------------------------
166 // error handling
167 if ($message->hasErrors())
168 {
169 $show['errors'] = true;
170 $_REQUEST['do'] = 'modify';
171 }
172 else
173 {
174 $userapi->update();
175
176 $db->query("DELETE FROM " . TABLE_PREFIX . "useremail WHERE userid = " . bugdar::$userinfo['userid']);
177 $input->inputClean('emailopts', TYPE_INT);
178 if (is_array($input->in['emailopts']))
179 {
180 foreach ($input->in['emailopts'] AS $relation => $bitarr)
181 {
182 $bitmask = 0;
183 if (is_array($bitarr))
184 {
185 foreach ($bitarr AS $option => $yes)
186 {
187 $bitmask += $option * $yes;
188 }
189 }
190 $db->query("INSERT INTO " . TABLE_PREFIX . "useremail (userid, relation, mask) VALUES (" . bugdar::$userinfo['userid'] . ", $relation, $bitmask)");
191 }
192 }
193
194 $message->redirect(T('The changes to your account have been made.'), 'userctrl.php');
195 }
196 }
197
198 // ###################################################################
199
200 if ($_REQUEST['do'] == 'modify')
201 {
202 $langselect = construct_datastore_select('language', 'title', 'languageid', $userinfo['languageid']);
203
204 $hidestatuses = construct_datastore_select('status', 'status', 'statusid', (!is_array($userinfo['hidestatuses']) ? explode(',', $userinfo['hidestatuses']) : $userinfo['hidestatuses']), 0);
205 $hidestatusesnum = (sizeof(bugdar::$datastore['status']) < 8 ? sizeof(bugdar::$datastore['status']) + 1 : 8);
206
207 $defaultsortkey = construct_option_select('defaultsortkey', ListSorter::fetch_by_text(false), $userinfo['defaultsortkey']);
208 $defaultsortas = construct_option_select('defaultsortas', ListSorter::fetch_as_text(false), $userinfo['defaultsortas']);
209
210 foreach ($datef->fetch_timezone_list() as $value => $label)
211 {
212 $tpl = new BSTemplate('selectoption');
213 $tpl->vars = array(
214 'value' => $value,
215 'label' => $label,
216 'selected' => ($value == $userinfo['timezone'])
217 );
218 $tzselect .= $tpl->evaluate()->getTemplate();
219 }
220
221 $checked = array();
222 if (!is_array($input->in['emailopts']))
223 {
224 $options = $db->query("SELECT * FROM " . TABLE_PREFIX . "useremail WHERE userid = " . bugdar::$userinfo['userid']);
225 foreach ($options as $opt)
226 {
227 foreach (bugdar::$emailOptions['notifications'] AS $name => $notif)
228 {
229 foreach (bugdar::$emailOptions['relations'] AS $name => $relation)
230 {
231 if ($opt['mask'] & $notif AND $opt['relation'] == $relation)
232 {
233 $checked["$relation"]["$notif"] = HTML_CHECKED;
234 }
235 }
236 }
237 }
238 }
239 else
240 {
241 $checked = $input->in['emailopts'];
242 foreach ($checked AS $key1 => $value1)
243 {
244 foreach ($value1 AS $key2 => $value2)
245 {
246 if ($value2)
247 {
248 $checked["$key1"]["$key2"] = HTML_CHECKED;
249 }
250 }
251 }
252 }
253
254 $columns = array();
255 $columnOptions = '';
256 require_once('./includes/class_sort.php');
257 if (!is_array($input->in['columnoptions']))
258 {
259 foreach (ListSorter::fetch_by_text(false) AS $column => $name)
260 {
261 if (is_array(bugdar::$userinfo['columnoptions']))
262 {
263 $columns["$column"] = bugdar::$userinfo['columnoptions']["$column"];
264 }
265 else
266 {
267 $columns["$column"] = bugdar::$options['columnoptions']["$column"];
268 }
269 $tpl = new BSTemplate('userctrl_column');
270 $tpl->vars = array(
271 'columns' => $columns,
272 'column' => $column,
273 'name' => $name
274 );
275 $columnOptions .= $tpl->evaluate()->getTemplate();
276 }
277 }
278 else
279 {
280 $columns = $input->in['columnoptions'];
281 foreach (ListSorter::fetch_by_text(false) as $column => $name)
282 {
283 $tpl = new BSTemplate('userctrl_column');
284 $tpl->vars = array(
285 'columns' => $columns,
286 'column' => $column,
287 'name' => $name
288 );
289 $columnOptions .= $tpl->evaluate()->getTemplate();
290 }
291 }
292
293 // searches
294 $searches = '';
295 $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . bugdar::$userinfo['userid']);
296 foreach ($searchesFetch as $search)
297 {
298 $tpl = new BSTemplate('userctrl_search');
299 $tpl->vars = array('search' => $search);
300 $searches .= $tpl->evaluate()->getTemplate();
301 }
302
303 $tpl = new BSTemplate('userctrl');
304 $tpl->vars = array(
305 'checked' => $checked,
306 'userinfo' => $userinfo,
307 'searches' => $searches,
308 'defaultsortkey' => $defaultsortkey,
309 'hidestatusesnum' => $hidestatusesnum,
310 'hidestatuses' => $hidestatuses,
311 'defaultsortas' => $defaultsortas,
312 'columnOptions' => $columnOptions,
313 'langselect' => $langselect,
314 'tzselect' => $tzselect
315 );
316 $tpl->evaluate()->flush();
317 }
318
319 /*=====================================================================*\
320 || ###################################################################
321 || # $HeadURL$
322 || # $Id$
323 || ###################################################################
324 \*=====================================================================*/
325 ?>