index.php 12 KB
Newer Older
1 2 3 4 5
<?php
/**
 *
 * @package    mahara
 * @subpackage core
6
 * @author     Catalyst IT Ltd
7 8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9 10 11 12
 *
 */

define('INTERNAL', 1);
13
define('MENUITEM', 'settings/account');
14 15 16
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'account');
define('SECTION_PAGE', 'preferences');
17 18

require(dirname(dirname(__FILE__)) . '/init.php');
19
define('TITLE', get_string('account'));
20
require_once('pieforms/pieform.php');
21

Penny Leach's avatar
Penny Leach committed
22
// load up user preferences
23
$prefs = (object) load_account_preferences($USER->id);
Penny Leach's avatar
Penny Leach committed
24

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

// @todo auth preference for a password change screen for all auth methods other than internal
28
if (method_exists($authobj, 'change_password')) {
29 30
    $elements = array(
        'changepassworddesc' => array(
31
            'value' => '<tr><td colspan="2"><h3>' . get_string('changepassworddesc', 'account') . '</h3></td></tr>'
32
        ),
33 34 35 36 37 38 39 40 41
        // HACK: A decoy password field to prevent Firefox from trying to autofill the "oldpassword" field.
        // (FF will fill in this one instead, because it comes first. Then we can just ignore it.
        // TODO: move the password reset form to a separate screen
        'password' => array(
            'type' => 'password',
            'title' => '',
            'class' => 'hidden',
            'value' => 'decoypassword',
        ),
42 43
        'oldpassword' => array( 'type' => 'password',
            'title' => get_string('oldpassword'),
44
            'help'  => true,
45
            'autocomplete' => 'off',
46 47 48 49 50 51 52 53 54 55 56
        ),
        'password1' => array(
            'type' => 'password',
            'title' => get_string('newpassword'),
        ),
        'password2' => array(
            'type' => 'password',
            'title' => get_string('confirmpassword')
        ),
    );
}
57
else if ($url = get_config_plugin_instance('auth', $USER->authinstance, 'changepasswordurl')) {
58
    // @todo contextual help
59 60
    $elements = array(
        'changepasswordotherinterface' => array(
61
            'value' => '<tr><td colspan="2"><h3>' . get_string('changepasswordotherinterface', 'account', $url) . '</h3></td></tr>'
62 63 64 65 66 67 68
        )
    );
}
else {
    $elements = array();
}

69 70
if ($authobj->authname == 'internal') {
    $elements['changeusernameheading'] = array(
71
        'value' => '<tr><td colspan="2"><h3>' . get_string('changeusernameheading', 'account') . '</h3></td></tr>'
72 73 74 75 76
    );
    $elements['username'] = array(
        'type' => 'text',
        'defaultvalue' => $USER->get('username'),
        'title' => get_string('changeusername', 'account'),
77
        'description' => get_string('changeusernamedesc', 'account', hsc(get_config('sitename'))),
78 79 80
    );
}

81
if (get_config('cleanurls') && get_config('cleanurlusereditable')) {
82 83 84
    $elements['changeprofileurl'] = array(
        'value' => '<tr><td colspan="2"><h3>' . get_string('changeprofileurl', 'account') . '</h3></td></tr>'
    );
85 86 87 88 89 90 91 92 93
    if (get_config('cleanurlusersubdomains')) {
        list($proto, $rest) = explode('://', get_config('wwwroot'));
        $prehtml = $proto . ':// ';
        $posthtml = ' .' . $rest;
    }
    else {
        $prehtml = get_config('wwwroot') . get_config('cleanurluserdefault') . '/ ';
        $posthtml = '';
    }
94 95 96 97
    $elements['urlid'] = array(
        'type'         => 'text',
        'defaultvalue' => $USER->get('urlid'),
        'title'        => get_string('profileurl', 'account'),
98 99
        'prehtml'      => '<span class="description">' . $prehtml . '</span>',
        'posthtml'     => '<span class="description">' . $posthtml . '</span>',
100 101 102 103 104
        'description'  => get_string('profileurldescription', 'account') . ' ' . get_string('cleanurlallowedcharacters'),
        'rules'        => array('maxlength' => 30, 'regex' => get_config('cleanurlvalidate')),
    );
}

