MDL-11448 FirstClass Authentication does not hold settings and has cosmetic and info...
[moodle.git] / auth / fc / fcFPP.php
CommitLineData
03f5a0f8 1<?php
2/************************************************************************/
3/* fcFPP: Php class for FirstClass Flexible Provisining Protocol */
4/* ============================================================= */
5/* */
6/* Copyright (c) 2004 SKERIA Utveckling, Teknous */
7/* http://skeria.skelleftea.se */
8/* */
9/* Flexible Provisioning Protocol is a real-time, IP based protocol */
10/* which provides direct access to the scriptable remote administration */
11/* subsystem of the core FirstClass Server. Using FPP, it is possible to*/
12/* implement automated provisioning and administration systems for */
13/* FirstClass, avoiding the need for a point and click GUI. FPP can also*/
14/* be used to integrate FirstClass components into a larger unified */
15/* system. */
16/* */
17/* This program is free software. You can redistribute it and/or modify */
18/* it under the terms of the GNU General Public License as published by */
19/* the Free Software Foundation; either version 2 of the License. */
20/************************************************************************/
21/* Author: Torsten Anderson, torsten.anderson@skeria.skelleftea.se
22 */
23
24class fcFPP
139ebfdb 25{
03f5a0f8 26 var $_hostname; // hostname of FirstClass server we are connection to
27 var $_port; // port on which fpp is running
28 var $_conn = 0; // socket we are connecting on
29 var $_debug = FALSE; // set to true to see some debug info
139ebfdb 30
03f5a0f8 31 // class constructor
32 function fcFPP($host="localhost", $port="3333")
33 {
61240489 34 $this->_hostname = $host;
35 $this->_port = $port;
36 $this->_user = "";
139ebfdb 37 $this->_pwd = "";
03f5a0f8 38 }
139ebfdb 39
03f5a0f8 40 // open a connection to the FirstClass server
41 function open()
42 {
b9ddb2d5 43 if ($this->_debug) echo "Connecting to host ";
61240489 44 $host = $this->_hostname;
45 $port = $this->_port;
46
b9ddb2d5 47 if ($this->_debug) echo "[$host:$port]..";
61240489 48
49 // open the connection to the FirstClass server
50 $conn = fsockopen($host, $port, $errno, $errstr, 5);
b9ddb2d5 51 if (!$conn)
61240489 52 {
e5245c73 53 print_error('auth_fcconnfail','auth', '', array($errno, $errstr));
61240489 54 return false;
55 }
139ebfdb 56
61240489 57 // We are connected
b9ddb2d5 58 if ($this->_debug) echo "connected!";
139ebfdb 59
61240489 60 // Read connection message.
61 $line = fgets ($conn); //+0
62 $line = fgets ($conn); //new line
63
64 // store the connection in this class, so we can use it later
65 $this->_conn = & $conn;
66
67 return true;
03f5a0f8 68 }
69
70 // close any open connections
71 function close()
139ebfdb 72 {
61240489 73 // get the current connection
74 $conn = &$this->_conn;
75
76 // close it if it's open
b9ddb2d5 77 if ($conn)
61240489 78 {
79 fclose($conn);
80
81 // cleanup the variable
82 unset($this->_conn);
83 return true;
84 }
85 return;
03f5a0f8 86 }
139ebfdb 87
88
03f5a0f8 89 // Authenticate to the FirstClass server
90 function login($userid, $passwd)
91 {
61240489 92 // we did have a connection right?!
b9ddb2d5 93 if ($this->_conn)
61240489 94 {
95 # Send username
96 fputs($this->_conn,"$userid\r\n");
97
98 $line = fgets ($this->_conn); //new line
99 $line = fgets ($this->_conn); //+0
100 $line = fgets ($this->_conn); //new line
139ebfdb 101
61240489 102 # Send password
103 fputs($this->_conn,"$passwd\r\n");
104 $line = fgets ($this->_conn); //new line
105 $line = fgets ($this->_conn); //+0
106 $line = fgets ($this->_conn); //+0 or message
139ebfdb 107
b9ddb2d5 108 if ($this->_debug) echo $line;
139ebfdb 109
61240489 110 if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges
6dfb2234 111 $this->_user = $userid;
139ebfdb 112 $this->_pwd = $passwd;
113 return TRUE;
b9ddb2d5 114 } elseif (strpos($line, 'You are not allowed')) { // Denied access but a valid user and password
6dfb2234 115 // "Sorry. You are not allowed to login with the FPP interface"
61240489 116 return TRUE;
117 } else { //Invalid user or password
6dfb2234 118 return FALSE;
61240489 119 }
139ebfdb 120
61240489 121
122 }
123 return FALSE;
03f5a0f8 124 }
125
139ebfdb 126 // Get the list of groups the user is a member of
b9ddb2d5 127 function getGroups($userid) {
139ebfdb 128
61240489 129 $groups = array();
139ebfdb 130
131 // we must be logged in as a user with subadmin privileges
b9ddb2d5 132 if ($this->_conn AND $this->_user) {
61240489 133 # Send BA-command to get groups
134 fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r");
135 $line = "";
136 while (!$line) {
137 $line = trim(fgets ($this->_conn));
138 }
139 $n = 0;
140 while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") {
141 list( , , $groups[$n++]) = explode(" ",$line,3);
142 $line = trim(fgets ($this->_conn));
143 }
b9ddb2d5 144 if ($this->_debug) echo "getGroups:" . implode(",",$groups);
61240489 145 }
139ebfdb 146
61240489 147 return $groups;
03f5a0f8 148 }
149
150 // Check if the user is member of any of the groups.
151 // Return the list of groups the user is member of.
b9ddb2d5 152 function isMemberOf($userid, $groups) {
139ebfdb 153
61240489 154 $usergroups = array_map("strtolower",$this->getGroups($userid));
155 $groups = array_map("strtolower",$groups);
139ebfdb 156
61240489 157 $result = array_intersect($groups,$usergroups);
139ebfdb 158
b9ddb2d5 159 if ($this->_debug) echo "isMemberOf:" . implode(",",$result);
139ebfdb 160
61240489 161 return $result;
03f5a0f8 162
163 }
139ebfdb 164
b9ddb2d5 165 function getUserInfo($userid, $field) {
139ebfdb 166
61240489 167 $userinfo = "";
139ebfdb 168
b9ddb2d5 169 if ($this->_conn AND $this->_user) {
61240489 170 # Send BA-command to get data
171 fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r");
172 $line = "";
173 while (!$line) {
174 $line = trim(fgets ($this->_conn));
175 }
176 $n = 0;
177 while ($line AND !preg_match("/^\+0/", $line)) {
178 list( , , $userinfo) = explode(" ",$line,3);
179 $line = trim(fgets ($this->_conn));
180 }
b9ddb2d5 181 if ($this->_debug) echo "getUserInfo:" . $userinfo;
61240489 182 }
139ebfdb 183
61240489 184 return str_replace('\r',' ',trim($userinfo,'"'));
03f5a0f8 185
186 }
187
b9ddb2d5 188 function getResume($userid) {
139ebfdb 189
61240489 190 $resume = "";
191
192 $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes
139ebfdb 193
b9ddb2d5 194 if ($this->_conn AND $this->_user) {
61240489 195 # Send BA-command to get data
196 fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r");
197 $line = "";
198 while (!$line) {
03f5a0f8 199 $line = trim(fgets ($this->_conn));
61240489 200 }
201 $n = 0;
202 while ($line AND !preg_match("/^\+0/", $line)) {
203 $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 ')));
204 $line = trim(fgets ($this->_conn));
205 //print $line;
139ebfdb 206
61240489 207 }
b9ddb2d5 208 if ($this->_debug) echo "getResume:" . $resume;
61240489 209 }
139ebfdb 210
61240489 211 return $resume;
03f5a0f8 212
213 }
139ebfdb 214
215
03f5a0f8 216}
139ebfdb 217
03f5a0f8 218
6dfb2234 219?>