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