2 // Logs into Hive from HarvestRoad and stores session ID in Moodle session
3 // Martin Dougiamas, Moodle
5 // Example CFG variables to make this work:
8 // $CFG->hiveprotocol = 'http';
9 // $CFG->hiveport = '80';
10 // $CFG->hivehost = 'turkey.harvestroad.com.au';
11 // $CFG->hivepath = '/cgi-bin/hive/hive.cgi';
12 // $CFG->hivecbid = '28';
14 function sso_user_login($username, $password) {
16 global $CFG, $SESSION;
18 include($CFG->libdir
.'/snoopy/Snoopy.class.inc');
20 if (empty($CFG->hivehost
)) {
21 return false; // Hive config variables not configured yet
28 $submit_url = $CFG->hiveprotocol
.'://'. $CFG->hivehost
.':'. $CFG->hiveport
.''. $CFG->hivepath
;
30 $submit_vars['HIVE_UNAME'] = $username;
31 $submit_vars['HIVE_UPASS'] = $password;
32 $submit_vars['HIVE_ENDUSER']= $username;
33 $submit_vars['HIVE_REQ'] = '2112';
34 $submit_vars['HIVE_REF'] = 'hin:hive@API Login 3';
35 $submit_vars['HIVE_RET'] = 'ORG';
36 $submit_vars['HIVE_REM'] = '';
37 $submit_vars['HIVE_PROD'] = '0';
38 $submit_vars['HIVE_USERIP'] = getremoteaddr();
41 /// We use POST to call Hive with a bit more security
42 $snoopy->submit($submit_url,$submit_vars);
44 /// Extract HIVE_SESSION from headers
46 foreach ($snoopy->headers
as $header) {
47 if (strpos($header, 'HIVE_SESSION=') !== false) {
48 $header = explode('HIVE_SESSION=', $header);
49 if (count($header) > 1) {
50 $cookie = explode(';', $header[1]);
52 $SESSION->HIVE_SESSION
= $cookie;
58 /// Try again with the guest username and password
60 $submit_vars['HIVE_UNAME'] = $CFG->hiveusername
;
61 $submit_vars['HIVE_UPASS'] = $CFG->hivepassword
;
62 $submit_vars['HIVE_ENDUSER']= $CFG->hiveusername
;
63 $snoopy->submit($submit_url,$submit_vars);
64 foreach ($snoopy->headers
as $header) {
65 if (strpos($header, 'HIVE_SESSION=') !== false) {
66 $header = explode('HIVE_SESSION=', $header);
67 if (count($header) > 1) {
68 $cookie = explode(';', $header[1]);
70 $SESSION->HIVE_SESSION
= $cookie;
76 return false; // No cookie found