Removed debug tracing from time.php to keep it simple.
[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 // Access checks.
37 $cl_ref = $request->getParameter('ref');
38 if (!$cl_ref || $auth->isPasswordExternal()) {
39   header('Location: login.php');
40   exit();
41 }
42 $user_id = ttUserHelper::getUserIdByTmpRef($cl_ref);
43 if (!$user_id) {
44   header('Location: access_denied.php'); // No user found by provided reference.
45   exit();
46 }
47 // End of access checks.
48
49 $user = new ttUser(null, $user_id); // Note: reusing $user from initialize.php.
50 // In case user language is different - reload $i18n.
51 if ($i18n->lang != $user->lang) {
52   $i18n->load($user->lang);
53   $smarty->assign('i18n', $i18n->keys);
54 }
55 if ($user->custom_logo) {
56   $smarty->assign('custom_logo', 'images/'.$user->group_id.'.png');
57   $smarty->assign('mobile_custom_logo', '../images/'.$user->group_id.'.png');
58 }
59 $smarty->assign('user', $user);
60
61 $cl_password1 = $request->getParameter('password1');
62 $cl_password2 = $request->getParameter('password2');
63
64 $form = new Form('newPasswordForm');
65 $form->addInput(array('type'=>'password','maxlength'=>'120','name'=>'password1','value'=>$cl_password1));
66 $form->addInput(array('type'=>'password','maxlength'=>'120','name'=>'password2','value'=>$cl_password2));
67 $form->addInput(array('type'=>'hidden','name'=>'ref','value'=>$cl_ref));
68 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
69
70 if ($request->isPost()) {
71   // Validate user input.
72   if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
73   if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
74   if ($cl_password1 !== $cl_password2)
75     $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
76
77   if ($err->no()) {
78     // Use the "limit" plugin if we have one. Ignore include errors.
79     // The "limit" plugin is not required for normal operation of Time Tracker.
80     $cl_login = $user->login; // $cl_login is used in access_check.cpp.
81     @include('plugins/limit/access_check.php');
82
83     ttUserHelper::setPassword($user_id, $cl_password1);
84
85     if ($auth->doLogin($user->login, $cl_password1)) {
86       setcookie('tt_login', $user->login, time() + COOKIE_EXPIRE, '/');
87       // Redirect, depending on user role.
88       if ($user->can('administer_site')) {
89         header('Location: admin_groups.php');
90       } elseif ($user->isClient()) {
91         header('Location: reports.php');
92       } else {
93         header('Location: time.php');
94       }
95       exit();
96     } else {
97       $err->add($i18n->get('error.auth'));
98     }
99   }
100 } // isPost
101
102 $smarty->assign('forms', array($form->getName() => $form->toArray()));
103 $smarty->assign('title', $i18n->get('title.change_password'));
104 $smarty->assign('content_page_name', 'password_change.tpl');
105 $smarty->display('index.tpl');