Another wretched adslashes problem. Roll on placeholder. Thanks to Howard Miller...
[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
25{
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
30
31 // class constructor
32 function fcFPP($host="localhost", $port="3333")
33 {
61240489 34 $this->_hostname = $host;
35 $this->_port = $port;
36 $this->_user = "";
37 $this->_pwd = "";
03f5a0f8 38 }
39
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 {
a9ad3633 53 print_error('auth_fcconnfail','auth', array($errno, $errstr));
61240489 54 return false;
55 }
56
57 // We are connected
b9ddb2d5 58 if ($this->_debug) echo "connected!";
61240489 59
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()
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 }
87
88
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
101
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
107
b9ddb2d5 108 if ($this->_debug) echo $line;
61240489 109
110 if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges
6dfb2234 111 $this->_user = $userid;
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 }
120
121
122 }
123 return FALSE;
03f5a0f8 124 }
125
126 // Get the list of groups the user is a member of
b9ddb2d5 127 function getGroups($userid) {
61240489 128
129 $groups = array();
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 }
146
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) {
61240489 153
154 $usergroups = array_map("strtolower",$this->getGroups($userid));
155 $groups = array_map("strtolower",$groups);
156
157 $result = array_intersect($groups,$usergroups);
158
b9ddb2d5 159 if ($this->_debug) echo "isMemberOf:" . implode(",",$result);
61240489 160
161 return $result;
03f5a0f8 162
163 }
164
b9ddb2d5 165 function getUserInfo($userid, $field) {
61240489 166
167 $userinfo = "";
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 }
183
184 return str_replace('\r',' ',trim($userinfo,'"'));
03f5a0f8 185
186 }
187
b9ddb2d5 188 function getResume($userid) {
61240489 189
190 $resume = "";
191
192 $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes
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;
206
207 }
b9ddb2d5 208 if ($this->_debug) echo "getResume:" . $resume;
61240489 209 }
210
211 return $resume;
03f5a0f8 212
213 }
214
215
216}
217
218
6dfb2234 219?>