Rest.php 4.04 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
5
6
7
8
9
10
<?php

	namespace WP_Piwik\Request;

	class Rest extends \WP_Piwik\Request {
			
		protected function request($id) {
			$count = 0;
			$url = self::$settings->getGlobalOption('piwik_mode') == 'http'?
				self::$settings->getGlobalOption('piwik_url'):
lucha's avatar
lucha committed
11
				'https://'.self::$settings->getGlobalOption('piwik_user').'.innocraft.cloud/';
ale's avatar
ale committed
12
13
14
15
16
17
18
19
20
21
22
			$params = 'module=API&method=API.getBulkRequest&format=json';
			foreach (self::$requests as $requestID => $config) {
				if (!isset(self::$results[$requestID])) {
					$params .= '&urls['.$count.']='.urlencode($this->buildURL($config));
					$map[$count] = $requestID;
					$count++;
				}
			}
			$results = ((function_exists('curl_init') && ini_get('allow_url_fopen') && self::$settings->getGlobalOption('http_connection') == 'curl') || (function_exists('curl_init') && !ini_get('allow_url_fopen')))?$this->curl($id, $url, $params):$this->fopen($id, $url, $params);
			if (is_array($results))
				foreach ($results as $num => $result)
lucha's avatar
lucha committed
23
24
				    if (isset($map[$num]))
					    self::$results[$map[$num]] = $result;
ale's avatar
ale committed
25
26
27
28
29
30
31
32
33
		}
			
		private function curl($id, $url, $params) {
			if (self::$settings->getGlobalOption('http_method')=='post') {
				$c = curl_init($url);
				curl_setopt($c, CURLOPT_POST, 1);
				curl_setopt($c, CURLOPT_POSTFIELDS, $params.'&token_auth='.self::$settings->getGlobalOption('piwik_token'));
			} else $c = curl_init($url.'?'.$params.'&token_auth='.self::$settings->getGlobalOption('piwik_token'));
			curl_setopt($c, CURLOPT_SSL_VERIFYPEER, !self::$settings->getGlobalOption('disable_ssl_verify'));
lucha's avatar
lucha committed
34
			curl_setopt($c, CURLOPT_SSL_VERIFYHOST, !self::$settings->getGlobalOption('disable_ssl_verify_host')?2:0);
ale's avatar
ale committed
35
36
37
38
39
			curl_setopt($c, CURLOPT_USERAGENT, self::$settings->getGlobalOption('piwik_useragent')=='php'?ini_get('user_agent'):self::$settings->getGlobalOption('piwik_useragent_string'));
			curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($c, CURLOPT_HEADER, $GLOBALS ['wp-piwik_debug'] );
			curl_setopt($c, CURLOPT_TIMEOUT, self::$settings->getGlobalOption('connection_timeout'));
			$httpProxyClass = new \WP_HTTP_Proxy();
lucha's avatar
lucha committed
40
			if ($httpProxyClass->is_enabled() && $httpProxyClass->send_through_proxy($url)) {
ale's avatar
ale committed
41
42
43
44
45
46
				curl_setopt($c, CURLOPT_PROXY, $httpProxyClass->host());
				curl_setopt($c, CURLOPT_PROXYPORT, $httpProxyClass->port());
				if ($httpProxyClass->use_authentication())
					curl_setopt($c, CURLOPT_PROXYUSERPWD, $httpProxyClass->username().':'.$httpProxyClass->password());
			}
			$result = curl_exec($c);
lechuck's avatar
lechuck committed
47
			self::$lastError = curl_error($c);
ale's avatar
ale committed
48
49
50
51
52
53
54
55
56
57
58
59
			if ($GLOBALS ['wp-piwik_debug']) {
				$header_size = curl_getinfo($c, CURLINFO_HEADER_SIZE);
				$header = substr($result, 0, $header_size);
				$body = substr($result, $header_size);
				$result = $this->unserialize($body);
				self::$debug[$id] = array ( $header, $url.'?'.$params.'&token_auth=...' );
			} else $result = $this->unserialize($result);
			curl_close($c);
			return $result;
		}

		private function fopen($id, $url, $params) {
lucha's avatar
lucha committed
60
61
			$contextDefinition = array('http'=>array('timeout' => self::$settings->getGlobalOption('connection_timeout'), 'header' => "Content-type: application/x-www-form-urlencoded\r\n") );
			$contextDefinition['ssl'] = array();
lechuck's avatar
lechuck committed
62
			if (self::$settings->getGlobalOption('disable_ssl_verify'))
lucha's avatar
lucha committed
63
64
65
				$contextDefinition['ssl'] = array('allow_self_signed' => true, 'verify_peer' => false );
			if (self::$settings->getGlobalOption('disable_ssl_verify_host'))
				$contextDefinition['ssl']['verify_peer_name'] = false;
ale's avatar
ale committed
66
67
68
69
			if (self::$settings->getGlobalOption('http_method')=='post') {
				$fullUrl = $url;
				$contextDefinition['http']['method'] = 'POST';
				$contextDefinition['http']['content'] = $params.'&token_auth='.self::$settings->getGlobalOption('piwik_token');
lucha's avatar
lucha committed
70
			} else $fullUrl = $url.'?'.$params.'&token_auth='.self::$settings->getGlobalOption('piwik_token');
ale's avatar
ale committed
71
72
73
74
75
76
77
			$context = stream_context_create($contextDefinition);
			$result = $this->unserialize(@file_get_contents($fullUrl, false, $context));
			if ($GLOBALS ['wp-piwik_debug'])
				self::$debug[$id] = array ( get_headers($fullUrl, 1), $url.'?'.$params.'&token_auth=...' );
			return $result;
		}
	}