Switched to using shorter ActionErrors functions
[timetracker.git] / password_change.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('ttUserHelper');
32 import('ttUser');
33
34 $auth->doLogout();
35
36 $cl_ref = $request->getParameter('ref');
37 if (!$cl_ref || $auth->isPasswordExternal()) {
38   header('Location: login.php');
39   exit();
40 }
41
42 // Get user ID.
43 $user_id = ttUserHelper::getUserIdByTmpRef($cl_ref);
44 if ($user_id) {
45   $user = new ttUser(null, $user_id); // Note: reusing $user from initialize.php.
46   // In case user language is different - reload $i18n.
47   if ($i18n->lang != $user->lang) {
48     $i18n->load($user->lang);
49     $smarty->assign('i18n', $i18n->keys);
50   }
51   if ($user->custom_logo) {
52     $smarty->assign('custom_logo', 'images/'.$user->team_id.'.png');
53     $smarty->assign('mobile_custom_logo', '../images/'.$user->team_id.'.png');
54   }
55   $smarty->assign('user', $user);
56 }
57
58 $cl_password1 = $request->getParameter('password1');
59 $cl_password2 = $request->getParameter('password2');
60
61 $form = new Form('newPasswordForm');
62 $form->addInput(array('type'=>'text','maxlength'=>'120','name'=>'password1','aspassword'=>true,'value'=>$cl_password1));
63 $form->addInput(array('type'=>'text','maxlength'=>'120','name'=>'password2','aspassword'=>true,'value'=>$cl_password2));
64 $form->addInput(array('type'=>'hidden','name'=>'ref','value'=>$cl_ref));
65 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->getKey('button.save')));
66
67 if ($request->getMethod() == 'POST') {
68   // Validate user input.
69   if (!ttValidString($cl_password1)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
70   if (!ttValidString($cl_password2)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
71   if ($cl_password1 !== $cl_password2)
72     $errors->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
73
74   if ($errors->no()) {
75     // Use the "limit" plugin if we have one. Ignore include errors.
76     // The "limit" plugin is not required for normal operation of Time Tracker.
77     $cl_login = $user->login; // $cl_login is used in access_check.cpp.
78     @include('plugins/limit/access_check.php');
79
80     ttUserHelper::setPassword($user_id, $cl_password1);
81
82     if ($auth->doLogin($user->login, $cl_password1)) {
83       setcookie('tt_login', $user->login, time() + COOKIE_EXPIRE, '/');
84       header('Location: time.php');
85       exit();
86     } else {
87       $errors->add($i18n->getKey('error.auth'));
88     }
89   }
90 } // POST
91
92 $smarty->assign('forms', array($form->getName() => $form->toArray()));
93 $smarty->assign('title', $i18n->getKey('title.change_password'));
94 $smarty->assign('content_page_name', 'password_change.tpl');
95 $smarty->display('index.tpl');