Jump to content

PHP Timing Issue???


tuxbuddy

Recommended Posts

 

We are using call tracking tool called HelpCORE.The Timing configured for that tool displays the 24hrs service but we have our office timing as 9AM to 5:30 PM.It was not an issue since the timing format may be UTC,GMT but when the SLA stuffs was studied it created problem as the SLA will get counted on holidays or after working hours too.

 

In Summary,the tool workd as 24/7 but  our requirement  is 5 days and timings are like this

 

9AM to 5.30 PM and We need to stop sla in defined holidays also.

 

I have included the reuired files which I think should be sufficient to track the issue.If yu want more structural file do let me know.

 

The files using are

 

incidents_escalate.php --> setting escalation time of incident

time.php ---> All time settings for the application

corepm.php  Main include for all files, handles setup and plugins

 

incidents_escalate.php

<?php

Purpose:
Implements functions that aid in the escalation of incidents


// This is needed because this file is called by timed_event_handler.php
if( ! isset( $coreapm ) )
{
if ( ! include( '../coreapm/coreapm.php' ) )
{
die( 'Cannot include CORE APM' );
} 
$GLOBALS['security']->secure();
} 

$GLOBALS['temp_db'] = new class_database;

/**
* Escalate an incident
*/
function incident_escalate( $incident_id, $force_escalation = false, $reason = -1 )
{
global $temp_db;

$temp_db->query( 'SELECT incidents.priorities_id AS prio_id,
priorities.name AS name,
incidents.date_reported AS date_reported,
incidents.date_escalated AS date_escalated,
incidents.owner_id AS owner,
incidents.sla_level_id AS sla_level_id,
organisation.sla_level_id AS organisation_sla_level_id,
buildings.sla_level_id AS building_sla_level_id,
departments.sla_level_id AS department_sla_level_id,
rooms.sla_level_id AS room_sla_level_id,
users.sla_level_id AS user_sla_level_id,
groups.sla_level_id AS group_sla_level_id,
hardware.sla_level_id AS hardware_sla_level_id
FROM incidents,
priorities
LEFT JOIN organisation ON ( organisation.id = incidents.organisation_id )
LEFT JOIN buildings ON ( buildings.id = incidents.building_id )
LEFT JOIN rooms ON ( rooms.id = incidents.rooms_id )
LEFT JOIN users ON ( users.id = incidents.owner_id )
LEFT JOIN groups ON ( groups.id = incidents.group_id )
LEFT JOIN hardware ON ( hardware.id = incidents.hardware )
LEFT JOIN departments ON ( departments.id = incidents.department_id )
WHERE incidents.priorities_id = priorities.id
AND incidents.id=' . $incident_id );

if( ! ( $incident_row = $temp_db->fetch_array() ) ) return false; 

$sla_level_id = '';
if( $incident_row['organisation_sla_level_id'] != '' ) $sla_level_id .= $incident_row['organisation_sla_level_id'] . ',';
if( $incident_row['building_sla_level_id'] != '' ) $sla_level_id .= $incident_row['building_sla_level_id'] . ',';
if( $incident_row['department_sla_level_id'] != '' ) $sla_level_id .= $incident_row['department_sla_level_id'] . ',';
if( $incident_row['room_sla_level_id'] != '' ) $sla_level_id .= $incident_row['room_sla_level_id'] . ',';
if( $incident_row['user_sla_level_id'] != '' ) $sla_level_id .= $incident_row['user_sla_level_id'] . ',';
if( $incident_row['group_sla_level_id'] != '' ) $sla_level_id .= $incident_row['group_sla_level_id'] . ',';
if( $incident_row['hardware_sla_level_id'] != '' ) $sla_level_id .= $incident_row['hardware_sla_level_id'] . ',';
if( $incident_row['sla_level_id'] != '' ) $sla_level_id .= $incident_row['sla_level_id'] . ',';
$sla_level_id = substr( $sla_level_id, 0, -1 );

// date escalted has higher precidence and if set use that as a the base time
// replace the date_reported with the escalation time
if ( is_valid_date( $incident_row['date_escalated'] ) && O_INC_USEESCALATION )
{
$incident_row['date_reported'] = $incident_row['date_escalated'];
} 

// Calc how many days from epoch this task is
// Calc how many seconds from the start of the day the task is
$ta = db_time_to_array( $incident_row['date_reported'] );
$epoch_incident = dmy_to_days_from_epoch( $ta[0], $ta[1], $ta[2] );

$secs_incident = ( $ta[3] * 3600 ) + ( $ta[4] * 60 ) + $ta[5]; 

if( $sla_level_id == '' ) {
return false;
}

$temp_db->query( 'SELECT s.name AS name,
s.id AS sla_id,
e.time AS time,
e.id AS esc_id,
e.escalate AS escalate,
e.time AS solve_time,
e.escalate_owner AS escalate_owner,
e.escalate_group AS escalate_group,
e.escalate_priority AS escalate_priority,
e.escalate_status AS escalate_status
FROM sla_levels as s, 
escalations as e 
WHERE e.sla_level_id = s.id 
AND s.id IN ( ' . $sla_level_id . ') 
AND e.priority_id=' . $incident_row['prio_id'] . '
HAVING e.time = min(e.time)' );

$row = $temp_db->fetch_array();
$hours_offset = $row['time'];

// Incident does not escalate
if( $hours_offset == '' ) {
return false; 
}

// Now calc the deadlines
$epoch_deadline = $epoch_incident + ( int )( $hours_offset / 24 );
$secs_deadline = $secs_incident + ( ( $hours_offset % 24 ) * 3600 ); 

// Fill in some values in the array that are from the incident_row
$row['date_reported'] = $incident_row['date_reported'];
$row['name'] = $incident_row['name'];
$row['owner'] = $incident_row['owner']; 

// Event can't escalate normally, we CAN force escalation tough
if( ( returned_database_boolean( $row['escalate'] ) == false ) &&
( $force_escalation == false ) )
{
return false;
} 

$epoch_now = dmy_to_days_from_epoch( date( 'd' ), date( 'm' ), date( 'Y' ) );
$secs_now = ( date( 'H' ) * 3600 ) + ( date( 'i' ) * 60 ) + date( 's' );

$ta = db_time_to_array( $row['date_reported'] );
$epoch_incident = dmy_to_days_from_epoch( $ta[0], $ta[1], $ta[2] );
$secs_incident = ( $ta[3] * 3600 ) + ( $ta[4] * 60 ) + $ta[5];

if( ( ( ( $epoch_deadline - $epoch_now ) * 86400 ) + ( $secs_deadline - $secs_now ) < 0 ) ||
( $force_escalation == true ) )
{ 
// We're escalating
$log = ''; 

// Update database and create log
if( $row['escalate_owner'] != '' )
{
$temp_db->query( 'UPDATE incidents SET owner_id=' . $row['escalate_owner'] . ' WHERE id=' . $incident_id );

$temp_db->query( 'SELECT firstname, middlename, surname FROM users WHERE id=' . $row['escalate_owner'] );
$temprow = $temp_db->fetch_array();
$log .= text( 'owner' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['firstname'] . ' ' . $temprow['middlename'] . ' ' . $temprow['surname'] . "\n";
} 

if( $row['escalate_group'] != '' )
{
$temp_db->query( 'UPDATE incidents SET group_id=' . $row['escalate_group'] . ' WHERE id=' . $incident_id );

$temp_db->query( 'SELECT name FROM groups WHERE id=' . $row['escalate_group'] );
$temprow = $temp_db->fetch_array();
$log .= text( 'group' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['name'] . "\n";
} 

if( $row['escalate_priority'] != '' )
{
$temp_db->query( 'UPDATE incidents SET priorities_id=' . $row['escalate_priority'] . ' WHERE id=' . $incident_id );

$temp_db->query( 'SELECT name FROM priorities WHERE id=' . $row['escalate_priority'] );
$temprow = $temp_db->fetch_array();
$log .= text( 'priority' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['name'] . "\n";
} 

if( $row['escalate_status'] != '' )
{
$temp_db->query( 'UPDATE incidents SET status_id=' . $row['escalate_status'] . ' WHERE id=' . $incident_id );

$temp_db->query( 'SELECT name FROM status WHERE id=' . $row['escalate_status'] );
$temprow = $temp_db->fetch_array();
$log .= text( 'status' ) . ' ' . text( 'escalated_to' ) . ' ' . $temprow['name'] . "\n";
} 

if( $log == '' )
{
$log = text( 'no_changes' );
} 

// There is a reason, alter the text
if( $reason > -1 )
{
if( $reason == 0 )
{
$log = text( 'no_reason' ) . '<br />' . $log;
} 
else
{
$temp_db->query( 'SELECT name FROM escalation_reasons WHERE id=' . $reason );
$temprow = $temp_db->fetch_array();
$log = text( 'reason' ) . ': ' . $temprow['name'] . '<br />' . $log;
} 
} 

$log = '<strong>' . text( 'incident_escalation_from' ) . ' ' . $row['name'] . "</strong>\n" . $log; 

// Add this to incident log
// Note: incident's old owner always escalates an incident
$temp_db->query( 'INSERT INTO incidents_log( incident_id, user_id, changed, log, sla_time ) 
values( ' . $incident_id . ', 
' . $row['owner'] . ', 
now(), 
\'' . stripslashes( $log ) . '\',
'.$hours_offset.' );' ); 

// Update incident and set the date_escalated to now
$temp_db->query( 'UPDATE incidents SET date_escalated=now() WHERE id=' . $incident_id ); 



// Email admin about it
if( O_INC_SENDEMAIL )
{
$GLOBALS['email']->email_user( $row['owner'],
'incident_escalated_owner',
array( 'id' => $incident_id ),
true );
} 

if( O_INC_ESCALATION_SENDADMINEMAIL ) {
$GLOBALS['email']->email_message( ADMIN_EMAIL,
'incident_escalated_admin',
array( 'id' => $incident_id ) ); 
}

return true;
} 

return false;
} 

?>

File :time.php

<?php

* Purpose:
* --------
* Date and Time related functions
* 

// Defines for easy handling of time
define( 'TIME_YEAR', 2 );
define( 'TIME_MONTH', 1 );
define( 'TIME_DAY', 0 );
define( 'TIME_HOUR', 3 );
define( 'TIME_MINUTE', 4 );
define( 'TIME_SECOND', 5 );


/**
* Return the current microtime
*/
function stopwatch_start()
{
list( $usec, $sec ) = explode( " ", microtime() );
return ( ( float )$usec + ( float )$sec );
} 

/**
* Return the time spent since the input time
*/
function stopwatch_stop( $starttime )
{
list( $usec, $sec ) = explode( " ", microtime() );
return ( ( ( float )$usec + ( float )$sec ) - $starttime );
} 

/*
* Db time to dfe
*/
function db_time_to_dfe( $db_time )
{
return dmy_array_to_days_from_epoch( db_time_to_array( $db_time ) );
} 

/**
* Calculates d m y to days from epoch
*/
function dmy_array_to_days_from_epoch( $array )
{
return dmy_to_days_from_epoch( $array[0], $array[1], $array[2] );
} 

/**
* calculates how long since the computer epoch we are
*/
function dmy_to_days_from_epoch( $day, $month, $year )
{
if( ($timestamp=@gmmktime (1, 0, 0, $month, $day, $year)) === -1 ) {
__FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Invalid timestamp( " D:'.$day.', M:'.$month.', Y:'.$year.' ) -> '.$timestamp.'" )' );
return 0;
}


return ($timestamp / 86400);
} 


/**
* Epoch to array
*/
function epoch_to_dmy( $epoch ) 
{
// We use day-epcohs
$epoch = $epoch * 86400;
return( array( gmdate('d', $epoch), gmdate('m', $epoch), gmdate('Y', $epoch), gmdate('H', $epoch), gmdate('i', $epoch), gmdate('s', $epoch) ) );
}


/**
* Calculates EXACT dmy
*/
function exact_epoch_to_dmy( $epoch ) 
{
return( array( gmdate('d', $epoch), gmdate('m', $epoch), gmdate('Y', $epoch), gmdate('H', $epoch), gmdate('i', $epoch), gmdate('s', $epoch) ) );
}

/**
* Calculates days to months and years
*/
function day_to_dmy_array( $days )
{
global $month_days;
$months = $month_days;

$year = 1970;
$month = 1; 

// Calc howmany years we have
while ( true )
{ 
// Count jaar (leapyear is counted later)
$days -= 365;
$year++; 
// Shrikkel
if ( ( $year % 4 == 0 ) && ( $year % 100 != 0 ) )
{
if ( $days < 366 )
{
// echo "+ Break on leap ($year)<br />";
break;
} 
$days--; 
// Normal
} 
else
{
if ( $days < 365 )
{
// echo "+ Normal break<br />";
break;
} 
} 
} 
// echo "v- After yearcount we have $days days left.<br />";
// Calc months
while( $days > $months[$month] )
{
$days -= $months[$month];
$month++;
} 

if( $days == 0 ) $days = 1; 
// echo "v- After monthcount we have $days days left.<br />";
if( $days < 10 ) $days = '0' . $days;
if( $month < 10 ) $month = '0' . $month; 
// echo "Days: $days Month: $month Year: $year<br />";
return array( $days, $month, $year, '00', '00', '00' );
} 

/*
* Integer to time
*/
function int_to_time( $time )
{
$days = '';
$mins = $time % 60;
$hours = ( int )( $time / 60 );

if( $mins < 10 )
{
$mins = '0' . $mins;
} 

$timestring = $hours . ':' . $mins;

return $timestring;
} 


/**
* Seconds to time string
*/
function seconds_to_time( $time )
{
$days = '';
$timestring = '';

$seconds = $time % 60;
$mins = ( ( int )($time/60) ) % 60;
$hours = ( int )( ($time/60) / 60 );

if( $mins < 10 )
{
$mins = '0' . $mins;
} 

if( $hours > 24 ) {
$timestring .= ((int) ($hours/24) ) .' '.text('days').' ';
$hours = $hours % 24;
}


$timestring .= $hours . ':' . $mins .':'.$seconds;

return $timestring;
} 


/**
* returns now
*/
function now()
{
return array_to_db_time( array( date( 'd' ), date( 'm' ), date( 'Y' ), date( 'H' ), date( 'i' ), date( 's' ) ) );
}

/**
* Find out in what week this date is
*/
function date_to_weeknr( $date ) 
{
if( ($timestamp=strtotime( $date ) ) === -1 ) {

__ERROR__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Invalid timestamp: strtotime("'.$date.'") => "'.$timestamp.'"' );
}

return date( 'W', $timestamp );
}

/**
* Find out on what day a week begins
* Note: we can't do leap years so we just guess if the user does not know if it's a leap year
*/
function weeknr_to_date( $weeknr, $year)
{
global $month_days;
$months = $month_days;

if( $weeknr > 53 ) __FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'weeknr_to_date(): week cannot be larger then 53.' );


// There is no week 53, it's week one of the next year
if( $weeknr == 53 ) {
return array_to_db_date( array( '01', '01', ($year+1) ) );
}

// No week zero and start counting at one
$weeknr_days = (($weeknr-1) * 7)+1;

// The second problem is finding out what day this year started. As not all years start at monday but all weeks DO !
$offset_day = date( 'w', mktime (0,0,0,1,1,$year) ) - 1; // Minus one because php starts at sunday and the world(c) at monday
//echo $offset_day.'<br />';

// Now fix the weernr days to start not at monday but at the actual day, this is probmeatic as we can go back in time
if( $offset_day >= $weeknr_days ) {

// Goes back a year
//echo "   weeknr_to_date($weeknr, $year): PREV YEAR EXECPT array_to_db_date( array( ( 31 - $offset_day ), '12', ($year-1) ) ): ".array_to_db_date( array( ( 31 - $offset_day ), '12', ($year-1) ) )."<br /><br />";

return array_to_db_date( array( ( 32 - $offset_day ), '12', ($year-1) ) ); // Dec has 32 days to compensate for 0-1
}

// Remains in this year
else {
$weeknr_days -= $offset_day;
}

//echo "   weeknr_to_date($weeknr, $year): weeknr_days: $weeknr_days<br />";

// Schrikkeljaar
if ( $year % 4 == 0 )
{
$months[02] = 29;
} 

$month = 1;
while( $weeknr_days >= $month_days[$month] ) {
//echo "   weeknr_to_date($weeknr, $year): while( $weeknr_days >= ".$month_days[$month]." )<br />";

$weeknr_days -= $month_days[$month];
$month++;
}


//echo "   weeknr_to_date($weeknr, $year): return array_to_db_date( array( $weeknr_days, $month, $year ) ): ".array_to_db_date( array( $weeknr_days, $month, $year ) )."<br /><br />";
if( $weeknr_days < 10 ) $weeknr_days = '0' . (int)$weeknr_days;
if( $month < 10 ) $month = '0' . (int)$month;

return array_to_db_date( array( $weeknr_days, $month, $year ) ); 
}

?>

 

 

Pls Help.

 

Link to comment
https://forums.phpfreaks.com/topic/100595-php-timing-issue/
Share on other sites

I Missed to include one file :

Here it is :

File : corepm.php

<?php


// Hack voor intern bij externe hosting
if(!isset($_SERVER['QUERY_STRING']))
$_SERVER['QUERY_STRING'] = '';

// only load once, the second time just give the coreapm object
if ( defined( 'COREAPM_LOADED' ) )
{
$coreapm = $GLOBALS['coreapm'];
return true;
} 
else
{
define( 'COREAPM_LOADED', true ); 

// Defines
define( 'PLUGIN_NAME', 0 );
define( 'PLUGIN_VERSION', 1 );
define( 'PLUGIN_URL', 2 );
define( 'PLUGIN_ACTIVE', 3 );
define( 'PLUGIN_NICE_NAME', 4 );

// ---- Vital requires, don't load without -----------------------------------------------------------------------

// Autogenerated always-perfect include path
if( ! @include_once( dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'application_config.php' ) ) {
die( 'Cannot load application_config.php');
}


// Determine OS
if( ( file_exists( 'c:\command.com' ) || file_exists( 'c:\autoexec.bat' ) || eregi( "windows", getenv( 'OS' ) ) ) && ( ! file_exists( '/etc/passwd' ) ) )
{
define( 'OS', 'WINDOWS' );
} 
else {
define( 'OS', 'UNIX' );
} 


// Load debug as soon as possible
if( DEBUG ) {
error_reporting(E_ALL);
ini_set( 'error_reporting', true );
ini_set( 'display_errors', true );
ini_set( 'display_startup_errors', true );
} 


// Built in check if the config is right
if ( ! defined( 'BASE_PATH' ) )
{
die( 'Config loaded, but expected defined variables not found! Loading the wrong config file. Load aborted' );
} 

if ( ! file_exists( BASE_PATH . COREAPM_LOCATION . 'includes/global_defines.php' ) )
{
die( 'Could not load global_defines from ("'.BASE_PATH . COREAPM_LOCATION . 'includes/global_defines.php")<br />. This means that your BASE_PATH("'.BASE_PATH.'") and COREAPM_LOCATION("'.COREAPM_LOCATION.'") are likely to be wrong' );
} 

// Include version
if( ! include_once( BASE_PATH . 'version.php' ) )
{
die( 'Cannot load version.php, no sense in loading CORE apm.' );
} 

// load some values that are not intended to be user-defineable
if ( ! include_once( BASE_PATH . COREAPM_LOCATION . 'includes/global_defines.php' ) )
{
die( 'Cannot load includes/global_defines.php, no sense in loading CORE apm. This holds needed defines.' );
} 

// ---- Load most-important requires -----------------------------------------------------------------------

//basic include functions needed for overall functionality
include_once( BASE_PATH . COREAPM_LOCATION . 'includes/time.php' );

if ( CLOCK_ME || SHOW_INFO ) {
$page_load_start_time = stopwatch_start();
}

// load text outputting and translation
require_once( BASE_PATH . COREAPM_LOCATION . 'includes/language.php' );

$language = new class_language;
$language->set( $language->selected_language );

if ( ! include_once( LANGUAGE_DIR . $language->selected_language . '.lng' ) )
{
die( 'language file could not be loaded !' );
} 

if( file_exists( LANGUAGE_DIR . $language->selected_language . '_ALTERED.lng' ) )
{
include_once( LANGUAGE_DIR . $language->selected_language . '_ALTERED.lng' );
} 
else
{
$_COREAPM_ALTERED_LANGUAGE = array();
} 

$month_names = array( 0 => '',
1 => text( 'jan' ),
2 => text( 'feb' ),
3 => text( 'mar' ),
4 => text( 'apr' ),
5 => text( 'may' ),
6 => text( 'jun' ),
7 => text( 'jul' ),
8 => text( 'aug' ),
9 => text( 'sep' ),
10 => text( 'okt' ),
11 => text( 'nov' ),
12 => text( 'dec' ) );

// load up email
require_once( BASE_PATH . COREAPM_LOCATION . 'includes/email.php' );
$email = new class_email;

// our own kind of syslog handles error-logging
require_once( BASE_PATH . COREAPM_LOCATION . 'includes/syslog.php' );
$syslog = new class_syslog;

// error handling
require_once( BASE_PATH . COREAPM_LOCATION . 'includes/error.php' );

// Now load the main gui classes
require_once( BASE_PATH . COREAPM_LOCATION . 'gui/screen.php' );
$screen = new class_screen;

require_once( BASE_PATH . COREAPM_LOCATION . 'gui/box.php' );
$box = new class_box;

require_once( BASE_PATH . COREAPM_LOCATION . 'gui/table.php' );
$table = new class_table;

require_once( BASE_PATH . COREAPM_LOCATION . 'gui/tree.php' );

require_once( BASE_PATH . COREAPM_LOCATION . 'gui/tabs.php' );
$tabs = new class_tabsheets;

require_once( BASE_PATH . COREAPM_LOCATION . 'gui/formtable.php' );
$form = new class_formtable;

require_once( BASE_PATH . COREAPM_LOCATION . 'gui/template.php' );
$template = new class_template;

require_once( BASE_PATH . COREAPM_LOCATION . 'gui/various.php' );

// load the database connection
require_once( BASE_PATH . COREAPM_LOCATION . 'database/db.php' );
$db = new class_database;

// load security
require_once( BASE_PATH . COREAPM_LOCATION . 'includes/security.php' );
$security = new class_security;


// caching
require_once( BASE_PATH . COREAPM_LOCATION . 'includes/cache.php' );
$cache = new class_cache;


require_once( BASE_PATH . COREAPM_LOCATION . 'includes/checks.php' );

require_once( BASE_PATH . COREAPM_LOCATION . 'includes/scripts.php' );

require_once( BASE_PATH . COREAPM_LOCATION . 'includes/binairy.php' );

require_once( BASE_PATH . COREAPM_LOCATION . 'forms/elements.php' );

require_once( BASE_PATH . COREAPM_LOCATION . 'forms/selectbuilder.php' ); 



/**
* This is our superclass. It is the most important class there is as it controls loads of functions that are important.
* Especially handy of you just want a start() and finish() on a page.
* 
* @author Dennis Fleurbaaij <[email protected]>
*/
class class_coreapm
{
var $instance_calls;
var $plugins;
var $plugin_menu_items;
var $plugin_quick_select_menu_items;
var $plugin_callbacks;

/**
* Constructor
* 
* @author Dennis Fleurbaaij <[email protected]>
*/
function class_coreapm()
{
$this->instance_calls = 0;
$this->plugins = array();
$this->plugin_menu_items = array();
$this->plugin_quick_select_menu_items = array();
$this->plugin_callbacks = array();
} 

/**
* Starts up everything, first secures you, checks for compression and starts page rendering
* 
* @author Dennis Fleurbaaij <[email protected]>
*/
function start()
{
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Starting' );

if( $this->instance_calls++ > 0 )
{ 
// echo( 'COREAPM ALLREADY STARTED CANNOT START TWICE, ADDING INSTANCE CALL AND COMING TO A TOTAL OF '.$this->instance_calls);
return true;
} 

// check if the browser is capable of gzip compression activate if possible
if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) && strstr( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip, deflate' ) )
{ 
//header ('Content-Encoding: gzip');
//ob_start('ob_gzhandler');
ob_start();
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Your browser is capable of gzip compression. We will compress the output before sending it.' );
} 
else
{
ob_start();
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Cannot handle compression. Sending clear text output.' );
} 

// If we need to export as CVS do not load top templates
if( isset( $_GET['csvexport'] ) )
{
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'cvsexport set, not creating top' );
return true;
} 

if( isset( $_GET['xmlexport'] ) )
{
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'xmlexport set, not creating top' );
return true;
}

// Can we load the page from cache ? Don't load on POSTS!
// 
// Dennis Fleurbaaij <[email protected]>, 1-1-2004:
// Page cache is disabled for a indefinate amount of time due to issues with showing the 
// wrong information.
// 
// if( sizeof( $_POST ) == 0 ) {
// 
// if( ( $data =& $GLOBALS['cache']->get_page_cache( $_SERVER['PHP_SELF'] ) ) != NULL ) {
// 
// echo $data;
// 
// if( DEBUG ) {
// $GLOBALS['box']->add( 'DEBUG BOX', $GLOBALS['syslog']->show_html_log() ); 
// }
// 
// if( CLOCK_ME ) {
// $load_time= stopwatch_stop( $GLOBALS['page_load_start_time'] );
// echo '<FONT color="#1F3985"><small><br />'.number_format($GLOBALS['syslog']->database_time, 5).' '.text('clock_me_bar1').' '.$GLOBALS['syslog']->database_calls.' '.text('clock_me_bar2').' '.number_format($load_time, 5).' '.text('clock_me_bar3').' '.number_format( ($GLOBALS['syslog']->database_time+$load_time), 5 ).' '.text('clock_me_bar4').' '.text('cache_hit')."</small></FONT>\n";
// }
// 
// $GLOBALS['screen']->create_bottom();
// die;
// }
// }

$GLOBALS['screen']->create_top(); 
// echo 'STARTING COREAPM ( this instance: '.$this->instance_calls.' (first has id 1!) )<br />';
} 

/**
* Stops rendering, emails admin if errors are found and cleans up
* 
* @author Dennis Fleurbaaij <[email protected]>
*/
function finish()
{
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Finishing' );


if( $this->instance_calls <= 0 ) {
die( 'COREAPM NOT YET STARTED CANNOT FINISH!' );
} 

// Deduct and see if we can finish
$this->instance_calls--;

if( $this->instance_calls >= 1 )
{ 
//echo 'COREAPM FINISH: Cannot finish, instance_calls >= 1 ( currently: '.$this->instance_calls.', tabs: '.$GLOBALS['tabs']->tabs.' )<br />';
return true;
} 

// Close db connection
$GLOBALS['db']->close(); 

// Csv rendering file, return file header and quit here
if( isset( $_GET['csvexport'] ) )
{
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'cvsexport set, rendering and returning file' ); 

// Send custom header
header( 'Content-type: text/comma-separated-values' );
header( 'Content-Disposition: attachment; filename=HelpCORE_report.csv' );
header( 'Content-length: ' . ob_get_length() );
header( 'Content-Description: HelpCORE Report' );
echo ob_get_clean();
die;
} 

// XML rendering file, return file header and quit here
if( isset( $_GET['xmlexport'] ) )
{
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'xmlexport set, rendering and returning file' ); 
$xml = '<?xml version="1.0" encoding="utf-8"?>';
// Get the name of the form to make some sort of relationship in the xml to the exported data
$file = explode('/', $_SERVER['PHP_SELF']);
$file = substr($file[count($file)-1],0,-4);

$xml .= "\r\n\t<".$file.">\r\n";
// put the content in an array
$content = explode('<br />', nl2br( ob_get_clean() ) );
// get the rough headers
$headers = explode(';', $content[0]);

// characters to be replaced in the header names
$replace_array = array( ' ',
'!',
'#',
'/',
'(',
')'
);
// the replacements
$replace_with_array = array( '_',
'',
'',
'_',
'',
''
);

// small check to make sure both arrays are equall in size
//if (count($replace_array) != count($replace_with_array))
// this will keep looping, lock up your webserver and eventually it will most likely also kill the mail server 
//__FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Replacements arrays are not equall in size' );

// initialze new header array and clean stuff up
$goodheaders = array();
foreach ($headers as $key => $header) {
if ($header != '') {
$header = str_replace( $replace_array, $replace_with_array, trim($header) );
$header = str_replace( '__', '_', $header );
if ($header != '') 
$goodheaders[] = $header;
else 
$goodheaders[] = 'field'.$key;
}
}
// get the data and turn it into xml
$headercount = count($goodheaders);
foreach ($content as $key => $value) {
if ($key != '0') {
$xml .= "\t\t<record>\r\n";
$values = explode(';', $value);
foreach ($values as $key2 => $value2) {
if (($key2 <= $headercount-1) && (count($values) >= $headercount))
$xml .= "\t\t\t<".$goodheaders[$key2].'>'.str_replace("\r\n", '', trim($value2)).'</'.$goodheaders[$key2].">\r\n";
}
$xml .= "\t\t</record>\r\n";
}
}
$xml .= "\t</".$file.">";
// Send custom header
header( 'Content-type: text/xml' );
header( 'Content-Disposition: attachment; filename=HelpCORE_export_'.$file.'.xml' );
header( 'Content-length: ' . ob_get_length() );
header( 'Content-Description: HelpCORE Report' );
// output the actuall XML
echo $xml;
die;
} 

// Page is now rendered, submit it to cache
// 
// Dennis Fleurbaaij <[email protected]>, 1-1-2004:
// Page cache is disabled for a indefinate amount of time due to issues with showing the 
// wrong information.
// 
// $GLOBALS['cache']->put_page_cache( $_SERVER['PHP_SELF']);

// Show the debug panel
if ( DEBUG )
{
$GLOBALS['tabs']->addDataPanel( 'Debug', $GLOBALS['box']->data( 'DEBUG BOX', $GLOBALS['syslog']->show_html_log() ) );
} 

// echo 'COREAPM FINISHING ( tabs ( amount: '.$GLOBALS['tabs']->tabs.' ) will render below, direct output will render above this text ) !<br />';
// if ( DEBUG ) {
// $content = '<ol><li>' . str_replace( "\n", "</li><li>\n", htmlspecialchars( ob_get_contents() . $GLOBALS['tabs']->show() ) ) . '</li></ol>';
// $content = str_replace( " ", ' ', $content );
// $GLOBALS['tabs']->addDataPanel( 'Source', $GLOBALS['box']->data( 'HTML SOURCE CODE', '<div align="left"><p>' . $content . '</p></div>' ) );
// }

// If there are tabs show them
echo $GLOBALS['tabs']->show();

// If we need to show timing information render it now
if ( CLOCK_ME )
{
$load_time = stopwatch_stop( $GLOBALS['page_load_start_time'] );
echo '<br /><span class="clockMe">' . number_format( $GLOBALS['syslog']->database_time, 5 ) . ' ' . text( 'clock_me_bar1' ) . ' ' . $GLOBALS['syslog']->database_calls . ' ' . text( 'clock_me_bar2' ) . ' ' . number_format( $load_time, 5 ) . ' ' . text( 'clock_me_bar3' ) . ' ' . number_format( ( $GLOBALS['syslog']->database_time + $load_time ), 5 ) . ' ' . text( 'clock_me_bar4' ) . ' ' . text( 'cache_miss' ) . "</span>\n";
} 
elseif ( SHOW_INFO )
{
$load_time = stopwatch_stop( $GLOBALS['page_load_start_time'] );
echo '<script type="text/javascript">'."\n".'window.status = "'. date( "D, d M Y" ) . ' | Version: ' . HELPCORE_VERSION . 
', Release: ' . HELPCORE_RELEASE_DATE .
' ( db-time: '. number_format( $GLOBALS['syslog']->database_time, 5 ) .
', db-calls: '. $GLOBALS['syslog']->database_calls .
', render-time: '. number_format( $load_time, 5 ) .
', size: '. number_format( ob_get_length() , 0 ) . 'b. )";'."\n".'</script>';
} 

ob_end_flush();
$GLOBALS['screen']->create_bottom();
} 


/**
* Register a plugin, return the plugin's id
*/
function plugin_register( $name, $nice_name, $version, $url, $active )
{
// Push
array_push( $this->plugins, array( PLUGIN_ACTIVE=>$active, PLUGIN_NICE_NAME=>$nice_name, PLUGIN_NAME=>$name, PLUGIN_URL=>$url, PLUGIN_VERSION=>$version ) );

// Return false if not active
if( ! $active ) {
return false;
}

// Return id if active
return sizeof( $this->plugins );
}

/**
* Add a plugin page to the menu
*/
function plugin_register_menu_page( $module_name, $parent, $menu_array )
{
// Create array if not yet exists
if( ! isset( $this->plugin_menu_items[ $module_name ] ) ) {
$this->plugin_menu_items[ $module_name ] = array(); 
}

// Create array if not yet exists
if( ! isset( $this->plugin_menu_items[ $module_name ][ $parent ] ) ) {
$this->plugin_menu_items[ $module_name ][ $parent ] = array(); 
} 

// Store it for use
$this->plugin_menu_items[ $module_name ][ $parent ] = array_merge( $this->plugin_menu_items[ $module_name ][ $parent ], $menu_array );
}

/**
* Add a plugin page to the quick select menu
*/
function plugin_register_quick_select_menu_page( $module_name, $html_code )
{
// Create array if not yet exists
if( ! isset( $this->plugin_quick_select_menu_items[ $module_name ] ) ) {
$this->plugin_quick_select_menu_items[ $module_name ] = ''; 
}

// Store it for use
$this->plugin_quick_select_menu_items[ $module_name ] .= $html_code;
}

/**
* Used by the menu for retrieving items to add, if there are none return an empty array
*/ 
function plugin_get_menu_pages( $module_name, $parent )
{
if( ! isset( $this->plugin_menu_items[ $module_name ][ $parent ] ) ) {
return array(); 
}

return $this->plugin_menu_items[ $module_name ][ $parent ];
}

/**
* Used by the quick menu for retrieving items to add, if there are none return an empty string
*/ 
function plugin_get_quick_select_menu_pages( $module_name )
{
if( ! isset( $this->plugin_quick_select_menu_items[ $module_name ] ) ) {
return ''; 
}

return $this->plugin_quick_select_menu_items[ $module_name ];
}

/**
* Regsiter with a relational (.php) callback function
*/
function plugin_register_relational_callback( $table, $action, $function )
{
if( ! isset( $this->plugin_callbacks[ $table ] ) ) {
$this->plugin_callbacks[ $table ] = array();
}

if( ! isset( $this->plugin_callbacks[ $table ][ $action ] ) ) {
$this->plugin_callbacks[ $table ][ $action ] = array();
}

array_push( $this->plugin_callbacks[ $table ][ $action ], $function );
}

/**
* Return the callbacks the relational function has to make
*/
function plugin_get_relational_callbacks( $table, $action )
{
if( ! isset( $this->plugin_callbacks[ $table ][ $action ] ) ) {
return array();
}

return $this->plugin_callbacks[ $table ][ $action ];
}

/**
* Return true if the plugin is loaded
*/
function plugin_loaded( $name ) 
{
foreach( $this->plugins as $plugin ) {
if( $plugin[ PLUGIN_NAME ] == $name ) {
return true; 
}
} 

return false;
}

function plugin_active( $name )
{ foreach( $this->plugins as $plugin ) {
if( $plugin[ PLUGIN_NAME ] == $name ) {
if ($plugin[ PLUGIN_ACTIVE ]) return TRUE;
}
} 
return false;
} 

} 
$coreapm = new class_coreapm; 

if ((!defined('REGISTRATION_SEND') || REGISTRATION_SEND == FALSE) && (strtolower(substr($_SERVER['PHP_SELF'],-22)) != 'registration/index.php')) {
header('location: '.BASE_URL.'registration/index.php?return='.urlencode(BASE_URL));
die();
} 

// Check if the application is allready configured
if( ( ( ! defined( 'CONFIGURED' ) ) || ( CONFIGURED !== true ) ) && ( strtolower(substr($_SERVER['PHP_SELF'],-16)) != 'configurator.php' ) && (strtolower(substr($_SERVER['PHP_SELF'],-22)) != 'registration/index.php') ) {

echo 'HelpCORE is not yet configured, please go to <a href="'.BASE_URL.'configurator.php">the configurator</a>';
die; 
}


// ---- Load user's module-specific config -----------------------------------------------------------------------
if ( ! defined( 'LOAD_NO_MODULES' ) )
{
if ( isset( $_GET['module_subdir'] ) )
{
$module_subdir = $_GET['module_subdir'];

if ( $module_subdir != BASE_PATH )
{
include_once( BASE_PATH . $module_subdir . '/config.php' );
} 
} 
} 


// To save us much work we will load the superclasses here. They are capable of managing all functions needed for a 
// relational database to work without the need for complex code. Keep in mind that these are HUGE-MASSIVE-GIAGANTIC in memory and cpu needs
if ( ! defined( 'LOAD_LIGHT' ) )
{
include_once( BASE_PATH . COREAPM_LOCATION . 'relational/relational.php' );
$relational = new class_relational;
} 

// Open handle
if( ( ! is_dir( PLUGINS_DIR ) ) || ( ( $dh = opendir(PLUGINS_DIR) ) == null ) ) {
__FATAL__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Could not open plugin directory: "'.PLUGINS_DIR.'"' ); 
}

// Loop and read
while( ($fileName = readdir($dh)) !== false ) {

if( ($fileName == '.') || ($fileName == '..') || (! is_dir( PLUGINS_DIR.$fileName )) ) {
continue; 
}

// Check if dir has the loadable plugin.php file
if( file_exists( PLUGINS_DIR . $fileName . DIRECTORY_SEPARATOR . 'plugin.php' ) ) {

$GLOBALS['current_plugin_active']=true;

// Do not load if disabled.dat
if( file_exists( PLUGINS_DIR . $fileName . DIRECTORY_SEPARATOR . 'disabled.dat' ) ) {
$GLOBALS['current_plugin_active']=false;
}

if( include_once( PLUGINS_DIR . $fileName . DIRECTORY_SEPARATOR . 'plugin.php' ) ) {
__DEBUG__( __FILE__, __LINE__, __CLASS__, __FUNCTION__, 'Loaded the "'.$fileName.'" plugin' ); 
}
}
}
closedir($dh);
} 

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/100595-php-timing-issue/#findComment-514470
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.