<?php

/**
* @version 2.8.17
* @package Joomla 2.5
* @subpackage DT Register
* @copyright Copyright (C) 2006 DTH Development
* @copyright contact dthdev@dthdevelopment.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

if (!defined('DTR_COM_COMPONENT')){

	define('DTR_COM_COMPONENT',"com_dtregister");

	define("DTR_COMPONENT",str_replace("com_","",'DT_COM_COMPONENT'));

}

JLoader::register('DtrModel' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtmodel.php');

JLoader::register('DtrModeladmin' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtmodeladmin.php');

JLoader::register('DtrController' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtcontroller.php');

JLoader::register('DtrControllerform' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtcontrollerform.php');

JLoader::register('DtrView' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtview.php');

JLoader::register('DtrTable' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dttable.php');

JLoader::register('DT_file' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtfile.php');

JLoader::register('DT_Session' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtsession.php');

JLoader::register('DT_Fee' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtfee.php');

JLoader::register('DT_Cart' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtcart.php');

JLoader::register('DTbarcode' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtbarcode.php');

JLoader::register('DTrCommon' ,JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtcommon.php');

JLoader::register('DT_Tagparser' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'tagparser.php');
JLoader::register('DtPagination' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtpagination.php');

JLoader::register('DtHtml' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'helpers'.DS.'html.php');

//$defaultimeZone = date_default_timezone_get();
function set_tz_by_offset($offset) {
	    global $defaultimeZone;
        $offset = $offset*60*60;
        $abbrarray = timezone_abbreviations_list();
        foreach ($abbrarray as $abbr) {

                foreach ($abbr as $city) {

                        if ($city['offset'] == $offset) { // remember to multiply $offset by -1 if you're getting it from js
                               date_default_timezone_set($city['timezone_id']);
                               return true;
                        }
                }
        }
    date_default_timezone_set($defaultimeZone);
    return false;
}
$conf =& JFactory::getConfig();
function sleep_till_session_written() {
	$conf =& JFactory::getConfig();
	$session = JFactory::getSession();
	if($conf->getValue('session_handler') != "database"){
	   $session->close();
	    return;
	}
	 $file = "components/com_dtregister/log.txt";
          $session_id = session_id();
	  $continue = true;
	   $db = JFactory::getDbo();
      $func_start_time = time();
	  $uuid = md5($func_start_time);
	  $session_encrypt = ini_get('suhosin.session.encrypt');
	  if($session_encrypt != "" && (strtolower($session_encrypt)=='on') || $session_encrypt == 1) {
	     $session_encrypt = true;
	  } else {
	     $session_encrypt = false;
	  }
      //return;
	  $i = 0;
//file_put_contents($file,var_export($uuid,true)." \n ",FILE_APPEND);
      $_SESSION['verify'] = $uuid;
	   $session->close();
	  do {
	    
		$query = "select * from #__session where session_id = '".$session_id."'";
		$db->setQuery($query);
		$results = $db->loadObject();
		if($session_encrypt) {
		  session_decode($results->data);
		  $results->data = serialize($_SESSION);
		
		}
		//file_put_contents($file,var_export($results->data,true)." \n ",FILE_APPEND);
		$condition = strpos($results->data,$uuid);
		//file_put_contents($file,var_export($condition,true)." \n ",FILE_APPEND);
		$i++ ;
		if(strpos($results->data,$uuid) !== false) {

		    $continue = false;
			
			//file_put_contents($file,"break loop"." \n ",FILE_APPEND);
			
			return;
			
	     } else {
		    // echo $i ."<br />".
			// ob_flush();
			usleep(10000);
			$session->close();
			//@session_write_close();
			
		 }
	  }while($continue || $i < 10);
	  
   }
if (!function_exists('pr')) {

function pr($data=array()){ //return;
/*
  	echo "<pre>";

	 $backtrace = debug_backtrace();

	 $index = 0;

	if(basename($backtrace[0]['file'])=="defines.php"){

	   $index = 1;	

	}

	echo "<br /><b>called in file ".$backtrace[$index]['file']."  line number ".$backtrace[$index]['line']."</b><br />";

	if(empty($data)){

	    var_dump($data);

	}else{

	  print_r($data);
	}

	echo "</pre>";
*/
}

}

if (!function_exists('prd')) {

function prd($data=null){ //return;
/*
  global $mainframe;
  pr($data);

  $mainframe->close();
*/
}

}

class Dtrdate extends JObject{
	
	/**
	 * Unix timestamp
	 *
	 * @var     int|boolean
	 * @access  protected
	 */
	var $_date = false;

	/**
	 * Time offset (in seconds)
	 *
	 * @var     string
	 * @access  protected
	 */
	var $_offset = 0;
	
	var $_tz = 0;

