File: External.php

package info (click to toggle)
homer-api 5.0.6%2Bdfsg2-3.3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,612 kB
  • sloc: php: 8,259; javascript: 4,688; sql: 1,212; perl: 984; sh: 318; makefile: 69
file content (157 lines) | stat: -rw-r--r-- 4,938 bytes parent folder | download | duplicates (4)
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
<?php
/*
 * HOMER API
 * Homer's external auth
 *
 * Copyright (C) 2011-2014 Alexandr Dubovikov <alexandr.dubovikov@gmail.com>
 * Copyright (C) 2011-2012 Lorenzo Mangani <lorenzo.mangani@gmail.com>
 *
 * The Initial Developers of the Original Code are
 *
 * Alexandr Dubovikov <alexandr.dubovikov@gmail.com>
 * Lorenzo Mangani <lorenzo.mangani@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
*/

namespace Authentication;

defined( '_HOMEREXEC' ) or die( 'Restricted access' );

class External extends Authentication {
	
	private $encrypt = true;
	private $group_column = "gid";
	private $id_column = "uid";
	private $pass_column = "password";
	private $user_column = "username";
	private $user_table = "user";
	private $user_level = "userlevel"; 
	private $db;
	private $_instance = null;

	function __construct($utable = NULL)
	{
		if($utable != NULL) $this->user_table = $utable;
        }

	function logIn($param) {

        	$key =  "";
        	if(EXTERNAL_AUTH_REQUEST_TYPE == "cookie") 
        	{
			$key = $_COOKIE[EXTERNAL_AUTH_REQUEST_KEY];
        	}
        	else if(EXTERNAL_AUTH_REQUEST_TYPE == "get") 
        	{
			$key = $_GET[EXTERNAL_AUTH_REQUEST_KEY];
        	}
        	
        	$param = preg_replace('[KEY]', $key, EXTERNAL_AUTH_PARAM);        	
        	$ch = curl_init();
        	
        	if(EXTERNAL_AUTH_METHOD == "POST") 
        	{
			curl_setopt($ch, CURLOPT_POST, 1);
        		curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        		curl_setopt($ch, CURLOPT_URL, EXTERNAL_AUTH_URI);
        		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");        	
        	}
        	else {        	        	
			
		        curl_setopt($ch, CURLOPT_URL, EXTERNAL_AUTH_URI."?".$param);
		        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
		}
				
        	curl_setopt($ch, CURLOPT_HEADER, 0);
        	if(defined(EXTERNAL_AUTH_USERNAME) && EXTERNAL_AUTH_USERNAME  != "") 
        	{
		        curl_setopt($ch, CURLOPT_USERPWD, EXTERNAL_AUTH_USERNAME . ":" . EXTERNAL_AUTH_PASSWORD);
		}
		
        	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        	
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper(EXTERNAL_AUTH_METHOD));        	
	        $result = curl_exec($ch);
	        $info = curl_getinfo($ch);
        	curl_close($ch);
	        $data = json_decode($result);

	        if($info['http_code'] == EXTERNAL_AUTH_POSITIVE_REPLY && count($data) > 0)
	        {

        	        $_SESSION['loggedin']   = $data->uid;
	                $_SESSION['uid']        = $data->uid;
        	        $_SESSION['username']   = $data->username;
	                $_SESSION['gid']        = $data->gid;
	                $_SESSION['grp']        = $data->grp;
	                                                
	                $user['uid']     = $data->uid;
        	        $user['username'] = $data->username;
        	        $user['gid']        = $data->gid;   
	                $user['grp']      = $data->grp;   
        	        $user['firstname']  = $data->firstname;
        	        $user['lastname']   = $data->lastname; 
	                $user['email']      = $data->email;    
        	        $user['lastvisit']  = $data->lastvisit;
        	        
        	        $_SESSION['extra'] = $user;
        	        
	                return $user;
		                    
		} else{
	               $_SESSION['loggedin'] = "-1";
        	       return array();
	        }
        }
        
        //logout function 
        function logOut(){
                //$_SESSION['loggedin'] = '-1';
         	session_destroy();
         	$_SESSION = array(); 
                return;
	}

	function checkSession () {
	
        	if(!isset($_SESSION['loggedin'])) $_SESSION['loggedin'] = '-1';
		if($_SESSION['loggedin'] == "-1") 
		{
			$user = $this->logIn($param);		
			if(empty($user)) return false;		
			else return true;        
		}
		
	        return true;
	}
	
	function checkAdmin () {
		if(preg_match('/admins/',$_SESSION['grp'])) return true;
	        else return false;
	}
	
	function getUser() {

           if(!isset($_SESSION['loggedin'])) $_SESSION['loggedin'] = '-1';
           if($_SESSION['loggedin'] == "-1") return array();

           $user = $_SESSION['extra'];
           return $user;
        }

}
?>