<?php
if (!defined("ROOT_PATH"))
{
	header("HTTP/1.1 403 Forbidden");
	exit;
}
class pjUtil extends pjToolkit
{
	static public function uuid()
	{
		return chr(rand(65,90)) . chr(rand(65,90)) . time();
	}
	
	static public function getReferer()
	{
		if (isset($_GET['_escaped_fragment_']))
		{
			if (isset($_SERVER['REDIRECT_URL']))
			{
				return $_SERVER['REDIRECT_URL'];
			}
		}
		
		if (isset($_SERVER['HTTP_REFERER']))
		{
			$pos = strpos($_SERVER['HTTP_REFERER'], "#");
			if ($pos !== FALSE)
			{
				// IE fix
				return substr($_SERVER['HTTP_REFERER'], 0, $pos);
			}
			return $_SERVER['HTTP_REFERER'];
		}
	}
	
	static public function getTimezone($offset)
	{
		$db = array(
			'-14400' => 'America/Porto_Acre',
			'-18000' => 'America/Porto_Acre',
			'-7200' => 'America/Goose_Bay',
			'-10800' => 'America/Halifax',
			'14400' => 'Asia/Baghdad',
			'-32400' => 'America/Anchorage',
			'-36000' => 'America/Anchorage',
			'-28800' => 'America/Anchorage',
			'21600' => 'Asia/Aqtobe',
			'18000' => 'Asia/Aqtobe',
			'25200' => 'Asia/Almaty',
			'10800' => 'Asia/Yerevan',
			'43200' => 'Asia/Anadyr',
			'46800' => 'Asia/Anadyr',
			'39600' => 'Asia/Anadyr',
			'0' => 'Atlantic/Azores',
			'-3600' => 'Atlantic/Azores',
			'7200' => 'Europe/London',
			'28800' => 'Asia/Brunei',
			'3600' => 'Europe/London',
			'-39600' => 'America/Adak',
			'32400' => 'Asia/Shanghai',
			'36000' => 'Asia/Choibalsan',
			'-21600' => 'America/Chicago',
			'-25200' => 'Chile/EasterIsland',
			'-43200' => 'Pacific/Kwajalein'
		);
		if (is_null($offset) && strlen($offset) === 0)
		{
			return $db;
		}
		return array_key_exists($offset, $db) ? $db[$offset] : false;
	}
	
	public static function printNotice($title, $body, $convert = true, $close = true, $autoClose = false)
	{
		?>
		<div class="notice-box">
			<span class="notice-info">&nbsp;</span>
			<?php
			if (!empty($title))
			{
				printf('<span class="block bold">%s</span>', $convert ? htmlspecialchars(stripslashes($title)) : stripslashes($title));
			}
			if (!empty($body))
			{
				printf('<span class="block">%s</span>', $convert ? htmlspecialchars(stripslashes($body)) : stripslashes($body));
			}
			if ($close)
			{
				?><a href="#" class="notice-close"></a><?php
			}
			?>
		</div>
		<?php
	}
	
	static public function textToHtml($content)
	{
		$content = preg_replace('/\r\n|\n/', '<br />', $content);
		return '<html><head><title></title></head><body>'.$content.'</body></html>';
	}
	
	static public function getPostMaxSize()
	{
		$post_max_size = ini_get('post_max_size');
		switch (substr($post_max_size, -1))
		{
			case 'G':
				$post_max_size = (int) $post_max_size * 1024 * 1024 * 1024;
				break;
			case 'M':
				$post_max_size = (int) $post_max_size * 1024 * 1024;
				break;
			case 'K':
				$post_max_size = (int) $post_max_size * 1024;
				break;
		}
		return $post_max_size;
	}
	
