Fixed getUser to include client_id.
[timetracker.git] / profile_edit.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('ttRoleHelper');
33
34 // Access checks.
35 if (!ttAccessAllowed('manage_own_settings')) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 // End of access checks.
40
41 $can_manage_account = $user->can('manage_own_account');
42
43 if ($request->isPost()) {
44   $cl_name = trim($request->getParameter('name'));
45   $cl_login = trim($request->getParameter('login'));
46   if (!$auth->isPasswordExternal()) {
47     $cl_password1 = $request->getParameter('password1');
48     $cl_password2 = $request->getParameter('password2');
49   }
50   $cl_email = trim($request->getParameter('email'));
51 } else {
52   $cl_name = $user->name;
53   $cl_login = $user->login;
54   $cl_email = $user->email;
55 }
56
57 $form = new Form('profileForm');
58 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name,'enable'=>$can_manage_account));
59 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login,'enable'=>$can_manage_account));
60 if (!$auth->isPasswordExternal()) {
61   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
62   $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
63 }
64 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email,'enable'=>$can_manage_account));
65 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
66
67 if ($request->isPost()) {
68   // Validate user input.
69   if (!ttValidString($cl_name)) $err->add($i18n->get('error.field'), $i18n->get('label.person_name'));
70   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
71
72   // New login must be unique.
73   if ($cl_login != $user->login && ttUserHelper::getUserByLogin($cl_login))
74     $err->add($i18n->get('error.user_exists'));
75
76   if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
77     if (!ttValidString($cl_password1)) $err->add($i18n->get('error.field'), $i18n->get('label.password'));
78     if (!ttValidString($cl_password2)) $err->add($i18n->get('error.field'), $i18n->get('label.confirm_password'));
79     if ($cl_password1 !== $cl_password2)
80       $err->add($i18n->get('error.not_equal'), $i18n->get('label.password'), $i18n->get('label.confirm_password'));
81   }
82   if (!ttValidEmail($cl_email, true)) $err->add($i18n->get('error.field'), $i18n->get('label.email'));
83   // Finished validating user input.
84
85   if ($err->no()) {
86     $update_result = ttUserHelper::update($user->id, array(
87         'name' => $cl_name,
88         'login' => $cl_login,
89         'password' => $cl_password1,
90         'email' => $cl_email,
91         'status' => ACTIVE));
92     if ($update_result) {
93       header('Location: time.php');
94       exit();
95     } else
96       $err->add($i18n->get('error.db'));
97   }
98 } // isPost
99
100 $smarty->assign('auth_external', $auth->isPasswordExternal());
101 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
102 $smarty->assign('title', $i18n->get('title.profile'));
103 $smarty->assign('content_page_name', 'profile_edit.tpl');
104 $smarty->display('index.tpl');