	/**
	 * Creates a new instance of JDate representing a given date.
	 *
	 * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
	 * If not specified, the current date and time is used.
	 *
	 * @param mixed $date optional the date this JDate will represent.
	 * @param int $tzOffset optional the timezone $date is from
	 */
	function __construct($date = 'now', $tzOffset = 0)
	{
		if ($date == 'now' || empty($date))
		{
			$this->_date = strtotime(gmdate("M d Y H:i:s", time()));
			return;
		}

		$tzOffset *= 3600;
		if (is_numeric($date))
		{
			$this->_date = $date - $tzOffset;
			return;
		}

		if (preg_match('~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~i',$date,$matches))
		{
			$months = Array(
				'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
				'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
				'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12
			);
			$matches[2] = strtolower($matches[2]);
			if (! isset($months[$matches[2]])) {
				return;
			}
			$this->_date = mktime(
				$matches[4], $matches[5], $matches[6],
				$months[$matches[2]], $matches[1], $matches[3]
			);
			if ($this->_date === false) {
				return;
			}

			if ($matches[7][0] == '+') {
				$tzOffset = 3600 * substr($matches[7], 1, 2)
					+ 60 * substr($matches[7], -2);
			} elseif ($matches[7][0] == '-') {
				$tzOffset = -3600 * substr($matches[7], 1, 2)
					- 60 * substr($matches[7], -2);
			} else {
				if (strlen($matches[7]) == 1) {
					$oneHour = 3600;
					$ord = ord($matches[7]);
					if ($ord < ord('M')) {
						$tzOffset = (ord('A') - $ord - 1) * $oneHour;
					} elseif ($ord >= ord('M') && $matches[7] != 'Z') {
						$tzOffset = ($ord - ord('M')) * $oneHour;
					} elseif ($matches[7] == 'Z') {
						$tzOffset = 0;
					}
				}
				switch ($matches[7]) {
					case 'UT':
					case 'GMT': $tzOffset = 0;
				}
			}
			$this->_date -= $tzOffset;
			return;
		}
		if (preg_match('~(\\d{4})-(\\d{2})-(\\d{2})[T\s](\\d{2}):(\\d{2}):(\\d{2})(.*)~', $date, $matches))
		{
			$this->_date = mktime(
				$matches[4], $matches[5], $matches[6],
				$matches[2], $matches[3], $matches[1]
			);
			if ($this->_date == false) {
				return;
			}
			if (isset($matches[7][0])) {
				if ($matches[7][0] == '+' || $matches[7][0] == '-') {
					$tzOffset = 60 * (
						substr($matches[7], 0, 3) * 60 + substr($matches[7], -2)
					);
				} elseif ($matches[7] == 'Z') {
					$tzOffset = 0;
				}
			}
			$this->_date -= $tzOffset;
			return;
		}
        $this->_date = (strtotime($date) == -1) ? false : strtotime($date);
		if ($this->_date) {
			$this->_date -= $tzOffset;
		}
	}

	/**
	 * Set the date offset (in hours)
	 *
	 * @access public
	 * @param float The offset in hours
	 */
	function setOffset($offset) {
		$this->_offset = 3600 * $offset;
	}

	/**
	 * Get the date offset (in hours)
	 *
	 * @access public
	 * @return integer
	 */
	function getOffset() {
		return ((float) $this->_offset) / 3600.0;
	}

	/**
	 * Gets the date as an RFC 822 date.
	 *
	 * @return a date in RFC 822 format
	 * @link http://www.ietf.org/rfc/rfc2822.txt?number=2822 IETF RFC 2822
	 * (replaces RFC 822)
	 */
	function toRFC822($local = false)
	{
		$date = ($local) ? $this->_date + $this->_offset : $this->_date;
		$date = ($this->_date !== false) ? date('D, d M Y H:i:s', $date).' +0000' : null;
		return $date;
	}

	/**
	 * Gets the date as an ISO 8601 date.
	 *
	 * @return a date in ISO 8601 (RFC 3339) format
	 * @link http://www.ietf.org/rfc/rfc3339.txt?number=3339 IETF RFC 3339
	 */
	function toISO8601($local = false)
	{
		$date   = ($local) ? $this->_date + $this->_offset : $this->_date;
		$offset = $this->getOffset();
        $offset = ($local && $this->_offset) ? sprintf("%+03d:%02d", $offset, abs(($offset-intval($offset))*60) ) : 'Z';
        $date   = ($this->_date !== false) ? date('Y-m-d\TH:i:s', $date).$offset : null;
		return $date;
	}

	/**
	 * Gets the date as in MySQL datetime format
	 *
	 * @return a date in MySQL datetime format
	 * @link http://dev.mysql.com/doc/refman/4.1/en/datetime.html MySQL DATETIME
	 * format
	 */
	function toMySQL($local = false)
	{
		$date = ($local) ? $this->_date + $this->_offset : $this->_date;
		$date = ($this->_date !== false) ? date('Y-m-d H:i:s', $date) : null;
		return $date;
	}