	static public function getWeekRange($date, $week_start)
	{
		$week_arr = array(
				0=>'sunday',
				1=>'monday',
				2=>'tuesday',
				3=>'wednesday',
				4=>'thursday',
				5=>'friday',
				6=>'saturday');
			
		$ts = strtotime($date);
		$start = (date('w', $ts) == 0) ? $ts : strtotime('last ' . $week_arr[$week_start], $ts);
		$week_start = ($week_start == 0 ? 6 : $week_start -1);
		return array(date('Y-m-d', $start), date('Y-m-d', strtotime('next ' . $week_arr[$week_start], $start)));
	}
	
	static public function getComingWhere($period, $week_start)
	{
		$where_str = '';
		switch ($period) {
			case 1:
				$where_str = "(CURDATE() = t2.date)";
				break;
				;
			case 2:
				$where_str = "(DATE(DATE_ADD(NOW(), INTERVAL 1 DAY)) = t2.date)";
				break;
				;
			case 3:
				list($start_week, $end_week) = pjUtil::getWeekRange(date('Y-m-d'), $week_start);
				$where_str = "(t2.date BETWEEN CURDATE() AND '$end_week')";
				break;
				;
			case 4:
				list($start_week, $end_week) = pjUtil::getWeekRange(date('Y-m-d', strtotime("+7 days")), $week_start);
				$where_str = "(t2.date BETWEEN '$start_week' AND '$end_week')";
				break;
				;
			case 5:
				$end_month = date('Y-m-t',strtotime('this month'));
				$where_str = "(t2.date BETWEEN CURDATE() AND '$end_month')";
				break;
				;
			case 6:
				$start_month = date("Y-m-d", mktime(0, 0, 0, date("m") + 1, 1, date("Y")));
				$end_month = date("Y-m-d", mktime(0, 0, 0, date("m") + 2, 0, date("Y")));
				$where_str = "(t2.date BETWEEN '$start_month' AND '$end_month')";
				break;
				;
		}
		return $where_str;
	}
	
	static public function getMadeWhere($period, $week_start)
	{
		$where_str = '';
		switch ($period) {
			case 1:
				$where_str = "(DATE(t1.created) = CURDATE() OR DATE(t1.modified) = CURDATE())";
				break;
				;
			case 2:
				$where_str = "(DATE(t1.created) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) OR DATE(t1.modified) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)))";
				break;
				;
			case 3:
				list($start_week, $end_week) = pjUtil::getWeekRange(date('Y-m-d'), $week_start);
				$where_str = "((DATE(t1.created) BETWEEN '$start_week' AND '$end_week') OR (DATE(t1.modified) BETWEEN '$start_week' AND '$end_week'))";
				break;
				;
			case 4:
				list($start_week, $end_week) = pjUtil::getWeekRange(date('Y-m-d', strtotime("-7 days")), $week_start);
				$where_str = "((DATE(t1.created) BETWEEN '$start_week' AND '$end_week') OR (DATE(t1.modified) BETWEEN '$start_week' AND '$end_week'))";
				break;
				;
			case 5:
				$start_month = date('Y-m-01',strtotime('this month'));
				$end_month = date('Y-m-t',strtotime('this month'));
				$where_str = "((DATE(t1.created) BETWEEN '$start_month' AND '$end_month') OR (DATE(t1.modified) BETWEEN '$start_month' AND '$end_month'))";
				break;
				;
			case 6:
				$start_month = date("Y-m-d", mktime(0, 0, 0, date("m")-1, 1, date("Y")));
				$end_month = date("Y-m-d", mktime(0, 0, 0, date("m"), 0, date("Y")));
				$where_str = "((DATE(t1.created) BETWEEN '$start_month' AND '$end_month') OR (DATE(t1.modified) BETWEEN '$start_month' AND '$end_month'))";
				break;
				;
		}
		return $where_str;
	}
	
