Skip to content

Commit

Permalink
fixed an issue on TZ when in cron
Browse files Browse the repository at this point in the history
  • Loading branch information
tomolimo committed Jun 18, 2019
1 parent 4a0e3ce commit 06974ce
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 272 deletions.
46 changes: 25 additions & 21 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
function plugin_timezones_install() {
global $DB;

if (!$DB->tableExists("glpi_plugin_timezones_users")) {
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,
Expand All @@ -23,8 +23,8 @@ function plugin_timezones_install() {

$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`
} 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,
Expand All @@ -36,8 +36,8 @@ function plugin_timezones_install() {

}

if (!$DB->tableExists("glpi_plugin_timezones_dbbackups")) {
$query = " CREATE TABLE `glpi_plugin_timezones_dbbackups` (
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
Expand All @@ -46,11 +46,11 @@ function plugin_timezones_install() {
ENGINE=InnoDB;
";

$DB->query($query) or die("error creating glpi_plugin_timezones_dbbackups" . $DB->error());
$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` (
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,
Expand All @@ -65,9 +65,9 @@ function plugin_timezones_install() {
;
";

$DB->query($query) or die("error creating glpi_plugin_timezones_tasks_localtimes" . $DB->error());
$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")) {
} 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`;
";
Expand Down Expand Up @@ -203,15 +203,20 @@ function plugin_timezones_uninstall() {


function plugin_init_session_timezones() {
if (!isset($_SESSION["glpicronuserrunning"]) || (Session::getLoginUserID() != $_SESSION["glpicronuserrunning"])) {
$users_id = Session::getLoginUserID();
if (!isset($_SESSION["glpicronuserrunning"]) || ($users_id != $_SESSION["glpicronuserrunning"])) {
$pref = new PluginTimezonesUser;
$tzid = $pref->getIDFromUserID( Session::getLoginUserID() );
$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 )) {
setTimeZone( $pref->fields['timezone'] );
} else {
setTimeZone( @date_default_timezone_get() );
$tz = $pref->fields['timezone'];
}
setTimeZone( $tz );
}
}

Expand All @@ -221,17 +226,16 @@ function plugin_init_session_timezones() {
*/
function setTimeZone($tz) {
global $DB;
$_SESSION['glpitimezone'] = $tz; // could be redondant, but anyway :)
$_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"); //die ("Can't set tz: ". $DB->error());
$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() {
if (isset($_SESSION['glpitimezone'])) {
setTimeZone( $_SESSION['glpitimezone'] );
$formerHandler = set_error_handler(['PluginTimezonesToolbox', 'userErrorHandlerNormal']);
}
$tz = isset($_SESSION['glpitimezone']) ? $_SESSION['glpitimezone'] : @date_default_timezone_get() ;
setTimeZone( $tz );
$formerHandler = set_error_handler(['PluginTimezonesToolbox', 'userErrorHandlerNormal']);
}


Expand Down
292 changes: 146 additions & 146 deletions setup.php
Original file line number Diff line number Diff line change
@@ -1,146 +1,146 @@
<?php
/*
* -------------------------------------------------------------------------
Timezones management plugin
Copyright (C) 2015-2016-2017 by Raynet SAS a company of A.Raymond Network.
http://www.araymond.com
-------------------------------------------------------------------------
LICENSE
This file is part of Timezones management plugin for GLPI.
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GLPI. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/

// ----------------------------------------------------------------------
// Original Author of file: Olivier Moron
// Purpose of file: to setup time zone management plugin to GLPI
// ----------------------------------------------------------------------
define ("PLUGIN_TIMEZONES_VERSION", "2.4.0");

/**
* Summary of plugin_init_timezones
*/
function plugin_init_timezones() {
global $PLUGIN_HOOKS,$CFG_GLPI;

Plugin::registerClass('PluginTimezonesUser',
['addtabon' => ['Preference', 'User']]);

// Init session
$PLUGIN_HOOKS['init_session']['timezones'] = 'plugin_init_session_timezones';

$PLUGIN_HOOKS['post_init']['timezones'] = 'plugin_timezones_postinit';

$PLUGIN_HOOKS['pre_item_update']['timezones']
= [ 'User' => [ 'PluginTimezonesUser', 'preItemUpdate']];

$PLUGIN_HOOKS['item_update']['timezones']
= [ 'TicketTask' => 'plugin_item_add_update_timezones_tasks',
'ProblemTask' => 'plugin_item_add_update_timezones_tasks',
'ProjectTask' => 'plugin_item_add_update_timezones_tasks',
'ChangeTask' => 'plugin_item_add_update_timezones_tasks',
'Config' => 'plugin_item_add_update_timezones_dbconnection'
];

$PLUGIN_HOOKS['item_add']['timezones']
= [ 'TicketTask' => 'plugin_item_add_update_timezones_tasks',
'ProblemTask' => 'plugin_item_add_update_timezones_tasks',
'ProjectTask' => 'plugin_item_add_update_timezones_tasks',
'ChangeTask' => 'plugin_item_add_update_timezones_tasks',
'Config' => 'plugin_item_add_update_timezones_dbconnection'
];

//$tz=(isset($_SESSION['glpitimezone'])?$_SESSION['glpitimezone']:date_default_timezone_get());
//$PLUGIN_HOOKS['add_javascript']['timezones'] = "js/tz.php?tz=$tz";
$PLUGIN_HOOKS['add_javascript']['timezones'] = "js/tz.js";

$PLUGIN_HOOKS['csrf_compliant']['timezones'] = true;

//$PLUGIN_HOOKS['use_massive_action']['timezones'] = 1;

}

/**
* Summary of plugin_version_timezones
* @return string[]
*/
function plugin_version_timezones() {

return ['name' => 'Timezones',
'version' => PLUGIN_TIMEZONES_VERSION,
'author' => 'Olivier Moron',
'license' => 'GPLv2+',
'homepage' => 'https://github.com/tomolimo/timezones',
'minGlpiVersion' => '9.3'];
}


/**
* Summary of plugin_timezones_check_prerequisites
* @return bool
*/
function plugin_timezones_check_prerequisites() {
global $DB, $LANG;

// Strict version check (could be less strict, or could allow various version)
if (version_compare(GLPI_VERSION, '9.3', 'lt')) {
echo $LANG['timezones']['glpiversion'];
return false;
}

// check if mySQL time_zones tables are empty
$query = "SELECT * FROM mysql.time_zone_name";
$res = $DB->query( $query );
if ($DB->numrows( $res ) == 0) {
echo $LANG['timezones']['timezonetables'];
return false;
}

return true;
}


/**
* Summary of plugin_timezones_check_config
* @param mixed $verbose
* @return bool
*/
function plugin_timezones_check_config($verbose = false) {
global $DB, $LANG;

$plug = new Plugin;
if ($plug->isActivated('timezones')) {
// check if all datetime fields of the glpi db have been converted to timestamp otherwise, timezone management can't be done correctly
$query = "SELECT DISTINCT( `INFORMATION_SCHEMA`.`COLUMNS`.`TABLE_NAME` ), TABLE_TYPE from `INFORMATION_SCHEMA`.`COLUMNS`
JOIN `INFORMATION_SCHEMA`.`TABLES` ON `INFORMATION_SCHEMA`.`TABLES`.`TABLE_NAME` = `INFORMATION_SCHEMA`.`COLUMNS`.`TABLE_NAME` AND `INFORMATION_SCHEMA`.`TABLES`.`TABLE_TYPE` = 'BASE TABLE'
WHERE `INFORMATION_SCHEMA`.`COLUMNS`.TABLE_SCHEMA = '".$DB->dbdefault."' AND `INFORMATION_SCHEMA`.`COLUMNS`.`COLUMN_TYPE` IN ('DATETIME') ; ";
$res = $DB->query( $query );
if ($DB->numrows( $res ) > 0) {
// will convert during GLPI execution any DATETIME fields that may have been added
include_once 'hook.php';
convertDB($verbose);
//if ($verbose) {
// echo $LANG['timezones']['dbnotconverted'];
//}
//return false;
}
}

return true;
}

<?php
/*
* -------------------------------------------------------------------------
Timezones management plugin
Copyright (C) 2015-2016-2017 by Raynet SAS a company of A.Raymond Network.
http://www.araymond.com
-------------------------------------------------------------------------
LICENSE
This file is part of Timezones management plugin for GLPI.
This file is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GLPI. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/

// ----------------------------------------------------------------------
// Original Author of file: Olivier Moron
// Purpose of file: to setup time zone management plugin to GLPI
// ----------------------------------------------------------------------
define ("PLUGIN_TIMEZONES_VERSION", "2.4.1");

/**
* Summary of plugin_init_timezones
*/
function plugin_init_timezones() {
global $PLUGIN_HOOKS,$CFG_GLPI;

Plugin::registerClass('PluginTimezonesUser',
['addtabon' => ['Preference', 'User']]);

// Init session
$PLUGIN_HOOKS['init_session']['timezones'] = 'plugin_init_session_timezones';

$PLUGIN_HOOKS['post_init']['timezones'] = 'plugin_timezones_postinit';

$PLUGIN_HOOKS['pre_item_update']['timezones']
= [ 'User' => [ 'PluginTimezonesUser', 'preItemUpdate']];

$PLUGIN_HOOKS['item_update']['timezones']
= [ 'TicketTask' => 'plugin_item_add_update_timezones_tasks',
'ProblemTask' => 'plugin_item_add_update_timezones_tasks',
'ProjectTask' => 'plugin_item_add_update_timezones_tasks',
'ChangeTask' => 'plugin_item_add_update_timezones_tasks',
'Config' => 'plugin_item_add_update_timezones_dbconnection'
];

$PLUGIN_HOOKS['item_add']['timezones']
= [ 'TicketTask' => 'plugin_item_add_update_timezones_tasks',
'ProblemTask' => 'plugin_item_add_update_timezones_tasks',
'ProjectTask' => 'plugin_item_add_update_timezones_tasks',
'ChangeTask' => 'plugin_item_add_update_timezones_tasks',
'Config' => 'plugin_item_add_update_timezones_dbconnection'
];

//$tz=(isset($_SESSION['glpitimezone'])?$_SESSION['glpitimezone']:date_default_timezone_get());
//$PLUGIN_HOOKS['add_javascript']['timezones'] = "js/tz.php?tz=$tz";
$PLUGIN_HOOKS['add_javascript']['timezones'] = "js/tz.js";

$PLUGIN_HOOKS['csrf_compliant']['timezones'] = true;

//$PLUGIN_HOOKS['use_massive_action']['timezones'] = 1;

}

/**
* Summary of plugin_version_timezones
* @return string[]
*/
function plugin_version_timezones() {

return ['name' => 'Timezones',
'version' => PLUGIN_TIMEZONES_VERSION,
'author' => 'Olivier Moron',
'license' => 'GPLv2+',
'homepage' => 'https://github.com/tomolimo/timezones',
'minGlpiVersion' => '9.3'];
}


/**
* Summary of plugin_timezones_check_prerequisites
* @return bool
*/
function plugin_timezones_check_prerequisites() {
global $DB, $LANG;

// Strict version check (could be less strict, or could allow various version)
if (version_compare(GLPI_VERSION, '9.3', 'lt')) {
echo $LANG['timezones']['glpiversion'];
return false;
}

// check if mySQL time_zones tables are empty
$query = "SELECT * FROM mysql.time_zone_name";
$res = $DB->query( $query );
if ($DB->numrows( $res ) == 0) {
echo $LANG['timezones']['timezonetables'];
return false;
}

return true;
}


/**
* Summary of plugin_timezones_check_config
* @param mixed $verbose
* @return bool
*/
function plugin_timezones_check_config($verbose = false) {
global $DB, $LANG;

$plug = new Plugin;
if ($plug->isActivated('timezones')) {
// check if all datetime fields of the glpi db have been converted to timestamp otherwise, timezone management can't be done correctly
$query = "SELECT DISTINCT( `INFORMATION_SCHEMA`.`COLUMNS`.`TABLE_NAME` ), TABLE_TYPE from `INFORMATION_SCHEMA`.`COLUMNS`
JOIN `INFORMATION_SCHEMA`.`TABLES` ON `INFORMATION_SCHEMA`.`TABLES`.`TABLE_NAME` = `INFORMATION_SCHEMA`.`COLUMNS`.`TABLE_NAME` AND `INFORMATION_SCHEMA`.`TABLES`.`TABLE_TYPE` = 'BASE TABLE'
WHERE `INFORMATION_SCHEMA`.`COLUMNS`.TABLE_SCHEMA = '".$DB->dbdefault."' AND `INFORMATION_SCHEMA`.`COLUMNS`.`COLUMN_TYPE` IN ('DATETIME') ; ";
$res = $DB->query( $query );
if ($DB->numrows( $res ) > 0) {
// will convert during GLPI execution any DATETIME fields that may have been added
include_once 'hook.php';
convertDB($verbose);
//if ($verbose) {
// echo $LANG['timezones']['dbnotconverted'];
//}
//return false;
}
}

return true;
}
Loading

0 comments on commit 06974ce

Please sign in to comment.