105
$elements['accountoptionsdesc'] = array(
106
    'value' => '<tr><td colspan="2"><h3>' . get_string('accountoptionsdesc', 'account') . '</h3></td></tr>'
107
);
108 109 110

// Add general account options
$elements = array_merge($elements, general_account_prefs_form_elements($prefs));
111

112 113 114
// Add plugins account options.
$elements = array_merge($elements, plugin_account_prefs_form_elements($prefs));

115 116
$blogcount = count_records('artefact', 'artefacttype', 'blog', 'owner', $USER->get('id')) ;
if ($blogcount != 1 && $prefs->multipleblogs == 1) {
117
    $elements['multipleblogs']['readonly'] = true;
118
}
119

120 121
$elements['submit'] = array(
    'type' => 'submit',
122
    'class' => 'btn-primary',
123 124 125
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
126 127
$prefsform = array(
    'name'        => 'accountprefs',
Pat Kira's avatar
Pat Kira committed
128
    'renderer'    => 'div',
Penny Leach's avatar
Penny Leach committed
129
    'method'      => 'post',
130
    'jsform'      => true,
131
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
132
    'pluginname'  => 'account',
133
    'jssuccesscallback' => 'clearPasswords',
134
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
135
);
136

137
function accountprefs_validate(Pieform $form, $values) {
138 139 140 141
    global $USER;

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

142 143 144
    if (isset($values['oldpassword'])) {
        if ($values['oldpassword'] !== '') {
            global $USER, $authtype, $authclass;
145 146 147 148 149 150 151 152 153 154 155
            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;
156 157 158 159 160
            }
            password_validate($form, $values, $USER);
        }
        else if ($values['password1'] !== '' || $values['password2'] !== '') {
            $form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
161 162
        }
    }
163 164 165 166 167

    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'));
        }
168
        if (!$form->get_error('username') && record_exists_select('usr', 'LOWER(username) = ?', array(strtolower($values['username'])))) {
169 170 171
            $form->set_error('username', get_string('usernamealreadytaken', 'auth.internal'));
        }
    }
172

173
    if (isset($values['urlid']) && get_config('cleanurls') && $values['urlid'] != $USER->get('urlid')) {
174 175 176 177 178 179 180
        if (strlen($values['urlid']) < 3) {
            $form->set_error('urlid', get_string('rule.minlength.minlength', 'pieforms', 3));
        }
        else if (record_exists('usr', 'urlid', $values['urlid'])) {
            $form->set_error('urlid', get_string('urlalreadytaken', 'account'));
        }
    }
181

182 183 184 185 186
    if (get_config('allowmobileuploads')) {
        foreach ($values['mobileuploadtoken'] as $k => $text) {
            if (strlen($text) > 0 && !preg_match('/^[a-zA-Z0-9 !@#$%^&*()\-_=+\[{\]};:\'",<\.>\/?]{6,}$/', $text)) {
                $form->set_error('mobileuploadtoken', get_string('badmobileuploadtoken', 'account'));
            }
187 188
        }
    }
189 190

    plugin_account_prefs_validate($form, $values);
191 192
}

193
function accountprefs_submit(Pieform $form, $values) {
194
    global $USER, $SESSION;
195

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

198
    db_begin();
199
    $ispasswordchanged = false;
200
    if (isset($values['password1']) && $values['password1'] !== '') {
201
        global $authclass;
202 203 204 205
        $password = $authobj->change_password($USER, $values['password1']);
        $USER->password = $password;
        $USER->passwordchange = 0;
        $USER->commit();
206
        $ispasswordchanged = true;
207 208
    }

209
    // use this as looping through values is not safe.
Aaron Wells's avatar
Aaron Wells committed
210
    $expectedprefs = expected_account_preferences();
211 212 213 214 215 216 217 218 219 220
    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);
    }

