Project

General

Profile

1
<?php
2

    
3
defined('_JEXEC') or die('Access denied');
4

    
5
jimport('joomla.application.component.helper');
6
jimport('joomla.application.component.model');
7
jimport('joomla.log.log');
8

    
9
// This model creates redirection URLs.
10
class OpenAireModelRedirect extends JModelItem {
11
	const LOG = 'openaire';
12
	
13
	private $invenioUrl;
14
	private $invenioPassword;
15
	private $validatorUrl;
16
	private $validatorPassword;
17
	
18
	// Construct a new OpenAireModelInvenio.
19
	// $configuration the configuration to use
20
	public function __construct($configuration) {
21
		parent::__construct($configuration);
22
		$parameters = JComponentHelper :: getParams('com_openaire');
23
		$this -> invenioUrl = $parameters -> get('invenioUrl');
24
		$this -> invenioPassword = $parameters -> get('invenioPassword');
25
		$this -> validatorUrl = $parameters -> get('validatorUrl');
26
		$this -> validatorPassword = $parameters -> get('validatorPassword');
27
	}
28

    
29
	// Create an Invenio redirect URL. Since an Invenio redirect URL involves a timeout, it can not be cached.
30
	// $user the user to create Invenio redirect URL for
31
	// return the Invenio redirect URL or NULL if any errors occur
32
	public function createInvenioRedirectUrl($user) {
33
		try {
34
			$userData = array(
35
				'email' => $user -> email,
36
				'fullname' => $user -> name,
37
				'nickname' => $user -> username,
38
				'id' => $user -> id,
39
				'__timeout__' => time() + 60 * 60,
40
				'__userip__' => $this -> getUserIp()
41
			);
42
			$assertion = json_encode($userData);
43
			$digest = hash_hmac('sha1', $assertion, $this -> invenioPassword);
44
			$redirectUrl = $this -> invenioUrl . 'youraccount/robotlogin?robot=OpenAIRE_Portal&login_method=OpenAIRE&assertion=' . urlencode($assertion) . '&digest=' . urlencode($digest) . '&referer=' . urlencode($this -> invenioUrl . 'deposit?style=portal&ln=en');
45
			JLog :: add('Created Invenio redirect URL ' . $redirectUrl . ' (user: ' . $user -> username . ')', JLog :: INFO, self :: LOG);
46
			return $redirectUrl;
47
		} catch (Exception $e) {
48
			JLog :: add('Error creating Invenio redirect URL (user: ' . $user -> username . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
49
			return NULL;
50
		}
51
	}
52
	
53
	// Create a validator redirect URL. Since a validator redirect URL involves a timeout, it can not be cached.
54
	// $user the user to create validator redirect URL for
55
	// return the validator redirect URL or NULL if any errors occur
56
	public function createValidatorRedirectUrl($user) {
57
		try {
58
			$redirectUrl = $this -> validatorUrl;
59
			if (!$user -> guest) {
60
				$username  = $user -> username;	
61
				$ip = $this -> getUserIp();
62
				$valid = (time() + 60 * 60) * 1000;
63
				$signature = hash_hmac('sha1', $username . $ip . $valid, $this -> validatorPassword);
64
				$redirectUrl .= 'portalLogin.action?user=' . urlencode($username) . '&valid=' . urlencode($valid) . '&ip=' . urlencode($ip) . '&signature=' . base64_encode($signature);
65
			}
66
			JLog :: add('Created validator redirect URL ' . $redirectUrl . ' (user: ' . ($user -> guest ? 'guest' : $user -> username) . ')', JLog :: INFO, self :: LOG);
67
			return $redirectUrl;
68
		} catch (Exception $e) {
69
			JLog :: add('Error creating validator redirect URL (user: ' . $user -> username . ', referer: ' . $referer . '): ' . $e -> getMessage(), JLog :: ERROR, self :: LOG);
70
			return NULL;
71
		}
72
	}
73

    
74
	private function getUserIp() {
75
		$ip = NULL;
76
		if (function_exists('apache_request_headers')) {
77
			$headers = apache_request_headers();
78
			if (array_key_exists('X-Forwarded-For', $headers))
79
				$ip = $headers['X-Forwarded-For'];
80
		}
81
		if ($ip === NULL)
82
			$ip = $_SERVER['REMOTE_ADDR'];
83
		return $ip;
84
	}
85
}
86

    
(6-6/9)