Work in progress creating a repo
[timetracker.git] / initialize.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 // Report all errors except E_NOTICE and E_STRICT.
30 // Ignoring E_STRICT is here because PEAR 1.9.4 that we use is not E_STRICT compliant.
31 if (!defined('E_STRICT')) define('E_STRICT', 2048);
32 // if (!defined('E_DEPRECATED')) define('E_DEPRECATED', 8192);
33 error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT); // & ~E_DEPRECATED);
34 // E_ALL tends to change as PHP evloves, therefore we use & here instead of exclusive OR (^).
35
36 // Disable displaying errors on screen.
37 ini_set('display_errors', 'Off');
38
39 // require_once('init_auth.php');
40 define("APP_DIR", dirname(__FILE__));
41 define("LIBRARY_DIR", APP_DIR."/WEB-INF/lib");
42 define("TEMPLATE_DIR", APP_DIR."/WEB-INF/templates");
43 // Date format for database and URI parameters.
44 define('DB_DATEFORMAT', '%Y-%m-%d');
45
46 require_once(LIBRARY_DIR.'/common.lib.php');
47
48 // Require the configuration file with application settings.
49 if (!file_exists(APP_DIR."/WEB-INF/config.php")) die ("WEB-INF/config.php file does not exist.");
50 require_once("WEB-INF/config.php");
51 // Check whether DSN is defined.
52 if (!defined("DSN")) {
53   die ("DSN value is not defined. Check your config.php file.");
54 }
55
56 // Depending on DSN, require either mysqli or mysql extensions.
57 if (strrpos(DSN, 'mysqli://', -strlen(DSN)) !== FALSE) {
58   check_extension('mysqli'); // DSN starts with mysqli:// - require mysqli extension.
59 }
60 if (strrpos(DSN, 'mysql://', -strlen(DSN)) !== FALSE) {
61   check_extension('mysql');  // DSN starts with mysql:// - require mysql extension.
62 }
63
64 // Require other extensions.
65 check_extension('mbstring');
66
67 // If auth params are not defined (in config.php) - initialize with an empty array.
68 if (!isset($GLOBALS['AUTH_MODULE_PARAMS']) || !is_array($GLOBALS['AUTH_MODULE_PARAMS']))
69   $GLOBALS['AUTH_MODULE_PARAMS'] = array();
70   
71 // Smarty initialization.
72 import('smarty.Smarty');
73 $smarty = new Smarty;
74 $smarty->use_sub_dirs = false;
75 $smarty->template_dir = TEMPLATE_DIR;
76 $smarty->compile_dir  = TEMPLATE_DIR.'_c';
77 $GLOBALS['SMARTY'] = &$smarty;
78
79 // Note: these 3 settings below used to be in .htaccess file. Moved them here to eliminate "error 500" problems
80 // with some shared hostings that do not have AllowOverride Options or AllowOverride All in their apache configurations.
81 // Change http cache expiration time to 1 minute.
82 session_cache_expire(1);
83
84 $phpsessid_ttl = defined('PHPSESSID_TTL') ? PHPSESSID_TTL : 60*60*24;
85 // Set lifetime for garbage collection.
86 ini_set('session.gc_maxlifetime', $phpsessid_ttl);
87 // Set session cookie lifetime.
88 session_set_cookie_params($phpsessid_ttl);
89 if (isset($_COOKIE['tt_PHPSESSID'])) {
90   // Extend PHP session cookie lifetime by PHPSESSID_TTL (if defined, otherwise 24 hours) 
91   // so that users don't have to re-login during this period from now. 
92   setcookie('tt_PHPSESSID', $_COOKIE['tt_PHPSESSID'],  time() + $phpsessid_ttl, '/');
93 }
94
95 // Start or resume PHP session.
96 session_name('tt_PHPSESSID'); // "tt_" prefix is to avoid sharing session with other PHP apps that do not name session.  
97 @session_start();
98
99 // Authorization.
100 import('Auth');
101 $auth = Auth::factory(AUTH_MODULE, $GLOBALS['AUTH_MODULE_PARAMS']);
102
103 // Some defines we'll need.
104 //
105 define('RESOURCE_DIR', APP_DIR.'/WEB-INF/resources');
106 define('COOKIE_EXPIRE', 60*60*24*30); // Cookies expire in 30 days.
107
108 // Status values for projects, users, etc.
109 define('ACTIVE', 1);
110 define('INACTIVE', 0);
111 // define('DELETED', -1); // DELETED items should have a NULL status. This allows us to have duplicate NULL status entries with existing indexes.
112
113 // Definitions for tracking mode types.
114 define('MODE_TIME', 0); // Tracking time only. There are no projects or tasks.
115 define('MODE_PROJECTS', 1); // Tracking time per projects. There are no tasks.
116 define('MODE_PROJECTS_AND_TASKS', 2); // Tracking time for projects and tasks.
117
118 // Definitions of types for time records.
119 define('TYPE_ALL', 0); // Time record can be specified with either duration or start and finish times.
120 define('TYPE_START_FINISH', 1); // Time record has start and finish times.
121 define('TYPE_DURATION', 2); // Time record has only duration, no start and finish times.
122
123 // User access rights - bits that collectively define an access mask to the system (a role).
124 // We'll have some bits here (1,2, etc...) reserved for future use.
125 define('right_data_entry', 4);     // Right to enter work hours and expenses.
126 define('right_view_charts', 8);    // Right to view charts.
127 define('right_view_reports', 16);  // Right to view reports.
128 define('right_view_invoices', 32); // Right to view invoices.
129 define('right_manage_team', 64);   // Right to manage team. Note that this is not full access to team.
130 define('right_assign_roles', 128); // Right to assign user roles.
131 define('right_export_team', 256);  // Right to export team data to a file.
132 define('right_administer_site', 1024); // Admin account right to manage the application as a whole. 
133
134 // User roles.
135 define('ROLE_USER', 4);          // Regular user.
136 define('ROLE_CLIENT', 16);       // Client (to view reports and invoices).
137 define('ROLE_COMANAGER', 68);    // Team co-manager. Can do many things but not as much as team manager.
138 define('ROLE_MANAGER', 324);     // Team manager. Can do everything for a team.
139 define('ROLE_SITE_ADMIN', 1024); // Site administrator.
140
141
142 define('CHARSET', 'utf-8');
143
144 date_default_timezone_set(@date_default_timezone_get());
145
146 // Strip auto-inserted extra slashes when magic_quotes ON for PHP versions prior to 5.4.0.
147 if (get_magic_quotes_gpc())
148   magic_quotes_off();
149
150 // Initialize global objects that are needed for the application.
151 import('html.HttpRequest');
152 $request = new ttHttpRequest();
153
154 import('form.ActionErrors');
155 $errors = new ActionErrors();
156 $messages = new ActionErrors();
157
158 // Create an instance of ttUser class. This gets us most of user details.
159 import('ttUser');
160 $user = new ttUser(null, $auth->getUserId());
161 if ($user->custom_logo) {
162   $smarty->assign('custom_logo', 'images/'.$user->team_id.'.png');
163   $smarty->assign('mobile_custom_logo', '../images/'.$user->team_id.'.png');
164 }
165 $smarty->assign('user', $user);
166
167 // Localization.
168 import('I18n');
169 $i18n = new I18n();
170
171 // Determine the language to use.
172 $lang = $user->lang;
173 if (!$lang) {
174   if (defined('LANG_DEFAULT'))
175     $lang = LANG_DEFAULT;
176   
177   // If we still do not have the language get it from the browser.
178   if (!$lang) {
179     $lang = $i18n->getBrowserLanguage();
180          
181     // Finally - English is the default.
182     if (!$lang) {
183       $lang = 'en';
184     }
185   }
186 }
187
188 // Load i18n file.
189 $i18n->load($lang);
190 $GLOBALS['I18N'] = &$i18n;
191
192 $GLOBALS['USER'] = &$user;
193
194 // Assign things for smarty to use in template files.
195 $smarty->assign('i18n', $i18n->keys);
196 $smarty->assign('errors', $errors);
197 $smarty->assign('messages', $messages);
198
199 // TODO: move this code out of here to the files that use it. 
200   
201 // We use js/strftime.js to print dates in JavaScript (in DateField controls).
202 // One of our date formats (%d.%m.%Y %a) prints a localized short weekday name (%a).
203 // The init_js_date_locale function iniitializes Date.ext.locales array in js/strftime.js for our language
204 // so that we could print localized short weekday names.
205 //
206 // JavaScript usage (see http://hacks.bluesmoon.info/strftime/localisation.html).
207 //
208 // var d = new Date();
209 // d.locale = "fr";           // Remember to initialize locale.
210 // d.strftime("%d.%m.%Y %a"); // This will output a localized %a as in "31.05.2013 Ven"
211
212 // Initialize date locale for JavaScript.
213 init_js_date_locale();
214
215 function init_js_date_locale()
216 {
217   global $i18n, $smarty;
218   $lang = $i18n->lang;
219
220   $days = $i18n->weekdayNames;
221   $short_day_names = array();
222   foreach($days as $k => $v) {
223     $short_day_names[$k] = mb_substr($v, 0, 3, 'utf-8');
224   }
225
226   /*
227   $months = $i18n->monthNames;
228   $short_month_names = array();
229   foreach ($months as $k => $v) {
230     $short_month_names[$k] = mb_substr($v, 0, 3, 'utf-8');
231   }
232   $js = "Date.ext.locales['$lang'] = {
233       a: ['" . join("', '", $short_day_names) . "'],
234       A: ['" . join("', '", $days) . "'],
235       b: ['" . join("', '", $short_month_names) . "'],
236       B: ['" . join("', '", $months) . "'],
237       c: '%a %d %b %Y %T %Z',
238       p: ['', ''],
239       P: ['', ''],
240       x: '%Y-%m-%d',
241       X: '%T'
242     };"; */
243   // We use %a in one of date formats. Therefore, simplified code here (instead of the above block).
244   // %p is also used on the Profile page in 12-hour time format example. Note that %p is not localized.
245   $js = "Date.ext.locales['$lang'] = {
246       a: ['" . join("', '", $short_day_names) . "'],
247       p: ['AM', 'PM']
248     };";
249   $smarty->assign('js_date_locale', $js);
250 }
251 ?>