index.php 11.9 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) load_account_preferences($USER->id);
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
    'options' => array(
        'nobody' => get_string('friendsnobody', 'account'),
        'auth'   => get_string('friendsauth', 'account'),
        'auto'   => get_string('friendsauto', 'account')
    ),
    'help' => true
);
$elements['wysiwyg'] = array(
104
    'type' => 'checkbox',
105 106
    'defaultvalue' => $prefs->wysiwyg,
    'title' => get_string('wysiwygdescr', 'account'),
107
    'help' => true,
108
);
109
$elements['maildisabled'] = array(
110
    'type' => 'checkbox',
111 112 113 114
    'defaultvalue' => get_account_preference($USER->get('id'), 'maildisabled'),
    'title' => get_string('email'),
    'help' => true,
);
115 116 117 118
$elements['messages'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->messages,
    'title' => get_string('messagesdescr', 'account'),
119
    'separator' => '<br>',
120 121 122 123 124
    'options' => array(
        'nobody' => get_string('messagesnobody', 'account'),
        'friends' => get_string('messagesfriends', 'account'),
        'allow' => get_string('messagesallow', 'account'),
    ),
125
    'help' => true,
126
);
127
$languages = get_languages();
128 129 130 131
$elements['lang'] = array(
    'type' => 'select',
    'defaultvalue' => $prefs->lang,
    'title' => get_string('language', 'account'),
132
    'options' => array_merge(array('default' => get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')'), $languages),
133
    'help' => true,
134
    'ignore' => count($languages) < 2,
135 136
);
$elements['addremovecolumns'] = array(
137
    'type' => 'checkbox',
138 139 140 141
    'defaultvalue' => $prefs->addremovecolumns,
    'title' => get_string('showviewcolumns', 'account'),
    'help' => 'true'
);
142 143 144 145 146 147 148
// 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,
);
149 150 151 152 153 154 155 156 157 158
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),
    );
}
159 160 161 162 163 164 165 166
if (get_config('userscanhiderealnames')) {
    $elements['hiderealname'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('hiderealname', 'account'),
        'description'  => get_string('hiderealnamedescription', 'account'),
        'defaultvalue' => $prefs->hiderealname,
    );
}
167 168
if (get_config('homepageinfo')) {
    $elements['showhomeinfo'] = array(
169
        'type' => 'checkbox',
170 171 172 173 174
        'defaultvalue' => $prefs->showhomeinfo,
        'title' => get_string('showhomeinfo', 'account'),
        'help' => 'true'
    );
}
175 176 177 178 179 180 181 182
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')
    );
}
183 184 185 186 187
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
188 189
$prefsform = array(
    'name'        => 'accountprefs',
190
    'renderer'    => 'table',
Penny Leach's avatar
Penny Leach committed
191
    'method'      => 'post',
192
    'jsform'      => true,
193
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
194
    'pluginname'  => 'account',
195
    'jssuccesscallback' => 'accountPrefsSuccess',
196
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
197
);
198

199
function accountprefs_validate(Pieform $form, $values) {
200 201 202 203
    global $USER;

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

204 205 206
    if (isset($values['oldpassword'])) {
        if ($values['oldpassword'] !== '') {
            global $USER, $authtype, $authclass;
207 208 209 210 211 212 213 214 215 216 217
            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;
218 219 220 221 222
            }
            password_validate($form, $values, $USER);
        }
        else if ($values['password1'] !== '' || $values['password2'] !== '') {
            $form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
223 224
        }
    }
225 226 227 228 229 230 231 232 233

    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'));
        }
    }
234 235 236 237 238 239 240

    // 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'));
    }
241 242
}

243
function accountprefs_submit(Pieform $form, $values) {
244
    global $USER;
245

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

248
    db_begin();
249
    if (isset($values['password1']) && $values['password1'] !== '') {
250
        global $authclass;
251 252 253 254
        $password = $authobj->change_password($USER, $values['password1']);
        $USER->password = $password;
        $USER->passwordchange = 0;
        $USER->commit();
255 256
    }

257 258
    // use this as looping through values is not safe.
    $expectedprefs = expected_account_preferences(); 
259 260 261 262 263 264 265 266 267 268
    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);
    }

269 270 271 272 273 274 275
    $returndata = array();
    if (isset($values['multipleblogs'])) {
        if ((bool) $USER->get_account_preference('multipleblogs') !== (bool) $values['multipleblogs']) {
            $returndata['multipleblogs'] = $values['multipleblogs'] ? 'on' : 'off'; // Rewrite the blogs link in the menu
        }
    }

Penny Leach's avatar
Penny Leach committed
276
    foreach (array_keys($expectedprefs) as $pref) {
277 278 279
        if (isset($values[$pref])) {
            $USER->set_account_preference($pref, $values[$pref]);
        }
280
    }
281

282 283 284
    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
        $USER->username = $values['username'];
        $USER->commit();
285
        $returndata['username'] = $values['username'];
286 287
    }

288
    db_commit();
289 290
    $returndata['message'] = get_string('prefssaved', 'account');
    $form->json_reply(PIEFORM_OK, $returndata);
Penny Leach's avatar
Penny Leach committed
291 292 293
}


294

Richard Mansfield's avatar
Richard Mansfield committed
295
$prefsform = pieform($prefsform);
296 297

$smarty = smarty();
Richard Mansfield's avatar
Richard Mansfield committed
298
$smarty->assign('form', $prefsform);
299
$smarty->assign('candeleteself', $USER->can_delete_self());
300
$smarty->assign('INLINEJAVASCRIPT', "
301
function accountPrefsSuccess(form, data) {
302
    formSuccess(form, data);
303 304 305 306 307
    if ($('accountprefs_oldpassword')) {
        $('accountprefs_oldpassword').value = '';
        $('accountprefs_password1').value = '';
        $('accountprefs_password2').value = '';
    }
308 309 310 311
    if (data.username) {
        var username = getFirstElementByTagAndClassName('a', null, 'profile-sideblock-username');
        replaceChildNodes(username, data.username);
    }
312 313 314 315 316 317 318 319
    if (data.multipleblogs) {
        var newhref = data.multipleblogs == 'on' ? 'artefact/blog/' : 'artefact/blog/view/';
        forEach(getElementsByTagAndClassName('a', null, 'main-nav'), function (link) {
            if (getNodeAttribute(link, 'href').match(new RegExp('/artefact/blog/'))) {
                setNodeAttribute(link, 'href', config.wwwroot + newhref);
            }
        });
    }
320 321
}
");
322
$smarty->assign('PAGEHEADING', TITLE);
323 324 325
$smarty->display('account/index.tpl');


326
?>