index.php 11.4 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7
8
9
10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12
13
14
15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
Francois Marier's avatar
Francois Marier committed
17
18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20
21
 *
 * @package    mahara
 * @subpackage core
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25
26
27
28
 *
 */

define('INTERNAL', 1);
29
define('MENUITEM', 'settings/account');
30
31
32
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'account');
define('SECTION_PAGE', 'preferences');
33
34

require(dirname(dirname(__FILE__)) . '/init.php');
35
define('TITLE', get_string('account'));
36
require_once('pieforms/pieform.php');
37

Penny Leach's avatar
Penny Leach committed
38
// load up user preferences
39
$prefs = (object)($USER->accountprefs);
Penny Leach's avatar
Penny Leach committed
40

41
$authobj = AuthFactory::create($USER->authinstance);
42
43

// @todo auth preference for a password change screen for all auth methods other than internal
44
if (method_exists($authobj, 'change_password')) {
45
46
    $elements = array(
        'changepassworddesc' => array(
47
            'value' => '<tr><td colspan="2"><h3>' . get_string('changepassworddesc', 'account') . '</h3></td></tr>'
48
49
50
        ),
        'oldpassword' => array( 'type' => 'password',
            'title' => get_string('oldpassword'),
51
            'help'  => true,
52
            'autocomplete' => 'off',
53
54
55
56
57
58
59
60
61
62
63
        ),
        'password1' => array(
            'type' => 'password',
            'title' => get_string('newpassword'),
        ),
        'password2' => array(
            'type' => 'password',
            'title' => get_string('confirmpassword')
        ),
    );
}
64
else if ($url = get_config_plugin_instance('auth', $USER->authinstance, 'changepasswordurl')) {
65
    // @todo contextual help
66
67
    $elements = array(
        'changepasswordotherinterface' => array(
68
            'value' => '<tr><td colspan="2"><h3>' . get_string('changepasswordotherinterface', 'account', $url) . '</h3></td></tr>'
69
70
71
72
73
74
75
        )
    );
}
else {
    $elements = array();
}

76
77
if ($authobj->authname == 'internal') {
    $elements['changeusernameheading'] = array(
78
        'value' => '<tr><td colspan="2"><h3>' . get_string('changeusernameheading', 'account') . '</h3></td></tr>'
79
80
81
82
83
    );
    $elements['username'] = array(
        'type' => 'text',
        'defaultvalue' => $USER->get('username'),
        'title' => get_string('changeusername', 'account'),
84
        'description' => get_string('changeusernamedesc', 'account', hsc(get_config('sitename'))),
85
86
87
    );
}