221
    // Remember the user's language & theme prefs, so we can reload the page if they change them
222
    $oldlang = $USER->get_account_preference('lang');
223
    $oldtheme = $USER->get_account_preference('theme');
224
    $oldgroupsideblockmaxgroups = $USER->get_account_preference('groupsideblockmaxgroups');
225
    $oldgroupsideblocksortby = $USER->get_account_preference('groupsideblocksortby');
226

227 228 229 230 231 232 233
    if (get_config('allowmobileuploads')) {
        // Make sure the mobile token is formatted / saved correctly
        $values['mobileuploadtoken'] = array_filter($values['mobileuploadtoken']);
        $new_token_pref = empty($values['mobileuploadtoken']) ? null : ('|' . join('|', $values['mobileuploadtoken']) . '|');
        $USER->set_account_preference('mobileuploadtoken', $new_token_pref);
        unset($values['mobileuploadtoken']);
    }
234

235 236
    // Set user account preferences
    foreach ($expectedprefs as $eprefkey => $epref) {
237
        if (isset($values[$eprefkey]) && $values[$eprefkey] !== get_account_preference($USER->get('id'), $eprefkey)) {
238
            $USER->set_account_preference($eprefkey, $values[$eprefkey]);
239
        }
240
    }
241

242
    if (isset($values['theme']) && $values['theme'] == 'sitedefault') {
243 244 245
        $USER->set_account_preference('theme', '');
    }

246
    $returndata = array();
247 248 249
    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
        $USER->username = $values['username'];
        $USER->commit();
250
        $returndata['username'] = $values['username'];
251 252
    }

253
    $reload = false;
254 255 256
    if (get_config('cleanurls') && isset($values['urlid']) && $values['urlid'] != $USER->get('urlid')) {
        $USER->urlid = $values['urlid'];
        $USER->commit();
257
        $reload = true;
258 259
    }

260 261 262 263 264 265
    if ($ispasswordchanged) {
        // Destroy other sessions of the user
        require_once(get_config('docroot') . 'auth/session.php');
        remove_user_sessions($USER->get('id'));
    }

266
    db_commit();
267

268
    $returndata['message'] = get_string('prefssaved', 'account');
269

270
    if (isset($values['theme']) && $values['theme'] != $oldtheme) {
271
        $USER->update_theme();
272
        $reload = true;
273
    }
274 275

    if (isset($values['lang']) && $values['lang'] != $oldlang) {
276 277 278
        // The session language pref is used when the user has no user pref,
        // and when logged out.
        $SESSION->set('lang', $values['lang']);
279 280
        $returndata['message'] = get_string_from_language($values['lang'], 'prefssaved', 'account');
        $reload = true;
281
    }
282 283 284
    if (isset($values['groupsideblockmaxgroups']) && $values['groupsideblockmaxgroups'] != $oldgroupsideblockmaxgroups) {
        $reload = true;
    }
285 286 287
    if ($values['groupsideblocksortby'] != $oldgroupsideblocksortby) {
        $reload = true;
    }
288

289 290
    $reload = plugin_account_prefs_submit($form, $values) || $reload;

291
    if (!empty($reload)) {
292 293
        // Use PIEFORM_CANCEL here to force a page reload and show the new language.
        $returndata['location'] = get_config('wwwroot') . 'account/index.php';
294
        $SESSION->add_ok_msg($returndata['message']);
295 296 297
        $form->json_reply(PIEFORM_CANCEL, $returndata);
    }

298
    $form->json_reply(PIEFORM_OK, $returndata);
Penny Leach's avatar
Penny Leach committed
299 300 301
}


302

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

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