index.php 6.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/**
 * This program is part of Mahara
 *
 *  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 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'account');
define('SUBMENUITEM', 'accountprefs');

require(dirname(dirname(__FILE__)) . '/init.php');
32
require_once('form.php');
33

Penny Leach's avatar
Penny Leach committed
34
35
36
// load up user preferences
$prefs = (object)($SESSION->get('accountprefs'));

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
$authtype  = auth_get_authtype_for_institution($USER->institution);
$authclass = 'Auth' . ucfirst($authtype);
safe_require('auth', $authtype);

// @todo auth preference for a password change screen for all auth methods other than internal
if (method_exists($authclass, 'change_password')) {
    $elements = array(
        'changepassworddesc' => array(
            'value' => '<tr><td colspan="2"><p>' . get_string('changepassworddesc', 'account') . '</p></td></tr>'
        ),
        'oldpassword' => array( 'type' => 'password',
            'title' => get_string('oldpassword'),
        ),
        'password1' => array(
            'type' => 'password',
            'title' => get_string('newpassword'),
        ),
        'password2' => array(
            'type' => 'password',
            'title' => get_string('confirmpassword')
        ),
    );
}
else if ($url = get_config_plugin('auth', $authtype, 'changepasswordurl')) {
    $elements = array(
        'changepasswordotherinterface' => array(
            'value' => '<tr><td colspan="2"><p>' . get_string('changepasswordotherinterface', 'account', $url) . '</p></td></tr>'
        )
    );
}
else {
    $elements = array();
}

$elements['accountoptionsdesc'] = array(
    'value' => '<tr><td colspan="2"><p>You can set general account options here</p></td></tr>'
);
$elements['friendscontrol'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->friendscontrol, 
    'title'  => get_string('friendsdescr', 'account'),
    'separator' => HTML_BR,
    'options' => array(
        'nobody' => get_string('friendsnobody', 'account'),
        'auth'   => get_string('friendsauth', 'account'),
        'auto'   => get_string('friendsauto', 'account')
    ),
   'rules' => array(
        'required' => true
    ),
    'help' => true
);
$elements['wysiwyg'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->wysiwyg,
    'title' => get_string('wysiwygdescr', 'account'),
    'options' => array(
        1 => get_string('on', 'account'),
        0 => get_string('off', 'account'),
    ),
   'rules' => array(
        'required' => true
    )
);
$elements['messages'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->messages,
    'title' => get_string('messagesdescr', 'account'),
    'separator' => HTML_BR,
    'options' => array(
        'nobody' => get_string('messagesnobody', 'account'),
        'friends' => get_string('messagesfriends', 'account'),
        'allow' => get_string('messagesallow', 'account'),
    ),
   'rules' => array(
       'required' => true
    )
);
$elements['lang'] = array(
    'type' => 'select',
    'defaultvalue' => $prefs->lang,
    'title' => get_string('language', 'account'),
    'options' => get_languages(),
    'rules' => array(
        'required' => true
    )
);                        
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
129
130
131
132
$prefsform = array(
    'name'        => 'accountprefs',
    'method'      => 'post',
    'ajaxpost'    => true,
133
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
134
    'pluginname'  => 'account',
135
136
    'ajaxsuccessfunction' => 'clearPasswords',
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
137
);
138

Penny Leach's avatar
Penny Leach committed
139
140
141

$smarty = smarty();
$smarty->assign('form', form($prefsform));
142
143
144
145
146
147
$smarty->assign('INLINEJAVASCRIPT', "
function clearPasswords() {
    $('oldpassword').value = '';
    $('password1').value = '';
    $('password2').value = '';
}");
148
149
$smarty->display('account/index.tpl');

150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
function accountprefs_validate(Form $form, $values) {
    if ($values['oldpassword'] !== '') {
        global $SESSION, $authtype, $authclass;
        if (!call_static_method($authclass, 'authenticate_user_account', $SESSION->get('username'), $values['oldpassword'], $SESSION->get('institution'))) {
            $form->set_error('oldpassword', get_string('oldpasswordincorrect', 'account'));
            return;
        }
        $user = new StdClass;
        $user->username    = $SESSION->get('username');
        $user->institution = $SESSION->get('institution');
        password_validate($form, $values, $user);
    }
    else if ($values['password1'] !== '' || $values['password2'] !== '') {
        $form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
    }
}

167
function accountprefs_submit($values) {
Penny Leach's avatar
Penny Leach committed
168
    global $SESSION;
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

    db_begin();
    if ($values['password1'] !== '') {
        global $authclass;
        $password = call_static_method($authclass, 'change_password', $SESSION->get('username'), $values['password1']);
        $user = new StdClass;
        $user->password = $password;
        $user->passwordchange = 0;
        $where = new StdClass;
        $where->username = $SESSION->get('username');
        update_record('usr', $user, $where);
        $SESSION->set('password', $password);
        $SESSION->set('passwordchange', 0);
    }

184
185
    // use this as looping through values is not safe.
    $expectedprefs = expected_account_preferences(); 
Penny Leach's avatar
Penny Leach committed
186
187
    foreach (array_keys($expectedprefs) as $pref) {
        $SESSION->set_account_preference($pref, $values[$pref]);
188
    }
189
190

    db_commit();
191
    json_reply(false, get_string('prefssaved', 'account'));
Penny Leach's avatar
Penny Leach committed
192
    exit;
Penny Leach's avatar
Penny Leach committed
193
194
195
}


196
?>