index.php 11.8 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 200 201 202
if (get_config('allowmobileuploads')) {
    $elements['mobileuploadtoken'] = array(
        'type'         => 'text',
        'title'        => get_string('mobileuploadtoken', 'account'),
        'description'  => get_string('mobileuploadtokendescription', 'account'),
        'defaultvalue' => isset($prefs->mobileuploadtoken) ? $prefs->mobileuploadtoken : get_config('mobileuploadtoken')
    );
}
203 204 205 206 207
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
208 209
$prefsform = array(
    'name'        => 'accountprefs',
210
    'renderer'    => 'table',
Penny Leach's avatar
Penny Leach committed
211
    'method'      => 'post',
212
    'jsform'      => true,
213
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
214
    'pluginname'  => 'account',
215
    'jssuccesscallback' => 'clearPasswords',
216
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
217
);
218

219
function accountprefs_validate(Pieform $form, $values) {
220 221 222 223
    global $USER;

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

224 225 226
    if (isset($values['oldpassword'])) {
        if ($values['oldpassword'] !== '') {
            global $USER, $authtype, $authclass;
227 228 229 230 231 232 233 234 235 236 237
            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;
238 239 240 241 242
            }
            password_validate($form, $values, $USER);
        }
        else if ($values['password1'] !== '' || $values['password2'] !== '') {
            $form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
243 244
        }
    }
245 246 247 248 249 250 251 252 253

    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'));
        }
    }
254 255 256 257 258 259 260

    // 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'));
    }
261 262
}

263
function accountprefs_submit(Pieform $form, $values) {
264
    global $USER;
265

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

268
    db_begin();
269
    if (isset($values['password1']) && $values['password1'] !== '') {
270
        global $authclass;
271 272 273 274
        $password = $authobj->change_password($USER, $values['password1']);
        $USER->password = $password;
        $USER->passwordchange = 0;
        $USER->commit();
275 276
    }

277 278
    // use this as looping through values is not safe.
    $expectedprefs = expected_account_preferences(); 
279 280 281 282 283 284 285 286 287 288
    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
289
    foreach (array_keys($expectedprefs) as $pref) {
290 291 292
        if (isset($values[$pref])) {
            $USER->set_account_preference($pref, $values[$pref]);
        }
293
    }
294

295 296
    $returndata = array();

297 298 299
    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
        $USER->username = $values['username'];
        $USER->commit();
300
        $returndata['username'] = $values['username'];
301 302
    }

303
    db_commit();
304 305
    $returndata['message'] = get_string('prefssaved', 'account');
    $form->json_reply(PIEFORM_OK, $returndata);
Penny Leach's avatar
Penny Leach committed
306 307 308
}


309

Richard Mansfield's avatar
Richard Mansfield committed
310
$prefsform = pieform($prefsform);
311 312

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


333
?>