Added an option to change admin login, name, and email.
[timetracker.git] / admin_options.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
33 // Access check.
34 if (!ttAccessCheck(right_administer_site)) {
35   header('Location: access_denied.php');
36   exit();
37 }
38
39 if ($request->isPost()) {
40   $cl_name = trim($request->getParameter('name'));
41   $cl_login = trim($request->getParameter('login'));
42   if (!$auth->isPasswordExternal()) {
43     $cl_password1 = $request->getParameter('password1');
44     $cl_password2 = $request->getParameter('password2');
45   }
46   $cl_email = trim($request->getParameter('email'));
47 } else {
48   $cl_name = $user->name;
49   $cl_login = $user->login;
50   $cl_email = $user->email;
51 }
52
53 $form = new Form('optionsForm');
54 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'name','value'=>$cl_name));
55 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','value'=>$cl_login));
56 if (!$auth->isPasswordExternal()) {
57   $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password1','aspassword'=>true,'value'=>$cl_password1));
58   $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password2','aspassword'=>true,'value'=>$cl_password2));
59 }
60 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'email','value'=>$cl_email));
61 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
62
63 if ($request->isPost()) {
64   // Validate user input.
65   if (!ttValidString($cl_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.person_name'));
66   if (!ttValidString($cl_login)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.login'));
67   // New login must be unique.
68   if ($cl_login != $user->login && ttUserHelper::getUserByLogin($cl_login))
69     $err->add($i18n->getKey('error.user_exists'));
70   if (!$auth->isPasswordExternal() && ($cl_password1 || $cl_password2)) {
71     if (!ttValidString($cl_password1)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
72     if (!ttValidString($cl_password2)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
73     if ($cl_password1 !== $cl_password2)
74       $err->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
75   }
76   if (!ttValidEmail($cl_email, true)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
77   // Finished validating user input.
78
79   if ($err->no()) {
80     if (ttUserHelper::update($user->id, array(
81       'name' => $cl_name,
82       'login' => $cl_login,
83       'password' => $cl_password1,
84       'email' => $cl_email,
85       'status' => ACTIVE))) {
86       header('Location: admin_teams.php');
87       exit();
88     } else {
89       $err->add($i18n->getKey('error.db'));
90     }
91   }
92 } // isPost
93
94 $smarty->assign('auth_external', $auth->isPasswordExternal());
95 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
96 $smarty->assign('title', $i18n->getKey('title.options'));
97 $smarty->assign('content_page_name', 'admin_options.tpl');
98 $smarty->display('index.tpl');