<?php
/**
 * ClickHeat - Clicks' heatmap
 *
 * @link http://www.labsmedia.com/clickheat/index.html
 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
 * @version $Id: ClickHeat.php 377 2008-03-14 22:36:46Z matt $
 *
 * @package Piwik_ClickHeat
 */

class Piwik_ClickHeat_Controller extends Piwik_Controller
{
	/** Local configuration file */
	static $conf = array();

	function init()
	{
		$__languages = array('bg', 'cz', 'de', 'en', 'es', 'fr', 'hu', 'id', 'it', 'ja', 'nl', 'pl', 'pt', 'ro', 'ru', 'sr', 'tr', 'uk', 'zh');

		if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] !== '')
		{
			$realPath = &$_SERVER['REQUEST_URI'];
		}
		elseif (isset($_SERVER['SCRIPT_NAME']) && $_SERVER['SCRIPT_NAME'] !== '')
		{
			$realPath = &$_SERVER['SCRIPT_NAME'];
		}
		else
		{
			exit(LANG_UNKNOWN_DIR);
		}

		/** First of all, check if we are inside Piwik */
		$dirName = dirname($realPath);
		if ($dirName === '/')
		{
			$dirName = '';
		}

		define('CLICKHEAT_PATH', $dirName.'/plugins/ClickHeat/libs/');
		define('CLICKHEAT_INDEX_PATH', 'index.php?module=ClickHeat&');
		define('CLICKHEAT_ROOT', PIWIK_INCLUDE_PATH.'/plugins/ClickHeat/libs/');
		define('CLICKHEAT_CONFIG', PIWIK_INCLUDE_PATH.'/config/clickheat.php');
		define('IS_PIWIK_MODULE', true);

		if (Zend_Registry::get('access')->isSuperUser())
		{
			define('CLICKHEAT_ADMIN', true);
		}
		else
		{
			define('CLICKHEAT_ADMIN', false);
		}

		define('CLICKHEAT_LANGUAGE', Piwik_Translate::getInstance()->getLanguageToLoad());
		include (CLICKHEAT_CONFIG);
		self::$conf = $clickheatConf;
		/** For use in external files */
		$GLOBALS['clickheatConf'] = &self::$conf;

		/** Specific definitions */
		self::$conf['__screenSizes'] = array(0 /** Must start with 0 */, 640, 800, 1024, 1280, 1440, 1600, 1800);
		self::$conf['__browsersList'] = array('all' => '', 'firefox' => 'Firefox', 'msie' => 'Internet Explorer', 'safari' => 'Safari', 'opera' => 'Opera', 'kmeleon' => 'K-meleon', 'unknown' => '');
	}

	/**
	 * Main method
	 */
	function view()
	{
		$this->init();

		/** List of available groups */
		$groups = array();
		$d = dir(self::$conf['logPath']);
		while (($dir = $d->read()) !== false)
		{
			if ($dir[0] === '.' || !is_dir($d->path.$dir)) continue;
			$pos = strpos($dir, ',');
			if ($pos !== false)
			{
				$site = substr($dir, 0, $pos);
			}
			else
			{
				$site = '';
			}
			if (!isset($groups[$site]))
			{
				$groups[$site] = array();
			}
			$groups[$site][] = '<option value="'.$dir.'">'.($pos === false ? $dir : substr($dir, $pos + 1)).'</option>';
		}
		$d->close();
		/** Sort groups in alphabetical order */
		ksort($groups);
		$__selectGroups = '';
		foreach ($groups as $key => $options)
		{
			sort($options);
			if ($key !== '')
			{
				$piwikSite = new Piwik_Site($key);
				$__selectGroups .= '<optgroup label="'.htmlentities($piwikSite->getName()).'">';
			}
			$__selectGroups .= implode("\n", $options);
			if ($key !== '')
			{
				$__selectGroups .= '</optgroup>';
			}
		}
		/** Screen sizes */
		$__selectScreens = '';
		for ($i = 0; $i < count(self::$conf['__screenSizes']); $i++)
		{
			$__selectScreens .= '<option value="'.self::$conf['__screenSizes'][$i].'">'.(self::$conf['__screenSizes'][$i] === 0 ? Piwik_Translate('LANG_ALL') : self::$conf['__screenSizes'][$i].'px').'</option>';
		}

		/** Browsers */
		$__selectBrowsers = '';
		foreach (self::$conf['__browsersList'] as $label => $name)
		{
			$__selectBrowsers .= '<option value="'.$label.'">'.($label === 'all' ? Piwik_Translate('LANG_ALL') : ($label === 'unknown' ? Piwik_Translate('LANG_UNKNOWN') : $name)).'</option>';
		}

		/** Date */
		$date = isset($_GET['date']) ? strtotime($_GET['date']) : (self::$conf['yesterday'] === true ? mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')) : false);
		if ($date === false)
		{
			$date = time();
		}
		$__day = (int) date('d', $date);
		$__month = (int) date('m', $date);
		$__year = (int) date('Y', $date);

		$view = new Piwik_View('ClickHeat/templates/view.tpl');

		$view->assign('clickheat_host', 'http://'.$_SERVER['SERVER_NAME']);
		$view->assign('clickheat_path', CLICKHEAT_PATH);
		$view->assign('clickheat_index', CLICKHEAT_INDEX_PATH);
		$view->assign('clickheat_group', Piwik_Translate('LANG_GROUP'));
		$view->assign('clickheat_groups', $__selectGroups);
		$view->assign('clickheat_browser', Piwik_Translate('LANG_BROWSER'));
		$view->assign('clickheat_browsers', $__selectBrowsers);
		$view->assign('clickheat_screen', Piwik_Translate('LANG_SCREENSIZE'));
		$view->assign('clickheat_screens', $__selectScreens);
		$view->assign('clickheat_heatmap', Piwik_Translate('LANG_HEATMAP'));
		$view->assign('clickheat_loading', str_replace('\'', '\\\'', Piwik_Translate('LANG_ERROR_LOADING')));
		$view->assign('clickheat_cleaner', str_replace('\'', '\\\'', Piwik_Translate('LANG_CLEANER_RUNNING')));
		$view->assign('clickheat_admincookie', str_replace('\'', '\\\'', Piwik_Translate('LANG_JAVASCRIPT_ADMIN_COOKIE')));
		$view->assign('clickheat_alpha', self::$conf['alpha']);
		$view->assign('clickheat_iframes', self::$conf['hideIframes'] === true ? 'true' : 'false');
		$view->assign('clickheat_flashes', self::$conf['hideFlashes'] === true ? 'true' : 'false');
		$view->assign('clickheat_force_heatmap', self::$conf['heatmap'] === true ? ' checked="checked"' : '');
		$view->assign('clickheat_jsokay', '<a href="#" onclick="showJsCode(); return false;">'.str_replace('\'', '\\\'', Piwik_Translate('LANG_ERROR_JAVASCRIPT')).'</a>');
		$view->assign('clickheat_day', $__day);
		$view->assign('clickheat_month', $__month);
		$view->assign('clickheat_year', $__year);
		$range = isset($_GET['period']) ? $_GET['period'][0] : 'd';
		if (!in_array($range, array('d', 'm', 'w')))
		{
			$range = 'd';
		}
		$view->assign('clickheat_range', $range);
		$view->assign('clickheat_menu', '<a href="#" onclick="adminCookie(); return false;">'.Piwik_Translate('LANG_LOG_MY_CLICKS').'</a><br /><a href="#" onclick="showJsCode(); return false;">Javascript</a>');

		echo $view->render();
	}

	function iframe()
	{
		$this->init();
		$group = isset($_GET['group']) ? str_replace('/', '', $_GET['group']) : '';
		if (is_dir(self::$conf['logPath'].$group))
		{
			$webPage = array('/');
			if (file_exists(self::$conf['logPath'].$group.'/url.txt'))
			{
				$f = @fopen(self::$conf['logPath'].$group.'/url.txt', 'r');
				if ($f !== false)
				{
					$webPage = explode('>', trim(fgets($f, 1024)));
					fclose($f);
				}
			}
			echo $webPage[0];
		}
	}

	function javascript()
	{
		$this->init();
		include (CLICKHEAT_ROOT.'javascript.php');
	}

	function layout()
	{
		$this->init();
		include (CLICKHEAT_ROOT.'layout.php');
	}

	function generate()
	{
		$this->init();
		include (CLICKHEAT_ROOT.'generate.php');
	}

	function png()
	{
		$this->init();
		$imagePath = self::$conf['cachePath'].(isset($_GET['file']) ? str_replace('/', '', $_GET['file']) : '**unknown**');

		header('Content-Type: image/png');
		if (file_exists($imagePath))
		{
			readfile($imagePath);
		}
		else
		{
			readfile(CLICKHEAT_ROOT.'images/warning.png');
		}
	}

	function layoutupdate()
	{
		$this->init();

		$group = isset($_GET['group']) ? str_replace('/', '', $_GET['group']) : '';
		$url = isset($_GET['url']) ? $_GET['url'] : '';
		if (strpos($url, 'http') !== 0)
		{
			$url = 'http://'.$_SERVER['SERVER_NAME'].'/'.ltrim($url, '/');
		}
		/** Improved security for PHP injection (PMV2.3b3 bug) */
		$url = parse_url(str_replace(array('<', '>'), array('', ''), $url));
		$left = isset($_GET['left']) ? (int) $_GET['left'] : 0;
		$center = isset($_GET['center']) ? (int) $_GET['center'] : 0;
		$right = isset($_GET['right']) ? (int) $_GET['right'] : 0;

		if (!is_dir(self::$conf['logPath'].$group) || !isset($url['host']) || !isset($url['path']))
		{
			exit('Error');
		}

		if ($url['scheme'] !== 'http' && $url['scheme'] !== 'https')
		{
			$url['scheme'] = 'http';
		}
		if (isset($url['query']))
		{
			$url = $url['scheme'].'://'.$url['host'].$url['path'].'?'.$url['query'];
		}
		else
		{
			$url = $url['scheme'].'://'.$url['host'].$url['path'];
		}
		$f = fopen(self::$conf['logPath'].$group.'/url.txt', 'w');
		fputs($f, $url.'>'.$left.'>'.$center.'>'.$right);
		fclose($f);

		exit('OK');
	}

	function cleaner()
	{
		$this->init();
		include (CLICKHEAT_ROOT.'cleaner.php');
	}
}
?>