	static public function getTimezoneName($timezone)
	{
		$offset = $timezone / 3600;
		$timezone_name = timezone_name_from_abbr(null, $offset * 3600, true);
		if($timezone_name === false)
		{
			$timezone_name = timezone_name_from_abbr(null, $offset * 3600, false);
		}
		if($offset == -12)
		{
			$timezone_name = 'Pacific/Wake';
		}
		return $timezone_name;
	}
	
	static public function sortArrayByArray(Array $array, Array $orderArray) {
		$ordered = array();
		foreach($orderArray as $key) 
		{
			if(array_key_exists($key,$array)) 
			{
				$ordered[$key] = $array[$key];
				unset($array[$key]);
			}
		}
		return $ordered + $array;
	}

	static public function convertTimezoneToDefault($datetime, $timezone, $options)
	{
		$date = new DateTime($datetime, new DateTimeZone($timezone));
		$date->setTimezone(new DateTimeZone(self::getTimezoneName($options['o_timezone'])));
		
		return $date->format('Y-m-d H:i:s');
	}

	static public function convertDefaultToTimezone($datetime, $timezone, $options)
	{
		$date = new DateTime($datetime, new DateTimeZone(self::getTimezoneName($options['o_timezone'])));
		$date->setTimezone(new DateTimeZone($timezone));

		return $date->format('Y-m-d H:i:s');
	}

	static public function convertTimezoneToTimezone($datetime, $timezone1, $timezone2)
	{
		$date = new DateTime($datetime, new DateTimeZone($timezone1));
		$date->setTimezone(new DateTimeZone($timezone2));

		return $date->format('Y-m-d H:i:s');
	}
	