88
$elements['accountoptionsdesc'] = array(
89
    'value' => '<tr><td colspan="2"><h3>' . get_string('accountoptionsdesc', 'account') . '</h3></td></tr>'
90
91
92
93
94
);
$elements['friendscontrol'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->friendscontrol, 
    'title'  => get_string('friendsdescr', 'account'),
95
    'separator' => '<br>',
96
97
98
99
100
101
102
103
104
105
106
    'options' => array(
        'nobody' => get_string('friendsnobody', 'account'),
        'auth'   => get_string('friendsauth', 'account'),
        'auto'   => get_string('friendsauto', 'account')
    ),
    'help' => true
);
$elements['wysiwyg'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->wysiwyg,
    'title' => get_string('wysiwygdescr', 'account'),
107
    'separator' => '<br>',
108
109
110
111
    'options' => array(
        1 => get_string('on', 'account'),
        0 => get_string('off', 'account'),
    ),
112
    'help' => true,
113
);
114
115
116
117
118
119
120
121
122
123
124
$elements['maildisabled'] = array(
    'type' => 'radio',
    'defaultvalue' => get_account_preference($USER->get('id'), 'maildisabled'),
    'title' => get_string('email'),
    'separator' => '<br>',
    'options' => array(
        0 => get_string('enabled', 'account'),
        1 => get_string('disabled', 'account'),
    ),
    'help' => true,
);
125
126
127
128
$elements['messages'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->messages,
    'title' => get_string('messagesdescr', 'account'),
129
    'separator' => '<br>',
130
131
132
133
134
    'options' => array(
        'nobody' => get_string('messagesnobody', 'account'),
        'friends' => get_string('messagesfriends', 'account'),
        'allow' => get_string('messagesallow', 'account'),
    ),
135
    'help' => true,
136
);
137
$languages = get_languages();
138
139
140
141
$elements['lang'] = array(
    'type' => 'select',
    'defaultvalue' => $prefs->lang,
    'title' => get_string('language', 'account'),
142
    'options' => array_merge(array('default' => get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')'), $languages),
143
    'help' => true,
144
    'ignore' => count($languages) < 2,
145
146
147
148
149
150
151
152
153
);
$elements['addremovecolumns'] = array(
    'type' => 'radio',
    'options' => array(
        1 => get_string('on', 'account'),
        0 => get_string('off', 'account'),
    ),
    'defaultvalue' => $prefs->addremovecolumns,
    'title' => get_string('showviewcolumns', 'account'),
154
    'separator' => '<br>',
155
156
    'help' => 'true'
);
157
158
159
160
161
162
163
// TODO: add a way for plugins (like blog!) to have account preferences
$elements['multipleblogs'] = array(
    'type' => 'checkbox',
    'title'=> get_string('enablemultipleblogs' ,'account'),
    'description' => get_string('enablemultipleblogsdescription', 'account'),
    'defaultvalue' => $prefs->multipleblogs,
);
164
165
166
167
168
169
170
171
172
173
if (get_config('showtagssideblock')) {
    $elements['tagssideblockmaxtags'] = array(
        'type'         => 'text',
        'size'         => 4,
        'title'        => get_string('tagssideblockmaxtags', 'account'),
        'description'  => get_string('tagssideblockmaxtagsdescription', 'account'),
        'defaultvalue' => isset($prefs->tagssideblockmaxtags) ? $prefs->tagssideblockmaxtags : get_config('tagssideblockmaxtags'),
        'rules'        => array('integer' => true, 'minvalue' => 0, 'maxvalue' => 1000),
    );
}
174
175
176
177
178
179
180
181
if (get_config('userscanhiderealnames')) {
    $elements['hiderealname'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('hiderealname', 'account'),
        'description'  => get_string('hiderealnamedescription', 'account'),
        'defaultvalue' => $prefs->hiderealname,
    );
}
182
183
184
185
186
187
188
189
190
191
192
193
194
if (get_config('homepageinfo')) {
    $elements['showhomeinfo'] = array(
        'type' => 'radio',
        'options' => array(
            1 => get_string('on', 'account'),
            0 => get_string('off', 'account'),
        ),
        'defaultvalue' => $prefs->showhomeinfo,
        'title' => get_string('showhomeinfo', 'account'),
        'separator' => '<br>',
        'help' => 'true'
    );
}
195
196
197
198
199
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
200
201
$prefsform = array(
    'name'        => 'accountprefs',
202
    'renderer'    => 'table',
Penny Leach's avatar
Penny Leach committed
203
    'method'      => 'post',
204
    'jsform'      => true,
205
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
206
    'pluginname'  => 'account',
207
    'jssuccesscallback' => 'clearPasswords',
208
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
209
);
210

