Removed PHP closing tag in more files
[timetracker.git] / register.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 if (!isTrue(MULTITEAM_MODE) || $auth->isPasswordExternal()) {
34   header('Location: login.php');
35   exit();
36 }
37
38 $auth->doLogout();
39
40 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
41
42 if ($request->getMethod() == 'POST') {
43   $cl_team_name = trim($request->getParameter('team_name'));
44   $cl_currency = trim($request->getParameter('currency'));
45   if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
46   $cl_manager_name = trim($request->getParameter('manager_name'));
47   $cl_manager_login = trim($request->getParameter('manager_login'));
48   $cl_password1 = $request->getParameter('password1');
49   $cl_password2 = $request->getParameter('password2');
50   $cl_manager_email = trim($request->getParameter('manager_email'));
51 } else  
52   $cl_currency = CURRENCY_DEFAULT;
53
54 $form = new Form('profileForm');
55 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'team_name','value'=>$cl_team_name));
56 $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
57 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name));
58 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login));
59 $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password1','aspassword'=>true,'value'=>$cl_password1));
60 $form->addInput(array('type'=>'text','maxlength'=>'30','name'=>'password2','aspassword'=>true,'value'=>$cl_password2));
61 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email','value'=>$cl_manager_email));
62 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->getKey('button.submit')));
63
64 if ($request->getMethod() == 'POST') {
65   // Validate user input.
66   if (!ttValidString($cl_team_name, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.team_name'));
67   if (!ttValidString($cl_currency, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.currency'));
68   if (!ttValidString($cl_manager_name)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_name'));
69   if (!ttValidString($cl_manager_login)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.manager_login'));
70   if (!ttValidString($cl_password1)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.password'));
71   if (!ttValidString($cl_password2)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.confirm_password'));
72   if ($cl_password1 !== $cl_password2)
73     $errors->add($i18n->getKey('error.not_equal'), $i18n->getKey('label.password'), $i18n->getKey('label.confirm_password'));
74   if (!ttValidEmail($cl_manager_email, true)) $errors->add($i18n->getKey('error.field'), $i18n->getKey('label.email'));
75
76   if ($errors->isEmpty()) {
77     if (!ttUserHelper::getUserByLogin($cl_manager_login)) {
78       // Create a new team.
79       $team_id = ttTeamHelper::insert(array('name'=>$cl_team_name,'currency'=>$cl_currency));
80       if ($team_id) {
81         // Team created, now create a team manager.
82         $user_id = ttUserHelper::insert(array(
83           'team_id' => $team_id,
84           'role' => ROLE_MANAGER,
85           'name' => $cl_manager_name,
86           'login' => $cl_manager_login,
87           'password' => $cl_password1,
88           'email' => $cl_manager_email));
89       }
90       if ($team_id && $user_id) {
91         if ($auth->doLogin($cl_manager_login, $cl_password1)) {
92           setcookie('tt_login', $cl_manager_login, time() + COOKIE_EXPIRE, '/');
93           header('Location: time.php');
94         } else {
95           header('Location: login.php');
96         }
97         exit();
98       } else
99         $errors->add($i18n->getKey('error.db'));
100     } else
101       $errors->add($i18n->getKey('error.user_exists'));
102   }
103 }
104
105 $smarty->assign('title', $i18n->getKey('title.create_team'));
106 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
107 $smarty->assign('onload', 'onLoad="document.profileForm.team.focus()"');
108 $smarty->assign('content_page_name', 'register.tpl');
109 $smarty->display('index.tpl');