posaune
[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 define('MAX_RANK', 512); // Max user rank.
46
47 require_once(LIBRARY_DIR.'/common.lib.php');
48
49 // Require the configuration file with application settings.
50 if (!file_exists(APP_DIR."/WEB-INF/config.php")) die ("WEB-INF/config.php file does not exist.");
51 require_once("WEB-INF/config.php");
52 // Check whether DSN is defined.
53 if (!defined("DSN")) {
54   die ("DSN value is not defined. Check your config.php file.");
55 }
56
57 // Depending on DSN, require either mysqli or mysql extensions.
58 if (strrpos(DSN, 'mysqli://', -strlen(DSN)) !== FALSE) {
59   check_extension('mysqli'); // DSN starts with mysqli:// - require mysqli extension.
60 }
61 if (strrpos(DSN, 'mysql://', -strlen(DSN)) !== FALSE) {
62   check_extension('mysql');  // DSN starts with mysql:// - require mysql extension.
63 }
64
65 // Require other extensions.
66 check_extension('mbstring');
67
68 // If auth params are not defined (in config.php) - initialize with an empty array.
69 if (!isset($GLOBALS['AUTH_MODULE_PARAMS']) || !is_array($GLOBALS['AUTH_MODULE_PARAMS']))
70   $GLOBALS['AUTH_MODULE_PARAMS'] = array();
71
72 // Smarty initialization.
73 import('smarty.Smarty');
74 $smarty = new Smarty;
75 $smarty->use_sub_dirs = false;
76 $smarty->template_dir = TEMPLATE_DIR;
77 $smarty->compile_dir  = TEMPLATE_DIR.'_c';
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 PHP session path, if defined to avoid garbage collection interference from other scripts.
88 if (defined('PHP_SESSION_PATH')) {
89   ini_set('session.save_path', PHP_SESSION_PATH);
90   ini_set('session.gc_probability', 1);
91 }
92
93 // Set session cookie lifetime.
94 session_set_cookie_params($phpsessid_ttl);
95 if (isset($_COOKIE['tt_PHPSESSID'])) {
96   // Extend PHP session cookie lifetime by PHPSESSID_TTL (if defined, otherwise 24 hours) 
97   // so that users don't have to re-login during this period from now. 
98   setcookie('tt_PHPSESSID', $_COOKIE['tt_PHPSESSID'],  time() + $phpsessid_ttl, '/');
99 }
100
101 // Start or resume PHP session.
102 session_name('tt_PHPSESSID'); // "tt_" prefix is to avoid sharing session with other PHP apps that do not name session.
103 @session_start();
104
105 // Authorization.
106 import('Auth');
107 $auth = Auth::factory(AUTH_MODULE, $GLOBALS['AUTH_MODULE_PARAMS']);
108
109 // Some defines we'll need.
110 //
111 define('RESOURCE_DIR', APP_DIR.'/WEB-INF/resources');
112 define('COOKIE_EXPIRE', 60*60*24*30); // Cookies expire in 30 days.
113
114 // Status values for projects, users, etc.
115 define('ACTIVE', 1);
116 define('INACTIVE', 0);
117 // define('DELETED', -1); // DELETED items should have a NULL status. This allows us to have duplicate NULL status entries with existing indexes.
118
119 // Definitions for tracking mode types.
120 define('MODE_TIME', 0); // Tracking time only. There are no projects or tasks.
121 define('MODE_PROJECTS', 1); // Tracking time per projects. There are no tasks.
122 define('MODE_PROJECTS_AND_TASKS', 2); // Tracking time for projects and tasks.
123
124 // Definitions of types for time records.
125 define('TYPE_ALL', 0); // Time record can be specified with either duration or start and finish times.
126 define('TYPE_START_FINISH', 1); // Time record has start and finish times.
127 define('TYPE_DURATION', 2); // Time record has only duration, no start and finish times.
128
129 define('CHARSET', 'utf-8');
130
131 date_default_timezone_set(@date_default_timezone_get());
132
133 // Strip auto-inserted extra slashes when magic_quotes ON for PHP versions prior to 5.4.0.
134 if (get_magic_quotes_gpc())
135   magic_quotes_off();
136
137 // Initialize global objects that are needed for the application.
138 import('html.HttpRequest');
139 $request = new ttHttpRequest();
140
141 import('form.ActionErrors');
142 $err = new ActionErrors(); // Error messages for user.
143 $msg = new ActionErrors(); // Notification messages (not errrors) for user.
144
145 // Create an instance of ttUser class. This gets us most of user details.
146 import('ttUser');
147 $user = new ttUser(null, $auth->getUserId());
148 if ($user->custom_logo) {
149   $smarty->assign('custom_logo', 'images/'.$user->group_id.'.png');
150   $smarty->assign('mobile_custom_logo', '../images/'.$user->group_id.'.png');
151 }
152 $smarty->assign('user', $user);
153
154 // Localization.
155 import('I18n');
156 $i18n = new I18n();
157
158 // Determine the language to use.
159 $lang = $user->lang;
160 if (!$lang) {
161   if (defined('LANG_DEFAULT'))
162     $lang = LANG_DEFAULT;
163
164   // If we still do not have the language get it from the browser.
165   if (!$lang) {
166     $lang = $i18n->getBrowserLanguage();
167
168     // Finally - English is the default.
169     if (!$lang) {
170       $lang = 'en';
171     }
172   }
173 }
174
175 // Load i18n file.
176 $i18n->load($lang);
177
178 // Assign things for smarty to use in template files.
179 $smarty->assign('i18n', $i18n->keys);
180 $smarty->assign('err', $err);
181 $smarty->assign('msg', $msg);
182
183 // TODO: move this code out of here to the files that use it.
184
185 // We use js/strftime.js to print dates in JavaScript (in DateField controls).
186 // One of our date formats (%d.%m.%Y %a) prints a localized short weekday name (%a).
187 // The init_js_date_locale function iniitializes Date.ext.locales array in js/strftime.js for our language
188 // so that we could print localized short weekday names.
189 //
190 // JavaScript usage (see http://hacks.bluesmoon.info/strftime/localisation.html).
191 //
192 // var d = new Date();
193 // d.locale = "fr";           // Remember to initialize locale.
194 // d.strftime("%d.%m.%Y %a"); // This will output a localized %a as in "31.05.2013 Ven"
195
196 // Initialize date locale for JavaScript.
197 init_js_date_locale();
198
199 function init_js_date_locale()
200 {
201   global $i18n, $smarty;
202   $lang = $i18n->lang;
203
204   $days = $i18n->weekdayNames;
205   $short_day_names = array();
206   foreach($days as $k => $v) {
207     $short_day_names[$k] = mb_substr($v, 0, 3, 'utf-8');
208   }
209
210   /*
211   $months = $i18n->monthNames;
212   $short_month_names = array();
213   foreach ($months as $k => $v) {
214     $short_month_names[$k] = mb_substr($v, 0, 3, 'utf-8');
215   }
216   $js = "Date.ext.locales['$lang'] = {
217       a: ['" . join("', '", $short_day_names) . "'],
218       A: ['" . join("', '", $days) . "'],
219       b: ['" . join("', '", $short_month_names) . "'],
220       B: ['" . join("', '", $months) . "'],
221       c: '%a %d %b %Y %T %Z',
222       p: ['', ''],
223       P: ['', ''],
224       x: '%Y-%m-%d',
225       X: '%T'
226     };"; */
227   // We use %a in one of date formats. Therefore, simplified code here (instead of the above block).
228   // %p is also used on the Profile page in 12-hour time format example. Note that %p is not localized.
229   $js = "Date.ext.locales['$lang'] = {
230       a: ['" . join("', '", $short_day_names) . "'],
231       p: ['AM', 'PM']
232     };";
233   $smarty->assign('js_date_locale', $js);
234 }