	static public function getNumericCurrencyCode($currency) {
		$arr = array(
		    'AFA' => array('Afghan Afghani', '971'),
		    'AWG' => array('Aruban Florin', '533'),
		    'AUD' => array('Australian Dollars', '036'),
		    'ARS' => array('Argentine Pes', '032'),
		    'AZN' => array('Azerbaijanian Manat', '944'),
		    'BSD' => array('Bahamian Dollar', '044'),
		    'BDT' => array('Bangladeshi Taka', '050'),
		    'BBD' => array('Barbados Dollar', '052'),
		    'BYR' => array('Belarussian Rouble', '974'),
		    'BOB' => array('Bolivian Boliviano', '068'),
		    'BRL' => array('Brazilian Real', '986'),
		    'GBP' => array('British Pounds Sterling', '826'),
		    'BGN' => array('Bulgarian Lev', '975'),
		    'KHR' => array('Cambodia Riel', '116'),
		    'CAD' => array('Canadian Dollars', '124'),
		    'KYD' => array('Cayman Islands Dollar', '136'),
		    'CLP' => array('Chilean Peso', '152'),
		    'CNY' => array('Chinese Renminbi Yuan', '156'),
		    'COP' => array('Colombian Peso', '170'),
		    'CRC' => array('Costa Rican Colon', '188'),
		    'HRK' => array('Croatia Kuna', '191'),
		    'CPY' => array('Cypriot Pounds', '196'),
		    'CZK' => array('Czech Koruna', '203'),
		    'DKK' => array('Danish Krone', '208'),
		    'DOP' => array('Dominican Republic Peso', '214'),
		    'XCD' => array('East Caribbean Dollar', '951'),
		    'EGP' => array('Egyptian Pound', '818'),
		    'ERN' => array('Eritrean Nakfa', '232'),
		    'EEK' => array('Estonia Kroon', '233'),
		    'EUR' => array('Euro', '978'),
		    'GEL' => array('Georgian Lari', '981'),
		    'GHC' => array('Ghana Cedi', '288'),
		    'GIP' => array('Gibraltar Pound', '292'),
		    'GTQ' => array('Guatemala Quetzal', '320'),
		    'HNL' => array('Honduras Lempira', '340'),
		    'HKD' => array('Hong Kong Dollars', '344'),
		    'HUF' => array('Hungary Forint', '348'),
		    'ISK' => array('Icelandic Krona', '352'),
		    'INR' => array('Indian Rupee', '356'),
		    'IDR' => array('Indonesia Rupiah', '360'),
		    'ILS' => array('Israel Shekel', '376'),
		    'JMD' => array('Jamaican Dollar', '388'),
		    'JPY' => array('Japanese yen', '392'),
		    'KZT' => array('Kazakhstan Tenge', '368'),
		    'KES' => array('Kenyan Shilling', '404'),
		    'KWD' => array('Kuwaiti Dinar', '414'),
		    'LVL' => array('Latvia Lat', '428'),
		    'LBP' => array('Lebanese Pound', '422'),
		    'LTL' => array('Lithuania Litas', '440'),
		    'MOP' => array('Macau Pataca', '446'),
		    'MKD' => array('Macedonian Denar', '807'),
		    'MGA' => array('Malagascy Ariary', '969'),
		    'MYR' => array('Malaysian Ringgit', '458'),
		    'MTL' => array('Maltese Lira', '470'),
		    'BAM' => array('Marka', '977'),
		    'MUR' => array('Mauritius Rupee', '480'),
		    'MXN' => array('Mexican Pesos', '484'),
		    'MZM' => array('Mozambique Metical', '508'),
		    'NPR' => array('Nepalese Rupee', '524'),
		    'ANG' => array('Netherlands Antilles Guilder', '532'),
		    'TWD' => array('New Taiwanese Dollars', '901'),
		    'NZD' => array('New Zealand Dollars', '554'),
		    'NIO' => array('Nicaragua Cordoba', '558'),
		    'NGN' => array('Nigeria Naira', '566'),
		    'KPW' => array('North Korean Won', '408'),
		    'NOK' => array('Norwegian Krone', '578'),
		    'OMR' => array('Omani Riyal', '512'),
		    'PKR' => array('Pakistani Rupee', '586'),
		    'PYG' => array('Paraguay Guarani', '600'),
		    'PEN' => array('Peru New Sol', '604'),
		    'PHP' => array('Philippine Pesos', '608'),
		    'QAR' => array('Qatari Riyal', '634'),
		    'RON' => array('Romanian New Leu', '946'),
		    'RUB' => array('Russian Federation Ruble', '643'),
		    'SAR' => array('Saudi Riyal', '682'),
		    'CSD' => array('Serbian Dinar', '891'),
		    'SCR' => array('Seychelles Rupee', '690'),
		    'SGD' => array('Singapore Dollars', '702'),
		    'SKK' => array('Slovak Koruna', '703'),
		    'SIT' => array('Slovenia Tolar', '705'),
		    'ZAR' => array('South African Rand', '710'),
		    'KRW' => array('South Korean Won', '410'),
		    'LKR' => array('Sri Lankan Rupee', '144'),
		    'SRD' => array('Surinam Dollar', '968'),
		    'SEK' => array('Swedish Krona', '752'),
		    'CHF' => array('Swiss Francs', '756'),
		    'TZS' => array('Tanzanian Shilling', '834'),
		    'THB' => array('Thai Baht', '764'),
		    'TTD' => array('Trinidad and Tobago Dollar', '780'),
		    'TRY' => array('Turkish New Lira', '949'),
		    'AED' => array('UAE Dirham', '784'),
		    'USD' => array('US Dollars', '840'),
		    'UGX' => array('Ugandian Shilling', '800'),
		    'UAH' => array('Ukraine Hryvna', '980'),
		    'UYU' => array('Uruguayan Peso', '858'),
		    'UZS' => array('Uzbekistani Som', '860'),
		    'VEB' => array('Venezuela Bolivar', '862'),
		    'VND' => array('Vietnam Dong', '704'),
		    'AMK' => array('Zambian Kwacha', '894'),
		    'ZWD' => array('Zimbabwe Dollar', '716'),
		);
		return isset($arr[$currency]) ? $arr[$currency][1] : 978;
	}
}
?>