	/**
	 * Gets the date as UNIX time stamp.
	 *
	 * @return a date as a unix time stamp
	 */
	function toUnix($local = false)
	{
		$date = null;
		if ($this->_date !== false) {
			$date = ($local) ? $this->_date + $this->_offset : $this->_date;
		}
		return $date;
	}

	/**
	 * Gets the date in a specific format
	 *
	 * Returns a string formatted according to the given format. Month and weekday names and
	 * other language dependent strings respect the current locale
	 *
	 * @param string $format  The date format specification string (see {@link PHP_MANUAL#strftime})
	 * @return a date in a specific format
	 */
	function toFormat($format = '%Y-%m-%d %H:%M:%S')
	{
		$date = ($this->_date !== false) ? $this->_strftime($format, $this->_date + $this->_offset) : null;

		return $date;
	}

	/**
	 * Translates needed strings in for JDate::toFormat (see {@link PHP_MANUAL#strftime})
	 *
	 * @access protected
	 * @param string $format The date format specification string (see {@link PHP_MANUAL#strftime})
	 * @param int $time Unix timestamp
	 * @return string a date in the specified format
	 */
	function _strftime($format, $time)
	{
		if(strpos($format, '%a') !== false)
			$format = str_replace('%a', $this->_dayToString(date('w', $time), true), $format);
		if(strpos($format, '%A') !== false)
			$format = str_replace('%A', $this->_dayToString(date('w', $time)), $format);
		if(strpos($format, '%b') !== false)
			$format = str_replace('%b', $this->_monthToString(date('n', $time), true), $format);
		if(strpos($format, '%B') !== false)
			$format = str_replace('%B', $this->_monthToString(date('n', $time)), $format);
		$date = strftime($format, $time);
		return $date;
	}

	/**
	 * Translates month number to string
	 *
	 * @access protected
	 * @param int $month The numeric month of the year
	 * @param bool $abbr Return the abreviated month string?
	 * @return string month string
	 */
	function _monthToString($month, $abbr = false)
	{
		switch ($month)
		{
			case 1:  return $abbr ? JText::_('JANUARY_SHORT')   : JText::_('JANUARY');
			case 2:  return $abbr ? JText::_('FEBRUARY_SHORT')  : JText::_('FEBRUARY');
			case 3:  return $abbr ? JText::_('MARCH_SHORT')     : JText::_('MARCH');
			case 4:  return $abbr ? JText::_('APRIL_SHORT')     : JText::_('APRIL');
			case 5:  return $abbr ? JText::_('MAY_SHORT')       : JText::_('MAY');
			case 6:  return $abbr ? JText::_('JUNE_SHORT')      : JText::_('JUNE');
			case 7:  return $abbr ? JText::_('JULY_SHORT')      : JText::_('JULY');
			case 8:  return $abbr ? JText::_('AUGUST_SHORT')    : JText::_('AUGUST');
			case 9:  return $abbr ? JText::_('SEPTEMBER_SHORT')  : JText::_('SEPTEMBER');
			case 10: return $abbr ? JText::_('OCTOBER_SHORT')   : JText::_('OCTOBER');
			case 11: return $abbr ? JText::_('NOVEMBER_SHORT')  : JText::_('NOVEMBER');
			case 12: return $abbr ? JText::_('DECEMBER_SHORT')  : JText::_('DECEMBER');
		}
	}

	/**
	 * Translates day of week number to string
	 *
	 * @access protected
	 * @param int $day The numeric day of the week
	 * @param bool $abbr Return the abreviated day string?
	 * @return string day string
	 */
	function _dayToString($day, $abbr = false)
	{
		switch ($day)
		{
			case 0: return $abbr ? JText::_('SUN') : JText::_('SUNDAY');
			case 1: return $abbr ? JText::_('MON') : JText::_('MONDAY');
			case 2: return $abbr ? JText::_('TUE') : JText::_('TUESDAY');
			case 3: return $abbr ? JText::_('WED') : JText::_('WEDNESDAY');
			case 4: return $abbr ? JText::_('THU') : JText::_('THURSDAY');
			case 5: return $abbr ? JText::_('FRI') : JText::_('FRIDAY');
			case 6: return $abbr ? JText::_('SAT') : JText::_('SATURDAY');
		}
	}

}
global $DT_config,$month_arr,$now,$xhtml_url,$amp,$sign_up_redirect;
jimport( 'joomla.application.module.helper' );

jimport('joomla.utilities.date');

$now = new Dtrdate('now');

$timezone = new DateTimeZone($conf->getValue('config.offset'));
$offset   = $timezone->getOffset(new DateTime);

