-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhook.php
465 lines (384 loc) · 17.8 KB
/
hook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/**
* Summary of plugin_timezones_install
* @return true or die!
*/
function plugin_timezones_install() {
global $DB;
if (!$DB->tableExists("glpi_plugin_timezones_users")) {
$query = " CREATE TABLE `glpi_plugin_timezones_users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`users_id` INT(11) NOT NULL,
`timezone` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `users_id` (`users_id`),
INDEX `timezone` (`timezone`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
";
$DB->query($query) or die("error creating glpi_plugin_timezones_users" . $DB->error());
} else if (!$DB->fieldExists("glpi_plugin_timezones_users", "users_id")) {
$query = " ALTER TABLE `glpi_plugin_timezones_users`
ADD COLUMN `id` INT(11) NOT NULL AUTO_INCREMENT FIRST,
CHANGE COLUMN `id` `users_id` INT(11) NOT NULL AFTER `id`,
DROP PRIMARY KEY,
ADD PRIMARY KEY (`id`),
ADD UNIQUE INDEX `users_id` (`users_id`);
";
$DB->query($query) or die("error altering glpi_plugin_timezones_users" . $DB->error());
}
if (!$DB->tableExists("glpi_plugin_timezones_dbbackups")) {
$query = " CREATE TABLE `glpi_plugin_timezones_dbbackups` (
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`table_name` VARCHAR(255) NULL ,
`alter_table` TEXT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
";
$DB->query($query) or die("error creating glpi_plugin_timezones_dbbackups" . $DB->error());
}
if (!$DB->tableExists("glpi_plugin_timezones_tasks_localtimes")) {
$query = " CREATE TABLE `glpi_plugin_timezones_tasks_localtimes` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`items_type` VARCHAR(50) NOT NULL,
`items_id` INT(11) NOT NULL,
`begin` VARCHAR(20) NULL DEFAULT NULL COMMENT 'In order to keep local time',
`end` VARCHAR(20) NULL DEFAULT NULL COMMENT 'In order to keep local time',
`tz_name` VARCHAR(64) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `items_type_items_id` (`items_type`, `items_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
";
$DB->query($query) or die("error creating glpi_plugin_timezones_tasks_localtimes" . $DB->error());
} else if (!$DB->fieldExists("glpi_plugin_timezones_tasks_localtimes", "tz_name")) {
$query = " ALTER TABLE `glpi_plugin_timezones_tasks_localtimes`
ADD COLUMN `tz_name` VARCHAR(64) NULL AFTER `end`;
";
$DB->query($query) or die("error adding 'tz_name' into glpi_plugin_timezones_tasks_localtimes" . $DB->error());
}
// last but not least, must convert any DATETIME field into TIMESTAMP
convertDB();
return true;
}
/**
* Summary of convertDB
* @param mixed $echo
* @return boolean true if success, else will die
*/
function convertDB($echo = false, $res = null) {
global $DB;
$now = date('Y-m-d H:i:s' );
if (!$res) {
$res = needConvert();
}
foreach ($res as $table) {
$tablealter = $tablebackup = ''; // init by default
// get table create code to get accurate table definition for backup purpose
$query="SHOW CREATE TABLE `".$table['TABLE_NAME']."`;";
$res = $DB->query( $query );
$tabledef = $DB->fetch_assoc( $res );
$tablelines = explode("\n", $tabledef['Create Table']);
foreach ($tablelines as $line) {
if (stripos( $line, " datetime " ) !== false) {
// then we must backup this line
$tablebackup .= "\n MODIFY ".$line;
}
}
// get accurate info from information_schema to perform correct alter
$query = "SELECT * from `INFORMATION_SCHEMA`.`COLUMNS` WHERE TABLE_SCHEMA = '".$DB->dbdefault."' AND TABLE_NAME LIKE '".$table['TABLE_NAME']."' AND COLUMN_TYPE IN ('DATETIME'); ";
foreach ($DB->request($query) as $column) {
$defaultalter = $commentalter = '';
// we have all columns representing temporal values that we want to change to TIMESTAMP
if ($column['IS_NULLABLE']=='YES') {
$nullable = "NULL";
} else {
$nullable = "NOT NULL";
}
if (is_null($column['COLUMN_DEFAULT']) && $column['IS_NULLABLE']=='NO') { // no default
$defaultalter=" DEFAULT '0000-00-00 00:00:00'";
} else if (is_null($column['COLUMN_DEFAULT']) && $column['IS_NULLABLE']=='YES') {
$defaultalter = " DEFAULT NULL";
} else if (!is_null($column['COLUMN_DEFAULT'])) {
if ($column['COLUMN_DEFAULT'] == '0000-00-00 00:00:00') {
$defaultalter = " DEFAULT '0000-00-00 00:00:00'";
} else if ($column['COLUMN_DEFAULT'] < '1970-01-01 00:00:01') {
$defaultDate = new DateTime( '1970-01-01 00:00:01', new DateTimeZone( 'UTC' ) );
$defaultDate->setTimezone( new DateTimeZone( date_default_timezone_get() ) );
$defaultalter = " DEFAULT '".$defaultDate->format("Y-m-d H:i:s")."'";
} else if ($column['COLUMN_DEFAULT'] > '2038-01-19 03:14:07') {
$defaultDate = new DateTime( '2038-01-19 03:14:07', new DateTimeZone( 'UTC' ) );
$defaultDate->setTimezone( new DateTimeZone( date_default_timezone_get() ) );
$defaultalter = " DEFAULT '".$defaultDate->format("Y-m-d H:i:s")."'";
} else {
$defaultalter = " DEFAULT '".$column['COLUMN_DEFAULT']."'";
}
}
if ($column['COLUMN_COMMENT'] != '') {
$commentalter = " COMMENT '".$column['COLUMN_COMMENT']."'";
}
$tablealter .= "\n MODIFY COLUMN `".$column['COLUMN_NAME']."` TIMESTAMP $nullable $defaultalter $commentalter,";
}
// must delete last ',' from $tablebackup and $tablealter if we have one
// create backup of the column definitions so that we may apply them to restore database when uninstalling plugin
$tablebackup = $DB->escape( rtrim( $tablebackup, "," ) );
$tablealter = rtrim( $tablealter, "," );
// special case for glpi_*tasks tables for objects like TicketTask, ProblemTask, ChangeTask
// we must first copy local times from 'glpi_tickettasks' and 'glpi_problemtasks' tables to glpi_plugin_timezones_tasks_localtimes
if ($table['TABLE_NAME'] == 'glpi_tickettasks') {
// begin drafts
//INSERT INTO glpi_plugin_timezones_tasks_localtimes SELECT NULL, 'TicketTask' as items_type, id as items_id, `begin`, `end`
//FROM glpi_tickettasks
//WHERE glpi_tickettasks.id NOT IN (SELECT glpi_plugin_timezones_tasks_localtimes.items_id FROM glpi_plugin_timezones_tasks_localtimes WHERE glpi_plugin_timezones_tasks_localtimes.items_type='TicketTask') ;
//SELECT NULL, tickets_id, 'TicketTask' as items_type, glpi_tickettasks.id as items_id, `begin`, `end`, CONVERT_TZ(`begin`, 'Europe/Paris', glpi_plugin_timezones_users.timezone) as 'begin-conv', glpi_plugin_timezones_users.timezone FROM glpi_tickettasks
//left join glpi_plugin_timezones_users on glpi_plugin_timezones_users.users_id=users_id_tech;
// end drafts
$query = "INSERT INTO glpi_plugin_timezones_tasks_localtimes SELECT NULL, 'TicketTask' as items_type, id as items_id, `begin`, `end`, NULL FROM glpi_tickettasks;";
$DB->query( $query );
}
if ($table['TABLE_NAME'] == 'glpi_problemtasks') {
$query = "INSERT INTO glpi_plugin_timezones_tasks_localtimes SELECT NULL, 'ProblemTask' as items_type, id as items_id, `begin`, `end`, NULL FROM glpi_problemtasks;";
$DB->query( $query );
}
if ($table['TABLE_NAME'] == 'glpi_changetasks') {
$query = "INSERT INTO glpi_plugin_timezones_tasks_localtimes SELECT NULL, 'ChangeTask' as items_type, id as items_id, `begin`, `end`, NULL FROM glpi_changetasks;";
$DB->query( $query );
}
// apply alter to table
$query ="ALTER TABLE `".$table['TABLE_NAME']."` ".$tablealter.";";
if ($echo) {
echo $query;
}
$DB->query( $query ) or die( " --> error when applying ". $DB->error()."\n");
$query = "INSERT INTO `glpi_plugin_timezones_dbbackups` ( `date`, `table_name`, `alter_table`) VALUES ( '$now', '".$table['TABLE_NAME']."', 'ALTER TABLE `".$table['TABLE_NAME']."` $tablebackup' );";
$DB->query( $query ) or die( ' --> error when backing up '.$DB->error()."\n");
if ($echo) {
echo " --> done\n";
}
}
return true;
}
function plugin_timezones_uninstall() {
global $DB;
return true;
}
function plugin_init_session_timezones() {
$users_id = Session::getLoginUserID();
if (!isset($_SESSION["glpicronuserrunning"]) || ($users_id != $_SESSION["glpicronuserrunning"])) {
$pref = new PluginTimezonesUser;
$tzid = false;
if ($users_id) {
$tzid = $pref->getIDFromUserID( $users_id );
}
// default value
$tz = isset($_SESSION['glpitimezone']) ? $_SESSION['glpitimezone'] : @date_default_timezone_get() ;
if ($tzid && $pref->getFromDB( $tzid )) {
$tz = $pref->fields['timezone'];
}
setTimeZone( $tz );
}
}
/**
* Summary of setTimeZone
* @param string $tz timezone to be set like 'Europe/Paris'
*/
function setTimeZone($tz) {
global $DB;
$_SESSION['glpitimezone'] = $tz;
date_default_timezone_set( $tz ) or Toolbox::logInFile("php-errors", "Can't set tz: $tz for ".Session::getLoginUserID()."\n");
$DB->query("SET SESSION time_zone = '$tz'" ) or Toolbox::logInFile("php-errors", "Can't set tz: $tz - ". $DB->error()."\n");
$_SESSION['glpi_currenttime'] = date("Y-m-d H:i:s");
}
function plugin_timezones_postinit() {
$tz = isset($_SESSION['glpitimezone']) ? $_SESSION['glpitimezone'] : @date_default_timezone_get() ;
setTimeZone( $tz );
$formerHandler = set_error_handler(['PluginTimezonesToolbox', 'userErrorHandlerNormal']);
}
function plugin_item_add_update_timezones_tasks(CommonDBTM $parm) {
global $DB;
if ($parm instanceof CommonITILTask) {
$itemType = $parm->getType();
$begin = (isset($parm->fields['begin'])?$parm->fields['begin']:'');
$end = (isset($parm->fields['end'])?$parm->fields['end']:'');
if (isset($_SESSION['glpitimezone'])) {
$tz = $_SESSION['glpitimezone'];
} else {
// a cron is running
// then use default timezone
$tz = @date_default_timezone_get();
}
$query = "REPLACE INTO `glpi_plugin_timezones_tasks_localtimes` (`items_type`, `items_id`, `begin`, `end`, `tz_name`) VALUES ('$itemType', ".$parm->getID().", '$begin', '$end', '$tz');";
$DB->query( $query );
}
}
function plugin_item_add_update_timezones_dbconnection(Config $parm) {
$slaveDB = DBConnection::getDBSlaveConf( );
if ($slaveDB) {
$host = $slaveDB->dbhost;
$user = $slaveDB->dbuser;
$password = $slaveDB->dbpassword;
$DBname = $slaveDB->dbdefault;
unset( $slaveDB );
timezones_createSlaveConnectionFile($host, $user, $password, $DBname) or Toolbox::logInFile('php-errors', "timezones: Can't create config_db_slave.php\n");
}
}
/**
* Create slave DB configuration file
*
* @param host the slave DB host(s)
* @param user the slave DB user
* @param password the slave DB password
* @param DBname the name of the slave DB
*
* @return boolean for success
**/
function timezones_createSlaveConnectionFile($host, $user, $password, $DBname) {
$DB_str = "<?php \n class DBSlave extends DBmysql { \n var \$slave = true; \n var \$dbhost = ";
$host = trim($host);
if (strpos($host, ' ')) {
$hosts = explode(' ', $host);
$first = true;
foreach ($hosts as $host) {
if (!empty($host)) {
$DB_str .= ($first ? "array('" : ",'").$host."'";
$first = false;
}
}
if ($first) {
// no host configured
return false;
}
$DB_str .= ");\n";
} else {
$DB_str .= "'$host';\n";
}
$DB_str .= " var \$dbuser = '" . $user . "'; \n var \$dbpassword= '" .rawurlencode($password) . "'; \n var \$dbdefault = '" . $DBname . "';
function __construct(\$choice=NULL) {
global \$DB;
parent::connect(\$choice);
if (\$this->connected && isset(\$_SESSION['glpitimezone']) ) {
\$dbInit = isset( \$DB ) ;
if( !\$dbInit ) {
\$DB=\$this;
}
\$plug = new Plugin;
if( \$plug->isActivated('timezones' ) ) {
\$tz = \$_SESSION['glpitimezone'] ;
\$this->query(\"SET SESSION time_zone = '\$tz'\" ) or Toolbox::logInFile(\"php-errors\", \"Can't set tz: \$tz - \". \$this->error().\"\\n\");
}
if( !\$dbInit ) {
unset(\$DB) ;
}
}
}
}";
$fp = fopen(GLPI_CONFIG_DIR . "/config_db_slave.php", 'wt');
if ($fp) {
$fw = fwrite($fp, $DB_str);
fclose($fp);
return true;
}
return false;
}
function plugin_timezones_getAddSearchOptions($itemtype) {
global $LANG;
$sopt = [];
if ($itemtype == 'User') {
$sopt[11001]['table'] = 'glpi_plugin_timezones_users';
$sopt[11001]['field'] = 'timezone';
$sopt[11001]['linkfield'] = 'plugin_timezones_users_timezone';
$sopt[11001]['massiveaction'] = true;
$sopt[11001]['name'] = $LANG['timezones']['item']['tab'];
$sopt[11001]['datatype'] = 'dropdown';
$sopt[11001]['forcegroupby'] = true;
$sopt[11001]['joinparams'] = ['jointype' => 'child'];
$sopt[11001]['searchtype'] = 'contains';
}
return $sopt;
}
//function plugin_timezones_addLeftJoin($type,$ref_table,$new_table,$linkfield,&$already_link_tables) {
// switch ($type){
// case 'User':
// switch ($new_table){
// case "glpi_plugin_timezones_users" :
// $out= " LEFT JOIN `glpi_plugin_timezones_users`
// ON (`$ref_table`.`id` = `glpi_plugin_timezones_users`.`id` ) ";
// return $out;
// }
// return "";
// }
// return "";
//}
//function plugin_pre_item_update_timezones_user(CommonDBTM $parm){
// global $DB;
// if($parm->getType() == 'User' && isset( $parm->input['plugin_timezones_users_timezone']) ) {
// $query = "REPLACE INTO `glpi_plugin_timezones_users` (`users_id`, `timezone`) VALUES (".$parm->getID().", '".$parm->input['plugin_timezones_users_timezone']."');";
// $DB->query( $query ) ;
// }
//}
function plugin_timezones_MassiveActionsFieldsDisplay($options = []) {
//$type,$table,$field,$linkfield
$table = $options['options']['table'];
$field = $options['options']['field'];
$linkfield = $options['options']['linkfield'];
if ($options['itemtype'] == 'User') {
// Table fields
switch ($table.".".$field) {
case 'glpi_plugin_timezones_users.timezone' :
$timezones = PluginTimezonesUser::getTimezones( );
// default timezone is the one of PHP
Dropdown::showFromArray('plugin_timezones_users_timezone', $timezones, ['value' => ini_get('date.timezone') ]);
// Need to return true if specific display
return true;
}
}
// Need to return false on non display item
return false;
}
//function plugin_timezones_MassiveActionsProcess($data) {
// global $LANG, $DB;
// switch ($data['action']) {
// case "plugin_timezones_users_timezone" :
// if ($data['itemtype'] == 'User') {
// foreach ($data["item"] as $key => $val) {
// if ($val == 1) {
// $tzUser = new PluginTimezonesUser ;
// $tzUser->getFromDB( $key ) ;
// }
// }
// }
// break;
// }
// return ;
//}
//function plugin_timezones_MassiveActions($type) {
// global $LANG;
// switch ($type) {
// case 'User' :
// return array('plugin_timezones_users_timezone' => 'Update Time Zone');
// }
// return array();
//}
//function plugin_timezones_MassiveActionsDisplay($options) {
// global $LANG;
// switch ($options['itemtype']) {
// case 'User' :
// switch ($options['action']) {
// case "plugin_timezones_users_timezone" :
// $timezones = PluginTimezonesUser::getTimezones( ) ;
// // default timezone is the one of PHP
// Dropdown::showFromArray('timezone', $timezones, array('value' => ini_get('date.timezone')
// ));
// echo " <input type='submit' name='massiveaction' class='submit' ".
// "value='".$LANG['buttons'][2]."'>";
// break;
// }
// break;
// }
// return "";
//}