Imporoved isTrue function to get rid of PHP warnings.
[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
32 if (!isTrue('MULTITEAM_MODE') || $auth->isPasswordExternal()) {
33   header('Location: login.php');
34   exit();
35 }
36
37 // Use the "limit" plugin if we have one. Ignore include errors.
38 // The "limit" plugin is not required for normal operation of Time Tracker.
39 @include('plugins/limit/register.php');
40
41 $auth->doLogout();
42
43 if (!defined('CURRENCY_DEFAULT')) define('CURRENCY_DEFAULT', '$');
44
45 if ($request->isPost()) {
46   $cl_group_name = trim($request->getParameter('group_name'));
47   $cl_currency = trim($request->getParameter('currency'));
48   if (!$cl_currency) $cl_currency = CURRENCY_DEFAULT;
49   $cl_lang = $request->getParameter('lang');
50   $cl_manager_name = trim($request->getParameter('manager_name'));
51   $cl_manager_login = trim($request->getParameter('manager_login'));
52   $cl_password1 = $request->getParameter('password1');
53   $cl_password2 = $request->getParameter('password2');
54   $cl_manager_email = trim($request->getParameter('manager_email'));
55 } else {
56   $cl_currency = CURRENCY_DEFAULT;
57   $cl_lang = $i18n->lang; // Browser setting from initialize.php.
58 }
59
60 $form = new Form('groupForm');
61 $form->addInput(array('type'=>'text','maxlength'=>'200','name'=>'group_name','value'=>$cl_group_name));
62 $form->addInput(array('type'=>'text','maxlength'=>'7','name'=>'currency','value'=>$cl_currency));
63
64 // Prepare an array of available languages.
65 $lang_files = I18n::getLangFileList();
66 foreach ($lang_files as $lfile) {
67   $content = file(RESOURCE_DIR."/".$lfile);
68   $lname = '';
69   foreach ($content as $line) {
70     if (strstr($line, 'i18n_language')) {
71       $a = explode('=', $line);
72       $lname = trim(str_replace(';','',str_replace("'","",$a[1])));
73       break;
74     }
75   }
76   unset($content);
77   $longname_lang[] = array('id'=>I18n::getLangFromFilename($lfile),'name'=>$lname);
78 }
79 $longname_lang = mu_sort($longname_lang, 'name');
80 $form->addInput(array('type'=>'combobox','name'=>'lang','style'=>'width: 200px','data'=>$longname_lang,'datakeys'=>array('id','name'),'value'=>$cl_lang));
81
82 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_name','value'=>$cl_manager_name));
83 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_login','value'=>$cl_manager_login));
84 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password1','value'=>$cl_password1));
85 $form->addInput(array('type'=>'password','maxlength'=>'30','name'=>'password2','value'=>$cl_password2));
86 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'manager_email','value'=>$cl_manager_email));
87 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.submit')));
88
89 if ($request->isPost()) {
90   // Create fields array for ttRegistrator instance.
91   $fields = array(
92     'user_name' => $cl_manager_name,
93     'login' => $cl_manager_login,
94     'password1' => $cl_password1,
95     'password2' => $cl_password2,
96     'email' => $cl_manager_email,
97     'group_name' => $cl_group_name,
98     'currency' => $cl_currency,
99     'lang' => $cl_lang);
100
101   // Create an instance of ttRegistrator class.
102   import('ttRegistrator');
103   $registrator = new ttRegistrator($fields, $err);
104   $registrator->register();
105
106   if ($err->no()) {
107     // Registration successful.
108     if ($auth->doLogin($cl_manager_login, $cl_password1)) {
109       setcookie('tt_login', $cl_manager_login, time() + COOKIE_EXPIRE, '/');
110       header('Location: time.php');
111     } else {
112       header('Location: login.php');
113     }
114     exit();
115   }
116 } // isPost
117
118 $smarty->assign('title', $i18n->get('title.add_group'));
119 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
120 $smarty->assign('onload', 'onLoad="document.groupForm.group_name.focus()"');
121 $smarty->assign('content_page_name', 'register.tpl');
122 $smarty->display('index.tpl');