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