Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Controller.php « Proxy « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 709538c83d91aa080a62abd38a1f588b04bd8a38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 * @version $Id$
 * 
 * @category Piwik_Plugins
 * @package Piwik_Proxy
 */

/**
 * Controller for proxy services
 *
 * @package Piwik_Proxy
 */
class Piwik_Proxy_Controller extends Piwik_Controller
{	
	const TRANSPARENT_PNG_PIXEL = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=';

	/**
	 * Display the "Export Image" window.
	 *
	 * @param string $imageData Base-64 encoded image data (via $_POST)
	 */
	static public function exportImageWindow()
	{
		Piwik::checkUserHasSomeViewAccess();

		$view = Piwik_View::factory('exportImage');
		$view->imageData = 'data:image/png;base64,'. Piwik_Common::getRequestVar('imageData', self::TRANSPARENT_PNG_PIXEL, 'string', $_POST);
		echo $view->render();
	}
	
	function exportImage()
	{
		self::exportImageWindow();
	}

	/**
	 * Output binary image from base-64 encoded data.
	 *
	 * @param string $imageData Base-64 encoded image data (via $_POST)
	 */
	static public function outputBinaryImage()
	{
		Piwik::checkUserHasSomeViewAccess();

		header('Content-Type: image/png');
		$data = base64_decode(Piwik_Common::getRequestVar('imageData', self::TRANSPARENT_PNG_PIXEL, 'string', $_POST));

		if(function_exists('imagecreatefromstring'))
		{
			// validate image data
			$imgResource = imagecreatefromstring($data);
			if($imgResource !== false)
			{
				// output image and clean-up
				imagepng($imgResource);
				imagedestroy($imgResource);
			}
		}
		else
		{
			echo $data;
		}
		exit;
	}

	function outputImage()
	{
		self::outputBinaryImage();
	}

	/**
	 * Output the merged CSS file.
	 * This method is called when the asset manager is enabled.
	 * 
	 * @see core/AssetManager.php
	 */
	public function getCss ()
	{
		$cssMergedFile = Piwik_AssetManager::getMergedCssFileLocation();
		Piwik::serveStaticFile($cssMergedFile, "text/css");
	}
	
	/**
	 * Output the merged JavaScript file.
	 * This method is called when the asset manager is enabled.
	 * 
	 * @see core/AssetManager.php
	 */
	public function getJs ()
	{
		$jsMergedFile = Piwik_AssetManager::getMergedJsFileLocation();
		Piwik::serveStaticFile($jsMergedFile, "application/javascript; charset=UTF-8");
	}

	/**
	 * Output the CSS3PIE PIE.htc file
	 *
	 * @see /libs/CSS3PIE
	 */
	public function getPieHtc ()
	{
		Piwik::serveStaticFile(PIWIK_INCLUDE_PATH ."/libs/CSS3PIE/PIE.htc", "text/x-component");
	}	

	/**
	 * Output redirection page instead of linking directly to avoid
	 * exposing the referer on the Piwik demo.
	 *
	 * @param string $url (via $_GET)
	 */
	public function redirect()
	{
		// validate referer
		$referer = Piwik_Url::getReferer();
		if(!empty($referer) && (Piwik_Url::getLocalReferer() === false))
		{
			exit;
		}

		$url = Piwik_Common::getRequestVar('url', '', 'string', $_GET);

		// mask visits to *.piwik.org
		if(self::isPiwikUrl($url))
		{
			echo
'<html><head>
<meta http-equiv="refresh" content="0;url=' . $url . '" />
</head></html>';
		}

		// standard redirect for other whitelisted URLs
		if(self::isAcceptableRemoteUrl($url))
		{
			Piwik_Url::redirectToUrl($url);
			exit;
		}
	}

	/**
	 * Validate URL against *.piwik.org domains
	 *
	 * @param string $url
	 * @return bool True if valid; false otherwise
	 */
	static public function isPiwikUrl($url)
	{
		if(preg_match('~^http://(qa\.|demo\.|dev\.|forum\.)?piwik.org([#?/]|$)~', $url))
		{
			return true;
		}
		return false;
	}

	/**
	 * Validate URL against a whitelist, so action=redirect can't be
	 * used as an open redirect proxy.
	 *
	 * @param string $url
	 * @return bool True if valid; false otherwise
	 */
	static public function isAcceptableRemoteUrl($url)
	{
		$homepageUrls = array();
		$listPlugins = Piwik_PluginsManager::getInstance()->readPluginsDirectory();

		foreach($listPlugins as $pluginName)
		{
			$oPlugin = Piwik_PluginsManager::getInstance()->loadPlugin($pluginName);
			$info = $oPlugin->getInformation();
			if((isset($info['homepage']) && $url == $info['homepage'])
				|| (isset($info['author_homepage']) && $url == $info['author_homepage'])
				|| (isset($info['license_homepage']) && $url == $info['license_homepage']))
			{
				return true;
			}
		}

		return false;
	}
}