if(date('I',$now->toUnix(true))=='1') {
	$offset = $offset/3600;
} else {
	$offset = $offset/3600;
}

$now->setoffset($offset);
$lang =& JFactory::getLanguage();
$lang->load('com_dtregister');
$month_arr = array (

    '01'=>JText::_( 'DT_JAN'),

    '02'=>JText::_( 'DT_FEB'),

	'03'=>JText::_( 'DT_MAR'),

    '04'=>JText::_( 'DT_APR'),

	'05'=>JText::_( 'DT_MAY'),

	'06'=>JText::_( 'DT_JUN'),

	'07'=>JText::_( 'DT_JUL'),

	'08'=>JText::_( 'DT_AUG'),

	'09'=>JText::_( 'DT_SEP'),

	'10'=>JText::_( 'DT_OCT'),

	'11'=>JText::_( 'DT_NOV'),

	'12'=>JText::_( 'DT_DEC'),

);

$xhtml_url = false;

if($xhtml_url){

   $amp = "&amp;";

}else{

   $amp = "&";

}

function Comp($Num1,$Num2,$Scale=null) {
  // check if they're valid positive numbers, extract the whole numbers and decimals
  if(!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num1,$Tmp1)||
     !preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');

  // remove leading zeroes from whole numbers
  $Num1=ltrim($Tmp1[1],'0');
  $Num2=ltrim($Tmp2[1],'0');

  // first, we can just check the lengths of the numbers, this can help save processing time
  // if $Num1 is longer than $Num2, return 1.. vice versa with the next step.
  if(strlen($Num1)>strlen($Num2)) return(1);
  else {
    if(strlen($Num1)<strlen($Num2)) return(-1);

    // if the two numbers are of equal length, we check digit-by-digit
    else {

      // remove ending zeroes from decimals and remove point
      $Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):'';
      $Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):'';

      // if the user defined $Scale, then make sure we use that only
      if($Scale!=null) {
        $Dec1=substr($Dec1,0,$Scale);
        $Dec2=substr($Dec2,0,$Scale);
      }

      // calculate the longest length of decimals
      $DLen=max(strlen($Dec1),strlen($Dec2));

      // append the padded decimals onto the end of the whole numbers
      $Num1.=str_pad($Dec1,$DLen,'0');
      $Num2.=str_pad($Dec2,$DLen,'0');

      // check digit-by-digit, if they have a difference, return 1 or -1 (greater/lower than)
      for($i=0;$i<strlen($Num1);$i++) {
        if((int)$Num1{$i}>(int)$Num2{$i}) return(1);
        else
          if((int)$Num1{$i}<(int)$Num2{$i}) return(-1);
      }
	  
      // if the two numbers have no difference (they're the same).. return 0
      return(0);
    }
  }
}

function db(){
/*
  echo "<pre>";

	 $backtrace = debug_backtrace();

	 $index = 0;

	if(basename($backtrace[0]['file'])=="defines.php"){

	   $index = 1;	

	}

	echo "<br /><b>called in file ".$backtrace[$index]['file']."  line number ".$backtrace[$index]['line']."</b><br />";

  echo "<pre>";

   debug_print_backtrace();

   echo "</pre>";
*/
}

//pr($now->toMySQL(true));
//pr(strftime('%Y-%m-%d %H:%M:%S %Z%n'));

function my_array_filter_fn($val) {
  $val = trim($val);
  $allowed_vals = array("0"); // Add here your valid values
  return in_array($val, $allowed_vals, true) ? true : ( $val ? true : false );
}
//pr($now->toMySQL(true));
//pr(strftime('%Y-%m-%d %H:%M:%S %Z%n'));

if (!function_exists('sys_get_temp_dir')) {
    function sys_get_temp_dir() {
        // check environment variables.
        foreach (array('TMP', 'TEMP', 'TMPDIR') as $env_var) {
            if ($temp = getenv($env_var)) {
                return $temp;
            }
        }
        // test for a temp directory by having PHP create a temporary file.
        $temp = tempnam(__FILE__, '');
        if (file_exists($temp)) {
            unlink($temp);
            return dirname($temp);
        }
        // couldn't find a temp directory.
        return null;
    }
}
global $mainframe,$Itemid,$option;
$mainframe = JFactory::getApplication();
$Itemid = JRequest::getInt('Itemid');
//pr($user->authorisedLevels());
//pr($user->get('aid'));
$lang = &JFactory::getLanguage();
$lang->load('joomla', JPath::clean(JPATH_ADMINISTRATOR), null, false, false);
$lang->load('com_media', JPath::clean(JPATH_ADMINISTRATOR), null, false, false);

$option = "com_dtregister";
function intersects($r1start, $r1end, $r2start, $r2end)
{
    return ($r1start == $r2start) || ($r1start > $r2start ? $r1start <= $r2end : $r2start <= $r1end);
}



?>