1<?php
2/*
3 * Gallery - a web based photo album viewer and editor
4 * Copyright (C) 2000-2008 Bharat Mediratta
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21/**
22 * This controller will handle changing a user password
23 * @package GalleryCore
24 * @subpackage UserInterface
25 * @author Bharat Mediratta <bharat@menalto.com>
26 * @version $Revision: 17580 $
27 */
28class UserChangePasswordController extends GalleryController {
29
30    /**
31     * @see GalleryController::handleRequest
32     */
33    function handleRequest($form) {
34	global $gallery;
35	$user = $gallery->getActiveUser();
36
37	list ($ret, $isAnonymous) = GalleryCoreApi::isAnonymousUser();
38	if ($ret) {
39	    return array($ret, null);
40	}
41	if ($isAnonymous || $user->isLocked()) {
42	    return array(GalleryCoreApi::error(ERROR_PERMISSION_DENIED), null);
43	}
44
45	$results = $status = $error = array();
46	if (isset($form['action']['cancel'])) {
47
48	    /* Redirect back to user preferences */
49	    $redirect['view'] = 'core.UserAdmin';
50	    $redirect['subView'] = 'core.UserPreferences';
51
52	} else if (isset($form['action']['undo'])) {
53
54	    /* Redirect back to the same view to reset the form */
55	    $redirect['view'] = 'core.UserAdmin';
56	    $redirect['subView'] = 'core.UserChangePassword';
57
58	} else if (isset($form['action']['save'])) {
59
60	    /*
61	     * Validate our data before we continue.
62	     */
63	    if (empty($form['currentPassword'])) {
64		$error[] = 'form[error][currentPassword][missing]';
65	    } else {
66		GalleryUtilities::unsanitizeInputValues($form['currentPassword'], false);
67		if (!$user->isCorrectPassword($form['currentPassword'])) {
68		    $error[] = 'form[error][currentPassword][incorrect]';
69
70		    $event = GalleryCoreApi::newEvent('Gallery::FailedLogin');
71		    $event->setData(array('userName' => $user->getUserName()));
72		    list ($ret, $ignored) = GalleryCoreApi::postEvent($event);
73		    if ($ret) {
74			return array($ret, null);
75		    }
76		} else {
77		/*
78		 * To be consistent, we really should post a Gallery::Login event here to show
79		 * that the user successfully authenticated.  But they're not really logging in so
80		 * that's misleading.  Perhaps we should rename the events to Gallery::Authenticate
81		 * and Gallery::FailedAuthenticate to be more accurate, and then post those here?
82		 *
83		 * @see UserPreferencesController::handleRequest
84		 */
85		}
86	    }
87
88	    if (empty($form['password1'])) {
89		$error[] = 'form[error][password1][missing]';
90	    }
91
92	    if (empty($form['password2'])) {
93		$error[] = 'form[error][password2][missing]';
94	    }
95
96	    if (!$error && $form['password1'] != $form['password2']) {
97		$error[] = 'form[error][password2][mismatch]';
98	    }
99
100	    /*
101	     * If all the right fields are in place then go ahead and modify
102	     * the user.
103	     */
104	    if (empty($error)) {
105		list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($user->getId());
106		if ($ret) {
107		    return array($ret, null);
108		}
109
110		list ($ret, $user) = $user->refresh();
111		if ($ret) {
112		    return array($ret, null);
113		}
114
115		GalleryUtilities::unsanitizeInputValues($form['password1'], false);
116		$user->changePassword($form['password1']);
117
118		$ret = $user->save();
119		if ($ret) {
120		    return array($ret, null);
121		}
122
123		$ret = GalleryCoreApi::releaseLocks($lockId);
124		if ($ret) {
125		    return array($ret, null);
126		}
127
128		/* Request a redirect to the confirmation screen */
129		$redirect['view'] = 'core.UserAdmin';
130		$redirect['subView'] = 'core.UserChangePassword';
131		$status['changedPassword'] = 1;
132	    }
133	}
134
135	if (!empty($redirect)) {
136	    $results['redirect'] = $redirect;
137	} else {
138	    $results['delegate']['view'] = 'core.UserAdmin';
139	    $results['delegate']['subView'] = 'core.UserChangePassword';
140	}
141	$results['status'] = $status;
142	$results['error'] = $error;
143
144	return array(null, $results);
145    }
146}
147
148/**
149 * This view will show a form to change a password
150 */
151class UserChangePasswordView extends GalleryView {
152
153    /**
154     * @see GalleryView::loadTemplate
155     */
156    function loadTemplate(&$template, &$form) {
157        global $gallery;
158
159	/* Load the form with user data the first time around. */
160	if ($form['formName'] != 'UserChangePassword') {
161	    $form['formName'] = 'UserChangePassword';
162	}
163
164	$user = $gallery->getActiveUser();
165
166        if ($user->isLocked()) {
167            return array(null, array('body' => 'modules/core/templates/UserLocked.tpl'));
168        }
169
170	$template->setVariable('controller', 'core.UserChangePassword');
171	return array(null, array('body' => 'modules/core/templates/UserChangePassword.tpl'));
172    }
173
174    /**
175     * @see GalleryView::getViewDescription
176     */
177    function getViewDescription() {
178	list ($ret, $core) = GalleryCoreApi::loadPlugin('module', 'core');
179	if ($ret) {
180	    return array($ret, null);
181	}
182
183	return array(null, $core->translate('change password'));
184    }
185}
186?>
187