211
function accountprefs_validate(Pieform $form, $values) {
212
213
214
215
    global $USER;

    $authobj = AuthFactory::create($USER->authinstance);

216
217
218
    if (isset($values['oldpassword'])) {
        if ($values['oldpassword'] !== '') {
            global $USER, $authtype, $authclass;
219
220
221
222
223
224
225
226
227
228
229
            try {
                if (!$authobj->authenticate_user_account($USER, $values['oldpassword'])) {
                    $form->set_error('oldpassword', get_string('oldpasswordincorrect', 'account'));
                    return;
                }
            }
            // propagate error correctly for User validation issues - this should
            // be catching AuthUnknownUserException and AuthInstanceException
             catch  (UserException $e) {
                 $form->set_error('oldpassword', $e->getMessage());
                 return;
230
231
232
233
234
            }
            password_validate($form, $values, $USER);
        }
        else if ($values['password1'] !== '' || $values['password2'] !== '') {
            $form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
235
236
        }
    }
237
238
239
240
241
242
243
244
245

    if ($authobj->authname == 'internal' && $values['username'] != $USER->get('username')) {
        if (!AuthInternal::is_username_valid($values['username'])) {
            $form->set_error('username', get_string('usernameinvalidform', 'auth.internal'));
        }
        if (!$form->get_error('username') && record_exists_select('usr', 'LOWER(username) = ?', strtolower($values['username']))) {
            $form->set_error('username', get_string('usernamealreadytaken', 'auth.internal'));
        }
    }
246
247
248
249
250
251
252

    // Don't let users turn multiple blogs off unless they only have 1 blog
    if ($USER->get_account_preference('multipleblogs')
        && empty($values['multipleblogs'])
        && count_records('artefact', 'artefacttype', 'blog', 'owner', $USER->get('id')) != 1) {
        $form->set_error('multipleblogs', get_string('disablemultipleblogserror', 'account'));
    }
253
254
}

255
function accountprefs_submit(Pieform $form, $values) {
256
    global $USER;
257

258
259
    $authobj = AuthFactory::create($USER->authinstance);

260
    db_begin();
261
    if (isset($values['password1']) && $values['password1'] !== '') {
262
        global $authclass;
263
264
265
266
        $password = $authobj->change_password($USER, $values['password1']);
        $USER->password = $password;
        $USER->passwordchange = 0;
        $USER->commit();
267
268
    }

269
270
    // use this as looping through values is not safe.
    $expectedprefs = expected_account_preferences(); 
271
272
273
274
275
276
277
278
279
280
    if ($values['maildisabled'] == 0 && get_account_preference($USER->get('id'), 'maildisabled') == 1) {
        // Reset the sent and bounce counts otherwise mail will be disabled
        // on the next send attempt
        $u = new StdClass;
        $u->email = $USER->get('email');
        $u->id = $USER->get('id');
        update_bounce_count($u,true);
        update_send_count($u,true);
    }

Penny Leach's avatar
Penny Leach committed
281
    foreach (array_keys($expectedprefs) as $pref) {
282
283
284
        if (isset($values[$pref])) {
            $USER->set_account_preference($pref, $values[$pref]);
        }
285
    }
286

287
288
    $returndata = array();

289
290
291
    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
        $USER->username = $values['username'];
        $USER->commit();
292
        $returndata['username'] = $values['username'];
293
294
    }

295
    db_commit();
296
297
    $returndata['message'] = get_string('prefssaved', 'account');
    $form->json_reply(PIEFORM_OK, $returndata);
Penny Leach's avatar
Penny Leach committed
298
299
300
}


301

Richard Mansfield's avatar
Richard Mansfield committed
302
$prefsform = pieform($prefsform);
303
304

$smarty = smarty();
Richard Mansfield's avatar
Richard Mansfield committed
305
$smarty->assign('form', $prefsform);
306
$smarty->assign('candeleteself', $USER->can_delete_self());
307
308
309
$smarty->assign('INLINEJAVASCRIPT', "
function clearPasswords(form, data) {
    formSuccess(form, data);
310
311
312
313
314
    if ($('accountprefs_oldpassword')) {
        $('accountprefs_oldpassword').value = '';
        $('accountprefs_password1').value = '';
        $('accountprefs_password2').value = '';
    }
315
316
317
318
319
320
    if (data.username) {
        var username = getFirstElementByTagAndClassName('a', null, 'profile-sideblock-username');
        replaceChildNodes(username, data.username);
    }
}
");
321
$smarty->assign('PAGEHEADING', TITLE);
322
323
324
$smarty->display('account/index.tpl');


325
?>